blob: 45ce27f11e01724fbcfb95e9d39ab319efe543c0 [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 */
Willy Tarreau91b47262022-05-20 16:36:46 +020057 .xprt_type = PROTO_TYPE_STREAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +010058 .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 Tarreau785b89f2023-04-22 15:09:07 +020091#ifdef SO_REUSEPORT
92 .flags = PROTO_F_REUSEPORT_SUPPORTED,
93#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +010094};
95
Willy Tarreau0108d902018-11-25 19:14:37 +010096INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
97
Willy Tarreaue6b98942007-10-29 01:09:36 +010098/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub9b2fd72020-12-08 14:13:11 +010099struct protocol proto_tcpv6 = {
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100100 .name = "tcpv6",
101
102 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +0200103 .xprt_type = PROTO_TYPE_STREAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100104 .listen = tcp_bind_listener,
105 .enable = tcp_enable_listener,
106 .disable = tcp_disable_listener,
107 .add = default_add_listener,
108 .unbind = default_unbind_listener,
109 .suspend = default_suspend_listener,
110 .resume = default_resume_listener,
111 .accept_conn = sock_accept_conn,
Willy Tarreaude471c42020-12-08 15:50:56 +0100112 .ctrl_init = sock_conn_ctrl_init,
113 .ctrl_close = sock_conn_ctrl_close,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100114 .connect = tcp_connect_server,
Willy Tarreau427c8462020-12-11 16:19:12 +0100115 .drain = sock_drain,
Willy Tarreau472125b2020-12-11 17:02:50 +0100116 .check_events = sock_check_events,
117 .ignore_events = sock_ignore_events,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100118
119 /* binding layer */
120 .rx_suspend = tcp_suspend_receiver,
121 .rx_resume = tcp_resume_receiver,
122
123 /* address family */
124 .fam = &proto_fam_inet6,
125
126 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200127 .proto_type = PROTO_TYPE_STREAM,
Willy Tarreaub366c9a2020-12-08 14:54:20 +0100128 .sock_type = SOCK_STREAM,
129 .sock_prot = IPPROTO_TCP,
130 .rx_enable = sock_enable,
131 .rx_disable = sock_disable,
132 .rx_unbind = sock_unbind,
133 .rx_listening = sock_accepting_conn,
134 .default_iocb = sock_accept_iocb,
135 .receivers = LIST_HEAD_INIT(proto_tcpv6.receivers),
136 .nb_receivers = 0,
Willy Tarreau785b89f2023-04-22 15:09:07 +0200137#ifdef SO_REUSEPORT
138 .flags = PROTO_F_REUSEPORT_SUPPORTED,
139#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100140};
141
Willy Tarreau0108d902018-11-25 19:14:37 +0100142INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
143
David du Colombier6f5ccb12011-03-10 22:26:24 +0100144/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100145 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
146 * - 0 : ignore remote address (may even be a NULL pointer)
147 * - 1 : use provided address
148 * - 2 : use provided port
149 * - 3 : use both
150 *
151 * The function supports multiple foreign binding methods :
152 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100153 * The second one can be used as a fallback for the first one.
154 * This function returns 0 when everything's OK, 1 if it could not bind, to the
155 * local address, 2 if it could not bind to the foreign address.
156 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100157int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100158{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100159 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100160 int foreign_ok = 0;
161 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100162 static THREAD_LOCAL int ip_transp_working = 1;
163 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200164
David du Colombier65c17962012-07-13 14:34:59 +0200165 switch (local->ss_family) {
166 case AF_INET:
167 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200168 /* This deserves some explanation. Some platforms will support
169 * multiple combinations of certain methods, so we try the
170 * supported ones until one succeeds.
171 */
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200172 if (sock_inet4_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200173 foreign_ok = 1;
174 else
175 ip_transp_working = 0;
176 }
177 break;
178 case AF_INET6:
179 if (flags && ip6_transp_working) {
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200180 if (sock_inet6_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200181 foreign_ok = 1;
182 else
183 ip6_transp_working = 0;
184 }
185 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100186 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200187
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100188 if (flags) {
189 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200190 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100191 switch (remote->ss_family) {
192 case AF_INET:
193 if (flags & 1)
194 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
195 if (flags & 2)
196 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
197 break;
198 case AF_INET6:
199 if (flags & 1)
200 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
201 if (flags & 2)
202 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
203 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100204 default:
205 /* we don't want to try to bind to an unknown address family */
206 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100207 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100208 }
209
Simon Hormande072bd2011-06-24 15:11:37 +0900210 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100211 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200212 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200213 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
214 if (ret < 0)
215 return 2;
216 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100217 }
218 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200219 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200220 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
221 if (ret < 0)
222 return 1;
223 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100224 }
225
226 if (!flags)
227 return 0;
228
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100229 if (!foreign_ok)
230 /* we could not bind to a foreign address */
231 return 2;
232
233 return 0;
234}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100235
Willy Tarreau9650f372009-08-16 14:02:45 +0200236/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200237 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200238 * to connection <conn> using (si->{target,dst}). A source address may be
239 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200240 * bind addresses are still determined locally (due to the possible need of a
241 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100242 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100243 * supported. The <data> parameter is a boolean indicating whether there are data
244 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200245 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
246 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100247 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200248 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
249 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200250 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200251 * Note that a pending send_proxy message accounts for data.
252 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200253 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200254 * - SF_ERR_NONE if everything's OK
255 * - SF_ERR_SRVTO if there are no more servers
256 * - SF_ERR_SRVCL if the connection was refused by the server
257 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
258 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
259 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100260 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100261 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200262 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100263 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200264 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100265
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200266int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200267{
268 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100269 struct server *srv;
270 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100271 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100272 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800273 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100274
Willy Tarreau158b6cf2022-05-02 17:45:12 +0200275 BUG_ON(!conn->dst);
276
Olivier Houchard637b6952018-11-23 14:23:07 +0100277 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100278
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100279 switch (obj_type(conn->target)) {
280 case OBJ_TYPE_PROXY:
Willy Tarreau88bc8002021-12-06 07:01:02 +0000281 be = __objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100282 srv = NULL;
283 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100284 case OBJ_TYPE_SERVER:
Willy Tarreau88bc8002021-12-06 07:01:02 +0000285 srv = __objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100286 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100287 /* Make sure we check that we have data before activating
288 * TFO, or we could trigger a kernel issue whereby after
289 * a successful connect() == 0, any subsequent connect()
290 * will return EINPROGRESS instead of EISCONN.
291 */
292 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
293 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
294 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100295 break;
296 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100297 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200298 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100299 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200300
Willy Tarreau18b7df72020-08-28 12:07:22 +0200301 fd = conn->handle.fd = sock_create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100302
303 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200304 qfprintf(stderr, "Cannot get a server socket.\n");
305
Willy Tarreau9ce70132014-01-24 16:08:19 +0100306 if (errno == ENFILE) {
307 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200308 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100309 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
310 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100311 }
312 else if (errno == EMFILE) {
313 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200314 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100315 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
316 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100317 }
318 else if (errno == ENOBUFS || errno == ENOMEM) {
319 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200320 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100321 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
322 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100323 }
324 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
325 conn->err_code = CO_ER_NOPROTO;
326 }
327 else
328 conn->err_code = CO_ER_SOCK_ERR;
329
Willy Tarreau9650f372009-08-16 14:02:45 +0200330 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100331 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200332 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200333 }
334
335 if (fd >= global.maxsock) {
336 /* do not log anything there, it's a normal condition when this option
337 * is used to serialize connections to a server !
338 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100339 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200340 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100341 conn->err_code = CO_ER_CONF_FDLIM;
342 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200343 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200344 }
345
Willy Tarreau38247432022-04-26 10:24:14 +0200346 if (fd_set_nonblock(fd) == -1 ||
Simon Hormande072bd2011-06-24 15:11:37 +0900347 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200348 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
349 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100350 conn->err_code = CO_ER_SOCK_ERR;
351 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200352 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200353 }
354
Willy Tarreau38247432022-04-26 10:24:14 +0200355 if (master == 1 && fd_set_cloexec(fd) == -1) {
William Lallemandc03eb012018-11-27 12:02:37 +0100356 ha_alert("Cannot set CLOEXEC on client socket.\n");
357 close(fd);
358 conn->err_code = CO_ER_SOCK_ERR;
359 conn->flags |= CO_FL_ERROR;
360 return SF_ERR_INTERNAL;
361 }
362
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900363 if (be->options & PR_O_TCP_SRV_KA) {
Simon Hormande072bd2011-06-24 15:11:37 +0900364 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200365
Willy Tarreau52543212020-07-09 05:58:51 +0200366#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900367 if (be->srvtcpka_cnt)
368 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200369#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900370
Willy Tarreau52543212020-07-09 05:58:51 +0200371#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900372 if (be->srvtcpka_idle)
373 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200374#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900375
Willy Tarreau52543212020-07-09 05:58:51 +0200376#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900377 if (be->srvtcpka_intvl)
378 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200379#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900380 }
381
Willy Tarreau9650f372009-08-16 14:02:45 +0200382 /* allow specific binding :
383 * - server-specific at first
384 * - proxy-specific next
385 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100386 if (srv && srv->conn_src.opts & CO_SRC_BIND)
387 src = &srv->conn_src;
388 else if (be->conn_src.opts & CO_SRC_BIND)
389 src = &be->conn_src;
390 else
391 src = NULL;
392
393 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200394 int ret, flags = 0;
395
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200396 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100397 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100398 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200399 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200400 flags = 3;
401 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100402 case CO_SRC_TPROXY_CIP:
403 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200404 flags = 1;
405 break;
406 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200407 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200408
Willy Tarreau9650f372009-08-16 14:02:45 +0200409#ifdef SO_BINDTODEVICE
410 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100411 if (src->iface_name)
412 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200413#endif
414
Willy Tarreaua4380b42012-12-08 22:49:11 +0100415 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200416 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100417 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200418
419 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200420 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200421
422 do {
423 /* note: in case of retry, we may have to release a previously
424 * allocated port, hence this loop's construct.
425 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200426 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
427 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200428
429 if (!attempts)
430 break;
431 attempts--;
432
Willy Tarreaua4380b42012-12-08 22:49:11 +0100433 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100434 if (!fdinfo[fd].local_port) {
435 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200436 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100437 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200438
Willy Tarreaua4380b42012-12-08 22:49:11 +0100439 fdinfo[fd].port_range = src->sport_range;
440 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200441
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200442 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100443 if (ret != 0)
444 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 } while (ret != 0); /* binding NOK */
446 }
447 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000448#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100449 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200450 setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000451#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200452 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100453 if (ret != 0)
454 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200455 }
456
Willy Tarreaua4380b42012-12-08 22:49:11 +0100457 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200458 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
459 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200460 close(fd);
461
Willy Tarreau9650f372009-08-16 14:02:45 +0200462 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100463 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
464 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200465 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100466 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200467 be->id);
468 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100469 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
470 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200471 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100472 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200473 be->id);
474 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100475 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200476 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200477 }
478 }
479
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400480#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200481 /* disabling tcp quick ack now allows the first request to leave the
482 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100483 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200484 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200485 if (flags & (CONNECT_DELACK_ALWAYS) ||
486 ((flags & CONNECT_DELACK_SMART_CONNECT ||
487 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
488 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900489 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200490#endif
491
Willy Tarreau163d4622015-10-13 16:16:41 +0200492#ifdef TCP_USER_TIMEOUT
493 /* there is not much more we can do here when it fails, it's still minor */
494 if (srv && srv->tcp_ut)
495 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
496#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100497
498 if (use_fastopen) {
499#if defined(TCP_FASTOPEN_CONNECT)
500 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
501#endif
502 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100503 if (global.tune.server_sndbuf)
504 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
505
506 if (global.tune.server_rcvbuf)
507 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
508
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200509 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800510 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100511 if (errno == EINPROGRESS || errno == EALREADY) {
512 /* common case, let's wait for connect status */
513 conn->flags |= CO_FL_WAIT_L4_CONN;
514 }
515 else if (errno == EISCONN) {
516 /* should normally not happen but if so, indicates that it's OK */
517 conn->flags &= ~CO_FL_WAIT_L4_CONN;
518 }
Willy Tarreauacef5e22022-04-25 20:32:15 +0200519 else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200520 char *msg;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200521 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200522 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100523 conn->err_code = CO_ER_FREE_PORTS;
524 }
525 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200526 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100527 conn->err_code = CO_ER_ADDR_INUSE;
528 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200529
Willy Tarreaub1719512012-12-08 23:03:28 +0100530 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200531 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
532 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200533 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100534 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100535 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200536 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200537 } else if (errno == ETIMEDOUT) {
538 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200539 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
540 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200541 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100542 conn->err_code = CO_ER_SOCK_ERR;
543 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200544 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200545 } else {
546 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
547 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200548 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
549 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200550 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100551 conn->err_code = CO_ER_SOCK_ERR;
552 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200553 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200554 }
555 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100556 else {
557 /* connect() == 0, this is great! */
558 conn->flags &= ~CO_FL_WAIT_L4_CONN;
559 }
Willy Tarreau9650f372009-08-16 14:02:45 +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 Tarreau09e266e2023-02-27 16:40:54 +0100612 if (listener->rx.flags & RX_F_MUST_DUP)
613 goto done;
614
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200615 fd = listener->rx.fd;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100616
Willy Tarreaucfb7c2f2023-01-12 19:37:07 +0100617 if (listener->bind_conf->options & BC_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900618 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200619 else {
620 struct linger tmplinger;
621 socklen_t len = sizeof(tmplinger);
622 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
623 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
624 tmplinger.l_onoff = 0;
625 tmplinger.l_linger = 0;
626 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
627 sizeof(tmplinger));
628 }
629 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100630
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400631#if defined(TCP_MAXSEG)
Willy Tarreauee378162023-01-12 18:42:49 +0100632 if (listener->bind_conf->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400633 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreauee378162023-01-12 18:42:49 +0100634 &listener->bind_conf->maxseg, sizeof(listener->bind_conf->maxseg)) == -1) {
635 chunk_appendf(msg, "%scannot set MSS to %d", msg->data ? ", " : "", listener->bind_conf->maxseg);
Willy Tarreaube1b9182009-06-14 18:48:19 +0200636 err |= ERR_WARN;
637 }
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200638 } else {
639 /* we may want to try to restore the default MSS if the socket was inherited */
Olivier Houchard153659f2017-04-05 22:39:56 +0200640 int tmpmaxseg = -1;
641 int defaultmss;
642 socklen_t len = sizeof(tmpmaxseg);
643
Willy Tarreau37159062020-08-27 07:48:42 +0200644 if (listener->rx.addr.ss_family == AF_INET)
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200645 defaultmss = sock_inet_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200646 else
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200647 defaultmss = sock_inet6_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200648
649 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100650 if (defaultmss > 0 &&
651 tmpmaxseg != defaultmss &&
652 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Willy Tarreau3cf05cb2021-10-14 11:39:18 +0200653 chunk_appendf(msg, "%scannot set MSS to %d", msg->data ? ", " : "", defaultmss);
Olivier Houchard153659f2017-04-05 22:39:56 +0200654 err |= ERR_WARN;
655 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200656 }
657#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100658#if defined(TCP_USER_TIMEOUT)
Willy Tarreauee378162023-01-12 18:42:49 +0100659 if (listener->bind_conf->tcp_ut) {
Willy Tarreau2af207a2015-02-04 00:45:58 +0100660 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
Willy Tarreauee378162023-01-12 18:42:49 +0100661 &listener->bind_conf->tcp_ut, sizeof(listener->bind_conf->tcp_ut)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100662 chunk_appendf(msg, "%scannot set TCP User Timeout", msg->data ? ", " : "");
Willy Tarreau2af207a2015-02-04 00:45:58 +0100663 err |= ERR_WARN;
664 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200665 } else
666 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
667 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100668#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200669#if defined(TCP_DEFER_ACCEPT)
Willy Tarreaud9b4d212023-01-12 19:42:48 +0100670 if (listener->bind_conf->options & BC_O_DEF_ACCEPT) {
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200671 /* defer accept by up to one second */
672 int accept_delay = 1;
673 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100674 chunk_appendf(msg, "%scannot enable DEFER_ACCEPT", msg->data ? ", " : "");
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200675 err |= ERR_WARN;
676 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200677 } else
678 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
679 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200680#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200681#if defined(TCP_FASTOPEN)
Willy Tarreauc492f1b2023-01-12 19:45:58 +0100682 if (listener->bind_conf->options & BC_O_TCP_FO) {
Willy Tarreau1c862c52012-10-05 16:21:00 +0200683 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100684 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200685 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100686 chunk_appendf(msg, "%scannot enable TCP_FASTOPEN", msg->data ? ", " : "");
Willy Tarreau1c862c52012-10-05 16:21:00 +0200687 err |= ERR_WARN;
688 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200689 } else {
690 socklen_t len;
691 int qlen;
692 len = sizeof(qlen);
693 /* Only disable fast open if it was enabled, we don't want
694 * the kernel to create a fast open queue if there's none.
695 */
696 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
697 qlen != 0) {
698 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
699 sizeof(zero)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100700 chunk_appendf(msg, "%scannot disable TCP_FASTOPEN", msg->data ? ", " : "");
Olivier Houchard153659f2017-04-05 22:39:56 +0200701 err |= ERR_WARN;
702 }
703 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200704 }
705#endif
Willy Tarreau3e12de22020-10-13 17:42:21 +0200706
Willy Tarreau7d053e42020-10-15 09:19:43 +0200707 ready = sock_accepting_conn(&listener->rx) > 0;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100708
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200709 if (!ready && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100710 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100711 err |= ERR_RETRYABLE | ERR_ALERT;
Bjoern Jackeed174852021-01-12 19:24:43 +0100712 chunk_appendf(msg, "%scannot listen to socket", msg->data ? ", " : "");
Willy Tarreaue6b98942007-10-29 01:09:36 +0100713 goto tcp_close_return;
714 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100715
David Carlier1eb595b2021-02-06 12:11:11 +0000716#if !defined(TCP_DEFER_ACCEPT) && defined(SO_ACCEPTFILTER)
717 /* the socket needs to listen first */
Willy Tarreaud9b4d212023-01-12 19:42:48 +0100718 if (listener->bind_conf->options & BC_O_DEF_ACCEPT) {
David Carlier1eb595b2021-02-06 12:11:11 +0000719 struct accept_filter_arg accept;
720 memset(&accept, 0, sizeof(accept));
Olivier Houchard0963b8a2023-04-08 14:58:53 +0200721 strlcpy2(accept.af_name, "dataready", sizeof(accept.af_name));
David Carlier1eb595b2021-02-06 12:11:11 +0000722 if (setsockopt(fd, SOL_SOCKET, SO_ACCEPTFILTER, &accept, sizeof(accept)) == -1) {
Bjoern Jackeed174852021-01-12 19:24:43 +0100723 chunk_appendf(msg, "%scannot enable ACCEPT_FILTER", msg->data ? ", " : "");
David Carlier1eb595b2021-02-06 12:11:11 +0000724 err |= ERR_WARN;
725 }
726 }
727#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400728#if defined(TCP_QUICKACK)
Willy Tarreau9bdcf422023-01-12 19:40:42 +0100729 if (listener->bind_conf->options & BC_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900730 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200731 else
732 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200733#endif
734
Willy Tarreau09e266e2023-02-27 16:40:54 +0100735 done:
Willy Tarreaue6b98942007-10-29 01:09:36 +0100736 /* the socket is ready */
Willy Tarreaua37b2442020-09-24 07:23:45 +0200737 listener_set_state(listener, LI_LISTEN);
Christopher Fauleta1000032020-10-07 11:14:47 +0200738 goto tcp_return;
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200739
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200740 tcp_close_return:
Bjoern Jackeed174852021-01-12 19:24:43 +0100741 free_trash_chunk(msg);
742 msg = NULL;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200743 close(fd);
Willy Tarreauad33acf2020-09-02 18:40:02 +0200744 tcp_return:
Bjoern Jackeed174852021-01-12 19:24:43 +0100745 if (msg && errlen && msg->data) {
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100746 char pn[INET6_ADDRSTRLEN];
747
Willy Tarreau37159062020-08-27 07:48:42 +0200748 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200749 snprintf(errmsg, errlen, "%s for [%s:%d]", msg->area, pn, get_host_port(&listener->rx.addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100750 }
Bjoern Jackeed174852021-01-12 19:24:43 +0100751 free_trash_chunk(msg);
752 msg = NULL;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100753 return err;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100754}
755
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200756/* Enable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100757 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200758 */
759static void tcp_enable_listener(struct listener *l)
760{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100761 fd_want_recv_safe(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200762}
763
764/* Disable receipt of incoming connections for listener <l>. The receiver must
Willy Tarreaua4380b22020-11-04 13:59:04 +0100765 * still be valid.
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200766 */
767static void tcp_disable_listener(struct listener *l)
768{
Willy Tarreaua4380b22020-11-04 13:59:04 +0100769 fd_stop_recv(l->rx.fd);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200770}
771
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200772/* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver
Willy Tarreau64763342023-01-16 11:47:01 +0100773 * was totally stopped, or > 0 if correctly suspended. Note that inherited FDs
774 * are neither suspended nor resumed, we only enable/disable polling on them.
Willy Tarreau092d8652014-07-07 20:22:12 +0200775 */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200776static int tcp_suspend_receiver(struct receiver *rx)
Willy Tarreau092d8652014-07-07 20:22:12 +0200777{
Willy Tarreau2f6f3622020-10-14 10:50:41 +0200778 const struct sockaddr sa = { .sa_family = AF_UNSPEC };
Willy Tarreau3e12de22020-10-13 17:42:21 +0200779 int ret;
Willy Tarreau092d8652014-07-07 20:22:12 +0200780
Willy Tarreau64763342023-01-16 11:47:01 +0100781 /* We never disconnect a shared FD otherwise we'd break it in the
Willy Tarreau59b5da42020-11-04 14:14:55 +0100782 * parent process and any possible subsequent worker inheriting it.
Willy Tarreau64763342023-01-16 11:47:01 +0100783 * Thus we just stop receiving from it.
Willy Tarreau59b5da42020-11-04 14:14:55 +0100784 */
785 if (rx->flags & RX_F_INHERITED)
Willy Tarreau64763342023-01-16 11:47:01 +0100786 goto done;
Willy Tarreau59b5da42020-11-04 14:14:55 +0100787
Willy Tarreau8b6fc3d2020-10-13 16:34:19 +0200788 if (connect(rx->fd, &sa, sizeof(sa)) < 0)
789 goto check_already_done;
Willy Tarreau64763342023-01-16 11:47:01 +0100790 done:
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200791 fd_stop_recv(rx->fd);
Willy Tarreau092d8652014-07-07 20:22:12 +0200792 return 1;
Willy Tarreau91c614d2020-10-08 16:51:09 +0200793
794 check_already_done:
795 /* in case one of the shutdown() above fails, it might be because we're
796 * dealing with a socket that is shared with other processes doing the
797 * same. Let's check if it's still accepting connections.
798 */
Willy Tarreau7d053e42020-10-15 09:19:43 +0200799 ret = sock_accepting_conn(rx);
Willy Tarreau3e12de22020-10-13 17:42:21 +0200800 if (ret <= 0) {
801 /* unrecoverable or paused by another process */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200802 fd_stop_recv(rx->fd);
Willy Tarreau3e12de22020-10-13 17:42:21 +0200803 return ret == 0;
Willy Tarreaue53608b2020-09-24 18:20:37 +0200804 }
Willy Tarreau91c614d2020-10-08 16:51:09 +0200805
Willy Tarreau3e12de22020-10-13 17:42:21 +0200806 /* still listening, that's not good */
Willy Tarreau91c614d2020-10-08 16:51:09 +0200807 return -1;
Willy Tarreau092d8652014-07-07 20:22:12 +0200808}
809
Willy Tarreau58e6b712020-09-25 19:40:31 +0200810/* Resume a receiver. Returns < 0 in case of failure, 0 if the receiver
Willy Tarreau64763342023-01-16 11:47:01 +0100811 * was totally stopped, or > 0 if correctly resumed. Note that inherited FDs
812 * are neither suspended nor resumed, we only enable/disable polling on them.
Willy Tarreau58e6b712020-09-25 19:40:31 +0200813 */
814static int tcp_resume_receiver(struct receiver *rx)
815{
816 struct listener *l = LIST_ELEM(rx, struct listener *, rx);
817
818 if (rx->fd < 0)
819 return 0;
820
Willy Tarreau64763342023-01-16 11:47:01 +0100821 if ((rx->flags & RX_F_INHERITED) || listen(rx->fd, listener_backlog(l)) == 0) {
Willy Tarreau58e6b712020-09-25 19:40:31 +0200822 fd_want_recv(l->rx.fd);
823 return 1;
824 }
825 return -1;
826}
827
Willy Tarreaue6b98942007-10-29 01:09:36 +0100828
829/*
830 * Local variables:
831 * c-indent-level: 8
832 * c-basic-offset: 8
833 * End:
834 */