blob: cb4dc32020bcf90011ee292d00c6ecbf16124dbc [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 Tarreau9d5be5c2017-09-15 07:55:51 +020048static void tcpv4_add_listener(struct listener *listener, int port);
49static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010050
51/* Note: must not be declared <const> as its list will be overwritten */
52static struct protocol proto_tcpv4 = {
53 .name = "tcpv4",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020054 .fam = &proto_fam_inet4,
Willy Tarreaue6b98942007-10-29 01:09:36 +010055 .sock_domain = AF_INET,
56 .sock_type = SOCK_STREAM,
57 .sock_prot = IPPROTO_TCP,
58 .sock_family = AF_INET,
59 .sock_addrlen = sizeof(struct sockaddr_in),
60 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020061 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020062 .connect = tcp_connect_server,
Willy Tarreaud69ce1f2020-09-01 14:18:04 +020063 .bind = sock_inet_bind_receiver,
Willy Tarreaub3580b12020-09-01 10:26:22 +020064 .listen = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010065 .enable_all = enable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020066 .get_src = sock_get_src,
Willy Tarreauc5a94c92020-08-28 15:19:45 +020067 .get_dst = sock_inet_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020068 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020069 .add = tcpv4_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020070 .addrcmp = sock_inet4_addrcmp,
Willy Tarreaue6b98942007-10-29 01:09:36 +010071 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
72 .nb_listeners = 0,
73};
74
Willy Tarreau0108d902018-11-25 19:14:37 +010075INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
76
Willy Tarreaue6b98942007-10-29 01:09:36 +010077/* Note: must not be declared <const> as its list will be overwritten */
78static struct protocol proto_tcpv6 = {
79 .name = "tcpv6",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020080 .fam = &proto_fam_inet6,
Willy Tarreaue6b98942007-10-29 01:09:36 +010081 .sock_domain = AF_INET6,
82 .sock_type = SOCK_STREAM,
83 .sock_prot = IPPROTO_TCP,
84 .sock_family = AF_INET6,
85 .sock_addrlen = sizeof(struct sockaddr_in6),
86 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020087 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020088 .connect = tcp_connect_server,
Willy Tarreaud69ce1f2020-09-01 14:18:04 +020089 .bind = sock_inet_bind_receiver,
Willy Tarreaub3580b12020-09-01 10:26:22 +020090 .listen = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010091 .enable_all = enable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020092 .get_src = sock_get_src,
Willy Tarreauc5a94c92020-08-28 15:19:45 +020093 .get_dst = sock_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020094 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020095 .add = tcpv6_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020096 .addrcmp = sock_inet6_addrcmp,
Willy Tarreaue6b98942007-10-29 01:09:36 +010097 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
98 .nb_listeners = 0,
99};
100
Willy Tarreau0108d902018-11-25 19:14:37 +0100101INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
102
David du Colombier6f5ccb12011-03-10 22:26:24 +0100103/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100104 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
105 * - 0 : ignore remote address (may even be a NULL pointer)
106 * - 1 : use provided address
107 * - 2 : use provided port
108 * - 3 : use both
109 *
110 * The function supports multiple foreign binding methods :
111 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100112 * The second one can be used as a fallback for the first one.
113 * This function returns 0 when everything's OK, 1 if it could not bind, to the
114 * local address, 2 if it could not bind to the foreign address.
115 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100116int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100117{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100118 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100119 int foreign_ok = 0;
120 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100121 static THREAD_LOCAL int ip_transp_working = 1;
122 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200123
David du Colombier65c17962012-07-13 14:34:59 +0200124 switch (local->ss_family) {
125 case AF_INET:
126 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200127 /* This deserves some explanation. Some platforms will support
128 * multiple combinations of certain methods, so we try the
129 * supported ones until one succeeds.
130 */
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200131 if (sock_inet4_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200132 foreign_ok = 1;
133 else
134 ip_transp_working = 0;
135 }
136 break;
137 case AF_INET6:
138 if (flags && ip6_transp_working) {
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200139 if (sock_inet6_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200140 foreign_ok = 1;
141 else
142 ip6_transp_working = 0;
143 }
144 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100145 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200146
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100147 if (flags) {
148 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200149 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100150 switch (remote->ss_family) {
151 case AF_INET:
152 if (flags & 1)
153 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
154 if (flags & 2)
155 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
156 break;
157 case AF_INET6:
158 if (flags & 1)
159 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
160 if (flags & 2)
161 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
162 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100163 default:
164 /* we don't want to try to bind to an unknown address family */
165 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100166 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100167 }
168
Simon Hormande072bd2011-06-24 15:11:37 +0900169 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100170 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200171 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200172 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
173 if (ret < 0)
174 return 2;
175 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100176 }
177 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200178 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200179 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
180 if (ret < 0)
181 return 1;
182 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100183 }
184
185 if (!flags)
186 return 0;
187
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100188 if (!foreign_ok)
189 /* we could not bind to a foreign address */
190 return 2;
191
192 return 0;
193}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100194
Willy Tarreau9650f372009-08-16 14:02:45 +0200195/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200196 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200197 * to connection <conn> using (si->{target,dst}). A source address may be
198 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200199 * bind addresses are still determined locally (due to the possible need of a
200 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100201 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100202 * supported. The <data> parameter is a boolean indicating whether there are data
203 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200204 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
205 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100206 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200207 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
208 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200209 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200210 * Note that a pending send_proxy message accounts for data.
211 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200212 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200213 * - SF_ERR_NONE if everything's OK
214 * - SF_ERR_SRVTO if there are no more servers
215 * - SF_ERR_SRVCL if the connection was refused by the server
216 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
217 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
218 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100219 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100220 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200221 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100222 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200223 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100224
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200225int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200226{
227 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100228 struct server *srv;
229 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100230 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100231 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800232 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100233
Olivier Houchard637b6952018-11-23 14:23:07 +0100234 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100235
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100236 switch (obj_type(conn->target)) {
237 case OBJ_TYPE_PROXY:
238 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100239 srv = NULL;
240 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100241 case OBJ_TYPE_SERVER:
242 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100243 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100244 /* Make sure we check that we have data before activating
245 * TFO, or we could trigger a kernel issue whereby after
246 * a successful connect() == 0, any subsequent connect()
247 * will return EINPROGRESS instead of EISCONN.
248 */
249 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
250 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
251 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100252 break;
253 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100254 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200255 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100256 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200257
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200258 if (!conn->dst) {
259 conn->flags |= CO_FL_ERROR;
260 return SF_ERR_INTERNAL;
261 }
262
Willy Tarreau18b7df72020-08-28 12:07:22 +0200263 fd = conn->handle.fd = sock_create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100264
265 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200266 qfprintf(stderr, "Cannot get a server socket.\n");
267
Willy Tarreau9ce70132014-01-24 16:08:19 +0100268 if (errno == ENFILE) {
269 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200270 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100271 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
272 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100273 }
274 else if (errno == EMFILE) {
275 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200276 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100277 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
278 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100279 }
280 else if (errno == ENOBUFS || errno == ENOMEM) {
281 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200282 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100283 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
284 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100285 }
286 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
287 conn->err_code = CO_ER_NOPROTO;
288 }
289 else
290 conn->err_code = CO_ER_SOCK_ERR;
291
Willy Tarreau9650f372009-08-16 14:02:45 +0200292 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100293 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200294 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200295 }
296
297 if (fd >= global.maxsock) {
298 /* do not log anything there, it's a normal condition when this option
299 * is used to serialize connections to a server !
300 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100301 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200302 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100303 conn->err_code = CO_ER_CONF_FDLIM;
304 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200305 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200306 }
307
308 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900309 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200310 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
311 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100312 conn->err_code = CO_ER_SOCK_ERR;
313 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200314 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200315 }
316
William Lallemandc03eb012018-11-27 12:02:37 +0100317 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
318 ha_alert("Cannot set CLOEXEC on client socket.\n");
319 close(fd);
320 conn->err_code = CO_ER_SOCK_ERR;
321 conn->flags |= CO_FL_ERROR;
322 return SF_ERR_INTERNAL;
323 }
324
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900325 if (be->options & PR_O_TCP_SRV_KA) {
Simon Hormande072bd2011-06-24 15:11:37 +0900326 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200327
Willy Tarreau52543212020-07-09 05:58:51 +0200328#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900329 if (be->srvtcpka_cnt)
330 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200331#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900332
Willy Tarreau52543212020-07-09 05:58:51 +0200333#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900334 if (be->srvtcpka_idle)
335 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200336#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900337
Willy Tarreau52543212020-07-09 05:58:51 +0200338#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900339 if (be->srvtcpka_intvl)
340 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200341#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900342 }
343
Willy Tarreau9650f372009-08-16 14:02:45 +0200344 /* allow specific binding :
345 * - server-specific at first
346 * - proxy-specific next
347 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100348 if (srv && srv->conn_src.opts & CO_SRC_BIND)
349 src = &srv->conn_src;
350 else if (be->conn_src.opts & CO_SRC_BIND)
351 src = &be->conn_src;
352 else
353 src = NULL;
354
355 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 int ret, flags = 0;
357
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200358 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100359 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100360 case CO_SRC_TPROXY_CLI:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200361 conn_set_private(conn);
Willy Tarreau387ebf82015-08-04 19:24:13 +0200362 /* fall through */
363 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200364 flags = 3;
365 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100366 case CO_SRC_TPROXY_CIP:
367 case CO_SRC_TPROXY_DYN:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200368 conn_set_private(conn);
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200369 flags = 1;
370 break;
371 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200372 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200373
Willy Tarreau9650f372009-08-16 14:02:45 +0200374#ifdef SO_BINDTODEVICE
375 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100376 if (src->iface_name)
377 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200378#endif
379
Willy Tarreaua4380b42012-12-08 22:49:11 +0100380 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200381 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100382 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200383
384 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200385 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200386
387 do {
388 /* note: in case of retry, we may have to release a previously
389 * allocated port, hence this loop's construct.
390 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200391 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
392 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200393
394 if (!attempts)
395 break;
396 attempts--;
397
Willy Tarreaua4380b42012-12-08 22:49:11 +0100398 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100399 if (!fdinfo[fd].local_port) {
400 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200401 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100402 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200403
Willy Tarreaua4380b42012-12-08 22:49:11 +0100404 fdinfo[fd].port_range = src->sport_range;
405 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200406
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200407 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100408 if (ret != 0)
409 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200410 } while (ret != 0); /* binding NOK */
411 }
412 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000413#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100414 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000415 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
416#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200417 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100418 if (ret != 0)
419 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200420 }
421
Willy Tarreaua4380b42012-12-08 22:49:11 +0100422 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200423 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
424 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200425 close(fd);
426
Willy Tarreau9650f372009-08-16 14:02:45 +0200427 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100428 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
429 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200430 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100431 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200432 be->id);
433 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100434 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
435 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200436 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100437 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 be->id);
439 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100440 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200441 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 }
443 }
444
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400445#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200446 /* disabling tcp quick ack now allows the first request to leave the
447 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100448 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200449 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200450 if (flags & (CONNECT_DELACK_ALWAYS) ||
451 ((flags & CONNECT_DELACK_SMART_CONNECT ||
452 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
453 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900454 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200455#endif
456
Willy Tarreau163d4622015-10-13 16:16:41 +0200457#ifdef TCP_USER_TIMEOUT
458 /* there is not much more we can do here when it fails, it's still minor */
459 if (srv && srv->tcp_ut)
460 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
461#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100462
463 if (use_fastopen) {
464#if defined(TCP_FASTOPEN_CONNECT)
465 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
466#endif
467 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100468 if (global.tune.server_sndbuf)
469 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
470
471 if (global.tune.server_rcvbuf)
472 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
473
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200474 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800475 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100476 if (errno == EINPROGRESS || errno == EALREADY) {
477 /* common case, let's wait for connect status */
478 conn->flags |= CO_FL_WAIT_L4_CONN;
479 }
480 else if (errno == EISCONN) {
481 /* should normally not happen but if so, indicates that it's OK */
482 conn->flags &= ~CO_FL_WAIT_L4_CONN;
483 }
484 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200485 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100486 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200487 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100488 conn->err_code = CO_ER_FREE_PORTS;
489 }
490 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200491 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100492 conn->err_code = CO_ER_ADDR_INUSE;
493 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200494
Willy Tarreaub1719512012-12-08 23:03:28 +0100495 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200496 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
497 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200498 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100499 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100500 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200501 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200502 } else if (errno == ETIMEDOUT) {
503 //qfprintf(stderr,"Connect(): ETIMEDOUT");
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 Tarreau9ce70132014-01-24 16:08:19 +0100507 conn->err_code = CO_ER_SOCK_ERR;
508 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200509 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200510 } else {
511 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
512 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200513 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
514 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200515 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100516 conn->err_code = CO_ER_SOCK_ERR;
517 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200518 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200519 }
520 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100521 else {
522 /* connect() == 0, this is great! */
523 conn->flags &= ~CO_FL_WAIT_L4_CONN;
524 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200525
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100526 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200527
Willy Tarreauf79c8172013-10-21 16:30:56 +0200528 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100529 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200530
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100531 if (conn->flags & CO_FL_WAIT_L4_CONN) {
532 fd_want_send(fd);
533 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200534 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100535 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200536
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200537 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200538 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100539 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200540 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200541 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200542
Willy Tarreaue7dff022015-04-03 01:14:29 +0200543 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200544}
545
Willy Tarreaue6b98942007-10-29 01:09:36 +0100546/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100547 * an error message in <errmsg> if the message is at most <errlen> bytes long
548 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
549 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100550 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
551 * was alright and that no message was returned. ERR_RETRYABLE means that an
552 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700553 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100554 * the meaning of the error, but just indicate that a message is present which
555 * should be displayed with the respective level. Last, ERR_ABORT indicates
556 * that it's pointless to try to start other listeners. No error message is
557 * returned if errlen is NULL.
558 */
559int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
560{
Willy Tarreaue6b98942007-10-29 01:09:36 +0100561 int fd, err;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200562 int ready;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100563 socklen_t ready_len;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200564 char *msg = NULL;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100565
Willy Tarreauad33acf2020-09-02 18:40:02 +0200566 err = ERR_NONE;
567
Willy Tarreaue6b98942007-10-29 01:09:36 +0100568 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100569 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100570 *errmsg = 0;
571
572 if (listener->state != LI_ASSIGNED)
573 return ERR_NONE; /* already bound */
574
Willy Tarreauad33acf2020-09-02 18:40:02 +0200575 if (!(listener->rx.flags & RX_F_BOUND)) {
576 msg = "receiving socket not bound";
577 goto tcp_return;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100578 }
579
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200580 fd = listener->rx.fd;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100581
582 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900583 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200584 else {
585 struct linger tmplinger;
586 socklen_t len = sizeof(tmplinger);
587 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
588 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
589 tmplinger.l_onoff = 0;
590 tmplinger.l_linger = 0;
591 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
592 sizeof(tmplinger));
593 }
594 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100595
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400596#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100597 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400598 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200599 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
600 msg = "cannot set MSS";
601 err |= ERR_WARN;
602 }
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200603 } else {
604 /* we may want to try to restore the default MSS if the socket was inherited */
Olivier Houchard153659f2017-04-05 22:39:56 +0200605 int tmpmaxseg = -1;
606 int defaultmss;
607 socklen_t len = sizeof(tmpmaxseg);
608
Willy Tarreau37159062020-08-27 07:48:42 +0200609 if (listener->rx.addr.ss_family == AF_INET)
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200610 defaultmss = sock_inet_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200611 else
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200612 defaultmss = sock_inet6_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200613
614 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100615 if (defaultmss > 0 &&
616 tmpmaxseg != defaultmss &&
617 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200618 msg = "cannot set MSS";
619 err |= ERR_WARN;
620 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200621 }
622#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100623#if defined(TCP_USER_TIMEOUT)
624 if (listener->tcp_ut) {
625 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
626 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
627 msg = "cannot set TCP User Timeout";
628 err |= ERR_WARN;
629 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200630 } else
631 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
632 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100633#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200634#if defined(TCP_DEFER_ACCEPT)
635 if (listener->options & LI_O_DEF_ACCEPT) {
636 /* defer accept by up to one second */
637 int accept_delay = 1;
638 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
639 msg = "cannot enable DEFER_ACCEPT";
640 err |= ERR_WARN;
641 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200642 } else
643 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
644 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200645#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200646#if defined(TCP_FASTOPEN)
647 if (listener->options & LI_O_TCP_FO) {
648 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100649 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200650 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
651 msg = "cannot enable TCP_FASTOPEN";
652 err |= ERR_WARN;
653 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200654 } else {
655 socklen_t len;
656 int qlen;
657 len = sizeof(qlen);
658 /* Only disable fast open if it was enabled, we don't want
659 * the kernel to create a fast open queue if there's none.
660 */
661 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
662 qlen != 0) {
663 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
664 sizeof(zero)) == -1) {
665 msg = "cannot disable TCP_FASTOPEN";
666 err |= ERR_WARN;
667 }
668 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200669 }
670#endif
Willy Tarreau40aa0702013-03-10 23:51:38 +0100671 ready = 0;
672 ready_len = sizeof(ready);
673 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
674 ready = 0;
675
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200676 if (!ready && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100677 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100678 err |= ERR_RETRYABLE | ERR_ALERT;
679 msg = "cannot listen to socket";
680 goto tcp_close_return;
681 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100682
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400683#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200684 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900685 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200686 else
687 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200688#endif
689
Willy Tarreaue6b98942007-10-29 01:09:36 +0100690 /* the socket is ready */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100691 listener->state = LI_LISTEN;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200692 return err;
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200693
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200694 tcp_close_return:
695 close(fd);
Willy Tarreauad33acf2020-09-02 18:40:02 +0200696 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100697 if (msg && errlen) {
698 char pn[INET6_ADDRSTRLEN];
699
Willy Tarreau37159062020-08-27 07:48:42 +0200700 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
701 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100702 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100703 return err;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100704}
705
Willy Tarreau32282382017-09-15 07:44:44 +0200706/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
707 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
708 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200709 *
710 * Must be called with proto_lock held.
711 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100712 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200713static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100714{
715 if (listener->state != LI_INIT)
716 return;
717 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200718 listener->rx.proto = &proto_tcpv4;
Willy Tarreau37159062020-08-27 07:48:42 +0200719 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200720 LIST_ADDQ(&proto_tcpv4.listeners, &listener->rx.proto_list);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100721 proto_tcpv4.nb_listeners++;
722}
723
Willy Tarreau32282382017-09-15 07:44:44 +0200724/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
725 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
726 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200727 *
728 * Must be called with proto_lock held.
729 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100730 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200731static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100732{
733 if (listener->state != LI_INIT)
734 return;
735 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200736 listener->rx.proto = &proto_tcpv6;
Willy Tarreau37159062020-08-27 07:48:42 +0200737 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200738 LIST_ADDQ(&proto_tcpv6.listeners, &listener->rx.proto_list);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100739 proto_tcpv6.nb_listeners++;
740}
741
Willy Tarreau092d8652014-07-07 20:22:12 +0200742/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
743 * was totally stopped, or > 0 if correctly paused.
744 */
745int tcp_pause_listener(struct listener *l)
746{
Willy Tarreau38ba6472020-08-27 08:16:52 +0200747 if (shutdown(l->rx.fd, SHUT_WR) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200748 return -1; /* Solaris dies here */
749
Willy Tarreau38ba6472020-08-27 08:16:52 +0200750 if (listen(l->rx.fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200751 return -1; /* OpenBSD dies here */
752
Willy Tarreau38ba6472020-08-27 08:16:52 +0200753 if (shutdown(l->rx.fd, SHUT_RD) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200754 return -1; /* should always be OK */
755 return 1;
756}
757
Willy Tarreaue6b98942007-10-29 01:09:36 +0100758
759/*
760 * Local variables:
761 * c-indent-level: 8
762 * c-basic-offset: 8
763 * End:
764 */