blob: b2733d650231ec6bf30db29c8ef1b805752c97b5 [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
Baptiste Assmann39a5f222016-08-08 14:12:08 +020013/* this is to have tcp_info defined on systems using musl
14 * library, such as Alpine Linux
15 */
16#define _GNU_SOURCE
17
Willy Tarreaue6b98942007-10-29 01:09:36 +010018#include <ctype.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <time.h>
25
26#include <sys/param.h>
27#include <sys/socket.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010028#include <sys/types.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010029
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040030#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020031#include <netinet/in.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040032
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020033#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/arg.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020035#include <haproxy/channel.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020036#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020037#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/fd.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020039#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020040#include <haproxy/http_rules.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020041#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020042#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020043#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020044#include <haproxy/namespace.h>
45#include <haproxy/port_range.h>
46#include <haproxy/proto_tcp.h>
47#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020048#include <haproxy/proxy-t.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020049#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020050#include <haproxy/server.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020051#include <haproxy/stream-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020052#include <haproxy/tcp_rules.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020053#include <haproxy/tools.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010054
Willy Tarreaue6b98942007-10-29 01:09:36 +010055
Emeric Bruncf20bf12010-10-22 16:06:11 +020056static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
57static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020058static void tcpv4_add_listener(struct listener *listener, int port);
59static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010060
61/* Note: must not be declared <const> as its list will be overwritten */
62static struct protocol proto_tcpv4 = {
63 .name = "tcpv4",
64 .sock_domain = AF_INET,
65 .sock_type = SOCK_STREAM,
66 .sock_prot = IPPROTO_TCP,
67 .sock_family = AF_INET,
68 .sock_addrlen = sizeof(struct sockaddr_in),
69 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020070 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020071 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020072 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010073 .bind_all = tcp_bind_listeners,
74 .unbind_all = unbind_all_listeners,
75 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020076 .get_src = tcp_get_src,
77 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020078 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020079 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010080 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
81 .nb_listeners = 0,
82};
83
Willy Tarreau0108d902018-11-25 19:14:37 +010084INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
85
Willy Tarreaue6b98942007-10-29 01:09:36 +010086/* Note: must not be declared <const> as its list will be overwritten */
87static struct protocol proto_tcpv6 = {
88 .name = "tcpv6",
89 .sock_domain = AF_INET6,
90 .sock_type = SOCK_STREAM,
91 .sock_prot = IPPROTO_TCP,
92 .sock_family = AF_INET6,
93 .sock_addrlen = sizeof(struct sockaddr_in6),
94 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020095 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020096 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020097 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010098 .bind_all = tcp_bind_listeners,
99 .unbind_all = unbind_all_listeners,
100 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200101 .get_src = tcp_get_src,
102 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +0200103 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200104 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100105 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
106 .nb_listeners = 0,
107};
108
Willy Tarreau0108d902018-11-25 19:14:37 +0100109INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
110
Olivier Houchard153659f2017-04-05 22:39:56 +0200111/* Default TCP parameters, got by opening a temporary TCP socket. */
112#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100113static THREAD_LOCAL int default_tcp_maxseg = -1;
114static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200115#endif
116
Willy Tarreaubca5a4e2020-08-26 09:29:21 +0200117/* determine if the operating system uses IPV6_V6ONLY by default.
118 * -1=unknown, 0=no, 1=yes.
119 */
120static int v6only_default = -1;
121
David du Colombier6f5ccb12011-03-10 22:26:24 +0100122/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100123 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
124 * - 0 : ignore remote address (may even be a NULL pointer)
125 * - 1 : use provided address
126 * - 2 : use provided port
127 * - 3 : use both
128 *
129 * The function supports multiple foreign binding methods :
130 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100131 * The second one can be used as a fallback for the first one.
132 * This function returns 0 when everything's OK, 1 if it could not bind, to the
133 * local address, 2 if it could not bind to the foreign address.
134 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100135int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100136{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100137 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100138 int foreign_ok = 0;
139 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100140 static THREAD_LOCAL int ip_transp_working = 1;
141 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200142
David du Colombier65c17962012-07-13 14:34:59 +0200143 switch (local->ss_family) {
144 case AF_INET:
145 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200146 /* This deserves some explanation. Some platforms will support
147 * multiple combinations of certain methods, so we try the
148 * supported ones until one succeeds.
149 */
150 if (0
151#if defined(IP_TRANSPARENT)
152 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
153#endif
154#if defined(IP_FREEBIND)
155 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
156#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200157#if defined(IP_BINDANY)
158 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
159#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200160#if defined(SO_BINDANY)
161 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
162#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200163 )
David du Colombier65c17962012-07-13 14:34:59 +0200164 foreign_ok = 1;
165 else
166 ip_transp_working = 0;
167 }
168 break;
169 case AF_INET6:
170 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200171 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200172#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200173 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
174#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100175#if defined(IP_FREEBIND)
176 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
177#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200178#if defined(IPV6_BINDANY)
179 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
180#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200181#if defined(SO_BINDANY)
182 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
183#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200184 )
David du Colombier65c17962012-07-13 14:34:59 +0200185 foreign_ok = 1;
186 else
187 ip6_transp_working = 0;
188 }
189 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100190 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200191
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100192 if (flags) {
193 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200194 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100195 switch (remote->ss_family) {
196 case AF_INET:
197 if (flags & 1)
198 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
199 if (flags & 2)
200 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
201 break;
202 case AF_INET6:
203 if (flags & 1)
204 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
205 if (flags & 2)
206 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
207 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100208 default:
209 /* we don't want to try to bind to an unknown address family */
210 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100211 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100212 }
213
Simon Hormande072bd2011-06-24 15:11:37 +0900214 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100215 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200216 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200217 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
218 if (ret < 0)
219 return 2;
220 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100221 }
222 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200223 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200224 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
225 if (ret < 0)
226 return 1;
227 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100228 }
229
230 if (!flags)
231 return 0;
232
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100233 if (!foreign_ok)
234 /* we could not bind to a foreign address */
235 return 2;
236
237 return 0;
238}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100239
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200240/* conn->dst MUST be valid */
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100241static int create_server_socket(struct connection *conn)
242{
Willy Tarreau529c1392014-12-24 13:47:55 +0100243 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100244
Willy Tarreaue5733232019-05-22 19:24:06 +0200245#ifdef USE_NS
Willy Tarreau529c1392014-12-24 13:47:55 +0100246 if (objt_server(conn->target)) {
247 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
248 ns = conn->proxy_netns;
249 else
250 ns = __objt_server(conn->target)->netns;
251 }
252#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200253 return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, IPPROTO_TCP);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100254}
Willy Tarreau9650f372009-08-16 14:02:45 +0200255
256/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200257 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200258 * to connection <conn> using (si->{target,dst}). A source address may be
259 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200260 * bind addresses are still determined locally (due to the possible need of a
261 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100262 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100263 * supported. The <data> parameter is a boolean indicating whether there are data
264 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200265 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
266 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100267 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200268 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
269 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200270 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200271 * Note that a pending send_proxy message accounts for data.
272 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200273 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200274 * - SF_ERR_NONE if everything's OK
275 * - SF_ERR_SRVTO if there are no more servers
276 * - SF_ERR_SRVCL if the connection was refused by the server
277 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
278 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
279 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100280 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100281 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200282 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100283 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200284 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100285
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200286int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200287{
288 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100289 struct server *srv;
290 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100291 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100292 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800293 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100294
Olivier Houchard637b6952018-11-23 14:23:07 +0100295 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100296
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100297 switch (obj_type(conn->target)) {
298 case OBJ_TYPE_PROXY:
299 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100300 srv = NULL;
301 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100302 case OBJ_TYPE_SERVER:
303 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100304 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100305 /* Make sure we check that we have data before activating
306 * TFO, or we could trigger a kernel issue whereby after
307 * a successful connect() == 0, any subsequent connect()
308 * will return EINPROGRESS instead of EISCONN.
309 */
310 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
311 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
312 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100313 break;
314 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100315 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200316 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100317 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200318
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200319 if (!conn->dst) {
320 conn->flags |= CO_FL_ERROR;
321 return SF_ERR_INTERNAL;
322 }
323
Willy Tarreau585744b2017-08-24 14:31:19 +0200324 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100325
326 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200327 qfprintf(stderr, "Cannot get a server socket.\n");
328
Willy Tarreau9ce70132014-01-24 16:08:19 +0100329 if (errno == ENFILE) {
330 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200331 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100332 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
333 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100334 }
335 else if (errno == EMFILE) {
336 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200337 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100338 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
339 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100340 }
341 else if (errno == ENOBUFS || errno == ENOMEM) {
342 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200343 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100344 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
345 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100346 }
347 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
348 conn->err_code = CO_ER_NOPROTO;
349 }
350 else
351 conn->err_code = CO_ER_SOCK_ERR;
352
Willy Tarreau9650f372009-08-16 14:02:45 +0200353 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100354 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200355 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 }
357
358 if (fd >= global.maxsock) {
359 /* do not log anything there, it's a normal condition when this option
360 * is used to serialize connections to a server !
361 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100362 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200363 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100364 conn->err_code = CO_ER_CONF_FDLIM;
365 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200366 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200367 }
368
369 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900370 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200371 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
372 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100373 conn->err_code = CO_ER_SOCK_ERR;
374 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200375 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200376 }
377
William Lallemandc03eb012018-11-27 12:02:37 +0100378 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
379 ha_alert("Cannot set CLOEXEC on client socket.\n");
380 close(fd);
381 conn->err_code = CO_ER_SOCK_ERR;
382 conn->flags |= CO_FL_ERROR;
383 return SF_ERR_INTERNAL;
384 }
385
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900386 if (be->options & PR_O_TCP_SRV_KA) {
Simon Hormande072bd2011-06-24 15:11:37 +0900387 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200388
Willy Tarreau52543212020-07-09 05:58:51 +0200389#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900390 if (be->srvtcpka_cnt)
391 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200392#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900393
Willy Tarreau52543212020-07-09 05:58:51 +0200394#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900395 if (be->srvtcpka_idle)
396 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200397#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900398
Willy Tarreau52543212020-07-09 05:58:51 +0200399#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900400 if (be->srvtcpka_intvl)
401 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200402#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900403 }
404
Willy Tarreau9650f372009-08-16 14:02:45 +0200405 /* allow specific binding :
406 * - server-specific at first
407 * - proxy-specific next
408 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100409 if (srv && srv->conn_src.opts & CO_SRC_BIND)
410 src = &srv->conn_src;
411 else if (be->conn_src.opts & CO_SRC_BIND)
412 src = &be->conn_src;
413 else
414 src = NULL;
415
416 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200417 int ret, flags = 0;
418
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200419 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100420 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100421 case CO_SRC_TPROXY_CLI:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200422 conn_set_private(conn);
Willy Tarreau387ebf82015-08-04 19:24:13 +0200423 /* fall through */
424 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200425 flags = 3;
426 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100427 case CO_SRC_TPROXY_CIP:
428 case CO_SRC_TPROXY_DYN:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200429 conn_set_private(conn);
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200430 flags = 1;
431 break;
432 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200433 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200434
Willy Tarreau9650f372009-08-16 14:02:45 +0200435#ifdef SO_BINDTODEVICE
436 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100437 if (src->iface_name)
438 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200439#endif
440
Willy Tarreaua4380b42012-12-08 22:49:11 +0100441 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100443 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200444
445 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200446 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200447
448 do {
449 /* note: in case of retry, we may have to release a previously
450 * allocated port, hence this loop's construct.
451 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200452 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
453 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200454
455 if (!attempts)
456 break;
457 attempts--;
458
Willy Tarreaua4380b42012-12-08 22:49:11 +0100459 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100460 if (!fdinfo[fd].local_port) {
461 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200462 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100463 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200464
Willy Tarreaua4380b42012-12-08 22:49:11 +0100465 fdinfo[fd].port_range = src->sport_range;
466 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200467
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200468 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100469 if (ret != 0)
470 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200471 } while (ret != 0); /* binding NOK */
472 }
473 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000474#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100475 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000476 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
477#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200478 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100479 if (ret != 0)
480 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200481 }
482
Willy Tarreaua4380b42012-12-08 22:49:11 +0100483 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200484 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
485 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200486 close(fd);
487
Willy Tarreau9650f372009-08-16 14:02:45 +0200488 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100489 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
490 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200491 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100492 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200493 be->id);
494 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100495 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
496 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200497 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100498 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200499 be->id);
500 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100501 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200502 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200503 }
504 }
505
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400506#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200507 /* disabling tcp quick ack now allows the first request to leave the
508 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100509 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200510 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200511 if (flags & (CONNECT_DELACK_ALWAYS) ||
512 ((flags & CONNECT_DELACK_SMART_CONNECT ||
513 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
514 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900515 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200516#endif
517
Willy Tarreau163d4622015-10-13 16:16:41 +0200518#ifdef TCP_USER_TIMEOUT
519 /* there is not much more we can do here when it fails, it's still minor */
520 if (srv && srv->tcp_ut)
521 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
522#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100523
524 if (use_fastopen) {
525#if defined(TCP_FASTOPEN_CONNECT)
526 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
527#endif
528 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100529 if (global.tune.server_sndbuf)
530 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
531
532 if (global.tune.server_rcvbuf)
533 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
534
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200535 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800536 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100537 if (errno == EINPROGRESS || errno == EALREADY) {
538 /* common case, let's wait for connect status */
539 conn->flags |= CO_FL_WAIT_L4_CONN;
540 }
541 else if (errno == EISCONN) {
542 /* should normally not happen but if so, indicates that it's OK */
543 conn->flags &= ~CO_FL_WAIT_L4_CONN;
544 }
545 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200546 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100547 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200548 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100549 conn->err_code = CO_ER_FREE_PORTS;
550 }
551 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200552 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100553 conn->err_code = CO_ER_ADDR_INUSE;
554 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200555
Willy Tarreaub1719512012-12-08 23:03:28 +0100556 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200557 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
558 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200559 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100560 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100561 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200562 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200563 } else if (errno == ETIMEDOUT) {
564 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200565 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
566 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200567 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100568 conn->err_code = CO_ER_SOCK_ERR;
569 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200570 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200571 } else {
572 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
573 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200574 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
575 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200576 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100577 conn->err_code = CO_ER_SOCK_ERR;
578 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200579 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200580 }
581 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100582 else {
583 /* connect() == 0, this is great! */
584 conn->flags &= ~CO_FL_WAIT_L4_CONN;
585 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200586
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100587 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200588
Willy Tarreauf79c8172013-10-21 16:30:56 +0200589 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100590 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200591
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100592 if (conn->flags & CO_FL_WAIT_L4_CONN) {
593 fd_want_send(fd);
594 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200595 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100596 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200597
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200598 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200599 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100600 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200601 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200602 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200603
Willy Tarreaue7dff022015-04-03 01:14:29 +0200604 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200605}
606
607
Willy Tarreau59b94792012-05-11 16:16:40 +0200608/*
609 * Retrieves the source address for the socket <fd>, with <dir> indicating
610 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
611 * success, -1 in case of error. The socket's source address is stored in
612 * <sa> for <salen> bytes.
613 */
614int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
615{
616 if (dir)
617 return getsockname(fd, sa, &salen);
618 else
619 return getpeername(fd, sa, &salen);
620}
621
622
623/*
624 * Retrieves the original destination address for the socket <fd>, with <dir>
625 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
626 * listener, if the original destination address was translated, the original
627 * address is retrieved. It returns 0 in case of success, -1 in case of error.
628 * The socket's source address is stored in <sa> for <salen> bytes.
629 */
630int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
631{
632 if (dir)
633 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100634 else {
635 int ret = getsockname(fd, sa, &salen);
636
637 if (ret < 0)
638 return ret;
639
Willy Tarreaue5733232019-05-22 19:24:06 +0200640#if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100641 /* For TPROXY and Netfilter's NAT, we can retrieve the original
642 * IPv4 address before DNAT/REDIRECT. We must not do that with
643 * other families because v6-mapped IPv4 addresses are still
644 * reported as v4.
645 */
646 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
647 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
648 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200649#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100650 return ret;
651 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200652}
653
Olivier Houchard153659f2017-04-05 22:39:56 +0200654/* XXX: Should probably be elsewhere */
655static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
656{
657 if (a->ss_family != b->ss_family) {
658 return (-1);
659 }
660 switch (a->ss_family) {
661 case AF_INET:
662 {
663 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
664 if (a4->sin_port != b4->sin_port)
665 return (-1);
666 return (memcmp(&a4->sin_addr, &b4->sin_addr,
667 sizeof(a4->sin_addr)));
668 }
669 case AF_INET6:
670 {
671 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
672 if (a6->sin6_port != b6->sin6_port)
673 return (-1);
674 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
675 sizeof(a6->sin6_addr)));
676 }
677 default:
678 return (-1);
679 }
680
Willy Tarreaubca5a4e2020-08-26 09:29:21 +0200681}
682
Willy Tarreaubf3b06b2020-08-26 10:23:40 +0200683/* Returns true if the passed FD corresponds to a socket bound with LI_O_FOREIGN
684 * according to the various supported socket options. The socket's address family
685 * must be passed in <family>.
686 */
687int tcp_is_foreign(int fd, sa_family_t family)
688{
689 int val __maybe_unused;
690 socklen_t len __maybe_unused;
691
692 switch (family) {
693 case AF_INET:
694#if defined(IP_TRANSPARENT)
695 val = 0; len = sizeof(val);
696 if (getsockopt(fd, SOL_IP, IP_TRANSPARENT, &val, &len) == 0 && val)
697 return 1;
698#endif
699#if defined(IP_FREEBIND)
700 val = 0; len = sizeof(val);
701 if (getsockopt(fd, SOL_IP, IP_FREEBIND, &val, &len) == 0 && val)
702 return 1;
703#endif
704#if defined(IP_BINDANY)
705 val = 0; len = sizeof(val);
706 if (getsockopt(fd, IPPROTO_IP, IP_BINDANY, &val, &len) == 0 && val)
707 return 1;
708#endif
709#if defined(SO_BINDANY)
710 val = 0; len = sizeof(val);
711 if (getsockopt(fd, SOL_SOCKET, SO_BINDANY, &val, &len) == 0 && val)
712 return 1;
713#endif
714 break;
715
716 case AF_INET6:
717#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
718 val = 0; len = sizeof(val);
719 if (getsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &val, &len) == 0 && val)
720 return 1;
721#endif
722#if defined(IP_FREEBIND)
723 val = 0; len = sizeof(val);
724 if (getsockopt(fd, SOL_IP, IP_FREEBIND, &val, &len) == 0 && val)
725 return 1;
726#endif
727#if defined(IPV6_BINDANY)
728 val = 0; len = sizeof(val);
729 if (getsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &val, &len) == 0 && val)
730 return 1;
731#endif
732#if defined(SO_BINDANY)
733 val = 0; len = sizeof(val);
734 if (getsockopt(fd, SOL_SOCKET, SO_BINDANY, &val, &len) == 0 && val)
735 return 1;
736#endif
737 break;
738 }
739 return 0;
740}
741
Willy Tarreaubca5a4e2020-08-26 09:29:21 +0200742/* sets the v6only_default flag according to the OS' default settings; for
743 * simplicity it's set to zero if not supported.
744 */
745static inline void tcp_test_v6only_default()
746{
747 if (v6only_default == -1) {
748#if defined(IPV6_V6ONLY)
749 int fd, val;
750 socklen_t len = sizeof(val);
751
752 v6only_default = 0;
753
754 fd = socket(AF_INET6, SOCK_STREAM, 0);
755 if (fd < 0)
756 return;
757
758 if (getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &len) == 0 && val > 0)
759 v6only_default = 1;
760
761 close(fd);
762#else
763 v6only_default = 0;
764#endif
765 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200766}
767
Willy Tarreaubca5a4e2020-08-26 09:29:21 +0200768#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY)
Olivier Houchard153659f2017-04-05 22:39:56 +0200769/* When binding the listeners, check if a socket has been sent to us by the
770 * previous process that we could reuse, instead of creating a new one.
771 */
772static int tcp_find_compatible_fd(struct listener *l)
773{
774 struct xfer_sock_list *xfer_sock = xfer_sock_list;
Willy Tarreaubca5a4e2020-08-26 09:29:21 +0200775 int options = l->options & (LI_MANDATORY_FLAGS | LI_O_V4V6);
Olivier Houchard153659f2017-04-05 22:39:56 +0200776 int ret = -1;
777
Willy Tarreaubca5a4e2020-08-26 09:29:21 +0200778 tcp_test_v6only_default();
779
780 /* Prepare to match the v6only option against what we really want. Note
781 * that sadly the two options are not exclusive to each other and that
782 * v6only is stronger than v4v6.
783 */
784 if ((options & LI_O_V6ONLY) || (v6only_default && !(options & LI_O_V4V6)))
785 options |= LI_O_V6ONLY;
786 else if ((options & LI_O_V4V6) || !v6only_default)
787 options &= ~LI_O_V6ONLY;
788 options &= ~LI_O_V4V6;
789
Olivier Houchard153659f2017-04-05 22:39:56 +0200790 while (xfer_sock) {
791 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
792 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
793 (l->interface != NULL && xfer_sock->iface != NULL &&
794 !strcmp(l->interface, xfer_sock->iface))) {
Willy Tarreaubca5a4e2020-08-26 09:29:21 +0200795 if (options == (xfer_sock->options & LI_MANDATORY_FLAGS)) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200796 if ((xfer_sock->namespace == NULL &&
797 l->netns == NULL)
Willy Tarreaue5733232019-05-22 19:24:06 +0200798#ifdef USE_NS
Olivier Houchard153659f2017-04-05 22:39:56 +0200799 || (xfer_sock->namespace != NULL &&
800 l->netns != NULL &&
801 !strcmp(xfer_sock->namespace,
802 l->netns->node.key))
803#endif
804 ) {
805 break;
806 }
807
808 }
809 }
810 }
811 xfer_sock = xfer_sock->next;
812 }
813 if (xfer_sock != NULL) {
814 ret = xfer_sock->fd;
815 if (xfer_sock == xfer_sock_list)
816 xfer_sock_list = xfer_sock->next;
817 if (xfer_sock->prev)
818 xfer_sock->prev->next = xfer_sock->next;
819 if (xfer_sock->next)
820 xfer_sock->next->prev = xfer_sock->prev;
821 free(xfer_sock->iface);
822 free(xfer_sock->namespace);
823 free(xfer_sock);
824 }
825 return ret;
826}
827#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200828
Willy Tarreaue6b98942007-10-29 01:09:36 +0100829/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100830 * an error message in <errmsg> if the message is at most <errlen> bytes long
831 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
832 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100833 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
834 * was alright and that no message was returned. ERR_RETRYABLE means that an
835 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700836 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100837 * the meaning of the error, but just indicate that a message is present which
838 * should be displayed with the respective level. Last, ERR_ABORT indicates
839 * that it's pointless to try to start other listeners. No error message is
840 * returned if errlen is NULL.
841 */
842int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
843{
844 __label__ tcp_return, tcp_close_return;
845 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100846 int ext, ready;
847 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100848 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200849#ifdef TCP_MAXSEG
850
851 /* Create a temporary TCP socket to get default parameters we can't
852 * guess.
853 * */
854 ready_len = sizeof(default_tcp_maxseg);
855 if (default_tcp_maxseg == -1) {
856 default_tcp_maxseg = -2;
857 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
858 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100859 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200860 else {
861 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
862 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100863 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
William Dauchyc0e23ae2020-02-12 10:09:14 +0100864 close(fd);
Olivier Houchard153659f2017-04-05 22:39:56 +0200865 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200866 }
867 if (default_tcp6_maxseg == -1) {
868 default_tcp6_maxseg = -2;
869 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
870 if (fd >= 0) {
871 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
872 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100873 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200874 close(fd);
875 }
876 }
877#endif
878
Willy Tarreaue6b98942007-10-29 01:09:36 +0100879
880 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100881 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100882 *errmsg = 0;
883
884 if (listener->state != LI_ASSIGNED)
885 return ERR_NONE; /* already bound */
886
887 err = ERR_NONE;
888
Olivier Houchard153659f2017-04-05 22:39:56 +0200889 if (listener->fd == -1)
890 listener->fd = tcp_find_compatible_fd(listener);
891
Willy Tarreau40aa0702013-03-10 23:51:38 +0100892 /* if the listener already has an fd assigned, then we were offered the
893 * fd by an external process (most likely the parent), and we don't want
894 * to create a new socket. However we still want to set a few flags on
895 * the socket.
896 */
897 fd = listener->fd;
898 ext = (fd >= 0);
899
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100900 if (!ext) {
901 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
902
903 if (fd == -1) {
904 err |= ERR_RETRYABLE | ERR_ALERT;
905 msg = "cannot create listening socket";
906 goto tcp_return;
907 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100908 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100909
Willy Tarreaue6b98942007-10-29 01:09:36 +0100910 if (fd >= global.maxsock) {
911 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
912 msg = "not enough free sockets (raise '-n' parameter)";
913 goto tcp_close_return;
914 }
915
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200916 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100917 err |= ERR_FATAL | ERR_ALERT;
918 msg = "cannot make socket non-blocking";
919 goto tcp_close_return;
920 }
921
Willy Tarreau40aa0702013-03-10 23:51:38 +0100922 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100923 /* not fatal but should be reported */
924 msg = "cannot do so_reuseaddr";
925 err |= ERR_ALERT;
926 }
927
928 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900929 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200930 else {
931 struct linger tmplinger;
932 socklen_t len = sizeof(tmplinger);
933 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
934 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
935 tmplinger.l_onoff = 0;
936 tmplinger.l_linger = 0;
937 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
938 sizeof(tmplinger));
939 }
940 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100941
Willy Tarreaue6b98942007-10-29 01:09:36 +0100942#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000943 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
944 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100945 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000946 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100947 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100948#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200949
Willy Tarreau40aa0702013-03-10 23:51:38 +0100950 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200951 switch (listener->addr.ss_family) {
952 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200953 if (1
954#if defined(IP_TRANSPARENT)
955 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
956#endif
957#if defined(IP_FREEBIND)
958 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
959#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200960#if defined(IP_BINDANY)
961 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
962#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200963#if defined(SO_BINDANY)
964 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
965#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200966 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200967 msg = "cannot make listening socket transparent";
968 err |= ERR_ALERT;
969 }
970 break;
971 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200972 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200973#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200974 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
975#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100976#if defined(IP_FREEBIND)
977 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
978#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200979#if defined(IPV6_BINDANY)
980 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
981#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200982#if defined(SO_BINDANY)
983 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
984#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200985 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200986 msg = "cannot make listening socket transparent";
987 err |= ERR_ALERT;
988 }
989 break;
990 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100991 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200992
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100993#ifdef SO_BINDTODEVICE
994 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100995 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100996 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100997 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100998 msg = "cannot bind listener to device";
999 err |= ERR_WARN;
1000 }
1001 }
1002#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001003#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +01001004 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001005 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +02001006 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
1007 msg = "cannot set MSS";
1008 err |= ERR_WARN;
1009 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001010 } else if (ext) {
1011 int tmpmaxseg = -1;
1012 int defaultmss;
1013 socklen_t len = sizeof(tmpmaxseg);
1014
1015 if (listener->addr.ss_family == AF_INET)
1016 defaultmss = default_tcp_maxseg;
1017 else
1018 defaultmss = default_tcp6_maxseg;
1019
1020 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +01001021 if (defaultmss > 0 &&
1022 tmpmaxseg != defaultmss &&
1023 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +02001024 msg = "cannot set MSS";
1025 err |= ERR_WARN;
1026 }
Willy Tarreaube1b9182009-06-14 18:48:19 +02001027 }
1028#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001029#if defined(TCP_USER_TIMEOUT)
1030 if (listener->tcp_ut) {
1031 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
1032 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
1033 msg = "cannot set TCP User Timeout";
1034 err |= ERR_WARN;
1035 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001036 } else
1037 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
1038 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +01001039#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001040#if defined(TCP_DEFER_ACCEPT)
1041 if (listener->options & LI_O_DEF_ACCEPT) {
1042 /* defer accept by up to one second */
1043 int accept_delay = 1;
1044 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
1045 msg = "cannot enable DEFER_ACCEPT";
1046 err |= ERR_WARN;
1047 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001048 } else
1049 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
1050 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001051#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001052#if defined(TCP_FASTOPEN)
1053 if (listener->options & LI_O_TCP_FO) {
1054 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +01001055 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +02001056 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
1057 msg = "cannot enable TCP_FASTOPEN";
1058 err |= ERR_WARN;
1059 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001060 } else {
1061 socklen_t len;
1062 int qlen;
1063 len = sizeof(qlen);
1064 /* Only disable fast open if it was enabled, we don't want
1065 * the kernel to create a fast open queue if there's none.
1066 */
1067 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
1068 qlen != 0) {
1069 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
1070 sizeof(zero)) == -1) {
1071 msg = "cannot disable TCP_FASTOPEN";
1072 err |= ERR_WARN;
1073 }
1074 }
Willy Tarreau1c862c52012-10-05 16:21:00 +02001075 }
1076#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001077#if defined(IPV6_V6ONLY)
Willy Tarreaubbb284d2020-08-26 10:21:06 +02001078 if (!ext && listener->options & LI_O_V6ONLY)
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001079 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreaubbb284d2020-08-26 10:21:06 +02001080 else if (!ext && listener->options & LI_O_V4V6)
Willy Tarreau77e3af92012-11-24 15:07:23 +01001081 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001082#endif
1083
Willy Tarreau40aa0702013-03-10 23:51:38 +01001084 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001085 err |= ERR_RETRYABLE | ERR_ALERT;
1086 msg = "cannot bind socket";
1087 goto tcp_close_return;
1088 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001089
Willy Tarreau40aa0702013-03-10 23:51:38 +01001090 ready = 0;
1091 ready_len = sizeof(ready);
1092 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
1093 ready = 0;
1094
1095 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +01001096 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001097 err |= ERR_RETRYABLE | ERR_ALERT;
1098 msg = "cannot listen to socket";
1099 goto tcp_close_return;
1100 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001101
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001102#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001103 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +09001104 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +02001105 else
1106 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001107#endif
1108
Willy Tarreaue6b98942007-10-29 01:09:36 +01001109 /* the socket is ready */
1110 listener->fd = fd;
1111 listener->state = LI_LISTEN;
1112
Willy Tarreaua9786b62018-01-25 07:22:13 +01001113 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +01001114 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaueb472682010-05-28 18:46:57 +02001115
Willy Tarreaubb1caff2020-08-19 10:00:57 +02001116 /* for now, all regularly bound TCP listeners are exportable */
1117 if (!(listener->options & LI_O_INHERITED))
1118 fdtab[fd].exported = 1;
1119
Willy Tarreaue6b98942007-10-29 01:09:36 +01001120 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001121 if (msg && errlen) {
1122 char pn[INET6_ADDRSTRLEN];
1123
Willy Tarreau631f01c2011-09-05 00:36:48 +02001124 addr_to_str(&listener->addr, pn, sizeof(pn));
1125 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001126 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001127 return err;
1128
1129 tcp_close_return:
1130 close(fd);
1131 goto tcp_return;
1132}
1133
1134/* This function creates all TCP sockets bound to the protocol entry <proto>.
1135 * It is intended to be used as the protocol's bind_all() function.
1136 * The sockets will be registered but not added to any fd_set, in order not to
1137 * loose them across the fork(). A call to enable_all_listeners() is needed
1138 * to complete initialization. The return value is composed from ERR_*.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001139 *
1140 * Must be called with proto_lock held.
1141 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001142 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001143static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001144{
1145 struct listener *listener;
1146 int err = ERR_NONE;
1147
1148 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001149 err |= tcp_bind_listener(listener, errmsg, errlen);
1150 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001151 break;
1152 }
1153
1154 return err;
1155}
1156
Willy Tarreau32282382017-09-15 07:44:44 +02001157/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1158 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1159 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001160 *
1161 * Must be called with proto_lock held.
1162 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001163 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001164static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001165{
1166 if (listener->state != LI_INIT)
1167 return;
1168 listener->state = LI_ASSIGNED;
1169 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001170 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001171 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1172 proto_tcpv4.nb_listeners++;
1173}
1174
Willy Tarreau32282382017-09-15 07:44:44 +02001175/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1176 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1177 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001178 *
1179 * Must be called with proto_lock held.
1180 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001181 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001182static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001183{
1184 if (listener->state != LI_INIT)
1185 return;
1186 listener->state = LI_ASSIGNED;
1187 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001188 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001189 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1190 proto_tcpv6.nb_listeners++;
1191}
1192
Willy Tarreau092d8652014-07-07 20:22:12 +02001193/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1194 * was totally stopped, or > 0 if correctly paused.
1195 */
1196int tcp_pause_listener(struct listener *l)
1197{
1198 if (shutdown(l->fd, SHUT_WR) != 0)
1199 return -1; /* Solaris dies here */
1200
Willy Tarreaue2711c72019-02-27 15:39:41 +01001201 if (listen(l->fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +02001202 return -1; /* OpenBSD dies here */
1203
1204 if (shutdown(l->fd, SHUT_RD) != 0)
1205 return -1; /* should always be OK */
1206 return 1;
1207}
1208
Willy Tarreau645513a2010-05-24 20:55:15 +02001209/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001210/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001211/************************************************************************/
1212
Willy Tarreau4a129812012-04-25 17:31:42 +02001213/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001214int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001215{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001216 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001217
1218 if (!cli_conn)
1219 return 0;
1220
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001221 if (!conn_get_src(cli_conn))
1222 return 0;
1223
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001224 switch (cli_conn->src->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001225 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001226 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->src)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001227 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001228 break;
1229 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001230 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->src)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001231 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001232 break;
1233 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001234 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001235 }
Emeric Brunf769f512010-10-22 17:14:01 +02001236
Willy Tarreau37406352012-04-23 16:16:37 +02001237 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001238 return 1;
1239}
1240
Willy Tarreaua5e37562011-12-16 17:06:15 +01001241/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001242static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001243smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001244{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001245 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001246
1247 if (!cli_conn)
1248 return 0;
1249
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001250 if (!conn_get_src(cli_conn))
1251 return 0;
1252
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001253 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001254 if (!(smp->data.u.sint = get_host_port(cli_conn->src)))
Emeric Brunf769f512010-10-22 17:14:01 +02001255 return 0;
1256
Willy Tarreau37406352012-04-23 16:16:37 +02001257 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001258 return 1;
1259}
1260
Willy Tarreau4a129812012-04-25 17:31:42 +02001261/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001262static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001263smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001264{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001265 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001266
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001267 if (!cli_conn)
1268 return 0;
1269
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001270 if (!conn_get_dst(cli_conn))
1271 return 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001272
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001273 switch (cli_conn->dst->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001274 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001275 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001276 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001277 break;
1278 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001279 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001280 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001281 break;
1282 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001283 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001284 }
Emeric Brunf769f512010-10-22 17:14:01 +02001285
Willy Tarreau37406352012-04-23 16:16:37 +02001286 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001287 return 1;
1288}
1289
Willy Tarreau16e01562016-08-09 16:46:18 +02001290/* check if the destination address of the front connection is local to the
1291 * system or if it was intercepted.
1292 */
1293int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1294{
1295 struct connection *conn = objt_conn(smp->sess->origin);
1296 struct listener *li = smp->sess->listener;
1297
1298 if (!conn)
1299 return 0;
1300
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001301 if (!conn_get_dst(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001302 return 0;
1303
1304 smp->data.type = SMP_T_BOOL;
1305 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001306 smp->data.u.sint = addr_is_local(li->netns, conn->dst);
Willy Tarreau16e01562016-08-09 16:46:18 +02001307 return smp->data.u.sint >= 0;
1308}
1309
1310/* check if the source address of the front connection is local to the system
1311 * or not.
1312 */
1313int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1314{
1315 struct connection *conn = objt_conn(smp->sess->origin);
1316 struct listener *li = smp->sess->listener;
1317
1318 if (!conn)
1319 return 0;
1320
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001321 if (!conn_get_src(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001322 return 0;
1323
1324 smp->data.type = SMP_T_BOOL;
1325 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001326 smp->data.u.sint = addr_is_local(li->netns, conn->src);
Willy Tarreau16e01562016-08-09 16:46:18 +02001327 return smp->data.u.sint >= 0;
1328}
1329
Willy Tarreaua5e37562011-12-16 17:06:15 +01001330/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001331static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001332smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001333{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001334 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001335
1336 if (!cli_conn)
1337 return 0;
1338
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001339 if (!conn_get_dst(cli_conn))
1340 return 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001341
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001342 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001343 if (!(smp->data.u.sint = get_host_port(cli_conn->dst)))
Emeric Brunf769f512010-10-22 17:14:01 +02001344 return 0;
1345
Willy Tarreau37406352012-04-23 16:16:37 +02001346 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001347 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001348}
1349
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001350#ifdef TCP_INFO
1351
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001352
1353/* Validates the arguments passed to "fc_*" fetch keywords returning a time
1354 * value. These keywords support an optional string representing the unit of the
1355 * result: "us" for microseconds and "ms" for milliseconds". Returns 0 on error
1356 * and non-zero if OK.
1357 */
1358static int val_fc_time_value(struct arg *args, char **err)
1359{
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001360 if (args[0].type == ARGT_STR) {
1361 if (strcmp(args[0].data.str.area, "us") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001362 chunk_destroy(&args[0].data.str);
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001363 args[0].type = ARGT_SINT;
1364 args[0].data.sint = TIME_UNIT_US;
1365 }
1366 else if (strcmp(args[0].data.str.area, "ms") == 0) {
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001367 chunk_destroy(&args[0].data.str);
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001368 args[0].type = ARGT_SINT;
1369 args[0].data.sint = TIME_UNIT_MS;
1370 }
1371 else {
1372 memprintf(err, "expects 'us' or 'ms', got '%s'",
1373 args[0].data.str.area);
1374 return 0;
1375 }
1376 }
1377 else {
1378 memprintf(err, "Unexpected arg type");
1379 return 0;
1380 }
1381
1382 return 1;
1383}
1384
1385/* Validates the arguments passed to "fc_*" fetch keywords returning a
1386 * counter. These keywords should be used without any keyword, but because of a
1387 * bug in previous versions, an optional string argument may be passed. In such
1388 * case, the argument is ignored and a warning is emitted. Returns 0 on error
1389 * and non-zero if OK.
1390 */
1391static int var_fc_counter(struct arg *args, char **err)
1392{
1393 if (args[0].type != ARGT_STOP) {
1394 ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n");
1395 if (args[0].type == ARGT_STR)
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001396 chunk_destroy(&args[0].data.str);
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001397 args[0].type = ARGT_STOP;
1398 }
1399
1400 return 1;
1401}
1402
Joseph Herlanta6331472018-11-25 12:59:12 -08001403/* Returns some tcp_info data if it's available. "dir" must be set to 0 if
1404 * the client connection is required, otherwise it is set to 1. "val" represents
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001405 * the required value.
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001406 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1407 */
1408static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1409 int dir, int val)
1410{
1411 struct connection *conn;
1412 struct tcp_info info;
1413 socklen_t optlen;
1414
1415 /* strm can be null. */
1416 if (!smp->strm)
1417 return 0;
1418
1419 /* get the object associated with the stream interface.The
1420 * object can be other thing than a connection. For example,
1421 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001422 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001423 if (!conn)
1424 return 0;
1425
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001426 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001427 syscal can fail. */
1428 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001429 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001430 return 0;
1431
1432 /* extract the value. */
1433 smp->data.type = SMP_T_SINT;
1434 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001435 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1436 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1437#if defined(__linux__)
1438 /* these ones are common to all Linux versions */
1439 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1440 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1441 case 4: smp->data.u.sint = info.tcpi_lost; break;
1442 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1443 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1444 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1445#elif defined(__FreeBSD__) || defined(__NetBSD__)
1446 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1447 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1448 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1449 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1450 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1451 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1452 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1453#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001454 default: return 0;
1455 }
1456
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001457 return 1;
1458}
1459
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001460/* get the mean rtt of a client connection */
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001461static int
1462smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1463{
1464 if (!get_tcp_info(args, smp, 0, 0))
1465 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001466
1467 /* By default or if explicitly specified, convert rtt to ms */
1468 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1469 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1470
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001471 return 1;
1472}
1473
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001474/* get the variance of the mean rtt of a client connection */
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001475static int
1476smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1477{
1478 if (!get_tcp_info(args, smp, 0, 1))
1479 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001480
1481 /* By default or if explicitly specified, convert rttvar to ms */
1482 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1483 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1484
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001485 return 1;
1486}
Joe Williams30fcd392016-08-10 07:06:44 -07001487
1488#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1489
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001490/* get the unacked counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001491static int
1492smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1493{
1494 if (!get_tcp_info(args, smp, 0, 2))
1495 return 0;
1496 return 1;
1497}
1498
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001499/* get the sacked counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001500static int
1501smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1502{
1503 if (!get_tcp_info(args, smp, 0, 3))
1504 return 0;
1505 return 1;
1506}
1507
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001508/* get the lost counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001509static int
1510smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1511{
1512 if (!get_tcp_info(args, smp, 0, 4))
1513 return 0;
1514 return 1;
1515}
1516
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001517/* get the retrans counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001518static int
1519smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1520{
1521 if (!get_tcp_info(args, smp, 0, 5))
1522 return 0;
1523 return 1;
1524}
1525
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001526/* get the fackets counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001527static int
1528smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1529{
1530 if (!get_tcp_info(args, smp, 0, 6))
1531 return 0;
1532 return 1;
1533}
1534
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001535/* get the reordering counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001536static int
1537smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1538{
1539 if (!get_tcp_info(args, smp, 0, 7))
1540 return 0;
1541 return 1;
1542}
1543#endif // linux || freebsd || netbsd
1544#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001545
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001546#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001547/* parse the "v4v6" bind keyword */
1548static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1549{
1550 struct listener *l;
1551
1552 list_for_each_entry(l, &conf->listeners, by_bind) {
1553 if (l->addr.ss_family == AF_INET6)
1554 l->options |= LI_O_V4V6;
1555 }
1556
1557 return 0;
1558}
1559
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001560/* parse the "v6only" bind keyword */
1561static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1562{
1563 struct listener *l;
1564
1565 list_for_each_entry(l, &conf->listeners, by_bind) {
1566 if (l->addr.ss_family == AF_INET6)
1567 l->options |= LI_O_V6ONLY;
1568 }
1569
1570 return 0;
1571}
1572#endif
1573
Pieter Baauwd551fb52013-05-08 22:49:23 +02001574#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001575/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001576static int bind_parse_transparent(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001577{
1578 struct listener *l;
1579
Willy Tarreau4348fad2012-09-20 16:48:07 +02001580 list_for_each_entry(l, &conf->listeners, by_bind) {
1581 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1582 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001583 }
1584
Willy Tarreau44791242012-09-12 23:27:21 +02001585 return 0;
1586}
1587#endif
1588
1589#ifdef TCP_DEFER_ACCEPT
1590/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001591static int bind_parse_defer_accept(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001592{
1593 struct listener *l;
1594
Willy Tarreau4348fad2012-09-20 16:48:07 +02001595 list_for_each_entry(l, &conf->listeners, by_bind) {
1596 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1597 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001598 }
1599
Willy Tarreau44791242012-09-12 23:27:21 +02001600 return 0;
1601}
1602#endif
1603
Willy Tarreau1c862c52012-10-05 16:21:00 +02001604#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001605/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001606static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1607{
1608 struct listener *l;
1609
1610 list_for_each_entry(l, &conf->listeners, by_bind) {
1611 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1612 l->options |= LI_O_TCP_FO;
1613 }
1614
1615 return 0;
1616}
1617#endif
1618
Willy Tarreau44791242012-09-12 23:27:21 +02001619#ifdef TCP_MAXSEG
1620/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001621static int bind_parse_mss(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001622{
1623 struct listener *l;
1624 int mss;
1625
Willy Tarreau44791242012-09-12 23:27:21 +02001626 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001627 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001628 return ERR_ALERT | ERR_FATAL;
1629 }
1630
1631 mss = atoi(args[cur_arg + 1]);
1632 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001633 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001634 return ERR_ALERT | ERR_FATAL;
1635 }
1636
Willy Tarreau4348fad2012-09-20 16:48:07 +02001637 list_for_each_entry(l, &conf->listeners, by_bind) {
1638 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1639 l->maxseg = mss;
1640 }
Willy Tarreau44791242012-09-12 23:27:21 +02001641
1642 return 0;
1643}
1644#endif
1645
Willy Tarreau2af207a2015-02-04 00:45:58 +01001646#ifdef TCP_USER_TIMEOUT
1647/* parse the "tcp-ut" bind keyword */
1648static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1649{
1650 const char *ptr = NULL;
1651 struct listener *l;
1652 unsigned int timeout;
1653
1654 if (!*args[cur_arg + 1]) {
1655 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1656 return ERR_ALERT | ERR_FATAL;
1657 }
1658
1659 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001660 if (ptr == PARSE_TIME_OVER) {
1661 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1662 args[cur_arg+1], args[cur_arg]);
1663 return ERR_ALERT | ERR_FATAL;
1664 }
1665 else if (ptr == PARSE_TIME_UNDER) {
1666 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1667 args[cur_arg+1], args[cur_arg]);
1668 return ERR_ALERT | ERR_FATAL;
1669 }
1670 else if (ptr) {
Willy Tarreau2af207a2015-02-04 00:45:58 +01001671 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1672 return ERR_ALERT | ERR_FATAL;
1673 }
1674
1675 list_for_each_entry(l, &conf->listeners, by_bind) {
1676 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1677 l->tcp_ut = timeout;
1678 }
1679
1680 return 0;
1681}
1682#endif
1683
Willy Tarreau44791242012-09-12 23:27:21 +02001684#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001685/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001686static int bind_parse_interface(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001687{
1688 struct listener *l;
1689
Willy Tarreau44791242012-09-12 23:27:21 +02001690 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001691 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001692 return ERR_ALERT | ERR_FATAL;
1693 }
1694
Willy Tarreau4348fad2012-09-20 16:48:07 +02001695 list_for_each_entry(l, &conf->listeners, by_bind) {
1696 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1697 l->interface = strdup(args[cur_arg + 1]);
1698 }
Willy Tarreau44791242012-09-12 23:27:21 +02001699
Willy Tarreau44791242012-09-12 23:27:21 +02001700 return 0;
1701}
1702#endif
1703
Willy Tarreaue5733232019-05-22 19:24:06 +02001704#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001705/* parse the "namespace" bind keyword */
1706static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1707{
1708 struct listener *l;
1709 char *namespace = NULL;
1710
1711 if (!*args[cur_arg + 1]) {
1712 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1713 return ERR_ALERT | ERR_FATAL;
1714 }
1715 namespace = args[cur_arg + 1];
1716
1717 list_for_each_entry(l, &conf->listeners, by_bind) {
1718 l->netns = netns_store_lookup(namespace, strlen(namespace));
1719
1720 if (l->netns == NULL)
1721 l->netns = netns_store_insert(namespace);
1722
1723 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001724 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001725 return ERR_ALERT | ERR_FATAL;
1726 }
1727 }
1728 return 0;
1729}
1730#endif
1731
Willy Tarreau163d4622015-10-13 16:16:41 +02001732#ifdef TCP_USER_TIMEOUT
1733/* parse the "tcp-ut" server keyword */
1734static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1735{
1736 const char *ptr = NULL;
1737 unsigned int timeout;
1738
1739 if (!*args[*cur_arg + 1]) {
1740 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1741 return ERR_ALERT | ERR_FATAL;
1742 }
1743
1744 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001745 if (ptr == PARSE_TIME_OVER) {
1746 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1747 args[*cur_arg+1], args[*cur_arg]);
1748 return ERR_ALERT | ERR_FATAL;
1749 }
1750 else if (ptr == PARSE_TIME_UNDER) {
1751 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1752 args[*cur_arg+1], args[*cur_arg]);
1753 return ERR_ALERT | ERR_FATAL;
1754 }
1755 else if (ptr) {
Willy Tarreau163d4622015-10-13 16:16:41 +02001756 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1757 return ERR_ALERT | ERR_FATAL;
1758 }
1759
1760 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1761 newsrv->tcp_ut = timeout;
1762
1763 return 0;
1764}
1765#endif
1766
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001767
Willy Tarreau4a129812012-04-25 17:31:42 +02001768/* Note: must not be declared <const> as its list will be overwritten.
1769 * Note: fetches that may return multiple types must be declared as the lowest
1770 * common denominator, the type that can be casted into all other ones. For
1771 * instance v4/v6 must be declared v4.
1772 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001773static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001774 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001775 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001776 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001777 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001778 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001779 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001780#ifdef TCP_INFO
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001781 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
1782 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
Joe Williams30fcd392016-08-10 07:06:44 -07001783#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001784 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1785 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1786 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1787 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1788 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1789 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
Joe Williams30fcd392016-08-10 07:06:44 -07001790#endif // linux || freebsd || netbsd
1791#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001792 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001793}};
1794
Willy Tarreau0108d902018-11-25 19:14:37 +01001795INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1796
Willy Tarreau44791242012-09-12 23:27:21 +02001797/************************************************************************/
1798/* All supported bind keywords must be declared here. */
1799/************************************************************************/
1800
1801/* Note: must not be declared <const> as its list will be overwritten.
1802 * Please take care of keeping this list alphabetically sorted, doing so helps
1803 * all code contributors.
1804 * Optional keywords are also declared with a NULL ->parse() function so that
1805 * the config parser can report an appropriate error when a known keyword was
1806 * not enabled.
1807 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001808static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001809#ifdef TCP_DEFER_ACCEPT
1810 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1811#endif
1812#ifdef SO_BINDTODEVICE
1813 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1814#endif
1815#ifdef TCP_MAXSEG
1816 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1817#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001818#ifdef TCP_USER_TIMEOUT
1819 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1820#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001821#ifdef TCP_FASTOPEN
1822 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1823#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001824#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001825 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1826#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001827#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001828 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001829 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1830#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001831#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001832 { "namespace", bind_parse_namespace, 1 },
1833#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001834 /* the versions with the NULL parse function*/
1835 { "defer-accept", NULL, 0 },
1836 { "interface", NULL, 1 },
1837 { "mss", NULL, 1 },
1838 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001839 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001840 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001841 { NULL, NULL, 0 },
1842}};
1843
Willy Tarreau0108d902018-11-25 19:14:37 +01001844INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1845
Willy Tarreau163d4622015-10-13 16:16:41 +02001846static struct srv_kw_list srv_kws = { "TCP", { }, {
1847#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001848 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02001849#endif
1850 { NULL, NULL, 0 },
1851}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02001852
Willy Tarreau0108d902018-11-25 19:14:37 +01001853INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
1854
Willy Tarreau2d392c22015-08-24 01:43:45 +02001855
Willy Tarreau80713382018-11-26 10:19:54 +01001856REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
Willy Tarreauba962912016-12-21 18:55:02 +01001857#if defined(IP_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01001858 " IP_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01001859#endif
1860#if defined(IPV6_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01001861 " IPV6_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01001862#endif
1863#if defined(IP_FREEBIND)
Willy Tarreau80713382018-11-26 10:19:54 +01001864 " IP_FREEBIND"
Willy Tarreauba962912016-12-21 18:55:02 +01001865#endif
1866#if defined(IP_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01001867 " IP_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01001868#endif
1869#if defined(IPV6_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01001870 " IPV6_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01001871#endif
1872#if defined(SO_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01001873 " SO_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01001874#endif
Willy Tarreau80713382018-11-26 10:19:54 +01001875 "");
Willy Tarreaue6b98942007-10-29 01:09:36 +01001876
1877
1878/*
1879 * Local variables:
1880 * c-indent-level: 8
1881 * c-basic-offset: 8
1882 * End:
1883 */