blob: 56e93916ecef32399935a6eb9d1b0085a316913c [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,
Emeric Bruncf20bf12010-10-22 16:06:11 +020062 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010063 .enable_all = enable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020064 .get_src = sock_get_src,
Willy Tarreauc5a94c92020-08-28 15:19:45 +020065 .get_dst = sock_inet_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020066 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020067 .add = tcpv4_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020068 .addrcmp = sock_inet4_addrcmp,
Willy Tarreaue6b98942007-10-29 01:09:36 +010069 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
70 .nb_listeners = 0,
71};
72
Willy Tarreau0108d902018-11-25 19:14:37 +010073INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
74
Willy Tarreaue6b98942007-10-29 01:09:36 +010075/* Note: must not be declared <const> as its list will be overwritten */
76static struct protocol proto_tcpv6 = {
77 .name = "tcpv6",
78 .sock_domain = AF_INET6,
79 .sock_type = SOCK_STREAM,
80 .sock_prot = IPPROTO_TCP,
81 .sock_family = AF_INET6,
82 .sock_addrlen = sizeof(struct sockaddr_in6),
83 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020084 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020085 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020086 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010087 .enable_all = enable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020088 .get_src = sock_get_src,
Willy Tarreauc5a94c92020-08-28 15:19:45 +020089 .get_dst = sock_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020090 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020091 .add = tcpv6_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020092 .addrcmp = sock_inet6_addrcmp,
Willy Tarreaue6b98942007-10-29 01:09:36 +010093 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
94 .nb_listeners = 0,
95};
96
Willy Tarreau0108d902018-11-25 19:14:37 +010097INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
98
David du Colombier6f5ccb12011-03-10 22:26:24 +010099/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100100 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
101 * - 0 : ignore remote address (may even be a NULL pointer)
102 * - 1 : use provided address
103 * - 2 : use provided port
104 * - 3 : use both
105 *
106 * The function supports multiple foreign binding methods :
107 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100108 * The second one can be used as a fallback for the first one.
109 * This function returns 0 when everything's OK, 1 if it could not bind, to the
110 * local address, 2 if it could not bind to the foreign address.
111 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100112int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100113{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100114 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100115 int foreign_ok = 0;
116 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100117 static THREAD_LOCAL int ip_transp_working = 1;
118 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200119
David du Colombier65c17962012-07-13 14:34:59 +0200120 switch (local->ss_family) {
121 case AF_INET:
122 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200123 /* This deserves some explanation. Some platforms will support
124 * multiple combinations of certain methods, so we try the
125 * supported ones until one succeeds.
126 */
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200127 if (sock_inet4_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200128 foreign_ok = 1;
129 else
130 ip_transp_working = 0;
131 }
132 break;
133 case AF_INET6:
134 if (flags && ip6_transp_working) {
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200135 if (sock_inet6_make_foreign(fd))
David du Colombier65c17962012-07-13 14:34:59 +0200136 foreign_ok = 1;
137 else
138 ip6_transp_working = 0;
139 }
140 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100141 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200142
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100143 if (flags) {
144 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200145 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100146 switch (remote->ss_family) {
147 case AF_INET:
148 if (flags & 1)
149 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
150 if (flags & 2)
151 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
152 break;
153 case AF_INET6:
154 if (flags & 1)
155 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
156 if (flags & 2)
157 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
158 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100159 default:
160 /* we don't want to try to bind to an unknown address family */
161 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100162 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100163 }
164
Simon Hormande072bd2011-06-24 15:11:37 +0900165 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100166 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200167 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200168 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
169 if (ret < 0)
170 return 2;
171 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100172 }
173 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200174 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200175 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
176 if (ret < 0)
177 return 1;
178 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100179 }
180
181 if (!flags)
182 return 0;
183
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100184 if (!foreign_ok)
185 /* we could not bind to a foreign address */
186 return 2;
187
188 return 0;
189}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100190
Willy Tarreau9650f372009-08-16 14:02:45 +0200191/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200192 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200193 * to connection <conn> using (si->{target,dst}). A source address may be
194 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200195 * bind addresses are still determined locally (due to the possible need of a
196 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100197 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100198 * supported. The <data> parameter is a boolean indicating whether there are data
199 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200200 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
201 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100202 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200203 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
204 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200205 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200206 * Note that a pending send_proxy message accounts for data.
207 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200208 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200209 * - SF_ERR_NONE if everything's OK
210 * - SF_ERR_SRVTO if there are no more servers
211 * - SF_ERR_SRVCL if the connection was refused by the server
212 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
213 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
214 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100215 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100216 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200217 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100218 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200219 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100220
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200221int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200222{
223 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100224 struct server *srv;
225 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100226 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100227 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800228 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100229
Olivier Houchard637b6952018-11-23 14:23:07 +0100230 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100231
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100232 switch (obj_type(conn->target)) {
233 case OBJ_TYPE_PROXY:
234 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100235 srv = NULL;
236 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100237 case OBJ_TYPE_SERVER:
238 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100239 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100240 /* Make sure we check that we have data before activating
241 * TFO, or we could trigger a kernel issue whereby after
242 * a successful connect() == 0, any subsequent connect()
243 * will return EINPROGRESS instead of EISCONN.
244 */
245 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
246 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
247 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100248 break;
249 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100250 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200251 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100252 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200253
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200254 if (!conn->dst) {
255 conn->flags |= CO_FL_ERROR;
256 return SF_ERR_INTERNAL;
257 }
258
Willy Tarreau18b7df72020-08-28 12:07:22 +0200259 fd = conn->handle.fd = sock_create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100260
261 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200262 qfprintf(stderr, "Cannot get a server socket.\n");
263
Willy Tarreau9ce70132014-01-24 16:08:19 +0100264 if (errno == ENFILE) {
265 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200266 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100267 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
268 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100269 }
270 else if (errno == EMFILE) {
271 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200272 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100273 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
274 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100275 }
276 else if (errno == ENOBUFS || errno == ENOMEM) {
277 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200278 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100279 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
280 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100281 }
282 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
283 conn->err_code = CO_ER_NOPROTO;
284 }
285 else
286 conn->err_code = CO_ER_SOCK_ERR;
287
Willy Tarreau9650f372009-08-16 14:02:45 +0200288 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100289 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200290 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200291 }
292
293 if (fd >= global.maxsock) {
294 /* do not log anything there, it's a normal condition when this option
295 * is used to serialize connections to a server !
296 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100297 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200298 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100299 conn->err_code = CO_ER_CONF_FDLIM;
300 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200301 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200302 }
303
304 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900305 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200306 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
307 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100308 conn->err_code = CO_ER_SOCK_ERR;
309 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200310 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200311 }
312
William Lallemandc03eb012018-11-27 12:02:37 +0100313 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
314 ha_alert("Cannot set CLOEXEC on client socket.\n");
315 close(fd);
316 conn->err_code = CO_ER_SOCK_ERR;
317 conn->flags |= CO_FL_ERROR;
318 return SF_ERR_INTERNAL;
319 }
320
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900321 if (be->options & PR_O_TCP_SRV_KA) {
Simon Hormande072bd2011-06-24 15:11:37 +0900322 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200323
Willy Tarreau52543212020-07-09 05:58:51 +0200324#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900325 if (be->srvtcpka_cnt)
326 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200327#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900328
Willy Tarreau52543212020-07-09 05:58:51 +0200329#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900330 if (be->srvtcpka_idle)
331 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200332#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900333
Willy Tarreau52543212020-07-09 05:58:51 +0200334#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900335 if (be->srvtcpka_intvl)
336 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200337#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900338 }
339
Willy Tarreau9650f372009-08-16 14:02:45 +0200340 /* allow specific binding :
341 * - server-specific at first
342 * - proxy-specific next
343 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100344 if (srv && srv->conn_src.opts & CO_SRC_BIND)
345 src = &srv->conn_src;
346 else if (be->conn_src.opts & CO_SRC_BIND)
347 src = &be->conn_src;
348 else
349 src = NULL;
350
351 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 int ret, flags = 0;
353
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200354 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100355 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100356 case CO_SRC_TPROXY_CLI:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200357 conn_set_private(conn);
Willy Tarreau387ebf82015-08-04 19:24:13 +0200358 /* fall through */
359 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200360 flags = 3;
361 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100362 case CO_SRC_TPROXY_CIP:
363 case CO_SRC_TPROXY_DYN:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200364 conn_set_private(conn);
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200365 flags = 1;
366 break;
367 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200368 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200369
Willy Tarreau9650f372009-08-16 14:02:45 +0200370#ifdef SO_BINDTODEVICE
371 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100372 if (src->iface_name)
373 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200374#endif
375
Willy Tarreaua4380b42012-12-08 22:49:11 +0100376 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200377 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100378 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200379
380 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200381 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200382
383 do {
384 /* note: in case of retry, we may have to release a previously
385 * allocated port, hence this loop's construct.
386 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200387 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
388 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200389
390 if (!attempts)
391 break;
392 attempts--;
393
Willy Tarreaua4380b42012-12-08 22:49:11 +0100394 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100395 if (!fdinfo[fd].local_port) {
396 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200397 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100398 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200399
Willy Tarreaua4380b42012-12-08 22:49:11 +0100400 fdinfo[fd].port_range = src->sport_range;
401 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200402
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200403 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100404 if (ret != 0)
405 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200406 } while (ret != 0); /* binding NOK */
407 }
408 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000409#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100410 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000411 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
412#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200413 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100414 if (ret != 0)
415 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200416 }
417
Willy Tarreaua4380b42012-12-08 22:49:11 +0100418 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200419 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
420 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200421 close(fd);
422
Willy Tarreau9650f372009-08-16 14:02:45 +0200423 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100424 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
425 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200426 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100427 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200428 be->id);
429 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100430 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
431 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200432 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100433 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200434 be->id);
435 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100436 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200437 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 }
439 }
440
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400441#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 /* disabling tcp quick ack now allows the first request to leave the
443 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100444 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200446 if (flags & (CONNECT_DELACK_ALWAYS) ||
447 ((flags & CONNECT_DELACK_SMART_CONNECT ||
448 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
449 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900450 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200451#endif
452
Willy Tarreau163d4622015-10-13 16:16:41 +0200453#ifdef TCP_USER_TIMEOUT
454 /* there is not much more we can do here when it fails, it's still minor */
455 if (srv && srv->tcp_ut)
456 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
457#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100458
459 if (use_fastopen) {
460#if defined(TCP_FASTOPEN_CONNECT)
461 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
462#endif
463 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100464 if (global.tune.server_sndbuf)
465 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
466
467 if (global.tune.server_rcvbuf)
468 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
469
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200470 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800471 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100472 if (errno == EINPROGRESS || errno == EALREADY) {
473 /* common case, let's wait for connect status */
474 conn->flags |= CO_FL_WAIT_L4_CONN;
475 }
476 else if (errno == EISCONN) {
477 /* should normally not happen but if so, indicates that it's OK */
478 conn->flags &= ~CO_FL_WAIT_L4_CONN;
479 }
480 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200481 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100482 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200483 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100484 conn->err_code = CO_ER_FREE_PORTS;
485 }
486 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200487 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100488 conn->err_code = CO_ER_ADDR_INUSE;
489 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200490
Willy Tarreaub1719512012-12-08 23:03:28 +0100491 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
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 Tarreaub1719512012-12-08 23:03:28 +0100495 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100496 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200497 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200498 } else if (errno == ETIMEDOUT) {
499 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200500 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
501 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200502 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100503 conn->err_code = CO_ER_SOCK_ERR;
504 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200505 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200506 } else {
507 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
508 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200509 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
510 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200511 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100512 conn->err_code = CO_ER_SOCK_ERR;
513 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200514 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200515 }
516 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100517 else {
518 /* connect() == 0, this is great! */
519 conn->flags &= ~CO_FL_WAIT_L4_CONN;
520 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200521
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100522 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200523
Willy Tarreauf79c8172013-10-21 16:30:56 +0200524 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100525 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200526
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100527 if (conn->flags & CO_FL_WAIT_L4_CONN) {
528 fd_want_send(fd);
529 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200530 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100531 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200532
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200533 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200534 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100535 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200536 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200537 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200538
Willy Tarreaue7dff022015-04-03 01:14:29 +0200539 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200540}
541
Willy Tarreaue6b98942007-10-29 01:09:36 +0100542/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100543 * an error message in <errmsg> if the message is at most <errlen> bytes long
544 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
545 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100546 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
547 * was alright and that no message was returned. ERR_RETRYABLE means that an
548 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700549 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100550 * the meaning of the error, but just indicate that a message is present which
551 * should be displayed with the respective level. Last, ERR_ABORT indicates
552 * that it's pointless to try to start other listeners. No error message is
553 * returned if errlen is NULL.
554 */
555int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
556{
557 __label__ tcp_return, tcp_close_return;
558 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100559 int ext, ready;
560 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100561 const char *msg = NULL;
562
563 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100564 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100565 *errmsg = 0;
566
567 if (listener->state != LI_ASSIGNED)
568 return ERR_NONE; /* already bound */
569
570 err = ERR_NONE;
571
Olivier Houchard153659f2017-04-05 22:39:56 +0200572 if (listener->fd == -1)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200573 listener->fd = sock_find_compatible_fd(listener);
Olivier Houchard153659f2017-04-05 22:39:56 +0200574
Willy Tarreau40aa0702013-03-10 23:51:38 +0100575 /* if the listener already has an fd assigned, then we were offered the
576 * fd by an external process (most likely the parent), and we don't want
577 * to create a new socket. However we still want to set a few flags on
578 * the socket.
579 */
580 fd = listener->fd;
581 ext = (fd >= 0);
582
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100583 if (!ext) {
584 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
585
586 if (fd == -1) {
587 err |= ERR_RETRYABLE | ERR_ALERT;
588 msg = "cannot create listening socket";
589 goto tcp_return;
590 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100591 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100592
Willy Tarreaue6b98942007-10-29 01:09:36 +0100593 if (fd >= global.maxsock) {
594 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
595 msg = "not enough free sockets (raise '-n' parameter)";
596 goto tcp_close_return;
597 }
598
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200599 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100600 err |= ERR_FATAL | ERR_ALERT;
601 msg = "cannot make socket non-blocking";
602 goto tcp_close_return;
603 }
604
Willy Tarreau40aa0702013-03-10 23:51:38 +0100605 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100606 /* not fatal but should be reported */
607 msg = "cannot do so_reuseaddr";
608 err |= ERR_ALERT;
609 }
610
611 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900612 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200613 else {
614 struct linger tmplinger;
615 socklen_t len = sizeof(tmplinger);
616 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
617 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
618 tmplinger.l_onoff = 0;
619 tmplinger.l_linger = 0;
620 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
621 sizeof(tmplinger));
622 }
623 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100624
Willy Tarreaue6b98942007-10-29 01:09:36 +0100625#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000626 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
627 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100628 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000629 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100630 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100631#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200632
Willy Tarreau40aa0702013-03-10 23:51:38 +0100633 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200634 switch (listener->addr.ss_family) {
635 case AF_INET:
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200636 if (!sock_inet4_make_foreign(fd)) {
David du Colombier65c17962012-07-13 14:34:59 +0200637 msg = "cannot make listening socket transparent";
638 err |= ERR_ALERT;
639 }
640 break;
641 case AF_INET6:
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200642 if (!sock_inet6_make_foreign(fd)) {
David du Colombier65c17962012-07-13 14:34:59 +0200643 msg = "cannot make listening socket transparent";
644 err |= ERR_ALERT;
645 }
646 break;
647 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100648 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200649
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100650#ifdef SO_BINDTODEVICE
651 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100652 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100653 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100654 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100655 msg = "cannot bind listener to device";
656 err |= ERR_WARN;
657 }
658 }
659#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400660#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100661 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400662 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200663 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
664 msg = "cannot set MSS";
665 err |= ERR_WARN;
666 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200667 } else if (ext) {
668 int tmpmaxseg = -1;
669 int defaultmss;
670 socklen_t len = sizeof(tmpmaxseg);
671
672 if (listener->addr.ss_family == AF_INET)
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200673 defaultmss = sock_inet_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200674 else
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200675 defaultmss = sock_inet6_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200676
677 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100678 if (defaultmss > 0 &&
679 tmpmaxseg != defaultmss &&
680 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200681 msg = "cannot set MSS";
682 err |= ERR_WARN;
683 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200684 }
685#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100686#if defined(TCP_USER_TIMEOUT)
687 if (listener->tcp_ut) {
688 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
689 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
690 msg = "cannot set TCP User Timeout";
691 err |= ERR_WARN;
692 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200693 } else
694 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
695 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100696#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200697#if defined(TCP_DEFER_ACCEPT)
698 if (listener->options & LI_O_DEF_ACCEPT) {
699 /* defer accept by up to one second */
700 int accept_delay = 1;
701 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
702 msg = "cannot enable DEFER_ACCEPT";
703 err |= ERR_WARN;
704 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200705 } else
706 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
707 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200708#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200709#if defined(TCP_FASTOPEN)
710 if (listener->options & LI_O_TCP_FO) {
711 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100712 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200713 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
714 msg = "cannot enable TCP_FASTOPEN";
715 err |= ERR_WARN;
716 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200717 } else {
718 socklen_t len;
719 int qlen;
720 len = sizeof(qlen);
721 /* Only disable fast open if it was enabled, we don't want
722 * the kernel to create a fast open queue if there's none.
723 */
724 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
725 qlen != 0) {
726 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
727 sizeof(zero)) == -1) {
728 msg = "cannot disable TCP_FASTOPEN";
729 err |= ERR_WARN;
730 }
731 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200732 }
733#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100734#if defined(IPV6_V6ONLY)
Willy Tarreaubbb284d2020-08-26 10:21:06 +0200735 if (!ext && listener->options & LI_O_V6ONLY)
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100736 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreaubbb284d2020-08-26 10:21:06 +0200737 else if (!ext && listener->options & LI_O_V4V6)
Willy Tarreau77e3af92012-11-24 15:07:23 +0100738 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100739#endif
740
Willy Tarreau40aa0702013-03-10 23:51:38 +0100741 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100742 err |= ERR_RETRYABLE | ERR_ALERT;
743 msg = "cannot bind socket";
744 goto tcp_close_return;
745 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100746
Willy Tarreau40aa0702013-03-10 23:51:38 +0100747 ready = 0;
748 ready_len = sizeof(ready);
749 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
750 ready = 0;
751
752 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100753 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100754 err |= ERR_RETRYABLE | ERR_ALERT;
755 msg = "cannot listen to socket";
756 goto tcp_close_return;
757 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100758
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400759#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200760 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900761 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200762 else
763 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200764#endif
765
Willy Tarreaue6b98942007-10-29 01:09:36 +0100766 /* the socket is ready */
767 listener->fd = fd;
768 listener->state = LI_LISTEN;
769
Willy Tarreaua9786b62018-01-25 07:22:13 +0100770 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100771 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaueb472682010-05-28 18:46:57 +0200772
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200773 /* for now, all regularly bound TCP listeners are exportable */
774 if (!(listener->options & LI_O_INHERITED))
775 fdtab[fd].exported = 1;
776
Willy Tarreaue6b98942007-10-29 01:09:36 +0100777 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100778 if (msg && errlen) {
779 char pn[INET6_ADDRSTRLEN];
780
Willy Tarreau631f01c2011-09-05 00:36:48 +0200781 addr_to_str(&listener->addr, pn, sizeof(pn));
782 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100783 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100784 return err;
785
786 tcp_close_return:
787 close(fd);
788 goto tcp_return;
789}
790
Willy Tarreau32282382017-09-15 07:44:44 +0200791/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
792 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
793 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200794 *
795 * Must be called with proto_lock held.
796 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100797 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200798static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100799{
800 if (listener->state != LI_INIT)
801 return;
802 listener->state = LI_ASSIGNED;
803 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +0200804 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100805 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
806 proto_tcpv4.nb_listeners++;
807}
808
Willy Tarreau32282382017-09-15 07:44:44 +0200809/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
810 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
811 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200812 *
813 * Must be called with proto_lock held.
814 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100815 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200816static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100817{
818 if (listener->state != LI_INIT)
819 return;
820 listener->state = LI_ASSIGNED;
821 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +0200822 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100823 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
824 proto_tcpv6.nb_listeners++;
825}
826
Willy Tarreau092d8652014-07-07 20:22:12 +0200827/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
828 * was totally stopped, or > 0 if correctly paused.
829 */
830int tcp_pause_listener(struct listener *l)
831{
832 if (shutdown(l->fd, SHUT_WR) != 0)
833 return -1; /* Solaris dies here */
834
Willy Tarreaue2711c72019-02-27 15:39:41 +0100835 if (listen(l->fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200836 return -1; /* OpenBSD dies here */
837
838 if (shutdown(l->fd, SHUT_RD) != 0)
839 return -1; /* should always be OK */
840 return 1;
841}
842
Willy Tarreaue6b98942007-10-29 01:09:36 +0100843
844/*
845 * Local variables:
846 * c-indent-level: 8
847 * c-basic-offset: 8
848 * End:
849 */