blob: 4aaac9d82f24baa4044c15c54b2db0fe086a0bfc [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 Tarreaue6b98942007-10-29 01:09:36 +010052
53/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub9b2fd72020-12-08 14:13:11 +010054struct protocol proto_tcpv4 = {
Willy Tarreaub366c9a2020-12-08 14:54:20 +010055 .name = "tcpv4",
56
57 /* connection layer */
58 .ctrl_type = SOCK_STREAM,
59 .listen = tcp_bind_listener,
60 .enable = tcp_enable_listener,
61 .disable = tcp_disable_listener,
62 .add = default_add_listener,
63 .unbind = default_unbind_listener,
64 .suspend = default_suspend_listener,
65 .resume = default_resume_listener,
66 .accept_conn = sock_accept_conn,
Willy Tarreaude471c42020-12-08 15:50:56 +010067 .ctrl_init = sock_conn_ctrl_init,
68 .ctrl_close = sock_conn_ctrl_close,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010069 .connect = tcp_connect_server,
Willy Tarreau427c8462020-12-11 16:19:12 +010070 .drain = sock_drain,
Willy Tarreau472125b2020-12-11 17:02:50 +010071 .check_events = sock_check_events,
72 .ignore_events = sock_ignore_events,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010073
74 /* binding layer */
75 .rx_suspend = tcp_suspend_receiver,
76 .rx_resume = tcp_resume_receiver,
77
78 /* address family */
79 .fam = &proto_fam_inet4,
80
81 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020082 .proto_type = PROTO_TYPE_STREAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010083 .sock_type = SOCK_STREAM,
84 .sock_prot = IPPROTO_TCP,
85 .rx_enable = sock_enable,
86 .rx_disable = sock_disable,
87 .rx_unbind = sock_unbind,
88 .rx_listening = sock_accepting_conn,
89 .default_iocb = sock_accept_iocb,
90 .receivers = LIST_HEAD_INIT(proto_tcpv4.receivers),
91 .nb_receivers = 0,
Willy Tarreaue6b98942007-10-29 01:09:36 +010092};
93
Willy Tarreau0108d902018-11-25 19:14:37 +010094INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
95
Willy Tarreaue6b98942007-10-29 01:09:36 +010096/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub9b2fd72020-12-08 14:13:11 +010097struct protocol proto_tcpv6 = {
Willy Tarreaub366c9a2020-12-08 14:54:20 +010098 .name = "tcpv6",
99
100 /* connection layer */
101 .ctrl_type = SOCK_STREAM,
102 .listen = tcp_bind_listener,
103 .enable = tcp_enable_listener,
104 .disable = tcp_disable_listener,
105 .add = default_add_listener,
106 .unbind = default_unbind_listener,
107 .suspend = default_suspend_listener,
108 .resume = default_resume_listener,
109 .accept_conn = sock_accept_conn,
Willy Tarreaude471c42020-12-08 15:50:56 +0100110 .ctrl_init = sock_conn_ctrl_init,
111 .ctrl_close = sock_conn_ctrl_close,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100112 .connect = tcp_connect_server,
Willy Tarreau427c8462020-12-11 16:19:12 +0100113 .drain = sock_drain,
Willy Tarreau472125b2020-12-11 17:02:50 +0100114 .check_events = sock_check_events,
115 .ignore_events = sock_ignore_events,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100116
117 /* binding layer */
118 .rx_suspend = tcp_suspend_receiver,
119 .rx_resume = tcp_resume_receiver,
120
121 /* address family */
122 .fam = &proto_fam_inet6,
123
124 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200125 .proto_type = PROTO_TYPE_STREAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100126 .sock_type = SOCK_STREAM,
127 .sock_prot = IPPROTO_TCP,
128 .rx_enable = sock_enable,
129 .rx_disable = sock_disable,
130 .rx_unbind = sock_unbind,
131 .rx_listening = sock_accepting_conn,
132 .default_iocb = sock_accept_iocb,
133 .receivers = LIST_HEAD_INIT(proto_tcpv6.receivers),
134 .nb_receivers = 0,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100135};
136
Willy Tarreau0108d902018-11-25 19:14:37 +0100137INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
138
David du Colombier6f5ccb12011-03-10 22:26:24 +0100139/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100140 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
141 * - 0 : ignore remote address (may even be a NULL pointer)
142 * - 1 : use provided address
143 * - 2 : use provided port
144 * - 3 : use both
145 *
146 * The function supports multiple foreign binding methods :
147 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100148 * The second one can be used as a fallback for the first one.
149 * This function returns 0 when everything's OK, 1 if it could not bind, to the
150 * local address, 2 if it could not bind to the foreign address.
151 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100152int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100153{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100154 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100155 int foreign_ok = 0;
156 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100157 static THREAD_LOCAL int ip_transp_working = 1;
158 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200159
David du Colombier65c17962012-07-13 14:34:59 +0200160 switch (local->ss_family) {
161 case AF_INET:
162 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200163 /* This deserves some explanation. Some platforms will support
164 * multiple combinations of certain methods, so we try the
165 * supported ones until one succeeds.
166 */
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200167 if (sock_inet4_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200168 foreign_ok = 1;
169 else
170 ip_transp_working = 0;
171 }
172 break;
173 case AF_INET6:
174 if (flags && ip6_transp_working) {
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200175 if (sock_inet6_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200176 foreign_ok = 1;
177 else
178 ip6_transp_working = 0;
179 }
180 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100181 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200182
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100183 if (flags) {
184 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200185 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100186 switch (remote->ss_family) {
187 case AF_INET:
188 if (flags & 1)
189 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
190 if (flags & 2)
191 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
192 break;
193 case AF_INET6:
194 if (flags & 1)
195 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
196 if (flags & 2)
197 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
198 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100199 default:
200 /* we don't want to try to bind to an unknown address family */
201 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100202 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100203 }
204
Simon Hormande072bd2011-06-24 15:11:37 +0900205 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100206 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200207 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200208 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
209 if (ret < 0)
210 return 2;
211 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100212 }
213 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200214 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200215 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
216 if (ret < 0)
217 return 1;
218 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100219 }
220
221 if (!flags)
222 return 0;
223
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100224 if (!foreign_ok)
225 /* we could not bind to a foreign address */
226 return 2;
227
228 return 0;
229}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100230
Willy Tarreau9650f372009-08-16 14:02:45 +0200231/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200232 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200233 * to connection <conn> using (si->{target,dst}). A source address may be
234 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200235 * bind addresses are still determined locally (due to the possible need of a
236 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100237 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100238 * supported. The <data> parameter is a boolean indicating whether there are data
239 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200240 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
241 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100242 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200243 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
244 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200245 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200246 * Note that a pending send_proxy message accounts for data.
247 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200248 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200249 * - SF_ERR_NONE if everything's OK
250 * - SF_ERR_SRVTO if there are no more servers
251 * - SF_ERR_SRVCL if the connection was refused by the server
252 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
253 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
254 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100255 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100256 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200257 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100258 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200259 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100260
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200261int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200262{
263 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100264 struct server *srv;
265 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100266 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100267 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800268 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100269
Olivier Houchard637b6952018-11-23 14:23:07 +0100270 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100271
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100272 switch (obj_type(conn->target)) {
273 case OBJ_TYPE_PROXY:
Willy Tarreau88bc8002021-12-06 07:01:02 +0000274 be = __objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100275 srv = NULL;
276 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100277 case OBJ_TYPE_SERVER:
Willy Tarreau88bc8002021-12-06 07:01:02 +0000278 srv = __objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100279 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100280 /* Make sure we check that we have data before activating
281 * TFO, or we could trigger a kernel issue whereby after
282 * a successful connect() == 0, any subsequent connect()
283 * will return EINPROGRESS instead of EISCONN.
284 */
285 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
286 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
287 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100288 break;
289 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100290 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200291 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100292 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200293
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200294 if (!conn->dst) {
295 conn->flags |= CO_FL_ERROR;
296 return SF_ERR_INTERNAL;
297 }
298
Willy Tarreau18b7df72020-08-28 12:07:22 +0200299 fd = conn->handle.fd = sock_create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100300
301 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200302 qfprintf(stderr, "Cannot get a server socket.\n");
303
Willy Tarreau9ce70132014-01-24 16:08:19 +0100304 if (errno == ENFILE) {
305 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200306 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100307 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
308 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100309 }
310 else if (errno == EMFILE) {
311 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200312 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100313 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
314 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100315 }
316 else if (errno == ENOBUFS || errno == ENOMEM) {
317 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200318 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100319 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
320 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100321 }
322 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
323 conn->err_code = CO_ER_NOPROTO;
324 }
325 else
326 conn->err_code = CO_ER_SOCK_ERR;
327
Willy Tarreau9650f372009-08-16 14:02:45 +0200328 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100329 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200330 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200331 }
332
333 if (fd >= global.maxsock) {
334 /* do not log anything there, it's a normal condition when this option
335 * is used to serialize connections to a server !
336 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100337 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200338 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100339 conn->err_code = CO_ER_CONF_FDLIM;
340 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200341 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200342 }
343
344 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900345 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200346 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
347 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100348 conn->err_code = CO_ER_SOCK_ERR;
349 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200350 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200351 }
352
William Lallemandc03eb012018-11-27 12:02:37 +0100353 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
354 ha_alert("Cannot set CLOEXEC on client socket.\n");
355 close(fd);
356 conn->err_code = CO_ER_SOCK_ERR;
357 conn->flags |= CO_FL_ERROR;
358 return SF_ERR_INTERNAL;
359 }
360
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900361 if (be->options & PR_O_TCP_SRV_KA) {
Simon Hormande072bd2011-06-24 15:11:37 +0900362 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200363
Willy Tarreau52543212020-07-09 05:58:51 +0200364#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900365 if (be->srvtcpka_cnt)
366 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200367#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900368
Willy Tarreau52543212020-07-09 05:58:51 +0200369#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900370 if (be->srvtcpka_idle)
371 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200372#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900373
Willy Tarreau52543212020-07-09 05:58:51 +0200374#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900375 if (be->srvtcpka_intvl)
376 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200377#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900378 }
379
Willy Tarreau9650f372009-08-16 14:02:45 +0200380 /* allow specific binding :
381 * - server-specific at first
382 * - proxy-specific next
383 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100384 if (srv && srv->conn_src.opts & CO_SRC_BIND)
385 src = &srv->conn_src;
386 else if (be->conn_src.opts & CO_SRC_BIND)
387 src = &be->conn_src;
388 else
389 src = NULL;
390
391 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200392 int ret, flags = 0;
393
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200394 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100395 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100396 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200397 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200398 flags = 3;
399 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100400 case CO_SRC_TPROXY_CIP:
401 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200402 flags = 1;
403 break;
404 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200405 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200406
Willy Tarreau9650f372009-08-16 14:02:45 +0200407#ifdef SO_BINDTODEVICE
408 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100409 if (src->iface_name)
410 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200411#endif
412
Willy Tarreaua4380b42012-12-08 22:49:11 +0100413 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200414 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100415 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200416
417 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200418 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200419
420 do {
421 /* note: in case of retry, we may have to release a previously
422 * allocated port, hence this loop's construct.
423 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200424 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
425 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200426
427 if (!attempts)
428 break;
429 attempts--;
430
Willy Tarreaua4380b42012-12-08 22:49:11 +0100431 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100432 if (!fdinfo[fd].local_port) {
433 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200434 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100435 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200436
Willy Tarreaua4380b42012-12-08 22:49:11 +0100437 fdinfo[fd].port_range = src->sport_range;
438 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200439
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200440 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100441 if (ret != 0)
442 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200443 } while (ret != 0); /* binding NOK */
444 }
445 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000446#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100447 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200448 setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000449#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200450 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100451 if (ret != 0)
452 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200453 }
454
Willy Tarreaua4380b42012-12-08 22:49:11 +0100455 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200456 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
457 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 close(fd);
459
Willy Tarreau9650f372009-08-16 14:02:45 +0200460 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100461 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
462 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200463 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100464 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200465 be->id);
466 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100467 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
468 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100470 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200471 be->id);
472 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100473 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200474 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200475 }
476 }
477
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400478#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200479 /* disabling tcp quick ack now allows the first request to leave the
480 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100481 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200482 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200483 if (flags & (CONNECT_DELACK_ALWAYS) ||
484 ((flags & CONNECT_DELACK_SMART_CONNECT ||
485 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
486 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900487 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200488#endif
489
Willy Tarreau163d4622015-10-13 16:16:41 +0200490#ifdef TCP_USER_TIMEOUT
491 /* there is not much more we can do here when it fails, it's still minor */
492 if (srv && srv->tcp_ut)
493 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
494#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100495
496 if (use_fastopen) {
497#if defined(TCP_FASTOPEN_CONNECT)
498 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
499#endif
500 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100501 if (global.tune.server_sndbuf)
502 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
503
504 if (global.tune.server_rcvbuf)
505 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
506
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200507 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800508 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100509 if (errno == EINPROGRESS || errno == EALREADY) {
510 /* common case, let's wait for connect status */
511 conn->flags |= CO_FL_WAIT_L4_CONN;
512 }
513 else if (errno == EISCONN) {
514 /* should normally not happen but if so, indicates that it's OK */
515 conn->flags &= ~CO_FL_WAIT_L4_CONN;
516 }
517 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200518 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100519 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200520 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100521 conn->err_code = CO_ER_FREE_PORTS;
522 }
523 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200524 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100525 conn->err_code = CO_ER_ADDR_INUSE;
526 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200527
Willy Tarreaub1719512012-12-08 23:03:28 +0100528 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200529 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
530 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200531 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100532 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100533 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200534 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200535 } else if (errno == ETIMEDOUT) {
536 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200537 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
538 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200539 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100540 conn->err_code = CO_ER_SOCK_ERR;
541 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200542 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200543 } else {
544 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
545 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200546 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
547 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200548 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100549 conn->err_code = CO_ER_SOCK_ERR;
550 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200551 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200552 }
553 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100554 else {
555 /* connect() == 0, this is great! */
556 conn->flags &= ~CO_FL_WAIT_L4_CONN;
557 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200558
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100559 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200560
Willy Tarreauf79c8172013-10-21 16:30:56 +0200561 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200562 HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200563
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100564 if (conn->flags & CO_FL_WAIT_L4_CONN) {
565 fd_want_send(fd);
566 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200567 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100568 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200569
Willy Tarreaue7dff022015-04-03 01:14:29 +0200570 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200571}
572
Willy Tarreaue6b98942007-10-29 01:09:36 +0100573/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100574 * an error message in <errmsg> if the message is at most <errlen> bytes long
575 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
576 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100577 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
578 * was alright and that no message was returned. ERR_RETRYABLE means that an
579 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700580 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100581 * the meaning of the error, but just indicate that a message is present which
582 * should be displayed with the respective level. Last, ERR_ABORT indicates
583 * that it's pointless to try to start other listeners. No error message is
584 * returned if errlen is NULL.
585 */
586int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
587{
Willy Tarreaue6b98942007-10-29 01:09:36 +0100588 int fd, err;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200589 int ready;
Bjoern Jackeed174852021-01-12 19:24:43 +0100590 struct buffer *msg = alloc_trash_chunk();
Willy Tarreaue6b98942007-10-29 01:09:36 +0100591
Willy Tarreauad33acf2020-09-02 18:40:02 +0200592 err = ERR_NONE;
593
Willy Tarreau7c4c8302021-10-16 14:54:19 +0200594 if (!msg) {
595 if (errlen)
596 snprintf(errmsg, errlen, "out of memory");
597 return ERR_ALERT | ERR_FATAL;
598 }
599
Willy Tarreaue6b98942007-10-29 01:09:36 +0100600 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100601 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100602 *errmsg = 0;
603
604 if (listener->state != LI_ASSIGNED)
605 return ERR_NONE; /* already bound */
606
Willy Tarreauad33acf2020-09-02 18:40:02 +0200607 if (!(listener->rx.flags & RX_F_BOUND)) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100608 chunk_appendf(msg, "%sreceiving socket not bound", msg->data ? ", " : "");
Willy Tarreauad33acf2020-09-02 18:40:02 +0200609 goto tcp_return;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100610 }
611
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200612 fd = listener->rx.fd;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100613
614 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900615 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200616 else {
617 struct linger tmplinger;
618 socklen_t len = sizeof(tmplinger);
619 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
620 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
621 tmplinger.l_onoff = 0;
622 tmplinger.l_linger = 0;
623 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
624 sizeof(tmplinger));
625 }
626 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100627
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400628#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100629 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400630 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200631 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
Willy Tarreau3cf05cb2021-10-14 11:39:18 +0200632 chunk_appendf(msg, "%scannot set MSS to %d", msg->data ? ", " : "", listener->maxseg);
Willy Tarreaube1b9182009-06-14 18:48:19 +0200633 err |= ERR_WARN;
634 }
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200635 } else {
636 /* we may want to try to restore the default MSS if the socket was inherited */
Olivier Houchard153659f2017-04-05 22:39:56 +0200637 int tmpmaxseg = -1;
638 int defaultmss;
639 socklen_t len = sizeof(tmpmaxseg);
640
Willy Tarreau37159062020-08-27 07:48:42 +0200641 if (listener->rx.addr.ss_family == AF_INET)
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200642 defaultmss = sock_inet_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200643 else
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200644 defaultmss = sock_inet6_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200645
646 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100647 if (defaultmss > 0 &&
648 tmpmaxseg != defaultmss &&
649 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Willy Tarreau3cf05cb2021-10-14 11:39:18 +0200650 chunk_appendf(msg, "%scannot set MSS to %d", msg->data ? ", " : "", defaultmss);
Olivier Houchard153659f2017-04-05 22:39:56 +0200651 err |= ERR_WARN;
652 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200653 }
654#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100655#if defined(TCP_USER_TIMEOUT)
656 if (listener->tcp_ut) {
657 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
658 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100659 chunk_appendf(msg, "%scannot set TCP User Timeout", msg->data ? ", " : "");
Willy Tarreau2af207a2015-02-04 00:45:58 +0100660 err |= ERR_WARN;
661 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200662 } else
663 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
664 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100665#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200666#if defined(TCP_DEFER_ACCEPT)
667 if (listener->options & LI_O_DEF_ACCEPT) {
668 /* defer accept by up to one second */
669 int accept_delay = 1;
670 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100671 chunk_appendf(msg, "%scannot enable DEFER_ACCEPT", msg->data ? ", " : "");
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200672 err |= ERR_WARN;
673 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200674 } else
675 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
676 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200677#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200678#if defined(TCP_FASTOPEN)
679 if (listener->options & LI_O_TCP_FO) {
680 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100681 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200682 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100683 chunk_appendf(msg, "%scannot enable TCP_FASTOPEN", msg->data ? ", " : "");
Willy Tarreau1c862c52012-10-05 16:21:00 +0200684 err |= ERR_WARN;
685 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200686 } else {
687 socklen_t len;
688 int qlen;
689 len = sizeof(qlen);
690 /* Only disable fast open if it was enabled, we don't want
691 * the kernel to create a fast open queue if there's none.
692 */
693 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
694 qlen != 0) {
695 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
696 sizeof(zero)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100697 chunk_appendf(msg, "%scannot disable TCP_FASTOPEN", msg->data ? ", " : "");
Olivier Houchard153659f2017-04-05 22:39:56 +0200698 err |= ERR_WARN;
699 }
700 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200701 }
702#endif
Willy Tarreau3e12de22020-10-13 17:42:21 +0200703
Willy Tarreau7d053e42020-10-15 09:19:43 +0200704 ready = sock_accepting_conn(&listener->rx) > 0;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100705
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200706 if (!ready && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100707 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100708 err |= ERR_RETRYABLE | ERR_ALERT;
Bjoern Jackeed174852021-01-12 19:24:43 +0100709 chunk_appendf(msg, "%scannot listen to socket", msg->data ? ", " : "");
Willy Tarreaue6b98942007-10-29 01:09:36 +0100710 goto tcp_close_return;
711 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100712
David Carlier1eb595b2021-02-06 12:11:11 +0000713#if !defined(TCP_DEFER_ACCEPT) && defined(SO_ACCEPTFILTER)
714 /* the socket needs to listen first */
715 if (listener->options & LI_O_DEF_ACCEPT) {
716 struct accept_filter_arg accept;
717 memset(&accept, 0, sizeof(accept));
718 strcpy(accept.af_name, "dataready");
719 if (setsockopt(fd, SOL_SOCKET, SO_ACCEPTFILTER, &accept, sizeof(accept)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100720 chunk_appendf(msg, "%scannot enable ACCEPT_FILTER", msg->data ? ", " : "");
David Carlier1eb595b2021-02-06 12:11:11 +0000721 err |= ERR_WARN;
722 }
723 }
724#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400725#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200726 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900727 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200728 else
729 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200730#endif
731
Willy Tarreaue6b98942007-10-29 01:09:36 +0100732 /* the socket is ready */
Willy Tarreaua37b2442020-09-24 07:23:45 +0200733 listener_set_state(listener, LI_LISTEN);
Christopher Fauleta1000032020-10-07 11:14:47 +0200734 goto tcp_return;
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200735
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200736 tcp_close_return:
Bjoern Jackeed174852021-01-12 19:24:43 +0100737 free_trash_chunk(msg);
738 msg = NULL;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200739 close(fd);
Willy Tarreauad33acf2020-09-02 18:40:02 +0200740 tcp_return:
Bjoern Jackeed174852021-01-12 19:24:43 +0100741 if (msg && errlen && msg->data) {
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100742 char pn[INET6_ADDRSTRLEN];
743
Willy Tarreau37159062020-08-27 07:48:42 +0200744 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200745 snprintf(errmsg, errlen, "%s for [%s:%d]", msg->area, pn, get_host_port(&listener->rx.addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100746 }
Bjoern Jackeed174852021-01-12 19:24:43 +0100747 free_trash_chunk(msg);
748 msg = NULL;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100749 return err;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100750}
751
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200752/* Enable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100753 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200754 */
755static void tcp_enable_listener(struct listener *l)
756{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100757 fd_want_recv_safe(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200758}
759
760/* Disable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100761 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200762 */
763static void tcp_disable_listener(struct listener *l)
764{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100765 fd_stop_recv(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200766}
767
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200768/* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver
769 * was totally stopped, or > 0 if correctly suspended.
Willy Tarreau092d8652014-07-07 20:22:12 +0200770 */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200771static int tcp_suspend_receiver(struct receiver *rx)
Willy Tarreau092d8652014-07-07 20:22:12 +0200772{
Willy Tarreau2f6f3622020-10-14 10:50:41 +0200773 const struct sockaddr sa = { .sa_family = AF_UNSPEC };
Willy Tarreau3e12de22020-10-13 17:42:21 +0200774 int ret;
Willy Tarreau092d8652014-07-07 20:22:12 +0200775
Willy Tarreau59b5da42020-11-04 14:14:55 +0100776 /* we never do that with a shared FD otherwise we'd break it in the
777 * parent process and any possible subsequent worker inheriting it.
778 */
779 if (rx->flags & RX_F_INHERITED)
780 return -1;
781
Willy Tarreau8b6fc3d2020-10-13 16:34:19 +0200782 if (connect(rx->fd, &sa, sizeof(sa)) < 0)
783 goto check_already_done;
Willy Tarreau91c614d2020-10-08 16:51:09 +0200784
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200785 fd_stop_recv(rx->fd);
Willy Tarreau092d8652014-07-07 20:22:12 +0200786 return 1;
Willy Tarreau91c614d2020-10-08 16:51:09 +0200787
788 check_already_done:
789 /* in case one of the shutdown() above fails, it might be because we're
790 * dealing with a socket that is shared with other processes doing the
791 * same. Let's check if it's still accepting connections.
792 */
Willy Tarreau7d053e42020-10-15 09:19:43 +0200793 ret = sock_accepting_conn(rx);
Willy Tarreau3e12de22020-10-13 17:42:21 +0200794 if (ret <= 0) {
795 /* unrecoverable or paused by another process */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200796 fd_stop_recv(rx->fd);
Willy Tarreau3e12de22020-10-13 17:42:21 +0200797 return ret == 0;
Willy Tarreaue53608b2020-09-24 18:20:37 +0200798 }
Willy Tarreau91c614d2020-10-08 16:51:09 +0200799
Willy Tarreau3e12de22020-10-13 17:42:21 +0200800 /* still listening, that's not good */
Willy Tarreau91c614d2020-10-08 16:51:09 +0200801 return -1;
Willy Tarreau092d8652014-07-07 20:22:12 +0200802}
803
Willy Tarreau58e6b712020-09-25 19:40:31 +0200804/* Resume a receiver. Returns < 0 in case of failure, 0 if the receiver
805 * was totally stopped, or > 0 if correctly suspended.
806 */
807static int tcp_resume_receiver(struct receiver *rx)
808{
809 struct listener *l = LIST_ELEM(rx, struct listener *, rx);
810
811 if (rx->fd < 0)
812 return 0;
813
814 if (listen(rx->fd, listener_backlog(l)) == 0) {
815 fd_want_recv(l->rx.fd);
816 return 1;
817 }
818 return -1;
819}
820
Willy Tarreaue6b98942007-10-29 01:09:36 +0100821
822/*
823 * Local variables:
824 * c-indent-level: 8
825 * c-basic-offset: 8
826 * End:
827 */