blob: afcb1bc0af2a02dfd679e193ea2f578f28a9dd31 [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 Tarreaue6b98942007-10-29 01:09:36 +010033#include <common/compat.h>
34#include <common/config.h>
35#include <common/debug.h>
36#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010037#include <common/initcall.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010038#include <common/mini-clist.h>
39#include <common/standard.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010040#include <common/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010041
Willy Tarreau39713102016-11-25 15:49:32 +010042#include <types/action.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020043#include <types/connection.h>
Willy Tarreau39713102016-11-25 15:49:32 +010044#include <types/global.h>
45#include <types/stream.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010046
Willy Tarreau9fcb9842012-04-20 14:45:49 +020047#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020048#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020049#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020050#include <proto/fd.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020051#include <proto/http_rules.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020052#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020053#include <proto/log.h>
54#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020055#include <proto/protocol.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056#include <proto/http_ana.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010057#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020059#include <proto/sample.h>
Willy Tarreau163d4622015-10-13 16:16:41 +020060#include <proto/server.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020061#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010062#include <proto/tcp_rules.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010063
Emeric Bruncf20bf12010-10-22 16:06:11 +020064static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
65static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020066static void tcpv4_add_listener(struct listener *listener, int port);
67static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010068
69/* Note: must not be declared <const> as its list will be overwritten */
70static struct protocol proto_tcpv4 = {
71 .name = "tcpv4",
72 .sock_domain = AF_INET,
73 .sock_type = SOCK_STREAM,
74 .sock_prot = IPPROTO_TCP,
75 .sock_family = AF_INET,
76 .sock_addrlen = sizeof(struct sockaddr_in),
77 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020078 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020079 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020080 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010081 .bind_all = tcp_bind_listeners,
82 .unbind_all = unbind_all_listeners,
83 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020084 .get_src = tcp_get_src,
85 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020086 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020087 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010088 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
89 .nb_listeners = 0,
90};
91
Willy Tarreau0108d902018-11-25 19:14:37 +010092INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
93
Willy Tarreaue6b98942007-10-29 01:09:36 +010094/* Note: must not be declared <const> as its list will be overwritten */
95static struct protocol proto_tcpv6 = {
96 .name = "tcpv6",
97 .sock_domain = AF_INET6,
98 .sock_type = SOCK_STREAM,
99 .sock_prot = IPPROTO_TCP,
100 .sock_family = AF_INET6,
101 .sock_addrlen = sizeof(struct sockaddr_in6),
102 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200103 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200104 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200105 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100106 .bind_all = tcp_bind_listeners,
107 .unbind_all = unbind_all_listeners,
108 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200109 .get_src = tcp_get_src,
110 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +0200111 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200112 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100113 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
114 .nb_listeners = 0,
115};
116
Willy Tarreau0108d902018-11-25 19:14:37 +0100117INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
118
Olivier Houchard153659f2017-04-05 22:39:56 +0200119/* Default TCP parameters, got by opening a temporary TCP socket. */
120#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100121static THREAD_LOCAL int default_tcp_maxseg = -1;
122static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200123#endif
124
David du Colombier6f5ccb12011-03-10 22:26:24 +0100125/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100126 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
127 * - 0 : ignore remote address (may even be a NULL pointer)
128 * - 1 : use provided address
129 * - 2 : use provided port
130 * - 3 : use both
131 *
132 * The function supports multiple foreign binding methods :
133 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100134 * The second one can be used as a fallback for the first one.
135 * This function returns 0 when everything's OK, 1 if it could not bind, to the
136 * local address, 2 if it could not bind to the foreign address.
137 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100138int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100139{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100140 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100141 int foreign_ok = 0;
142 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100143 static THREAD_LOCAL int ip_transp_working = 1;
144 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200145
David du Colombier65c17962012-07-13 14:34:59 +0200146 switch (local->ss_family) {
147 case AF_INET:
148 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200149 /* This deserves some explanation. Some platforms will support
150 * multiple combinations of certain methods, so we try the
151 * supported ones until one succeeds.
152 */
153 if (0
154#if defined(IP_TRANSPARENT)
155 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
156#endif
157#if defined(IP_FREEBIND)
158 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
159#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200160#if defined(IP_BINDANY)
161 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
162#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200163#if defined(SO_BINDANY)
164 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
165#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200166 )
David du Colombier65c17962012-07-13 14:34:59 +0200167 foreign_ok = 1;
168 else
169 ip_transp_working = 0;
170 }
171 break;
172 case AF_INET6:
173 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200174 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200175#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200176 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
177#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100178#if defined(IP_FREEBIND)
179 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
180#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200181#if defined(IPV6_BINDANY)
182 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
183#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200184#if defined(SO_BINDANY)
185 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
186#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200187 )
David du Colombier65c17962012-07-13 14:34:59 +0200188 foreign_ok = 1;
189 else
190 ip6_transp_working = 0;
191 }
192 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100193 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200194
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100195 if (flags) {
196 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200197 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100198 switch (remote->ss_family) {
199 case AF_INET:
200 if (flags & 1)
201 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
202 if (flags & 2)
203 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
204 break;
205 case AF_INET6:
206 if (flags & 1)
207 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
208 if (flags & 2)
209 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
210 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100211 default:
212 /* we don't want to try to bind to an unknown address family */
213 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100214 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100215 }
216
Simon Hormande072bd2011-06-24 15:11:37 +0900217 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100218 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200219 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200220 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
221 if (ret < 0)
222 return 2;
223 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100224 }
225 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200226 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200227 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
228 if (ret < 0)
229 return 1;
230 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100231 }
232
233 if (!flags)
234 return 0;
235
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100236 if (!foreign_ok)
237 /* we could not bind to a foreign address */
238 return 2;
239
240 return 0;
241}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100242
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200243/* conn->dst MUST be valid */
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100244static int create_server_socket(struct connection *conn)
245{
Willy Tarreau529c1392014-12-24 13:47:55 +0100246 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100247
Willy Tarreaue5733232019-05-22 19:24:06 +0200248#ifdef USE_NS
Willy Tarreau529c1392014-12-24 13:47:55 +0100249 if (objt_server(conn->target)) {
250 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
251 ns = conn->proxy_netns;
252 else
253 ns = __objt_server(conn->target)->netns;
254 }
255#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200256 return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, IPPROTO_TCP);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100257}
Willy Tarreau9650f372009-08-16 14:02:45 +0200258
259/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200260 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200261 * to connection <conn> using (si->{target,dst}). A source address may be
262 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200263 * bind addresses are still determined locally (due to the possible need of a
264 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100265 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100266 * supported. The <data> parameter is a boolean indicating whether there are data
267 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200268 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
269 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100270 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200271 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
272 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200273 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200274 * Note that a pending send_proxy message accounts for data.
275 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200276 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200277 * - SF_ERR_NONE if everything's OK
278 * - SF_ERR_SRVTO if there are no more servers
279 * - SF_ERR_SRVCL if the connection was refused by the server
280 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
281 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
282 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100283 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100284 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200285 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100286 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200287 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100288
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200289int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200290{
291 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100292 struct server *srv;
293 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100294 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100295 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800296 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100297
Olivier Houchard637b6952018-11-23 14:23:07 +0100298 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100299
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100300 switch (obj_type(conn->target)) {
301 case OBJ_TYPE_PROXY:
302 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100303 srv = NULL;
304 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100305 case OBJ_TYPE_SERVER:
306 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100307 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100308 /* Make sure we check that we have data before activating
309 * TFO, or we could trigger a kernel issue whereby after
310 * a successful connect() == 0, any subsequent connect()
311 * will return EINPROGRESS instead of EISCONN.
312 */
313 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
314 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
315 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100316 break;
317 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100318 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200319 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100320 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200321
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200322 if (!conn->dst) {
323 conn->flags |= CO_FL_ERROR;
324 return SF_ERR_INTERNAL;
325 }
326
Willy Tarreau585744b2017-08-24 14:31:19 +0200327 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100328
329 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200330 qfprintf(stderr, "Cannot get a server socket.\n");
331
Willy Tarreau9ce70132014-01-24 16:08:19 +0100332 if (errno == ENFILE) {
333 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200334 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100335 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
336 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100337 }
338 else if (errno == EMFILE) {
339 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200340 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100341 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
342 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100343 }
344 else if (errno == ENOBUFS || errno == ENOMEM) {
345 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200346 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100347 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
348 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100349 }
350 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
351 conn->err_code = CO_ER_NOPROTO;
352 }
353 else
354 conn->err_code = CO_ER_SOCK_ERR;
355
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100357 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200358 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200359 }
360
361 if (fd >= global.maxsock) {
362 /* do not log anything there, it's a normal condition when this option
363 * is used to serialize connections to a server !
364 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100365 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200366 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100367 conn->err_code = CO_ER_CONF_FDLIM;
368 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200369 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200370 }
371
372 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900373 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200374 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
375 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100376 conn->err_code = CO_ER_SOCK_ERR;
377 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200378 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200379 }
380
William Lallemandc03eb012018-11-27 12:02:37 +0100381 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
382 ha_alert("Cannot set CLOEXEC on client socket.\n");
383 close(fd);
384 conn->err_code = CO_ER_SOCK_ERR;
385 conn->flags |= CO_FL_ERROR;
386 return SF_ERR_INTERNAL;
387 }
388
Willy Tarreau9650f372009-08-16 14:02:45 +0200389 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900390 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200391
Willy Tarreau9650f372009-08-16 14:02:45 +0200392 /* allow specific binding :
393 * - server-specific at first
394 * - proxy-specific next
395 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100396 if (srv && srv->conn_src.opts & CO_SRC_BIND)
397 src = &srv->conn_src;
398 else if (be->conn_src.opts & CO_SRC_BIND)
399 src = &be->conn_src;
400 else
401 src = NULL;
402
403 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200404 int ret, flags = 0;
405
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200406 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100407 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100408 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200409 conn->flags |= CO_FL_PRIVATE;
410 /* fall through */
411 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200412 flags = 3;
413 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100414 case CO_SRC_TPROXY_CIP:
415 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200416 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200417 flags = 1;
418 break;
419 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200420 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200421
Willy Tarreau9650f372009-08-16 14:02:45 +0200422#ifdef SO_BINDTODEVICE
423 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100424 if (src->iface_name)
425 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200426#endif
427
Willy Tarreaua4380b42012-12-08 22:49:11 +0100428 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200429 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100430 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200431
432 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200433 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200434
435 do {
436 /* note: in case of retry, we may have to release a previously
437 * allocated port, hence this loop's construct.
438 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200439 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
440 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200441
442 if (!attempts)
443 break;
444 attempts--;
445
Willy Tarreaua4380b42012-12-08 22:49:11 +0100446 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100447 if (!fdinfo[fd].local_port) {
448 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200449 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100450 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200451
Willy Tarreaua4380b42012-12-08 22:49:11 +0100452 fdinfo[fd].port_range = src->sport_range;
453 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200454
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200455 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100456 if (ret != 0)
457 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 } while (ret != 0); /* binding NOK */
459 }
460 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000461#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100462 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000463 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
464#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200465 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100466 if (ret != 0)
467 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200468 }
469
Willy Tarreaua4380b42012-12-08 22:49:11 +0100470 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200471 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
472 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200473 close(fd);
474
Willy Tarreau9650f372009-08-16 14:02:45 +0200475 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100476 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
477 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200478 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100479 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200480 be->id);
481 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100482 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
483 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200484 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100485 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200486 be->id);
487 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100488 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200489 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200490 }
491 }
492
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400493#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200494 /* disabling tcp quick ack now allows the first request to leave the
495 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100496 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200497 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200498 if (flags & (CONNECT_DELACK_ALWAYS) ||
499 ((flags & CONNECT_DELACK_SMART_CONNECT ||
500 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
501 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900502 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200503#endif
504
Willy Tarreau163d4622015-10-13 16:16:41 +0200505#ifdef TCP_USER_TIMEOUT
506 /* there is not much more we can do here when it fails, it's still minor */
507 if (srv && srv->tcp_ut)
508 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
509#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100510
511 if (use_fastopen) {
512#if defined(TCP_FASTOPEN_CONNECT)
513 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
514#endif
515 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100516 if (global.tune.server_sndbuf)
517 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
518
519 if (global.tune.server_rcvbuf)
520 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
521
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200522 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800523 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100524 if (errno == EINPROGRESS || errno == EALREADY) {
525 /* common case, let's wait for connect status */
526 conn->flags |= CO_FL_WAIT_L4_CONN;
527 }
528 else if (errno == EISCONN) {
529 /* should normally not happen but if so, indicates that it's OK */
530 conn->flags &= ~CO_FL_WAIT_L4_CONN;
531 }
532 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200533 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100534 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200535 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100536 conn->err_code = CO_ER_FREE_PORTS;
537 }
538 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200539 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100540 conn->err_code = CO_ER_ADDR_INUSE;
541 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200542
Willy Tarreaub1719512012-12-08 23:03:28 +0100543 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200544 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
545 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200546 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100547 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100548 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200549 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200550 } else if (errno == ETIMEDOUT) {
551 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200552 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
553 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200554 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100555 conn->err_code = CO_ER_SOCK_ERR;
556 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200557 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200558 } else {
559 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
560 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200561 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
562 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200563 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100564 conn->err_code = CO_ER_SOCK_ERR;
565 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200566 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200567 }
568 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100569 else {
570 /* connect() == 0, this is great! */
571 conn->flags &= ~CO_FL_WAIT_L4_CONN;
572 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200573
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100574 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200575
Willy Tarreauf79c8172013-10-21 16:30:56 +0200576 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100577 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200578
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100579 if (conn->flags & CO_FL_WAIT_L4_CONN) {
580 fd_want_send(fd);
581 fd_cant_send(fd);
582 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200583
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200584 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200585 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100586 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200587 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200588 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200589
Willy Tarreaue7dff022015-04-03 01:14:29 +0200590 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200591}
592
593
Willy Tarreau59b94792012-05-11 16:16:40 +0200594/*
595 * Retrieves the source address for the socket <fd>, with <dir> indicating
596 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
597 * success, -1 in case of error. The socket's source address is stored in
598 * <sa> for <salen> bytes.
599 */
600int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
601{
602 if (dir)
603 return getsockname(fd, sa, &salen);
604 else
605 return getpeername(fd, sa, &salen);
606}
607
608
609/*
610 * Retrieves the original destination address for the socket <fd>, with <dir>
611 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
612 * listener, if the original destination address was translated, the original
613 * address is retrieved. It returns 0 in case of success, -1 in case of error.
614 * The socket's source address is stored in <sa> for <salen> bytes.
615 */
616int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
617{
618 if (dir)
619 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100620 else {
621 int ret = getsockname(fd, sa, &salen);
622
623 if (ret < 0)
624 return ret;
625
Willy Tarreaue5733232019-05-22 19:24:06 +0200626#if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100627 /* For TPROXY and Netfilter's NAT, we can retrieve the original
628 * IPv4 address before DNAT/REDIRECT. We must not do that with
629 * other families because v6-mapped IPv4 addresses are still
630 * reported as v4.
631 */
632 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
633 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
634 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200635#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100636 return ret;
637 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200638}
639
Olivier Houchard153659f2017-04-05 22:39:56 +0200640/* XXX: Should probably be elsewhere */
641static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
642{
643 if (a->ss_family != b->ss_family) {
644 return (-1);
645 }
646 switch (a->ss_family) {
647 case AF_INET:
648 {
649 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
650 if (a4->sin_port != b4->sin_port)
651 return (-1);
652 return (memcmp(&a4->sin_addr, &b4->sin_addr,
653 sizeof(a4->sin_addr)));
654 }
655 case AF_INET6:
656 {
657 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
658 if (a6->sin6_port != b6->sin6_port)
659 return (-1);
660 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
661 sizeof(a6->sin6_addr)));
662 }
663 default:
664 return (-1);
665 }
666
667}
668
669#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
670/* When binding the listeners, check if a socket has been sent to us by the
671 * previous process that we could reuse, instead of creating a new one.
672 */
673static int tcp_find_compatible_fd(struct listener *l)
674{
675 struct xfer_sock_list *xfer_sock = xfer_sock_list;
676 int ret = -1;
677
678 while (xfer_sock) {
679 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
680 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
681 (l->interface != NULL && xfer_sock->iface != NULL &&
682 !strcmp(l->interface, xfer_sock->iface))) {
683 if ((l->options & LI_MANDATORY_FLAGS) ==
684 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
685 if ((xfer_sock->namespace == NULL &&
686 l->netns == NULL)
Willy Tarreaue5733232019-05-22 19:24:06 +0200687#ifdef USE_NS
Olivier Houchard153659f2017-04-05 22:39:56 +0200688 || (xfer_sock->namespace != NULL &&
689 l->netns != NULL &&
690 !strcmp(xfer_sock->namespace,
691 l->netns->node.key))
692#endif
693 ) {
694 break;
695 }
696
697 }
698 }
699 }
700 xfer_sock = xfer_sock->next;
701 }
702 if (xfer_sock != NULL) {
703 ret = xfer_sock->fd;
704 if (xfer_sock == xfer_sock_list)
705 xfer_sock_list = xfer_sock->next;
706 if (xfer_sock->prev)
707 xfer_sock->prev->next = xfer_sock->next;
708 if (xfer_sock->next)
709 xfer_sock->next->prev = xfer_sock->prev;
710 free(xfer_sock->iface);
711 free(xfer_sock->namespace);
712 free(xfer_sock);
713 }
714 return ret;
715}
716#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200717
Willy Tarreaue6b98942007-10-29 01:09:36 +0100718/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100719 * an error message in <errmsg> if the message is at most <errlen> bytes long
720 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
721 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100722 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
723 * was alright and that no message was returned. ERR_RETRYABLE means that an
724 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700725 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100726 * the meaning of the error, but just indicate that a message is present which
727 * should be displayed with the respective level. Last, ERR_ABORT indicates
728 * that it's pointless to try to start other listeners. No error message is
729 * returned if errlen is NULL.
730 */
731int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
732{
733 __label__ tcp_return, tcp_close_return;
734 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100735 int ext, ready;
736 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100737 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200738#ifdef TCP_MAXSEG
739
740 /* Create a temporary TCP socket to get default parameters we can't
741 * guess.
742 * */
743 ready_len = sizeof(default_tcp_maxseg);
744 if (default_tcp_maxseg == -1) {
745 default_tcp_maxseg = -2;
746 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
747 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100748 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200749 else {
750 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
751 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100752 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
William Dauchyc0e23ae2020-02-12 10:09:14 +0100753 close(fd);
Olivier Houchard153659f2017-04-05 22:39:56 +0200754 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200755 }
756 if (default_tcp6_maxseg == -1) {
757 default_tcp6_maxseg = -2;
758 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
759 if (fd >= 0) {
760 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
761 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100762 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200763 close(fd);
764 }
765 }
766#endif
767
Willy Tarreaue6b98942007-10-29 01:09:36 +0100768
769 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100770 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100771 *errmsg = 0;
772
773 if (listener->state != LI_ASSIGNED)
774 return ERR_NONE; /* already bound */
775
776 err = ERR_NONE;
777
Olivier Houchard153659f2017-04-05 22:39:56 +0200778 if (listener->fd == -1)
779 listener->fd = tcp_find_compatible_fd(listener);
780
Willy Tarreau40aa0702013-03-10 23:51:38 +0100781 /* if the listener already has an fd assigned, then we were offered the
782 * fd by an external process (most likely the parent), and we don't want
783 * to create a new socket. However we still want to set a few flags on
784 * the socket.
785 */
786 fd = listener->fd;
787 ext = (fd >= 0);
788
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100789 if (!ext) {
790 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
791
792 if (fd == -1) {
793 err |= ERR_RETRYABLE | ERR_ALERT;
794 msg = "cannot create listening socket";
795 goto tcp_return;
796 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100797 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100798
Willy Tarreaue6b98942007-10-29 01:09:36 +0100799 if (fd >= global.maxsock) {
800 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
801 msg = "not enough free sockets (raise '-n' parameter)";
802 goto tcp_close_return;
803 }
804
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200805 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100806 err |= ERR_FATAL | ERR_ALERT;
807 msg = "cannot make socket non-blocking";
808 goto tcp_close_return;
809 }
810
Willy Tarreau40aa0702013-03-10 23:51:38 +0100811 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100812 /* not fatal but should be reported */
813 msg = "cannot do so_reuseaddr";
814 err |= ERR_ALERT;
815 }
816
817 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900818 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200819 else {
820 struct linger tmplinger;
821 socklen_t len = sizeof(tmplinger);
822 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
823 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
824 tmplinger.l_onoff = 0;
825 tmplinger.l_linger = 0;
826 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
827 sizeof(tmplinger));
828 }
829 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100830
Willy Tarreaue6b98942007-10-29 01:09:36 +0100831#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000832 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
833 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100834 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000835 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100836 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100837#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200838
Willy Tarreau40aa0702013-03-10 23:51:38 +0100839 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200840 switch (listener->addr.ss_family) {
841 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200842 if (1
843#if defined(IP_TRANSPARENT)
844 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
845#endif
846#if defined(IP_FREEBIND)
847 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
848#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200849#if defined(IP_BINDANY)
850 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
851#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200852#if defined(SO_BINDANY)
853 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
854#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200855 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200856 msg = "cannot make listening socket transparent";
857 err |= ERR_ALERT;
858 }
859 break;
860 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200861 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200862#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200863 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
864#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100865#if defined(IP_FREEBIND)
866 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
867#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200868#if defined(IPV6_BINDANY)
869 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
870#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200871#if defined(SO_BINDANY)
872 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
873#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200874 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200875 msg = "cannot make listening socket transparent";
876 err |= ERR_ALERT;
877 }
878 break;
879 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100880 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200881
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100882#ifdef SO_BINDTODEVICE
883 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100884 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100885 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100886 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100887 msg = "cannot bind listener to device";
888 err |= ERR_WARN;
889 }
890 }
891#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400892#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100893 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400894 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200895 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
896 msg = "cannot set MSS";
897 err |= ERR_WARN;
898 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200899 } else if (ext) {
900 int tmpmaxseg = -1;
901 int defaultmss;
902 socklen_t len = sizeof(tmpmaxseg);
903
904 if (listener->addr.ss_family == AF_INET)
905 defaultmss = default_tcp_maxseg;
906 else
907 defaultmss = default_tcp6_maxseg;
908
909 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100910 if (defaultmss > 0 &&
911 tmpmaxseg != defaultmss &&
912 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200913 msg = "cannot set MSS";
914 err |= ERR_WARN;
915 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200916 }
917#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100918#if defined(TCP_USER_TIMEOUT)
919 if (listener->tcp_ut) {
920 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
921 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
922 msg = "cannot set TCP User Timeout";
923 err |= ERR_WARN;
924 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200925 } else
926 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
927 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100928#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200929#if defined(TCP_DEFER_ACCEPT)
930 if (listener->options & LI_O_DEF_ACCEPT) {
931 /* defer accept by up to one second */
932 int accept_delay = 1;
933 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
934 msg = "cannot enable DEFER_ACCEPT";
935 err |= ERR_WARN;
936 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200937 } else
938 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
939 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200940#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200941#if defined(TCP_FASTOPEN)
942 if (listener->options & LI_O_TCP_FO) {
943 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100944 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200945 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
946 msg = "cannot enable TCP_FASTOPEN";
947 err |= ERR_WARN;
948 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200949 } else {
950 socklen_t len;
951 int qlen;
952 len = sizeof(qlen);
953 /* Only disable fast open if it was enabled, we don't want
954 * the kernel to create a fast open queue if there's none.
955 */
956 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
957 qlen != 0) {
958 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
959 sizeof(zero)) == -1) {
960 msg = "cannot disable TCP_FASTOPEN";
961 err |= ERR_WARN;
962 }
963 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200964 }
965#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100966#if defined(IPV6_V6ONLY)
967 if (listener->options & LI_O_V6ONLY)
968 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100969 else if (listener->options & LI_O_V4V6)
970 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100971#endif
972
Willy Tarreau40aa0702013-03-10 23:51:38 +0100973 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100974 err |= ERR_RETRYABLE | ERR_ALERT;
975 msg = "cannot bind socket";
976 goto tcp_close_return;
977 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100978
Willy Tarreau40aa0702013-03-10 23:51:38 +0100979 ready = 0;
980 ready_len = sizeof(ready);
981 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
982 ready = 0;
983
984 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100985 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100986 err |= ERR_RETRYABLE | ERR_ALERT;
987 msg = "cannot listen to socket";
988 goto tcp_close_return;
989 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100990
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400991#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200992 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900993 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200994 else
995 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200996#endif
997
Willy Tarreaue6b98942007-10-29 01:09:36 +0100998 /* the socket is ready */
999 listener->fd = fd;
1000 listener->state = LI_LISTEN;
1001
Willy Tarreaua9786b62018-01-25 07:22:13 +01001002 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +01001003 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaueb472682010-05-28 18:46:57 +02001004
Willy Tarreaue6b98942007-10-29 01:09:36 +01001005 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001006 if (msg && errlen) {
1007 char pn[INET6_ADDRSTRLEN];
1008
Willy Tarreau631f01c2011-09-05 00:36:48 +02001009 addr_to_str(&listener->addr, pn, sizeof(pn));
1010 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001011 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001012 return err;
1013
1014 tcp_close_return:
1015 close(fd);
1016 goto tcp_return;
1017}
1018
1019/* This function creates all TCP sockets bound to the protocol entry <proto>.
1020 * It is intended to be used as the protocol's bind_all() function.
1021 * The sockets will be registered but not added to any fd_set, in order not to
1022 * loose them across the fork(). A call to enable_all_listeners() is needed
1023 * to complete initialization. The return value is composed from ERR_*.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001024 *
1025 * Must be called with proto_lock held.
1026 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001027 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001028static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001029{
1030 struct listener *listener;
1031 int err = ERR_NONE;
1032
1033 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001034 err |= tcp_bind_listener(listener, errmsg, errlen);
1035 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001036 break;
1037 }
1038
1039 return err;
1040}
1041
Willy Tarreau32282382017-09-15 07:44:44 +02001042/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1043 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1044 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001045 *
1046 * Must be called with proto_lock held.
1047 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001048 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001049static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001050{
1051 if (listener->state != LI_INIT)
1052 return;
1053 listener->state = LI_ASSIGNED;
1054 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001055 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001056 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1057 proto_tcpv4.nb_listeners++;
1058}
1059
Willy Tarreau32282382017-09-15 07:44:44 +02001060/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1061 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1062 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001063 *
1064 * Must be called with proto_lock held.
1065 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001066 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001067static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001068{
1069 if (listener->state != LI_INIT)
1070 return;
1071 listener->state = LI_ASSIGNED;
1072 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001073 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001074 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1075 proto_tcpv6.nb_listeners++;
1076}
1077
Willy Tarreau092d8652014-07-07 20:22:12 +02001078/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1079 * was totally stopped, or > 0 if correctly paused.
1080 */
1081int tcp_pause_listener(struct listener *l)
1082{
1083 if (shutdown(l->fd, SHUT_WR) != 0)
1084 return -1; /* Solaris dies here */
1085
Willy Tarreaue2711c72019-02-27 15:39:41 +01001086 if (listen(l->fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +02001087 return -1; /* OpenBSD dies here */
1088
1089 if (shutdown(l->fd, SHUT_RD) != 0)
1090 return -1; /* should always be OK */
1091 return 1;
1092}
1093
William Lallemand2e785f22016-05-25 01:48:42 +02001094/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001095 * Execute the "set-src" action. May be called from {tcp,http}request.
1096 * It only changes the address and tries to preserve the original port. If the
1097 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001098 */
1099enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1100 struct session *sess, struct stream *s, int flags)
1101{
1102 struct connection *cli_conn;
1103
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001104 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand2e785f22016-05-25 01:48:42 +02001105 struct sample *smp;
1106
1107 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1108 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001109 int port = get_net_port(cli_conn->src);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001110
William Lallemand2e785f22016-05-25 01:48:42 +02001111 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001112 ((struct sockaddr_in *)cli_conn->src)->sin_family = AF_INET;
1113 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1114 ((struct sockaddr_in *)cli_conn->src)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001115 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001116 ((struct sockaddr_in6 *)cli_conn->src)->sin6_family = AF_INET6;
1117 memcpy(&((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1118 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001119 }
1120 }
William Lallemand01252ed2016-05-25 02:33:16 +02001121 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001122 }
1123 return ACT_RET_CONT;
1124}
1125
William Lallemand44be6402016-05-25 01:51:35 +02001126/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001127 * Execute the "set-dst" action. May be called from {tcp,http}request.
1128 * It only changes the address and tries to preserve the original port. If the
1129 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001130 */
1131enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1132 struct session *sess, struct stream *s, int flags)
1133{
1134 struct connection *cli_conn;
1135
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001136 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001137 struct sample *smp;
1138
1139 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1140 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001141 int port = get_net_port(cli_conn->dst);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001142
William Lallemand13e9b0c2016-05-25 02:34:07 +02001143 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001144 ((struct sockaddr_in *)cli_conn->dst)->sin_family = AF_INET;
1145 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001146 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001147 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_family = AF_INET6;
1148 memcpy(&((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1149 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001150 }
1151 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1152 }
1153 }
1154 return ACT_RET_CONT;
1155}
1156
1157/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001158 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1159 * We must test the sin_family before setting the port. If the address family
1160 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1161 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001162 */
1163enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1164 struct session *sess, struct stream *s, int flags)
1165{
1166 struct connection *cli_conn;
1167
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001168 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand44be6402016-05-25 01:51:35 +02001169 struct sample *smp;
1170
William Lallemand44be6402016-05-25 01:51:35 +02001171 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1172 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001173 if (cli_conn->src->ss_family == AF_INET6) {
1174 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001175 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001176 if (cli_conn->src->ss_family != AF_INET) {
1177 cli_conn->src->ss_family = AF_INET;
1178 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001179 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001180 ((struct sockaddr_in *)cli_conn->src)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001181 }
1182 }
1183 }
1184 return ACT_RET_CONT;
1185}
1186
William Lallemand13e9b0c2016-05-25 02:34:07 +02001187/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001188 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1189 * We must test the sin_family before setting the port. If the address family
1190 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1191 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001192 */
1193enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1194 struct session *sess, struct stream *s, int flags)
1195{
1196 struct connection *cli_conn;
1197
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001198 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001199 struct sample *smp;
1200
William Lallemand13e9b0c2016-05-25 02:34:07 +02001201 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1202 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001203 if (cli_conn->dst->ss_family == AF_INET6) {
1204 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001205 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001206 if (cli_conn->dst->ss_family != AF_INET) {
1207 cli_conn->dst->ss_family = AF_INET;
1208 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001209 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001210 ((struct sockaddr_in *)cli_conn->dst)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001211 }
1212 }
1213 }
1214 return ACT_RET_CONT;
1215}
1216
Willy Tarreau2d392c22015-08-24 01:43:45 +02001217/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1218static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1219{
1220 struct connection *conn = objt_conn(sess->origin);
1221
1222 if (!conn)
1223 goto out;
1224
1225 if (!conn_ctrl_ready(conn))
1226 goto out;
1227
Willy Tarreau2d392c22015-08-24 01:43:45 +02001228#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001229 /* drain is needed only to send the quick ACK */
1230 conn_sock_drain(conn);
1231
Willy Tarreau2d392c22015-08-24 01:43:45 +02001232 /* re-enable quickack if it was disabled to ack all data and avoid
1233 * retransmits from the client that might trigger a real reset.
1234 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001235 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001236#endif
1237 /* lingering must absolutely be disabled so that we don't send a
1238 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1239 * is present, returning with ERR will cause lingering to be disabled.
1240 */
1241 if (strm)
1242 strm->si[0].flags |= SI_FL_NOLINGER;
1243
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001244 /* We're on the client-facing side, we must force to disable lingering to
1245 * ensure we will use an RST exclusively and kill any pending data.
1246 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001247 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001248
Willy Tarreau2d392c22015-08-24 01:43:45 +02001249#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001250 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001251 /* socket will be quiet now */
1252 goto out;
1253 }
1254#endif
1255 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1256 * Let's fall back on the TTL trick, though it only works for routed
1257 * network and has no effect on local net.
1258 */
1259#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001260 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001261#endif
1262 out:
1263 /* kill the stream if any */
1264 if (strm) {
1265 channel_abort(&strm->req);
1266 channel_abort(&strm->res);
Christopher Faulet177f4802020-03-06 15:10:46 +01001267 strm->req.analysers &= AN_REQ_FLT_END;
1268 strm->res.analysers &= AN_RES_FLT_END;
Christopher Fauletddc005a2020-03-06 15:23:18 +01001269 if (strm->flags & SF_BE_ASSIGNED)
1270 _HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001271 if (!(strm->flags & SF_ERR_MASK))
1272 strm->flags |= SF_ERR_PRXCOND;
1273 if (!(strm->flags & SF_FINST_MASK))
1274 strm->flags |= SF_FINST_R;
1275 }
1276
Olivier Houchard40514102019-03-08 18:54:04 +01001277 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001278 if (sess->listener->counters)
Olivier Houchard40514102019-03-08 18:54:04 +01001279 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001280
Christopher Fauletf573ba22020-03-06 15:15:49 +01001281 return ACT_RET_ABRT;
Willy Tarreau2d392c22015-08-24 01:43:45 +02001282}
1283
William Lallemand13e9b0c2016-05-25 02:34:07 +02001284/* parse "set-{src,dst}[-port]" action */
1285enum act_parse_ret tcp_parse_set_src_dst(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
William Lallemand2e785f22016-05-25 01:48:42 +02001286{
1287 int cur_arg;
1288 struct sample_expr *expr;
1289 unsigned int where;
1290
1291 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001292 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
William Lallemand2e785f22016-05-25 01:48:42 +02001293 if (!expr)
1294 return ACT_RET_PRS_ERR;
1295
1296 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001297 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001298 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001299 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001300 where |= SMP_VAL_BE_HRQ_HDR;
1301
1302 if (!(expr->fetch->val & where)) {
1303 memprintf(err,
1304 "fetch method '%s' extracts information from '%s', none of which is available here",
1305 args[cur_arg-1], sample_src_names(expr->fetch->use));
1306 free(expr);
1307 return ACT_RET_PRS_ERR;
1308 }
1309 rule->arg.expr = expr;
1310 rule->action = ACT_CUSTOM;
1311
1312 if (!strcmp(args[*orig_arg-1], "set-src")) {
1313 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001314 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1315 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001316 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1317 rule->action_ptr = tcp_action_req_set_dst;
1318 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1319 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001320 } else {
1321 return ACT_RET_PRS_ERR;
1322 }
1323
1324 (*orig_arg)++;
1325
1326 return ACT_RET_PRS_OK;
1327}
1328
1329
Willy Tarreau2d392c22015-08-24 01:43:45 +02001330/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1331 * success, ACT_RET_PRS_ERR on error.
1332 */
1333static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1334 struct act_rule *rule, char **err)
1335{
1336 rule->action = ACT_CUSTOM;
1337 rule->action_ptr = tcp_exec_action_silent_drop;
1338 return ACT_RET_PRS_OK;
1339}
1340
Willy Tarreau645513a2010-05-24 20:55:15 +02001341
1342/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001343/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001344/************************************************************************/
1345
Willy Tarreau4a129812012-04-25 17:31:42 +02001346/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001347int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001348{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001349 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001350
1351 if (!cli_conn)
1352 return 0;
1353
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001354 if (!conn_get_src(cli_conn))
1355 return 0;
1356
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001357 switch (cli_conn->src->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001358 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001359 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->src)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001360 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001361 break;
1362 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001363 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->src)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001364 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001365 break;
1366 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001367 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001368 }
Emeric Brunf769f512010-10-22 17:14:01 +02001369
Willy Tarreau37406352012-04-23 16:16:37 +02001370 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001371 return 1;
1372}
1373
Willy Tarreaua5e37562011-12-16 17:06:15 +01001374/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001375static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001376smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001377{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001378 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001379
1380 if (!cli_conn)
1381 return 0;
1382
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001383 if (!conn_get_src(cli_conn))
1384 return 0;
1385
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001386 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001387 if (!(smp->data.u.sint = get_host_port(cli_conn->src)))
Emeric Brunf769f512010-10-22 17:14:01 +02001388 return 0;
1389
Willy Tarreau37406352012-04-23 16:16:37 +02001390 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001391 return 1;
1392}
1393
Willy Tarreau4a129812012-04-25 17:31:42 +02001394/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001395static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001396smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001397{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001398 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001399
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001400 if (!cli_conn)
1401 return 0;
1402
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001403 if (!conn_get_dst(cli_conn))
1404 return 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001405
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001406 switch (cli_conn->dst->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001407 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001408 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001409 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001410 break;
1411 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001412 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001413 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001414 break;
1415 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001416 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001417 }
Emeric Brunf769f512010-10-22 17:14:01 +02001418
Willy Tarreau37406352012-04-23 16:16:37 +02001419 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001420 return 1;
1421}
1422
Willy Tarreau16e01562016-08-09 16:46:18 +02001423/* check if the destination address of the front connection is local to the
1424 * system or if it was intercepted.
1425 */
1426int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1427{
1428 struct connection *conn = objt_conn(smp->sess->origin);
1429 struct listener *li = smp->sess->listener;
1430
1431 if (!conn)
1432 return 0;
1433
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001434 if (!conn_get_dst(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001435 return 0;
1436
1437 smp->data.type = SMP_T_BOOL;
1438 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001439 smp->data.u.sint = addr_is_local(li->netns, conn->dst);
Willy Tarreau16e01562016-08-09 16:46:18 +02001440 return smp->data.u.sint >= 0;
1441}
1442
1443/* check if the source address of the front connection is local to the system
1444 * or not.
1445 */
1446int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1447{
1448 struct connection *conn = objt_conn(smp->sess->origin);
1449 struct listener *li = smp->sess->listener;
1450
1451 if (!conn)
1452 return 0;
1453
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001454 if (!conn_get_src(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001455 return 0;
1456
1457 smp->data.type = SMP_T_BOOL;
1458 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001459 smp->data.u.sint = addr_is_local(li->netns, conn->src);
Willy Tarreau16e01562016-08-09 16:46:18 +02001460 return smp->data.u.sint >= 0;
1461}
1462
Willy Tarreaua5e37562011-12-16 17:06:15 +01001463/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001464static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001465smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001466{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001467 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001468
1469 if (!cli_conn)
1470 return 0;
1471
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001472 if (!conn_get_dst(cli_conn))
1473 return 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001474
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001475 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001476 if (!(smp->data.u.sint = get_host_port(cli_conn->dst)))
Emeric Brunf769f512010-10-22 17:14:01 +02001477 return 0;
1478
Willy Tarreau37406352012-04-23 16:16:37 +02001479 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001480 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001481}
1482
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001483#ifdef TCP_INFO
1484
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001485
1486/* Validates the arguments passed to "fc_*" fetch keywords returning a time
1487 * value. These keywords support an optional string representing the unit of the
1488 * result: "us" for microseconds and "ms" for milliseconds". Returns 0 on error
1489 * and non-zero if OK.
1490 */
1491static int val_fc_time_value(struct arg *args, char **err)
1492{
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001493 if (args[0].type == ARGT_STR) {
1494 if (strcmp(args[0].data.str.area, "us") == 0) {
1495 free(args[0].data.str.area);
1496 args[0].type = ARGT_SINT;
1497 args[0].data.sint = TIME_UNIT_US;
1498 }
1499 else if (strcmp(args[0].data.str.area, "ms") == 0) {
1500 free(args[0].data.str.area);
1501 args[0].type = ARGT_SINT;
1502 args[0].data.sint = TIME_UNIT_MS;
1503 }
1504 else {
1505 memprintf(err, "expects 'us' or 'ms', got '%s'",
1506 args[0].data.str.area);
1507 return 0;
1508 }
1509 }
1510 else {
1511 memprintf(err, "Unexpected arg type");
1512 return 0;
1513 }
1514
1515 return 1;
1516}
1517
1518/* Validates the arguments passed to "fc_*" fetch keywords returning a
1519 * counter. These keywords should be used without any keyword, but because of a
1520 * bug in previous versions, an optional string argument may be passed. In such
1521 * case, the argument is ignored and a warning is emitted. Returns 0 on error
1522 * and non-zero if OK.
1523 */
1524static int var_fc_counter(struct arg *args, char **err)
1525{
1526 if (args[0].type != ARGT_STOP) {
1527 ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n");
1528 if (args[0].type == ARGT_STR)
1529 free(args[0].data.str.area);
1530 args[0].type = ARGT_STOP;
1531 }
1532
1533 return 1;
1534}
1535
Joseph Herlanta6331472018-11-25 12:59:12 -08001536/* Returns some tcp_info data if it's available. "dir" must be set to 0 if
1537 * the client connection is required, otherwise it is set to 1. "val" represents
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001538 * the required value.
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001539 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1540 */
1541static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1542 int dir, int val)
1543{
1544 struct connection *conn;
1545 struct tcp_info info;
1546 socklen_t optlen;
1547
1548 /* strm can be null. */
1549 if (!smp->strm)
1550 return 0;
1551
1552 /* get the object associated with the stream interface.The
1553 * object can be other thing than a connection. For example,
1554 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001555 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001556 if (!conn)
1557 return 0;
1558
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001559 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001560 syscal can fail. */
1561 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001562 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001563 return 0;
1564
1565 /* extract the value. */
1566 smp->data.type = SMP_T_SINT;
1567 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001568 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1569 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1570#if defined(__linux__)
1571 /* these ones are common to all Linux versions */
1572 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1573 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1574 case 4: smp->data.u.sint = info.tcpi_lost; break;
1575 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1576 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1577 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1578#elif defined(__FreeBSD__) || defined(__NetBSD__)
1579 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1580 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1581 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1582 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1583 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1584 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1585 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1586#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001587 default: return 0;
1588 }
1589
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001590 return 1;
1591}
1592
1593/* get the mean rtt of a client connexion */
1594static int
1595smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1596{
1597 if (!get_tcp_info(args, smp, 0, 0))
1598 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001599
1600 /* By default or if explicitly specified, convert rtt to ms */
1601 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1602 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1603
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001604 return 1;
1605}
1606
1607/* get the variance of the mean rtt of a client connexion */
1608static int
1609smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1610{
1611 if (!get_tcp_info(args, smp, 0, 1))
1612 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001613
1614 /* By default or if explicitly specified, convert rttvar to ms */
1615 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1616 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1617
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001618 return 1;
1619}
Joe Williams30fcd392016-08-10 07:06:44 -07001620
1621#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1622
1623/* get the unacked counter on a client connexion */
1624static int
1625smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1626{
1627 if (!get_tcp_info(args, smp, 0, 2))
1628 return 0;
1629 return 1;
1630}
1631
1632/* get the sacked counter on a client connexion */
1633static int
1634smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1635{
1636 if (!get_tcp_info(args, smp, 0, 3))
1637 return 0;
1638 return 1;
1639}
1640
1641/* get the lost counter on a client connexion */
1642static int
1643smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1644{
1645 if (!get_tcp_info(args, smp, 0, 4))
1646 return 0;
1647 return 1;
1648}
1649
1650/* get the retrans counter on a client connexion */
1651static int
1652smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1653{
1654 if (!get_tcp_info(args, smp, 0, 5))
1655 return 0;
1656 return 1;
1657}
1658
1659/* get the fackets counter on a client connexion */
1660static int
1661smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1662{
1663 if (!get_tcp_info(args, smp, 0, 6))
1664 return 0;
1665 return 1;
1666}
1667
1668/* get the reordering counter on a client connexion */
1669static int
1670smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1671{
1672 if (!get_tcp_info(args, smp, 0, 7))
1673 return 0;
1674 return 1;
1675}
1676#endif // linux || freebsd || netbsd
1677#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001678
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001679#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001680/* parse the "v4v6" bind keyword */
1681static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1682{
1683 struct listener *l;
1684
1685 list_for_each_entry(l, &conf->listeners, by_bind) {
1686 if (l->addr.ss_family == AF_INET6)
1687 l->options |= LI_O_V4V6;
1688 }
1689
1690 return 0;
1691}
1692
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001693/* parse the "v6only" bind keyword */
1694static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1695{
1696 struct listener *l;
1697
1698 list_for_each_entry(l, &conf->listeners, by_bind) {
1699 if (l->addr.ss_family == AF_INET6)
1700 l->options |= LI_O_V6ONLY;
1701 }
1702
1703 return 0;
1704}
1705#endif
1706
Pieter Baauwd551fb52013-05-08 22:49:23 +02001707#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001708/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001709static 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 +02001710{
1711 struct listener *l;
1712
Willy Tarreau4348fad2012-09-20 16:48:07 +02001713 list_for_each_entry(l, &conf->listeners, by_bind) {
1714 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1715 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001716 }
1717
Willy Tarreau44791242012-09-12 23:27:21 +02001718 return 0;
1719}
1720#endif
1721
1722#ifdef TCP_DEFER_ACCEPT
1723/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001724static 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 +02001725{
1726 struct listener *l;
1727
Willy Tarreau4348fad2012-09-20 16:48:07 +02001728 list_for_each_entry(l, &conf->listeners, by_bind) {
1729 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1730 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001731 }
1732
Willy Tarreau44791242012-09-12 23:27:21 +02001733 return 0;
1734}
1735#endif
1736
Willy Tarreau1c862c52012-10-05 16:21:00 +02001737#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001738/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001739static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1740{
1741 struct listener *l;
1742
1743 list_for_each_entry(l, &conf->listeners, by_bind) {
1744 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1745 l->options |= LI_O_TCP_FO;
1746 }
1747
1748 return 0;
1749}
1750#endif
1751
Willy Tarreau44791242012-09-12 23:27:21 +02001752#ifdef TCP_MAXSEG
1753/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001754static 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 +02001755{
1756 struct listener *l;
1757 int mss;
1758
Willy Tarreau44791242012-09-12 23:27:21 +02001759 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001760 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001761 return ERR_ALERT | ERR_FATAL;
1762 }
1763
1764 mss = atoi(args[cur_arg + 1]);
1765 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001766 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001767 return ERR_ALERT | ERR_FATAL;
1768 }
1769
Willy Tarreau4348fad2012-09-20 16:48:07 +02001770 list_for_each_entry(l, &conf->listeners, by_bind) {
1771 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1772 l->maxseg = mss;
1773 }
Willy Tarreau44791242012-09-12 23:27:21 +02001774
1775 return 0;
1776}
1777#endif
1778
Willy Tarreau2af207a2015-02-04 00:45:58 +01001779#ifdef TCP_USER_TIMEOUT
1780/* parse the "tcp-ut" bind keyword */
1781static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1782{
1783 const char *ptr = NULL;
1784 struct listener *l;
1785 unsigned int timeout;
1786
1787 if (!*args[cur_arg + 1]) {
1788 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1789 return ERR_ALERT | ERR_FATAL;
1790 }
1791
1792 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001793 if (ptr == PARSE_TIME_OVER) {
1794 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1795 args[cur_arg+1], args[cur_arg]);
1796 return ERR_ALERT | ERR_FATAL;
1797 }
1798 else if (ptr == PARSE_TIME_UNDER) {
1799 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1800 args[cur_arg+1], args[cur_arg]);
1801 return ERR_ALERT | ERR_FATAL;
1802 }
1803 else if (ptr) {
Willy Tarreau2af207a2015-02-04 00:45:58 +01001804 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1805 return ERR_ALERT | ERR_FATAL;
1806 }
1807
1808 list_for_each_entry(l, &conf->listeners, by_bind) {
1809 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1810 l->tcp_ut = timeout;
1811 }
1812
1813 return 0;
1814}
1815#endif
1816
Willy Tarreau44791242012-09-12 23:27:21 +02001817#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001818/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001819static 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 +02001820{
1821 struct listener *l;
1822
Willy Tarreau44791242012-09-12 23:27:21 +02001823 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001824 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001825 return ERR_ALERT | ERR_FATAL;
1826 }
1827
Willy Tarreau4348fad2012-09-20 16:48:07 +02001828 list_for_each_entry(l, &conf->listeners, by_bind) {
1829 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1830 l->interface = strdup(args[cur_arg + 1]);
1831 }
Willy Tarreau44791242012-09-12 23:27:21 +02001832
Willy Tarreau44791242012-09-12 23:27:21 +02001833 return 0;
1834}
1835#endif
1836
Willy Tarreaue5733232019-05-22 19:24:06 +02001837#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001838/* parse the "namespace" bind keyword */
1839static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1840{
1841 struct listener *l;
1842 char *namespace = NULL;
1843
1844 if (!*args[cur_arg + 1]) {
1845 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1846 return ERR_ALERT | ERR_FATAL;
1847 }
1848 namespace = args[cur_arg + 1];
1849
1850 list_for_each_entry(l, &conf->listeners, by_bind) {
1851 l->netns = netns_store_lookup(namespace, strlen(namespace));
1852
1853 if (l->netns == NULL)
1854 l->netns = netns_store_insert(namespace);
1855
1856 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001857 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001858 return ERR_ALERT | ERR_FATAL;
1859 }
1860 }
1861 return 0;
1862}
1863#endif
1864
Willy Tarreau163d4622015-10-13 16:16:41 +02001865#ifdef TCP_USER_TIMEOUT
1866/* parse the "tcp-ut" server keyword */
1867static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1868{
1869 const char *ptr = NULL;
1870 unsigned int timeout;
1871
1872 if (!*args[*cur_arg + 1]) {
1873 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1874 return ERR_ALERT | ERR_FATAL;
1875 }
1876
1877 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001878 if (ptr == PARSE_TIME_OVER) {
1879 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1880 args[*cur_arg+1], args[*cur_arg]);
1881 return ERR_ALERT | ERR_FATAL;
1882 }
1883 else if (ptr == PARSE_TIME_UNDER) {
1884 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1885 args[*cur_arg+1], args[*cur_arg]);
1886 return ERR_ALERT | ERR_FATAL;
1887 }
1888 else if (ptr) {
Willy Tarreau163d4622015-10-13 16:16:41 +02001889 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1890 return ERR_ALERT | ERR_FATAL;
1891 }
1892
1893 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1894 newsrv->tcp_ut = timeout;
1895
1896 return 0;
1897}
1898#endif
1899
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001900
Willy Tarreau4a129812012-04-25 17:31:42 +02001901/* Note: must not be declared <const> as its list will be overwritten.
1902 * Note: fetches that may return multiple types must be declared as the lowest
1903 * common denominator, the type that can be casted into all other ones. For
1904 * instance v4/v6 must be declared v4.
1905 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001906static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001907 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001908 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001909 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001910 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001911 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001912 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001913#ifdef TCP_INFO
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001914 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
1915 { "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 -07001916#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001917 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1918 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1919 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1920 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1921 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1922 { "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 -07001923#endif // linux || freebsd || netbsd
1924#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001925 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001926}};
1927
Willy Tarreau0108d902018-11-25 19:14:37 +01001928INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1929
Willy Tarreau44791242012-09-12 23:27:21 +02001930/************************************************************************/
1931/* All supported bind keywords must be declared here. */
1932/************************************************************************/
1933
1934/* Note: must not be declared <const> as its list will be overwritten.
1935 * Please take care of keeping this list alphabetically sorted, doing so helps
1936 * all code contributors.
1937 * Optional keywords are also declared with a NULL ->parse() function so that
1938 * the config parser can report an appropriate error when a known keyword was
1939 * not enabled.
1940 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001941static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001942#ifdef TCP_DEFER_ACCEPT
1943 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1944#endif
1945#ifdef SO_BINDTODEVICE
1946 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1947#endif
1948#ifdef TCP_MAXSEG
1949 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1950#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001951#ifdef TCP_USER_TIMEOUT
1952 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1953#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001954#ifdef TCP_FASTOPEN
1955 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1956#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001957#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001958 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1959#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001960#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001961 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001962 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1963#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001964#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001965 { "namespace", bind_parse_namespace, 1 },
1966#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001967 /* the versions with the NULL parse function*/
1968 { "defer-accept", NULL, 0 },
1969 { "interface", NULL, 1 },
1970 { "mss", NULL, 1 },
1971 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001972 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001973 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001974 { NULL, NULL, 0 },
1975}};
1976
Willy Tarreau0108d902018-11-25 19:14:37 +01001977INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1978
Willy Tarreau163d4622015-10-13 16:16:41 +02001979static struct srv_kw_list srv_kws = { "TCP", { }, {
1980#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001981 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02001982#endif
1983 { NULL, NULL, 0 },
1984}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02001985
Willy Tarreau0108d902018-11-25 19:14:37 +01001986INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
1987
Willy Tarreau2d392c22015-08-24 01:43:45 +02001988static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001989 { "set-src", tcp_parse_set_src_dst },
1990 { "set-src-port", tcp_parse_set_src_dst },
1991 { "set-dst" , tcp_parse_set_src_dst },
1992 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02001993 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02001994 { /* END */ }
1995}};
1996
Willy Tarreau0108d902018-11-25 19:14:37 +01001997INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions);
1998
Willy Tarreau620408f2016-10-21 16:37:51 +02001999static struct action_kw_list tcp_req_sess_actions = {ILH, {
Willy Tarreau620408f2016-10-21 16:37:51 +02002000 { "set-src", tcp_parse_set_src_dst },
2001 { "set-src-port", tcp_parse_set_src_dst },
2002 { "set-dst" , tcp_parse_set_src_dst },
2003 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002004 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau620408f2016-10-21 16:37:51 +02002005 { /* END */ }
2006}};
2007
Willy Tarreau0108d902018-11-25 19:14:37 +01002008INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions);
2009
Willy Tarreau2d392c22015-08-24 01:43:45 +02002010static struct action_kw_list tcp_req_cont_actions = {ILH, {
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002011 { "set-dst" , tcp_parse_set_src_dst },
2012 { "set-dst-port", tcp_parse_set_src_dst },
2013 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002014 { /* END */ }
2015}};
2016
Willy Tarreau0108d902018-11-25 19:14:37 +01002017INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2018
Willy Tarreau2d392c22015-08-24 01:43:45 +02002019static struct action_kw_list tcp_res_cont_actions = {ILH, {
2020 { "silent-drop", tcp_parse_silent_drop },
2021 { /* END */ }
2022}};
2023
Willy Tarreau0108d902018-11-25 19:14:37 +01002024INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
2025
Willy Tarreau2d392c22015-08-24 01:43:45 +02002026static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002027 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002028 { "set-src", tcp_parse_set_src_dst },
2029 { "set-src-port", tcp_parse_set_src_dst },
2030 { "set-dst", tcp_parse_set_src_dst },
2031 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002032 { /* END */ }
2033}};
2034
Willy Tarreau0108d902018-11-25 19:14:37 +01002035INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2036
Willy Tarreau2d392c22015-08-24 01:43:45 +02002037static struct action_kw_list http_res_actions = {ILH, {
2038 { "silent-drop", tcp_parse_silent_drop },
2039 { /* END */ }
2040}};
2041
Willy Tarreau0108d902018-11-25 19:14:37 +01002042INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002043
Willy Tarreau80713382018-11-26 10:19:54 +01002044REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
Willy Tarreauba962912016-12-21 18:55:02 +01002045#if defined(IP_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002046 " IP_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002047#endif
2048#if defined(IPV6_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002049 " IPV6_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002050#endif
2051#if defined(IP_FREEBIND)
Willy Tarreau80713382018-11-26 10:19:54 +01002052 " IP_FREEBIND"
Willy Tarreauba962912016-12-21 18:55:02 +01002053#endif
2054#if defined(IP_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002055 " IP_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002056#endif
2057#if defined(IPV6_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002058 " IPV6_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002059#endif
2060#if defined(SO_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002061 " SO_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002062#endif
Willy Tarreau80713382018-11-26 10:19:54 +01002063 "");
Willy Tarreaue6b98942007-10-29 01:09:36 +01002064
2065
2066/*
2067 * Local variables:
2068 * c-indent-level: 8
2069 * c-basic-offset: 8
2070 * End:
2071 */