blob: 356ee3ac6871bb350912c2c0e01a4c45df233fcb [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{
559 __label__ tcp_return, tcp_close_return;
560 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100561 int ext, ready;
562 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100563 const char *msg = NULL;
564
565 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100566 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100567 *errmsg = 0;
568
569 if (listener->state != LI_ASSIGNED)
570 return ERR_NONE; /* already bound */
571
572 err = ERR_NONE;
573
Willy Tarreau0b915012020-09-01 10:47:07 +0200574 if (listener->rx.flags & RX_F_BOUND)
575 goto bound;
576
Willy Tarreau38ba6472020-08-27 08:16:52 +0200577 if (listener->rx.fd == -1)
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200578 listener->rx.fd = sock_find_compatible_fd(&listener->rx);
Olivier Houchard153659f2017-04-05 22:39:56 +0200579
Willy Tarreau40aa0702013-03-10 23:51:38 +0100580 /* if the listener already has an fd assigned, then we were offered the
581 * fd by an external process (most likely the parent), and we don't want
582 * to create a new socket. However we still want to set a few flags on
583 * the socket.
584 */
Willy Tarreau38ba6472020-08-27 08:16:52 +0200585 fd = listener->rx.fd;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100586 ext = (fd >= 0);
587
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100588 if (!ext) {
Willy Tarreau818a92e2020-09-03 07:50:19 +0200589 fd = my_socketat(listener->rx.settings->netns, listener->rx.addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100590
591 if (fd == -1) {
592 err |= ERR_RETRYABLE | ERR_ALERT;
593 msg = "cannot create listening socket";
594 goto tcp_return;
595 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100596 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100597
Willy Tarreaue6b98942007-10-29 01:09:36 +0100598 if (fd >= global.maxsock) {
599 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
600 msg = "not enough free sockets (raise '-n' parameter)";
601 goto tcp_close_return;
602 }
603
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200604 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100605 err |= ERR_FATAL | ERR_ALERT;
606 msg = "cannot make socket non-blocking";
607 goto tcp_close_return;
608 }
609
Willy Tarreau40aa0702013-03-10 23:51:38 +0100610 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100611 /* not fatal but should be reported */
612 msg = "cannot do so_reuseaddr";
613 err |= ERR_ALERT;
614 }
615
616 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900617 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200618 else {
619 struct linger tmplinger;
620 socklen_t len = sizeof(tmplinger);
621 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
622 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
623 tmplinger.l_onoff = 0;
624 tmplinger.l_linger = 0;
625 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
626 sizeof(tmplinger));
627 }
628 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100629
Willy Tarreaue6b98942007-10-29 01:09:36 +0100630#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000631 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
632 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100633 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000634 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100635 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100636#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200637
Willy Tarreau3fd3bdc2020-09-01 15:12:08 +0200638 if (!ext && (listener->rx.settings->options & RX_O_FOREIGN)) {
Willy Tarreau37159062020-08-27 07:48:42 +0200639 switch (listener->rx.addr.ss_family) {
David du Colombier65c17962012-07-13 14:34:59 +0200640 case AF_INET:
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200641 if (!sock_inet4_make_foreign(fd)) {
David du Colombier65c17962012-07-13 14:34:59 +0200642 msg = "cannot make listening socket transparent";
643 err |= ERR_ALERT;
644 }
645 break;
646 case AF_INET6:
Willy Tarreau37bafdc2020-08-28 17:23:40 +0200647 if (!sock_inet6_make_foreign(fd)) {
David du Colombier65c17962012-07-13 14:34:59 +0200648 msg = "cannot make listening socket transparent";
649 err |= ERR_ALERT;
650 }
651 break;
652 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100653 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200654
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100655#ifdef SO_BINDTODEVICE
656 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau818a92e2020-09-03 07:50:19 +0200657 if (!ext && listener->rx.settings->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100658 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau818a92e2020-09-03 07:50:19 +0200659 listener->rx.settings->interface,
660 strlen(listener->rx.settings->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100661 msg = "cannot bind listener to device";
662 err |= ERR_WARN;
663 }
664 }
665#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400666#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100667 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400668 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200669 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
670 msg = "cannot set MSS";
671 err |= ERR_WARN;
672 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200673 } else if (ext) {
674 int tmpmaxseg = -1;
675 int defaultmss;
676 socklen_t len = sizeof(tmpmaxseg);
677
Willy Tarreau37159062020-08-27 07:48:42 +0200678 if (listener->rx.addr.ss_family == AF_INET)
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200679 defaultmss = sock_inet_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200680 else
Willy Tarreaue5bdc512020-08-28 18:03:10 +0200681 defaultmss = sock_inet6_tcp_maxseg_default;
Olivier Houchard153659f2017-04-05 22:39:56 +0200682
683 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100684 if (defaultmss > 0 &&
685 tmpmaxseg != defaultmss &&
686 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200687 msg = "cannot set MSS";
688 err |= ERR_WARN;
689 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200690 }
691#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100692#if defined(TCP_USER_TIMEOUT)
693 if (listener->tcp_ut) {
694 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
695 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
696 msg = "cannot set TCP User Timeout";
697 err |= ERR_WARN;
698 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200699 } else
700 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
701 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100702#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200703#if defined(TCP_DEFER_ACCEPT)
704 if (listener->options & LI_O_DEF_ACCEPT) {
705 /* defer accept by up to one second */
706 int accept_delay = 1;
707 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
708 msg = "cannot enable DEFER_ACCEPT";
709 err |= ERR_WARN;
710 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200711 } else
712 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
713 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200714#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200715#if defined(TCP_FASTOPEN)
716 if (listener->options & LI_O_TCP_FO) {
717 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100718 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200719 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
720 msg = "cannot enable TCP_FASTOPEN";
721 err |= ERR_WARN;
722 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200723 } else {
724 socklen_t len;
725 int qlen;
726 len = sizeof(qlen);
727 /* Only disable fast open if it was enabled, we don't want
728 * the kernel to create a fast open queue if there's none.
729 */
730 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
731 qlen != 0) {
732 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
733 sizeof(zero)) == -1) {
734 msg = "cannot disable TCP_FASTOPEN";
735 err |= ERR_WARN;
736 }
737 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200738 }
739#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100740#if defined(IPV6_V6ONLY)
Willy Tarreau3fd3bdc2020-09-01 15:12:08 +0200741 if (!ext && listener->rx.addr.ss_family == AF_INET6) {
742 /* Prepare to match the v6only option against what we really want. Note
743 * that sadly the two options are not exclusive to each other and that
744 * v6only is stronger than v4v6.
745 */
746 if ((listener->rx.settings->options & RX_O_V6ONLY) ||
747 (sock_inet6_v6only_default && !(listener->rx.settings->options & RX_O_V4V6)))
748 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
749 else
750 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
751 }
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100752#endif
753
Willy Tarreaub7436612020-08-28 19:51:44 +0200754 if (!ext && bind(fd, (struct sockaddr *)&listener->rx.addr, listener->rx.proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100755 err |= ERR_RETRYABLE | ERR_ALERT;
756 msg = "cannot bind socket";
757 goto tcp_close_return;
758 }
Willy Tarreau0b915012020-09-01 10:47:07 +0200759 listener->rx.flags |= RX_F_BOUND;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100760
Willy Tarreau0b915012020-09-01 10:47:07 +0200761 bound:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100762 ready = 0;
763 ready_len = sizeof(ready);
764 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
765 ready = 0;
766
767 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100768 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100769 err |= ERR_RETRYABLE | ERR_ALERT;
770 msg = "cannot listen to socket";
771 goto tcp_close_return;
772 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100773
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400774#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200775 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900776 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200777 else
778 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200779#endif
780
Willy Tarreaue6b98942007-10-29 01:09:36 +0100781 /* the socket is ready */
Willy Tarreau38ba6472020-08-27 08:16:52 +0200782 listener->rx.fd = fd;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100783 listener->state = LI_LISTEN;
784
Willy Tarreaub7436612020-08-28 19:51:44 +0200785 fd_insert(fd, listener, listener->rx.proto->accept,
Willy Tarreau818a92e2020-09-03 07:50:19 +0200786 thread_mask(listener->rx.settings->bind_thread) & all_threads_mask);
Willy Tarreaueb472682010-05-28 18:46:57 +0200787
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200788 /* for now, all regularly bound TCP listeners are exportable */
Willy Tarreau43046fa2020-09-01 15:41:59 +0200789 if (!(listener->rx.flags & RX_F_INHERITED))
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200790 fdtab[fd].exported = 1;
791
Willy Tarreaue6b98942007-10-29 01:09:36 +0100792 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100793 if (msg && errlen) {
794 char pn[INET6_ADDRSTRLEN];
795
Willy Tarreau37159062020-08-27 07:48:42 +0200796 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
797 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100798 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100799 return err;
800
801 tcp_close_return:
802 close(fd);
803 goto tcp_return;
804}
805
Willy Tarreau32282382017-09-15 07:44:44 +0200806/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
807 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
808 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200809 *
810 * Must be called with proto_lock held.
811 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100812 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200813static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100814{
815 if (listener->state != LI_INIT)
816 return;
817 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200818 listener->rx.proto = &proto_tcpv4;
Willy Tarreau37159062020-08-27 07:48:42 +0200819 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200820 LIST_ADDQ(&proto_tcpv4.listeners, &listener->rx.proto_list);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100821 proto_tcpv4.nb_listeners++;
822}
823
Willy Tarreau32282382017-09-15 07:44:44 +0200824/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
825 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
826 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200827 *
828 * Must be called with proto_lock held.
829 *
Willy Tarreaue6b98942007-10-29 01:09:36 +0100830 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200831static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100832{
833 if (listener->state != LI_INIT)
834 return;
835 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200836 listener->rx.proto = &proto_tcpv6;
Willy Tarreau37159062020-08-27 07:48:42 +0200837 ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
Willy Tarreaub7436612020-08-28 19:51:44 +0200838 LIST_ADDQ(&proto_tcpv6.listeners, &listener->rx.proto_list);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100839 proto_tcpv6.nb_listeners++;
840}
841
Willy Tarreau092d8652014-07-07 20:22:12 +0200842/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
843 * was totally stopped, or > 0 if correctly paused.
844 */
845int tcp_pause_listener(struct listener *l)
846{
Willy Tarreau38ba6472020-08-27 08:16:52 +0200847 if (shutdown(l->rx.fd, SHUT_WR) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200848 return -1; /* Solaris dies here */
849
Willy Tarreau38ba6472020-08-27 08:16:52 +0200850 if (listen(l->rx.fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200851 return -1; /* OpenBSD dies here */
852
Willy Tarreau38ba6472020-08-27 08:16:52 +0200853 if (shutdown(l->rx.fd, SHUT_RD) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +0200854 return -1; /* should always be OK */
855 return 1;
856}
857
Willy Tarreaue6b98942007-10-29 01:09:36 +0100858
859/*
860 * Local variables:
861 * c-indent-level: 8
862 * c-basic-offset: 8
863 * End:
864 */