blob: 2568d101482f6a6d6df59de5b45a6fef3e2e8c78 [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 Tarreau122eba92020-06-04 10:15:32 +020033#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020034#include <haproxy/api.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020035#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020036#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020037#include <haproxy/http_rules.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020038#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020039#include <haproxy/listener.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020040#include <haproxy/sample.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020041#include <haproxy/tools.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020042#include <haproxy/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010043
Willy Tarreau2d392c22015-08-24 01:43:45 +020044#include <types/connection.h>
Willy Tarreau39713102016-11-25 15:49:32 +010045#include <types/stream.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010046
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020047#include <haproxy/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 Tarreau0f6ffd62020-06-03 19:33:00 +020050#include <haproxy/fd.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020051#include <proto/log.h>
Willy Tarreaufc8f6a82020-06-03 19:20:59 +020052#include <haproxy/port_range.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +020053#include <haproxy/protocol.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020054#include <proto/http_ana.h>
Willy Tarreaufc774542020-06-04 17:31:04 +020055#include <haproxy/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020056#include <proto/proxy.h>
Willy Tarreau163d4622015-10-13 16:16:41 +020057#include <proto/server.h>
Willy Tarreau39713102016-11-25 15:49:32 +010058#include <proto/tcp_rules.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010059
Emeric Bruncf20bf12010-10-22 16:06:11 +020060static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
61static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020062static void tcpv4_add_listener(struct listener *listener, int port);
63static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010064
65/* Note: must not be declared <const> as its list will be overwritten */
66static struct protocol proto_tcpv4 = {
67 .name = "tcpv4",
68 .sock_domain = AF_INET,
69 .sock_type = SOCK_STREAM,
70 .sock_prot = IPPROTO_TCP,
71 .sock_family = AF_INET,
72 .sock_addrlen = sizeof(struct sockaddr_in),
73 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020074 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020075 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020076 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010077 .bind_all = tcp_bind_listeners,
78 .unbind_all = unbind_all_listeners,
79 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020080 .get_src = tcp_get_src,
81 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020082 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020083 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010084 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
85 .nb_listeners = 0,
86};
87
Willy Tarreau0108d902018-11-25 19:14:37 +010088INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
89
Willy Tarreaue6b98942007-10-29 01:09:36 +010090/* Note: must not be declared <const> as its list will be overwritten */
91static struct protocol proto_tcpv6 = {
92 .name = "tcpv6",
93 .sock_domain = AF_INET6,
94 .sock_type = SOCK_STREAM,
95 .sock_prot = IPPROTO_TCP,
96 .sock_family = AF_INET6,
97 .sock_addrlen = sizeof(struct sockaddr_in6),
98 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020099 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200100 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200101 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100102 .bind_all = tcp_bind_listeners,
103 .unbind_all = unbind_all_listeners,
104 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200105 .get_src = tcp_get_src,
106 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +0200107 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200108 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100109 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
110 .nb_listeners = 0,
111};
112
Willy Tarreau0108d902018-11-25 19:14:37 +0100113INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
114
Olivier Houchard153659f2017-04-05 22:39:56 +0200115/* Default TCP parameters, got by opening a temporary TCP socket. */
116#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100117static THREAD_LOCAL int default_tcp_maxseg = -1;
118static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200119#endif
120
David du Colombier6f5ccb12011-03-10 22:26:24 +0100121/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100122 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
123 * - 0 : ignore remote address (may even be a NULL pointer)
124 * - 1 : use provided address
125 * - 2 : use provided port
126 * - 3 : use both
127 *
128 * The function supports multiple foreign binding methods :
129 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100130 * The second one can be used as a fallback for the first one.
131 * This function returns 0 when everything's OK, 1 if it could not bind, to the
132 * local address, 2 if it could not bind to the foreign address.
133 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100134int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100135{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100136 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100137 int foreign_ok = 0;
138 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100139 static THREAD_LOCAL int ip_transp_working = 1;
140 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200141
David du Colombier65c17962012-07-13 14:34:59 +0200142 switch (local->ss_family) {
143 case AF_INET:
144 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200145 /* This deserves some explanation. Some platforms will support
146 * multiple combinations of certain methods, so we try the
147 * supported ones until one succeeds.
148 */
149 if (0
150#if defined(IP_TRANSPARENT)
151 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
152#endif
153#if defined(IP_FREEBIND)
154 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
155#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200156#if defined(IP_BINDANY)
157 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
158#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200159#if defined(SO_BINDANY)
160 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
161#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200162 )
David du Colombier65c17962012-07-13 14:34:59 +0200163 foreign_ok = 1;
164 else
165 ip_transp_working = 0;
166 }
167 break;
168 case AF_INET6:
169 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200170 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200171#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200172 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
173#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100174#if defined(IP_FREEBIND)
175 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
176#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200177#if defined(IPV6_BINDANY)
178 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
179#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200180#if defined(SO_BINDANY)
181 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
182#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200183 )
David du Colombier65c17962012-07-13 14:34:59 +0200184 foreign_ok = 1;
185 else
186 ip6_transp_working = 0;
187 }
188 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100189 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200190
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100191 if (flags) {
192 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200193 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100194 switch (remote->ss_family) {
195 case AF_INET:
196 if (flags & 1)
197 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
198 if (flags & 2)
199 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
200 break;
201 case AF_INET6:
202 if (flags & 1)
203 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
204 if (flags & 2)
205 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
206 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100207 default:
208 /* we don't want to try to bind to an unknown address family */
209 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100210 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100211 }
212
Simon Hormande072bd2011-06-24 15:11:37 +0900213 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100214 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200215 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200216 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
217 if (ret < 0)
218 return 2;
219 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100220 }
221 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200222 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200223 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
224 if (ret < 0)
225 return 1;
226 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100227 }
228
229 if (!flags)
230 return 0;
231
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100232 if (!foreign_ok)
233 /* we could not bind to a foreign address */
234 return 2;
235
236 return 0;
237}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100238
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200239/* conn->dst MUST be valid */
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100240static int create_server_socket(struct connection *conn)
241{
Willy Tarreau529c1392014-12-24 13:47:55 +0100242 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100243
Willy Tarreaue5733232019-05-22 19:24:06 +0200244#ifdef USE_NS
Willy Tarreau529c1392014-12-24 13:47:55 +0100245 if (objt_server(conn->target)) {
246 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
247 ns = conn->proxy_netns;
248 else
249 ns = __objt_server(conn->target)->netns;
250 }
251#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200252 return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, IPPROTO_TCP);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100253}
Willy Tarreau9650f372009-08-16 14:02:45 +0200254
255/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200256 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200257 * to connection <conn> using (si->{target,dst}). A source address may be
258 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200259 * bind addresses are still determined locally (due to the possible need of a
260 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100261 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100262 * supported. The <data> parameter is a boolean indicating whether there are data
263 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200264 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
265 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100266 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200267 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
268 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200269 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200270 * Note that a pending send_proxy message accounts for data.
271 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200272 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200273 * - SF_ERR_NONE if everything's OK
274 * - SF_ERR_SRVTO if there are no more servers
275 * - SF_ERR_SRVCL if the connection was refused by the server
276 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
277 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
278 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100279 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100280 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200281 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100282 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200283 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100284
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200285int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200286{
287 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100288 struct server *srv;
289 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100290 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100291 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800292 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100293
Olivier Houchard637b6952018-11-23 14:23:07 +0100294 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100295
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100296 switch (obj_type(conn->target)) {
297 case OBJ_TYPE_PROXY:
298 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100299 srv = NULL;
300 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100301 case OBJ_TYPE_SERVER:
302 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100303 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100304 /* Make sure we check that we have data before activating
305 * TFO, or we could trigger a kernel issue whereby after
306 * a successful connect() == 0, any subsequent connect()
307 * will return EINPROGRESS instead of EISCONN.
308 */
309 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
310 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
311 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100312 break;
313 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100314 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200315 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100316 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200317
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200318 if (!conn->dst) {
319 conn->flags |= CO_FL_ERROR;
320 return SF_ERR_INTERNAL;
321 }
322
Willy Tarreau585744b2017-08-24 14:31:19 +0200323 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100324
325 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200326 qfprintf(stderr, "Cannot get a server socket.\n");
327
Willy Tarreau9ce70132014-01-24 16:08:19 +0100328 if (errno == ENFILE) {
329 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200330 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100331 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
332 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100333 }
334 else if (errno == EMFILE) {
335 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200336 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100337 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
338 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100339 }
340 else if (errno == ENOBUFS || errno == ENOMEM) {
341 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200342 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100343 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
344 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100345 }
346 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
347 conn->err_code = CO_ER_NOPROTO;
348 }
349 else
350 conn->err_code = CO_ER_SOCK_ERR;
351
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100353 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200354 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200355 }
356
357 if (fd >= global.maxsock) {
358 /* do not log anything there, it's a normal condition when this option
359 * is used to serialize connections to a server !
360 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100361 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200362 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100363 conn->err_code = CO_ER_CONF_FDLIM;
364 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200365 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200366 }
367
368 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900369 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200370 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
371 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100372 conn->err_code = CO_ER_SOCK_ERR;
373 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200374 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 }
376
William Lallemandc03eb012018-11-27 12:02:37 +0100377 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
378 ha_alert("Cannot set CLOEXEC on client socket.\n");
379 close(fd);
380 conn->err_code = CO_ER_SOCK_ERR;
381 conn->flags |= CO_FL_ERROR;
382 return SF_ERR_INTERNAL;
383 }
384
Willy Tarreau9650f372009-08-16 14:02:45 +0200385 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900386 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200387
Willy Tarreau9650f372009-08-16 14:02:45 +0200388 /* allow specific binding :
389 * - server-specific at first
390 * - proxy-specific next
391 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100392 if (srv && srv->conn_src.opts & CO_SRC_BIND)
393 src = &srv->conn_src;
394 else if (be->conn_src.opts & CO_SRC_BIND)
395 src = &be->conn_src;
396 else
397 src = NULL;
398
399 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200400 int ret, flags = 0;
401
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200402 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100403 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100404 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200405 conn->flags |= CO_FL_PRIVATE;
406 /* fall through */
407 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200408 flags = 3;
409 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100410 case CO_SRC_TPROXY_CIP:
411 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200412 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200413 flags = 1;
414 break;
415 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200416 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200417
Willy Tarreau9650f372009-08-16 14:02:45 +0200418#ifdef SO_BINDTODEVICE
419 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100420 if (src->iface_name)
421 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200422#endif
423
Willy Tarreaua4380b42012-12-08 22:49:11 +0100424 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200425 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100426 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200427
428 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200429 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200430
431 do {
432 /* note: in case of retry, we may have to release a previously
433 * allocated port, hence this loop's construct.
434 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200435 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
436 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200437
438 if (!attempts)
439 break;
440 attempts--;
441
Willy Tarreaua4380b42012-12-08 22:49:11 +0100442 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100443 if (!fdinfo[fd].local_port) {
444 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100446 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200447
Willy Tarreaua4380b42012-12-08 22:49:11 +0100448 fdinfo[fd].port_range = src->sport_range;
449 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200450
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200451 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100452 if (ret != 0)
453 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 } while (ret != 0); /* binding NOK */
455 }
456 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000457#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100458 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000459 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
460#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200461 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100462 if (ret != 0)
463 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200464 }
465
Willy Tarreaua4380b42012-12-08 22:49:11 +0100466 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200467 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
468 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 close(fd);
470
Willy Tarreau9650f372009-08-16 14:02:45 +0200471 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100472 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
473 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200474 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100475 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200476 be->id);
477 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100478 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
479 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200480 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100481 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200482 be->id);
483 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100484 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200485 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200486 }
487 }
488
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400489#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200490 /* disabling tcp quick ack now allows the first request to leave the
491 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100492 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200493 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200494 if (flags & (CONNECT_DELACK_ALWAYS) ||
495 ((flags & CONNECT_DELACK_SMART_CONNECT ||
496 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
497 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900498 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200499#endif
500
Willy Tarreau163d4622015-10-13 16:16:41 +0200501#ifdef TCP_USER_TIMEOUT
502 /* there is not much more we can do here when it fails, it's still minor */
503 if (srv && srv->tcp_ut)
504 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
505#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100506
507 if (use_fastopen) {
508#if defined(TCP_FASTOPEN_CONNECT)
509 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
510#endif
511 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100512 if (global.tune.server_sndbuf)
513 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
514
515 if (global.tune.server_rcvbuf)
516 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
517
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200518 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800519 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100520 if (errno == EINPROGRESS || errno == EALREADY) {
521 /* common case, let's wait for connect status */
522 conn->flags |= CO_FL_WAIT_L4_CONN;
523 }
524 else if (errno == EISCONN) {
525 /* should normally not happen but if so, indicates that it's OK */
526 conn->flags &= ~CO_FL_WAIT_L4_CONN;
527 }
528 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200529 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100530 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200531 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100532 conn->err_code = CO_ER_FREE_PORTS;
533 }
534 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200535 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100536 conn->err_code = CO_ER_ADDR_INUSE;
537 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200538
Willy Tarreaub1719512012-12-08 23:03:28 +0100539 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200540 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
541 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200542 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100543 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100544 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200545 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200546 } else if (errno == ETIMEDOUT) {
547 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200548 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
549 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200550 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100551 conn->err_code = CO_ER_SOCK_ERR;
552 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200553 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200554 } else {
555 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
556 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200557 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
558 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200559 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100560 conn->err_code = CO_ER_SOCK_ERR;
561 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200562 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200563 }
564 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100565 else {
566 /* connect() == 0, this is great! */
567 conn->flags &= ~CO_FL_WAIT_L4_CONN;
568 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200569
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100570 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200571
Willy Tarreauf79c8172013-10-21 16:30:56 +0200572 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100573 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200574
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100575 if (conn->flags & CO_FL_WAIT_L4_CONN) {
576 fd_want_send(fd);
577 fd_cant_send(fd);
578 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200579
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200580 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200581 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100582 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200583 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200584 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200585
Willy Tarreaue7dff022015-04-03 01:14:29 +0200586 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200587}
588
589
Willy Tarreau59b94792012-05-11 16:16:40 +0200590/*
591 * Retrieves the source address for the socket <fd>, with <dir> indicating
592 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
593 * success, -1 in case of error. The socket's source address is stored in
594 * <sa> for <salen> bytes.
595 */
596int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
597{
598 if (dir)
599 return getsockname(fd, sa, &salen);
600 else
601 return getpeername(fd, sa, &salen);
602}
603
604
605/*
606 * Retrieves the original destination address for the socket <fd>, with <dir>
607 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
608 * listener, if the original destination address was translated, the original
609 * address is retrieved. It returns 0 in case of success, -1 in case of error.
610 * The socket's source address is stored in <sa> for <salen> bytes.
611 */
612int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
613{
614 if (dir)
615 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100616 else {
617 int ret = getsockname(fd, sa, &salen);
618
619 if (ret < 0)
620 return ret;
621
Willy Tarreaue5733232019-05-22 19:24:06 +0200622#if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100623 /* For TPROXY and Netfilter's NAT, we can retrieve the original
624 * IPv4 address before DNAT/REDIRECT. We must not do that with
625 * other families because v6-mapped IPv4 addresses are still
626 * reported as v4.
627 */
628 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
629 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
630 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200631#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100632 return ret;
633 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200634}
635
Olivier Houchard153659f2017-04-05 22:39:56 +0200636/* XXX: Should probably be elsewhere */
637static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
638{
639 if (a->ss_family != b->ss_family) {
640 return (-1);
641 }
642 switch (a->ss_family) {
643 case AF_INET:
644 {
645 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
646 if (a4->sin_port != b4->sin_port)
647 return (-1);
648 return (memcmp(&a4->sin_addr, &b4->sin_addr,
649 sizeof(a4->sin_addr)));
650 }
651 case AF_INET6:
652 {
653 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
654 if (a6->sin6_port != b6->sin6_port)
655 return (-1);
656 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
657 sizeof(a6->sin6_addr)));
658 }
659 default:
660 return (-1);
661 }
662
663}
664
665#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
666/* When binding the listeners, check if a socket has been sent to us by the
667 * previous process that we could reuse, instead of creating a new one.
668 */
669static int tcp_find_compatible_fd(struct listener *l)
670{
671 struct xfer_sock_list *xfer_sock = xfer_sock_list;
672 int ret = -1;
673
674 while (xfer_sock) {
675 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
676 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
677 (l->interface != NULL && xfer_sock->iface != NULL &&
678 !strcmp(l->interface, xfer_sock->iface))) {
679 if ((l->options & LI_MANDATORY_FLAGS) ==
680 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
681 if ((xfer_sock->namespace == NULL &&
682 l->netns == NULL)
Willy Tarreaue5733232019-05-22 19:24:06 +0200683#ifdef USE_NS
Olivier Houchard153659f2017-04-05 22:39:56 +0200684 || (xfer_sock->namespace != NULL &&
685 l->netns != NULL &&
686 !strcmp(xfer_sock->namespace,
687 l->netns->node.key))
688#endif
689 ) {
690 break;
691 }
692
693 }
694 }
695 }
696 xfer_sock = xfer_sock->next;
697 }
698 if (xfer_sock != NULL) {
699 ret = xfer_sock->fd;
700 if (xfer_sock == xfer_sock_list)
701 xfer_sock_list = xfer_sock->next;
702 if (xfer_sock->prev)
703 xfer_sock->prev->next = xfer_sock->next;
704 if (xfer_sock->next)
705 xfer_sock->next->prev = xfer_sock->prev;
706 free(xfer_sock->iface);
707 free(xfer_sock->namespace);
708 free(xfer_sock);
709 }
710 return ret;
711}
712#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200713
Willy Tarreaue6b98942007-10-29 01:09:36 +0100714/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100715 * an error message in <errmsg> if the message is at most <errlen> bytes long
716 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
717 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100718 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
719 * was alright and that no message was returned. ERR_RETRYABLE means that an
720 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700721 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100722 * the meaning of the error, but just indicate that a message is present which
723 * should be displayed with the respective level. Last, ERR_ABORT indicates
724 * that it's pointless to try to start other listeners. No error message is
725 * returned if errlen is NULL.
726 */
727int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
728{
729 __label__ tcp_return, tcp_close_return;
730 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100731 int ext, ready;
732 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100733 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200734#ifdef TCP_MAXSEG
735
736 /* Create a temporary TCP socket to get default parameters we can't
737 * guess.
738 * */
739 ready_len = sizeof(default_tcp_maxseg);
740 if (default_tcp_maxseg == -1) {
741 default_tcp_maxseg = -2;
742 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
743 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100744 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200745 else {
746 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
747 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100748 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
William Dauchyc0e23ae2020-02-12 10:09:14 +0100749 close(fd);
Olivier Houchard153659f2017-04-05 22:39:56 +0200750 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200751 }
752 if (default_tcp6_maxseg == -1) {
753 default_tcp6_maxseg = -2;
754 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
755 if (fd >= 0) {
756 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
757 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100758 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200759 close(fd);
760 }
761 }
762#endif
763
Willy Tarreaue6b98942007-10-29 01:09:36 +0100764
765 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100766 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100767 *errmsg = 0;
768
769 if (listener->state != LI_ASSIGNED)
770 return ERR_NONE; /* already bound */
771
772 err = ERR_NONE;
773
Olivier Houchard153659f2017-04-05 22:39:56 +0200774 if (listener->fd == -1)
775 listener->fd = tcp_find_compatible_fd(listener);
776
Willy Tarreau40aa0702013-03-10 23:51:38 +0100777 /* if the listener already has an fd assigned, then we were offered the
778 * fd by an external process (most likely the parent), and we don't want
779 * to create a new socket. However we still want to set a few flags on
780 * the socket.
781 */
782 fd = listener->fd;
783 ext = (fd >= 0);
784
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100785 if (!ext) {
786 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
787
788 if (fd == -1) {
789 err |= ERR_RETRYABLE | ERR_ALERT;
790 msg = "cannot create listening socket";
791 goto tcp_return;
792 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100793 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100794
Willy Tarreaue6b98942007-10-29 01:09:36 +0100795 if (fd >= global.maxsock) {
796 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
797 msg = "not enough free sockets (raise '-n' parameter)";
798 goto tcp_close_return;
799 }
800
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200801 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100802 err |= ERR_FATAL | ERR_ALERT;
803 msg = "cannot make socket non-blocking";
804 goto tcp_close_return;
805 }
806
Willy Tarreau40aa0702013-03-10 23:51:38 +0100807 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100808 /* not fatal but should be reported */
809 msg = "cannot do so_reuseaddr";
810 err |= ERR_ALERT;
811 }
812
813 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900814 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200815 else {
816 struct linger tmplinger;
817 socklen_t len = sizeof(tmplinger);
818 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
819 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
820 tmplinger.l_onoff = 0;
821 tmplinger.l_linger = 0;
822 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
823 sizeof(tmplinger));
824 }
825 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100826
Willy Tarreaue6b98942007-10-29 01:09:36 +0100827#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000828 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
829 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100830 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000831 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100832 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100833#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200834
Willy Tarreau40aa0702013-03-10 23:51:38 +0100835 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200836 switch (listener->addr.ss_family) {
837 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200838 if (1
839#if defined(IP_TRANSPARENT)
840 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
841#endif
842#if defined(IP_FREEBIND)
843 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
844#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200845#if defined(IP_BINDANY)
846 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
847#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200848#if defined(SO_BINDANY)
849 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
850#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200851 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200852 msg = "cannot make listening socket transparent";
853 err |= ERR_ALERT;
854 }
855 break;
856 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200857 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200858#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200859 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
860#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100861#if defined(IP_FREEBIND)
862 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
863#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200864#if defined(IPV6_BINDANY)
865 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
866#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200867#if defined(SO_BINDANY)
868 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
869#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200870 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200871 msg = "cannot make listening socket transparent";
872 err |= ERR_ALERT;
873 }
874 break;
875 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100876 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200877
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100878#ifdef SO_BINDTODEVICE
879 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100880 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100881 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100882 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100883 msg = "cannot bind listener to device";
884 err |= ERR_WARN;
885 }
886 }
887#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400888#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100889 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400890 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200891 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
892 msg = "cannot set MSS";
893 err |= ERR_WARN;
894 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200895 } else if (ext) {
896 int tmpmaxseg = -1;
897 int defaultmss;
898 socklen_t len = sizeof(tmpmaxseg);
899
900 if (listener->addr.ss_family == AF_INET)
901 defaultmss = default_tcp_maxseg;
902 else
903 defaultmss = default_tcp6_maxseg;
904
905 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100906 if (defaultmss > 0 &&
907 tmpmaxseg != defaultmss &&
908 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200909 msg = "cannot set MSS";
910 err |= ERR_WARN;
911 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200912 }
913#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100914#if defined(TCP_USER_TIMEOUT)
915 if (listener->tcp_ut) {
916 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
917 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
918 msg = "cannot set TCP User Timeout";
919 err |= ERR_WARN;
920 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200921 } else
922 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
923 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100924#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200925#if defined(TCP_DEFER_ACCEPT)
926 if (listener->options & LI_O_DEF_ACCEPT) {
927 /* defer accept by up to one second */
928 int accept_delay = 1;
929 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
930 msg = "cannot enable DEFER_ACCEPT";
931 err |= ERR_WARN;
932 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200933 } else
934 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
935 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200936#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200937#if defined(TCP_FASTOPEN)
938 if (listener->options & LI_O_TCP_FO) {
939 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100940 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200941 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
942 msg = "cannot enable TCP_FASTOPEN";
943 err |= ERR_WARN;
944 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200945 } else {
946 socklen_t len;
947 int qlen;
948 len = sizeof(qlen);
949 /* Only disable fast open if it was enabled, we don't want
950 * the kernel to create a fast open queue if there's none.
951 */
952 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
953 qlen != 0) {
954 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
955 sizeof(zero)) == -1) {
956 msg = "cannot disable TCP_FASTOPEN";
957 err |= ERR_WARN;
958 }
959 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200960 }
961#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100962#if defined(IPV6_V6ONLY)
963 if (listener->options & LI_O_V6ONLY)
964 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100965 else if (listener->options & LI_O_V4V6)
966 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100967#endif
968
Willy Tarreau40aa0702013-03-10 23:51:38 +0100969 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100970 err |= ERR_RETRYABLE | ERR_ALERT;
971 msg = "cannot bind socket";
972 goto tcp_close_return;
973 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100974
Willy Tarreau40aa0702013-03-10 23:51:38 +0100975 ready = 0;
976 ready_len = sizeof(ready);
977 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
978 ready = 0;
979
980 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100981 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100982 err |= ERR_RETRYABLE | ERR_ALERT;
983 msg = "cannot listen to socket";
984 goto tcp_close_return;
985 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100986
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400987#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200988 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900989 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200990 else
991 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200992#endif
993
Willy Tarreaue6b98942007-10-29 01:09:36 +0100994 /* the socket is ready */
995 listener->fd = fd;
996 listener->state = LI_LISTEN;
997
Willy Tarreaua9786b62018-01-25 07:22:13 +0100998 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100999 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaueb472682010-05-28 18:46:57 +02001000
Willy Tarreaue6b98942007-10-29 01:09:36 +01001001 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001002 if (msg && errlen) {
1003 char pn[INET6_ADDRSTRLEN];
1004
Willy Tarreau631f01c2011-09-05 00:36:48 +02001005 addr_to_str(&listener->addr, pn, sizeof(pn));
1006 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001007 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001008 return err;
1009
1010 tcp_close_return:
1011 close(fd);
1012 goto tcp_return;
1013}
1014
1015/* This function creates all TCP sockets bound to the protocol entry <proto>.
1016 * It is intended to be used as the protocol's bind_all() function.
1017 * The sockets will be registered but not added to any fd_set, in order not to
1018 * loose them across the fork(). A call to enable_all_listeners() is needed
1019 * to complete initialization. The return value is composed from ERR_*.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001020 *
1021 * Must be called with proto_lock held.
1022 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001023 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001024static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001025{
1026 struct listener *listener;
1027 int err = ERR_NONE;
1028
1029 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001030 err |= tcp_bind_listener(listener, errmsg, errlen);
1031 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001032 break;
1033 }
1034
1035 return err;
1036}
1037
Willy Tarreau32282382017-09-15 07:44:44 +02001038/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1039 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1040 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001041 *
1042 * Must be called with proto_lock held.
1043 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001044 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001045static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001046{
1047 if (listener->state != LI_INIT)
1048 return;
1049 listener->state = LI_ASSIGNED;
1050 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001051 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001052 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1053 proto_tcpv4.nb_listeners++;
1054}
1055
Willy Tarreau32282382017-09-15 07:44:44 +02001056/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1057 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1058 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001059 *
1060 * Must be called with proto_lock held.
1061 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001062 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001063static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001064{
1065 if (listener->state != LI_INIT)
1066 return;
1067 listener->state = LI_ASSIGNED;
1068 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001069 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001070 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1071 proto_tcpv6.nb_listeners++;
1072}
1073
Willy Tarreau092d8652014-07-07 20:22:12 +02001074/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1075 * was totally stopped, or > 0 if correctly paused.
1076 */
1077int tcp_pause_listener(struct listener *l)
1078{
1079 if (shutdown(l->fd, SHUT_WR) != 0)
1080 return -1; /* Solaris dies here */
1081
Willy Tarreaue2711c72019-02-27 15:39:41 +01001082 if (listen(l->fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +02001083 return -1; /* OpenBSD dies here */
1084
1085 if (shutdown(l->fd, SHUT_RD) != 0)
1086 return -1; /* should always be OK */
1087 return 1;
1088}
1089
William Lallemand2e785f22016-05-25 01:48:42 +02001090/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001091 * Execute the "set-src" action. May be called from {tcp,http}request.
1092 * It only changes the address and tries to preserve the original port. If the
1093 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001094 */
1095enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1096 struct session *sess, struct stream *s, int flags)
1097{
1098 struct connection *cli_conn;
1099
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001100 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand2e785f22016-05-25 01:48:42 +02001101 struct sample *smp;
1102
1103 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1104 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001105 int port = get_net_port(cli_conn->src);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001106
William Lallemand2e785f22016-05-25 01:48:42 +02001107 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001108 ((struct sockaddr_in *)cli_conn->src)->sin_family = AF_INET;
1109 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1110 ((struct sockaddr_in *)cli_conn->src)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001111 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001112 ((struct sockaddr_in6 *)cli_conn->src)->sin6_family = AF_INET6;
1113 memcpy(&((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1114 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001115 }
1116 }
William Lallemand01252ed2016-05-25 02:33:16 +02001117 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001118 }
1119 return ACT_RET_CONT;
1120}
1121
William Lallemand44be6402016-05-25 01:51:35 +02001122/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001123 * Execute the "set-dst" action. May be called from {tcp,http}request.
1124 * It only changes the address and tries to preserve the original port. If the
1125 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001126 */
1127enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1128 struct session *sess, struct stream *s, int flags)
1129{
1130 struct connection *cli_conn;
1131
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001132 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001133 struct sample *smp;
1134
1135 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1136 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001137 int port = get_net_port(cli_conn->dst);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001138
William Lallemand13e9b0c2016-05-25 02:34:07 +02001139 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001140 ((struct sockaddr_in *)cli_conn->dst)->sin_family = AF_INET;
1141 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001142 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001143 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_family = AF_INET6;
1144 memcpy(&((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1145 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001146 }
1147 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1148 }
1149 }
1150 return ACT_RET_CONT;
1151}
1152
1153/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001154 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1155 * We must test the sin_family before setting the port. If the address family
1156 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1157 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001158 */
1159enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1160 struct session *sess, struct stream *s, int flags)
1161{
1162 struct connection *cli_conn;
1163
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001164 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand44be6402016-05-25 01:51:35 +02001165 struct sample *smp;
1166
William Lallemand44be6402016-05-25 01:51:35 +02001167 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1168 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001169 if (cli_conn->src->ss_family == AF_INET6) {
1170 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001171 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001172 if (cli_conn->src->ss_family != AF_INET) {
1173 cli_conn->src->ss_family = AF_INET;
1174 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001175 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001176 ((struct sockaddr_in *)cli_conn->src)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001177 }
1178 }
1179 }
1180 return ACT_RET_CONT;
1181}
1182
William Lallemand13e9b0c2016-05-25 02:34:07 +02001183/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001184 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1185 * We must test the sin_family before setting the port. If the address family
1186 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1187 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001188 */
1189enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1190 struct session *sess, struct stream *s, int flags)
1191{
1192 struct connection *cli_conn;
1193
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001194 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001195 struct sample *smp;
1196
William Lallemand13e9b0c2016-05-25 02:34:07 +02001197 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1198 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001199 if (cli_conn->dst->ss_family == AF_INET6) {
1200 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001201 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001202 if (cli_conn->dst->ss_family != AF_INET) {
1203 cli_conn->dst->ss_family = AF_INET;
1204 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001205 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001206 ((struct sockaddr_in *)cli_conn->dst)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001207 }
1208 }
1209 }
1210 return ACT_RET_CONT;
1211}
1212
Willy Tarreau2d392c22015-08-24 01:43:45 +02001213/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1214static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1215{
1216 struct connection *conn = objt_conn(sess->origin);
1217
1218 if (!conn)
1219 goto out;
1220
1221 if (!conn_ctrl_ready(conn))
1222 goto out;
1223
Willy Tarreau2d392c22015-08-24 01:43:45 +02001224#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001225 /* drain is needed only to send the quick ACK */
1226 conn_sock_drain(conn);
1227
Willy Tarreau2d392c22015-08-24 01:43:45 +02001228 /* re-enable quickack if it was disabled to ack all data and avoid
1229 * retransmits from the client that might trigger a real reset.
1230 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001231 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001232#endif
1233 /* lingering must absolutely be disabled so that we don't send a
1234 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1235 * is present, returning with ERR will cause lingering to be disabled.
1236 */
1237 if (strm)
1238 strm->si[0].flags |= SI_FL_NOLINGER;
1239
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001240 /* We're on the client-facing side, we must force to disable lingering to
1241 * ensure we will use an RST exclusively and kill any pending data.
1242 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001243 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001244
Willy Tarreau2d392c22015-08-24 01:43:45 +02001245#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001246 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001247 /* socket will be quiet now */
1248 goto out;
1249 }
1250#endif
1251 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1252 * Let's fall back on the TTL trick, though it only works for routed
1253 * network and has no effect on local net.
1254 */
1255#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001256 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001257#endif
1258 out:
1259 /* kill the stream if any */
1260 if (strm) {
1261 channel_abort(&strm->req);
1262 channel_abort(&strm->res);
Christopher Faulet177f4802020-03-06 15:10:46 +01001263 strm->req.analysers &= AN_REQ_FLT_END;
1264 strm->res.analysers &= AN_RES_FLT_END;
Christopher Fauletddc005a2020-03-06 15:23:18 +01001265 if (strm->flags & SF_BE_ASSIGNED)
1266 _HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001267 if (!(strm->flags & SF_ERR_MASK))
1268 strm->flags |= SF_ERR_PRXCOND;
1269 if (!(strm->flags & SF_FINST_MASK))
1270 strm->flags |= SF_FINST_R;
1271 }
1272
Olivier Houchard40514102019-03-08 18:54:04 +01001273 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001274 if (sess->listener->counters)
Olivier Houchard40514102019-03-08 18:54:04 +01001275 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001276
Christopher Fauletf573ba22020-03-06 15:15:49 +01001277 return ACT_RET_ABRT;
Willy Tarreau2d392c22015-08-24 01:43:45 +02001278}
1279
William Lallemand13e9b0c2016-05-25 02:34:07 +02001280/* parse "set-{src,dst}[-port]" action */
1281enum 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 +02001282{
1283 int cur_arg;
1284 struct sample_expr *expr;
1285 unsigned int where;
1286
1287 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001288 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 +02001289 if (!expr)
1290 return ACT_RET_PRS_ERR;
1291
1292 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001293 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001294 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001295 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001296 where |= SMP_VAL_BE_HRQ_HDR;
1297
1298 if (!(expr->fetch->val & where)) {
1299 memprintf(err,
1300 "fetch method '%s' extracts information from '%s', none of which is available here",
1301 args[cur_arg-1], sample_src_names(expr->fetch->use));
1302 free(expr);
1303 return ACT_RET_PRS_ERR;
1304 }
1305 rule->arg.expr = expr;
1306 rule->action = ACT_CUSTOM;
1307
1308 if (!strcmp(args[*orig_arg-1], "set-src")) {
1309 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001310 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1311 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001312 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1313 rule->action_ptr = tcp_action_req_set_dst;
1314 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1315 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001316 } else {
1317 return ACT_RET_PRS_ERR;
1318 }
1319
1320 (*orig_arg)++;
1321
1322 return ACT_RET_PRS_OK;
1323}
1324
1325
Willy Tarreau2d392c22015-08-24 01:43:45 +02001326/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1327 * success, ACT_RET_PRS_ERR on error.
1328 */
1329static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1330 struct act_rule *rule, char **err)
1331{
1332 rule->action = ACT_CUSTOM;
1333 rule->action_ptr = tcp_exec_action_silent_drop;
1334 return ACT_RET_PRS_OK;
1335}
1336
Willy Tarreau645513a2010-05-24 20:55:15 +02001337
1338/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001339/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001340/************************************************************************/
1341
Willy Tarreau4a129812012-04-25 17:31:42 +02001342/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001343int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001344{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001345 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001346
1347 if (!cli_conn)
1348 return 0;
1349
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001350 if (!conn_get_src(cli_conn))
1351 return 0;
1352
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001353 switch (cli_conn->src->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001354 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001355 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->src)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001356 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001357 break;
1358 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001359 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->src)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001360 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001361 break;
1362 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001363 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001364 }
Emeric Brunf769f512010-10-22 17:14:01 +02001365
Willy Tarreau37406352012-04-23 16:16:37 +02001366 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001367 return 1;
1368}
1369
Willy Tarreaua5e37562011-12-16 17:06:15 +01001370/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001371static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001372smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001373{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001374 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001375
1376 if (!cli_conn)
1377 return 0;
1378
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001379 if (!conn_get_src(cli_conn))
1380 return 0;
1381
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001382 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001383 if (!(smp->data.u.sint = get_host_port(cli_conn->src)))
Emeric Brunf769f512010-10-22 17:14:01 +02001384 return 0;
1385
Willy Tarreau37406352012-04-23 16:16:37 +02001386 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001387 return 1;
1388}
1389
Willy Tarreau4a129812012-04-25 17:31:42 +02001390/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001391static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001392smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001393{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001394 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001395
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001396 if (!cli_conn)
1397 return 0;
1398
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001399 if (!conn_get_dst(cli_conn))
1400 return 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001401
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001402 switch (cli_conn->dst->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001403 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001404 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001405 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001406 break;
1407 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001408 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001409 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001410 break;
1411 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001412 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001413 }
Emeric Brunf769f512010-10-22 17:14:01 +02001414
Willy Tarreau37406352012-04-23 16:16:37 +02001415 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001416 return 1;
1417}
1418
Willy Tarreau16e01562016-08-09 16:46:18 +02001419/* check if the destination address of the front connection is local to the
1420 * system or if it was intercepted.
1421 */
1422int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1423{
1424 struct connection *conn = objt_conn(smp->sess->origin);
1425 struct listener *li = smp->sess->listener;
1426
1427 if (!conn)
1428 return 0;
1429
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001430 if (!conn_get_dst(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001431 return 0;
1432
1433 smp->data.type = SMP_T_BOOL;
1434 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001435 smp->data.u.sint = addr_is_local(li->netns, conn->dst);
Willy Tarreau16e01562016-08-09 16:46:18 +02001436 return smp->data.u.sint >= 0;
1437}
1438
1439/* check if the source address of the front connection is local to the system
1440 * or not.
1441 */
1442int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1443{
1444 struct connection *conn = objt_conn(smp->sess->origin);
1445 struct listener *li = smp->sess->listener;
1446
1447 if (!conn)
1448 return 0;
1449
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001450 if (!conn_get_src(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001451 return 0;
1452
1453 smp->data.type = SMP_T_BOOL;
1454 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001455 smp->data.u.sint = addr_is_local(li->netns, conn->src);
Willy Tarreau16e01562016-08-09 16:46:18 +02001456 return smp->data.u.sint >= 0;
1457}
1458
Willy Tarreaua5e37562011-12-16 17:06:15 +01001459/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001460static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001461smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001462{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001463 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001464
1465 if (!cli_conn)
1466 return 0;
1467
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001468 if (!conn_get_dst(cli_conn))
1469 return 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001470
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001471 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001472 if (!(smp->data.u.sint = get_host_port(cli_conn->dst)))
Emeric Brunf769f512010-10-22 17:14:01 +02001473 return 0;
1474
Willy Tarreau37406352012-04-23 16:16:37 +02001475 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001476 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001477}
1478
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001479#ifdef TCP_INFO
1480
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001481
1482/* Validates the arguments passed to "fc_*" fetch keywords returning a time
1483 * value. These keywords support an optional string representing the unit of the
1484 * result: "us" for microseconds and "ms" for milliseconds". Returns 0 on error
1485 * and non-zero if OK.
1486 */
1487static int val_fc_time_value(struct arg *args, char **err)
1488{
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001489 if (args[0].type == ARGT_STR) {
1490 if (strcmp(args[0].data.str.area, "us") == 0) {
1491 free(args[0].data.str.area);
1492 args[0].type = ARGT_SINT;
1493 args[0].data.sint = TIME_UNIT_US;
1494 }
1495 else if (strcmp(args[0].data.str.area, "ms") == 0) {
1496 free(args[0].data.str.area);
1497 args[0].type = ARGT_SINT;
1498 args[0].data.sint = TIME_UNIT_MS;
1499 }
1500 else {
1501 memprintf(err, "expects 'us' or 'ms', got '%s'",
1502 args[0].data.str.area);
1503 return 0;
1504 }
1505 }
1506 else {
1507 memprintf(err, "Unexpected arg type");
1508 return 0;
1509 }
1510
1511 return 1;
1512}
1513
1514/* Validates the arguments passed to "fc_*" fetch keywords returning a
1515 * counter. These keywords should be used without any keyword, but because of a
1516 * bug in previous versions, an optional string argument may be passed. In such
1517 * case, the argument is ignored and a warning is emitted. Returns 0 on error
1518 * and non-zero if OK.
1519 */
1520static int var_fc_counter(struct arg *args, char **err)
1521{
1522 if (args[0].type != ARGT_STOP) {
1523 ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n");
1524 if (args[0].type == ARGT_STR)
1525 free(args[0].data.str.area);
1526 args[0].type = ARGT_STOP;
1527 }
1528
1529 return 1;
1530}
1531
Joseph Herlanta6331472018-11-25 12:59:12 -08001532/* Returns some tcp_info data if it's available. "dir" must be set to 0 if
1533 * the client connection is required, otherwise it is set to 1. "val" represents
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001534 * the required value.
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001535 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1536 */
1537static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1538 int dir, int val)
1539{
1540 struct connection *conn;
1541 struct tcp_info info;
1542 socklen_t optlen;
1543
1544 /* strm can be null. */
1545 if (!smp->strm)
1546 return 0;
1547
1548 /* get the object associated with the stream interface.The
1549 * object can be other thing than a connection. For example,
1550 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001551 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001552 if (!conn)
1553 return 0;
1554
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001555 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001556 syscal can fail. */
1557 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001558 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001559 return 0;
1560
1561 /* extract the value. */
1562 smp->data.type = SMP_T_SINT;
1563 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001564 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1565 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1566#if defined(__linux__)
1567 /* these ones are common to all Linux versions */
1568 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1569 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1570 case 4: smp->data.u.sint = info.tcpi_lost; break;
1571 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1572 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1573 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1574#elif defined(__FreeBSD__) || defined(__NetBSD__)
1575 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1576 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1577 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1578 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1579 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1580 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1581 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1582#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001583 default: return 0;
1584 }
1585
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001586 return 1;
1587}
1588
1589/* get the mean rtt of a client connexion */
1590static int
1591smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1592{
1593 if (!get_tcp_info(args, smp, 0, 0))
1594 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001595
1596 /* By default or if explicitly specified, convert rtt to ms */
1597 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1598 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1599
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001600 return 1;
1601}
1602
1603/* get the variance of the mean rtt of a client connexion */
1604static int
1605smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1606{
1607 if (!get_tcp_info(args, smp, 0, 1))
1608 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001609
1610 /* By default or if explicitly specified, convert rttvar to ms */
1611 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1612 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1613
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001614 return 1;
1615}
Joe Williams30fcd392016-08-10 07:06:44 -07001616
1617#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1618
1619/* get the unacked counter on a client connexion */
1620static int
1621smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1622{
1623 if (!get_tcp_info(args, smp, 0, 2))
1624 return 0;
1625 return 1;
1626}
1627
1628/* get the sacked counter on a client connexion */
1629static int
1630smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1631{
1632 if (!get_tcp_info(args, smp, 0, 3))
1633 return 0;
1634 return 1;
1635}
1636
1637/* get the lost counter on a client connexion */
1638static int
1639smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1640{
1641 if (!get_tcp_info(args, smp, 0, 4))
1642 return 0;
1643 return 1;
1644}
1645
1646/* get the retrans counter on a client connexion */
1647static int
1648smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1649{
1650 if (!get_tcp_info(args, smp, 0, 5))
1651 return 0;
1652 return 1;
1653}
1654
1655/* get the fackets counter on a client connexion */
1656static int
1657smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1658{
1659 if (!get_tcp_info(args, smp, 0, 6))
1660 return 0;
1661 return 1;
1662}
1663
1664/* get the reordering counter on a client connexion */
1665static int
1666smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1667{
1668 if (!get_tcp_info(args, smp, 0, 7))
1669 return 0;
1670 return 1;
1671}
1672#endif // linux || freebsd || netbsd
1673#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001674
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001675#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001676/* parse the "v4v6" bind keyword */
1677static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1678{
1679 struct listener *l;
1680
1681 list_for_each_entry(l, &conf->listeners, by_bind) {
1682 if (l->addr.ss_family == AF_INET6)
1683 l->options |= LI_O_V4V6;
1684 }
1685
1686 return 0;
1687}
1688
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001689/* parse the "v6only" bind keyword */
1690static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1691{
1692 struct listener *l;
1693
1694 list_for_each_entry(l, &conf->listeners, by_bind) {
1695 if (l->addr.ss_family == AF_INET6)
1696 l->options |= LI_O_V6ONLY;
1697 }
1698
1699 return 0;
1700}
1701#endif
1702
Pieter Baauwd551fb52013-05-08 22:49:23 +02001703#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001704/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001705static 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 +02001706{
1707 struct listener *l;
1708
Willy Tarreau4348fad2012-09-20 16:48:07 +02001709 list_for_each_entry(l, &conf->listeners, by_bind) {
1710 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1711 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001712 }
1713
Willy Tarreau44791242012-09-12 23:27:21 +02001714 return 0;
1715}
1716#endif
1717
1718#ifdef TCP_DEFER_ACCEPT
1719/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001720static 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 +02001721{
1722 struct listener *l;
1723
Willy Tarreau4348fad2012-09-20 16:48:07 +02001724 list_for_each_entry(l, &conf->listeners, by_bind) {
1725 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1726 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001727 }
1728
Willy Tarreau44791242012-09-12 23:27:21 +02001729 return 0;
1730}
1731#endif
1732
Willy Tarreau1c862c52012-10-05 16:21:00 +02001733#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001734/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001735static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1736{
1737 struct listener *l;
1738
1739 list_for_each_entry(l, &conf->listeners, by_bind) {
1740 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1741 l->options |= LI_O_TCP_FO;
1742 }
1743
1744 return 0;
1745}
1746#endif
1747
Willy Tarreau44791242012-09-12 23:27:21 +02001748#ifdef TCP_MAXSEG
1749/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001750static 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 +02001751{
1752 struct listener *l;
1753 int mss;
1754
Willy Tarreau44791242012-09-12 23:27:21 +02001755 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001756 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001757 return ERR_ALERT | ERR_FATAL;
1758 }
1759
1760 mss = atoi(args[cur_arg + 1]);
1761 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001762 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001763 return ERR_ALERT | ERR_FATAL;
1764 }
1765
Willy Tarreau4348fad2012-09-20 16:48:07 +02001766 list_for_each_entry(l, &conf->listeners, by_bind) {
1767 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1768 l->maxseg = mss;
1769 }
Willy Tarreau44791242012-09-12 23:27:21 +02001770
1771 return 0;
1772}
1773#endif
1774
Willy Tarreau2af207a2015-02-04 00:45:58 +01001775#ifdef TCP_USER_TIMEOUT
1776/* parse the "tcp-ut" bind keyword */
1777static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1778{
1779 const char *ptr = NULL;
1780 struct listener *l;
1781 unsigned int timeout;
1782
1783 if (!*args[cur_arg + 1]) {
1784 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1785 return ERR_ALERT | ERR_FATAL;
1786 }
1787
1788 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001789 if (ptr == PARSE_TIME_OVER) {
1790 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1791 args[cur_arg+1], args[cur_arg]);
1792 return ERR_ALERT | ERR_FATAL;
1793 }
1794 else if (ptr == PARSE_TIME_UNDER) {
1795 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1796 args[cur_arg+1], args[cur_arg]);
1797 return ERR_ALERT | ERR_FATAL;
1798 }
1799 else if (ptr) {
Willy Tarreau2af207a2015-02-04 00:45:58 +01001800 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1801 return ERR_ALERT | ERR_FATAL;
1802 }
1803
1804 list_for_each_entry(l, &conf->listeners, by_bind) {
1805 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1806 l->tcp_ut = timeout;
1807 }
1808
1809 return 0;
1810}
1811#endif
1812
Willy Tarreau44791242012-09-12 23:27:21 +02001813#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001814/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001815static 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 +02001816{
1817 struct listener *l;
1818
Willy Tarreau44791242012-09-12 23:27:21 +02001819 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001820 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001821 return ERR_ALERT | ERR_FATAL;
1822 }
1823
Willy Tarreau4348fad2012-09-20 16:48:07 +02001824 list_for_each_entry(l, &conf->listeners, by_bind) {
1825 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1826 l->interface = strdup(args[cur_arg + 1]);
1827 }
Willy Tarreau44791242012-09-12 23:27:21 +02001828
Willy Tarreau44791242012-09-12 23:27:21 +02001829 return 0;
1830}
1831#endif
1832
Willy Tarreaue5733232019-05-22 19:24:06 +02001833#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001834/* parse the "namespace" bind keyword */
1835static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1836{
1837 struct listener *l;
1838 char *namespace = NULL;
1839
1840 if (!*args[cur_arg + 1]) {
1841 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1842 return ERR_ALERT | ERR_FATAL;
1843 }
1844 namespace = args[cur_arg + 1];
1845
1846 list_for_each_entry(l, &conf->listeners, by_bind) {
1847 l->netns = netns_store_lookup(namespace, strlen(namespace));
1848
1849 if (l->netns == NULL)
1850 l->netns = netns_store_insert(namespace);
1851
1852 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001853 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001854 return ERR_ALERT | ERR_FATAL;
1855 }
1856 }
1857 return 0;
1858}
1859#endif
1860
Willy Tarreau163d4622015-10-13 16:16:41 +02001861#ifdef TCP_USER_TIMEOUT
1862/* parse the "tcp-ut" server keyword */
1863static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1864{
1865 const char *ptr = NULL;
1866 unsigned int timeout;
1867
1868 if (!*args[*cur_arg + 1]) {
1869 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1870 return ERR_ALERT | ERR_FATAL;
1871 }
1872
1873 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001874 if (ptr == PARSE_TIME_OVER) {
1875 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1876 args[*cur_arg+1], args[*cur_arg]);
1877 return ERR_ALERT | ERR_FATAL;
1878 }
1879 else if (ptr == PARSE_TIME_UNDER) {
1880 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1881 args[*cur_arg+1], args[*cur_arg]);
1882 return ERR_ALERT | ERR_FATAL;
1883 }
1884 else if (ptr) {
Willy Tarreau163d4622015-10-13 16:16:41 +02001885 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1886 return ERR_ALERT | ERR_FATAL;
1887 }
1888
1889 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1890 newsrv->tcp_ut = timeout;
1891
1892 return 0;
1893}
1894#endif
1895
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001896
Willy Tarreau4a129812012-04-25 17:31:42 +02001897/* Note: must not be declared <const> as its list will be overwritten.
1898 * Note: fetches that may return multiple types must be declared as the lowest
1899 * common denominator, the type that can be casted into all other ones. For
1900 * instance v4/v6 must be declared v4.
1901 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001902static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001903 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001904 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001905 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001906 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001907 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001908 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001909#ifdef TCP_INFO
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001910 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
1911 { "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 -07001912#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001913 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1914 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1915 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1916 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1917 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1918 { "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 -07001919#endif // linux || freebsd || netbsd
1920#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001921 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001922}};
1923
Willy Tarreau0108d902018-11-25 19:14:37 +01001924INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1925
Willy Tarreau44791242012-09-12 23:27:21 +02001926/************************************************************************/
1927/* All supported bind keywords must be declared here. */
1928/************************************************************************/
1929
1930/* Note: must not be declared <const> as its list will be overwritten.
1931 * Please take care of keeping this list alphabetically sorted, doing so helps
1932 * all code contributors.
1933 * Optional keywords are also declared with a NULL ->parse() function so that
1934 * the config parser can report an appropriate error when a known keyword was
1935 * not enabled.
1936 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001937static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001938#ifdef TCP_DEFER_ACCEPT
1939 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1940#endif
1941#ifdef SO_BINDTODEVICE
1942 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1943#endif
1944#ifdef TCP_MAXSEG
1945 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1946#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001947#ifdef TCP_USER_TIMEOUT
1948 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1949#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001950#ifdef TCP_FASTOPEN
1951 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1952#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001953#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001954 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1955#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001956#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001957 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001958 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1959#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001960#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001961 { "namespace", bind_parse_namespace, 1 },
1962#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001963 /* the versions with the NULL parse function*/
1964 { "defer-accept", NULL, 0 },
1965 { "interface", NULL, 1 },
1966 { "mss", NULL, 1 },
1967 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001968 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001969 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001970 { NULL, NULL, 0 },
1971}};
1972
Willy Tarreau0108d902018-11-25 19:14:37 +01001973INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1974
Willy Tarreau163d4622015-10-13 16:16:41 +02001975static struct srv_kw_list srv_kws = { "TCP", { }, {
1976#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001977 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02001978#endif
1979 { NULL, NULL, 0 },
1980}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02001981
Willy Tarreau0108d902018-11-25 19:14:37 +01001982INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
1983
Willy Tarreau2d392c22015-08-24 01:43:45 +02001984static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001985 { "set-src", tcp_parse_set_src_dst },
1986 { "set-src-port", tcp_parse_set_src_dst },
1987 { "set-dst" , tcp_parse_set_src_dst },
1988 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02001989 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02001990 { /* END */ }
1991}};
1992
Willy Tarreau0108d902018-11-25 19:14:37 +01001993INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions);
1994
Willy Tarreau620408f2016-10-21 16:37:51 +02001995static struct action_kw_list tcp_req_sess_actions = {ILH, {
Willy Tarreau620408f2016-10-21 16:37:51 +02001996 { "set-src", tcp_parse_set_src_dst },
1997 { "set-src-port", tcp_parse_set_src_dst },
1998 { "set-dst" , tcp_parse_set_src_dst },
1999 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002000 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau620408f2016-10-21 16:37:51 +02002001 { /* END */ }
2002}};
2003
Willy Tarreau0108d902018-11-25 19:14:37 +01002004INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions);
2005
Willy Tarreau2d392c22015-08-24 01:43:45 +02002006static struct action_kw_list tcp_req_cont_actions = {ILH, {
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002007 { "set-dst" , tcp_parse_set_src_dst },
2008 { "set-dst-port", tcp_parse_set_src_dst },
2009 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002010 { /* END */ }
2011}};
2012
Willy Tarreau0108d902018-11-25 19:14:37 +01002013INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2014
Willy Tarreau2d392c22015-08-24 01:43:45 +02002015static struct action_kw_list tcp_res_cont_actions = {ILH, {
2016 { "silent-drop", tcp_parse_silent_drop },
2017 { /* END */ }
2018}};
2019
Willy Tarreau0108d902018-11-25 19:14:37 +01002020INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
2021
Willy Tarreau2d392c22015-08-24 01:43:45 +02002022static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002023 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002024 { "set-src", tcp_parse_set_src_dst },
2025 { "set-src-port", tcp_parse_set_src_dst },
2026 { "set-dst", tcp_parse_set_src_dst },
2027 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002028 { /* END */ }
2029}};
2030
Willy Tarreau0108d902018-11-25 19:14:37 +01002031INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2032
Willy Tarreau2d392c22015-08-24 01:43:45 +02002033static struct action_kw_list http_res_actions = {ILH, {
2034 { "silent-drop", tcp_parse_silent_drop },
2035 { /* END */ }
2036}};
2037
Willy Tarreau0108d902018-11-25 19:14:37 +01002038INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002039
Willy Tarreau80713382018-11-26 10:19:54 +01002040REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
Willy Tarreauba962912016-12-21 18:55:02 +01002041#if defined(IP_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002042 " IP_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002043#endif
2044#if defined(IPV6_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002045 " IPV6_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002046#endif
2047#if defined(IP_FREEBIND)
Willy Tarreau80713382018-11-26 10:19:54 +01002048 " IP_FREEBIND"
Willy Tarreauba962912016-12-21 18:55:02 +01002049#endif
2050#if defined(IP_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002051 " IP_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002052#endif
2053#if defined(IPV6_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002054 " IPV6_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002055#endif
2056#if defined(SO_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002057 " SO_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002058#endif
Willy Tarreau80713382018-11-26 10:19:54 +01002059 "");
Willy Tarreaue6b98942007-10-29 01:09:36 +01002060
2061
2062/*
2063 * Local variables:
2064 * c-indent-level: 8
2065 * c-basic-offset: 8
2066 * End:
2067 */