blob: 09d3e861922370825c2c9dbdfd203d0ff24f6189 [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 Tarreaua54553f2020-09-16 17:50:45 +020055 .ctrl_type = SOCK_STREAM,
Willy Tarreaue6b98942007-10-29 01:09:36 +010056 .sock_domain = AF_INET,
57 .sock_type = SOCK_STREAM,
58 .sock_prot = IPPROTO_TCP,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020059 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020060 .connect = tcp_connect_server,
Willy Tarreaub3580b12020-09-01 10:26:22 +020061 .listen = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010062 .enable_all = enable_all_listeners,
Willy Tarreau092d8652014-07-07 20:22:12 +020063 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020064 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010065 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
66 .nb_listeners = 0,
67};
68
Willy Tarreau0108d902018-11-25 19:14:37 +010069INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
70
Willy Tarreaue6b98942007-10-29 01:09:36 +010071/* Note: must not be declared <const> as its list will be overwritten */
72static struct protocol proto_tcpv6 = {
73 .name = "tcpv6",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020074 .fam = &proto_fam_inet6,
Willy Tarreaua54553f2020-09-16 17:50:45 +020075 .ctrl_type = SOCK_STREAM,
Willy Tarreaue6b98942007-10-29 01:09:36 +010076 .sock_domain = AF_INET6,
77 .sock_type = SOCK_STREAM,
78 .sock_prot = IPPROTO_TCP,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020079 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020080 .connect = tcp_connect_server,
Willy Tarreaub3580b12020-09-01 10:26:22 +020081 .listen = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010082 .enable_all = enable_all_listeners,
Willy Tarreau092d8652014-07-07 20:22:12 +020083 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020084 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010085 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
86 .nb_listeners = 0,
87};
88
Willy Tarreau0108d902018-11-25 19:14:37 +010089INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
90
David du Colombier6f5ccb12011-03-10 22:26:24 +010091/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +010092 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
93 * - 0 : ignore remote address (may even be a NULL pointer)
94 * - 1 : use provided address
95 * - 2 : use provided port
96 * - 3 : use both
97 *
98 * The function supports multiple foreign binding methods :
99 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100100 * The second one can be used as a fallback for the first one.
101 * This function returns 0 when everything's OK, 1 if it could not bind, to the
102 * local address, 2 if it could not bind to the foreign address.
103 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100104int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100105{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100106 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100107 int foreign_ok = 0;
108 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100109 static THREAD_LOCAL int ip_transp_working = 1;
110 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200111
David du Colombier65c17962012-07-13 14:34:59 +0200112 switch (local->ss_family) {
113 case AF_INET:
114 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200115 /* This deserves some explanation. Some platforms will support
116 * multiple combinations of certain methods, so we try the
117 * supported ones until one succeeds.
118 */
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200119 if (sock_inet4_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200120 foreign_ok = 1;
121 else
122 ip_transp_working = 0;
123 }
124 break;
125 case AF_INET6:
126 if (flags && ip6_transp_working) {
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200127 if (sock_inet6_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200128 foreign_ok = 1;
129 else
130 ip6_transp_working = 0;
131 }
132 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100133 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200134
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100135 if (flags) {
136 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200137 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100138 switch (remote->ss_family) {
139 case AF_INET:
140 if (flags & 1)
141 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
142 if (flags & 2)
143 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
144 break;
145 case AF_INET6:
146 if (flags & 1)
147 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
148 if (flags & 2)
149 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
150 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100151 default:
152 /* we don't want to try to bind to an unknown address family */
153 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100154 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100155 }
156
Simon Hormande072bd2011-06-24 15:11:37 +0900157 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100158 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200159 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200160 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
161 if (ret < 0)
162 return 2;
163 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100164 }
165 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200166 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200167 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
168 if (ret < 0)
169 return 1;
170 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100171 }
172
173 if (!flags)
174 return 0;
175
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100176 if (!foreign_ok)
177 /* we could not bind to a foreign address */
178 return 2;
179
180 return 0;
181}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100182
Willy Tarreau9650f372009-08-16 14:02:45 +0200183/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200184 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200185 * to connection <conn> using (si->{target,dst}). A source address may be
186 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200187 * bind addresses are still determined locally (due to the possible need of a
188 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100189 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100190 * supported. The <data> parameter is a boolean indicating whether there are data
191 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200192 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
193 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100194 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200195 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
196 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200197 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200198 * Note that a pending send_proxy message accounts for data.
199 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200200 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200201 * - SF_ERR_NONE if everything's OK
202 * - SF_ERR_SRVTO if there are no more servers
203 * - SF_ERR_SRVCL if the connection was refused by the server
204 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
205 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
206 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100207 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100208 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200209 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100210 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200211 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100212
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200213int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200214{
215 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100216 struct server *srv;
217 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100218 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100219 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800220 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100221
Olivier Houchard637b6952018-11-23 14:23:07 +0100222 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100223
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100224 switch (obj_type(conn->target)) {
225 case OBJ_TYPE_PROXY:
226 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100227 srv = NULL;
228 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100229 case OBJ_TYPE_SERVER:
230 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100231 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100232 /* Make sure we check that we have data before activating
233 * TFO, or we could trigger a kernel issue whereby after
234 * a successful connect() == 0, any subsequent connect()
235 * will return EINPROGRESS instead of EISCONN.
236 */
237 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
238 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
239 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100240 break;
241 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100242 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200243 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100244 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200245
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200246 if (!conn->dst) {
247 conn->flags |= CO_FL_ERROR;
248 return SF_ERR_INTERNAL;
249 }
250
Willy Tarreau18b7df72020-08-28 12:07:22 +0200251 fd = conn->handle.fd = sock_create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100252
253 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200254 qfprintf(stderr, "Cannot get a server socket.\n");
255
Willy Tarreau9ce70132014-01-24 16:08:19 +0100256 if (errno == ENFILE) {
257 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200258 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100259 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
260 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100261 }
262 else if (errno == EMFILE) {
263 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200264 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100265 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
266 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100267 }
268 else if (errno == ENOBUFS || errno == ENOMEM) {
269 conn->err_code = CO_ER_SYS_MEMLIM;
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 memory 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 == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
275 conn->err_code = CO_ER_NOPROTO;
276 }
277 else
278 conn->err_code = CO_ER_SOCK_ERR;
279
Willy Tarreau9650f372009-08-16 14:02:45 +0200280 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100281 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200282 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200283 }
284
285 if (fd >= global.maxsock) {
286 /* do not log anything there, it's a normal condition when this option
287 * is used to serialize connections to a server !
288 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100289 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200290 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100291 conn->err_code = CO_ER_CONF_FDLIM;
292 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200293 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200294 }
295
296 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900297 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200298 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
299 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100300 conn->err_code = CO_ER_SOCK_ERR;
301 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200302 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200303 }
304
William Lallemandc03eb012018-11-27 12:02:37 +0100305 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
306 ha_alert("Cannot set CLOEXEC on client socket.\n");
307 close(fd);
308 conn->err_code = CO_ER_SOCK_ERR;
309 conn->flags |= CO_FL_ERROR;
310 return SF_ERR_INTERNAL;
311 }
312
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900313 if (be->options & PR_O_TCP_SRV_KA) {
Simon Hormande072bd2011-06-24 15:11:37 +0900314 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200315
Willy Tarreau52543212020-07-09 05:58:51 +0200316#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900317 if (be->srvtcpka_cnt)
318 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200319#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900320
Willy Tarreau52543212020-07-09 05:58:51 +0200321#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900322 if (be->srvtcpka_idle)
323 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200324#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900325
Willy Tarreau52543212020-07-09 05:58:51 +0200326#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900327 if (be->srvtcpka_intvl)
328 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200329#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900330 }
331
Willy Tarreau9650f372009-08-16 14:02:45 +0200332 /* allow specific binding :
333 * - server-specific at first
334 * - proxy-specific next
335 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100336 if (srv && srv->conn_src.opts & CO_SRC_BIND)
337 src = &srv->conn_src;
338 else if (be->conn_src.opts & CO_SRC_BIND)
339 src = &be->conn_src;
340 else
341 src = NULL;
342
343 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200344 int ret, flags = 0;
345
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200346 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100347 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100348 case CO_SRC_TPROXY_CLI:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200349 conn_set_private(conn);
Willy Tarreau387ebf82015-08-04 19:24:13 +0200350 /* fall through */
351 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200352 flags = 3;
353 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100354 case CO_SRC_TPROXY_CIP:
355 case CO_SRC_TPROXY_DYN:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200356 conn_set_private(conn);
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200357 flags = 1;
358 break;
359 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200360 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200361
Willy Tarreau9650f372009-08-16 14:02:45 +0200362#ifdef SO_BINDTODEVICE
363 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100364 if (src->iface_name)
365 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200366#endif
367
Willy Tarreaua4380b42012-12-08 22:49:11 +0100368 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200369 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100370 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200371
372 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200373 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200374
375 do {
376 /* note: in case of retry, we may have to release a previously
377 * allocated port, hence this loop's construct.
378 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200379 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
380 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200381
382 if (!attempts)
383 break;
384 attempts--;
385
Willy Tarreaua4380b42012-12-08 22:49:11 +0100386 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100387 if (!fdinfo[fd].local_port) {
388 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200389 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100390 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200391
Willy Tarreaua4380b42012-12-08 22:49:11 +0100392 fdinfo[fd].port_range = src->sport_range;
393 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200394
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200395 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100396 if (ret != 0)
397 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200398 } while (ret != 0); /* binding NOK */
399 }
400 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000401#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100402 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000403 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
404#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200405 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100406 if (ret != 0)
407 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200408 }
409
Willy Tarreaua4380b42012-12-08 22:49:11 +0100410 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200411 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
412 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200413 close(fd);
414
Willy Tarreau9650f372009-08-16 14:02:45 +0200415 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100416 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
417 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200418 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100419 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200420 be->id);
421 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100422 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
423 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200424 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100425 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200426 be->id);
427 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100428 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200429 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200430 }
431 }
432
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400433#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200434 /* disabling tcp quick ack now allows the first request to leave the
435 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100436 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200437 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200438 if (flags & (CONNECT_DELACK_ALWAYS) ||
439 ((flags & CONNECT_DELACK_SMART_CONNECT ||
440 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
441 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900442 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200443#endif
444
Willy Tarreau163d4622015-10-13 16:16:41 +0200445#ifdef TCP_USER_TIMEOUT
446 /* there is not much more we can do here when it fails, it's still minor */
447 if (srv && srv->tcp_ut)
448 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
449#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100450
451 if (use_fastopen) {
452#if defined(TCP_FASTOPEN_CONNECT)
453 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
454#endif
455 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100456 if (global.tune.server_sndbuf)
457 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
458
459 if (global.tune.server_rcvbuf)
460 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
461
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200462 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800463 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100464 if (errno == EINPROGRESS || errno == EALREADY) {
465 /* common case, let's wait for connect status */
466 conn->flags |= CO_FL_WAIT_L4_CONN;
467 }
468 else if (errno == EISCONN) {
469 /* should normally not happen but if so, indicates that it's OK */
470 conn->flags &= ~CO_FL_WAIT_L4_CONN;
471 }
472 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200473 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100474 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200475 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100476 conn->err_code = CO_ER_FREE_PORTS;
477 }
478 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200479 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100480 conn->err_code = CO_ER_ADDR_INUSE;
481 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200482
Willy Tarreaub1719512012-12-08 23:03:28 +0100483 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200484 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
485 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200486 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100487 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100488 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200489 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200490 } else if (errno == ETIMEDOUT) {
491 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200492 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
493 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200494 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100495 conn->err_code = CO_ER_SOCK_ERR;
496 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200497 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200498 } else {
499 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
500 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200501 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
502 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200503 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100504 conn->err_code = CO_ER_SOCK_ERR;
505 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200506 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200507 }
508 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100509 else {
510 /* connect() == 0, this is great! */
511 conn->flags &= ~CO_FL_WAIT_L4_CONN;
512 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200513
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100514 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200515
Willy Tarreauf79c8172013-10-21 16:30:56 +0200516 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100517 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200518
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100519 if (conn->flags & CO_FL_WAIT_L4_CONN) {
520 fd_want_send(fd);
521 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200522 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100523 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200524
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200525 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200526 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100527 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200528 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200529 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200530
Willy Tarreaue7dff022015-04-03 01:14:29 +0200531 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200532}
533
Willy Tarreaue6b98942007-10-29 01:09:36 +0100534/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100535 * an error message in <errmsg> if the message is at most <errlen> bytes long
536 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
537 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100538 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
539 * was alright and that no message was returned. ERR_RETRYABLE means that an
540 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700541 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100542 * the meaning of the error, but just indicate that a message is present which
543 * should be displayed with the respective level. Last, ERR_ABORT indicates
544 * that it's pointless to try to start other listeners. No error message is
545 * returned if errlen is NULL.
546 */
547int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
548{
Willy Tarreaue6b98942007-10-29 01:09:36 +0100549 int fd, err;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200550 int ready;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100551 socklen_t ready_len;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200552 char *msg = NULL;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100553
Willy Tarreauad33acf2020-09-02 18:40:02 +0200554 err = ERR_NONE;
555
Willy Tarreaue6b98942007-10-29 01:09:36 +0100556 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100557 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100558 *errmsg = 0;
559
560 if (listener->state != LI_ASSIGNED)
561 return ERR_NONE; /* already bound */
562
Willy Tarreauad33acf2020-09-02 18:40:02 +0200563 if (!(listener->rx.flags & RX_F_BOUND)) {
564 msg = "receiving socket not bound";
565 goto tcp_return;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100566 }
567
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200568 fd = listener->rx.fd;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100569
570 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900571 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200572 else {
573 struct linger tmplinger;
574 socklen_t len = sizeof(tmplinger);
575 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
576 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
577 tmplinger.l_onoff = 0;
578 tmplinger.l_linger = 0;
579 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
580 sizeof(tmplinger));
581 }
582 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100583
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400584#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100585 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400586 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200587 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
588 msg = "cannot set MSS";
589 err |= ERR_WARN;
590 }
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200591 } else {
592 /* we may want to try to restore the default MSS if the socket was inherited */
Olivier Houchard153659f2017-04-05 22:39:56 +0200593 int tmpmaxseg = -1;
594 int defaultmss;
595 socklen_t len = sizeof(tmpmaxseg);
596
Willy Tarreau37159062020-08-27 07:48:42 +0200597 if (listener->rx.addr.ss_family == AF_INET)
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200598 defaultmss = sock_inet_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200599 else
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200600 defaultmss = sock_inet6_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200601
602 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100603 if (defaultmss > 0 &&
604 tmpmaxseg != defaultmss &&
605 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200606 msg = "cannot set MSS";
607 err |= ERR_WARN;
608 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200609 }
610#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100611#if defined(TCP_USER_TIMEOUT)
612 if (listener->tcp_ut) {
613 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
614 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
615 msg = "cannot set TCP User Timeout";
616 err |= ERR_WARN;
617 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200618 } else
619 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
620 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100621#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200622#if defined(TCP_DEFER_ACCEPT)
623 if (listener->options & LI_O_DEF_ACCEPT) {
624 /* defer accept by up to one second */
625 int accept_delay = 1;
626 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
627 msg = "cannot enable DEFER_ACCEPT";
628 err |= ERR_WARN;
629 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200630 } else
631 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
632 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200633#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200634#if defined(TCP_FASTOPEN)
635 if (listener->options & LI_O_TCP_FO) {
636 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100637 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200638 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
639 msg = "cannot enable TCP_FASTOPEN";
640 err |= ERR_WARN;
641 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200642 } else {
643 socklen_t len;
644 int qlen;
645 len = sizeof(qlen);
646 /* Only disable fast open if it was enabled, we don't want
647 * the kernel to create a fast open queue if there's none.
648 */
649 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
650 qlen != 0) {
651 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
652 sizeof(zero)) == -1) {
653 msg = "cannot disable TCP_FASTOPEN";
654 err |= ERR_WARN;
655 }
656 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200657 }
658#endif
Willy Tarreau40aa0702013-03-10 23:51:38 +0100659 ready = 0;
660 ready_len = sizeof(ready);
661 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
662 ready = 0;
663
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200664 if (!ready && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100665 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100666 err |= ERR_RETRYABLE | ERR_ALERT;
667 msg = "cannot listen to socket";
668 goto tcp_close_return;
669 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100670
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400671#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200672 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900673 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200674 else
675 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200676#endif
677
Willy Tarreaue6b98942007-10-29 01:09:36 +0100678 /* the socket is ready */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100679 listener->state = LI_LISTEN;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200680 return err;
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200681
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200682 tcp_close_return:
683 close(fd);
Willy Tarreauad33acf2020-09-02 18:40:02 +0200684 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100685 if (msg && errlen) {
686 char pn[INET6_ADDRSTRLEN];
687
Willy Tarreau37159062020-08-27 07:48:42 +0200688 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
689 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100690 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100691 return err;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100692}
693
Willy Tarreau32282382017-09-15 07:44:44 +0200694/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
695 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
696 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200697 *
698 * Must be called with proto_lock held.
699 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100700 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200701static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100702{
703 if (listener->state != LI_INIT)
704 return;
705 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200706 listener->rx.proto = &proto_tcpv4;
Willy Tarreau37159062020-08-27 07:48:42 +0200707 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200708 LIST_ADDQ(&proto_tcpv4.listeners, &listener->rx.proto_list);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100709 proto_tcpv4.nb_listeners++;
710}
711
Willy Tarreau32282382017-09-15 07:44:44 +0200712/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
713 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
714 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200715 *
716 * Must be called with proto_lock held.
717 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100718 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200719static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100720{
721 if (listener->state != LI_INIT)
722 return;
723 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200724 listener->rx.proto = &proto_tcpv6;
Willy Tarreau37159062020-08-27 07:48:42 +0200725 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200726 LIST_ADDQ(&proto_tcpv6.listeners, &listener->rx.proto_list);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100727 proto_tcpv6.nb_listeners++;
728}
729
Willy Tarreau092d8652014-07-07 20:22:12 +0200730/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
731 * was totally stopped, or > 0 if correctly paused.
732 */
733int tcp_pause_listener(struct listener *l)
734{
Willy Tarreau38ba6472020-08-27 08:16:52 +0200735 if (shutdown(l->rx.fd, SHUT_WR) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200736 return -1; /* Solaris dies here */
737
Willy Tarreau38ba6472020-08-27 08:16:52 +0200738 if (listen(l->rx.fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200739 return -1; /* OpenBSD dies here */
740
Willy Tarreau38ba6472020-08-27 08:16:52 +0200741 if (shutdown(l->rx.fd, SHUT_RD) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200742 return -1; /* should always be OK */
743 return 1;
744}
745
Willy Tarreaue6b98942007-10-29 01:09:36 +0100746
747/*
748 * Local variables:
749 * c-indent-level: 8
750 * c-basic-offset: 8
751 * End:
752 */