blob: a160b1b87af9bbab19c3b674d28cc8b70ad4a20e [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>
Willy Tarreaue6b98942007-10-29 01:09:36 +010015#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <time.h>
19
20#include <sys/param.h>
21#include <sys/socket.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010022#include <sys/types.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010023
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040024#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020025#include <netinet/in.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040026
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/arg.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020029#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/fd.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020032#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020033#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020034#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020035#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <haproxy/namespace.h>
37#include <haproxy/port_range.h>
38#include <haproxy/proto_tcp.h>
39#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020040#include <haproxy/proxy-t.h>
Willy Tarreau18b7df72020-08-28 12:07:22 +020041#include <haproxy/sock.h>
Willy Tarreauf1725582020-08-28 15:30:11 +020042#include <haproxy/sock_inet.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020043#include <haproxy/tools.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010044
Willy Tarreaue6b98942007-10-29 01:09:36 +010045
Emeric Bruncf20bf12010-10-22 16:06:11 +020046static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaucb66ea62020-09-25 17:12:32 +020047static int tcp_suspend_receiver(struct receiver *rx);
Willy Tarreau58e6b712020-09-25 19:40:31 +020048static int tcp_resume_receiver(struct receiver *rx);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020049static void tcp_enable_listener(struct listener *listener);
50static void tcp_disable_listener(struct listener *listener);
Willy Tarreaue6b98942007-10-29 01:09:36 +010051
52/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub9b2fd72020-12-08 14:13:11 +010053struct protocol proto_tcpv4 = {
Willy Tarreaub366c9a2020-12-08 14:54:20 +010054 .name = "tcpv4",
55
56 /* connection layer */
57 .ctrl_type = SOCK_STREAM,
58 .listen = tcp_bind_listener,
59 .enable = tcp_enable_listener,
60 .disable = tcp_disable_listener,
61 .add = default_add_listener,
62 .unbind = default_unbind_listener,
63 .suspend = default_suspend_listener,
64 .resume = default_resume_listener,
65 .accept_conn = sock_accept_conn,
Willy Tarreaude471c42020-12-08 15:50:56 +010066 .ctrl_init = sock_conn_ctrl_init,
67 .ctrl_close = sock_conn_ctrl_close,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010068 .connect = tcp_connect_server,
Willy Tarreau427c8462020-12-11 16:19:12 +010069 .drain = sock_drain,
Willy Tarreau472125b2020-12-11 17:02:50 +010070 .check_events = sock_check_events,
71 .ignore_events = sock_ignore_events,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010072
73 /* binding layer */
74 .rx_suspend = tcp_suspend_receiver,
75 .rx_resume = tcp_resume_receiver,
76
77 /* address family */
78 .fam = &proto_fam_inet4,
79
80 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020081 .proto_type = PROTO_TYPE_STREAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010082 .sock_type = SOCK_STREAM,
83 .sock_prot = IPPROTO_TCP,
84 .rx_enable = sock_enable,
85 .rx_disable = sock_disable,
86 .rx_unbind = sock_unbind,
87 .rx_listening = sock_accepting_conn,
88 .default_iocb = sock_accept_iocb,
89 .receivers = LIST_HEAD_INIT(proto_tcpv4.receivers),
90 .nb_receivers = 0,
Willy Tarreaue6b98942007-10-29 01:09:36 +010091};
92
Willy Tarreau0108d902018-11-25 19:14:37 +010093INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
94
Willy Tarreaue6b98942007-10-29 01:09:36 +010095/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub9b2fd72020-12-08 14:13:11 +010096struct protocol proto_tcpv6 = {
Willy Tarreaub366c9a2020-12-08 14:54:20 +010097 .name = "tcpv6",
98
99 /* connection layer */
100 .ctrl_type = SOCK_STREAM,
101 .listen = tcp_bind_listener,
102 .enable = tcp_enable_listener,
103 .disable = tcp_disable_listener,
104 .add = default_add_listener,
105 .unbind = default_unbind_listener,
106 .suspend = default_suspend_listener,
107 .resume = default_resume_listener,
108 .accept_conn = sock_accept_conn,
Willy Tarreaude471c42020-12-08 15:50:56 +0100109 .ctrl_init = sock_conn_ctrl_init,
110 .ctrl_close = sock_conn_ctrl_close,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100111 .connect = tcp_connect_server,
Willy Tarreau427c8462020-12-11 16:19:12 +0100112 .drain = sock_drain,
Willy Tarreau472125b2020-12-11 17:02:50 +0100113 .check_events = sock_check_events,
114 .ignore_events = sock_ignore_events,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100115
116 /* binding layer */
117 .rx_suspend = tcp_suspend_receiver,
118 .rx_resume = tcp_resume_receiver,
119
120 /* address family */
121 .fam = &proto_fam_inet6,
122
123 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200124 .proto_type = PROTO_TYPE_STREAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100125 .sock_type = SOCK_STREAM,
126 .sock_prot = IPPROTO_TCP,
127 .rx_enable = sock_enable,
128 .rx_disable = sock_disable,
129 .rx_unbind = sock_unbind,
130 .rx_listening = sock_accepting_conn,
131 .default_iocb = sock_accept_iocb,
132 .receivers = LIST_HEAD_INIT(proto_tcpv6.receivers),
133 .nb_receivers = 0,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100134};
135
Willy Tarreau0108d902018-11-25 19:14:37 +0100136INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
137
David du Colombier6f5ccb12011-03-10 22:26:24 +0100138/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100139 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
140 * - 0 : ignore remote address (may even be a NULL pointer)
141 * - 1 : use provided address
142 * - 2 : use provided port
143 * - 3 : use both
144 *
145 * The function supports multiple foreign binding methods :
146 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100147 * The second one can be used as a fallback for the first one.
148 * This function returns 0 when everything's OK, 1 if it could not bind, to the
149 * local address, 2 if it could not bind to the foreign address.
150 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100151int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100152{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100153 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100154 int foreign_ok = 0;
155 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100156 static THREAD_LOCAL int ip_transp_working = 1;
157 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200158
David du Colombier65c17962012-07-13 14:34:59 +0200159 switch (local->ss_family) {
160 case AF_INET:
161 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200162 /* This deserves some explanation. Some platforms will support
163 * multiple combinations of certain methods, so we try the
164 * supported ones until one succeeds.
165 */
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200166 if (sock_inet4_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200167 foreign_ok = 1;
168 else
169 ip_transp_working = 0;
170 }
171 break;
172 case AF_INET6:
173 if (flags && ip6_transp_working) {
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200174 if (sock_inet6_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200175 foreign_ok = 1;
176 else
177 ip6_transp_working = 0;
178 }
179 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100180 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200181
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100182 if (flags) {
183 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200184 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100185 switch (remote->ss_family) {
186 case AF_INET:
187 if (flags & 1)
188 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
189 if (flags & 2)
190 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
191 break;
192 case AF_INET6:
193 if (flags & 1)
194 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
195 if (flags & 2)
196 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
197 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100198 default:
199 /* we don't want to try to bind to an unknown address family */
200 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100201 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100202 }
203
Simon Hormande072bd2011-06-24 15:11:37 +0900204 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100205 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200206 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200207 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
208 if (ret < 0)
209 return 2;
210 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100211 }
212 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200213 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200214 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
215 if (ret < 0)
216 return 1;
217 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100218 }
219
220 if (!flags)
221 return 0;
222
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100223 if (!foreign_ok)
224 /* we could not bind to a foreign address */
225 return 2;
226
227 return 0;
228}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100229
Willy Tarreau9650f372009-08-16 14:02:45 +0200230/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200231 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200232 * to connection <conn> using (si->{target,dst}). A source address may be
233 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200234 * bind addresses are still determined locally (due to the possible need of a
235 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100236 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100237 * supported. The <data> parameter is a boolean indicating whether there are data
238 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200239 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
240 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100241 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200242 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
243 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200244 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200245 * Note that a pending send_proxy message accounts for data.
246 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200247 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200248 * - SF_ERR_NONE if everything's OK
249 * - SF_ERR_SRVTO if there are no more servers
250 * - SF_ERR_SRVCL if the connection was refused by the server
251 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
252 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
253 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100254 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100255 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200256 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100257 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200258 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100259
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200260int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200261{
262 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100263 struct server *srv;
264 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100265 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100266 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800267 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100268
Olivier Houchard637b6952018-11-23 14:23:07 +0100269 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100270
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100271 switch (obj_type(conn->target)) {
272 case OBJ_TYPE_PROXY:
Willy Tarreau88bc8002021-12-06 07:01:02 +0000273 be = __objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100274 srv = NULL;
275 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100276 case OBJ_TYPE_SERVER:
Willy Tarreau88bc8002021-12-06 07:01:02 +0000277 srv = __objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100278 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100279 /* Make sure we check that we have data before activating
280 * TFO, or we could trigger a kernel issue whereby after
281 * a successful connect() == 0, any subsequent connect()
282 * will return EINPROGRESS instead of EISCONN.
283 */
284 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
285 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
286 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100287 break;
288 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100289 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200290 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100291 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200292
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200293 if (!conn->dst) {
294 conn->flags |= CO_FL_ERROR;
295 return SF_ERR_INTERNAL;
296 }
297
Willy Tarreau18b7df72020-08-28 12:07:22 +0200298 fd = conn->handle.fd = sock_create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100299
300 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200301 qfprintf(stderr, "Cannot get a server socket.\n");
302
Willy Tarreau9ce70132014-01-24 16:08:19 +0100303 if (errno == ENFILE) {
304 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200305 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100306 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
307 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100308 }
309 else if (errno == EMFILE) {
310 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200311 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100312 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
313 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100314 }
315 else if (errno == ENOBUFS || errno == ENOMEM) {
316 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200317 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100318 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
319 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100320 }
321 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
322 conn->err_code = CO_ER_NOPROTO;
323 }
324 else
325 conn->err_code = CO_ER_SOCK_ERR;
326
Willy Tarreau9650f372009-08-16 14:02:45 +0200327 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100328 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200329 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200330 }
331
332 if (fd >= global.maxsock) {
333 /* do not log anything there, it's a normal condition when this option
334 * is used to serialize connections to a server !
335 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100336 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200337 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100338 conn->err_code = CO_ER_CONF_FDLIM;
339 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200340 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200341 }
342
Willy Tarreau38247432022-04-26 10:24:14 +0200343 if (fd_set_nonblock(fd) == -1 ||
Simon Hormande072bd2011-06-24 15:11:37 +0900344 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200345 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
346 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100347 conn->err_code = CO_ER_SOCK_ERR;
348 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200349 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200350 }
351
Willy Tarreau38247432022-04-26 10:24:14 +0200352 if (master == 1 && fd_set_cloexec(fd) == -1) {
William Lallemandc03eb012018-11-27 12:02:37 +0100353 ha_alert("Cannot set CLOEXEC on client socket.\n");
354 close(fd);
355 conn->err_code = CO_ER_SOCK_ERR;
356 conn->flags |= CO_FL_ERROR;
357 return SF_ERR_INTERNAL;
358 }
359
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900360 if (be->options & PR_O_TCP_SRV_KA) {
Simon Hormande072bd2011-06-24 15:11:37 +0900361 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200362
Willy Tarreau52543212020-07-09 05:58:51 +0200363#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900364 if (be->srvtcpka_cnt)
365 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200366#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900367
Willy Tarreau52543212020-07-09 05:58:51 +0200368#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900369 if (be->srvtcpka_idle)
370 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200371#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900372
Willy Tarreau52543212020-07-09 05:58:51 +0200373#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900374 if (be->srvtcpka_intvl)
375 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200376#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900377 }
378
Willy Tarreau9650f372009-08-16 14:02:45 +0200379 /* allow specific binding :
380 * - server-specific at first
381 * - proxy-specific next
382 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100383 if (srv && srv->conn_src.opts & CO_SRC_BIND)
384 src = &srv->conn_src;
385 else if (be->conn_src.opts & CO_SRC_BIND)
386 src = &be->conn_src;
387 else
388 src = NULL;
389
390 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200391 int ret, flags = 0;
392
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200393 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100394 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100395 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200396 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200397 flags = 3;
398 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100399 case CO_SRC_TPROXY_CIP:
400 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200401 flags = 1;
402 break;
403 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200404 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200405
Willy Tarreau9650f372009-08-16 14:02:45 +0200406#ifdef SO_BINDTODEVICE
407 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100408 if (src->iface_name)
409 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200410#endif
411
Willy Tarreaua4380b42012-12-08 22:49:11 +0100412 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200413 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100414 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200415
416 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200417 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200418
419 do {
420 /* note: in case of retry, we may have to release a previously
421 * allocated port, hence this loop's construct.
422 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200423 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
424 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200425
426 if (!attempts)
427 break;
428 attempts--;
429
Willy Tarreaua4380b42012-12-08 22:49:11 +0100430 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100431 if (!fdinfo[fd].local_port) {
432 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200433 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100434 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200435
Willy Tarreaua4380b42012-12-08 22:49:11 +0100436 fdinfo[fd].port_range = src->sport_range;
437 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200438
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200439 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100440 if (ret != 0)
441 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 } while (ret != 0); /* binding NOK */
443 }
444 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000445#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100446 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200447 setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000448#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200449 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100450 if (ret != 0)
451 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 }
453
Willy Tarreaua4380b42012-12-08 22:49:11 +0100454 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200455 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
456 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200457 close(fd);
458
Willy Tarreau9650f372009-08-16 14:02:45 +0200459 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100460 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
461 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200462 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100463 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200464 be->id);
465 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100466 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
467 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200468 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100469 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200470 be->id);
471 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100472 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200473 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200474 }
475 }
476
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400477#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200478 /* disabling tcp quick ack now allows the first request to leave the
479 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100480 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200481 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200482 if (flags & (CONNECT_DELACK_ALWAYS) ||
483 ((flags & CONNECT_DELACK_SMART_CONNECT ||
484 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
485 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900486 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200487#endif
488
Willy Tarreau163d4622015-10-13 16:16:41 +0200489#ifdef TCP_USER_TIMEOUT
490 /* there is not much more we can do here when it fails, it's still minor */
491 if (srv && srv->tcp_ut)
492 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
493#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100494
495 if (use_fastopen) {
496#if defined(TCP_FASTOPEN_CONNECT)
497 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
498#endif
499 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100500 if (global.tune.server_sndbuf)
501 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
502
503 if (global.tune.server_rcvbuf)
504 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
505
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200506 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800507 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100508 if (errno == EINPROGRESS || errno == EALREADY) {
509 /* common case, let's wait for connect status */
510 conn->flags |= CO_FL_WAIT_L4_CONN;
511 }
512 else if (errno == EISCONN) {
513 /* should normally not happen but if so, indicates that it's OK */
514 conn->flags &= ~CO_FL_WAIT_L4_CONN;
515 }
Willy Tarreauacef5e22022-04-25 20:32:15 +0200516 else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200517 char *msg;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200518 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200519 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100520 conn->err_code = CO_ER_FREE_PORTS;
521 }
522 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200523 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100524 conn->err_code = CO_ER_ADDR_INUSE;
525 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200526
Willy Tarreaub1719512012-12-08 23:03:28 +0100527 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200528 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
529 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200530 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100531 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100532 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200533 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200534 } else if (errno == ETIMEDOUT) {
535 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200536 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
537 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200538 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100539 conn->err_code = CO_ER_SOCK_ERR;
540 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200541 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200542 } else {
543 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
544 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200545 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
546 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200547 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100548 conn->err_code = CO_ER_SOCK_ERR;
549 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200550 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200551 }
552 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100553 else {
554 /* connect() == 0, this is great! */
555 conn->flags &= ~CO_FL_WAIT_L4_CONN;
556 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200557
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100558 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200559
Willy Tarreauf79c8172013-10-21 16:30:56 +0200560 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200561 HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200562
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100563 if (conn->flags & CO_FL_WAIT_L4_CONN) {
564 fd_want_send(fd);
565 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200566 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100567 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200568
Willy Tarreaue7dff022015-04-03 01:14:29 +0200569 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200570}
571
Willy Tarreaue6b98942007-10-29 01:09:36 +0100572/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100573 * an error message in <errmsg> if the message is at most <errlen> bytes long
574 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
575 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100576 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
577 * was alright and that no message was returned. ERR_RETRYABLE means that an
578 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700579 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100580 * the meaning of the error, but just indicate that a message is present which
581 * should be displayed with the respective level. Last, ERR_ABORT indicates
582 * that it's pointless to try to start other listeners. No error message is
583 * returned if errlen is NULL.
584 */
585int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
586{
Willy Tarreaue6b98942007-10-29 01:09:36 +0100587 int fd, err;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200588 int ready;
Bjoern Jackeed174852021-01-12 19:24:43 +0100589 struct buffer *msg = alloc_trash_chunk();
Willy Tarreaue6b98942007-10-29 01:09:36 +0100590
Willy Tarreauad33acf2020-09-02 18:40:02 +0200591 err = ERR_NONE;
592
Willy Tarreau7c4c8302021-10-16 14:54:19 +0200593 if (!msg) {
594 if (errlen)
595 snprintf(errmsg, errlen, "out of memory");
596 return ERR_ALERT | ERR_FATAL;
597 }
598
Willy Tarreaue6b98942007-10-29 01:09:36 +0100599 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100600 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100601 *errmsg = 0;
602
603 if (listener->state != LI_ASSIGNED)
604 return ERR_NONE; /* already bound */
605
Willy Tarreauad33acf2020-09-02 18:40:02 +0200606 if (!(listener->rx.flags & RX_F_BOUND)) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100607 chunk_appendf(msg, "%sreceiving socket not bound", msg->data ? ", " : "");
Willy Tarreauad33acf2020-09-02 18:40:02 +0200608 goto tcp_return;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100609 }
610
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200611 fd = listener->rx.fd;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100612
613 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900614 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200615 else {
616 struct linger tmplinger;
617 socklen_t len = sizeof(tmplinger);
618 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
619 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
620 tmplinger.l_onoff = 0;
621 tmplinger.l_linger = 0;
622 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
623 sizeof(tmplinger));
624 }
625 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100626
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400627#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100628 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400629 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200630 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
Willy Tarreau3cf05cb2021-10-14 11:39:18 +0200631 chunk_appendf(msg, "%scannot set MSS to %d", msg->data ? ", " : "", listener->maxseg);
Willy Tarreaube1b9182009-06-14 18:48:19 +0200632 err |= ERR_WARN;
633 }
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200634 } else {
635 /* we may want to try to restore the default MSS if the socket was inherited */
Olivier Houchard153659f2017-04-05 22:39:56 +0200636 int tmpmaxseg = -1;
637 int defaultmss;
638 socklen_t len = sizeof(tmpmaxseg);
639
Willy Tarreau37159062020-08-27 07:48:42 +0200640 if (listener->rx.addr.ss_family == AF_INET)
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200641 defaultmss = sock_inet_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200642 else
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200643 defaultmss = sock_inet6_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200644
645 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100646 if (defaultmss > 0 &&
647 tmpmaxseg != defaultmss &&
648 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Willy Tarreau3cf05cb2021-10-14 11:39:18 +0200649 chunk_appendf(msg, "%scannot set MSS to %d", msg->data ? ", " : "", defaultmss);
Olivier Houchard153659f2017-04-05 22:39:56 +0200650 err |= ERR_WARN;
651 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200652 }
653#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100654#if defined(TCP_USER_TIMEOUT)
655 if (listener->tcp_ut) {
656 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
657 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100658 chunk_appendf(msg, "%scannot set TCP User Timeout", msg->data ? ", " : "");
Willy Tarreau2af207a2015-02-04 00:45:58 +0100659 err |= ERR_WARN;
660 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200661 } else
662 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
663 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100664#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200665#if defined(TCP_DEFER_ACCEPT)
666 if (listener->options & LI_O_DEF_ACCEPT) {
667 /* defer accept by up to one second */
668 int accept_delay = 1;
669 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100670 chunk_appendf(msg, "%scannot enable DEFER_ACCEPT", msg->data ? ", " : "");
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200671 err |= ERR_WARN;
672 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200673 } else
674 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
675 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200676#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200677#if defined(TCP_FASTOPEN)
678 if (listener->options & LI_O_TCP_FO) {
679 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100680 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200681 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100682 chunk_appendf(msg, "%scannot enable TCP_FASTOPEN", msg->data ? ", " : "");
Willy Tarreau1c862c52012-10-05 16:21:00 +0200683 err |= ERR_WARN;
684 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200685 } else {
686 socklen_t len;
687 int qlen;
688 len = sizeof(qlen);
689 /* Only disable fast open if it was enabled, we don't want
690 * the kernel to create a fast open queue if there's none.
691 */
692 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
693 qlen != 0) {
694 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
695 sizeof(zero)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100696 chunk_appendf(msg, "%scannot disable TCP_FASTOPEN", msg->data ? ", " : "");
Olivier Houchard153659f2017-04-05 22:39:56 +0200697 err |= ERR_WARN;
698 }
699 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200700 }
701#endif
Willy Tarreau3e12de22020-10-13 17:42:21 +0200702
Willy Tarreau7d053e42020-10-15 09:19:43 +0200703 ready = sock_accepting_conn(&listener->rx) > 0;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100704
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200705 if (!ready && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100706 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100707 err |= ERR_RETRYABLE | ERR_ALERT;
Bjoern Jackeed174852021-01-12 19:24:43 +0100708 chunk_appendf(msg, "%scannot listen to socket", msg->data ? ", " : "");
Willy Tarreaue6b98942007-10-29 01:09:36 +0100709 goto tcp_close_return;
710 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100711
David Carlier1eb595b2021-02-06 12:11:11 +0000712#if !defined(TCP_DEFER_ACCEPT) && defined(SO_ACCEPTFILTER)
713 /* the socket needs to listen first */
714 if (listener->options & LI_O_DEF_ACCEPT) {
715 struct accept_filter_arg accept;
716 memset(&accept, 0, sizeof(accept));
717 strcpy(accept.af_name, "dataready");
718 if (setsockopt(fd, SOL_SOCKET, SO_ACCEPTFILTER, &accept, sizeof(accept)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100719 chunk_appendf(msg, "%scannot enable ACCEPT_FILTER", msg->data ? ", " : "");
David Carlier1eb595b2021-02-06 12:11:11 +0000720 err |= ERR_WARN;
721 }
722 }
723#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400724#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200725 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900726 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200727 else
728 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200729#endif
730
Willy Tarreaue6b98942007-10-29 01:09:36 +0100731 /* the socket is ready */
Willy Tarreaua37b2442020-09-24 07:23:45 +0200732 listener_set_state(listener, LI_LISTEN);
Christopher Fauleta1000032020-10-07 11:14:47 +0200733 goto tcp_return;
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200734
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200735 tcp_close_return:
Bjoern Jackeed174852021-01-12 19:24:43 +0100736 free_trash_chunk(msg);
737 msg = NULL;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200738 close(fd);
Willy Tarreauad33acf2020-09-02 18:40:02 +0200739 tcp_return:
Bjoern Jackeed174852021-01-12 19:24:43 +0100740 if (msg && errlen && msg->data) {
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100741 char pn[INET6_ADDRSTRLEN];
742
Willy Tarreau37159062020-08-27 07:48:42 +0200743 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200744 snprintf(errmsg, errlen, "%s for [%s:%d]", msg->area, pn, get_host_port(&listener->rx.addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100745 }
Bjoern Jackeed174852021-01-12 19:24:43 +0100746 free_trash_chunk(msg);
747 msg = NULL;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100748 return err;
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
Willy Tarreaua4380b22020-11-04 13:59:04 +0100752 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200753 */
754static void tcp_enable_listener(struct listener *l)
755{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100756 fd_want_recv_safe(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200757}
758
759/* Disable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100760 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200761 */
762static void tcp_disable_listener(struct listener *l)
763{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100764 fd_stop_recv(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200765}
766
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200767/* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver
768 * was totally stopped, or > 0 if correctly suspended.
Willy Tarreau092d8652014-07-07 20:22:12 +0200769 */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200770static int tcp_suspend_receiver(struct receiver *rx)
Willy Tarreau092d8652014-07-07 20:22:12 +0200771{
Willy Tarreau2f6f3622020-10-14 10:50:41 +0200772 const struct sockaddr sa = { .sa_family = AF_UNSPEC };
Willy Tarreau3e12de22020-10-13 17:42:21 +0200773 int ret;
Willy Tarreau092d8652014-07-07 20:22:12 +0200774
Willy Tarreau59b5da42020-11-04 14:14:55 +0100775 /* we never do that with a shared FD otherwise we'd break it in the
776 * parent process and any possible subsequent worker inheriting it.
777 */
778 if (rx->flags & RX_F_INHERITED)
779 return -1;
780
Willy Tarreau8b6fc3d2020-10-13 16:34:19 +0200781 if (connect(rx->fd, &sa, sizeof(sa)) < 0)
782 goto check_already_done;
Willy Tarreau91c614d2020-10-08 16:51:09 +0200783
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200784 fd_stop_recv(rx->fd);
Willy Tarreau092d8652014-07-07 20:22:12 +0200785 return 1;
Willy Tarreau91c614d2020-10-08 16:51:09 +0200786
787 check_already_done:
788 /* in case one of the shutdown() above fails, it might be because we're
789 * dealing with a socket that is shared with other processes doing the
790 * same. Let's check if it's still accepting connections.
791 */
Willy Tarreau7d053e42020-10-15 09:19:43 +0200792 ret = sock_accepting_conn(rx);
Willy Tarreau3e12de22020-10-13 17:42:21 +0200793 if (ret <= 0) {
794 /* unrecoverable or paused by another process */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200795 fd_stop_recv(rx->fd);
Willy Tarreau3e12de22020-10-13 17:42:21 +0200796 return ret == 0;
Willy Tarreaue53608b2020-09-24 18:20:37 +0200797 }
Willy Tarreau91c614d2020-10-08 16:51:09 +0200798
Willy Tarreau3e12de22020-10-13 17:42:21 +0200799 /* still listening, that's not good */
Willy Tarreau91c614d2020-10-08 16:51:09 +0200800 return -1;
Willy Tarreau092d8652014-07-07 20:22:12 +0200801}
802
Willy Tarreau58e6b712020-09-25 19:40:31 +0200803/* Resume a receiver. Returns < 0 in case of failure, 0 if the receiver
804 * was totally stopped, or > 0 if correctly suspended.
805 */
806static int tcp_resume_receiver(struct receiver *rx)
807{
808 struct listener *l = LIST_ELEM(rx, struct listener *, rx);
809
810 if (rx->fd < 0)
811 return 0;
812
813 if (listen(rx->fd, listener_backlog(l)) == 0) {
814 fd_want_recv(l->rx.fd);
815 return 1;
816 }
817 return -1;
818}
819
Willy Tarreaue6b98942007-10-29 01:09:36 +0100820
821/*
822 * Local variables:
823 * c-indent-level: 8
824 * c-basic-offset: 8
825 * End:
826 */