blob: 9f0309672aa8188585e9f9ea744dd1c8a823e779 [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 Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/arg.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020036#include <haproxy/channel.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020037#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020038#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/fd.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020040#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020041#include <haproxy/http_rules.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020042#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020043#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020044#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020045#include <haproxy/namespace.h>
46#include <haproxy/port_range.h>
47#include <haproxy/proto_tcp.h>
48#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020049#include <haproxy/proxy-t.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020050#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020051#include <haproxy/server.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020052#include <haproxy/stream-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020053#include <haproxy/tcp_rules.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020054#include <haproxy/tools.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010055
Willy Tarreaue6b98942007-10-29 01:09:36 +010056
Emeric Bruncf20bf12010-10-22 16:06:11 +020057static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
58static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020059static void tcpv4_add_listener(struct listener *listener, int port);
60static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010061
62/* Note: must not be declared <const> as its list will be overwritten */
63static struct protocol proto_tcpv4 = {
64 .name = "tcpv4",
65 .sock_domain = AF_INET,
66 .sock_type = SOCK_STREAM,
67 .sock_prot = IPPROTO_TCP,
68 .sock_family = AF_INET,
69 .sock_addrlen = sizeof(struct sockaddr_in),
70 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020071 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020072 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020073 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010074 .bind_all = tcp_bind_listeners,
75 .unbind_all = unbind_all_listeners,
76 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020077 .get_src = tcp_get_src,
78 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020079 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020080 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010081 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
82 .nb_listeners = 0,
83};
84
Willy Tarreau0108d902018-11-25 19:14:37 +010085INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
86
Willy Tarreaue6b98942007-10-29 01:09:36 +010087/* Note: must not be declared <const> as its list will be overwritten */
88static struct protocol proto_tcpv6 = {
89 .name = "tcpv6",
90 .sock_domain = AF_INET6,
91 .sock_type = SOCK_STREAM,
92 .sock_prot = IPPROTO_TCP,
93 .sock_family = AF_INET6,
94 .sock_addrlen = sizeof(struct sockaddr_in6),
95 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020096 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020097 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020098 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010099 .bind_all = tcp_bind_listeners,
100 .unbind_all = unbind_all_listeners,
101 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200102 .get_src = tcp_get_src,
103 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +0200104 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200105 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100106 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
107 .nb_listeners = 0,
108};
109
Willy Tarreau0108d902018-11-25 19:14:37 +0100110INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
111
Olivier Houchard153659f2017-04-05 22:39:56 +0200112/* Default TCP parameters, got by opening a temporary TCP socket. */
113#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100114static THREAD_LOCAL int default_tcp_maxseg = -1;
115static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200116#endif
117
David du Colombier6f5ccb12011-03-10 22:26:24 +0100118/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100119 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
120 * - 0 : ignore remote address (may even be a NULL pointer)
121 * - 1 : use provided address
122 * - 2 : use provided port
123 * - 3 : use both
124 *
125 * The function supports multiple foreign binding methods :
126 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100127 * The second one can be used as a fallback for the first one.
128 * This function returns 0 when everything's OK, 1 if it could not bind, to the
129 * local address, 2 if it could not bind to the foreign address.
130 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100131int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100132{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100133 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100134 int foreign_ok = 0;
135 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100136 static THREAD_LOCAL int ip_transp_working = 1;
137 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200138
David du Colombier65c17962012-07-13 14:34:59 +0200139 switch (local->ss_family) {
140 case AF_INET:
141 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200142 /* This deserves some explanation. Some platforms will support
143 * multiple combinations of certain methods, so we try the
144 * supported ones until one succeeds.
145 */
146 if (0
147#if defined(IP_TRANSPARENT)
148 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
149#endif
150#if defined(IP_FREEBIND)
151 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
152#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200153#if defined(IP_BINDANY)
154 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
155#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200156#if defined(SO_BINDANY)
157 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
158#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200159 )
David du Colombier65c17962012-07-13 14:34:59 +0200160 foreign_ok = 1;
161 else
162 ip_transp_working = 0;
163 }
164 break;
165 case AF_INET6:
166 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200167 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200168#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200169 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
170#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100171#if defined(IP_FREEBIND)
172 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
173#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200174#if defined(IPV6_BINDANY)
175 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
176#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200177#if defined(SO_BINDANY)
178 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
179#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200180 )
David du Colombier65c17962012-07-13 14:34:59 +0200181 foreign_ok = 1;
182 else
183 ip6_transp_working = 0;
184 }
185 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100186 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200187
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100188 if (flags) {
189 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200190 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100191 switch (remote->ss_family) {
192 case AF_INET:
193 if (flags & 1)
194 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
195 if (flags & 2)
196 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
197 break;
198 case AF_INET6:
199 if (flags & 1)
200 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
201 if (flags & 2)
202 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
203 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100204 default:
205 /* we don't want to try to bind to an unknown address family */
206 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100207 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100208 }
209
Simon Hormande072bd2011-06-24 15:11:37 +0900210 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100211 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200212 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200213 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
214 if (ret < 0)
215 return 2;
216 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100217 }
218 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200219 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200220 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
221 if (ret < 0)
222 return 1;
223 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100224 }
225
226 if (!flags)
227 return 0;
228
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100229 if (!foreign_ok)
230 /* we could not bind to a foreign address */
231 return 2;
232
233 return 0;
234}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100235
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200236/* conn->dst MUST be valid */
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100237static int create_server_socket(struct connection *conn)
238{
Willy Tarreau529c1392014-12-24 13:47:55 +0100239 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100240
Willy Tarreaue5733232019-05-22 19:24:06 +0200241#ifdef USE_NS
Willy Tarreau529c1392014-12-24 13:47:55 +0100242 if (objt_server(conn->target)) {
243 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
244 ns = conn->proxy_netns;
245 else
246 ns = __objt_server(conn->target)->netns;
247 }
248#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200249 return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, IPPROTO_TCP);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100250}
Willy Tarreau9650f372009-08-16 14:02:45 +0200251
252/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200253 * This function initiates a TCP connection establishment to the target assigned
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200254 * to connection <conn> using (si->{target,dst}). A source address may be
255 * pointed to by conn->src in case of transparent proxying. Normal source
Willy Tarreau14f8e862012-08-30 22:23:13 +0200256 * bind addresses are still determined locally (due to the possible need of a
257 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100258 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100259 * supported. The <data> parameter is a boolean indicating whether there are data
260 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200261 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
262 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100263 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200264 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
265 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200266 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200267 * Note that a pending send_proxy message accounts for data.
268 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200269 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200270 * - SF_ERR_NONE if everything's OK
271 * - SF_ERR_SRVTO if there are no more servers
272 * - SF_ERR_SRVCL if the connection was refused by the server
273 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
274 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
275 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100276 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100277 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200278 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100279 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200280 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100281
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200282int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200283{
284 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100285 struct server *srv;
286 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100287 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100288 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800289 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100290
Olivier Houchard637b6952018-11-23 14:23:07 +0100291 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100292
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100293 switch (obj_type(conn->target)) {
294 case OBJ_TYPE_PROXY:
295 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100296 srv = NULL;
297 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100298 case OBJ_TYPE_SERVER:
299 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100300 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100301 /* Make sure we check that we have data before activating
302 * TFO, or we could trigger a kernel issue whereby after
303 * a successful connect() == 0, any subsequent connect()
304 * will return EINPROGRESS instead of EISCONN.
305 */
306 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
307 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
308 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100309 break;
310 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100311 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200312 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100313 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200314
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200315 if (!conn->dst) {
316 conn->flags |= CO_FL_ERROR;
317 return SF_ERR_INTERNAL;
318 }
319
Willy Tarreau585744b2017-08-24 14:31:19 +0200320 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100321
322 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200323 qfprintf(stderr, "Cannot get a server socket.\n");
324
Willy Tarreau9ce70132014-01-24 16:08:19 +0100325 if (errno == ENFILE) {
326 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200327 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100328 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
329 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100330 }
331 else if (errno == EMFILE) {
332 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200333 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100334 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
335 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100336 }
337 else if (errno == ENOBUFS || errno == ENOMEM) {
338 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200339 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100340 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
341 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100342 }
343 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
344 conn->err_code = CO_ER_NOPROTO;
345 }
346 else
347 conn->err_code = CO_ER_SOCK_ERR;
348
Willy Tarreau9650f372009-08-16 14:02:45 +0200349 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100350 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200351 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 }
353
354 if (fd >= global.maxsock) {
355 /* do not log anything there, it's a normal condition when this option
356 * is used to serialize connections to a server !
357 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100358 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200359 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100360 conn->err_code = CO_ER_CONF_FDLIM;
361 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200362 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200363 }
364
365 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900366 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200367 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
368 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100369 conn->err_code = CO_ER_SOCK_ERR;
370 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200371 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200372 }
373
William Lallemandc03eb012018-11-27 12:02:37 +0100374 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
375 ha_alert("Cannot set CLOEXEC on client socket.\n");
376 close(fd);
377 conn->err_code = CO_ER_SOCK_ERR;
378 conn->flags |= CO_FL_ERROR;
379 return SF_ERR_INTERNAL;
380 }
381
Willy Tarreau9650f372009-08-16 14:02:45 +0200382 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900383 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200384
Willy Tarreau9650f372009-08-16 14:02:45 +0200385 /* allow specific binding :
386 * - server-specific at first
387 * - proxy-specific next
388 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100389 if (srv && srv->conn_src.opts & CO_SRC_BIND)
390 src = &srv->conn_src;
391 else if (be->conn_src.opts & CO_SRC_BIND)
392 src = &be->conn_src;
393 else
394 src = NULL;
395
396 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200397 int ret, flags = 0;
398
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200399 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100400 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100401 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200402 conn->flags |= CO_FL_PRIVATE;
403 /* fall through */
404 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200405 flags = 3;
406 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100407 case CO_SRC_TPROXY_CIP:
408 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200409 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200410 flags = 1;
411 break;
412 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200413 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200414
Willy Tarreau9650f372009-08-16 14:02:45 +0200415#ifdef SO_BINDTODEVICE
416 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100417 if (src->iface_name)
418 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200419#endif
420
Willy Tarreaua4380b42012-12-08 22:49:11 +0100421 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200422 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100423 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200424
425 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200426 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200427
428 do {
429 /* note: in case of retry, we may have to release a previously
430 * allocated port, hence this loop's construct.
431 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200432 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
433 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200434
435 if (!attempts)
436 break;
437 attempts--;
438
Willy Tarreaua4380b42012-12-08 22:49:11 +0100439 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100440 if (!fdinfo[fd].local_port) {
441 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100443 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200444
Willy Tarreaua4380b42012-12-08 22:49:11 +0100445 fdinfo[fd].port_range = src->sport_range;
446 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200447
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200448 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100449 if (ret != 0)
450 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200451 } while (ret != 0); /* binding NOK */
452 }
453 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000454#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100455 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000456 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
457#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200458 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100459 if (ret != 0)
460 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200461 }
462
Willy Tarreaua4380b42012-12-08 22:49:11 +0100463 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200464 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
465 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200466 close(fd);
467
Willy Tarreau9650f372009-08-16 14:02:45 +0200468 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100469 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
470 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200471 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100472 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200473 be->id);
474 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100475 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
476 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200477 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100478 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200479 be->id);
480 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100481 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200482 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200483 }
484 }
485
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400486#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200487 /* disabling tcp quick ack now allows the first request to leave the
488 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100489 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200490 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200491 if (flags & (CONNECT_DELACK_ALWAYS) ||
492 ((flags & CONNECT_DELACK_SMART_CONNECT ||
493 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
494 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900495 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200496#endif
497
Willy Tarreau163d4622015-10-13 16:16:41 +0200498#ifdef TCP_USER_TIMEOUT
499 /* there is not much more we can do here when it fails, it's still minor */
500 if (srv && srv->tcp_ut)
501 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
502#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100503
504 if (use_fastopen) {
505#if defined(TCP_FASTOPEN_CONNECT)
506 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
507#endif
508 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100509 if (global.tune.server_sndbuf)
510 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
511
512 if (global.tune.server_rcvbuf)
513 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
514
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200515 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800516 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100517 if (errno == EINPROGRESS || errno == EALREADY) {
518 /* common case, let's wait for connect status */
519 conn->flags |= CO_FL_WAIT_L4_CONN;
520 }
521 else if (errno == EISCONN) {
522 /* should normally not happen but if so, indicates that it's OK */
523 conn->flags &= ~CO_FL_WAIT_L4_CONN;
524 }
525 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200526 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100527 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200528 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100529 conn->err_code = CO_ER_FREE_PORTS;
530 }
531 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200532 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100533 conn->err_code = CO_ER_ADDR_INUSE;
534 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200535
Willy Tarreaub1719512012-12-08 23:03:28 +0100536 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200537 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
538 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200539 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100540 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100541 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200542 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200543 } else if (errno == ETIMEDOUT) {
544 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200545 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
546 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200547 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100548 conn->err_code = CO_ER_SOCK_ERR;
549 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200550 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200551 } else {
552 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
553 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200554 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
555 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200556 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100557 conn->err_code = CO_ER_SOCK_ERR;
558 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200559 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200560 }
561 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100562 else {
563 /* connect() == 0, this is great! */
564 conn->flags &= ~CO_FL_WAIT_L4_CONN;
565 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200566
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100567 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200568
Willy Tarreauf79c8172013-10-21 16:30:56 +0200569 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100570 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200571
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100572 if (conn->flags & CO_FL_WAIT_L4_CONN) {
573 fd_want_send(fd);
574 fd_cant_send(fd);
575 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200576
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200577 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200578 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100579 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200580 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200581 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200582
Willy Tarreaue7dff022015-04-03 01:14:29 +0200583 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200584}
585
586
Willy Tarreau59b94792012-05-11 16:16:40 +0200587/*
588 * Retrieves the source address for the socket <fd>, with <dir> indicating
589 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
590 * success, -1 in case of error. The socket's source address is stored in
591 * <sa> for <salen> bytes.
592 */
593int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
594{
595 if (dir)
596 return getsockname(fd, sa, &salen);
597 else
598 return getpeername(fd, sa, &salen);
599}
600
601
602/*
603 * Retrieves the original destination address for the socket <fd>, with <dir>
604 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
605 * listener, if the original destination address was translated, the original
606 * address is retrieved. It returns 0 in case of success, -1 in case of error.
607 * The socket's source address is stored in <sa> for <salen> bytes.
608 */
609int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
610{
611 if (dir)
612 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100613 else {
614 int ret = getsockname(fd, sa, &salen);
615
616 if (ret < 0)
617 return ret;
618
Willy Tarreaue5733232019-05-22 19:24:06 +0200619#if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100620 /* For TPROXY and Netfilter's NAT, we can retrieve the original
621 * IPv4 address before DNAT/REDIRECT. We must not do that with
622 * other families because v6-mapped IPv4 addresses are still
623 * reported as v4.
624 */
625 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
626 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
627 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200628#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100629 return ret;
630 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200631}
632
Olivier Houchard153659f2017-04-05 22:39:56 +0200633/* XXX: Should probably be elsewhere */
634static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
635{
636 if (a->ss_family != b->ss_family) {
637 return (-1);
638 }
639 switch (a->ss_family) {
640 case AF_INET:
641 {
642 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
643 if (a4->sin_port != b4->sin_port)
644 return (-1);
645 return (memcmp(&a4->sin_addr, &b4->sin_addr,
646 sizeof(a4->sin_addr)));
647 }
648 case AF_INET6:
649 {
650 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
651 if (a6->sin6_port != b6->sin6_port)
652 return (-1);
653 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
654 sizeof(a6->sin6_addr)));
655 }
656 default:
657 return (-1);
658 }
659
660}
661
662#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
663/* When binding the listeners, check if a socket has been sent to us by the
664 * previous process that we could reuse, instead of creating a new one.
665 */
666static int tcp_find_compatible_fd(struct listener *l)
667{
668 struct xfer_sock_list *xfer_sock = xfer_sock_list;
669 int ret = -1;
670
671 while (xfer_sock) {
672 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
673 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
674 (l->interface != NULL && xfer_sock->iface != NULL &&
675 !strcmp(l->interface, xfer_sock->iface))) {
676 if ((l->options & LI_MANDATORY_FLAGS) ==
677 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
678 if ((xfer_sock->namespace == NULL &&
679 l->netns == NULL)
Willy Tarreaue5733232019-05-22 19:24:06 +0200680#ifdef USE_NS
Olivier Houchard153659f2017-04-05 22:39:56 +0200681 || (xfer_sock->namespace != NULL &&
682 l->netns != NULL &&
683 !strcmp(xfer_sock->namespace,
684 l->netns->node.key))
685#endif
686 ) {
687 break;
688 }
689
690 }
691 }
692 }
693 xfer_sock = xfer_sock->next;
694 }
695 if (xfer_sock != NULL) {
696 ret = xfer_sock->fd;
697 if (xfer_sock == xfer_sock_list)
698 xfer_sock_list = xfer_sock->next;
699 if (xfer_sock->prev)
700 xfer_sock->prev->next = xfer_sock->next;
701 if (xfer_sock->next)
702 xfer_sock->next->prev = xfer_sock->prev;
703 free(xfer_sock->iface);
704 free(xfer_sock->namespace);
705 free(xfer_sock);
706 }
707 return ret;
708}
709#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200710
Willy Tarreaue6b98942007-10-29 01:09:36 +0100711/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100712 * an error message in <errmsg> if the message is at most <errlen> bytes long
713 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
714 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100715 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
716 * was alright and that no message was returned. ERR_RETRYABLE means that an
717 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700718 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100719 * the meaning of the error, but just indicate that a message is present which
720 * should be displayed with the respective level. Last, ERR_ABORT indicates
721 * that it's pointless to try to start other listeners. No error message is
722 * returned if errlen is NULL.
723 */
724int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
725{
726 __label__ tcp_return, tcp_close_return;
727 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100728 int ext, ready;
729 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100730 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200731#ifdef TCP_MAXSEG
732
733 /* Create a temporary TCP socket to get default parameters we can't
734 * guess.
735 * */
736 ready_len = sizeof(default_tcp_maxseg);
737 if (default_tcp_maxseg == -1) {
738 default_tcp_maxseg = -2;
739 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
740 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100741 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200742 else {
743 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
744 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100745 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
William Dauchyc0e23ae2020-02-12 10:09:14 +0100746 close(fd);
Olivier Houchard153659f2017-04-05 22:39:56 +0200747 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200748 }
749 if (default_tcp6_maxseg == -1) {
750 default_tcp6_maxseg = -2;
751 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
752 if (fd >= 0) {
753 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
754 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100755 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200756 close(fd);
757 }
758 }
759#endif
760
Willy Tarreaue6b98942007-10-29 01:09:36 +0100761
762 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100763 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100764 *errmsg = 0;
765
766 if (listener->state != LI_ASSIGNED)
767 return ERR_NONE; /* already bound */
768
769 err = ERR_NONE;
770
Olivier Houchard153659f2017-04-05 22:39:56 +0200771 if (listener->fd == -1)
772 listener->fd = tcp_find_compatible_fd(listener);
773
Willy Tarreau40aa0702013-03-10 23:51:38 +0100774 /* if the listener already has an fd assigned, then we were offered the
775 * fd by an external process (most likely the parent), and we don't want
776 * to create a new socket. However we still want to set a few flags on
777 * the socket.
778 */
779 fd = listener->fd;
780 ext = (fd >= 0);
781
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100782 if (!ext) {
783 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
784
785 if (fd == -1) {
786 err |= ERR_RETRYABLE | ERR_ALERT;
787 msg = "cannot create listening socket";
788 goto tcp_return;
789 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100790 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100791
Willy Tarreaue6b98942007-10-29 01:09:36 +0100792 if (fd >= global.maxsock) {
793 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
794 msg = "not enough free sockets (raise '-n' parameter)";
795 goto tcp_close_return;
796 }
797
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200798 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100799 err |= ERR_FATAL | ERR_ALERT;
800 msg = "cannot make socket non-blocking";
801 goto tcp_close_return;
802 }
803
Willy Tarreau40aa0702013-03-10 23:51:38 +0100804 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100805 /* not fatal but should be reported */
806 msg = "cannot do so_reuseaddr";
807 err |= ERR_ALERT;
808 }
809
810 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900811 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200812 else {
813 struct linger tmplinger;
814 socklen_t len = sizeof(tmplinger);
815 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
816 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
817 tmplinger.l_onoff = 0;
818 tmplinger.l_linger = 0;
819 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
820 sizeof(tmplinger));
821 }
822 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100823
Willy Tarreaue6b98942007-10-29 01:09:36 +0100824#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000825 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
826 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100827 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000828 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100829 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100830#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200831
Willy Tarreau40aa0702013-03-10 23:51:38 +0100832 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200833 switch (listener->addr.ss_family) {
834 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200835 if (1
836#if defined(IP_TRANSPARENT)
837 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
838#endif
839#if defined(IP_FREEBIND)
840 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
841#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200842#if defined(IP_BINDANY)
843 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
844#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200845#if defined(SO_BINDANY)
846 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
847#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200848 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200849 msg = "cannot make listening socket transparent";
850 err |= ERR_ALERT;
851 }
852 break;
853 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200854 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200855#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200856 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
857#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100858#if defined(IP_FREEBIND)
859 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
860#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200861#if defined(IPV6_BINDANY)
862 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
863#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200864#if defined(SO_BINDANY)
865 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
866#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200867 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200868 msg = "cannot make listening socket transparent";
869 err |= ERR_ALERT;
870 }
871 break;
872 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100873 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200874
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100875#ifdef SO_BINDTODEVICE
876 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100877 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100878 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100879 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100880 msg = "cannot bind listener to device";
881 err |= ERR_WARN;
882 }
883 }
884#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400885#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100886 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400887 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200888 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
889 msg = "cannot set MSS";
890 err |= ERR_WARN;
891 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200892 } else if (ext) {
893 int tmpmaxseg = -1;
894 int defaultmss;
895 socklen_t len = sizeof(tmpmaxseg);
896
897 if (listener->addr.ss_family == AF_INET)
898 defaultmss = default_tcp_maxseg;
899 else
900 defaultmss = default_tcp6_maxseg;
901
902 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100903 if (defaultmss > 0 &&
904 tmpmaxseg != defaultmss &&
905 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200906 msg = "cannot set MSS";
907 err |= ERR_WARN;
908 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200909 }
910#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100911#if defined(TCP_USER_TIMEOUT)
912 if (listener->tcp_ut) {
913 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
914 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
915 msg = "cannot set TCP User Timeout";
916 err |= ERR_WARN;
917 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200918 } else
919 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
920 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100921#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200922#if defined(TCP_DEFER_ACCEPT)
923 if (listener->options & LI_O_DEF_ACCEPT) {
924 /* defer accept by up to one second */
925 int accept_delay = 1;
926 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
927 msg = "cannot enable DEFER_ACCEPT";
928 err |= ERR_WARN;
929 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200930 } else
931 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
932 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200933#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200934#if defined(TCP_FASTOPEN)
935 if (listener->options & LI_O_TCP_FO) {
936 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100937 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200938 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
939 msg = "cannot enable TCP_FASTOPEN";
940 err |= ERR_WARN;
941 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200942 } else {
943 socklen_t len;
944 int qlen;
945 len = sizeof(qlen);
946 /* Only disable fast open if it was enabled, we don't want
947 * the kernel to create a fast open queue if there's none.
948 */
949 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
950 qlen != 0) {
951 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
952 sizeof(zero)) == -1) {
953 msg = "cannot disable TCP_FASTOPEN";
954 err |= ERR_WARN;
955 }
956 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200957 }
958#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100959#if defined(IPV6_V6ONLY)
960 if (listener->options & LI_O_V6ONLY)
961 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100962 else if (listener->options & LI_O_V4V6)
963 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100964#endif
965
Willy Tarreau40aa0702013-03-10 23:51:38 +0100966 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100967 err |= ERR_RETRYABLE | ERR_ALERT;
968 msg = "cannot bind socket";
969 goto tcp_close_return;
970 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100971
Willy Tarreau40aa0702013-03-10 23:51:38 +0100972 ready = 0;
973 ready_len = sizeof(ready);
974 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
975 ready = 0;
976
977 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100978 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100979 err |= ERR_RETRYABLE | ERR_ALERT;
980 msg = "cannot listen to socket";
981 goto tcp_close_return;
982 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100983
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400984#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200985 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900986 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +0200987 else
988 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200989#endif
990
Willy Tarreaue6b98942007-10-29 01:09:36 +0100991 /* the socket is ready */
992 listener->fd = fd;
993 listener->state = LI_LISTEN;
994
Willy Tarreaua9786b62018-01-25 07:22:13 +0100995 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100996 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaueb472682010-05-28 18:46:57 +0200997
Willy Tarreaue6b98942007-10-29 01:09:36 +0100998 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100999 if (msg && errlen) {
1000 char pn[INET6_ADDRSTRLEN];
1001
Willy Tarreau631f01c2011-09-05 00:36:48 +02001002 addr_to_str(&listener->addr, pn, sizeof(pn));
1003 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001004 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001005 return err;
1006
1007 tcp_close_return:
1008 close(fd);
1009 goto tcp_return;
1010}
1011
1012/* This function creates all TCP sockets bound to the protocol entry <proto>.
1013 * It is intended to be used as the protocol's bind_all() function.
1014 * The sockets will be registered but not added to any fd_set, in order not to
1015 * loose them across the fork(). A call to enable_all_listeners() is needed
1016 * to complete initialization. The return value is composed from ERR_*.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001017 *
1018 * Must be called with proto_lock held.
1019 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001020 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001021static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001022{
1023 struct listener *listener;
1024 int err = ERR_NONE;
1025
1026 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001027 err |= tcp_bind_listener(listener, errmsg, errlen);
1028 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001029 break;
1030 }
1031
1032 return err;
1033}
1034
Willy Tarreau32282382017-09-15 07:44:44 +02001035/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1036 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1037 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001038 *
1039 * Must be called with proto_lock held.
1040 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001041 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001042static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001043{
1044 if (listener->state != LI_INIT)
1045 return;
1046 listener->state = LI_ASSIGNED;
1047 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001048 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001049 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1050 proto_tcpv4.nb_listeners++;
1051}
1052
Willy Tarreau32282382017-09-15 07:44:44 +02001053/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1054 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1055 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001056 *
1057 * Must be called with proto_lock held.
1058 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001059 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001060static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001061{
1062 if (listener->state != LI_INIT)
1063 return;
1064 listener->state = LI_ASSIGNED;
1065 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001066 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001067 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1068 proto_tcpv6.nb_listeners++;
1069}
1070
Willy Tarreau092d8652014-07-07 20:22:12 +02001071/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1072 * was totally stopped, or > 0 if correctly paused.
1073 */
1074int tcp_pause_listener(struct listener *l)
1075{
1076 if (shutdown(l->fd, SHUT_WR) != 0)
1077 return -1; /* Solaris dies here */
1078
Willy Tarreaue2711c72019-02-27 15:39:41 +01001079 if (listen(l->fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +02001080 return -1; /* OpenBSD dies here */
1081
1082 if (shutdown(l->fd, SHUT_RD) != 0)
1083 return -1; /* should always be OK */
1084 return 1;
1085}
1086
William Lallemand2e785f22016-05-25 01:48:42 +02001087/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001088 * Execute the "set-src" action. May be called from {tcp,http}request.
1089 * It only changes the address and tries to preserve the original port. If the
1090 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001091 */
1092enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1093 struct session *sess, struct stream *s, int flags)
1094{
1095 struct connection *cli_conn;
1096
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001097 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand2e785f22016-05-25 01:48:42 +02001098 struct sample *smp;
1099
1100 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1101 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001102 int port = get_net_port(cli_conn->src);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001103
William Lallemand2e785f22016-05-25 01:48:42 +02001104 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001105 ((struct sockaddr_in *)cli_conn->src)->sin_family = AF_INET;
1106 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1107 ((struct sockaddr_in *)cli_conn->src)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001108 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001109 ((struct sockaddr_in6 *)cli_conn->src)->sin6_family = AF_INET6;
1110 memcpy(&((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1111 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001112 }
1113 }
William Lallemand01252ed2016-05-25 02:33:16 +02001114 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001115 }
1116 return ACT_RET_CONT;
1117}
1118
William Lallemand44be6402016-05-25 01:51:35 +02001119/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001120 * Execute the "set-dst" action. May be called from {tcp,http}request.
1121 * It only changes the address and tries to preserve the original port. If the
1122 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001123 */
1124enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1125 struct session *sess, struct stream *s, int flags)
1126{
1127 struct connection *cli_conn;
1128
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001129 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001130 struct sample *smp;
1131
1132 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1133 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001134 int port = get_net_port(cli_conn->dst);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001135
William Lallemand13e9b0c2016-05-25 02:34:07 +02001136 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001137 ((struct sockaddr_in *)cli_conn->dst)->sin_family = AF_INET;
1138 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001139 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001140 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_family = AF_INET6;
1141 memcpy(&((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1142 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001143 }
1144 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1145 }
1146 }
1147 return ACT_RET_CONT;
1148}
1149
1150/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001151 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1152 * We must test the sin_family before setting the port. If the address family
1153 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1154 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001155 */
1156enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1157 struct session *sess, struct stream *s, int flags)
1158{
1159 struct connection *cli_conn;
1160
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001161 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand44be6402016-05-25 01:51:35 +02001162 struct sample *smp;
1163
William Lallemand44be6402016-05-25 01:51:35 +02001164 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1165 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001166 if (cli_conn->src->ss_family == AF_INET6) {
1167 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001168 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001169 if (cli_conn->src->ss_family != AF_INET) {
1170 cli_conn->src->ss_family = AF_INET;
1171 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001172 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001173 ((struct sockaddr_in *)cli_conn->src)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001174 }
1175 }
1176 }
1177 return ACT_RET_CONT;
1178}
1179
William Lallemand13e9b0c2016-05-25 02:34:07 +02001180/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001181 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1182 * We must test the sin_family before setting the port. If the address family
1183 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1184 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001185 */
1186enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1187 struct session *sess, struct stream *s, int flags)
1188{
1189 struct connection *cli_conn;
1190
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001191 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001192 struct sample *smp;
1193
William Lallemand13e9b0c2016-05-25 02:34:07 +02001194 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1195 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001196 if (cli_conn->dst->ss_family == AF_INET6) {
1197 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001198 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001199 if (cli_conn->dst->ss_family != AF_INET) {
1200 cli_conn->dst->ss_family = AF_INET;
1201 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001202 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001203 ((struct sockaddr_in *)cli_conn->dst)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001204 }
1205 }
1206 }
1207 return ACT_RET_CONT;
1208}
1209
Willy Tarreau2d392c22015-08-24 01:43:45 +02001210/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1211static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1212{
1213 struct connection *conn = objt_conn(sess->origin);
1214
1215 if (!conn)
1216 goto out;
1217
1218 if (!conn_ctrl_ready(conn))
1219 goto out;
1220
Willy Tarreau2d392c22015-08-24 01:43:45 +02001221#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001222 /* drain is needed only to send the quick ACK */
1223 conn_sock_drain(conn);
1224
Willy Tarreau2d392c22015-08-24 01:43:45 +02001225 /* re-enable quickack if it was disabled to ack all data and avoid
1226 * retransmits from the client that might trigger a real reset.
1227 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001228 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001229#endif
1230 /* lingering must absolutely be disabled so that we don't send a
1231 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1232 * is present, returning with ERR will cause lingering to be disabled.
1233 */
1234 if (strm)
1235 strm->si[0].flags |= SI_FL_NOLINGER;
1236
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001237 /* We're on the client-facing side, we must force to disable lingering to
1238 * ensure we will use an RST exclusively and kill any pending data.
1239 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001240 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001241
Willy Tarreau2d392c22015-08-24 01:43:45 +02001242#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001243 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001244 /* socket will be quiet now */
1245 goto out;
1246 }
1247#endif
1248 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1249 * Let's fall back on the TTL trick, though it only works for routed
1250 * network and has no effect on local net.
1251 */
1252#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001253 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001254#endif
1255 out:
1256 /* kill the stream if any */
1257 if (strm) {
1258 channel_abort(&strm->req);
1259 channel_abort(&strm->res);
Christopher Faulet177f4802020-03-06 15:10:46 +01001260 strm->req.analysers &= AN_REQ_FLT_END;
1261 strm->res.analysers &= AN_RES_FLT_END;
Christopher Fauletddc005a2020-03-06 15:23:18 +01001262 if (strm->flags & SF_BE_ASSIGNED)
1263 _HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001264 if (!(strm->flags & SF_ERR_MASK))
1265 strm->flags |= SF_ERR_PRXCOND;
1266 if (!(strm->flags & SF_FINST_MASK))
1267 strm->flags |= SF_FINST_R;
1268 }
1269
Olivier Houchard40514102019-03-08 18:54:04 +01001270 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001271 if (sess->listener->counters)
Olivier Houchard40514102019-03-08 18:54:04 +01001272 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001273
Christopher Fauletf573ba22020-03-06 15:15:49 +01001274 return ACT_RET_ABRT;
Willy Tarreau2d392c22015-08-24 01:43:45 +02001275}
1276
William Lallemand13e9b0c2016-05-25 02:34:07 +02001277/* parse "set-{src,dst}[-port]" action */
1278enum 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 +02001279{
1280 int cur_arg;
1281 struct sample_expr *expr;
1282 unsigned int where;
1283
1284 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001285 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 +02001286 if (!expr)
1287 return ACT_RET_PRS_ERR;
1288
1289 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001290 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001291 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001292 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001293 where |= SMP_VAL_BE_HRQ_HDR;
1294
1295 if (!(expr->fetch->val & where)) {
1296 memprintf(err,
1297 "fetch method '%s' extracts information from '%s', none of which is available here",
1298 args[cur_arg-1], sample_src_names(expr->fetch->use));
1299 free(expr);
1300 return ACT_RET_PRS_ERR;
1301 }
1302 rule->arg.expr = expr;
1303 rule->action = ACT_CUSTOM;
1304
1305 if (!strcmp(args[*orig_arg-1], "set-src")) {
1306 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001307 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1308 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001309 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1310 rule->action_ptr = tcp_action_req_set_dst;
1311 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1312 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001313 } else {
1314 return ACT_RET_PRS_ERR;
1315 }
1316
1317 (*orig_arg)++;
1318
1319 return ACT_RET_PRS_OK;
1320}
1321
1322
Willy Tarreau2d392c22015-08-24 01:43:45 +02001323/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1324 * success, ACT_RET_PRS_ERR on error.
1325 */
1326static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1327 struct act_rule *rule, char **err)
1328{
1329 rule->action = ACT_CUSTOM;
1330 rule->action_ptr = tcp_exec_action_silent_drop;
1331 return ACT_RET_PRS_OK;
1332}
1333
Willy Tarreau645513a2010-05-24 20:55:15 +02001334
1335/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001336/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001337/************************************************************************/
1338
Willy Tarreau4a129812012-04-25 17:31:42 +02001339/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001340int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001341{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001342 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001343
1344 if (!cli_conn)
1345 return 0;
1346
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001347 if (!conn_get_src(cli_conn))
1348 return 0;
1349
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001350 switch (cli_conn->src->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001351 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001352 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->src)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001353 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001354 break;
1355 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001356 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->src)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001357 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001358 break;
1359 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001360 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001361 }
Emeric Brunf769f512010-10-22 17:14:01 +02001362
Willy Tarreau37406352012-04-23 16:16:37 +02001363 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001364 return 1;
1365}
1366
Willy Tarreaua5e37562011-12-16 17:06:15 +01001367/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001368static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001369smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001370{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001371 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001372
1373 if (!cli_conn)
1374 return 0;
1375
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001376 if (!conn_get_src(cli_conn))
1377 return 0;
1378
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001379 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001380 if (!(smp->data.u.sint = get_host_port(cli_conn->src)))
Emeric Brunf769f512010-10-22 17:14:01 +02001381 return 0;
1382
Willy Tarreau37406352012-04-23 16:16:37 +02001383 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001384 return 1;
1385}
1386
Willy Tarreau4a129812012-04-25 17:31:42 +02001387/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001388static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001389smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001390{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001391 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001392
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001393 if (!cli_conn)
1394 return 0;
1395
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001396 if (!conn_get_dst(cli_conn))
1397 return 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001398
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001399 switch (cli_conn->dst->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001400 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001401 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001402 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001403 break;
1404 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001405 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001406 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001407 break;
1408 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001409 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001410 }
Emeric Brunf769f512010-10-22 17:14:01 +02001411
Willy Tarreau37406352012-04-23 16:16:37 +02001412 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001413 return 1;
1414}
1415
Willy Tarreau16e01562016-08-09 16:46:18 +02001416/* check if the destination address of the front connection is local to the
1417 * system or if it was intercepted.
1418 */
1419int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1420{
1421 struct connection *conn = objt_conn(smp->sess->origin);
1422 struct listener *li = smp->sess->listener;
1423
1424 if (!conn)
1425 return 0;
1426
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001427 if (!conn_get_dst(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001428 return 0;
1429
1430 smp->data.type = SMP_T_BOOL;
1431 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001432 smp->data.u.sint = addr_is_local(li->netns, conn->dst);
Willy Tarreau16e01562016-08-09 16:46:18 +02001433 return smp->data.u.sint >= 0;
1434}
1435
1436/* check if the source address of the front connection is local to the system
1437 * or not.
1438 */
1439int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1440{
1441 struct connection *conn = objt_conn(smp->sess->origin);
1442 struct listener *li = smp->sess->listener;
1443
1444 if (!conn)
1445 return 0;
1446
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001447 if (!conn_get_src(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001448 return 0;
1449
1450 smp->data.type = SMP_T_BOOL;
1451 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001452 smp->data.u.sint = addr_is_local(li->netns, conn->src);
Willy Tarreau16e01562016-08-09 16:46:18 +02001453 return smp->data.u.sint >= 0;
1454}
1455
Willy Tarreaua5e37562011-12-16 17:06:15 +01001456/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001457static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001458smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001459{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001460 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001461
1462 if (!cli_conn)
1463 return 0;
1464
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001465 if (!conn_get_dst(cli_conn))
1466 return 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001467
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001468 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001469 if (!(smp->data.u.sint = get_host_port(cli_conn->dst)))
Emeric Brunf769f512010-10-22 17:14:01 +02001470 return 0;
1471
Willy Tarreau37406352012-04-23 16:16:37 +02001472 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001473 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001474}
1475
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001476#ifdef TCP_INFO
1477
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001478
1479/* Validates the arguments passed to "fc_*" fetch keywords returning a time
1480 * value. These keywords support an optional string representing the unit of the
1481 * result: "us" for microseconds and "ms" for milliseconds". Returns 0 on error
1482 * and non-zero if OK.
1483 */
1484static int val_fc_time_value(struct arg *args, char **err)
1485{
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001486 if (args[0].type == ARGT_STR) {
1487 if (strcmp(args[0].data.str.area, "us") == 0) {
1488 free(args[0].data.str.area);
1489 args[0].type = ARGT_SINT;
1490 args[0].data.sint = TIME_UNIT_US;
1491 }
1492 else if (strcmp(args[0].data.str.area, "ms") == 0) {
1493 free(args[0].data.str.area);
1494 args[0].type = ARGT_SINT;
1495 args[0].data.sint = TIME_UNIT_MS;
1496 }
1497 else {
1498 memprintf(err, "expects 'us' or 'ms', got '%s'",
1499 args[0].data.str.area);
1500 return 0;
1501 }
1502 }
1503 else {
1504 memprintf(err, "Unexpected arg type");
1505 return 0;
1506 }
1507
1508 return 1;
1509}
1510
1511/* Validates the arguments passed to "fc_*" fetch keywords returning a
1512 * counter. These keywords should be used without any keyword, but because of a
1513 * bug in previous versions, an optional string argument may be passed. In such
1514 * case, the argument is ignored and a warning is emitted. Returns 0 on error
1515 * and non-zero if OK.
1516 */
1517static int var_fc_counter(struct arg *args, char **err)
1518{
1519 if (args[0].type != ARGT_STOP) {
1520 ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n");
1521 if (args[0].type == ARGT_STR)
1522 free(args[0].data.str.area);
1523 args[0].type = ARGT_STOP;
1524 }
1525
1526 return 1;
1527}
1528
Joseph Herlanta6331472018-11-25 12:59:12 -08001529/* Returns some tcp_info data if it's available. "dir" must be set to 0 if
1530 * the client connection is required, otherwise it is set to 1. "val" represents
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001531 * the required value.
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001532 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1533 */
1534static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1535 int dir, int val)
1536{
1537 struct connection *conn;
1538 struct tcp_info info;
1539 socklen_t optlen;
1540
1541 /* strm can be null. */
1542 if (!smp->strm)
1543 return 0;
1544
1545 /* get the object associated with the stream interface.The
1546 * object can be other thing than a connection. For example,
1547 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001548 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001549 if (!conn)
1550 return 0;
1551
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001552 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001553 syscal can fail. */
1554 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001555 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001556 return 0;
1557
1558 /* extract the value. */
1559 smp->data.type = SMP_T_SINT;
1560 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001561 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1562 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1563#if defined(__linux__)
1564 /* these ones are common to all Linux versions */
1565 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1566 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1567 case 4: smp->data.u.sint = info.tcpi_lost; break;
1568 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1569 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1570 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1571#elif defined(__FreeBSD__) || defined(__NetBSD__)
1572 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1573 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1574 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1575 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1576 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1577 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1578 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1579#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001580 default: return 0;
1581 }
1582
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001583 return 1;
1584}
1585
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001586/* get the mean rtt of a client connection */
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001587static int
1588smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1589{
1590 if (!get_tcp_info(args, smp, 0, 0))
1591 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001592
1593 /* By default or if explicitly specified, convert rtt to ms */
1594 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1595 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1596
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001597 return 1;
1598}
1599
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001600/* get the variance of the mean rtt of a client connection */
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001601static int
1602smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1603{
1604 if (!get_tcp_info(args, smp, 0, 1))
1605 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001606
1607 /* By default or if explicitly specified, convert rttvar to ms */
1608 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1609 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1610
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001611 return 1;
1612}
Joe Williams30fcd392016-08-10 07:06:44 -07001613
1614#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1615
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001616/* get the unacked counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001617static int
1618smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1619{
1620 if (!get_tcp_info(args, smp, 0, 2))
1621 return 0;
1622 return 1;
1623}
1624
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001625/* get the sacked counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001626static int
1627smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1628{
1629 if (!get_tcp_info(args, smp, 0, 3))
1630 return 0;
1631 return 1;
1632}
1633
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001634/* get the lost counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001635static int
1636smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1637{
1638 if (!get_tcp_info(args, smp, 0, 4))
1639 return 0;
1640 return 1;
1641}
1642
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001643/* get the retrans counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001644static int
1645smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1646{
1647 if (!get_tcp_info(args, smp, 0, 5))
1648 return 0;
1649 return 1;
1650}
1651
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001652/* get the fackets counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001653static int
1654smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1655{
1656 if (!get_tcp_info(args, smp, 0, 6))
1657 return 0;
1658 return 1;
1659}
1660
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001661/* get the reordering counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001662static int
1663smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1664{
1665 if (!get_tcp_info(args, smp, 0, 7))
1666 return 0;
1667 return 1;
1668}
1669#endif // linux || freebsd || netbsd
1670#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001671
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001672#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001673/* parse the "v4v6" bind keyword */
1674static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1675{
1676 struct listener *l;
1677
1678 list_for_each_entry(l, &conf->listeners, by_bind) {
1679 if (l->addr.ss_family == AF_INET6)
1680 l->options |= LI_O_V4V6;
1681 }
1682
1683 return 0;
1684}
1685
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001686/* parse the "v6only" bind keyword */
1687static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1688{
1689 struct listener *l;
1690
1691 list_for_each_entry(l, &conf->listeners, by_bind) {
1692 if (l->addr.ss_family == AF_INET6)
1693 l->options |= LI_O_V6ONLY;
1694 }
1695
1696 return 0;
1697}
1698#endif
1699
Pieter Baauwd551fb52013-05-08 22:49:23 +02001700#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001701/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001702static 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 +02001703{
1704 struct listener *l;
1705
Willy Tarreau4348fad2012-09-20 16:48:07 +02001706 list_for_each_entry(l, &conf->listeners, by_bind) {
1707 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1708 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001709 }
1710
Willy Tarreau44791242012-09-12 23:27:21 +02001711 return 0;
1712}
1713#endif
1714
1715#ifdef TCP_DEFER_ACCEPT
1716/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001717static 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 +02001718{
1719 struct listener *l;
1720
Willy Tarreau4348fad2012-09-20 16:48:07 +02001721 list_for_each_entry(l, &conf->listeners, by_bind) {
1722 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1723 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001724 }
1725
Willy Tarreau44791242012-09-12 23:27:21 +02001726 return 0;
1727}
1728#endif
1729
Willy Tarreau1c862c52012-10-05 16:21:00 +02001730#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001731/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001732static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1733{
1734 struct listener *l;
1735
1736 list_for_each_entry(l, &conf->listeners, by_bind) {
1737 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1738 l->options |= LI_O_TCP_FO;
1739 }
1740
1741 return 0;
1742}
1743#endif
1744
Willy Tarreau44791242012-09-12 23:27:21 +02001745#ifdef TCP_MAXSEG
1746/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001747static 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 +02001748{
1749 struct listener *l;
1750 int mss;
1751
Willy Tarreau44791242012-09-12 23:27:21 +02001752 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001753 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001754 return ERR_ALERT | ERR_FATAL;
1755 }
1756
1757 mss = atoi(args[cur_arg + 1]);
1758 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001759 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001760 return ERR_ALERT | ERR_FATAL;
1761 }
1762
Willy Tarreau4348fad2012-09-20 16:48:07 +02001763 list_for_each_entry(l, &conf->listeners, by_bind) {
1764 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1765 l->maxseg = mss;
1766 }
Willy Tarreau44791242012-09-12 23:27:21 +02001767
1768 return 0;
1769}
1770#endif
1771
Willy Tarreau2af207a2015-02-04 00:45:58 +01001772#ifdef TCP_USER_TIMEOUT
1773/* parse the "tcp-ut" bind keyword */
1774static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1775{
1776 const char *ptr = NULL;
1777 struct listener *l;
1778 unsigned int timeout;
1779
1780 if (!*args[cur_arg + 1]) {
1781 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1782 return ERR_ALERT | ERR_FATAL;
1783 }
1784
1785 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001786 if (ptr == PARSE_TIME_OVER) {
1787 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1788 args[cur_arg+1], args[cur_arg]);
1789 return ERR_ALERT | ERR_FATAL;
1790 }
1791 else if (ptr == PARSE_TIME_UNDER) {
1792 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1793 args[cur_arg+1], args[cur_arg]);
1794 return ERR_ALERT | ERR_FATAL;
1795 }
1796 else if (ptr) {
Willy Tarreau2af207a2015-02-04 00:45:58 +01001797 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1798 return ERR_ALERT | ERR_FATAL;
1799 }
1800
1801 list_for_each_entry(l, &conf->listeners, by_bind) {
1802 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1803 l->tcp_ut = timeout;
1804 }
1805
1806 return 0;
1807}
1808#endif
1809
Willy Tarreau44791242012-09-12 23:27:21 +02001810#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001811/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001812static 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 +02001813{
1814 struct listener *l;
1815
Willy Tarreau44791242012-09-12 23:27:21 +02001816 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001817 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001818 return ERR_ALERT | ERR_FATAL;
1819 }
1820
Willy Tarreau4348fad2012-09-20 16:48:07 +02001821 list_for_each_entry(l, &conf->listeners, by_bind) {
1822 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1823 l->interface = strdup(args[cur_arg + 1]);
1824 }
Willy Tarreau44791242012-09-12 23:27:21 +02001825
Willy Tarreau44791242012-09-12 23:27:21 +02001826 return 0;
1827}
1828#endif
1829
Willy Tarreaue5733232019-05-22 19:24:06 +02001830#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001831/* parse the "namespace" bind keyword */
1832static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1833{
1834 struct listener *l;
1835 char *namespace = NULL;
1836
1837 if (!*args[cur_arg + 1]) {
1838 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1839 return ERR_ALERT | ERR_FATAL;
1840 }
1841 namespace = args[cur_arg + 1];
1842
1843 list_for_each_entry(l, &conf->listeners, by_bind) {
1844 l->netns = netns_store_lookup(namespace, strlen(namespace));
1845
1846 if (l->netns == NULL)
1847 l->netns = netns_store_insert(namespace);
1848
1849 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001850 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001851 return ERR_ALERT | ERR_FATAL;
1852 }
1853 }
1854 return 0;
1855}
1856#endif
1857
Willy Tarreau163d4622015-10-13 16:16:41 +02001858#ifdef TCP_USER_TIMEOUT
1859/* parse the "tcp-ut" server keyword */
1860static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1861{
1862 const char *ptr = NULL;
1863 unsigned int timeout;
1864
1865 if (!*args[*cur_arg + 1]) {
1866 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1867 return ERR_ALERT | ERR_FATAL;
1868 }
1869
1870 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001871 if (ptr == PARSE_TIME_OVER) {
1872 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1873 args[*cur_arg+1], args[*cur_arg]);
1874 return ERR_ALERT | ERR_FATAL;
1875 }
1876 else if (ptr == PARSE_TIME_UNDER) {
1877 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1878 args[*cur_arg+1], args[*cur_arg]);
1879 return ERR_ALERT | ERR_FATAL;
1880 }
1881 else if (ptr) {
Willy Tarreau163d4622015-10-13 16:16:41 +02001882 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1883 return ERR_ALERT | ERR_FATAL;
1884 }
1885
1886 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1887 newsrv->tcp_ut = timeout;
1888
1889 return 0;
1890}
1891#endif
1892
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001893
Willy Tarreau4a129812012-04-25 17:31:42 +02001894/* Note: must not be declared <const> as its list will be overwritten.
1895 * Note: fetches that may return multiple types must be declared as the lowest
1896 * common denominator, the type that can be casted into all other ones. For
1897 * instance v4/v6 must be declared v4.
1898 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001899static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001900 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001901 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001902 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001903 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001904 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001905 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001906#ifdef TCP_INFO
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001907 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
1908 { "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 -07001909#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001910 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1911 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1912 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1913 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1914 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1915 { "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 -07001916#endif // linux || freebsd || netbsd
1917#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001918 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001919}};
1920
Willy Tarreau0108d902018-11-25 19:14:37 +01001921INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1922
Willy Tarreau44791242012-09-12 23:27:21 +02001923/************************************************************************/
1924/* All supported bind keywords must be declared here. */
1925/************************************************************************/
1926
1927/* Note: must not be declared <const> as its list will be overwritten.
1928 * Please take care of keeping this list alphabetically sorted, doing so helps
1929 * all code contributors.
1930 * Optional keywords are also declared with a NULL ->parse() function so that
1931 * the config parser can report an appropriate error when a known keyword was
1932 * not enabled.
1933 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001934static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001935#ifdef TCP_DEFER_ACCEPT
1936 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1937#endif
1938#ifdef SO_BINDTODEVICE
1939 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1940#endif
1941#ifdef TCP_MAXSEG
1942 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1943#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001944#ifdef TCP_USER_TIMEOUT
1945 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1946#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001947#ifdef TCP_FASTOPEN
1948 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1949#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001950#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001951 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1952#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001953#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001954 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001955 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1956#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001957#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001958 { "namespace", bind_parse_namespace, 1 },
1959#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001960 /* the versions with the NULL parse function*/
1961 { "defer-accept", NULL, 0 },
1962 { "interface", NULL, 1 },
1963 { "mss", NULL, 1 },
1964 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001965 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001966 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001967 { NULL, NULL, 0 },
1968}};
1969
Willy Tarreau0108d902018-11-25 19:14:37 +01001970INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1971
Willy Tarreau163d4622015-10-13 16:16:41 +02001972static struct srv_kw_list srv_kws = { "TCP", { }, {
1973#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001974 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02001975#endif
1976 { NULL, NULL, 0 },
1977}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02001978
Willy Tarreau0108d902018-11-25 19:14:37 +01001979INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
1980
Willy Tarreau2d392c22015-08-24 01:43:45 +02001981static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001982 { "set-src", tcp_parse_set_src_dst },
1983 { "set-src-port", tcp_parse_set_src_dst },
1984 { "set-dst" , tcp_parse_set_src_dst },
1985 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02001986 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02001987 { /* END */ }
1988}};
1989
Willy Tarreau0108d902018-11-25 19:14:37 +01001990INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions);
1991
Willy Tarreau620408f2016-10-21 16:37:51 +02001992static struct action_kw_list tcp_req_sess_actions = {ILH, {
Willy Tarreau620408f2016-10-21 16:37:51 +02001993 { "set-src", tcp_parse_set_src_dst },
1994 { "set-src-port", tcp_parse_set_src_dst },
1995 { "set-dst" , tcp_parse_set_src_dst },
1996 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02001997 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau620408f2016-10-21 16:37:51 +02001998 { /* END */ }
1999}};
2000
Willy Tarreau0108d902018-11-25 19:14:37 +01002001INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions);
2002
Willy Tarreau2d392c22015-08-24 01:43:45 +02002003static struct action_kw_list tcp_req_cont_actions = {ILH, {
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002004 { "set-dst" , tcp_parse_set_src_dst },
2005 { "set-dst-port", tcp_parse_set_src_dst },
2006 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002007 { /* END */ }
2008}};
2009
Willy Tarreau0108d902018-11-25 19:14:37 +01002010INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2011
Willy Tarreau2d392c22015-08-24 01:43:45 +02002012static struct action_kw_list tcp_res_cont_actions = {ILH, {
2013 { "silent-drop", tcp_parse_silent_drop },
2014 { /* END */ }
2015}};
2016
Willy Tarreau0108d902018-11-25 19:14:37 +01002017INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
2018
Willy Tarreau2d392c22015-08-24 01:43:45 +02002019static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002020 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002021 { "set-src", tcp_parse_set_src_dst },
2022 { "set-src-port", tcp_parse_set_src_dst },
2023 { "set-dst", tcp_parse_set_src_dst },
2024 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002025 { /* END */ }
2026}};
2027
Willy Tarreau0108d902018-11-25 19:14:37 +01002028INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2029
Willy Tarreau2d392c22015-08-24 01:43:45 +02002030static struct action_kw_list http_res_actions = {ILH, {
2031 { "silent-drop", tcp_parse_silent_drop },
2032 { /* END */ }
2033}};
2034
Willy Tarreau0108d902018-11-25 19:14:37 +01002035INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002036
Willy Tarreau80713382018-11-26 10:19:54 +01002037REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
Willy Tarreauba962912016-12-21 18:55:02 +01002038#if defined(IP_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002039 " IP_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002040#endif
2041#if defined(IPV6_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002042 " IPV6_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002043#endif
2044#if defined(IP_FREEBIND)
Willy Tarreau80713382018-11-26 10:19:54 +01002045 " IP_FREEBIND"
Willy Tarreauba962912016-12-21 18:55:02 +01002046#endif
2047#if defined(IP_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002048 " IP_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002049#endif
2050#if defined(IPV6_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002051 " IPV6_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002052#endif
2053#if defined(SO_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002054 " SO_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002055#endif
Willy Tarreau80713382018-11-26 10:19:54 +01002056 "");
Willy Tarreaue6b98942007-10-29 01:09:36 +01002057
2058
2059/*
2060 * Local variables:
2061 * c-indent-level: 8
2062 * c-basic-offset: 8
2063 * End:
2064 */