blob: 9b109f4259b25d92f0f5894ae6dfe1e564a4767a [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Baptiste Assmann39a5f222016-08-08 14:12:08 +020013/* this is to have tcp_info defined on systems using musl
14 * library, such as Alpine Linux
15 */
16#define _GNU_SOURCE
17
Willy Tarreaue6b98942007-10-29 01:09:36 +010018#include <ctype.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <time.h>
25
26#include <sys/param.h>
27#include <sys/socket.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010028#include <sys/types.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010029
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040030#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020031#include <netinet/in.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040032
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020033#include <haproxy/api.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020034#include <haproxy/errors.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020035#include <haproxy/list.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020036#include <haproxy/tools.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020037#include <haproxy/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010038
Willy Tarreau39713102016-11-25 15:49:32 +010039#include <types/action.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020040#include <types/connection.h>
Willy Tarreau39713102016-11-25 15:49:32 +010041#include <types/global.h>
42#include <types/stream.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010043
Willy Tarreau9fcb9842012-04-20 14:45:49 +020044#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020045#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020046#include <proto/connection.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020047#include <haproxy/fd.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020048#include <proto/http_rules.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020049#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020050#include <proto/log.h>
Willy Tarreaufc8f6a82020-06-03 19:20:59 +020051#include <haproxy/port_range.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +020052#include <haproxy/protocol.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053#include <proto/http_ana.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010054#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020055#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020056#include <proto/sample.h>
Willy Tarreau163d4622015-10-13 16:16:41 +020057#include <proto/server.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020058#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010059#include <proto/tcp_rules.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010060
Emeric Bruncf20bf12010-10-22 16:06:11 +020061static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
62static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020063static void tcpv4_add_listener(struct listener *listener, int port);
64static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010065
66/* Note: must not be declared <const> as its list will be overwritten */
67static struct protocol proto_tcpv4 = {
68 .name = "tcpv4",
69 .sock_domain = AF_INET,
70 .sock_type = SOCK_STREAM,
71 .sock_prot = IPPROTO_TCP,
72 .sock_family = AF_INET,
73 .sock_addrlen = sizeof(struct sockaddr_in),
74 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020075 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020076 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020077 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010078 .bind_all = tcp_bind_listeners,
79 .unbind_all = unbind_all_listeners,
80 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020081 .get_src = tcp_get_src,
82 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020083 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020084 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010085 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
86 .nb_listeners = 0,
87};
88
Willy Tarreau0108d902018-11-25 19:14:37 +010089INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
90
Willy Tarreaue6b98942007-10-29 01:09:36 +010091/* Note: must not be declared <const> as its list will be overwritten */
92static struct protocol proto_tcpv6 = {
93 .name = "tcpv6",
94 .sock_domain = AF_INET6,
95 .sock_type = SOCK_STREAM,
96 .sock_prot = IPPROTO_TCP,
97 .sock_family = AF_INET6,
98 .sock_addrlen = sizeof(struct sockaddr_in6),
99 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200100 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200101 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200102 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100103 .bind_all = tcp_bind_listeners,
104 .unbind_all = unbind_all_listeners,
105 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200106 .get_src = tcp_get_src,
107 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +0200108 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200109 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100110 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
111 .nb_listeners = 0,
112};
113
Willy Tarreau0108d902018-11-25 19:14:37 +0100114INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
115
Olivier Houchard153659f2017-04-05 22:39:56 +0200116/* Default TCP parameters, got by opening a temporary TCP socket. */
117#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100118static THREAD_LOCAL int default_tcp_maxseg = -1;
119static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200120#endif
121
David du Colombier6f5ccb12011-03-10 22:26:24 +0100122/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100123 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
124 * - 0 : ignore remote address (may even be a NULL pointer)
125 * - 1 : use provided address
126 * - 2 : use provided port
127 * - 3 : use both
128 *
129 * The function supports multiple foreign binding methods :
130 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100131 * The second one can be used as a fallback for the first one.
132 * This function returns 0 when everything's OK, 1 if it could not bind, to the
133 * local address, 2 if it could not bind to the foreign address.
134 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100135int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100136{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100137 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100138 int foreign_ok = 0;
139 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100140 static THREAD_LOCAL int ip_transp_working = 1;
141 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200142
David du Colombier65c17962012-07-13 14:34:59 +0200143 switch (local->ss_family) {
144 case AF_INET:
145 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200146 /* This deserves some explanation. Some platforms will support
147 * multiple combinations of certain methods, so we try the
148 * supported ones until one succeeds.
149 */
150 if (0
151#if defined(IP_TRANSPARENT)
152 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
153#endif
154#if defined(IP_FREEBIND)
155 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
156#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200157#if defined(IP_BINDANY)
158 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
159#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200160#if defined(SO_BINDANY)
161 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
162#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200163 )
David du Colombier65c17962012-07-13 14:34:59 +0200164 foreign_ok = 1;
165 else
166 ip_transp_working = 0;
167 }
168 break;
169 case AF_INET6:
170 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200171 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200172#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200173 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
174#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100175#if defined(IP_FREEBIND)
176 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
177#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200178#if defined(IPV6_BINDANY)
179 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
180#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200181#if defined(SO_BINDANY)
182 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
183#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200184 )
David du Colombier65c17962012-07-13 14:34:59 +0200185 foreign_ok = 1;
186 else
187 ip6_transp_working = 0;
188 }
189 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100190 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200191
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100192 if (flags) {
193 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200194 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100195 switch (remote->ss_family) {
196 case AF_INET:
197 if (flags & 1)
198 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
199 if (flags & 2)
200 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
201 break;
202 case AF_INET6:
203 if (flags & 1)
204 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
205 if (flags & 2)
206 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
207 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100208 default:
209 /* we don't want to try to bind to an unknown address family */
210 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100211 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100212 }
213
Simon Hormande072bd2011-06-24 15:11:37 +0900214 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100215 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200216 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200217 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
218 if (ret < 0)
219 return 2;
220 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100221 }
222 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200223 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200224 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
225 if (ret < 0)
226 return 1;
227 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100228 }
229
230 if (!flags)
231 return 0;
232
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100233 if (!foreign_ok)
234 /* we could not bind to a foreign address */
235 return 2;
236
237 return 0;
238}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100239
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200240/* conn->dst MUST be valid */
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100241static int create_server_socket(struct connection *conn)
242{
Willy Tarreau529c1392014-12-24 13:47:55 +0100243 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100244
Willy Tarreaue5733232019-05-22 19:24:06 +0200245#ifdef USE_NS
Willy Tarreau529c1392014-12-24 13:47:55 +0100246 if (objt_server(conn->target)) {
247 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
248 ns = conn->proxy_netns;
249 else
250 ns = __objt_server(conn->target)->netns;
251 }
252#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200253 return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, IPPROTO_TCP);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100254}
Willy Tarreau9650f372009-08-16 14:02:45 +0200255
256/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200257 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200258 * to connection <conn> using (si->{target,dst}). A source address may be
259 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200260 * bind addresses are still determined locally (due to the possible need of a
261 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100262 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100263 * supported. The <data> parameter is a boolean indicating whether there are data
264 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200265 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
266 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100267 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200268 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
269 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200270 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200271 * Note that a pending send_proxy message accounts for data.
272 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200273 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200274 * - SF_ERR_NONE if everything's OK
275 * - SF_ERR_SRVTO if there are no more servers
276 * - SF_ERR_SRVCL if the connection was refused by the server
277 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
278 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
279 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100280 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100281 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200282 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100283 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200284 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100285
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200286int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200287{
288 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100289 struct server *srv;
290 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100291 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100292 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800293 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100294
Olivier Houchard637b6952018-11-23 14:23:07 +0100295 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100296
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100297 switch (obj_type(conn->target)) {
298 case OBJ_TYPE_PROXY:
299 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100300 srv = NULL;
301 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100302 case OBJ_TYPE_SERVER:
303 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100304 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100305 /* Make sure we check that we have data before activating
306 * TFO, or we could trigger a kernel issue whereby after
307 * a successful connect() == 0, any subsequent connect()
308 * will return EINPROGRESS instead of EISCONN.
309 */
310 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
311 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
312 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100313 break;
314 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100315 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200316 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100317 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200318
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200319 if (!conn->dst) {
320 conn->flags |= CO_FL_ERROR;
321 return SF_ERR_INTERNAL;
322 }
323
Willy Tarreau585744b2017-08-24 14:31:19 +0200324 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100325
326 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200327 qfprintf(stderr, "Cannot get a server socket.\n");
328
Willy Tarreau9ce70132014-01-24 16:08:19 +0100329 if (errno == ENFILE) {
330 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200331 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100332 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
333 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100334 }
335 else if (errno == EMFILE) {
336 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200337 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100338 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
339 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100340 }
341 else if (errno == ENOBUFS || errno == ENOMEM) {
342 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200343 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100344 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
345 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100346 }
347 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
348 conn->err_code = CO_ER_NOPROTO;
349 }
350 else
351 conn->err_code = CO_ER_SOCK_ERR;
352
Willy Tarreau9650f372009-08-16 14:02:45 +0200353 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100354 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200355 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 }
357
358 if (fd >= global.maxsock) {
359 /* do not log anything there, it's a normal condition when this option
360 * is used to serialize connections to a server !
361 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100362 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200363 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100364 conn->err_code = CO_ER_CONF_FDLIM;
365 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200366 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200367 }
368
369 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900370 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200371 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
372 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100373 conn->err_code = CO_ER_SOCK_ERR;
374 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200375 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200376 }
377
William Lallemandc03eb012018-11-27 12:02:37 +0100378 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
379 ha_alert("Cannot set CLOEXEC on client socket.\n");
380 close(fd);
381 conn->err_code = CO_ER_SOCK_ERR;
382 conn->flags |= CO_FL_ERROR;
383 return SF_ERR_INTERNAL;
384 }
385
Willy Tarreau9650f372009-08-16 14:02:45 +0200386 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900387 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200388
Willy Tarreau9650f372009-08-16 14:02:45 +0200389 /* allow specific binding :
390 * - server-specific at first
391 * - proxy-specific next
392 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100393 if (srv && srv->conn_src.opts & CO_SRC_BIND)
394 src = &srv->conn_src;
395 else if (be->conn_src.opts & CO_SRC_BIND)
396 src = &be->conn_src;
397 else
398 src = NULL;
399
400 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200401 int ret, flags = 0;
402
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200403 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100404 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100405 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200406 conn->flags |= CO_FL_PRIVATE;
407 /* fall through */
408 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200409 flags = 3;
410 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100411 case CO_SRC_TPROXY_CIP:
412 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200413 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200414 flags = 1;
415 break;
416 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200417 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200418
Willy Tarreau9650f372009-08-16 14:02:45 +0200419#ifdef SO_BINDTODEVICE
420 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100421 if (src->iface_name)
422 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200423#endif
424
Willy Tarreaua4380b42012-12-08 22:49:11 +0100425 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200426 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100427 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200428
429 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200430 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200431
432 do {
433 /* note: in case of retry, we may have to release a previously
434 * allocated port, hence this loop's construct.
435 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200436 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
437 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200438
439 if (!attempts)
440 break;
441 attempts--;
442
Willy Tarreaua4380b42012-12-08 22:49:11 +0100443 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100444 if (!fdinfo[fd].local_port) {
445 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200446 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100447 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200448
Willy Tarreaua4380b42012-12-08 22:49:11 +0100449 fdinfo[fd].port_range = src->sport_range;
450 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200451
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200452 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100453 if (ret != 0)
454 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200455 } while (ret != 0); /* binding NOK */
456 }
457 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000458#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100459 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000460 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
461#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200462 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100463 if (ret != 0)
464 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200465 }
466
Willy Tarreaua4380b42012-12-08 22:49:11 +0100467 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200468 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
469 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200470 close(fd);
471
Willy Tarreau9650f372009-08-16 14:02:45 +0200472 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100473 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
474 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200475 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100476 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200477 be->id);
478 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100479 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
480 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200481 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100482 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200483 be->id);
484 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100485 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200486 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200487 }
488 }
489
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400490#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200491 /* disabling tcp quick ack now allows the first request to leave the
492 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100493 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200494 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200495 if (flags & (CONNECT_DELACK_ALWAYS) ||
496 ((flags & CONNECT_DELACK_SMART_CONNECT ||
497 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
498 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900499 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200500#endif
501
Willy Tarreau163d4622015-10-13 16:16:41 +0200502#ifdef TCP_USER_TIMEOUT
503 /* there is not much more we can do here when it fails, it's still minor */
504 if (srv && srv->tcp_ut)
505 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
506#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100507
508 if (use_fastopen) {
509#if defined(TCP_FASTOPEN_CONNECT)
510 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
511#endif
512 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100513 if (global.tune.server_sndbuf)
514 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
515
516 if (global.tune.server_rcvbuf)
517 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
518
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200519 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800520 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100521 if (errno == EINPROGRESS || errno == EALREADY) {
522 /* common case, let's wait for connect status */
523 conn->flags |= CO_FL_WAIT_L4_CONN;
524 }
525 else if (errno == EISCONN) {
526 /* should normally not happen but if so, indicates that it's OK */
527 conn->flags &= ~CO_FL_WAIT_L4_CONN;
528 }
529 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200530 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100531 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200532 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100533 conn->err_code = CO_ER_FREE_PORTS;
534 }
535 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200536 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100537 conn->err_code = CO_ER_ADDR_INUSE;
538 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200539
Willy Tarreaub1719512012-12-08 23:03:28 +0100540 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200541 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
542 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200543 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100544 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100545 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200546 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200547 } else if (errno == ETIMEDOUT) {
548 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200549 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
550 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200551 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100552 conn->err_code = CO_ER_SOCK_ERR;
553 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200554 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200555 } else {
556 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
557 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200558 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
559 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200560 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100561 conn->err_code = CO_ER_SOCK_ERR;
562 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200563 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200564 }
565 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100566 else {
567 /* connect() == 0, this is great! */
568 conn->flags &= ~CO_FL_WAIT_L4_CONN;
569 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200570
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100571 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200572
Willy Tarreauf79c8172013-10-21 16:30:56 +0200573 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100574 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200575
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100576 if (conn->flags & CO_FL_WAIT_L4_CONN) {
577 fd_want_send(fd);
578 fd_cant_send(fd);
579 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200580
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200581 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200582 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100583 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200584 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200585 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200586
Willy Tarreaue7dff022015-04-03 01:14:29 +0200587 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200588}
589
590
Willy Tarreau59b94792012-05-11 16:16:40 +0200591/*
592 * Retrieves the source address for the socket <fd>, with <dir> indicating
593 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
594 * success, -1 in case of error. The socket's source address is stored in
595 * <sa> for <salen> bytes.
596 */
597int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
598{
599 if (dir)
600 return getsockname(fd, sa, &salen);
601 else
602 return getpeername(fd, sa, &salen);
603}
604
605
606/*
607 * Retrieves the original destination address for the socket <fd>, with <dir>
608 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
609 * listener, if the original destination address was translated, the original
610 * address is retrieved. It returns 0 in case of success, -1 in case of error.
611 * The socket's source address is stored in <sa> for <salen> bytes.
612 */
613int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
614{
615 if (dir)
616 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100617 else {
618 int ret = getsockname(fd, sa, &salen);
619
620 if (ret < 0)
621 return ret;
622
Willy Tarreaue5733232019-05-22 19:24:06 +0200623#if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100624 /* For TPROXY and Netfilter's NAT, we can retrieve the original
625 * IPv4 address before DNAT/REDIRECT. We must not do that with
626 * other families because v6-mapped IPv4 addresses are still
627 * reported as v4.
628 */
629 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
630 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
631 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200632#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100633 return ret;
634 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200635}
636
Olivier Houchard153659f2017-04-05 22:39:56 +0200637/* XXX: Should probably be elsewhere */
638static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
639{
640 if (a->ss_family != b->ss_family) {
641 return (-1);
642 }
643 switch (a->ss_family) {
644 case AF_INET:
645 {
646 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
647 if (a4->sin_port != b4->sin_port)
648 return (-1);
649 return (memcmp(&a4->sin_addr, &b4->sin_addr,
650 sizeof(a4->sin_addr)));
651 }
652 case AF_INET6:
653 {
654 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
655 if (a6->sin6_port != b6->sin6_port)
656 return (-1);
657 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
658 sizeof(a6->sin6_addr)));
659 }
660 default:
661 return (-1);
662 }
663
664}
665
666#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
667/* When binding the listeners, check if a socket has been sent to us by the
668 * previous process that we could reuse, instead of creating a new one.
669 */
670static int tcp_find_compatible_fd(struct listener *l)
671{
672 struct xfer_sock_list *xfer_sock = xfer_sock_list;
673 int ret = -1;
674
675 while (xfer_sock) {
676 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
677 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
678 (l->interface != NULL && xfer_sock->iface != NULL &&
679 !strcmp(l->interface, xfer_sock->iface))) {
680 if ((l->options & LI_MANDATORY_FLAGS) ==
681 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
682 if ((xfer_sock->namespace == NULL &&
683 l->netns == NULL)
Willy Tarreaue5733232019-05-22 19:24:06 +0200684#ifdef USE_NS
Olivier Houchard153659f2017-04-05 22:39:56 +0200685 || (xfer_sock->namespace != NULL &&
686 l->netns != NULL &&
687 !strcmp(xfer_sock->namespace,
688 l->netns->node.key))
689#endif
690 ) {
691 break;
692 }
693
694 }
695 }
696 }
697 xfer_sock = xfer_sock->next;
698 }
699 if (xfer_sock != NULL) {
700 ret = xfer_sock->fd;
701 if (xfer_sock == xfer_sock_list)
702 xfer_sock_list = xfer_sock->next;
703 if (xfer_sock->prev)
704 xfer_sock->prev->next = xfer_sock->next;
705 if (xfer_sock->next)
706 xfer_sock->next->prev = xfer_sock->prev;
707 free(xfer_sock->iface);
708 free(xfer_sock->namespace);
709 free(xfer_sock);
710 }
711 return ret;
712}
713#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200714
Willy Tarreaue6b98942007-10-29 01:09:36 +0100715/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100716 * an error message in <errmsg> if the message is at most <errlen> bytes long
717 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
718 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100719 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
720 * was alright and that no message was returned. ERR_RETRYABLE means that an
721 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700722 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100723 * the meaning of the error, but just indicate that a message is present which
724 * should be displayed with the respective level. Last, ERR_ABORT indicates
725 * that it's pointless to try to start other listeners. No error message is
726 * returned if errlen is NULL.
727 */
728int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
729{
730 __label__ tcp_return, tcp_close_return;
731 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100732 int ext, ready;
733 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100734 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200735#ifdef TCP_MAXSEG
736
737 /* Create a temporary TCP socket to get default parameters we can't
738 * guess.
739 * */
740 ready_len = sizeof(default_tcp_maxseg);
741 if (default_tcp_maxseg == -1) {
742 default_tcp_maxseg = -2;
743 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
744 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100745 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200746 else {
747 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
748 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100749 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
William Dauchyc0e23ae2020-02-12 10:09:14 +0100750 close(fd);
Olivier Houchard153659f2017-04-05 22:39:56 +0200751 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200752 }
753 if (default_tcp6_maxseg == -1) {
754 default_tcp6_maxseg = -2;
755 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
756 if (fd >= 0) {
757 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
758 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100759 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200760 close(fd);
761 }
762 }
763#endif
764
Willy Tarreaue6b98942007-10-29 01:09:36 +0100765
766 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100767 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100768 *errmsg = 0;
769
770 if (listener->state != LI_ASSIGNED)
771 return ERR_NONE; /* already bound */
772
773 err = ERR_NONE;
774
Olivier Houchard153659f2017-04-05 22:39:56 +0200775 if (listener->fd == -1)
776 listener->fd = tcp_find_compatible_fd(listener);
777
Willy Tarreau40aa0702013-03-10 23:51:38 +0100778 /* if the listener already has an fd assigned, then we were offered the
779 * fd by an external process (most likely the parent), and we don't want
780 * to create a new socket. However we still want to set a few flags on
781 * the socket.
782 */
783 fd = listener->fd;
784 ext = (fd >= 0);
785
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100786 if (!ext) {
787 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
788
789 if (fd == -1) {
790 err |= ERR_RETRYABLE | ERR_ALERT;
791 msg = "cannot create listening socket";
792 goto tcp_return;
793 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100794 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100795
Willy Tarreaue6b98942007-10-29 01:09:36 +0100796 if (fd >= global.maxsock) {
797 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
798 msg = "not enough free sockets (raise '-n' parameter)";
799 goto tcp_close_return;
800 }
801
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200802 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100803 err |= ERR_FATAL | ERR_ALERT;
804 msg = "cannot make socket non-blocking";
805 goto tcp_close_return;
806 }
807
Willy Tarreau40aa0702013-03-10 23:51:38 +0100808 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100809 /* not fatal but should be reported */
810 msg = "cannot do so_reuseaddr";
811 err |= ERR_ALERT;
812 }
813
814 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900815 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200816 else {
817 struct linger tmplinger;
818 socklen_t len = sizeof(tmplinger);
819 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
820 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
821 tmplinger.l_onoff = 0;
822 tmplinger.l_linger = 0;
823 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
824 sizeof(tmplinger));
825 }
826 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100827
Willy Tarreaue6b98942007-10-29 01:09:36 +0100828#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000829 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
830 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100831 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000832 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100833 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100834#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200835
Willy Tarreau40aa0702013-03-10 23:51:38 +0100836 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200837 switch (listener->addr.ss_family) {
838 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200839 if (1
840#if defined(IP_TRANSPARENT)
841 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
842#endif
843#if defined(IP_FREEBIND)
844 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
845#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200846#if defined(IP_BINDANY)
847 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
848#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200849#if defined(SO_BINDANY)
850 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
851#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200852 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200853 msg = "cannot make listening socket transparent";
854 err |= ERR_ALERT;
855 }
856 break;
857 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200858 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200859#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200860 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
861#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100862#if defined(IP_FREEBIND)
863 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
864#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200865#if defined(IPV6_BINDANY)
866 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
867#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200868#if defined(SO_BINDANY)
869 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
870#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200871 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200872 msg = "cannot make listening socket transparent";
873 err |= ERR_ALERT;
874 }
875 break;
876 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100877 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200878
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100879#ifdef SO_BINDTODEVICE
880 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100881 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100882 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100883 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100884 msg = "cannot bind listener to device";
885 err |= ERR_WARN;
886 }
887 }
888#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400889#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100890 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400891 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200892 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
893 msg = "cannot set MSS";
894 err |= ERR_WARN;
895 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200896 } else if (ext) {
897 int tmpmaxseg = -1;
898 int defaultmss;
899 socklen_t len = sizeof(tmpmaxseg);
900
901 if (listener->addr.ss_family == AF_INET)
902 defaultmss = default_tcp_maxseg;
903 else
904 defaultmss = default_tcp6_maxseg;
905
906 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100907 if (defaultmss > 0 &&
908 tmpmaxseg != defaultmss &&
909 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200910 msg = "cannot set MSS";
911 err |= ERR_WARN;
912 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200913 }
914#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100915#if defined(TCP_USER_TIMEOUT)
916 if (listener->tcp_ut) {
917 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
918 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
919 msg = "cannot set TCP User Timeout";
920 err |= ERR_WARN;
921 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200922 } else
923 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
924 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100925#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200926#if defined(TCP_DEFER_ACCEPT)
927 if (listener->options & LI_O_DEF_ACCEPT) {
928 /* defer accept by up to one second */
929 int accept_delay = 1;
930 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
931 msg = "cannot enable DEFER_ACCEPT";
932 err |= ERR_WARN;
933 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200934 } else
935 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
936 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200937#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200938#if defined(TCP_FASTOPEN)
939 if (listener->options & LI_O_TCP_FO) {
940 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100941 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200942 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
943 msg = "cannot enable TCP_FASTOPEN";
944 err |= ERR_WARN;
945 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200946 } else {
947 socklen_t len;
948 int qlen;
949 len = sizeof(qlen);
950 /* Only disable fast open if it was enabled, we don't want
951 * the kernel to create a fast open queue if there's none.
952 */
953 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
954 qlen != 0) {
955 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
956 sizeof(zero)) == -1) {
957 msg = "cannot disable TCP_FASTOPEN";
958 err |= ERR_WARN;
959 }
960 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200961 }
962#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100963#if defined(IPV6_V6ONLY)
964 if (listener->options & LI_O_V6ONLY)
965 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100966 else if (listener->options & LI_O_V4V6)
967 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100968#endif
969
Willy Tarreau40aa0702013-03-10 23:51:38 +0100970 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100971 err |= ERR_RETRYABLE | ERR_ALERT;
972 msg = "cannot bind socket";
973 goto tcp_close_return;
974 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100975
Willy Tarreau40aa0702013-03-10 23:51:38 +0100976 ready = 0;
977 ready_len = sizeof(ready);
978 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
979 ready = 0;
980
981 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100982 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100983 err |= ERR_RETRYABLE | ERR_ALERT;
984 msg = "cannot listen to socket";
985 goto tcp_close_return;
986 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100987
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400988#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200989 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900990 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200991 else
992 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200993#endif
994
Willy Tarreaue6b98942007-10-29 01:09:36 +0100995 /* the socket is ready */
996 listener->fd = fd;
997 listener->state = LI_LISTEN;
998
Willy Tarreaua9786b62018-01-25 07:22:13 +0100999 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +01001000 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaueb472682010-05-28 18:46:57 +02001001
Willy Tarreaue6b98942007-10-29 01:09:36 +01001002 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001003 if (msg && errlen) {
1004 char pn[INET6_ADDRSTRLEN];
1005
Willy Tarreau631f01c2011-09-05 00:36:48 +02001006 addr_to_str(&listener->addr, pn, sizeof(pn));
1007 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001008 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001009 return err;
1010
1011 tcp_close_return:
1012 close(fd);
1013 goto tcp_return;
1014}
1015
1016/* This function creates all TCP sockets bound to the protocol entry <proto>.
1017 * It is intended to be used as the protocol's bind_all() function.
1018 * The sockets will be registered but not added to any fd_set, in order not to
1019 * loose them across the fork(). A call to enable_all_listeners() is needed
1020 * to complete initialization. The return value is composed from ERR_*.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001021 *
1022 * Must be called with proto_lock held.
1023 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001024 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001025static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001026{
1027 struct listener *listener;
1028 int err = ERR_NONE;
1029
1030 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001031 err |= tcp_bind_listener(listener, errmsg, errlen);
1032 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001033 break;
1034 }
1035
1036 return err;
1037}
1038
Willy Tarreau32282382017-09-15 07:44:44 +02001039/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1040 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1041 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001042 *
1043 * Must be called with proto_lock held.
1044 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001045 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001046static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001047{
1048 if (listener->state != LI_INIT)
1049 return;
1050 listener->state = LI_ASSIGNED;
1051 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001052 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001053 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1054 proto_tcpv4.nb_listeners++;
1055}
1056
Willy Tarreau32282382017-09-15 07:44:44 +02001057/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1058 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1059 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001060 *
1061 * Must be called with proto_lock held.
1062 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001063 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001064static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001065{
1066 if (listener->state != LI_INIT)
1067 return;
1068 listener->state = LI_ASSIGNED;
1069 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001070 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001071 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1072 proto_tcpv6.nb_listeners++;
1073}
1074
Willy Tarreau092d8652014-07-07 20:22:12 +02001075/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1076 * was totally stopped, or > 0 if correctly paused.
1077 */
1078int tcp_pause_listener(struct listener *l)
1079{
1080 if (shutdown(l->fd, SHUT_WR) != 0)
1081 return -1; /* Solaris dies here */
1082
Willy Tarreaue2711c72019-02-27 15:39:41 +01001083 if (listen(l->fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +02001084 return -1; /* OpenBSD dies here */
1085
1086 if (shutdown(l->fd, SHUT_RD) != 0)
1087 return -1; /* should always be OK */
1088 return 1;
1089}
1090
William Lallemand2e785f22016-05-25 01:48:42 +02001091/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001092 * Execute the "set-src" action. May be called from {tcp,http}request.
1093 * It only changes the address and tries to preserve the original port. If the
1094 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001095 */
1096enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1097 struct session *sess, struct stream *s, int flags)
1098{
1099 struct connection *cli_conn;
1100
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001101 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand2e785f22016-05-25 01:48:42 +02001102 struct sample *smp;
1103
1104 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1105 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001106 int port = get_net_port(cli_conn->src);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001107
William Lallemand2e785f22016-05-25 01:48:42 +02001108 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001109 ((struct sockaddr_in *)cli_conn->src)->sin_family = AF_INET;
1110 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1111 ((struct sockaddr_in *)cli_conn->src)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001112 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001113 ((struct sockaddr_in6 *)cli_conn->src)->sin6_family = AF_INET6;
1114 memcpy(&((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1115 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001116 }
1117 }
William Lallemand01252ed2016-05-25 02:33:16 +02001118 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001119 }
1120 return ACT_RET_CONT;
1121}
1122
William Lallemand44be6402016-05-25 01:51:35 +02001123/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001124 * Execute the "set-dst" action. May be called from {tcp,http}request.
1125 * It only changes the address and tries to preserve the original port. If the
1126 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001127 */
1128enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1129 struct session *sess, struct stream *s, int flags)
1130{
1131 struct connection *cli_conn;
1132
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001133 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001134 struct sample *smp;
1135
1136 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1137 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001138 int port = get_net_port(cli_conn->dst);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001139
William Lallemand13e9b0c2016-05-25 02:34:07 +02001140 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001141 ((struct sockaddr_in *)cli_conn->dst)->sin_family = AF_INET;
1142 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001143 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001144 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_family = AF_INET6;
1145 memcpy(&((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1146 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001147 }
1148 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1149 }
1150 }
1151 return ACT_RET_CONT;
1152}
1153
1154/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001155 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1156 * We must test the sin_family before setting the port. If the address family
1157 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1158 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001159 */
1160enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1161 struct session *sess, struct stream *s, int flags)
1162{
1163 struct connection *cli_conn;
1164
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001165 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand44be6402016-05-25 01:51:35 +02001166 struct sample *smp;
1167
William Lallemand44be6402016-05-25 01:51:35 +02001168 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1169 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001170 if (cli_conn->src->ss_family == AF_INET6) {
1171 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001172 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001173 if (cli_conn->src->ss_family != AF_INET) {
1174 cli_conn->src->ss_family = AF_INET;
1175 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001176 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001177 ((struct sockaddr_in *)cli_conn->src)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001178 }
1179 }
1180 }
1181 return ACT_RET_CONT;
1182}
1183
William Lallemand13e9b0c2016-05-25 02:34:07 +02001184/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001185 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1186 * We must test the sin_family before setting the port. If the address family
1187 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1188 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001189 */
1190enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1191 struct session *sess, struct stream *s, int flags)
1192{
1193 struct connection *cli_conn;
1194
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001195 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001196 struct sample *smp;
1197
William Lallemand13e9b0c2016-05-25 02:34:07 +02001198 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1199 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001200 if (cli_conn->dst->ss_family == AF_INET6) {
1201 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001202 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001203 if (cli_conn->dst->ss_family != AF_INET) {
1204 cli_conn->dst->ss_family = AF_INET;
1205 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001206 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001207 ((struct sockaddr_in *)cli_conn->dst)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001208 }
1209 }
1210 }
1211 return ACT_RET_CONT;
1212}
1213
Willy Tarreau2d392c22015-08-24 01:43:45 +02001214/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1215static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1216{
1217 struct connection *conn = objt_conn(sess->origin);
1218
1219 if (!conn)
1220 goto out;
1221
1222 if (!conn_ctrl_ready(conn))
1223 goto out;
1224
Willy Tarreau2d392c22015-08-24 01:43:45 +02001225#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001226 /* drain is needed only to send the quick ACK */
1227 conn_sock_drain(conn);
1228
Willy Tarreau2d392c22015-08-24 01:43:45 +02001229 /* re-enable quickack if it was disabled to ack all data and avoid
1230 * retransmits from the client that might trigger a real reset.
1231 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001232 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001233#endif
1234 /* lingering must absolutely be disabled so that we don't send a
1235 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1236 * is present, returning with ERR will cause lingering to be disabled.
1237 */
1238 if (strm)
1239 strm->si[0].flags |= SI_FL_NOLINGER;
1240
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001241 /* We're on the client-facing side, we must force to disable lingering to
1242 * ensure we will use an RST exclusively and kill any pending data.
1243 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001244 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001245
Willy Tarreau2d392c22015-08-24 01:43:45 +02001246#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001247 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001248 /* socket will be quiet now */
1249 goto out;
1250 }
1251#endif
1252 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1253 * Let's fall back on the TTL trick, though it only works for routed
1254 * network and has no effect on local net.
1255 */
1256#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001257 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001258#endif
1259 out:
1260 /* kill the stream if any */
1261 if (strm) {
1262 channel_abort(&strm->req);
1263 channel_abort(&strm->res);
Christopher Faulet177f4802020-03-06 15:10:46 +01001264 strm->req.analysers &= AN_REQ_FLT_END;
1265 strm->res.analysers &= AN_RES_FLT_END;
Christopher Fauletddc005a2020-03-06 15:23:18 +01001266 if (strm->flags & SF_BE_ASSIGNED)
1267 _HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001268 if (!(strm->flags & SF_ERR_MASK))
1269 strm->flags |= SF_ERR_PRXCOND;
1270 if (!(strm->flags & SF_FINST_MASK))
1271 strm->flags |= SF_FINST_R;
1272 }
1273
Olivier Houchard40514102019-03-08 18:54:04 +01001274 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001275 if (sess->listener->counters)
Olivier Houchard40514102019-03-08 18:54:04 +01001276 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001277
Christopher Fauletf573ba22020-03-06 15:15:49 +01001278 return ACT_RET_ABRT;
Willy Tarreau2d392c22015-08-24 01:43:45 +02001279}
1280
William Lallemand13e9b0c2016-05-25 02:34:07 +02001281/* parse "set-{src,dst}[-port]" action */
1282enum 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 +02001283{
1284 int cur_arg;
1285 struct sample_expr *expr;
1286 unsigned int where;
1287
1288 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001289 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 +02001290 if (!expr)
1291 return ACT_RET_PRS_ERR;
1292
1293 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001294 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001295 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001296 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001297 where |= SMP_VAL_BE_HRQ_HDR;
1298
1299 if (!(expr->fetch->val & where)) {
1300 memprintf(err,
1301 "fetch method '%s' extracts information from '%s', none of which is available here",
1302 args[cur_arg-1], sample_src_names(expr->fetch->use));
1303 free(expr);
1304 return ACT_RET_PRS_ERR;
1305 }
1306 rule->arg.expr = expr;
1307 rule->action = ACT_CUSTOM;
1308
1309 if (!strcmp(args[*orig_arg-1], "set-src")) {
1310 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001311 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1312 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001313 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1314 rule->action_ptr = tcp_action_req_set_dst;
1315 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1316 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001317 } else {
1318 return ACT_RET_PRS_ERR;
1319 }
1320
1321 (*orig_arg)++;
1322
1323 return ACT_RET_PRS_OK;
1324}
1325
1326
Willy Tarreau2d392c22015-08-24 01:43:45 +02001327/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1328 * success, ACT_RET_PRS_ERR on error.
1329 */
1330static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1331 struct act_rule *rule, char **err)
1332{
1333 rule->action = ACT_CUSTOM;
1334 rule->action_ptr = tcp_exec_action_silent_drop;
1335 return ACT_RET_PRS_OK;
1336}
1337
Willy Tarreau645513a2010-05-24 20:55:15 +02001338
1339/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001340/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001341/************************************************************************/
1342
Willy Tarreau4a129812012-04-25 17:31:42 +02001343/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001344int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001345{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001346 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001347
1348 if (!cli_conn)
1349 return 0;
1350
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001351 if (!conn_get_src(cli_conn))
1352 return 0;
1353
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001354 switch (cli_conn->src->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001355 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001356 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->src)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001357 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001358 break;
1359 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001360 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->src)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001361 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001362 break;
1363 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001364 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001365 }
Emeric Brunf769f512010-10-22 17:14:01 +02001366
Willy Tarreau37406352012-04-23 16:16:37 +02001367 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001368 return 1;
1369}
1370
Willy Tarreaua5e37562011-12-16 17:06:15 +01001371/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001372static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001373smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001374{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001375 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001376
1377 if (!cli_conn)
1378 return 0;
1379
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001380 if (!conn_get_src(cli_conn))
1381 return 0;
1382
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001383 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001384 if (!(smp->data.u.sint = get_host_port(cli_conn->src)))
Emeric Brunf769f512010-10-22 17:14:01 +02001385 return 0;
1386
Willy Tarreau37406352012-04-23 16:16:37 +02001387 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001388 return 1;
1389}
1390
Willy Tarreau4a129812012-04-25 17:31:42 +02001391/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001392static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001393smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001394{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001395 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001396
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001397 if (!cli_conn)
1398 return 0;
1399
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001400 if (!conn_get_dst(cli_conn))
1401 return 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001402
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001403 switch (cli_conn->dst->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001404 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001405 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001406 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001407 break;
1408 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001409 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001410 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001411 break;
1412 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001413 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001414 }
Emeric Brunf769f512010-10-22 17:14:01 +02001415
Willy Tarreau37406352012-04-23 16:16:37 +02001416 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001417 return 1;
1418}
1419
Willy Tarreau16e01562016-08-09 16:46:18 +02001420/* check if the destination address of the front connection is local to the
1421 * system or if it was intercepted.
1422 */
1423int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1424{
1425 struct connection *conn = objt_conn(smp->sess->origin);
1426 struct listener *li = smp->sess->listener;
1427
1428 if (!conn)
1429 return 0;
1430
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001431 if (!conn_get_dst(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001432 return 0;
1433
1434 smp->data.type = SMP_T_BOOL;
1435 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001436 smp->data.u.sint = addr_is_local(li->netns, conn->dst);
Willy Tarreau16e01562016-08-09 16:46:18 +02001437 return smp->data.u.sint >= 0;
1438}
1439
1440/* check if the source address of the front connection is local to the system
1441 * or not.
1442 */
1443int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1444{
1445 struct connection *conn = objt_conn(smp->sess->origin);
1446 struct listener *li = smp->sess->listener;
1447
1448 if (!conn)
1449 return 0;
1450
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001451 if (!conn_get_src(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001452 return 0;
1453
1454 smp->data.type = SMP_T_BOOL;
1455 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001456 smp->data.u.sint = addr_is_local(li->netns, conn->src);
Willy Tarreau16e01562016-08-09 16:46:18 +02001457 return smp->data.u.sint >= 0;
1458}
1459
Willy Tarreaua5e37562011-12-16 17:06:15 +01001460/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001461static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001462smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001463{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001464 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001465
1466 if (!cli_conn)
1467 return 0;
1468
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001469 if (!conn_get_dst(cli_conn))
1470 return 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001471
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001472 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001473 if (!(smp->data.u.sint = get_host_port(cli_conn->dst)))
Emeric Brunf769f512010-10-22 17:14:01 +02001474 return 0;
1475
Willy Tarreau37406352012-04-23 16:16:37 +02001476 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001477 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001478}
1479
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001480#ifdef TCP_INFO
1481
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001482
1483/* Validates the arguments passed to "fc_*" fetch keywords returning a time
1484 * value. These keywords support an optional string representing the unit of the
1485 * result: "us" for microseconds and "ms" for milliseconds". Returns 0 on error
1486 * and non-zero if OK.
1487 */
1488static int val_fc_time_value(struct arg *args, char **err)
1489{
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001490 if (args[0].type == ARGT_STR) {
1491 if (strcmp(args[0].data.str.area, "us") == 0) {
1492 free(args[0].data.str.area);
1493 args[0].type = ARGT_SINT;
1494 args[0].data.sint = TIME_UNIT_US;
1495 }
1496 else if (strcmp(args[0].data.str.area, "ms") == 0) {
1497 free(args[0].data.str.area);
1498 args[0].type = ARGT_SINT;
1499 args[0].data.sint = TIME_UNIT_MS;
1500 }
1501 else {
1502 memprintf(err, "expects 'us' or 'ms', got '%s'",
1503 args[0].data.str.area);
1504 return 0;
1505 }
1506 }
1507 else {
1508 memprintf(err, "Unexpected arg type");
1509 return 0;
1510 }
1511
1512 return 1;
1513}
1514
1515/* Validates the arguments passed to "fc_*" fetch keywords returning a
1516 * counter. These keywords should be used without any keyword, but because of a
1517 * bug in previous versions, an optional string argument may be passed. In such
1518 * case, the argument is ignored and a warning is emitted. Returns 0 on error
1519 * and non-zero if OK.
1520 */
1521static int var_fc_counter(struct arg *args, char **err)
1522{
1523 if (args[0].type != ARGT_STOP) {
1524 ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n");
1525 if (args[0].type == ARGT_STR)
1526 free(args[0].data.str.area);
1527 args[0].type = ARGT_STOP;
1528 }
1529
1530 return 1;
1531}
1532
Joseph Herlanta6331472018-11-25 12:59:12 -08001533/* Returns some tcp_info data if it's available. "dir" must be set to 0 if
1534 * the client connection is required, otherwise it is set to 1. "val" represents
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001535 * the required value.
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001536 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1537 */
1538static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1539 int dir, int val)
1540{
1541 struct connection *conn;
1542 struct tcp_info info;
1543 socklen_t optlen;
1544
1545 /* strm can be null. */
1546 if (!smp->strm)
1547 return 0;
1548
1549 /* get the object associated with the stream interface.The
1550 * object can be other thing than a connection. For example,
1551 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001552 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001553 if (!conn)
1554 return 0;
1555
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001556 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001557 syscal can fail. */
1558 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001559 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001560 return 0;
1561
1562 /* extract the value. */
1563 smp->data.type = SMP_T_SINT;
1564 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001565 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1566 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1567#if defined(__linux__)
1568 /* these ones are common to all Linux versions */
1569 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1570 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1571 case 4: smp->data.u.sint = info.tcpi_lost; break;
1572 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1573 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1574 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1575#elif defined(__FreeBSD__) || defined(__NetBSD__)
1576 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1577 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1578 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1579 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1580 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1581 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1582 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1583#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001584 default: return 0;
1585 }
1586
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001587 return 1;
1588}
1589
1590/* get the mean rtt of a client connexion */
1591static int
1592smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1593{
1594 if (!get_tcp_info(args, smp, 0, 0))
1595 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001596
1597 /* By default or if explicitly specified, convert rtt to ms */
1598 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1599 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1600
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001601 return 1;
1602}
1603
1604/* get the variance of the mean rtt of a client connexion */
1605static int
1606smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1607{
1608 if (!get_tcp_info(args, smp, 0, 1))
1609 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001610
1611 /* By default or if explicitly specified, convert rttvar to ms */
1612 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1613 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1614
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001615 return 1;
1616}
Joe Williams30fcd392016-08-10 07:06:44 -07001617
1618#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1619
1620/* get the unacked counter on a client connexion */
1621static int
1622smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1623{
1624 if (!get_tcp_info(args, smp, 0, 2))
1625 return 0;
1626 return 1;
1627}
1628
1629/* get the sacked counter on a client connexion */
1630static int
1631smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1632{
1633 if (!get_tcp_info(args, smp, 0, 3))
1634 return 0;
1635 return 1;
1636}
1637
1638/* get the lost counter on a client connexion */
1639static int
1640smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1641{
1642 if (!get_tcp_info(args, smp, 0, 4))
1643 return 0;
1644 return 1;
1645}
1646
1647/* get the retrans counter on a client connexion */
1648static int
1649smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1650{
1651 if (!get_tcp_info(args, smp, 0, 5))
1652 return 0;
1653 return 1;
1654}
1655
1656/* get the fackets counter on a client connexion */
1657static int
1658smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1659{
1660 if (!get_tcp_info(args, smp, 0, 6))
1661 return 0;
1662 return 1;
1663}
1664
1665/* get the reordering counter on a client connexion */
1666static int
1667smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1668{
1669 if (!get_tcp_info(args, smp, 0, 7))
1670 return 0;
1671 return 1;
1672}
1673#endif // linux || freebsd || netbsd
1674#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001675
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001676#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001677/* parse the "v4v6" bind keyword */
1678static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1679{
1680 struct listener *l;
1681
1682 list_for_each_entry(l, &conf->listeners, by_bind) {
1683 if (l->addr.ss_family == AF_INET6)
1684 l->options |= LI_O_V4V6;
1685 }
1686
1687 return 0;
1688}
1689
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001690/* parse the "v6only" bind keyword */
1691static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1692{
1693 struct listener *l;
1694
1695 list_for_each_entry(l, &conf->listeners, by_bind) {
1696 if (l->addr.ss_family == AF_INET6)
1697 l->options |= LI_O_V6ONLY;
1698 }
1699
1700 return 0;
1701}
1702#endif
1703
Pieter Baauwd551fb52013-05-08 22:49:23 +02001704#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001705/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001706static 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 +02001707{
1708 struct listener *l;
1709
Willy Tarreau4348fad2012-09-20 16:48:07 +02001710 list_for_each_entry(l, &conf->listeners, by_bind) {
1711 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1712 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001713 }
1714
Willy Tarreau44791242012-09-12 23:27:21 +02001715 return 0;
1716}
1717#endif
1718
1719#ifdef TCP_DEFER_ACCEPT
1720/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001721static 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 +02001722{
1723 struct listener *l;
1724
Willy Tarreau4348fad2012-09-20 16:48:07 +02001725 list_for_each_entry(l, &conf->listeners, by_bind) {
1726 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1727 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001728 }
1729
Willy Tarreau44791242012-09-12 23:27:21 +02001730 return 0;
1731}
1732#endif
1733
Willy Tarreau1c862c52012-10-05 16:21:00 +02001734#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001735/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001736static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1737{
1738 struct listener *l;
1739
1740 list_for_each_entry(l, &conf->listeners, by_bind) {
1741 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1742 l->options |= LI_O_TCP_FO;
1743 }
1744
1745 return 0;
1746}
1747#endif
1748
Willy Tarreau44791242012-09-12 23:27:21 +02001749#ifdef TCP_MAXSEG
1750/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001751static 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 +02001752{
1753 struct listener *l;
1754 int mss;
1755
Willy Tarreau44791242012-09-12 23:27:21 +02001756 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001757 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001758 return ERR_ALERT | ERR_FATAL;
1759 }
1760
1761 mss = atoi(args[cur_arg + 1]);
1762 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001763 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001764 return ERR_ALERT | ERR_FATAL;
1765 }
1766
Willy Tarreau4348fad2012-09-20 16:48:07 +02001767 list_for_each_entry(l, &conf->listeners, by_bind) {
1768 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1769 l->maxseg = mss;
1770 }
Willy Tarreau44791242012-09-12 23:27:21 +02001771
1772 return 0;
1773}
1774#endif
1775
Willy Tarreau2af207a2015-02-04 00:45:58 +01001776#ifdef TCP_USER_TIMEOUT
1777/* parse the "tcp-ut" bind keyword */
1778static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1779{
1780 const char *ptr = NULL;
1781 struct listener *l;
1782 unsigned int timeout;
1783
1784 if (!*args[cur_arg + 1]) {
1785 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1786 return ERR_ALERT | ERR_FATAL;
1787 }
1788
1789 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001790 if (ptr == PARSE_TIME_OVER) {
1791 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1792 args[cur_arg+1], args[cur_arg]);
1793 return ERR_ALERT | ERR_FATAL;
1794 }
1795 else if (ptr == PARSE_TIME_UNDER) {
1796 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1797 args[cur_arg+1], args[cur_arg]);
1798 return ERR_ALERT | ERR_FATAL;
1799 }
1800 else if (ptr) {
Willy Tarreau2af207a2015-02-04 00:45:58 +01001801 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1802 return ERR_ALERT | ERR_FATAL;
1803 }
1804
1805 list_for_each_entry(l, &conf->listeners, by_bind) {
1806 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1807 l->tcp_ut = timeout;
1808 }
1809
1810 return 0;
1811}
1812#endif
1813
Willy Tarreau44791242012-09-12 23:27:21 +02001814#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001815/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001816static 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 +02001817{
1818 struct listener *l;
1819
Willy Tarreau44791242012-09-12 23:27:21 +02001820 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001821 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001822 return ERR_ALERT | ERR_FATAL;
1823 }
1824
Willy Tarreau4348fad2012-09-20 16:48:07 +02001825 list_for_each_entry(l, &conf->listeners, by_bind) {
1826 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1827 l->interface = strdup(args[cur_arg + 1]);
1828 }
Willy Tarreau44791242012-09-12 23:27:21 +02001829
Willy Tarreau44791242012-09-12 23:27:21 +02001830 return 0;
1831}
1832#endif
1833
Willy Tarreaue5733232019-05-22 19:24:06 +02001834#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001835/* parse the "namespace" bind keyword */
1836static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1837{
1838 struct listener *l;
1839 char *namespace = NULL;
1840
1841 if (!*args[cur_arg + 1]) {
1842 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1843 return ERR_ALERT | ERR_FATAL;
1844 }
1845 namespace = args[cur_arg + 1];
1846
1847 list_for_each_entry(l, &conf->listeners, by_bind) {
1848 l->netns = netns_store_lookup(namespace, strlen(namespace));
1849
1850 if (l->netns == NULL)
1851 l->netns = netns_store_insert(namespace);
1852
1853 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001854 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001855 return ERR_ALERT | ERR_FATAL;
1856 }
1857 }
1858 return 0;
1859}
1860#endif
1861
Willy Tarreau163d4622015-10-13 16:16:41 +02001862#ifdef TCP_USER_TIMEOUT
1863/* parse the "tcp-ut" server keyword */
1864static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1865{
1866 const char *ptr = NULL;
1867 unsigned int timeout;
1868
1869 if (!*args[*cur_arg + 1]) {
1870 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1871 return ERR_ALERT | ERR_FATAL;
1872 }
1873
1874 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001875 if (ptr == PARSE_TIME_OVER) {
1876 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1877 args[*cur_arg+1], args[*cur_arg]);
1878 return ERR_ALERT | ERR_FATAL;
1879 }
1880 else if (ptr == PARSE_TIME_UNDER) {
1881 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1882 args[*cur_arg+1], args[*cur_arg]);
1883 return ERR_ALERT | ERR_FATAL;
1884 }
1885 else if (ptr) {
Willy Tarreau163d4622015-10-13 16:16:41 +02001886 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1887 return ERR_ALERT | ERR_FATAL;
1888 }
1889
1890 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1891 newsrv->tcp_ut = timeout;
1892
1893 return 0;
1894}
1895#endif
1896
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001897
Willy Tarreau4a129812012-04-25 17:31:42 +02001898/* Note: must not be declared <const> as its list will be overwritten.
1899 * Note: fetches that may return multiple types must be declared as the lowest
1900 * common denominator, the type that can be casted into all other ones. For
1901 * instance v4/v6 must be declared v4.
1902 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001903static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001904 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001905 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001906 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001907 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001908 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001909 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001910#ifdef TCP_INFO
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001911 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
1912 { "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 -07001913#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001914 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1915 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1916 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1917 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1918 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1919 { "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 -07001920#endif // linux || freebsd || netbsd
1921#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001922 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001923}};
1924
Willy Tarreau0108d902018-11-25 19:14:37 +01001925INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1926
Willy Tarreau44791242012-09-12 23:27:21 +02001927/************************************************************************/
1928/* All supported bind keywords must be declared here. */
1929/************************************************************************/
1930
1931/* Note: must not be declared <const> as its list will be overwritten.
1932 * Please take care of keeping this list alphabetically sorted, doing so helps
1933 * all code contributors.
1934 * Optional keywords are also declared with a NULL ->parse() function so that
1935 * the config parser can report an appropriate error when a known keyword was
1936 * not enabled.
1937 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001938static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001939#ifdef TCP_DEFER_ACCEPT
1940 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1941#endif
1942#ifdef SO_BINDTODEVICE
1943 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1944#endif
1945#ifdef TCP_MAXSEG
1946 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1947#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001948#ifdef TCP_USER_TIMEOUT
1949 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1950#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001951#ifdef TCP_FASTOPEN
1952 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1953#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001954#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001955 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1956#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001957#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001958 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001959 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1960#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001961#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001962 { "namespace", bind_parse_namespace, 1 },
1963#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001964 /* the versions with the NULL parse function*/
1965 { "defer-accept", NULL, 0 },
1966 { "interface", NULL, 1 },
1967 { "mss", NULL, 1 },
1968 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001969 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001970 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001971 { NULL, NULL, 0 },
1972}};
1973
Willy Tarreau0108d902018-11-25 19:14:37 +01001974INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1975
Willy Tarreau163d4622015-10-13 16:16:41 +02001976static struct srv_kw_list srv_kws = { "TCP", { }, {
1977#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001978 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02001979#endif
1980 { NULL, NULL, 0 },
1981}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02001982
Willy Tarreau0108d902018-11-25 19:14:37 +01001983INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
1984
Willy Tarreau2d392c22015-08-24 01:43:45 +02001985static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001986 { "set-src", tcp_parse_set_src_dst },
1987 { "set-src-port", tcp_parse_set_src_dst },
1988 { "set-dst" , tcp_parse_set_src_dst },
1989 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02001990 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02001991 { /* END */ }
1992}};
1993
Willy Tarreau0108d902018-11-25 19:14:37 +01001994INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions);
1995
Willy Tarreau620408f2016-10-21 16:37:51 +02001996static struct action_kw_list tcp_req_sess_actions = {ILH, {
Willy Tarreau620408f2016-10-21 16:37:51 +02001997 { "set-src", tcp_parse_set_src_dst },
1998 { "set-src-port", tcp_parse_set_src_dst },
1999 { "set-dst" , tcp_parse_set_src_dst },
2000 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002001 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau620408f2016-10-21 16:37:51 +02002002 { /* END */ }
2003}};
2004
Willy Tarreau0108d902018-11-25 19:14:37 +01002005INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions);
2006
Willy Tarreau2d392c22015-08-24 01:43:45 +02002007static struct action_kw_list tcp_req_cont_actions = {ILH, {
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002008 { "set-dst" , tcp_parse_set_src_dst },
2009 { "set-dst-port", tcp_parse_set_src_dst },
2010 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002011 { /* END */ }
2012}};
2013
Willy Tarreau0108d902018-11-25 19:14:37 +01002014INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2015
Willy Tarreau2d392c22015-08-24 01:43:45 +02002016static struct action_kw_list tcp_res_cont_actions = {ILH, {
2017 { "silent-drop", tcp_parse_silent_drop },
2018 { /* END */ }
2019}};
2020
Willy Tarreau0108d902018-11-25 19:14:37 +01002021INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
2022
Willy Tarreau2d392c22015-08-24 01:43:45 +02002023static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002024 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002025 { "set-src", tcp_parse_set_src_dst },
2026 { "set-src-port", tcp_parse_set_src_dst },
2027 { "set-dst", tcp_parse_set_src_dst },
2028 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002029 { /* END */ }
2030}};
2031
Willy Tarreau0108d902018-11-25 19:14:37 +01002032INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2033
Willy Tarreau2d392c22015-08-24 01:43:45 +02002034static struct action_kw_list http_res_actions = {ILH, {
2035 { "silent-drop", tcp_parse_silent_drop },
2036 { /* END */ }
2037}};
2038
Willy Tarreau0108d902018-11-25 19:14:37 +01002039INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002040
Willy Tarreau80713382018-11-26 10:19:54 +01002041REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
Willy Tarreauba962912016-12-21 18:55:02 +01002042#if defined(IP_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002043 " IP_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002044#endif
2045#if defined(IPV6_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002046 " IPV6_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002047#endif
2048#if defined(IP_FREEBIND)
Willy Tarreau80713382018-11-26 10:19:54 +01002049 " IP_FREEBIND"
Willy Tarreauba962912016-12-21 18:55:02 +01002050#endif
2051#if defined(IP_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002052 " IP_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002053#endif
2054#if defined(IPV6_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002055 " IPV6_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002056#endif
2057#if defined(SO_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002058 " SO_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002059#endif
Willy Tarreau80713382018-11-26 10:19:54 +01002060 "");
Willy Tarreaue6b98942007-10-29 01:09:36 +01002061
2062
2063/*
2064 * Local variables:
2065 * c-indent-level: 8
2066 * c-basic-offset: 8
2067 * End:
2068 */