blob: d872ed318215d10d0b5ca19a3737f3804235c360 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010023#include <sys/types.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010024
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040025#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020026#include <netinet/in.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040027
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020028#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/arg.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020030#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/fd.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020033#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020034#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020035#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/namespace.h>
38#include <haproxy/port_range.h>
39#include <haproxy/proto_tcp.h>
40#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020041#include <haproxy/proxy-t.h>
Willy Tarreau18b7df72020-08-28 12:07:22 +020042#include <haproxy/sock.h>
Willy Tarreauf1725582020-08-28 15:30:11 +020043#include <haproxy/sock_inet.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020044#include <haproxy/tools.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010045
Willy Tarreaue6b98942007-10-29 01:09:36 +010046
Emeric Bruncf20bf12010-10-22 16:06:11 +020047static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaucb66ea62020-09-25 17:12:32 +020048static int tcp_suspend_receiver(struct receiver *rx);
Willy Tarreau58e6b712020-09-25 19:40:31 +020049static int tcp_resume_receiver(struct receiver *rx);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020050static void tcp_enable_listener(struct listener *listener);
51static void tcp_disable_listener(struct listener *listener);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020052static void tcpv4_add_listener(struct listener *listener, int port);
53static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010054
55/* Note: must not be declared <const> as its list will be overwritten */
56static struct protocol proto_tcpv4 = {
57 .name = "tcpv4",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020058 .fam = &proto_fam_inet4,
Willy Tarreaua54553f2020-09-16 17:50:45 +020059 .ctrl_type = SOCK_STREAM,
Willy Tarreaue6b98942007-10-29 01:09:36 +010060 .sock_domain = AF_INET,
61 .sock_type = SOCK_STREAM,
62 .sock_prot = IPPROTO_TCP,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020063 .add = tcpv4_add_listener,
64 .listen = tcp_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020065 .enable = tcp_enable_listener,
66 .disable = tcp_disable_listener,
Willy Tarreau7b2febd2020-10-09 17:18:29 +020067 .unbind = default_unbind_listener,
Willy Tarreaue03204c2020-10-09 17:02:21 +020068 .suspend = default_suspend_listener,
69 .resume = default_resume_listener,
Willy Tarreauf1dc9f22020-10-15 09:21:31 +020070 .accept_conn = sock_accept_conn,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020071 .rx_enable = sock_enable,
72 .rx_disable = sock_disable,
Willy Tarreauf58b8db2020-10-09 16:32:08 +020073 .rx_unbind = sock_unbind,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020074 .rx_suspend = tcp_suspend_receiver,
Willy Tarreau58e6b712020-09-25 19:40:31 +020075 .rx_resume = tcp_resume_receiver,
Willy Tarreau7d053e42020-10-15 09:19:43 +020076 .rx_listening = sock_accepting_conn,
Willy Tarreaua74cb382020-10-15 21:29:49 +020077 .default_iocb = &sock_accept_iocb,
Willy Tarreau26d8c592012-05-07 18:12:14 +020078 .connect = tcp_connect_server,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020079 .receivers = LIST_HEAD_INIT(proto_tcpv4.receivers),
80 .nb_receivers = 0,
Willy Tarreaue6b98942007-10-29 01:09:36 +010081};
82
Willy Tarreau0108d902018-11-25 19:14:37 +010083INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
84
Willy Tarreaue6b98942007-10-29 01:09:36 +010085/* Note: must not be declared <const> as its list will be overwritten */
86static struct protocol proto_tcpv6 = {
87 .name = "tcpv6",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020088 .fam = &proto_fam_inet6,
Willy Tarreaua54553f2020-09-16 17:50:45 +020089 .ctrl_type = SOCK_STREAM,
Willy Tarreaue6b98942007-10-29 01:09:36 +010090 .sock_domain = AF_INET6,
91 .sock_type = SOCK_STREAM,
92 .sock_prot = IPPROTO_TCP,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020093 .add = tcpv6_add_listener,
94 .listen = tcp_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020095 .enable = tcp_enable_listener,
96 .disable = tcp_disable_listener,
Willy Tarreau7b2febd2020-10-09 17:18:29 +020097 .unbind = default_unbind_listener,
Willy Tarreaue03204c2020-10-09 17:02:21 +020098 .suspend = default_suspend_listener,
99 .resume = default_resume_listener,
Willy Tarreauf1dc9f22020-10-15 09:21:31 +0200100 .accept_conn = sock_accept_conn,
Willy Tarreau686fa3d2020-09-25 19:09:53 +0200101 .rx_enable = sock_enable,
102 .rx_disable = sock_disable,
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200103 .rx_unbind = sock_unbind,
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200104 .rx_suspend = tcp_suspend_receiver,
Willy Tarreau58e6b712020-09-25 19:40:31 +0200105 .rx_resume = tcp_resume_receiver,
Willy Tarreau7d053e42020-10-15 09:19:43 +0200106 .rx_listening = sock_accepting_conn,
Willy Tarreaua74cb382020-10-15 21:29:49 +0200107 .default_iocb = &sock_accept_iocb,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200108 .connect = tcp_connect_server,
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200109 .receivers = LIST_HEAD_INIT(proto_tcpv6.receivers),
110 .nb_receivers = 0,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100111};
112
Willy Tarreau0108d902018-11-25 19:14:37 +0100113INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
114
David du Colombier6f5ccb12011-03-10 22:26:24 +0100115/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100116 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
117 * - 0 : ignore remote address (may even be a NULL pointer)
118 * - 1 : use provided address
119 * - 2 : use provided port
120 * - 3 : use both
121 *
122 * The function supports multiple foreign binding methods :
123 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100124 * The second one can be used as a fallback for the first one.
125 * This function returns 0 when everything's OK, 1 if it could not bind, to the
126 * local address, 2 if it could not bind to the foreign address.
127 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100128int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100129{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100130 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100131 int foreign_ok = 0;
132 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100133 static THREAD_LOCAL int ip_transp_working = 1;
134 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200135
David du Colombier65c17962012-07-13 14:34:59 +0200136 switch (local->ss_family) {
137 case AF_INET:
138 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200139 /* This deserves some explanation. Some platforms will support
140 * multiple combinations of certain methods, so we try the
141 * supported ones until one succeeds.
142 */
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200143 if (sock_inet4_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200144 foreign_ok = 1;
145 else
146 ip_transp_working = 0;
147 }
148 break;
149 case AF_INET6:
150 if (flags && ip6_transp_working) {
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200151 if (sock_inet6_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200152 foreign_ok = 1;
153 else
154 ip6_transp_working = 0;
155 }
156 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100157 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200158
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100159 if (flags) {
160 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200161 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100162 switch (remote->ss_family) {
163 case AF_INET:
164 if (flags & 1)
165 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
166 if (flags & 2)
167 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
168 break;
169 case AF_INET6:
170 if (flags & 1)
171 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
172 if (flags & 2)
173 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
174 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100175 default:
176 /* we don't want to try to bind to an unknown address family */
177 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100178 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100179 }
180
Simon Hormande072bd2011-06-24 15:11:37 +0900181 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100182 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200183 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200184 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
185 if (ret < 0)
186 return 2;
187 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100188 }
189 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200190 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200191 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
192 if (ret < 0)
193 return 1;
194 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100195 }
196
197 if (!flags)
198 return 0;
199
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100200 if (!foreign_ok)
201 /* we could not bind to a foreign address */
202 return 2;
203
204 return 0;
205}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100206
Willy Tarreau9650f372009-08-16 14:02:45 +0200207/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200208 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200209 * to connection <conn> using (si->{target,dst}). A source address may be
210 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200211 * bind addresses are still determined locally (due to the possible need of a
212 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100213 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100214 * supported. The <data> parameter is a boolean indicating whether there are data
215 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200216 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
217 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100218 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200219 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
220 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200221 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200222 * Note that a pending send_proxy message accounts for data.
223 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200224 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200225 * - SF_ERR_NONE if everything's OK
226 * - SF_ERR_SRVTO if there are no more servers
227 * - SF_ERR_SRVCL if the connection was refused by the server
228 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
229 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
230 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100231 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100232 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200233 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100234 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200235 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100236
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200237int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200238{
239 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100240 struct server *srv;
241 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100242 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100243 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800244 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100245
Olivier Houchard637b6952018-11-23 14:23:07 +0100246 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100247
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100248 switch (obj_type(conn->target)) {
249 case OBJ_TYPE_PROXY:
250 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100251 srv = NULL;
252 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100253 case OBJ_TYPE_SERVER:
254 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100255 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100256 /* Make sure we check that we have data before activating
257 * TFO, or we could trigger a kernel issue whereby after
258 * a successful connect() == 0, any subsequent connect()
259 * will return EINPROGRESS instead of EISCONN.
260 */
261 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
262 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
263 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100264 break;
265 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100266 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200267 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100268 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200269
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200270 if (!conn->dst) {
271 conn->flags |= CO_FL_ERROR;
272 return SF_ERR_INTERNAL;
273 }
274
Willy Tarreau18b7df72020-08-28 12:07:22 +0200275 fd = conn->handle.fd = sock_create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100276
277 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200278 qfprintf(stderr, "Cannot get a server socket.\n");
279
Willy Tarreau9ce70132014-01-24 16:08:19 +0100280 if (errno == ENFILE) {
281 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200282 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100283 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
284 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100285 }
286 else if (errno == EMFILE) {
287 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200288 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100289 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
290 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100291 }
292 else if (errno == ENOBUFS || errno == ENOMEM) {
293 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200294 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100295 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
296 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100297 }
298 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
299 conn->err_code = CO_ER_NOPROTO;
300 }
301 else
302 conn->err_code = CO_ER_SOCK_ERR;
303
Willy Tarreau9650f372009-08-16 14:02:45 +0200304 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100305 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200306 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200307 }
308
309 if (fd >= global.maxsock) {
310 /* do not log anything there, it's a normal condition when this option
311 * is used to serialize connections to a server !
312 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100313 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200314 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100315 conn->err_code = CO_ER_CONF_FDLIM;
316 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200317 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200318 }
319
320 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900321 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200322 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
323 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100324 conn->err_code = CO_ER_SOCK_ERR;
325 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200326 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200327 }
328
William Lallemandc03eb012018-11-27 12:02:37 +0100329 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
330 ha_alert("Cannot set CLOEXEC on client socket.\n");
331 close(fd);
332 conn->err_code = CO_ER_SOCK_ERR;
333 conn->flags |= CO_FL_ERROR;
334 return SF_ERR_INTERNAL;
335 }
336
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900337 if (be->options & PR_O_TCP_SRV_KA) {
Simon Hormande072bd2011-06-24 15:11:37 +0900338 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200339
Willy Tarreau52543212020-07-09 05:58:51 +0200340#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900341 if (be->srvtcpka_cnt)
342 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200343#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900344
Willy Tarreau52543212020-07-09 05:58:51 +0200345#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900346 if (be->srvtcpka_idle)
347 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200348#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900349
Willy Tarreau52543212020-07-09 05:58:51 +0200350#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900351 if (be->srvtcpka_intvl)
352 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200353#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900354 }
355
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 /* allow specific binding :
357 * - server-specific at first
358 * - proxy-specific next
359 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100360 if (srv && srv->conn_src.opts & CO_SRC_BIND)
361 src = &srv->conn_src;
362 else if (be->conn_src.opts & CO_SRC_BIND)
363 src = &be->conn_src;
364 else
365 src = NULL;
366
367 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200368 int ret, flags = 0;
369
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200370 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100371 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100372 case CO_SRC_TPROXY_CLI:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200373 conn_set_private(conn);
Willy Tarreau387ebf82015-08-04 19:24:13 +0200374 /* fall through */
375 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200376 flags = 3;
377 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100378 case CO_SRC_TPROXY_CIP:
379 case CO_SRC_TPROXY_DYN:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200380 conn_set_private(conn);
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200381 flags = 1;
382 break;
383 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200384 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200385
Willy Tarreau9650f372009-08-16 14:02:45 +0200386#ifdef SO_BINDTODEVICE
387 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100388 if (src->iface_name)
389 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200390#endif
391
Willy Tarreaua4380b42012-12-08 22:49:11 +0100392 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200393 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100394 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200395
396 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200397 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200398
399 do {
400 /* note: in case of retry, we may have to release a previously
401 * allocated port, hence this loop's construct.
402 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200403 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
404 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200405
406 if (!attempts)
407 break;
408 attempts--;
409
Willy Tarreaua4380b42012-12-08 22:49:11 +0100410 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100411 if (!fdinfo[fd].local_port) {
412 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200413 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100414 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200415
Willy Tarreaua4380b42012-12-08 22:49:11 +0100416 fdinfo[fd].port_range = src->sport_range;
417 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200418
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200419 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100420 if (ret != 0)
421 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200422 } while (ret != 0); /* binding NOK */
423 }
424 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000425#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100426 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000427 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
428#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200429 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100430 if (ret != 0)
431 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200432 }
433
Willy Tarreaua4380b42012-12-08 22:49:11 +0100434 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200435 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
436 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200437 close(fd);
438
Willy Tarreau9650f372009-08-16 14:02:45 +0200439 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100440 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
441 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100443 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200444 be->id);
445 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100446 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
447 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200448 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100449 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200450 be->id);
451 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100452 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200453 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 }
455 }
456
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400457#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 /* disabling tcp quick ack now allows the first request to leave the
459 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100460 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200461 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200462 if (flags & (CONNECT_DELACK_ALWAYS) ||
463 ((flags & CONNECT_DELACK_SMART_CONNECT ||
464 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
465 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900466 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200467#endif
468
Willy Tarreau163d4622015-10-13 16:16:41 +0200469#ifdef TCP_USER_TIMEOUT
470 /* there is not much more we can do here when it fails, it's still minor */
471 if (srv && srv->tcp_ut)
472 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
473#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100474
475 if (use_fastopen) {
476#if defined(TCP_FASTOPEN_CONNECT)
477 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
478#endif
479 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100480 if (global.tune.server_sndbuf)
481 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
482
483 if (global.tune.server_rcvbuf)
484 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
485
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200486 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800487 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100488 if (errno == EINPROGRESS || errno == EALREADY) {
489 /* common case, let's wait for connect status */
490 conn->flags |= CO_FL_WAIT_L4_CONN;
491 }
492 else if (errno == EISCONN) {
493 /* should normally not happen but if so, indicates that it's OK */
494 conn->flags &= ~CO_FL_WAIT_L4_CONN;
495 }
496 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200497 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100498 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200499 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100500 conn->err_code = CO_ER_FREE_PORTS;
501 }
502 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200503 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100504 conn->err_code = CO_ER_ADDR_INUSE;
505 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200506
Willy Tarreaub1719512012-12-08 23:03:28 +0100507 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200508 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
509 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200510 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100511 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100512 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200513 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200514 } else if (errno == ETIMEDOUT) {
515 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200516 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
517 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200518 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100519 conn->err_code = CO_ER_SOCK_ERR;
520 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200521 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200522 } else {
523 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
524 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200525 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
526 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200527 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100528 conn->err_code = CO_ER_SOCK_ERR;
529 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200530 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200531 }
532 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100533 else {
534 /* connect() == 0, this is great! */
535 conn->flags &= ~CO_FL_WAIT_L4_CONN;
536 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200537
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100538 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200539
Willy Tarreauf79c8172013-10-21 16:30:56 +0200540 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100541 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200542
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100543 if (conn->flags & CO_FL_WAIT_L4_CONN) {
544 fd_want_send(fd);
545 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200546 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100547 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200548
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200549 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200550 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100551 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200552 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200553 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200554
Willy Tarreaue7dff022015-04-03 01:14:29 +0200555 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200556}
557
Willy Tarreaue6b98942007-10-29 01:09:36 +0100558/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100559 * an error message in <errmsg> if the message is at most <errlen> bytes long
560 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
561 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100562 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
563 * was alright and that no message was returned. ERR_RETRYABLE means that an
564 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700565 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100566 * the meaning of the error, but just indicate that a message is present which
567 * should be displayed with the respective level. Last, ERR_ABORT indicates
568 * that it's pointless to try to start other listeners. No error message is
569 * returned if errlen is NULL.
570 */
571int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
572{
Willy Tarreaue6b98942007-10-29 01:09:36 +0100573 int fd, err;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200574 int ready;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200575 char *msg = NULL;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100576
Willy Tarreauad33acf2020-09-02 18:40:02 +0200577 err = ERR_NONE;
578
Willy Tarreaue6b98942007-10-29 01:09:36 +0100579 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100580 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100581 *errmsg = 0;
582
583 if (listener->state != LI_ASSIGNED)
584 return ERR_NONE; /* already bound */
585
Willy Tarreauad33acf2020-09-02 18:40:02 +0200586 if (!(listener->rx.flags & RX_F_BOUND)) {
587 msg = "receiving socket not bound";
588 goto tcp_return;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100589 }
590
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200591 fd = listener->rx.fd;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100592
593 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900594 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200595 else {
596 struct linger tmplinger;
597 socklen_t len = sizeof(tmplinger);
598 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
599 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
600 tmplinger.l_onoff = 0;
601 tmplinger.l_linger = 0;
602 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
603 sizeof(tmplinger));
604 }
605 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100606
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400607#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100608 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400609 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200610 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
611 msg = "cannot set MSS";
612 err |= ERR_WARN;
613 }
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200614 } else {
615 /* we may want to try to restore the default MSS if the socket was inherited */
Olivier Houchard153659f2017-04-05 22:39:56 +0200616 int tmpmaxseg = -1;
617 int defaultmss;
618 socklen_t len = sizeof(tmpmaxseg);
619
Willy Tarreau37159062020-08-27 07:48:42 +0200620 if (listener->rx.addr.ss_family == AF_INET)
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200621 defaultmss = sock_inet_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200622 else
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200623 defaultmss = sock_inet6_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200624
625 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100626 if (defaultmss > 0 &&
627 tmpmaxseg != defaultmss &&
628 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200629 msg = "cannot set MSS";
630 err |= ERR_WARN;
631 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200632 }
633#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100634#if defined(TCP_USER_TIMEOUT)
635 if (listener->tcp_ut) {
636 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
637 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
638 msg = "cannot set TCP User Timeout";
639 err |= ERR_WARN;
640 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200641 } else
642 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
643 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100644#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200645#if defined(TCP_DEFER_ACCEPT)
646 if (listener->options & LI_O_DEF_ACCEPT) {
647 /* defer accept by up to one second */
648 int accept_delay = 1;
649 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
650 msg = "cannot enable DEFER_ACCEPT";
651 err |= ERR_WARN;
652 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200653 } else
654 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
655 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200656#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200657#if defined(TCP_FASTOPEN)
658 if (listener->options & LI_O_TCP_FO) {
659 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100660 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200661 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
662 msg = "cannot enable TCP_FASTOPEN";
663 err |= ERR_WARN;
664 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200665 } else {
666 socklen_t len;
667 int qlen;
668 len = sizeof(qlen);
669 /* Only disable fast open if it was enabled, we don't want
670 * the kernel to create a fast open queue if there's none.
671 */
672 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
673 qlen != 0) {
674 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
675 sizeof(zero)) == -1) {
676 msg = "cannot disable TCP_FASTOPEN";
677 err |= ERR_WARN;
678 }
679 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200680 }
681#endif
Willy Tarreau3e12de22020-10-13 17:42:21 +0200682
Willy Tarreau7d053e42020-10-15 09:19:43 +0200683 ready = sock_accepting_conn(&listener->rx) > 0;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100684
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200685 if (!ready && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100686 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100687 err |= ERR_RETRYABLE | ERR_ALERT;
688 msg = "cannot listen to socket";
689 goto tcp_close_return;
690 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100691
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400692#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200693 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900694 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200695 else
696 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200697#endif
698
Willy Tarreaue6b98942007-10-29 01:09:36 +0100699 /* the socket is ready */
Willy Tarreaua37b2442020-09-24 07:23:45 +0200700 listener_set_state(listener, LI_LISTEN);
Christopher Fauleta1000032020-10-07 11:14:47 +0200701 goto tcp_return;
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200702
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200703 tcp_close_return:
704 close(fd);
Willy Tarreauad33acf2020-09-02 18:40:02 +0200705 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100706 if (msg && errlen) {
707 char pn[INET6_ADDRSTRLEN];
708
Willy Tarreau37159062020-08-27 07:48:42 +0200709 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
710 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100711 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100712 return err;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100713}
714
Willy Tarreau32282382017-09-15 07:44:44 +0200715/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
716 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
717 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200718 *
719 * Must be called with proto_lock held.
720 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100721 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200722static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100723{
724 if (listener->state != LI_INIT)
725 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200726 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200727 listener->rx.proto = &proto_tcpv4;
Willy Tarreau37159062020-08-27 07:48:42 +0200728 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200729 LIST_ADDQ(&proto_tcpv4.receivers, &listener->rx.proto_list);
730 proto_tcpv4.nb_receivers++;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100731}
732
Willy Tarreau32282382017-09-15 07:44:44 +0200733/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
734 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
735 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200736 *
737 * Must be called with proto_lock held.
738 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100739 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200740static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100741{
742 if (listener->state != LI_INIT)
743 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200744 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200745 listener->rx.proto = &proto_tcpv6;
Willy Tarreau37159062020-08-27 07:48:42 +0200746 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200747 LIST_ADDQ(&proto_tcpv6.receivers, &listener->rx.proto_list);
748 proto_tcpv6.nb_receivers++;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100749}
750
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200751/* Enable receipt of incoming connections for listener <l>. The receiver must
752 * still be valid. Does nothing in early boot (needs fd_updt).
753 */
754static void tcp_enable_listener(struct listener *l)
755{
756 if (fd_updt)
757 fd_want_recv(l->rx.fd);
758}
759
760/* Disable receipt of incoming connections for listener <l>. The receiver must
761 * still be valid. Does nothing in early boot (needs fd_updt).
762 */
763static void tcp_disable_listener(struct listener *l)
764{
765 if (fd_updt)
766 fd_stop_recv(l->rx.fd);
767}
768
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200769/* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver
770 * was totally stopped, or > 0 if correctly suspended.
Willy Tarreau092d8652014-07-07 20:22:12 +0200771 */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200772static int tcp_suspend_receiver(struct receiver *rx)
Willy Tarreau092d8652014-07-07 20:22:12 +0200773{
Willy Tarreau2f6f3622020-10-14 10:50:41 +0200774 const struct sockaddr sa = { .sa_family = AF_UNSPEC };
Willy Tarreau3e12de22020-10-13 17:42:21 +0200775 int ret;
Willy Tarreau092d8652014-07-07 20:22:12 +0200776
Willy Tarreau8b6fc3d2020-10-13 16:34:19 +0200777 if (connect(rx->fd, &sa, sizeof(sa)) < 0)
778 goto check_already_done;
Willy Tarreau91c614d2020-10-08 16:51:09 +0200779
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200780 fd_stop_recv(rx->fd);
Willy Tarreau092d8652014-07-07 20:22:12 +0200781 return 1;
Willy Tarreau91c614d2020-10-08 16:51:09 +0200782
783 check_already_done:
784 /* in case one of the shutdown() above fails, it might be because we're
785 * dealing with a socket that is shared with other processes doing the
786 * same. Let's check if it's still accepting connections.
787 */
Willy Tarreau7d053e42020-10-15 09:19:43 +0200788 ret = sock_accepting_conn(rx);
Willy Tarreau3e12de22020-10-13 17:42:21 +0200789 if (ret <= 0) {
790 /* unrecoverable or paused by another process */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200791 fd_stop_recv(rx->fd);
Willy Tarreau3e12de22020-10-13 17:42:21 +0200792 return ret == 0;
Willy Tarreaue53608b2020-09-24 18:20:37 +0200793 }
Willy Tarreau91c614d2020-10-08 16:51:09 +0200794
Willy Tarreau3e12de22020-10-13 17:42:21 +0200795 /* still listening, that's not good */
Willy Tarreau91c614d2020-10-08 16:51:09 +0200796 return -1;
Willy Tarreau092d8652014-07-07 20:22:12 +0200797}
798
Willy Tarreau58e6b712020-09-25 19:40:31 +0200799/* Resume a receiver. Returns < 0 in case of failure, 0 if the receiver
800 * was totally stopped, or > 0 if correctly suspended.
801 */
802static int tcp_resume_receiver(struct receiver *rx)
803{
804 struct listener *l = LIST_ELEM(rx, struct listener *, rx);
805
806 if (rx->fd < 0)
807 return 0;
808
809 if (listen(rx->fd, listener_backlog(l)) == 0) {
810 fd_want_recv(l->rx.fd);
811 return 1;
812 }
813 return -1;
814}
815
Willy Tarreaue6b98942007-10-29 01:09:36 +0100816
817/*
818 * Local variables:
819 * c-indent-level: 8
820 * c-basic-offset: 8
821 * End:
822 */