blob: 4ca4100096ba414725a7cc96e38487f55eedb0b0 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Baptiste Assmann39a5f222016-08-08 14:12:08 +020013/* this is to have tcp_info defined on systems using musl
14 * library, such as Alpine Linux
15 */
16#define _GNU_SOURCE
17
Willy Tarreaue6b98942007-10-29 01:09:36 +010018#include <ctype.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <time.h>
25
26#include <sys/param.h>
27#include <sys/socket.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010028#include <sys/types.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010029
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040030#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020031#include <netinet/in.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040032
Willy Tarreau122eba92020-06-04 10:15:32 +020033#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020034#include <haproxy/api.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020035#include <haproxy/channel.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020036#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020037#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020038#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020039#include <haproxy/http_rules.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020040#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020041#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020042#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020043#include <haproxy/proxy-t.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020044#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020045#include <haproxy/server.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020046#include <haproxy/tools.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020047#include <haproxy/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010048
Willy Tarreau39713102016-11-25 15:49:32 +010049#include <types/stream.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010050
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020051#include <haproxy/arg.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020052#include <haproxy/fd.h>
Willy Tarreaufc8f6a82020-06-03 19:20:59 +020053#include <haproxy/port_range.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +020054#include <haproxy/protocol.h>
Willy Tarreaufc774542020-06-04 17:31:04 +020055#include <haproxy/proto_tcp.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020056#include <haproxy/tcp_rules.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010057
Emeric Bruncf20bf12010-10-22 16:06:11 +020058static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
59static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020060static void tcpv4_add_listener(struct listener *listener, int port);
61static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010062
63/* Note: must not be declared <const> as its list will be overwritten */
64static struct protocol proto_tcpv4 = {
65 .name = "tcpv4",
66 .sock_domain = AF_INET,
67 .sock_type = SOCK_STREAM,
68 .sock_prot = IPPROTO_TCP,
69 .sock_family = AF_INET,
70 .sock_addrlen = sizeof(struct sockaddr_in),
71 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020072 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020073 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020074 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010075 .bind_all = tcp_bind_listeners,
76 .unbind_all = unbind_all_listeners,
77 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020078 .get_src = tcp_get_src,
79 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020080 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020081 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010082 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
83 .nb_listeners = 0,
84};
85
Willy Tarreau0108d902018-11-25 19:14:37 +010086INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
87
Willy Tarreaue6b98942007-10-29 01:09:36 +010088/* Note: must not be declared <const> as its list will be overwritten */
89static struct protocol proto_tcpv6 = {
90 .name = "tcpv6",
91 .sock_domain = AF_INET6,
92 .sock_type = SOCK_STREAM,
93 .sock_prot = IPPROTO_TCP,
94 .sock_family = AF_INET6,
95 .sock_addrlen = sizeof(struct sockaddr_in6),
96 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020097 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020098 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020099 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100100 .bind_all = tcp_bind_listeners,
101 .unbind_all = unbind_all_listeners,
102 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200103 .get_src = tcp_get_src,
104 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +0200105 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200106 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100107 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
108 .nb_listeners = 0,
109};
110
Willy Tarreau0108d902018-11-25 19:14:37 +0100111INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
112
Olivier Houchard153659f2017-04-05 22:39:56 +0200113/* Default TCP parameters, got by opening a temporary TCP socket. */
114#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100115static THREAD_LOCAL int default_tcp_maxseg = -1;
116static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200117#endif
118
David du Colombier6f5ccb12011-03-10 22:26:24 +0100119/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100120 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
121 * - 0 : ignore remote address (may even be a NULL pointer)
122 * - 1 : use provided address
123 * - 2 : use provided port
124 * - 3 : use both
125 *
126 * The function supports multiple foreign binding methods :
127 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100128 * The second one can be used as a fallback for the first one.
129 * This function returns 0 when everything's OK, 1 if it could not bind, to the
130 * local address, 2 if it could not bind to the foreign address.
131 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100132int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100133{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100134 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100135 int foreign_ok = 0;
136 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100137 static THREAD_LOCAL int ip_transp_working = 1;
138 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200139
David du Colombier65c17962012-07-13 14:34:59 +0200140 switch (local->ss_family) {
141 case AF_INET:
142 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200143 /* This deserves some explanation. Some platforms will support
144 * multiple combinations of certain methods, so we try the
145 * supported ones until one succeeds.
146 */
147 if (0
148#if defined(IP_TRANSPARENT)
149 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
150#endif
151#if defined(IP_FREEBIND)
152 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
153#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200154#if defined(IP_BINDANY)
155 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
156#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200157#if defined(SO_BINDANY)
158 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
159#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200160 )
David du Colombier65c17962012-07-13 14:34:59 +0200161 foreign_ok = 1;
162 else
163 ip_transp_working = 0;
164 }
165 break;
166 case AF_INET6:
167 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200168 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200169#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200170 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
171#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100172#if defined(IP_FREEBIND)
173 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
174#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200175#if defined(IPV6_BINDANY)
176 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
177#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200178#if defined(SO_BINDANY)
179 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
180#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200181 )
David du Colombier65c17962012-07-13 14:34:59 +0200182 foreign_ok = 1;
183 else
184 ip6_transp_working = 0;
185 }
186 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100187 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200188
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100189 if (flags) {
190 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200191 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100192 switch (remote->ss_family) {
193 case AF_INET:
194 if (flags & 1)
195 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
196 if (flags & 2)
197 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
198 break;
199 case AF_INET6:
200 if (flags & 1)
201 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
202 if (flags & 2)
203 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
204 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100205 default:
206 /* we don't want to try to bind to an unknown address family */
207 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100208 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100209 }
210
Simon Hormande072bd2011-06-24 15:11:37 +0900211 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100212 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200213 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200214 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
215 if (ret < 0)
216 return 2;
217 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100218 }
219 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200220 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200221 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
222 if (ret < 0)
223 return 1;
224 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100225 }
226
227 if (!flags)
228 return 0;
229
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100230 if (!foreign_ok)
231 /* we could not bind to a foreign address */
232 return 2;
233
234 return 0;
235}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100236
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200237/* conn->dst MUST be valid */
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100238static int create_server_socket(struct connection *conn)
239{
Willy Tarreau529c1392014-12-24 13:47:55 +0100240 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100241
Willy Tarreaue5733232019-05-22 19:24:06 +0200242#ifdef USE_NS
Willy Tarreau529c1392014-12-24 13:47:55 +0100243 if (objt_server(conn->target)) {
244 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
245 ns = conn->proxy_netns;
246 else
247 ns = __objt_server(conn->target)->netns;
248 }
249#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200250 return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, IPPROTO_TCP);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100251}
Willy Tarreau9650f372009-08-16 14:02:45 +0200252
253/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200254 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200255 * to connection <conn> using (si->{target,dst}). A source address may be
256 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200257 * bind addresses are still determined locally (due to the possible need of a
258 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100259 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100260 * supported. The <data> parameter is a boolean indicating whether there are data
261 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200262 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
263 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100264 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200265 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
266 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200267 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200268 * Note that a pending send_proxy message accounts for data.
269 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200270 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200271 * - SF_ERR_NONE if everything's OK
272 * - SF_ERR_SRVTO if there are no more servers
273 * - SF_ERR_SRVCL if the connection was refused by the server
274 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
275 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
276 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100277 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100278 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200279 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100280 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200281 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100282
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200283int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200284{
285 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100286 struct server *srv;
287 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100288 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100289 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800290 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100291
Olivier Houchard637b6952018-11-23 14:23:07 +0100292 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100293
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100294 switch (obj_type(conn->target)) {
295 case OBJ_TYPE_PROXY:
296 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100297 srv = NULL;
298 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100299 case OBJ_TYPE_SERVER:
300 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100301 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100302 /* Make sure we check that we have data before activating
303 * TFO, or we could trigger a kernel issue whereby after
304 * a successful connect() == 0, any subsequent connect()
305 * will return EINPROGRESS instead of EISCONN.
306 */
307 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
308 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
309 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100310 break;
311 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100312 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200313 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100314 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200315
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200316 if (!conn->dst) {
317 conn->flags |= CO_FL_ERROR;
318 return SF_ERR_INTERNAL;
319 }
320
Willy Tarreau585744b2017-08-24 14:31:19 +0200321 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100322
323 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200324 qfprintf(stderr, "Cannot get a server socket.\n");
325
Willy Tarreau9ce70132014-01-24 16:08:19 +0100326 if (errno == ENFILE) {
327 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200328 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100329 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
330 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100331 }
332 else if (errno == EMFILE) {
333 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200334 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100335 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
336 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100337 }
338 else if (errno == ENOBUFS || errno == ENOMEM) {
339 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200340 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100341 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
342 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100343 }
344 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
345 conn->err_code = CO_ER_NOPROTO;
346 }
347 else
348 conn->err_code = CO_ER_SOCK_ERR;
349
Willy Tarreau9650f372009-08-16 14:02:45 +0200350 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100351 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200352 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200353 }
354
355 if (fd >= global.maxsock) {
356 /* do not log anything there, it's a normal condition when this option
357 * is used to serialize connections to a server !
358 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100359 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200360 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100361 conn->err_code = CO_ER_CONF_FDLIM;
362 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200363 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200364 }
365
366 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900367 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200368 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
369 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100370 conn->err_code = CO_ER_SOCK_ERR;
371 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200372 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200373 }
374
William Lallemandc03eb012018-11-27 12:02:37 +0100375 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
376 ha_alert("Cannot set CLOEXEC on client socket.\n");
377 close(fd);
378 conn->err_code = CO_ER_SOCK_ERR;
379 conn->flags |= CO_FL_ERROR;
380 return SF_ERR_INTERNAL;
381 }
382
Willy Tarreau9650f372009-08-16 14:02:45 +0200383 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900384 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200385
Willy Tarreau9650f372009-08-16 14:02:45 +0200386 /* allow specific binding :
387 * - server-specific at first
388 * - proxy-specific next
389 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100390 if (srv && srv->conn_src.opts & CO_SRC_BIND)
391 src = &srv->conn_src;
392 else if (be->conn_src.opts & CO_SRC_BIND)
393 src = &be->conn_src;
394 else
395 src = NULL;
396
397 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200398 int ret, flags = 0;
399
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200400 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100401 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100402 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200403 conn->flags |= CO_FL_PRIVATE;
404 /* fall through */
405 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200406 flags = 3;
407 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100408 case CO_SRC_TPROXY_CIP:
409 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200410 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200411 flags = 1;
412 break;
413 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200414 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200415
Willy Tarreau9650f372009-08-16 14:02:45 +0200416#ifdef SO_BINDTODEVICE
417 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100418 if (src->iface_name)
419 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200420#endif
421
Willy Tarreaua4380b42012-12-08 22:49:11 +0100422 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200423 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100424 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200425
426 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200427 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200428
429 do {
430 /* note: in case of retry, we may have to release a previously
431 * allocated port, hence this loop's construct.
432 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200433 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
434 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200435
436 if (!attempts)
437 break;
438 attempts--;
439
Willy Tarreaua4380b42012-12-08 22:49:11 +0100440 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100441 if (!fdinfo[fd].local_port) {
442 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200443 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100444 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200445
Willy Tarreaua4380b42012-12-08 22:49:11 +0100446 fdinfo[fd].port_range = src->sport_range;
447 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200448
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200449 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100450 if (ret != 0)
451 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 } while (ret != 0); /* binding NOK */
453 }
454 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000455#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100456 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000457 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
458#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200459 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100460 if (ret != 0)
461 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200462 }
463
Willy Tarreaua4380b42012-12-08 22:49:11 +0100464 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200465 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
466 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200467 close(fd);
468
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100470 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
471 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200472 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100473 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200474 be->id);
475 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100476 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
477 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200478 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100479 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200480 be->id);
481 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100482 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200483 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200484 }
485 }
486
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400487#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200488 /* disabling tcp quick ack now allows the first request to leave the
489 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100490 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200491 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200492 if (flags & (CONNECT_DELACK_ALWAYS) ||
493 ((flags & CONNECT_DELACK_SMART_CONNECT ||
494 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
495 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900496 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200497#endif
498
Willy Tarreau163d4622015-10-13 16:16:41 +0200499#ifdef TCP_USER_TIMEOUT
500 /* there is not much more we can do here when it fails, it's still minor */
501 if (srv && srv->tcp_ut)
502 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
503#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100504
505 if (use_fastopen) {
506#if defined(TCP_FASTOPEN_CONNECT)
507 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
508#endif
509 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100510 if (global.tune.server_sndbuf)
511 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
512
513 if (global.tune.server_rcvbuf)
514 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
515
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200516 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800517 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100518 if (errno == EINPROGRESS || errno == EALREADY) {
519 /* common case, let's wait for connect status */
520 conn->flags |= CO_FL_WAIT_L4_CONN;
521 }
522 else if (errno == EISCONN) {
523 /* should normally not happen but if so, indicates that it's OK */
524 conn->flags &= ~CO_FL_WAIT_L4_CONN;
525 }
526 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200527 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100528 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200529 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100530 conn->err_code = CO_ER_FREE_PORTS;
531 }
532 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200533 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100534 conn->err_code = CO_ER_ADDR_INUSE;
535 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200536
Willy Tarreaub1719512012-12-08 23:03:28 +0100537 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200538 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
539 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200540 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100541 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100542 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200543 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200544 } else if (errno == ETIMEDOUT) {
545 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200546 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
547 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200548 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100549 conn->err_code = CO_ER_SOCK_ERR;
550 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200551 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200552 } else {
553 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
554 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200555 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
556 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200557 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100558 conn->err_code = CO_ER_SOCK_ERR;
559 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200560 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200561 }
562 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100563 else {
564 /* connect() == 0, this is great! */
565 conn->flags &= ~CO_FL_WAIT_L4_CONN;
566 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200567
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100568 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200569
Willy Tarreauf79c8172013-10-21 16:30:56 +0200570 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100571 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200572
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100573 if (conn->flags & CO_FL_WAIT_L4_CONN) {
574 fd_want_send(fd);
575 fd_cant_send(fd);
576 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200577
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200578 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200579 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100580 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200581 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200582 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200583
Willy Tarreaue7dff022015-04-03 01:14:29 +0200584 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200585}
586
587
Willy Tarreau59b94792012-05-11 16:16:40 +0200588/*
589 * Retrieves the source address for the socket <fd>, with <dir> indicating
590 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
591 * success, -1 in case of error. The socket's source address is stored in
592 * <sa> for <salen> bytes.
593 */
594int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
595{
596 if (dir)
597 return getsockname(fd, sa, &salen);
598 else
599 return getpeername(fd, sa, &salen);
600}
601
602
603/*
604 * Retrieves the original destination address for the socket <fd>, with <dir>
605 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
606 * listener, if the original destination address was translated, the original
607 * address is retrieved. It returns 0 in case of success, -1 in case of error.
608 * The socket's source address is stored in <sa> for <salen> bytes.
609 */
610int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
611{
612 if (dir)
613 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100614 else {
615 int ret = getsockname(fd, sa, &salen);
616
617 if (ret < 0)
618 return ret;
619
Willy Tarreaue5733232019-05-22 19:24:06 +0200620#if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100621 /* For TPROXY and Netfilter's NAT, we can retrieve the original
622 * IPv4 address before DNAT/REDIRECT. We must not do that with
623 * other families because v6-mapped IPv4 addresses are still
624 * reported as v4.
625 */
626 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
627 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
628 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200629#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100630 return ret;
631 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200632}
633
Olivier Houchard153659f2017-04-05 22:39:56 +0200634/* XXX: Should probably be elsewhere */
635static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
636{
637 if (a->ss_family != b->ss_family) {
638 return (-1);
639 }
640 switch (a->ss_family) {
641 case AF_INET:
642 {
643 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
644 if (a4->sin_port != b4->sin_port)
645 return (-1);
646 return (memcmp(&a4->sin_addr, &b4->sin_addr,
647 sizeof(a4->sin_addr)));
648 }
649 case AF_INET6:
650 {
651 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
652 if (a6->sin6_port != b6->sin6_port)
653 return (-1);
654 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
655 sizeof(a6->sin6_addr)));
656 }
657 default:
658 return (-1);
659 }
660
661}
662
663#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
664/* When binding the listeners, check if a socket has been sent to us by the
665 * previous process that we could reuse, instead of creating a new one.
666 */
667static int tcp_find_compatible_fd(struct listener *l)
668{
669 struct xfer_sock_list *xfer_sock = xfer_sock_list;
670 int ret = -1;
671
672 while (xfer_sock) {
673 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
674 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
675 (l->interface != NULL && xfer_sock->iface != NULL &&
676 !strcmp(l->interface, xfer_sock->iface))) {
677 if ((l->options & LI_MANDATORY_FLAGS) ==
678 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
679 if ((xfer_sock->namespace == NULL &&
680 l->netns == NULL)
Willy Tarreaue5733232019-05-22 19:24:06 +0200681#ifdef USE_NS
Olivier Houchard153659f2017-04-05 22:39:56 +0200682 || (xfer_sock->namespace != NULL &&
683 l->netns != NULL &&
684 !strcmp(xfer_sock->namespace,
685 l->netns->node.key))
686#endif
687 ) {
688 break;
689 }
690
691 }
692 }
693 }
694 xfer_sock = xfer_sock->next;
695 }
696 if (xfer_sock != NULL) {
697 ret = xfer_sock->fd;
698 if (xfer_sock == xfer_sock_list)
699 xfer_sock_list = xfer_sock->next;
700 if (xfer_sock->prev)
701 xfer_sock->prev->next = xfer_sock->next;
702 if (xfer_sock->next)
703 xfer_sock->next->prev = xfer_sock->prev;
704 free(xfer_sock->iface);
705 free(xfer_sock->namespace);
706 free(xfer_sock);
707 }
708 return ret;
709}
710#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200711
Willy Tarreaue6b98942007-10-29 01:09:36 +0100712/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100713 * an error message in <errmsg> if the message is at most <errlen> bytes long
714 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
715 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100716 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
717 * was alright and that no message was returned. ERR_RETRYABLE means that an
718 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700719 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100720 * the meaning of the error, but just indicate that a message is present which
721 * should be displayed with the respective level. Last, ERR_ABORT indicates
722 * that it's pointless to try to start other listeners. No error message is
723 * returned if errlen is NULL.
724 */
725int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
726{
727 __label__ tcp_return, tcp_close_return;
728 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100729 int ext, ready;
730 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100731 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200732#ifdef TCP_MAXSEG
733
734 /* Create a temporary TCP socket to get default parameters we can't
735 * guess.
736 * */
737 ready_len = sizeof(default_tcp_maxseg);
738 if (default_tcp_maxseg == -1) {
739 default_tcp_maxseg = -2;
740 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
741 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100742 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200743 else {
744 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
745 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100746 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
William Dauchyc0e23ae2020-02-12 10:09:14 +0100747 close(fd);
Olivier Houchard153659f2017-04-05 22:39:56 +0200748 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200749 }
750 if (default_tcp6_maxseg == -1) {
751 default_tcp6_maxseg = -2;
752 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
753 if (fd >= 0) {
754 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
755 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100756 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200757 close(fd);
758 }
759 }
760#endif
761
Willy Tarreaue6b98942007-10-29 01:09:36 +0100762
763 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100764 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100765 *errmsg = 0;
766
767 if (listener->state != LI_ASSIGNED)
768 return ERR_NONE; /* already bound */
769
770 err = ERR_NONE;
771
Olivier Houchard153659f2017-04-05 22:39:56 +0200772 if (listener->fd == -1)
773 listener->fd = tcp_find_compatible_fd(listener);
774
Willy Tarreau40aa0702013-03-10 23:51:38 +0100775 /* if the listener already has an fd assigned, then we were offered the
776 * fd by an external process (most likely the parent), and we don't want
777 * to create a new socket. However we still want to set a few flags on
778 * the socket.
779 */
780 fd = listener->fd;
781 ext = (fd >= 0);
782
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100783 if (!ext) {
784 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
785
786 if (fd == -1) {
787 err |= ERR_RETRYABLE | ERR_ALERT;
788 msg = "cannot create listening socket";
789 goto tcp_return;
790 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100791 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100792
Willy Tarreaue6b98942007-10-29 01:09:36 +0100793 if (fd >= global.maxsock) {
794 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
795 msg = "not enough free sockets (raise '-n' parameter)";
796 goto tcp_close_return;
797 }
798
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200799 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100800 err |= ERR_FATAL | ERR_ALERT;
801 msg = "cannot make socket non-blocking";
802 goto tcp_close_return;
803 }
804
Willy Tarreau40aa0702013-03-10 23:51:38 +0100805 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100806 /* not fatal but should be reported */
807 msg = "cannot do so_reuseaddr";
808 err |= ERR_ALERT;
809 }
810
811 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900812 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200813 else {
814 struct linger tmplinger;
815 socklen_t len = sizeof(tmplinger);
816 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
817 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
818 tmplinger.l_onoff = 0;
819 tmplinger.l_linger = 0;
820 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
821 sizeof(tmplinger));
822 }
823 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100824
Willy Tarreaue6b98942007-10-29 01:09:36 +0100825#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000826 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
827 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100828 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000829 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100830 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100831#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200832
Willy Tarreau40aa0702013-03-10 23:51:38 +0100833 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200834 switch (listener->addr.ss_family) {
835 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200836 if (1
837#if defined(IP_TRANSPARENT)
838 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
839#endif
840#if defined(IP_FREEBIND)
841 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
842#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200843#if defined(IP_BINDANY)
844 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
845#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200846#if defined(SO_BINDANY)
847 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
848#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200849 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200850 msg = "cannot make listening socket transparent";
851 err |= ERR_ALERT;
852 }
853 break;
854 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200855 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200856#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200857 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
858#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100859#if defined(IP_FREEBIND)
860 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
861#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200862#if defined(IPV6_BINDANY)
863 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
864#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200865#if defined(SO_BINDANY)
866 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
867#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200868 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200869 msg = "cannot make listening socket transparent";
870 err |= ERR_ALERT;
871 }
872 break;
873 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100874 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200875
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100876#ifdef SO_BINDTODEVICE
877 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100878 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100879 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100880 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100881 msg = "cannot bind listener to device";
882 err |= ERR_WARN;
883 }
884 }
885#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400886#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100887 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400888 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200889 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
890 msg = "cannot set MSS";
891 err |= ERR_WARN;
892 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200893 } else if (ext) {
894 int tmpmaxseg = -1;
895 int defaultmss;
896 socklen_t len = sizeof(tmpmaxseg);
897
898 if (listener->addr.ss_family == AF_INET)
899 defaultmss = default_tcp_maxseg;
900 else
901 defaultmss = default_tcp6_maxseg;
902
903 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100904 if (defaultmss > 0 &&
905 tmpmaxseg != defaultmss &&
906 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200907 msg = "cannot set MSS";
908 err |= ERR_WARN;
909 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200910 }
911#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100912#if defined(TCP_USER_TIMEOUT)
913 if (listener->tcp_ut) {
914 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
915 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
916 msg = "cannot set TCP User Timeout";
917 err |= ERR_WARN;
918 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200919 } else
920 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
921 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100922#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200923#if defined(TCP_DEFER_ACCEPT)
924 if (listener->options & LI_O_DEF_ACCEPT) {
925 /* defer accept by up to one second */
926 int accept_delay = 1;
927 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
928 msg = "cannot enable DEFER_ACCEPT";
929 err |= ERR_WARN;
930 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200931 } else
932 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
933 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200934#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200935#if defined(TCP_FASTOPEN)
936 if (listener->options & LI_O_TCP_FO) {
937 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100938 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200939 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
940 msg = "cannot enable TCP_FASTOPEN";
941 err |= ERR_WARN;
942 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200943 } else {
944 socklen_t len;
945 int qlen;
946 len = sizeof(qlen);
947 /* Only disable fast open if it was enabled, we don't want
948 * the kernel to create a fast open queue if there's none.
949 */
950 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
951 qlen != 0) {
952 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
953 sizeof(zero)) == -1) {
954 msg = "cannot disable TCP_FASTOPEN";
955 err |= ERR_WARN;
956 }
957 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200958 }
959#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100960#if defined(IPV6_V6ONLY)
961 if (listener->options & LI_O_V6ONLY)
962 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100963 else if (listener->options & LI_O_V4V6)
964 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100965#endif
966
Willy Tarreau40aa0702013-03-10 23:51:38 +0100967 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100968 err |= ERR_RETRYABLE | ERR_ALERT;
969 msg = "cannot bind socket";
970 goto tcp_close_return;
971 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100972
Willy Tarreau40aa0702013-03-10 23:51:38 +0100973 ready = 0;
974 ready_len = sizeof(ready);
975 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
976 ready = 0;
977
978 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100979 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100980 err |= ERR_RETRYABLE | ERR_ALERT;
981 msg = "cannot listen to socket";
982 goto tcp_close_return;
983 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100984
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400985#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200986 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900987 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200988 else
989 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200990#endif
991
Willy Tarreaue6b98942007-10-29 01:09:36 +0100992 /* the socket is ready */
993 listener->fd = fd;
994 listener->state = LI_LISTEN;
995
Willy Tarreaua9786b62018-01-25 07:22:13 +0100996 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100997 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaueb472682010-05-28 18:46:57 +0200998
Willy Tarreaue6b98942007-10-29 01:09:36 +0100999 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001000 if (msg && errlen) {
1001 char pn[INET6_ADDRSTRLEN];
1002
Willy Tarreau631f01c2011-09-05 00:36:48 +02001003 addr_to_str(&listener->addr, pn, sizeof(pn));
1004 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001005 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001006 return err;
1007
1008 tcp_close_return:
1009 close(fd);
1010 goto tcp_return;
1011}
1012
1013/* This function creates all TCP sockets bound to the protocol entry <proto>.
1014 * It is intended to be used as the protocol's bind_all() function.
1015 * The sockets will be registered but not added to any fd_set, in order not to
1016 * loose them across the fork(). A call to enable_all_listeners() is needed
1017 * to complete initialization. The return value is composed from ERR_*.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001018 *
1019 * Must be called with proto_lock held.
1020 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001021 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001022static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001023{
1024 struct listener *listener;
1025 int err = ERR_NONE;
1026
1027 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001028 err |= tcp_bind_listener(listener, errmsg, errlen);
1029 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001030 break;
1031 }
1032
1033 return err;
1034}
1035
Willy Tarreau32282382017-09-15 07:44:44 +02001036/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1037 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1038 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001039 *
1040 * Must be called with proto_lock held.
1041 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001042 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001043static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001044{
1045 if (listener->state != LI_INIT)
1046 return;
1047 listener->state = LI_ASSIGNED;
1048 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001049 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001050 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1051 proto_tcpv4.nb_listeners++;
1052}
1053
Willy Tarreau32282382017-09-15 07:44:44 +02001054/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1055 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1056 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001057 *
1058 * Must be called with proto_lock held.
1059 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001060 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001061static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001062{
1063 if (listener->state != LI_INIT)
1064 return;
1065 listener->state = LI_ASSIGNED;
1066 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001067 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001068 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1069 proto_tcpv6.nb_listeners++;
1070}
1071
Willy Tarreau092d8652014-07-07 20:22:12 +02001072/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1073 * was totally stopped, or > 0 if correctly paused.
1074 */
1075int tcp_pause_listener(struct listener *l)
1076{
1077 if (shutdown(l->fd, SHUT_WR) != 0)
1078 return -1; /* Solaris dies here */
1079
Willy Tarreaue2711c72019-02-27 15:39:41 +01001080 if (listen(l->fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +02001081 return -1; /* OpenBSD dies here */
1082
1083 if (shutdown(l->fd, SHUT_RD) != 0)
1084 return -1; /* should always be OK */
1085 return 1;
1086}
1087
William Lallemand2e785f22016-05-25 01:48:42 +02001088/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001089 * Execute the "set-src" action. May be called from {tcp,http}request.
1090 * It only changes the address and tries to preserve the original port. If the
1091 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001092 */
1093enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1094 struct session *sess, struct stream *s, int flags)
1095{
1096 struct connection *cli_conn;
1097
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001098 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand2e785f22016-05-25 01:48:42 +02001099 struct sample *smp;
1100
1101 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1102 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001103 int port = get_net_port(cli_conn->src);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001104
William Lallemand2e785f22016-05-25 01:48:42 +02001105 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001106 ((struct sockaddr_in *)cli_conn->src)->sin_family = AF_INET;
1107 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1108 ((struct sockaddr_in *)cli_conn->src)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001109 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001110 ((struct sockaddr_in6 *)cli_conn->src)->sin6_family = AF_INET6;
1111 memcpy(&((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1112 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001113 }
1114 }
William Lallemand01252ed2016-05-25 02:33:16 +02001115 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001116 }
1117 return ACT_RET_CONT;
1118}
1119
William Lallemand44be6402016-05-25 01:51:35 +02001120/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001121 * Execute the "set-dst" action. May be called from {tcp,http}request.
1122 * It only changes the address and tries to preserve the original port. If the
1123 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001124 */
1125enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1126 struct session *sess, struct stream *s, int flags)
1127{
1128 struct connection *cli_conn;
1129
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001130 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001131 struct sample *smp;
1132
1133 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1134 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001135 int port = get_net_port(cli_conn->dst);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001136
William Lallemand13e9b0c2016-05-25 02:34:07 +02001137 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001138 ((struct sockaddr_in *)cli_conn->dst)->sin_family = AF_INET;
1139 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001140 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001141 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_family = AF_INET6;
1142 memcpy(&((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1143 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001144 }
1145 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1146 }
1147 }
1148 return ACT_RET_CONT;
1149}
1150
1151/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001152 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1153 * We must test the sin_family before setting the port. If the address family
1154 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1155 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001156 */
1157enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1158 struct session *sess, struct stream *s, int flags)
1159{
1160 struct connection *cli_conn;
1161
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001162 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand44be6402016-05-25 01:51:35 +02001163 struct sample *smp;
1164
William Lallemand44be6402016-05-25 01:51:35 +02001165 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1166 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001167 if (cli_conn->src->ss_family == AF_INET6) {
1168 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001169 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001170 if (cli_conn->src->ss_family != AF_INET) {
1171 cli_conn->src->ss_family = AF_INET;
1172 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001173 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001174 ((struct sockaddr_in *)cli_conn->src)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001175 }
1176 }
1177 }
1178 return ACT_RET_CONT;
1179}
1180
William Lallemand13e9b0c2016-05-25 02:34:07 +02001181/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001182 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1183 * We must test the sin_family before setting the port. If the address family
1184 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1185 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001186 */
1187enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1188 struct session *sess, struct stream *s, int flags)
1189{
1190 struct connection *cli_conn;
1191
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001192 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001193 struct sample *smp;
1194
William Lallemand13e9b0c2016-05-25 02:34:07 +02001195 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1196 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001197 if (cli_conn->dst->ss_family == AF_INET6) {
1198 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001199 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001200 if (cli_conn->dst->ss_family != AF_INET) {
1201 cli_conn->dst->ss_family = AF_INET;
1202 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001203 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001204 ((struct sockaddr_in *)cli_conn->dst)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001205 }
1206 }
1207 }
1208 return ACT_RET_CONT;
1209}
1210
Willy Tarreau2d392c22015-08-24 01:43:45 +02001211/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1212static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1213{
1214 struct connection *conn = objt_conn(sess->origin);
1215
1216 if (!conn)
1217 goto out;
1218
1219 if (!conn_ctrl_ready(conn))
1220 goto out;
1221
Willy Tarreau2d392c22015-08-24 01:43:45 +02001222#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001223 /* drain is needed only to send the quick ACK */
1224 conn_sock_drain(conn);
1225
Willy Tarreau2d392c22015-08-24 01:43:45 +02001226 /* re-enable quickack if it was disabled to ack all data and avoid
1227 * retransmits from the client that might trigger a real reset.
1228 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001229 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001230#endif
1231 /* lingering must absolutely be disabled so that we don't send a
1232 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1233 * is present, returning with ERR will cause lingering to be disabled.
1234 */
1235 if (strm)
1236 strm->si[0].flags |= SI_FL_NOLINGER;
1237
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001238 /* We're on the client-facing side, we must force to disable lingering to
1239 * ensure we will use an RST exclusively and kill any pending data.
1240 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001241 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001242
Willy Tarreau2d392c22015-08-24 01:43:45 +02001243#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001244 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001245 /* socket will be quiet now */
1246 goto out;
1247 }
1248#endif
1249 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1250 * Let's fall back on the TTL trick, though it only works for routed
1251 * network and has no effect on local net.
1252 */
1253#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001254 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001255#endif
1256 out:
1257 /* kill the stream if any */
1258 if (strm) {
1259 channel_abort(&strm->req);
1260 channel_abort(&strm->res);
Christopher Faulet177f4802020-03-06 15:10:46 +01001261 strm->req.analysers &= AN_REQ_FLT_END;
1262 strm->res.analysers &= AN_RES_FLT_END;
Christopher Fauletddc005a2020-03-06 15:23:18 +01001263 if (strm->flags & SF_BE_ASSIGNED)
1264 _HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001265 if (!(strm->flags & SF_ERR_MASK))
1266 strm->flags |= SF_ERR_PRXCOND;
1267 if (!(strm->flags & SF_FINST_MASK))
1268 strm->flags |= SF_FINST_R;
1269 }
1270
Olivier Houchard40514102019-03-08 18:54:04 +01001271 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001272 if (sess->listener->counters)
Olivier Houchard40514102019-03-08 18:54:04 +01001273 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001274
Christopher Fauletf573ba22020-03-06 15:15:49 +01001275 return ACT_RET_ABRT;
Willy Tarreau2d392c22015-08-24 01:43:45 +02001276}
1277
William Lallemand13e9b0c2016-05-25 02:34:07 +02001278/* parse "set-{src,dst}[-port]" action */
1279enum 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 +02001280{
1281 int cur_arg;
1282 struct sample_expr *expr;
1283 unsigned int where;
1284
1285 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001286 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 +02001287 if (!expr)
1288 return ACT_RET_PRS_ERR;
1289
1290 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001291 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001292 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001293 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001294 where |= SMP_VAL_BE_HRQ_HDR;
1295
1296 if (!(expr->fetch->val & where)) {
1297 memprintf(err,
1298 "fetch method '%s' extracts information from '%s', none of which is available here",
1299 args[cur_arg-1], sample_src_names(expr->fetch->use));
1300 free(expr);
1301 return ACT_RET_PRS_ERR;
1302 }
1303 rule->arg.expr = expr;
1304 rule->action = ACT_CUSTOM;
1305
1306 if (!strcmp(args[*orig_arg-1], "set-src")) {
1307 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001308 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1309 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001310 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1311 rule->action_ptr = tcp_action_req_set_dst;
1312 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1313 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001314 } else {
1315 return ACT_RET_PRS_ERR;
1316 }
1317
1318 (*orig_arg)++;
1319
1320 return ACT_RET_PRS_OK;
1321}
1322
1323
Willy Tarreau2d392c22015-08-24 01:43:45 +02001324/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1325 * success, ACT_RET_PRS_ERR on error.
1326 */
1327static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1328 struct act_rule *rule, char **err)
1329{
1330 rule->action = ACT_CUSTOM;
1331 rule->action_ptr = tcp_exec_action_silent_drop;
1332 return ACT_RET_PRS_OK;
1333}
1334
Willy Tarreau645513a2010-05-24 20:55:15 +02001335
1336/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001337/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001338/************************************************************************/
1339
Willy Tarreau4a129812012-04-25 17:31:42 +02001340/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001341int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001342{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001343 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001344
1345 if (!cli_conn)
1346 return 0;
1347
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001348 if (!conn_get_src(cli_conn))
1349 return 0;
1350
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001351 switch (cli_conn->src->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001352 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001353 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->src)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001354 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001355 break;
1356 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001357 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->src)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001358 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001359 break;
1360 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001361 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001362 }
Emeric Brunf769f512010-10-22 17:14:01 +02001363
Willy Tarreau37406352012-04-23 16:16:37 +02001364 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001365 return 1;
1366}
1367
Willy Tarreaua5e37562011-12-16 17:06:15 +01001368/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001369static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001370smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001371{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001372 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001373
1374 if (!cli_conn)
1375 return 0;
1376
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001377 if (!conn_get_src(cli_conn))
1378 return 0;
1379
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001380 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001381 if (!(smp->data.u.sint = get_host_port(cli_conn->src)))
Emeric Brunf769f512010-10-22 17:14:01 +02001382 return 0;
1383
Willy Tarreau37406352012-04-23 16:16:37 +02001384 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001385 return 1;
1386}
1387
Willy Tarreau4a129812012-04-25 17:31:42 +02001388/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001389static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001390smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001391{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001392 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001393
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001394 if (!cli_conn)
1395 return 0;
1396
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001397 if (!conn_get_dst(cli_conn))
1398 return 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001399
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001400 switch (cli_conn->dst->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001401 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001402 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001403 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001404 break;
1405 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001406 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001407 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001408 break;
1409 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001410 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001411 }
Emeric Brunf769f512010-10-22 17:14:01 +02001412
Willy Tarreau37406352012-04-23 16:16:37 +02001413 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001414 return 1;
1415}
1416
Willy Tarreau16e01562016-08-09 16:46:18 +02001417/* check if the destination address of the front connection is local to the
1418 * system or if it was intercepted.
1419 */
1420int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1421{
1422 struct connection *conn = objt_conn(smp->sess->origin);
1423 struct listener *li = smp->sess->listener;
1424
1425 if (!conn)
1426 return 0;
1427
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001428 if (!conn_get_dst(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001429 return 0;
1430
1431 smp->data.type = SMP_T_BOOL;
1432 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001433 smp->data.u.sint = addr_is_local(li->netns, conn->dst);
Willy Tarreau16e01562016-08-09 16:46:18 +02001434 return smp->data.u.sint >= 0;
1435}
1436
1437/* check if the source address of the front connection is local to the system
1438 * or not.
1439 */
1440int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1441{
1442 struct connection *conn = objt_conn(smp->sess->origin);
1443 struct listener *li = smp->sess->listener;
1444
1445 if (!conn)
1446 return 0;
1447
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001448 if (!conn_get_src(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001449 return 0;
1450
1451 smp->data.type = SMP_T_BOOL;
1452 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001453 smp->data.u.sint = addr_is_local(li->netns, conn->src);
Willy Tarreau16e01562016-08-09 16:46:18 +02001454 return smp->data.u.sint >= 0;
1455}
1456
Willy Tarreaua5e37562011-12-16 17:06:15 +01001457/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001458static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001459smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001460{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001461 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001462
1463 if (!cli_conn)
1464 return 0;
1465
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001466 if (!conn_get_dst(cli_conn))
1467 return 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001468
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001469 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001470 if (!(smp->data.u.sint = get_host_port(cli_conn->dst)))
Emeric Brunf769f512010-10-22 17:14:01 +02001471 return 0;
1472
Willy Tarreau37406352012-04-23 16:16:37 +02001473 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001474 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001475}
1476
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001477#ifdef TCP_INFO
1478
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001479
1480/* Validates the arguments passed to "fc_*" fetch keywords returning a time
1481 * value. These keywords support an optional string representing the unit of the
1482 * result: "us" for microseconds and "ms" for milliseconds". Returns 0 on error
1483 * and non-zero if OK.
1484 */
1485static int val_fc_time_value(struct arg *args, char **err)
1486{
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001487 if (args[0].type == ARGT_STR) {
1488 if (strcmp(args[0].data.str.area, "us") == 0) {
1489 free(args[0].data.str.area);
1490 args[0].type = ARGT_SINT;
1491 args[0].data.sint = TIME_UNIT_US;
1492 }
1493 else if (strcmp(args[0].data.str.area, "ms") == 0) {
1494 free(args[0].data.str.area);
1495 args[0].type = ARGT_SINT;
1496 args[0].data.sint = TIME_UNIT_MS;
1497 }
1498 else {
1499 memprintf(err, "expects 'us' or 'ms', got '%s'",
1500 args[0].data.str.area);
1501 return 0;
1502 }
1503 }
1504 else {
1505 memprintf(err, "Unexpected arg type");
1506 return 0;
1507 }
1508
1509 return 1;
1510}
1511
1512/* Validates the arguments passed to "fc_*" fetch keywords returning a
1513 * counter. These keywords should be used without any keyword, but because of a
1514 * bug in previous versions, an optional string argument may be passed. In such
1515 * case, the argument is ignored and a warning is emitted. Returns 0 on error
1516 * and non-zero if OK.
1517 */
1518static int var_fc_counter(struct arg *args, char **err)
1519{
1520 if (args[0].type != ARGT_STOP) {
1521 ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n");
1522 if (args[0].type == ARGT_STR)
1523 free(args[0].data.str.area);
1524 args[0].type = ARGT_STOP;
1525 }
1526
1527 return 1;
1528}
1529
Joseph Herlanta6331472018-11-25 12:59:12 -08001530/* Returns some tcp_info data if it's available. "dir" must be set to 0 if
1531 * the client connection is required, otherwise it is set to 1. "val" represents
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001532 * the required value.
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001533 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1534 */
1535static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1536 int dir, int val)
1537{
1538 struct connection *conn;
1539 struct tcp_info info;
1540 socklen_t optlen;
1541
1542 /* strm can be null. */
1543 if (!smp->strm)
1544 return 0;
1545
1546 /* get the object associated with the stream interface.The
1547 * object can be other thing than a connection. For example,
1548 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001549 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001550 if (!conn)
1551 return 0;
1552
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001553 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001554 syscal can fail. */
1555 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001556 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001557 return 0;
1558
1559 /* extract the value. */
1560 smp->data.type = SMP_T_SINT;
1561 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001562 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1563 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1564#if defined(__linux__)
1565 /* these ones are common to all Linux versions */
1566 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1567 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1568 case 4: smp->data.u.sint = info.tcpi_lost; break;
1569 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1570 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1571 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1572#elif defined(__FreeBSD__) || defined(__NetBSD__)
1573 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1574 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1575 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1576 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1577 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1578 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1579 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1580#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001581 default: return 0;
1582 }
1583
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001584 return 1;
1585}
1586
1587/* get the mean rtt of a client connexion */
1588static int
1589smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1590{
1591 if (!get_tcp_info(args, smp, 0, 0))
1592 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001593
1594 /* By default or if explicitly specified, convert rtt to ms */
1595 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1596 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1597
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001598 return 1;
1599}
1600
1601/* get the variance of the mean rtt of a client connexion */
1602static int
1603smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1604{
1605 if (!get_tcp_info(args, smp, 0, 1))
1606 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001607
1608 /* By default or if explicitly specified, convert rttvar to ms */
1609 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1610 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1611
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001612 return 1;
1613}
Joe Williams30fcd392016-08-10 07:06:44 -07001614
1615#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1616
1617/* get the unacked counter on a client connexion */
1618static int
1619smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1620{
1621 if (!get_tcp_info(args, smp, 0, 2))
1622 return 0;
1623 return 1;
1624}
1625
1626/* get the sacked counter on a client connexion */
1627static int
1628smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1629{
1630 if (!get_tcp_info(args, smp, 0, 3))
1631 return 0;
1632 return 1;
1633}
1634
1635/* get the lost counter on a client connexion */
1636static int
1637smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1638{
1639 if (!get_tcp_info(args, smp, 0, 4))
1640 return 0;
1641 return 1;
1642}
1643
1644/* get the retrans counter on a client connexion */
1645static int
1646smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1647{
1648 if (!get_tcp_info(args, smp, 0, 5))
1649 return 0;
1650 return 1;
1651}
1652
1653/* get the fackets counter on a client connexion */
1654static int
1655smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1656{
1657 if (!get_tcp_info(args, smp, 0, 6))
1658 return 0;
1659 return 1;
1660}
1661
1662/* get the reordering counter on a client connexion */
1663static int
1664smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1665{
1666 if (!get_tcp_info(args, smp, 0, 7))
1667 return 0;
1668 return 1;
1669}
1670#endif // linux || freebsd || netbsd
1671#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001672
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001673#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001674/* parse the "v4v6" bind keyword */
1675static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1676{
1677 struct listener *l;
1678
1679 list_for_each_entry(l, &conf->listeners, by_bind) {
1680 if (l->addr.ss_family == AF_INET6)
1681 l->options |= LI_O_V4V6;
1682 }
1683
1684 return 0;
1685}
1686
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001687/* parse the "v6only" bind keyword */
1688static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1689{
1690 struct listener *l;
1691
1692 list_for_each_entry(l, &conf->listeners, by_bind) {
1693 if (l->addr.ss_family == AF_INET6)
1694 l->options |= LI_O_V6ONLY;
1695 }
1696
1697 return 0;
1698}
1699#endif
1700
Pieter Baauwd551fb52013-05-08 22:49:23 +02001701#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001702/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001703static 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 +02001704{
1705 struct listener *l;
1706
Willy Tarreau4348fad2012-09-20 16:48:07 +02001707 list_for_each_entry(l, &conf->listeners, by_bind) {
1708 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1709 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001710 }
1711
Willy Tarreau44791242012-09-12 23:27:21 +02001712 return 0;
1713}
1714#endif
1715
1716#ifdef TCP_DEFER_ACCEPT
1717/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001718static 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 +02001719{
1720 struct listener *l;
1721
Willy Tarreau4348fad2012-09-20 16:48:07 +02001722 list_for_each_entry(l, &conf->listeners, by_bind) {
1723 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1724 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001725 }
1726
Willy Tarreau44791242012-09-12 23:27:21 +02001727 return 0;
1728}
1729#endif
1730
Willy Tarreau1c862c52012-10-05 16:21:00 +02001731#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001732/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001733static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1734{
1735 struct listener *l;
1736
1737 list_for_each_entry(l, &conf->listeners, by_bind) {
1738 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1739 l->options |= LI_O_TCP_FO;
1740 }
1741
1742 return 0;
1743}
1744#endif
1745
Willy Tarreau44791242012-09-12 23:27:21 +02001746#ifdef TCP_MAXSEG
1747/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001748static 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 +02001749{
1750 struct listener *l;
1751 int mss;
1752
Willy Tarreau44791242012-09-12 23:27:21 +02001753 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001754 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001755 return ERR_ALERT | ERR_FATAL;
1756 }
1757
1758 mss = atoi(args[cur_arg + 1]);
1759 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001760 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001761 return ERR_ALERT | ERR_FATAL;
1762 }
1763
Willy Tarreau4348fad2012-09-20 16:48:07 +02001764 list_for_each_entry(l, &conf->listeners, by_bind) {
1765 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1766 l->maxseg = mss;
1767 }
Willy Tarreau44791242012-09-12 23:27:21 +02001768
1769 return 0;
1770}
1771#endif
1772
Willy Tarreau2af207a2015-02-04 00:45:58 +01001773#ifdef TCP_USER_TIMEOUT
1774/* parse the "tcp-ut" bind keyword */
1775static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1776{
1777 const char *ptr = NULL;
1778 struct listener *l;
1779 unsigned int timeout;
1780
1781 if (!*args[cur_arg + 1]) {
1782 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1783 return ERR_ALERT | ERR_FATAL;
1784 }
1785
1786 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001787 if (ptr == PARSE_TIME_OVER) {
1788 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1789 args[cur_arg+1], args[cur_arg]);
1790 return ERR_ALERT | ERR_FATAL;
1791 }
1792 else if (ptr == PARSE_TIME_UNDER) {
1793 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1794 args[cur_arg+1], args[cur_arg]);
1795 return ERR_ALERT | ERR_FATAL;
1796 }
1797 else if (ptr) {
Willy Tarreau2af207a2015-02-04 00:45:58 +01001798 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1799 return ERR_ALERT | ERR_FATAL;
1800 }
1801
1802 list_for_each_entry(l, &conf->listeners, by_bind) {
1803 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1804 l->tcp_ut = timeout;
1805 }
1806
1807 return 0;
1808}
1809#endif
1810
Willy Tarreau44791242012-09-12 23:27:21 +02001811#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001812/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001813static 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 +02001814{
1815 struct listener *l;
1816
Willy Tarreau44791242012-09-12 23:27:21 +02001817 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001818 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001819 return ERR_ALERT | ERR_FATAL;
1820 }
1821
Willy Tarreau4348fad2012-09-20 16:48:07 +02001822 list_for_each_entry(l, &conf->listeners, by_bind) {
1823 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1824 l->interface = strdup(args[cur_arg + 1]);
1825 }
Willy Tarreau44791242012-09-12 23:27:21 +02001826
Willy Tarreau44791242012-09-12 23:27:21 +02001827 return 0;
1828}
1829#endif
1830
Willy Tarreaue5733232019-05-22 19:24:06 +02001831#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001832/* parse the "namespace" bind keyword */
1833static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1834{
1835 struct listener *l;
1836 char *namespace = NULL;
1837
1838 if (!*args[cur_arg + 1]) {
1839 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1840 return ERR_ALERT | ERR_FATAL;
1841 }
1842 namespace = args[cur_arg + 1];
1843
1844 list_for_each_entry(l, &conf->listeners, by_bind) {
1845 l->netns = netns_store_lookup(namespace, strlen(namespace));
1846
1847 if (l->netns == NULL)
1848 l->netns = netns_store_insert(namespace);
1849
1850 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001851 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001852 return ERR_ALERT | ERR_FATAL;
1853 }
1854 }
1855 return 0;
1856}
1857#endif
1858
Willy Tarreau163d4622015-10-13 16:16:41 +02001859#ifdef TCP_USER_TIMEOUT
1860/* parse the "tcp-ut" server keyword */
1861static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1862{
1863 const char *ptr = NULL;
1864 unsigned int timeout;
1865
1866 if (!*args[*cur_arg + 1]) {
1867 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1868 return ERR_ALERT | ERR_FATAL;
1869 }
1870
1871 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001872 if (ptr == PARSE_TIME_OVER) {
1873 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1874 args[*cur_arg+1], args[*cur_arg]);
1875 return ERR_ALERT | ERR_FATAL;
1876 }
1877 else if (ptr == PARSE_TIME_UNDER) {
1878 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1879 args[*cur_arg+1], args[*cur_arg]);
1880 return ERR_ALERT | ERR_FATAL;
1881 }
1882 else if (ptr) {
Willy Tarreau163d4622015-10-13 16:16:41 +02001883 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1884 return ERR_ALERT | ERR_FATAL;
1885 }
1886
1887 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1888 newsrv->tcp_ut = timeout;
1889
1890 return 0;
1891}
1892#endif
1893
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001894
Willy Tarreau4a129812012-04-25 17:31:42 +02001895/* Note: must not be declared <const> as its list will be overwritten.
1896 * Note: fetches that may return multiple types must be declared as the lowest
1897 * common denominator, the type that can be casted into all other ones. For
1898 * instance v4/v6 must be declared v4.
1899 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001900static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001901 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001902 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001903 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001904 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001905 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001906 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001907#ifdef TCP_INFO
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001908 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
1909 { "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 -07001910#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001911 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1912 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1913 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1914 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1915 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1916 { "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 -07001917#endif // linux || freebsd || netbsd
1918#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001919 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001920}};
1921
Willy Tarreau0108d902018-11-25 19:14:37 +01001922INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1923
Willy Tarreau44791242012-09-12 23:27:21 +02001924/************************************************************************/
1925/* All supported bind keywords must be declared here. */
1926/************************************************************************/
1927
1928/* Note: must not be declared <const> as its list will be overwritten.
1929 * Please take care of keeping this list alphabetically sorted, doing so helps
1930 * all code contributors.
1931 * Optional keywords are also declared with a NULL ->parse() function so that
1932 * the config parser can report an appropriate error when a known keyword was
1933 * not enabled.
1934 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001935static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001936#ifdef TCP_DEFER_ACCEPT
1937 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1938#endif
1939#ifdef SO_BINDTODEVICE
1940 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1941#endif
1942#ifdef TCP_MAXSEG
1943 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1944#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001945#ifdef TCP_USER_TIMEOUT
1946 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1947#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001948#ifdef TCP_FASTOPEN
1949 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1950#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001951#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001952 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1953#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001954#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001955 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001956 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1957#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001958#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001959 { "namespace", bind_parse_namespace, 1 },
1960#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001961 /* the versions with the NULL parse function*/
1962 { "defer-accept", NULL, 0 },
1963 { "interface", NULL, 1 },
1964 { "mss", NULL, 1 },
1965 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001966 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001967 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001968 { NULL, NULL, 0 },
1969}};
1970
Willy Tarreau0108d902018-11-25 19:14:37 +01001971INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1972
Willy Tarreau163d4622015-10-13 16:16:41 +02001973static struct srv_kw_list srv_kws = { "TCP", { }, {
1974#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001975 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02001976#endif
1977 { NULL, NULL, 0 },
1978}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02001979
Willy Tarreau0108d902018-11-25 19:14:37 +01001980INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
1981
Willy Tarreau2d392c22015-08-24 01:43:45 +02001982static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001983 { "set-src", tcp_parse_set_src_dst },
1984 { "set-src-port", tcp_parse_set_src_dst },
1985 { "set-dst" , tcp_parse_set_src_dst },
1986 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02001987 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02001988 { /* END */ }
1989}};
1990
Willy Tarreau0108d902018-11-25 19:14:37 +01001991INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions);
1992
Willy Tarreau620408f2016-10-21 16:37:51 +02001993static struct action_kw_list tcp_req_sess_actions = {ILH, {
Willy Tarreau620408f2016-10-21 16:37:51 +02001994 { "set-src", tcp_parse_set_src_dst },
1995 { "set-src-port", tcp_parse_set_src_dst },
1996 { "set-dst" , tcp_parse_set_src_dst },
1997 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02001998 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau620408f2016-10-21 16:37:51 +02001999 { /* END */ }
2000}};
2001
Willy Tarreau0108d902018-11-25 19:14:37 +01002002INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions);
2003
Willy Tarreau2d392c22015-08-24 01:43:45 +02002004static struct action_kw_list tcp_req_cont_actions = {ILH, {
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002005 { "set-dst" , tcp_parse_set_src_dst },
2006 { "set-dst-port", tcp_parse_set_src_dst },
2007 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002008 { /* END */ }
2009}};
2010
Willy Tarreau0108d902018-11-25 19:14:37 +01002011INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2012
Willy Tarreau2d392c22015-08-24 01:43:45 +02002013static struct action_kw_list tcp_res_cont_actions = {ILH, {
2014 { "silent-drop", tcp_parse_silent_drop },
2015 { /* END */ }
2016}};
2017
Willy Tarreau0108d902018-11-25 19:14:37 +01002018INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
2019
Willy Tarreau2d392c22015-08-24 01:43:45 +02002020static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002021 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002022 { "set-src", tcp_parse_set_src_dst },
2023 { "set-src-port", tcp_parse_set_src_dst },
2024 { "set-dst", tcp_parse_set_src_dst },
2025 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002026 { /* END */ }
2027}};
2028
Willy Tarreau0108d902018-11-25 19:14:37 +01002029INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2030
Willy Tarreau2d392c22015-08-24 01:43:45 +02002031static struct action_kw_list http_res_actions = {ILH, {
2032 { "silent-drop", tcp_parse_silent_drop },
2033 { /* END */ }
2034}};
2035
Willy Tarreau0108d902018-11-25 19:14:37 +01002036INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002037
Willy Tarreau80713382018-11-26 10:19:54 +01002038REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
Willy Tarreauba962912016-12-21 18:55:02 +01002039#if defined(IP_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002040 " IP_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002041#endif
2042#if defined(IPV6_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002043 " IPV6_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002044#endif
2045#if defined(IP_FREEBIND)
Willy Tarreau80713382018-11-26 10:19:54 +01002046 " IP_FREEBIND"
Willy Tarreauba962912016-12-21 18:55:02 +01002047#endif
2048#if defined(IP_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002049 " IP_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002050#endif
2051#if defined(IPV6_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002052 " IPV6_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002053#endif
2054#if defined(SO_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002055 " SO_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002056#endif
Willy Tarreau80713382018-11-26 10:19:54 +01002057 "");
Willy Tarreaue6b98942007-10-29 01:09:36 +01002058
2059
2060/*
2061 * Local variables:
2062 * c-indent-level: 8
2063 * c-basic-offset: 8
2064 * End:
2065 */