blob: 9e6a3d7226035306a03e6fba9da6966f32de4107 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010023#include <sys/types.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010024
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040025#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020026#include <netinet/in.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040027
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020028#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/arg.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020030#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/fd.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020033#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020034#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020035#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/namespace.h>
38#include <haproxy/port_range.h>
39#include <haproxy/proto_tcp.h>
40#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020041#include <haproxy/proxy-t.h>
Willy Tarreau18b7df72020-08-28 12:07:22 +020042#include <haproxy/sock.h>
Willy Tarreauf1725582020-08-28 15:30:11 +020043#include <haproxy/sock_inet.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020044#include <haproxy/tools.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010045
Willy Tarreaue6b98942007-10-29 01:09:36 +010046
Emeric Bruncf20bf12010-10-22 16:06:11 +020047static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaucb66ea62020-09-25 17:12:32 +020048static int tcp_suspend_receiver(struct receiver *rx);
Willy Tarreau58e6b712020-09-25 19:40:31 +020049static int tcp_resume_receiver(struct receiver *rx);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020050static void tcp_enable_listener(struct listener *listener);
51static void tcp_disable_listener(struct listener *listener);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020052static void tcpv4_add_listener(struct listener *listener, int port);
53static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010054
55/* Note: must not be declared <const> as its list will be overwritten */
56static struct protocol proto_tcpv4 = {
57 .name = "tcpv4",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020058 .fam = &proto_fam_inet4,
Willy Tarreaua54553f2020-09-16 17:50:45 +020059 .ctrl_type = SOCK_STREAM,
Willy Tarreaue6b98942007-10-29 01:09:36 +010060 .sock_domain = AF_INET,
61 .sock_type = SOCK_STREAM,
62 .sock_prot = IPPROTO_TCP,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020063 .add = tcpv4_add_listener,
64 .listen = tcp_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020065 .enable = tcp_enable_listener,
66 .disable = tcp_disable_listener,
Willy Tarreau7b2febd2020-10-09 17:18:29 +020067 .unbind = default_unbind_listener,
Willy Tarreaue03204c2020-10-09 17:02:21 +020068 .suspend = default_suspend_listener,
69 .resume = default_resume_listener,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020070 .rx_enable = sock_enable,
71 .rx_disable = sock_disable,
Willy Tarreauf58b8db2020-10-09 16:32:08 +020072 .rx_unbind = sock_unbind,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020073 .rx_suspend = tcp_suspend_receiver,
Willy Tarreau58e6b712020-09-25 19:40:31 +020074 .rx_resume = tcp_resume_receiver,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020075 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020076 .connect = tcp_connect_server,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020077 .receivers = LIST_HEAD_INIT(proto_tcpv4.receivers),
78 .nb_receivers = 0,
Willy Tarreaue6b98942007-10-29 01:09:36 +010079};
80
Willy Tarreau0108d902018-11-25 19:14:37 +010081INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
82
Willy Tarreaue6b98942007-10-29 01:09:36 +010083/* Note: must not be declared <const> as its list will be overwritten */
84static struct protocol proto_tcpv6 = {
85 .name = "tcpv6",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020086 .fam = &proto_fam_inet6,
Willy Tarreaua54553f2020-09-16 17:50:45 +020087 .ctrl_type = SOCK_STREAM,
Willy Tarreaue6b98942007-10-29 01:09:36 +010088 .sock_domain = AF_INET6,
89 .sock_type = SOCK_STREAM,
90 .sock_prot = IPPROTO_TCP,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020091 .add = tcpv6_add_listener,
92 .listen = tcp_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020093 .enable = tcp_enable_listener,
94 .disable = tcp_disable_listener,
Willy Tarreau7b2febd2020-10-09 17:18:29 +020095 .unbind = default_unbind_listener,
Willy Tarreaue03204c2020-10-09 17:02:21 +020096 .suspend = default_suspend_listener,
97 .resume = default_resume_listener,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020098 .rx_enable = sock_enable,
99 .rx_disable = sock_disable,
Willy Tarreauf58b8db2020-10-09 16:32:08 +0200100 .rx_unbind = sock_unbind,
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200101 .rx_suspend = tcp_suspend_receiver,
Willy Tarreau58e6b712020-09-25 19:40:31 +0200102 .rx_resume = tcp_resume_receiver,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200103 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200104 .connect = tcp_connect_server,
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200105 .receivers = LIST_HEAD_INIT(proto_tcpv6.receivers),
106 .nb_receivers = 0,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100107};
108
Willy Tarreau0108d902018-11-25 19:14:37 +0100109INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
110
David du Colombier6f5ccb12011-03-10 22:26:24 +0100111/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100112 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
113 * - 0 : ignore remote address (may even be a NULL pointer)
114 * - 1 : use provided address
115 * - 2 : use provided port
116 * - 3 : use both
117 *
118 * The function supports multiple foreign binding methods :
119 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100120 * The second one can be used as a fallback for the first one.
121 * This function returns 0 when everything's OK, 1 if it could not bind, to the
122 * local address, 2 if it could not bind to the foreign address.
123 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100124int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100125{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100126 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100127 int foreign_ok = 0;
128 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100129 static THREAD_LOCAL int ip_transp_working = 1;
130 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200131
David du Colombier65c17962012-07-13 14:34:59 +0200132 switch (local->ss_family) {
133 case AF_INET:
134 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200135 /* This deserves some explanation. Some platforms will support
136 * multiple combinations of certain methods, so we try the
137 * supported ones until one succeeds.
138 */
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200139 if (sock_inet4_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200140 foreign_ok = 1;
141 else
142 ip_transp_working = 0;
143 }
144 break;
145 case AF_INET6:
146 if (flags && ip6_transp_working) {
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200147 if (sock_inet6_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200148 foreign_ok = 1;
149 else
150 ip6_transp_working = 0;
151 }
152 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100153 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200154
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100155 if (flags) {
156 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200157 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100158 switch (remote->ss_family) {
159 case AF_INET:
160 if (flags & 1)
161 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
162 if (flags & 2)
163 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
164 break;
165 case AF_INET6:
166 if (flags & 1)
167 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
168 if (flags & 2)
169 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
170 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100171 default:
172 /* we don't want to try to bind to an unknown address family */
173 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100174 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100175 }
176
Simon Hormande072bd2011-06-24 15:11:37 +0900177 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100178 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200179 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200180 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
181 if (ret < 0)
182 return 2;
183 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100184 }
185 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200186 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200187 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
188 if (ret < 0)
189 return 1;
190 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100191 }
192
193 if (!flags)
194 return 0;
195
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100196 if (!foreign_ok)
197 /* we could not bind to a foreign address */
198 return 2;
199
200 return 0;
201}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100202
Willy Tarreau9650f372009-08-16 14:02:45 +0200203/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200204 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200205 * to connection <conn> using (si->{target,dst}). A source address may be
206 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200207 * bind addresses are still determined locally (due to the possible need of a
208 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100209 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100210 * supported. The <data> parameter is a boolean indicating whether there are data
211 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200212 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
213 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100214 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200215 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
216 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200217 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200218 * Note that a pending send_proxy message accounts for data.
219 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200220 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200221 * - SF_ERR_NONE if everything's OK
222 * - SF_ERR_SRVTO if there are no more servers
223 * - SF_ERR_SRVCL if the connection was refused by the server
224 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
225 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
226 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100227 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100228 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200229 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100230 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200231 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100232
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200233int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200234{
235 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100236 struct server *srv;
237 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100238 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100239 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800240 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100241
Olivier Houchard637b6952018-11-23 14:23:07 +0100242 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100243
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100244 switch (obj_type(conn->target)) {
245 case OBJ_TYPE_PROXY:
246 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100247 srv = NULL;
248 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100249 case OBJ_TYPE_SERVER:
250 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100251 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100252 /* Make sure we check that we have data before activating
253 * TFO, or we could trigger a kernel issue whereby after
254 * a successful connect() == 0, any subsequent connect()
255 * will return EINPROGRESS instead of EISCONN.
256 */
257 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
258 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
259 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100260 break;
261 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100262 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200263 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100264 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200265
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200266 if (!conn->dst) {
267 conn->flags |= CO_FL_ERROR;
268 return SF_ERR_INTERNAL;
269 }
270
Willy Tarreau18b7df72020-08-28 12:07:22 +0200271 fd = conn->handle.fd = sock_create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100272
273 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200274 qfprintf(stderr, "Cannot get a server socket.\n");
275
Willy Tarreau9ce70132014-01-24 16:08:19 +0100276 if (errno == ENFILE) {
277 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200278 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100279 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
280 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100281 }
282 else if (errno == EMFILE) {
283 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200284 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100285 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
286 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100287 }
288 else if (errno == ENOBUFS || errno == ENOMEM) {
289 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200290 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100291 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
292 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100293 }
294 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
295 conn->err_code = CO_ER_NOPROTO;
296 }
297 else
298 conn->err_code = CO_ER_SOCK_ERR;
299
Willy Tarreau9650f372009-08-16 14:02:45 +0200300 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100301 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200302 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200303 }
304
305 if (fd >= global.maxsock) {
306 /* do not log anything there, it's a normal condition when this option
307 * is used to serialize connections to a server !
308 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100309 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200310 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100311 conn->err_code = CO_ER_CONF_FDLIM;
312 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200313 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200314 }
315
316 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900317 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200318 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
319 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100320 conn->err_code = CO_ER_SOCK_ERR;
321 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200322 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200323 }
324
William Lallemandc03eb012018-11-27 12:02:37 +0100325 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
326 ha_alert("Cannot set CLOEXEC on client socket.\n");
327 close(fd);
328 conn->err_code = CO_ER_SOCK_ERR;
329 conn->flags |= CO_FL_ERROR;
330 return SF_ERR_INTERNAL;
331 }
332
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900333 if (be->options & PR_O_TCP_SRV_KA) {
Simon Hormande072bd2011-06-24 15:11:37 +0900334 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200335
Willy Tarreau52543212020-07-09 05:58:51 +0200336#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900337 if (be->srvtcpka_cnt)
338 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200339#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900340
Willy Tarreau52543212020-07-09 05:58:51 +0200341#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900342 if (be->srvtcpka_idle)
343 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200344#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900345
Willy Tarreau52543212020-07-09 05:58:51 +0200346#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900347 if (be->srvtcpka_intvl)
348 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200349#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900350 }
351
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 /* allow specific binding :
353 * - server-specific at first
354 * - proxy-specific next
355 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100356 if (srv && srv->conn_src.opts & CO_SRC_BIND)
357 src = &srv->conn_src;
358 else if (be->conn_src.opts & CO_SRC_BIND)
359 src = &be->conn_src;
360 else
361 src = NULL;
362
363 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200364 int ret, flags = 0;
365
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200366 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100367 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100368 case CO_SRC_TPROXY_CLI:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200369 conn_set_private(conn);
Willy Tarreau387ebf82015-08-04 19:24:13 +0200370 /* fall through */
371 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200372 flags = 3;
373 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100374 case CO_SRC_TPROXY_CIP:
375 case CO_SRC_TPROXY_DYN:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200376 conn_set_private(conn);
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200377 flags = 1;
378 break;
379 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200380 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200381
Willy Tarreau9650f372009-08-16 14:02:45 +0200382#ifdef SO_BINDTODEVICE
383 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100384 if (src->iface_name)
385 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200386#endif
387
Willy Tarreaua4380b42012-12-08 22:49:11 +0100388 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200389 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100390 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200391
392 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200393 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200394
395 do {
396 /* note: in case of retry, we may have to release a previously
397 * allocated port, hence this loop's construct.
398 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200399 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
400 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200401
402 if (!attempts)
403 break;
404 attempts--;
405
Willy Tarreaua4380b42012-12-08 22:49:11 +0100406 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100407 if (!fdinfo[fd].local_port) {
408 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200409 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100410 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200411
Willy Tarreaua4380b42012-12-08 22:49:11 +0100412 fdinfo[fd].port_range = src->sport_range;
413 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200414
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200415 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100416 if (ret != 0)
417 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200418 } while (ret != 0); /* binding NOK */
419 }
420 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000421#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100422 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000423 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
424#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200425 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100426 if (ret != 0)
427 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200428 }
429
Willy Tarreaua4380b42012-12-08 22:49:11 +0100430 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200431 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
432 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200433 close(fd);
434
Willy Tarreau9650f372009-08-16 14:02:45 +0200435 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100436 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
437 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100439 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200440 be->id);
441 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100442 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
443 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200444 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100445 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200446 be->id);
447 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100448 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200449 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200450 }
451 }
452
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400453#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 /* disabling tcp quick ack now allows the first request to leave the
455 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100456 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200457 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200458 if (flags & (CONNECT_DELACK_ALWAYS) ||
459 ((flags & CONNECT_DELACK_SMART_CONNECT ||
460 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
461 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900462 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200463#endif
464
Willy Tarreau163d4622015-10-13 16:16:41 +0200465#ifdef TCP_USER_TIMEOUT
466 /* there is not much more we can do here when it fails, it's still minor */
467 if (srv && srv->tcp_ut)
468 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
469#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100470
471 if (use_fastopen) {
472#if defined(TCP_FASTOPEN_CONNECT)
473 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
474#endif
475 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100476 if (global.tune.server_sndbuf)
477 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
478
479 if (global.tune.server_rcvbuf)
480 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
481
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200482 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800483 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100484 if (errno == EINPROGRESS || errno == EALREADY) {
485 /* common case, let's wait for connect status */
486 conn->flags |= CO_FL_WAIT_L4_CONN;
487 }
488 else if (errno == EISCONN) {
489 /* should normally not happen but if so, indicates that it's OK */
490 conn->flags &= ~CO_FL_WAIT_L4_CONN;
491 }
492 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200493 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100494 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200495 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100496 conn->err_code = CO_ER_FREE_PORTS;
497 }
498 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200499 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100500 conn->err_code = CO_ER_ADDR_INUSE;
501 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200502
Willy Tarreaub1719512012-12-08 23:03:28 +0100503 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200504 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
505 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200506 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100507 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100508 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200509 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200510 } else if (errno == ETIMEDOUT) {
511 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200512 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
513 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200514 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100515 conn->err_code = CO_ER_SOCK_ERR;
516 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200517 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200518 } else {
519 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
520 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200521 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
522 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200523 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100524 conn->err_code = CO_ER_SOCK_ERR;
525 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200526 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200527 }
528 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100529 else {
530 /* connect() == 0, this is great! */
531 conn->flags &= ~CO_FL_WAIT_L4_CONN;
532 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200533
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100534 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200535
Willy Tarreauf79c8172013-10-21 16:30:56 +0200536 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100537 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200538
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100539 if (conn->flags & CO_FL_WAIT_L4_CONN) {
540 fd_want_send(fd);
541 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200542 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100543 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200544
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200545 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200546 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100547 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200548 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200549 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200550
Willy Tarreaue7dff022015-04-03 01:14:29 +0200551 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200552}
553
Willy Tarreaue6b98942007-10-29 01:09:36 +0100554/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100555 * an error message in <errmsg> if the message is at most <errlen> bytes long
556 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
557 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100558 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
559 * was alright and that no message was returned. ERR_RETRYABLE means that an
560 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700561 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100562 * the meaning of the error, but just indicate that a message is present which
563 * should be displayed with the respective level. Last, ERR_ABORT indicates
564 * that it's pointless to try to start other listeners. No error message is
565 * returned if errlen is NULL.
566 */
567int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
568{
Willy Tarreaue6b98942007-10-29 01:09:36 +0100569 int fd, err;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200570 int ready;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100571 socklen_t ready_len;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200572 char *msg = NULL;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100573
Willy Tarreauad33acf2020-09-02 18:40:02 +0200574 err = ERR_NONE;
575
Willy Tarreaue6b98942007-10-29 01:09:36 +0100576 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100577 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100578 *errmsg = 0;
579
580 if (listener->state != LI_ASSIGNED)
581 return ERR_NONE; /* already bound */
582
Willy Tarreauad33acf2020-09-02 18:40:02 +0200583 if (!(listener->rx.flags & RX_F_BOUND)) {
584 msg = "receiving socket not bound";
585 goto tcp_return;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100586 }
587
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200588 fd = listener->rx.fd;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100589
590 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900591 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200592 else {
593 struct linger tmplinger;
594 socklen_t len = sizeof(tmplinger);
595 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
596 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
597 tmplinger.l_onoff = 0;
598 tmplinger.l_linger = 0;
599 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
600 sizeof(tmplinger));
601 }
602 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100603
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400604#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100605 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400606 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200607 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
608 msg = "cannot set MSS";
609 err |= ERR_WARN;
610 }
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200611 } else {
612 /* we may want to try to restore the default MSS if the socket was inherited */
Olivier Houchard153659f2017-04-05 22:39:56 +0200613 int tmpmaxseg = -1;
614 int defaultmss;
615 socklen_t len = sizeof(tmpmaxseg);
616
Willy Tarreau37159062020-08-27 07:48:42 +0200617 if (listener->rx.addr.ss_family == AF_INET)
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200618 defaultmss = sock_inet_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200619 else
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200620 defaultmss = sock_inet6_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200621
622 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100623 if (defaultmss > 0 &&
624 tmpmaxseg != defaultmss &&
625 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200626 msg = "cannot set MSS";
627 err |= ERR_WARN;
628 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200629 }
630#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100631#if defined(TCP_USER_TIMEOUT)
632 if (listener->tcp_ut) {
633 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
634 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
635 msg = "cannot set TCP User Timeout";
636 err |= ERR_WARN;
637 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200638 } else
639 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
640 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100641#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200642#if defined(TCP_DEFER_ACCEPT)
643 if (listener->options & LI_O_DEF_ACCEPT) {
644 /* defer accept by up to one second */
645 int accept_delay = 1;
646 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
647 msg = "cannot enable DEFER_ACCEPT";
648 err |= ERR_WARN;
649 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200650 } else
651 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
652 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200653#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200654#if defined(TCP_FASTOPEN)
655 if (listener->options & LI_O_TCP_FO) {
656 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100657 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200658 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
659 msg = "cannot enable TCP_FASTOPEN";
660 err |= ERR_WARN;
661 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200662 } else {
663 socklen_t len;
664 int qlen;
665 len = sizeof(qlen);
666 /* Only disable fast open if it was enabled, we don't want
667 * the kernel to create a fast open queue if there's none.
668 */
669 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
670 qlen != 0) {
671 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
672 sizeof(zero)) == -1) {
673 msg = "cannot disable TCP_FASTOPEN";
674 err |= ERR_WARN;
675 }
676 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200677 }
678#endif
Willy Tarreau40aa0702013-03-10 23:51:38 +0100679 ready = 0;
680 ready_len = sizeof(ready);
681 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
682 ready = 0;
683
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200684 if (!ready && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100685 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100686 err |= ERR_RETRYABLE | ERR_ALERT;
687 msg = "cannot listen to socket";
688 goto tcp_close_return;
689 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100690
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400691#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200692 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900693 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200694 else
695 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200696#endif
697
Willy Tarreaue6b98942007-10-29 01:09:36 +0100698 /* the socket is ready */
Willy Tarreaua37b2442020-09-24 07:23:45 +0200699 listener_set_state(listener, LI_LISTEN);
Christopher Fauleta1000032020-10-07 11:14:47 +0200700 goto tcp_return;
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200701
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200702 tcp_close_return:
703 close(fd);
Willy Tarreauad33acf2020-09-02 18:40:02 +0200704 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100705 if (msg && errlen) {
706 char pn[INET6_ADDRSTRLEN];
707
Willy Tarreau37159062020-08-27 07:48:42 +0200708 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
709 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100710 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100711 return err;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100712}
713
Willy Tarreau32282382017-09-15 07:44:44 +0200714/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
715 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
716 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200717 *
718 * Must be called with proto_lock held.
719 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100720 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200721static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100722{
723 if (listener->state != LI_INIT)
724 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200725 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200726 listener->rx.proto = &proto_tcpv4;
Willy Tarreau37159062020-08-27 07:48:42 +0200727 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200728 LIST_ADDQ(&proto_tcpv4.receivers, &listener->rx.proto_list);
729 proto_tcpv4.nb_receivers++;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100730}
731
Willy Tarreau32282382017-09-15 07:44:44 +0200732/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
733 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
734 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200735 *
736 * Must be called with proto_lock held.
737 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100738 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200739static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100740{
741 if (listener->state != LI_INIT)
742 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200743 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200744 listener->rx.proto = &proto_tcpv6;
Willy Tarreau37159062020-08-27 07:48:42 +0200745 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200746 LIST_ADDQ(&proto_tcpv6.receivers, &listener->rx.proto_list);
747 proto_tcpv6.nb_receivers++;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100748}
749
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200750/* Enable receipt of incoming connections for listener <l>. The receiver must
751 * still be valid. Does nothing in early boot (needs fd_updt).
752 */
753static void tcp_enable_listener(struct listener *l)
754{
755 if (fd_updt)
756 fd_want_recv(l->rx.fd);
757}
758
759/* Disable receipt of incoming connections for listener <l>. The receiver must
760 * still be valid. Does nothing in early boot (needs fd_updt).
761 */
762static void tcp_disable_listener(struct listener *l)
763{
764 if (fd_updt)
765 fd_stop_recv(l->rx.fd);
766}
767
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200768/* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver
769 * was totally stopped, or > 0 if correctly suspended.
Willy Tarreau092d8652014-07-07 20:22:12 +0200770 */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200771static int tcp_suspend_receiver(struct receiver *rx)
Willy Tarreau092d8652014-07-07 20:22:12 +0200772{
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200773 struct listener *l = LIST_ELEM(rx, struct listener *, rx);
Willy Tarreau91c614d2020-10-08 16:51:09 +0200774 socklen_t opt_val, opt_len;
775
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200776 if (shutdown(rx->fd, SHUT_WR) != 0)
Willy Tarreau91c614d2020-10-08 16:51:09 +0200777 goto check_already_done; /* usually Solaris fails here */
Willy Tarreau092d8652014-07-07 20:22:12 +0200778
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200779 if (listen(rx->fd, listener_backlog(l)) != 0)
Willy Tarreau91c614d2020-10-08 16:51:09 +0200780 goto check_already_done; /* Usually OpenBSD fails here */
Willy Tarreau092d8652014-07-07 20:22:12 +0200781
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200782 if (shutdown(rx->fd, SHUT_RD) != 0)
Willy Tarreau91c614d2020-10-08 16:51:09 +0200783 goto check_already_done; /* show always be OK */
784
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200785 fd_stop_recv(rx->fd);
Willy Tarreau092d8652014-07-07 20:22:12 +0200786 return 1;
Willy Tarreau91c614d2020-10-08 16:51:09 +0200787
788 check_already_done:
789 /* in case one of the shutdown() above fails, it might be because we're
790 * dealing with a socket that is shared with other processes doing the
791 * same. Let's check if it's still accepting connections.
792 */
793 opt_val = 0;
794 opt_len = sizeof(opt_val);
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200795 if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &opt_val, &opt_len) == -1) {
796 fd_stop_recv(rx->fd);
Willy Tarreau91c614d2020-10-08 16:51:09 +0200797 return 0; /* the socket is really unrecoverable */
Willy Tarreaue53608b2020-09-24 18:20:37 +0200798 }
Willy Tarreau91c614d2020-10-08 16:51:09 +0200799
Willy Tarreaue53608b2020-09-24 18:20:37 +0200800 if (!opt_val) {
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200801 fd_stop_recv(rx->fd);
Willy Tarreau91c614d2020-10-08 16:51:09 +0200802 return 1; /* already paused by another process */
Willy Tarreaue53608b2020-09-24 18:20:37 +0200803 }
Willy Tarreau91c614d2020-10-08 16:51:09 +0200804
805 /* something looks fishy here */
806 return -1;
Willy Tarreau092d8652014-07-07 20:22:12 +0200807}
808
Willy Tarreau58e6b712020-09-25 19:40:31 +0200809/* Resume a receiver. Returns < 0 in case of failure, 0 if the receiver
810 * was totally stopped, or > 0 if correctly suspended.
811 */
812static int tcp_resume_receiver(struct receiver *rx)
813{
814 struct listener *l = LIST_ELEM(rx, struct listener *, rx);
815
816 if (rx->fd < 0)
817 return 0;
818
819 if (listen(rx->fd, listener_backlog(l)) == 0) {
820 fd_want_recv(l->rx.fd);
821 return 1;
822 }
823 return -1;
824}
825
Willy Tarreaue6b98942007-10-29 01:09:36 +0100826
827/*
828 * Local variables:
829 * c-indent-level: 8
830 * c-basic-offset: 8
831 * End:
832 */