blob: 57ccfed23b0f28b8a65b384a02900c41461923b0 [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",
54 .sock_domain = AF_INET,
55 .sock_type = SOCK_STREAM,
56 .sock_prot = IPPROTO_TCP,
57 .sock_family = AF_INET,
58 .sock_addrlen = sizeof(struct sockaddr_in),
59 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020060 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020061 .connect = tcp_connect_server,
Willy Tarreaud69ce1f2020-09-01 14:18:04 +020062 .bind = sock_inet_bind_receiver,
Willy Tarreaub3580b12020-09-01 10:26:22 +020063 .listen = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010064 .enable_all = enable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020065 .get_src = sock_get_src,
Willy Tarreauc5a94c92020-08-28 15:19:45 +020066 .get_dst = sock_inet_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020067 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020068 .add = tcpv4_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020069 .addrcmp = sock_inet4_addrcmp,
Willy Tarreaue6b98942007-10-29 01:09:36 +010070 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
71 .nb_listeners = 0,
72};
73
Willy Tarreau0108d902018-11-25 19:14:37 +010074INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
75
Willy Tarreaue6b98942007-10-29 01:09:36 +010076/* Note: must not be declared <const> as its list will be overwritten */
77static struct protocol proto_tcpv6 = {
78 .name = "tcpv6",
79 .sock_domain = AF_INET6,
80 .sock_type = SOCK_STREAM,
81 .sock_prot = IPPROTO_TCP,
82 .sock_family = AF_INET6,
83 .sock_addrlen = sizeof(struct sockaddr_in6),
84 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020085 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020086 .connect = tcp_connect_server,
Willy Tarreaud69ce1f2020-09-01 14:18:04 +020087 .bind = sock_inet_bind_receiver,
Willy Tarreaub3580b12020-09-01 10:26:22 +020088 .listen = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010089 .enable_all = enable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020090 .get_src = sock_get_src,
Willy Tarreauc5a94c92020-08-28 15:19:45 +020091 .get_dst = sock_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020092 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020093 .add = tcpv6_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020094 .addrcmp = sock_inet6_addrcmp,
Willy Tarreaue6b98942007-10-29 01:09:36 +010095 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
96 .nb_listeners = 0,
97};
98
Willy Tarreau0108d902018-11-25 19:14:37 +010099INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
100
David du Colombier6f5ccb12011-03-10 22:26:24 +0100101/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100102 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
103 * - 0 : ignore remote address (may even be a NULL pointer)
104 * - 1 : use provided address
105 * - 2 : use provided port
106 * - 3 : use both
107 *
108 * The function supports multiple foreign binding methods :
109 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100110 * The second one can be used as a fallback for the first one.
111 * This function returns 0 when everything's OK, 1 if it could not bind, to the
112 * local address, 2 if it could not bind to the foreign address.
113 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100114int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100115{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100116 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100117 int foreign_ok = 0;
118 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100119 static THREAD_LOCAL int ip_transp_working = 1;
120 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200121
David du Colombier65c17962012-07-13 14:34:59 +0200122 switch (local->ss_family) {
123 case AF_INET:
124 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200125 /* This deserves some explanation. Some platforms will support
126 * multiple combinations of certain methods, so we try the
127 * supported ones until one succeeds.
128 */
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200129 if (sock_inet4_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200130 foreign_ok = 1;
131 else
132 ip_transp_working = 0;
133 }
134 break;
135 case AF_INET6:
136 if (flags && ip6_transp_working) {
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200137 if (sock_inet6_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200138 foreign_ok = 1;
139 else
140 ip6_transp_working = 0;
141 }
142 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100143 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200144
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100145 if (flags) {
146 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200147 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100148 switch (remote->ss_family) {
149 case AF_INET:
150 if (flags & 1)
151 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
152 if (flags & 2)
153 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
154 break;
155 case AF_INET6:
156 if (flags & 1)
157 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
158 if (flags & 2)
159 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
160 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100161 default:
162 /* we don't want to try to bind to an unknown address family */
163 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100164 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100165 }
166
Simon Hormande072bd2011-06-24 15:11:37 +0900167 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100168 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200169 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200170 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
171 if (ret < 0)
172 return 2;
173 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100174 }
175 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200176 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200177 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
178 if (ret < 0)
179 return 1;
180 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100181 }
182
183 if (!flags)
184 return 0;
185
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100186 if (!foreign_ok)
187 /* we could not bind to a foreign address */
188 return 2;
189
190 return 0;
191}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100192
Willy Tarreau9650f372009-08-16 14:02:45 +0200193/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200194 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200195 * to connection <conn> using (si->{target,dst}). A source address may be
196 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200197 * bind addresses are still determined locally (due to the possible need of a
198 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100199 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100200 * supported. The <data> parameter is a boolean indicating whether there are data
201 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200202 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
203 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100204 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200205 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
206 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200207 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200208 * Note that a pending send_proxy message accounts for data.
209 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200210 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200211 * - SF_ERR_NONE if everything's OK
212 * - SF_ERR_SRVTO if there are no more servers
213 * - SF_ERR_SRVCL if the connection was refused by the server
214 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
215 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
216 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100217 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100218 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200219 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100220 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200221 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100222
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200223int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200224{
225 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100226 struct server *srv;
227 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100228 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100229 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800230 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100231
Olivier Houchard637b6952018-11-23 14:23:07 +0100232 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100233
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100234 switch (obj_type(conn->target)) {
235 case OBJ_TYPE_PROXY:
236 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100237 srv = NULL;
238 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100239 case OBJ_TYPE_SERVER:
240 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100241 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100242 /* Make sure we check that we have data before activating
243 * TFO, or we could trigger a kernel issue whereby after
244 * a successful connect() == 0, any subsequent connect()
245 * will return EINPROGRESS instead of EISCONN.
246 */
247 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
248 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
249 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100250 break;
251 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100252 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200253 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100254 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200255
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200256 if (!conn->dst) {
257 conn->flags |= CO_FL_ERROR;
258 return SF_ERR_INTERNAL;
259 }
260
Willy Tarreau18b7df72020-08-28 12:07:22 +0200261 fd = conn->handle.fd = sock_create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100262
263 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200264 qfprintf(stderr, "Cannot get a server socket.\n");
265
Willy Tarreau9ce70132014-01-24 16:08:19 +0100266 if (errno == ENFILE) {
267 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200268 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100269 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
270 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100271 }
272 else if (errno == EMFILE) {
273 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200274 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100275 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
276 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100277 }
278 else if (errno == ENOBUFS || errno == ENOMEM) {
279 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200280 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100281 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
282 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100283 }
284 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
285 conn->err_code = CO_ER_NOPROTO;
286 }
287 else
288 conn->err_code = CO_ER_SOCK_ERR;
289
Willy Tarreau9650f372009-08-16 14:02:45 +0200290 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100291 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200292 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200293 }
294
295 if (fd >= global.maxsock) {
296 /* do not log anything there, it's a normal condition when this option
297 * is used to serialize connections to a server !
298 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100299 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200300 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100301 conn->err_code = CO_ER_CONF_FDLIM;
302 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200303 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200304 }
305
306 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900307 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200308 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
309 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100310 conn->err_code = CO_ER_SOCK_ERR;
311 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200312 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200313 }
314
William Lallemandc03eb012018-11-27 12:02:37 +0100315 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
316 ha_alert("Cannot set CLOEXEC on client socket.\n");
317 close(fd);
318 conn->err_code = CO_ER_SOCK_ERR;
319 conn->flags |= CO_FL_ERROR;
320 return SF_ERR_INTERNAL;
321 }
322
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900323 if (be->options & PR_O_TCP_SRV_KA) {
Simon Hormande072bd2011-06-24 15:11:37 +0900324 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200325
Willy Tarreau52543212020-07-09 05:58:51 +0200326#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900327 if (be->srvtcpka_cnt)
328 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200329#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900330
Willy Tarreau52543212020-07-09 05:58:51 +0200331#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900332 if (be->srvtcpka_idle)
333 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200334#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900335
Willy Tarreau52543212020-07-09 05:58:51 +0200336#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900337 if (be->srvtcpka_intvl)
338 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200339#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900340 }
341
Willy Tarreau9650f372009-08-16 14:02:45 +0200342 /* allow specific binding :
343 * - server-specific at first
344 * - proxy-specific next
345 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100346 if (srv && srv->conn_src.opts & CO_SRC_BIND)
347 src = &srv->conn_src;
348 else if (be->conn_src.opts & CO_SRC_BIND)
349 src = &be->conn_src;
350 else
351 src = NULL;
352
353 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200354 int ret, flags = 0;
355
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200356 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100357 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100358 case CO_SRC_TPROXY_CLI:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200359 conn_set_private(conn);
Willy Tarreau387ebf82015-08-04 19:24:13 +0200360 /* fall through */
361 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200362 flags = 3;
363 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100364 case CO_SRC_TPROXY_CIP:
365 case CO_SRC_TPROXY_DYN:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200366 conn_set_private(conn);
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200367 flags = 1;
368 break;
369 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200370 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200371
Willy Tarreau9650f372009-08-16 14:02:45 +0200372#ifdef SO_BINDTODEVICE
373 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100374 if (src->iface_name)
375 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200376#endif
377
Willy Tarreaua4380b42012-12-08 22:49:11 +0100378 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200379 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100380 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200381
382 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200383 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200384
385 do {
386 /* note: in case of retry, we may have to release a previously
387 * allocated port, hence this loop's construct.
388 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200389 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
390 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200391
392 if (!attempts)
393 break;
394 attempts--;
395
Willy Tarreaua4380b42012-12-08 22:49:11 +0100396 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100397 if (!fdinfo[fd].local_port) {
398 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200399 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100400 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200401
Willy Tarreaua4380b42012-12-08 22:49:11 +0100402 fdinfo[fd].port_range = src->sport_range;
403 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200404
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200405 ret = tcp_bind_socket(fd, flags, &sa, 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 } while (ret != 0); /* binding NOK */
409 }
410 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000411#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100412 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000413 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
414#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200415 ret = tcp_bind_socket(fd, flags, &src->source_addr, 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 }
419
Willy Tarreaua4380b42012-12-08 22:49:11 +0100420 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200421 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
422 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200423 close(fd);
424
Willy Tarreau9650f372009-08-16 14:02:45 +0200425 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100426 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
427 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200428 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100429 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200430 be->id);
431 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100432 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
433 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200434 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100435 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200436 be->id);
437 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100438 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200439 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200440 }
441 }
442
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400443#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200444 /* disabling tcp quick ack now allows the first request to leave the
445 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100446 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200447 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200448 if (flags & (CONNECT_DELACK_ALWAYS) ||
449 ((flags & CONNECT_DELACK_SMART_CONNECT ||
450 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
451 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900452 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200453#endif
454
Willy Tarreau163d4622015-10-13 16:16:41 +0200455#ifdef TCP_USER_TIMEOUT
456 /* there is not much more we can do here when it fails, it's still minor */
457 if (srv && srv->tcp_ut)
458 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
459#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100460
461 if (use_fastopen) {
462#if defined(TCP_FASTOPEN_CONNECT)
463 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
464#endif
465 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100466 if (global.tune.server_sndbuf)
467 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
468
469 if (global.tune.server_rcvbuf)
470 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
471
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200472 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800473 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100474 if (errno == EINPROGRESS || errno == EALREADY) {
475 /* common case, let's wait for connect status */
476 conn->flags |= CO_FL_WAIT_L4_CONN;
477 }
478 else if (errno == EISCONN) {
479 /* should normally not happen but if so, indicates that it's OK */
480 conn->flags &= ~CO_FL_WAIT_L4_CONN;
481 }
482 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200483 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100484 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200485 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100486 conn->err_code = CO_ER_FREE_PORTS;
487 }
488 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200489 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100490 conn->err_code = CO_ER_ADDR_INUSE;
491 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200492
Willy Tarreaub1719512012-12-08 23:03:28 +0100493 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200494 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
495 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200496 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100497 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100498 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200499 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200500 } else if (errno == ETIMEDOUT) {
501 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200502 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
503 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200504 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100505 conn->err_code = CO_ER_SOCK_ERR;
506 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200507 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200508 } else {
509 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
510 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200511 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
512 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200513 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100514 conn->err_code = CO_ER_SOCK_ERR;
515 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200516 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200517 }
518 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100519 else {
520 /* connect() == 0, this is great! */
521 conn->flags &= ~CO_FL_WAIT_L4_CONN;
522 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200523
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100524 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200525
Willy Tarreauf79c8172013-10-21 16:30:56 +0200526 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100527 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200528
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100529 if (conn->flags & CO_FL_WAIT_L4_CONN) {
530 fd_want_send(fd);
531 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200532 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100533 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200534
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200535 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200536 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100537 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200538 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200539 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200540
Willy Tarreaue7dff022015-04-03 01:14:29 +0200541 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200542}
543
Willy Tarreaue6b98942007-10-29 01:09:36 +0100544/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100545 * an error message in <errmsg> if the message is at most <errlen> bytes long
546 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
547 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100548 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
549 * was alright and that no message was returned. ERR_RETRYABLE means that an
550 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700551 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100552 * the meaning of the error, but just indicate that a message is present which
553 * should be displayed with the respective level. Last, ERR_ABORT indicates
554 * that it's pointless to try to start other listeners. No error message is
555 * returned if errlen is NULL.
556 */
557int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
558{
Willy Tarreaue6b98942007-10-29 01:09:36 +0100559 int fd, err;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200560 int ready;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100561 socklen_t ready_len;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200562 char *msg = NULL;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100563
564 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100565 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100566 *errmsg = 0;
567
568 if (listener->state != LI_ASSIGNED)
569 return ERR_NONE; /* already bound */
570
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200571 err = sock_inet_bind_receiver(&listener->rx, listener->rx.proto->accept, &msg);
572 if (err != ERR_NONE) {
573 snprintf(errmsg, errlen, "%s", msg);
574 free(msg); msg = NULL;
575 return err;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100576 }
577
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200578 fd = listener->rx.fd;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100579
580 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900581 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200582 else {
583 struct linger tmplinger;
584 socklen_t len = sizeof(tmplinger);
585 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
586 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
587 tmplinger.l_onoff = 0;
588 tmplinger.l_linger = 0;
589 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
590 sizeof(tmplinger));
591 }
592 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100593
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400594#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100595 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400596 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200597 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
598 msg = "cannot set MSS";
599 err |= ERR_WARN;
600 }
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200601 } else {
602 /* we may want to try to restore the default MSS if the socket was inherited */
Olivier Houchard153659f2017-04-05 22:39:56 +0200603 int tmpmaxseg = -1;
604 int defaultmss;
605 socklen_t len = sizeof(tmpmaxseg);
606
Willy Tarreau37159062020-08-27 07:48:42 +0200607 if (listener->rx.addr.ss_family == AF_INET)
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200608 defaultmss = sock_inet_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200609 else
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200610 defaultmss = sock_inet6_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200611
612 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100613 if (defaultmss > 0 &&
614 tmpmaxseg != defaultmss &&
615 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200616 msg = "cannot set MSS";
617 err |= ERR_WARN;
618 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200619 }
620#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100621#if defined(TCP_USER_TIMEOUT)
622 if (listener->tcp_ut) {
623 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
624 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
625 msg = "cannot set TCP User Timeout";
626 err |= ERR_WARN;
627 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200628 } else
629 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
630 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100631#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200632#if defined(TCP_DEFER_ACCEPT)
633 if (listener->options & LI_O_DEF_ACCEPT) {
634 /* defer accept by up to one second */
635 int accept_delay = 1;
636 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
637 msg = "cannot enable DEFER_ACCEPT";
638 err |= ERR_WARN;
639 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200640 } else
641 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
642 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200643#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200644#if defined(TCP_FASTOPEN)
645 if (listener->options & LI_O_TCP_FO) {
646 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100647 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200648 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
649 msg = "cannot enable TCP_FASTOPEN";
650 err |= ERR_WARN;
651 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200652 } else {
653 socklen_t len;
654 int qlen;
655 len = sizeof(qlen);
656 /* Only disable fast open if it was enabled, we don't want
657 * the kernel to create a fast open queue if there's none.
658 */
659 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
660 qlen != 0) {
661 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
662 sizeof(zero)) == -1) {
663 msg = "cannot disable TCP_FASTOPEN";
664 err |= ERR_WARN;
665 }
666 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200667 }
668#endif
Willy Tarreau40aa0702013-03-10 23:51:38 +0100669 ready = 0;
670 ready_len = sizeof(ready);
671 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
672 ready = 0;
673
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200674 if (!ready && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100675 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100676 err |= ERR_RETRYABLE | ERR_ALERT;
677 msg = "cannot listen to socket";
678 goto tcp_close_return;
679 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100680
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400681#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200682 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900683 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200684 else
685 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200686#endif
687
Willy Tarreaue6b98942007-10-29 01:09:36 +0100688 /* the socket is ready */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100689 listener->state = LI_LISTEN;
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200690 return err;
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200691
Willy Tarreauaf9a7f52020-09-01 16:12:50 +0200692 tcp_close_return:
693 close(fd);
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100694 if (msg && errlen) {
695 char pn[INET6_ADDRSTRLEN];
696
Willy Tarreau37159062020-08-27 07:48:42 +0200697 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
698 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100699 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100700 return err;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100701}
702
Willy Tarreau32282382017-09-15 07:44:44 +0200703/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
704 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
705 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200706 *
707 * Must be called with proto_lock held.
708 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100709 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200710static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100711{
712 if (listener->state != LI_INIT)
713 return;
714 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200715 listener->rx.proto = &proto_tcpv4;
Willy Tarreau37159062020-08-27 07:48:42 +0200716 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200717 LIST_ADDQ(&proto_tcpv4.listeners, &listener->rx.proto_list);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100718 proto_tcpv4.nb_listeners++;
719}
720
Willy Tarreau32282382017-09-15 07:44:44 +0200721/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
722 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
723 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200724 *
725 * Must be called with proto_lock held.
726 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100727 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200728static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100729{
730 if (listener->state != LI_INIT)
731 return;
732 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200733 listener->rx.proto = &proto_tcpv6;
Willy Tarreau37159062020-08-27 07:48:42 +0200734 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200735 LIST_ADDQ(&proto_tcpv6.listeners, &listener->rx.proto_list);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100736 proto_tcpv6.nb_listeners++;
737}
738
Willy Tarreau092d8652014-07-07 20:22:12 +0200739/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
740 * was totally stopped, or > 0 if correctly paused.
741 */
742int tcp_pause_listener(struct listener *l)
743{
Willy Tarreau38ba6472020-08-27 08:16:52 +0200744 if (shutdown(l->rx.fd, SHUT_WR) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200745 return -1; /* Solaris dies here */
746
Willy Tarreau38ba6472020-08-27 08:16:52 +0200747 if (listen(l->rx.fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200748 return -1; /* OpenBSD dies here */
749
Willy Tarreau38ba6472020-08-27 08:16:52 +0200750 if (shutdown(l->rx.fd, SHUT_RD) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200751 return -1; /* should always be OK */
752 return 1;
753}
754
Willy Tarreaue6b98942007-10-29 01:09:36 +0100755
756/*
757 * Local variables:
758 * c-indent-level: 8
759 * c-basic-offset: 8
760 * End:
761 */