blob: f2bc3de224f28235b3236bf1709dd2ec8c40959e [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
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900382 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 Tarreau52543212020-07-09 05:58:51 +0200385#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900386 if (be->srvtcpka_cnt)
387 setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt));
Willy Tarreau52543212020-07-09 05:58:51 +0200388#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900389
Willy Tarreau52543212020-07-09 05:58:51 +0200390#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900391 if (be->srvtcpka_idle)
392 setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle));
Willy Tarreau52543212020-07-09 05:58:51 +0200393#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900394
Willy Tarreau52543212020-07-09 05:58:51 +0200395#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900396 if (be->srvtcpka_intvl)
397 setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl));
Willy Tarreau52543212020-07-09 05:58:51 +0200398#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900399 }
400
Willy Tarreau9650f372009-08-16 14:02:45 +0200401 /* allow specific binding :
402 * - server-specific at first
403 * - proxy-specific next
404 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100405 if (srv && srv->conn_src.opts & CO_SRC_BIND)
406 src = &srv->conn_src;
407 else if (be->conn_src.opts & CO_SRC_BIND)
408 src = &be->conn_src;
409 else
410 src = NULL;
411
412 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200413 int ret, flags = 0;
414
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200415 if (conn->src && is_inet_addr(conn->src)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100416 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100417 case CO_SRC_TPROXY_CLI:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200418 conn_set_private(conn);
Willy Tarreau387ebf82015-08-04 19:24:13 +0200419 /* fall through */
420 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200421 flags = 3;
422 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100423 case CO_SRC_TPROXY_CIP:
424 case CO_SRC_TPROXY_DYN:
Christopher Faulet21ddc742020-07-01 15:26:14 +0200425 conn_set_private(conn);
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200426 flags = 1;
427 break;
428 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200429 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200430
Willy Tarreau9650f372009-08-16 14:02:45 +0200431#ifdef SO_BINDTODEVICE
432 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100433 if (src->iface_name)
434 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200435#endif
436
Willy Tarreaua4380b42012-12-08 22:49:11 +0100437 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100439 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200440
441 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200442 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200443
444 do {
445 /* note: in case of retry, we may have to release a previously
446 * allocated port, hence this loop's construct.
447 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200448 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
449 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200450
451 if (!attempts)
452 break;
453 attempts--;
454
Willy Tarreaua4380b42012-12-08 22:49:11 +0100455 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100456 if (!fdinfo[fd].local_port) {
457 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100459 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200460
Willy Tarreaua4380b42012-12-08 22:49:11 +0100461 fdinfo[fd].port_range = src->sport_range;
462 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200463
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200464 ret = tcp_bind_socket(fd, flags, &sa, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100465 if (ret != 0)
466 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200467 } while (ret != 0); /* binding NOK */
468 }
469 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000470#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100471 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000472 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
473#endif
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200474 ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100475 if (ret != 0)
476 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200477 }
478
Willy Tarreaua4380b42012-12-08 22:49:11 +0100479 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200480 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
481 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200482 close(fd);
483
Willy Tarreau9650f372009-08-16 14:02:45 +0200484 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100485 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
486 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200487 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100488 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200489 be->id);
490 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100491 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
492 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200493 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100494 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200495 be->id);
496 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100497 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200498 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200499 }
500 }
501
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400502#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200503 /* disabling tcp quick ack now allows the first request to leave the
504 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100505 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200506 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200507 if (flags & (CONNECT_DELACK_ALWAYS) ||
508 ((flags & CONNECT_DELACK_SMART_CONNECT ||
509 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
510 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900511 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200512#endif
513
Willy Tarreau163d4622015-10-13 16:16:41 +0200514#ifdef TCP_USER_TIMEOUT
515 /* there is not much more we can do here when it fails, it's still minor */
516 if (srv && srv->tcp_ut)
517 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
518#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100519
520 if (use_fastopen) {
521#if defined(TCP_FASTOPEN_CONNECT)
522 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
523#endif
524 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100525 if (global.tune.server_sndbuf)
526 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
527
528 if (global.tune.server_rcvbuf)
529 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
530
Willy Tarreau7bbc4a52019-07-17 15:41:35 +0200531 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800532 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100533 if (errno == EINPROGRESS || errno == EALREADY) {
534 /* common case, let's wait for connect status */
535 conn->flags |= CO_FL_WAIT_L4_CONN;
536 }
537 else if (errno == EISCONN) {
538 /* should normally not happen but if so, indicates that it's OK */
539 conn->flags &= ~CO_FL_WAIT_L4_CONN;
540 }
541 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200542 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100543 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200544 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100545 conn->err_code = CO_ER_FREE_PORTS;
546 }
547 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200548 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100549 conn->err_code = CO_ER_ADDR_INUSE;
550 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200551
Willy Tarreaub1719512012-12-08 23:03:28 +0100552 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200553 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
554 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200555 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100556 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100557 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200558 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200559 } else if (errno == ETIMEDOUT) {
560 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200561 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
562 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200563 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100564 conn->err_code = CO_ER_SOCK_ERR;
565 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200566 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200567 } else {
568 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
569 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200570 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
571 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200572 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100573 conn->err_code = CO_ER_SOCK_ERR;
574 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200575 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200576 }
577 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100578 else {
579 /* connect() == 0, this is great! */
580 conn->flags &= ~CO_FL_WAIT_L4_CONN;
581 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200582
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100583 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200584
Willy Tarreauf79c8172013-10-21 16:30:56 +0200585 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100586 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200587
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100588 if (conn->flags & CO_FL_WAIT_L4_CONN) {
589 fd_want_send(fd);
590 fd_cant_send(fd);
591 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200592
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200593 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200594 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100595 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200596 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200597 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200598
Willy Tarreaue7dff022015-04-03 01:14:29 +0200599 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200600}
601
602
Willy Tarreau59b94792012-05-11 16:16:40 +0200603/*
604 * Retrieves the source address for the socket <fd>, with <dir> indicating
605 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
606 * success, -1 in case of error. The socket's source address is stored in
607 * <sa> for <salen> bytes.
608 */
609int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
610{
611 if (dir)
612 return getsockname(fd, sa, &salen);
613 else
614 return getpeername(fd, sa, &salen);
615}
616
617
618/*
619 * Retrieves the original destination address for the socket <fd>, with <dir>
620 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
621 * listener, if the original destination address was translated, the original
622 * address is retrieved. It returns 0 in case of success, -1 in case of error.
623 * The socket's source address is stored in <sa> for <salen> bytes.
624 */
625int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
626{
627 if (dir)
628 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100629 else {
630 int ret = getsockname(fd, sa, &salen);
631
632 if (ret < 0)
633 return ret;
634
Willy Tarreaue5733232019-05-22 19:24:06 +0200635#if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100636 /* For TPROXY and Netfilter's NAT, we can retrieve the original
637 * IPv4 address before DNAT/REDIRECT. We must not do that with
638 * other families because v6-mapped IPv4 addresses are still
639 * reported as v4.
640 */
641 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
642 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
643 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200644#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100645 return ret;
646 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200647}
648
Olivier Houchard153659f2017-04-05 22:39:56 +0200649/* XXX: Should probably be elsewhere */
650static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
651{
652 if (a->ss_family != b->ss_family) {
653 return (-1);
654 }
655 switch (a->ss_family) {
656 case AF_INET:
657 {
658 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
659 if (a4->sin_port != b4->sin_port)
660 return (-1);
661 return (memcmp(&a4->sin_addr, &b4->sin_addr,
662 sizeof(a4->sin_addr)));
663 }
664 case AF_INET6:
665 {
666 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
667 if (a6->sin6_port != b6->sin6_port)
668 return (-1);
669 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
670 sizeof(a6->sin6_addr)));
671 }
672 default:
673 return (-1);
674 }
675
676}
677
678#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
679/* When binding the listeners, check if a socket has been sent to us by the
680 * previous process that we could reuse, instead of creating a new one.
681 */
682static int tcp_find_compatible_fd(struct listener *l)
683{
684 struct xfer_sock_list *xfer_sock = xfer_sock_list;
685 int ret = -1;
686
687 while (xfer_sock) {
688 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
689 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
690 (l->interface != NULL && xfer_sock->iface != NULL &&
691 !strcmp(l->interface, xfer_sock->iface))) {
692 if ((l->options & LI_MANDATORY_FLAGS) ==
693 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
694 if ((xfer_sock->namespace == NULL &&
695 l->netns == NULL)
Willy Tarreaue5733232019-05-22 19:24:06 +0200696#ifdef USE_NS
Olivier Houchard153659f2017-04-05 22:39:56 +0200697 || (xfer_sock->namespace != NULL &&
698 l->netns != NULL &&
699 !strcmp(xfer_sock->namespace,
700 l->netns->node.key))
701#endif
702 ) {
703 break;
704 }
705
706 }
707 }
708 }
709 xfer_sock = xfer_sock->next;
710 }
711 if (xfer_sock != NULL) {
712 ret = xfer_sock->fd;
713 if (xfer_sock == xfer_sock_list)
714 xfer_sock_list = xfer_sock->next;
715 if (xfer_sock->prev)
716 xfer_sock->prev->next = xfer_sock->next;
717 if (xfer_sock->next)
718 xfer_sock->next->prev = xfer_sock->prev;
719 free(xfer_sock->iface);
720 free(xfer_sock->namespace);
721 free(xfer_sock);
722 }
723 return ret;
724}
725#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200726
Willy Tarreaue6b98942007-10-29 01:09:36 +0100727/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100728 * an error message in <errmsg> if the message is at most <errlen> bytes long
729 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
730 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100731 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
732 * was alright and that no message was returned. ERR_RETRYABLE means that an
733 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700734 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100735 * the meaning of the error, but just indicate that a message is present which
736 * should be displayed with the respective level. Last, ERR_ABORT indicates
737 * that it's pointless to try to start other listeners. No error message is
738 * returned if errlen is NULL.
739 */
740int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
741{
742 __label__ tcp_return, tcp_close_return;
743 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100744 int ext, ready;
745 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100746 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200747#ifdef TCP_MAXSEG
748
749 /* Create a temporary TCP socket to get default parameters we can't
750 * guess.
751 * */
752 ready_len = sizeof(default_tcp_maxseg);
753 if (default_tcp_maxseg == -1) {
754 default_tcp_maxseg = -2;
755 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
756 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100757 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200758 else {
759 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
760 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100761 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
William Dauchyc0e23ae2020-02-12 10:09:14 +0100762 close(fd);
Olivier Houchard153659f2017-04-05 22:39:56 +0200763 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200764 }
765 if (default_tcp6_maxseg == -1) {
766 default_tcp6_maxseg = -2;
767 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
768 if (fd >= 0) {
769 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
770 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100771 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200772 close(fd);
773 }
774 }
775#endif
776
Willy Tarreaue6b98942007-10-29 01:09:36 +0100777
778 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100779 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100780 *errmsg = 0;
781
782 if (listener->state != LI_ASSIGNED)
783 return ERR_NONE; /* already bound */
784
785 err = ERR_NONE;
786
Olivier Houchard153659f2017-04-05 22:39:56 +0200787 if (listener->fd == -1)
788 listener->fd = tcp_find_compatible_fd(listener);
789
Willy Tarreau40aa0702013-03-10 23:51:38 +0100790 /* if the listener already has an fd assigned, then we were offered the
791 * fd by an external process (most likely the parent), and we don't want
792 * to create a new socket. However we still want to set a few flags on
793 * the socket.
794 */
795 fd = listener->fd;
796 ext = (fd >= 0);
797
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100798 if (!ext) {
799 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
800
801 if (fd == -1) {
802 err |= ERR_RETRYABLE | ERR_ALERT;
803 msg = "cannot create listening socket";
804 goto tcp_return;
805 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100806 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100807
Willy Tarreaue6b98942007-10-29 01:09:36 +0100808 if (fd >= global.maxsock) {
809 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
810 msg = "not enough free sockets (raise '-n' parameter)";
811 goto tcp_close_return;
812 }
813
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200814 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100815 err |= ERR_FATAL | ERR_ALERT;
816 msg = "cannot make socket non-blocking";
817 goto tcp_close_return;
818 }
819
Willy Tarreau40aa0702013-03-10 23:51:38 +0100820 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100821 /* not fatal but should be reported */
822 msg = "cannot do so_reuseaddr";
823 err |= ERR_ALERT;
824 }
825
826 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900827 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200828 else {
829 struct linger tmplinger;
830 socklen_t len = sizeof(tmplinger);
831 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
832 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
833 tmplinger.l_onoff = 0;
834 tmplinger.l_linger = 0;
835 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
836 sizeof(tmplinger));
837 }
838 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100839
Willy Tarreaue6b98942007-10-29 01:09:36 +0100840#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000841 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
842 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100843 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000844 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100845 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100846#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200847
Willy Tarreau40aa0702013-03-10 23:51:38 +0100848 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200849 switch (listener->addr.ss_family) {
850 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200851 if (1
852#if defined(IP_TRANSPARENT)
853 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
854#endif
855#if defined(IP_FREEBIND)
856 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
857#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200858#if defined(IP_BINDANY)
859 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
860#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200861#if defined(SO_BINDANY)
862 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
863#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200864 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200865 msg = "cannot make listening socket transparent";
866 err |= ERR_ALERT;
867 }
868 break;
869 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200870 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200871#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200872 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
873#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100874#if defined(IP_FREEBIND)
875 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
876#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200877#if defined(IPV6_BINDANY)
878 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
879#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200880#if defined(SO_BINDANY)
881 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
882#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200883 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200884 msg = "cannot make listening socket transparent";
885 err |= ERR_ALERT;
886 }
887 break;
888 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100889 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200890
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100891#ifdef SO_BINDTODEVICE
892 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100893 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100894 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100895 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100896 msg = "cannot bind listener to device";
897 err |= ERR_WARN;
898 }
899 }
900#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400901#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100902 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400903 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200904 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
905 msg = "cannot set MSS";
906 err |= ERR_WARN;
907 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200908 } else if (ext) {
909 int tmpmaxseg = -1;
910 int defaultmss;
911 socklen_t len = sizeof(tmpmaxseg);
912
913 if (listener->addr.ss_family == AF_INET)
914 defaultmss = default_tcp_maxseg;
915 else
916 defaultmss = default_tcp6_maxseg;
917
918 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchy97a7bda2020-02-12 15:53:04 +0100919 if (defaultmss > 0 &&
920 tmpmaxseg != defaultmss &&
921 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200922 msg = "cannot set MSS";
923 err |= ERR_WARN;
924 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200925 }
926#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100927#if defined(TCP_USER_TIMEOUT)
928 if (listener->tcp_ut) {
929 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
930 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
931 msg = "cannot set TCP User Timeout";
932 err |= ERR_WARN;
933 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200934 } else
935 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
936 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100937#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200938#if defined(TCP_DEFER_ACCEPT)
939 if (listener->options & LI_O_DEF_ACCEPT) {
940 /* defer accept by up to one second */
941 int accept_delay = 1;
942 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
943 msg = "cannot enable DEFER_ACCEPT";
944 err |= ERR_WARN;
945 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200946 } else
947 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
948 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200949#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200950#if defined(TCP_FASTOPEN)
951 if (listener->options & LI_O_TCP_FO) {
952 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100953 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +0200954 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
955 msg = "cannot enable TCP_FASTOPEN";
956 err |= ERR_WARN;
957 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200958 } else {
959 socklen_t len;
960 int qlen;
961 len = sizeof(qlen);
962 /* Only disable fast open if it was enabled, we don't want
963 * the kernel to create a fast open queue if there's none.
964 */
965 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
966 qlen != 0) {
967 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
968 sizeof(zero)) == -1) {
969 msg = "cannot disable TCP_FASTOPEN";
970 err |= ERR_WARN;
971 }
972 }
Willy Tarreau1c862c52012-10-05 16:21:00 +0200973 }
974#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100975#if defined(IPV6_V6ONLY)
976 if (listener->options & LI_O_V6ONLY)
977 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100978 else if (listener->options & LI_O_V4V6)
979 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100980#endif
981
Willy Tarreau40aa0702013-03-10 23:51:38 +0100982 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100983 err |= ERR_RETRYABLE | ERR_ALERT;
984 msg = "cannot bind socket";
985 goto tcp_close_return;
986 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100987
Willy Tarreau40aa0702013-03-10 23:51:38 +0100988 ready = 0;
989 ready_len = sizeof(ready);
990 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
991 ready = 0;
992
993 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100994 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100995 err |= ERR_RETRYABLE | ERR_ALERT;
996 msg = "cannot listen to socket";
997 goto tcp_close_return;
998 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100999
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001000#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001001 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +09001002 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +02001003 else
1004 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001005#endif
1006
Willy Tarreaue6b98942007-10-29 01:09:36 +01001007 /* the socket is ready */
1008 listener->fd = fd;
1009 listener->state = LI_LISTEN;
1010
Willy Tarreaua9786b62018-01-25 07:22:13 +01001011 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +01001012 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaueb472682010-05-28 18:46:57 +02001013
Willy Tarreaue6b98942007-10-29 01:09:36 +01001014 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001015 if (msg && errlen) {
1016 char pn[INET6_ADDRSTRLEN];
1017
Willy Tarreau631f01c2011-09-05 00:36:48 +02001018 addr_to_str(&listener->addr, pn, sizeof(pn));
1019 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001020 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001021 return err;
1022
1023 tcp_close_return:
1024 close(fd);
1025 goto tcp_return;
1026}
1027
1028/* This function creates all TCP sockets bound to the protocol entry <proto>.
1029 * It is intended to be used as the protocol's bind_all() function.
1030 * The sockets will be registered but not added to any fd_set, in order not to
1031 * loose them across the fork(). A call to enable_all_listeners() is needed
1032 * to complete initialization. The return value is composed from ERR_*.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001033 *
1034 * Must be called with proto_lock held.
1035 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001036 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001037static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001038{
1039 struct listener *listener;
1040 int err = ERR_NONE;
1041
1042 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001043 err |= tcp_bind_listener(listener, errmsg, errlen);
1044 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001045 break;
1046 }
1047
1048 return err;
1049}
1050
Willy Tarreau32282382017-09-15 07:44:44 +02001051/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1052 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1053 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001054 *
1055 * Must be called with proto_lock held.
1056 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001057 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001058static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001059{
1060 if (listener->state != LI_INIT)
1061 return;
1062 listener->state = LI_ASSIGNED;
1063 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001064 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001065 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1066 proto_tcpv4.nb_listeners++;
1067}
1068
Willy Tarreau32282382017-09-15 07:44:44 +02001069/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1070 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1071 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +02001072 *
1073 * Must be called with proto_lock held.
1074 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001075 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001076static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001077{
1078 if (listener->state != LI_INIT)
1079 return;
1080 listener->state = LI_ASSIGNED;
1081 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001082 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001083 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1084 proto_tcpv6.nb_listeners++;
1085}
1086
Willy Tarreau092d8652014-07-07 20:22:12 +02001087/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1088 * was totally stopped, or > 0 if correctly paused.
1089 */
1090int tcp_pause_listener(struct listener *l)
1091{
1092 if (shutdown(l->fd, SHUT_WR) != 0)
1093 return -1; /* Solaris dies here */
1094
Willy Tarreaue2711c72019-02-27 15:39:41 +01001095 if (listen(l->fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +02001096 return -1; /* OpenBSD dies here */
1097
1098 if (shutdown(l->fd, SHUT_RD) != 0)
1099 return -1; /* should always be OK */
1100 return 1;
1101}
1102
William Lallemand2e785f22016-05-25 01:48:42 +02001103/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001104 * Execute the "set-src" action. May be called from {tcp,http}request.
1105 * It only changes the address and tries to preserve the original port. If the
1106 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001107 */
1108enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1109 struct session *sess, struct stream *s, int flags)
1110{
1111 struct connection *cli_conn;
1112
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001113 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand2e785f22016-05-25 01:48:42 +02001114 struct sample *smp;
1115
1116 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1117 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001118 int port = get_net_port(cli_conn->src);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001119
William Lallemand2e785f22016-05-25 01:48:42 +02001120 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001121 ((struct sockaddr_in *)cli_conn->src)->sin_family = AF_INET;
1122 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1123 ((struct sockaddr_in *)cli_conn->src)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001124 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001125 ((struct sockaddr_in6 *)cli_conn->src)->sin6_family = AF_INET6;
1126 memcpy(&((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1127 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001128 }
1129 }
William Lallemand01252ed2016-05-25 02:33:16 +02001130 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001131 }
1132 return ACT_RET_CONT;
1133}
1134
William Lallemand44be6402016-05-25 01:51:35 +02001135/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001136 * Execute the "set-dst" action. May be called from {tcp,http}request.
1137 * It only changes the address and tries to preserve the original port. If the
1138 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001139 */
1140enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1141 struct session *sess, struct stream *s, int flags)
1142{
1143 struct connection *cli_conn;
1144
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001145 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001146 struct sample *smp;
1147
1148 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1149 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001150 int port = get_net_port(cli_conn->dst);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001151
William Lallemand13e9b0c2016-05-25 02:34:07 +02001152 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001153 ((struct sockaddr_in *)cli_conn->dst)->sin_family = AF_INET;
1154 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001155 } else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001156 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_family = AF_INET6;
1157 memcpy(&((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1158 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001159 }
1160 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1161 }
1162 }
1163 return ACT_RET_CONT;
1164}
1165
1166/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001167 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1168 * We must test the sin_family before setting the port. If the address family
1169 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1170 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001171 */
1172enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1173 struct session *sess, struct stream *s, int flags)
1174{
1175 struct connection *cli_conn;
1176
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001177 if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) {
William Lallemand44be6402016-05-25 01:51:35 +02001178 struct sample *smp;
1179
William Lallemand44be6402016-05-25 01:51:35 +02001180 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1181 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001182 if (cli_conn->src->ss_family == AF_INET6) {
1183 ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001184 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001185 if (cli_conn->src->ss_family != AF_INET) {
1186 cli_conn->src->ss_family = AF_INET;
1187 ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001188 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001189 ((struct sockaddr_in *)cli_conn->src)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001190 }
1191 }
1192 }
1193 return ACT_RET_CONT;
1194}
1195
William Lallemand13e9b0c2016-05-25 02:34:07 +02001196/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001197 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1198 * We must test the sin_family before setting the port. If the address family
1199 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1200 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001201 */
1202enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1203 struct session *sess, struct stream *s, int flags)
1204{
1205 struct connection *cli_conn;
1206
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001207 if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001208 struct sample *smp;
1209
William Lallemand13e9b0c2016-05-25 02:34:07 +02001210 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1211 if (smp) {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001212 if (cli_conn->dst->ss_family == AF_INET6) {
1213 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001214 } else {
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001215 if (cli_conn->dst->ss_family != AF_INET) {
1216 cli_conn->dst->ss_family = AF_INET;
1217 ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001218 }
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001219 ((struct sockaddr_in *)cli_conn->dst)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001220 }
1221 }
1222 }
1223 return ACT_RET_CONT;
1224}
1225
Willy Tarreau2d392c22015-08-24 01:43:45 +02001226/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1227static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1228{
1229 struct connection *conn = objt_conn(sess->origin);
1230
1231 if (!conn)
1232 goto out;
1233
1234 if (!conn_ctrl_ready(conn))
1235 goto out;
1236
Willy Tarreau2d392c22015-08-24 01:43:45 +02001237#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001238 /* drain is needed only to send the quick ACK */
1239 conn_sock_drain(conn);
1240
Willy Tarreau2d392c22015-08-24 01:43:45 +02001241 /* re-enable quickack if it was disabled to ack all data and avoid
1242 * retransmits from the client that might trigger a real reset.
1243 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001244 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001245#endif
1246 /* lingering must absolutely be disabled so that we don't send a
1247 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1248 * is present, returning with ERR will cause lingering to be disabled.
1249 */
1250 if (strm)
1251 strm->si[0].flags |= SI_FL_NOLINGER;
1252
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001253 /* We're on the client-facing side, we must force to disable lingering to
1254 * ensure we will use an RST exclusively and kill any pending data.
1255 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001256 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001257
Willy Tarreau2d392c22015-08-24 01:43:45 +02001258#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001259 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001260 /* socket will be quiet now */
1261 goto out;
1262 }
1263#endif
1264 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1265 * Let's fall back on the TTL trick, though it only works for routed
1266 * network and has no effect on local net.
1267 */
1268#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001269 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001270#endif
1271 out:
1272 /* kill the stream if any */
1273 if (strm) {
1274 channel_abort(&strm->req);
1275 channel_abort(&strm->res);
Christopher Faulet177f4802020-03-06 15:10:46 +01001276 strm->req.analysers &= AN_REQ_FLT_END;
1277 strm->res.analysers &= AN_RES_FLT_END;
Christopher Fauletddc005a2020-03-06 15:23:18 +01001278 if (strm->flags & SF_BE_ASSIGNED)
1279 _HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001280 if (!(strm->flags & SF_ERR_MASK))
1281 strm->flags |= SF_ERR_PRXCOND;
1282 if (!(strm->flags & SF_FINST_MASK))
1283 strm->flags |= SF_FINST_R;
1284 }
1285
Olivier Houchard40514102019-03-08 18:54:04 +01001286 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001287 if (sess->listener->counters)
Olivier Houchard40514102019-03-08 18:54:04 +01001288 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001289
Christopher Fauletf573ba22020-03-06 15:15:49 +01001290 return ACT_RET_ABRT;
Willy Tarreau2d392c22015-08-24 01:43:45 +02001291}
1292
William Lallemand13e9b0c2016-05-25 02:34:07 +02001293/* parse "set-{src,dst}[-port]" action */
1294enum 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 +02001295{
1296 int cur_arg;
1297 struct sample_expr *expr;
1298 unsigned int where;
1299
1300 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001301 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 +02001302 if (!expr)
1303 return ACT_RET_PRS_ERR;
1304
1305 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001306 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001307 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001308 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001309 where |= SMP_VAL_BE_HRQ_HDR;
1310
1311 if (!(expr->fetch->val & where)) {
1312 memprintf(err,
1313 "fetch method '%s' extracts information from '%s', none of which is available here",
1314 args[cur_arg-1], sample_src_names(expr->fetch->use));
1315 free(expr);
1316 return ACT_RET_PRS_ERR;
1317 }
1318 rule->arg.expr = expr;
1319 rule->action = ACT_CUSTOM;
1320
1321 if (!strcmp(args[*orig_arg-1], "set-src")) {
1322 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001323 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1324 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001325 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1326 rule->action_ptr = tcp_action_req_set_dst;
1327 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1328 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001329 } else {
1330 return ACT_RET_PRS_ERR;
1331 }
1332
1333 (*orig_arg)++;
1334
1335 return ACT_RET_PRS_OK;
1336}
1337
1338
Willy Tarreau2d392c22015-08-24 01:43:45 +02001339/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1340 * success, ACT_RET_PRS_ERR on error.
1341 */
1342static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1343 struct act_rule *rule, char **err)
1344{
1345 rule->action = ACT_CUSTOM;
1346 rule->action_ptr = tcp_exec_action_silent_drop;
1347 return ACT_RET_PRS_OK;
1348}
1349
Willy Tarreau645513a2010-05-24 20:55:15 +02001350
1351/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001352/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001353/************************************************************************/
1354
Willy Tarreau4a129812012-04-25 17:31:42 +02001355/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001356int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001357{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001358 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001359
1360 if (!cli_conn)
1361 return 0;
1362
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001363 if (!conn_get_src(cli_conn))
1364 return 0;
1365
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001366 switch (cli_conn->src->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001367 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001368 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->src)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001369 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001370 break;
1371 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001372 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->src)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001373 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001374 break;
1375 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001376 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001377 }
Emeric Brunf769f512010-10-22 17:14:01 +02001378
Willy Tarreau37406352012-04-23 16:16:37 +02001379 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001380 return 1;
1381}
1382
Willy Tarreaua5e37562011-12-16 17:06:15 +01001383/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001384static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001385smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001386{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001387 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001388
1389 if (!cli_conn)
1390 return 0;
1391
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001392 if (!conn_get_src(cli_conn))
1393 return 0;
1394
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001395 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001396 if (!(smp->data.u.sint = get_host_port(cli_conn->src)))
Emeric Brunf769f512010-10-22 17:14:01 +02001397 return 0;
1398
Willy Tarreau37406352012-04-23 16:16:37 +02001399 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001400 return 1;
1401}
1402
Willy Tarreau4a129812012-04-25 17:31:42 +02001403/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001404static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001405smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001406{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001407 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001408
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001409 if (!cli_conn)
1410 return 0;
1411
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001412 if (!conn_get_dst(cli_conn))
1413 return 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001414
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001415 switch (cli_conn->dst->ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001416 case AF_INET:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001417 smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001418 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001419 break;
1420 case AF_INET6:
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001421 smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001422 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001423 break;
1424 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001425 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001426 }
Emeric Brunf769f512010-10-22 17:14:01 +02001427
Willy Tarreau37406352012-04-23 16:16:37 +02001428 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001429 return 1;
1430}
1431
Willy Tarreau16e01562016-08-09 16:46:18 +02001432/* check if the destination address of the front connection is local to the
1433 * system or if it was intercepted.
1434 */
1435int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1436{
1437 struct connection *conn = objt_conn(smp->sess->origin);
1438 struct listener *li = smp->sess->listener;
1439
1440 if (!conn)
1441 return 0;
1442
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001443 if (!conn_get_dst(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001444 return 0;
1445
1446 smp->data.type = SMP_T_BOOL;
1447 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001448 smp->data.u.sint = addr_is_local(li->netns, conn->dst);
Willy Tarreau16e01562016-08-09 16:46:18 +02001449 return smp->data.u.sint >= 0;
1450}
1451
1452/* check if the source address of the front connection is local to the system
1453 * or not.
1454 */
1455int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1456{
1457 struct connection *conn = objt_conn(smp->sess->origin);
1458 struct listener *li = smp->sess->listener;
1459
1460 if (!conn)
1461 return 0;
1462
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001463 if (!conn_get_src(conn))
Willy Tarreau16e01562016-08-09 16:46:18 +02001464 return 0;
1465
1466 smp->data.type = SMP_T_BOOL;
1467 smp->flags = 0;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001468 smp->data.u.sint = addr_is_local(li->netns, conn->src);
Willy Tarreau16e01562016-08-09 16:46:18 +02001469 return smp->data.u.sint >= 0;
1470}
1471
Willy Tarreaua5e37562011-12-16 17:06:15 +01001472/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001473static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001474smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001475{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001476 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001477
1478 if (!cli_conn)
1479 return 0;
1480
Willy Tarreaudddd2b42019-07-17 11:37:29 +02001481 if (!conn_get_dst(cli_conn))
1482 return 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001483
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001484 smp->data.type = SMP_T_SINT;
Willy Tarreau7bbc4a52019-07-17 15:41:35 +02001485 if (!(smp->data.u.sint = get_host_port(cli_conn->dst)))
Emeric Brunf769f512010-10-22 17:14:01 +02001486 return 0;
1487
Willy Tarreau37406352012-04-23 16:16:37 +02001488 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001489 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001490}
1491
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001492#ifdef TCP_INFO
1493
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001494
1495/* Validates the arguments passed to "fc_*" fetch keywords returning a time
1496 * value. These keywords support an optional string representing the unit of the
1497 * result: "us" for microseconds and "ms" for milliseconds". Returns 0 on error
1498 * and non-zero if OK.
1499 */
1500static int val_fc_time_value(struct arg *args, char **err)
1501{
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001502 if (args[0].type == ARGT_STR) {
1503 if (strcmp(args[0].data.str.area, "us") == 0) {
1504 free(args[0].data.str.area);
1505 args[0].type = ARGT_SINT;
1506 args[0].data.sint = TIME_UNIT_US;
1507 }
1508 else if (strcmp(args[0].data.str.area, "ms") == 0) {
1509 free(args[0].data.str.area);
1510 args[0].type = ARGT_SINT;
1511 args[0].data.sint = TIME_UNIT_MS;
1512 }
1513 else {
1514 memprintf(err, "expects 'us' or 'ms', got '%s'",
1515 args[0].data.str.area);
1516 return 0;
1517 }
1518 }
1519 else {
1520 memprintf(err, "Unexpected arg type");
1521 return 0;
1522 }
1523
1524 return 1;
1525}
1526
1527/* Validates the arguments passed to "fc_*" fetch keywords returning a
1528 * counter. These keywords should be used without any keyword, but because of a
1529 * bug in previous versions, an optional string argument may be passed. In such
1530 * case, the argument is ignored and a warning is emitted. Returns 0 on error
1531 * and non-zero if OK.
1532 */
1533static int var_fc_counter(struct arg *args, char **err)
1534{
1535 if (args[0].type != ARGT_STOP) {
1536 ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n");
1537 if (args[0].type == ARGT_STR)
1538 free(args[0].data.str.area);
1539 args[0].type = ARGT_STOP;
1540 }
1541
1542 return 1;
1543}
1544
Joseph Herlanta6331472018-11-25 12:59:12 -08001545/* Returns some tcp_info data if it's available. "dir" must be set to 0 if
1546 * the client connection is required, otherwise it is set to 1. "val" represents
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001547 * the required value.
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001548 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1549 */
1550static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1551 int dir, int val)
1552{
1553 struct connection *conn;
1554 struct tcp_info info;
1555 socklen_t optlen;
1556
1557 /* strm can be null. */
1558 if (!smp->strm)
1559 return 0;
1560
1561 /* get the object associated with the stream interface.The
1562 * object can be other thing than a connection. For example,
1563 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001564 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001565 if (!conn)
1566 return 0;
1567
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001568 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001569 syscal can fail. */
1570 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001571 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001572 return 0;
1573
1574 /* extract the value. */
1575 smp->data.type = SMP_T_SINT;
1576 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001577 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1578 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1579#if defined(__linux__)
1580 /* these ones are common to all Linux versions */
1581 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1582 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1583 case 4: smp->data.u.sint = info.tcpi_lost; break;
1584 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1585 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1586 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1587#elif defined(__FreeBSD__) || defined(__NetBSD__)
1588 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1589 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1590 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1591 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1592 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1593 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1594 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1595#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001596 default: return 0;
1597 }
1598
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001599 return 1;
1600}
1601
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001602/* get the mean rtt of a client connection */
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001603static int
1604smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1605{
1606 if (!get_tcp_info(args, smp, 0, 0))
1607 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001608
1609 /* By default or if explicitly specified, convert rtt to ms */
1610 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1611 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1612
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001613 return 1;
1614}
1615
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001616/* get the variance of the mean rtt of a client connection */
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001617static int
1618smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1619{
1620 if (!get_tcp_info(args, smp, 0, 1))
1621 return 0;
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001622
1623 /* By default or if explicitly specified, convert rttvar to ms */
1624 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1625 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1626
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001627 return 1;
1628}
Joe Williams30fcd392016-08-10 07:06:44 -07001629
1630#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1631
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001632/* get the unacked counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001633static int
1634smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1635{
1636 if (!get_tcp_info(args, smp, 0, 2))
1637 return 0;
1638 return 1;
1639}
1640
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001641/* get the sacked counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001642static int
1643smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1644{
1645 if (!get_tcp_info(args, smp, 0, 3))
1646 return 0;
1647 return 1;
1648}
1649
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001650/* get the lost counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001651static int
1652smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1653{
1654 if (!get_tcp_info(args, smp, 0, 4))
1655 return 0;
1656 return 1;
1657}
1658
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001659/* get the retrans counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001660static int
1661smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1662{
1663 if (!get_tcp_info(args, smp, 0, 5))
1664 return 0;
1665 return 1;
1666}
1667
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001668/* get the fackets counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001669static int
1670smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1671{
1672 if (!get_tcp_info(args, smp, 0, 6))
1673 return 0;
1674 return 1;
1675}
1676
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001677/* get the reordering counter on a client connection */
Joe Williams30fcd392016-08-10 07:06:44 -07001678static int
1679smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1680{
1681 if (!get_tcp_info(args, smp, 0, 7))
1682 return 0;
1683 return 1;
1684}
1685#endif // linux || freebsd || netbsd
1686#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001687
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001688#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001689/* parse the "v4v6" bind keyword */
1690static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1691{
1692 struct listener *l;
1693
1694 list_for_each_entry(l, &conf->listeners, by_bind) {
1695 if (l->addr.ss_family == AF_INET6)
1696 l->options |= LI_O_V4V6;
1697 }
1698
1699 return 0;
1700}
1701
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001702/* parse the "v6only" bind keyword */
1703static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1704{
1705 struct listener *l;
1706
1707 list_for_each_entry(l, &conf->listeners, by_bind) {
1708 if (l->addr.ss_family == AF_INET6)
1709 l->options |= LI_O_V6ONLY;
1710 }
1711
1712 return 0;
1713}
1714#endif
1715
Pieter Baauwd551fb52013-05-08 22:49:23 +02001716#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001717/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001718static 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 +02001719{
1720 struct listener *l;
1721
Willy Tarreau4348fad2012-09-20 16:48:07 +02001722 list_for_each_entry(l, &conf->listeners, by_bind) {
1723 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1724 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001725 }
1726
Willy Tarreau44791242012-09-12 23:27:21 +02001727 return 0;
1728}
1729#endif
1730
1731#ifdef TCP_DEFER_ACCEPT
1732/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001733static 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 +02001734{
1735 struct listener *l;
1736
Willy Tarreau4348fad2012-09-20 16:48:07 +02001737 list_for_each_entry(l, &conf->listeners, by_bind) {
1738 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1739 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001740 }
1741
Willy Tarreau44791242012-09-12 23:27:21 +02001742 return 0;
1743}
1744#endif
1745
Willy Tarreau1c862c52012-10-05 16:21:00 +02001746#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001747/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001748static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1749{
1750 struct listener *l;
1751
1752 list_for_each_entry(l, &conf->listeners, by_bind) {
1753 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1754 l->options |= LI_O_TCP_FO;
1755 }
1756
1757 return 0;
1758}
1759#endif
1760
Willy Tarreau44791242012-09-12 23:27:21 +02001761#ifdef TCP_MAXSEG
1762/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001763static 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 +02001764{
1765 struct listener *l;
1766 int mss;
1767
Willy Tarreau44791242012-09-12 23:27:21 +02001768 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001769 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001770 return ERR_ALERT | ERR_FATAL;
1771 }
1772
1773 mss = atoi(args[cur_arg + 1]);
1774 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001775 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001776 return ERR_ALERT | ERR_FATAL;
1777 }
1778
Willy Tarreau4348fad2012-09-20 16:48:07 +02001779 list_for_each_entry(l, &conf->listeners, by_bind) {
1780 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1781 l->maxseg = mss;
1782 }
Willy Tarreau44791242012-09-12 23:27:21 +02001783
1784 return 0;
1785}
1786#endif
1787
Willy Tarreau2af207a2015-02-04 00:45:58 +01001788#ifdef TCP_USER_TIMEOUT
1789/* parse the "tcp-ut" bind keyword */
1790static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1791{
1792 const char *ptr = NULL;
1793 struct listener *l;
1794 unsigned int timeout;
1795
1796 if (!*args[cur_arg + 1]) {
1797 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1798 return ERR_ALERT | ERR_FATAL;
1799 }
1800
1801 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001802 if (ptr == PARSE_TIME_OVER) {
1803 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1804 args[cur_arg+1], args[cur_arg]);
1805 return ERR_ALERT | ERR_FATAL;
1806 }
1807 else if (ptr == PARSE_TIME_UNDER) {
1808 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1809 args[cur_arg+1], args[cur_arg]);
1810 return ERR_ALERT | ERR_FATAL;
1811 }
1812 else if (ptr) {
Willy Tarreau2af207a2015-02-04 00:45:58 +01001813 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1814 return ERR_ALERT | ERR_FATAL;
1815 }
1816
1817 list_for_each_entry(l, &conf->listeners, by_bind) {
1818 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1819 l->tcp_ut = timeout;
1820 }
1821
1822 return 0;
1823}
1824#endif
1825
Willy Tarreau44791242012-09-12 23:27:21 +02001826#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001827/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001828static 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 +02001829{
1830 struct listener *l;
1831
Willy Tarreau44791242012-09-12 23:27:21 +02001832 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001833 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001834 return ERR_ALERT | ERR_FATAL;
1835 }
1836
Willy Tarreau4348fad2012-09-20 16:48:07 +02001837 list_for_each_entry(l, &conf->listeners, by_bind) {
1838 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1839 l->interface = strdup(args[cur_arg + 1]);
1840 }
Willy Tarreau44791242012-09-12 23:27:21 +02001841
Willy Tarreau44791242012-09-12 23:27:21 +02001842 return 0;
1843}
1844#endif
1845
Willy Tarreaue5733232019-05-22 19:24:06 +02001846#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001847/* parse the "namespace" bind keyword */
1848static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1849{
1850 struct listener *l;
1851 char *namespace = NULL;
1852
1853 if (!*args[cur_arg + 1]) {
1854 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1855 return ERR_ALERT | ERR_FATAL;
1856 }
1857 namespace = args[cur_arg + 1];
1858
1859 list_for_each_entry(l, &conf->listeners, by_bind) {
1860 l->netns = netns_store_lookup(namespace, strlen(namespace));
1861
1862 if (l->netns == NULL)
1863 l->netns = netns_store_insert(namespace);
1864
1865 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001866 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001867 return ERR_ALERT | ERR_FATAL;
1868 }
1869 }
1870 return 0;
1871}
1872#endif
1873
Willy Tarreau163d4622015-10-13 16:16:41 +02001874#ifdef TCP_USER_TIMEOUT
1875/* parse the "tcp-ut" server keyword */
1876static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1877{
1878 const char *ptr = NULL;
1879 unsigned int timeout;
1880
1881 if (!*args[*cur_arg + 1]) {
1882 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1883 return ERR_ALERT | ERR_FATAL;
1884 }
1885
1886 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001887 if (ptr == PARSE_TIME_OVER) {
1888 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1889 args[*cur_arg+1], args[*cur_arg]);
1890 return ERR_ALERT | ERR_FATAL;
1891 }
1892 else if (ptr == PARSE_TIME_UNDER) {
1893 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1894 args[*cur_arg+1], args[*cur_arg]);
1895 return ERR_ALERT | ERR_FATAL;
1896 }
1897 else if (ptr) {
Willy Tarreau163d4622015-10-13 16:16:41 +02001898 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1899 return ERR_ALERT | ERR_FATAL;
1900 }
1901
1902 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1903 newsrv->tcp_ut = timeout;
1904
1905 return 0;
1906}
1907#endif
1908
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001909
Willy Tarreau4a129812012-04-25 17:31:42 +02001910/* Note: must not be declared <const> as its list will be overwritten.
1911 * Note: fetches that may return multiple types must be declared as the lowest
1912 * common denominator, the type that can be casted into all other ones. For
1913 * instance v4/v6 must be declared v4.
1914 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001915static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001916 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001917 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001918 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001919 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001920 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001921 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001922#ifdef TCP_INFO
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001923 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
1924 { "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 -07001925#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Christopher Fauletba0c53e2019-10-17 14:40:48 +02001926 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1927 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1928 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1929 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1930 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1931 { "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 -07001932#endif // linux || freebsd || netbsd
1933#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001934 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001935}};
1936
Willy Tarreau0108d902018-11-25 19:14:37 +01001937INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1938
Willy Tarreau44791242012-09-12 23:27:21 +02001939/************************************************************************/
1940/* All supported bind keywords must be declared here. */
1941/************************************************************************/
1942
1943/* Note: must not be declared <const> as its list will be overwritten.
1944 * Please take care of keeping this list alphabetically sorted, doing so helps
1945 * all code contributors.
1946 * Optional keywords are also declared with a NULL ->parse() function so that
1947 * the config parser can report an appropriate error when a known keyword was
1948 * not enabled.
1949 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001950static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001951#ifdef TCP_DEFER_ACCEPT
1952 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1953#endif
1954#ifdef SO_BINDTODEVICE
1955 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1956#endif
1957#ifdef TCP_MAXSEG
1958 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1959#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001960#ifdef TCP_USER_TIMEOUT
1961 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1962#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001963#ifdef TCP_FASTOPEN
1964 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1965#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001966#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001967 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1968#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001969#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001970 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001971 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1972#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001973#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001974 { "namespace", bind_parse_namespace, 1 },
1975#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001976 /* the versions with the NULL parse function*/
1977 { "defer-accept", NULL, 0 },
1978 { "interface", NULL, 1 },
1979 { "mss", NULL, 1 },
1980 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001981 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001982 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001983 { NULL, NULL, 0 },
1984}};
1985
Willy Tarreau0108d902018-11-25 19:14:37 +01001986INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1987
Willy Tarreau163d4622015-10-13 16:16:41 +02001988static struct srv_kw_list srv_kws = { "TCP", { }, {
1989#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001990 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02001991#endif
1992 { NULL, NULL, 0 },
1993}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02001994
Willy Tarreau0108d902018-11-25 19:14:37 +01001995INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
1996
Willy Tarreau2d392c22015-08-24 01:43:45 +02001997static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand13e9b0c2016-05-25 02:34:07 +02001998 { "set-src", tcp_parse_set_src_dst },
1999 { "set-src-port", tcp_parse_set_src_dst },
2000 { "set-dst" , tcp_parse_set_src_dst },
2001 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002002 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002003 { /* END */ }
2004}};
2005
Willy Tarreau0108d902018-11-25 19:14:37 +01002006INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions);
2007
Willy Tarreau620408f2016-10-21 16:37:51 +02002008static struct action_kw_list tcp_req_sess_actions = {ILH, {
Willy Tarreau620408f2016-10-21 16:37:51 +02002009 { "set-src", tcp_parse_set_src_dst },
2010 { "set-src-port", tcp_parse_set_src_dst },
2011 { "set-dst" , tcp_parse_set_src_dst },
2012 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002013 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau620408f2016-10-21 16:37:51 +02002014 { /* END */ }
2015}};
2016
Willy Tarreau0108d902018-11-25 19:14:37 +01002017INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions);
2018
Willy Tarreau2d392c22015-08-24 01:43:45 +02002019static struct action_kw_list tcp_req_cont_actions = {ILH, {
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002020 { "set-dst" , tcp_parse_set_src_dst },
2021 { "set-dst-port", tcp_parse_set_src_dst },
2022 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002023 { /* END */ }
2024}};
2025
Willy Tarreau0108d902018-11-25 19:14:37 +01002026INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2027
Willy Tarreau2d392c22015-08-24 01:43:45 +02002028static struct action_kw_list tcp_res_cont_actions = {ILH, {
2029 { "silent-drop", tcp_parse_silent_drop },
2030 { /* END */ }
2031}};
2032
Willy Tarreau0108d902018-11-25 19:14:37 +01002033INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
2034
Willy Tarreau2d392c22015-08-24 01:43:45 +02002035static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002036 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002037 { "set-src", tcp_parse_set_src_dst },
2038 { "set-src-port", tcp_parse_set_src_dst },
2039 { "set-dst", tcp_parse_set_src_dst },
2040 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002041 { /* END */ }
2042}};
2043
Willy Tarreau0108d902018-11-25 19:14:37 +01002044INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2045
Willy Tarreau2d392c22015-08-24 01:43:45 +02002046static struct action_kw_list http_res_actions = {ILH, {
2047 { "silent-drop", tcp_parse_silent_drop },
2048 { /* END */ }
2049}};
2050
Willy Tarreau0108d902018-11-25 19:14:37 +01002051INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002052
Willy Tarreau80713382018-11-26 10:19:54 +01002053REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
Willy Tarreauba962912016-12-21 18:55:02 +01002054#if defined(IP_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002055 " IP_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002056#endif
2057#if defined(IPV6_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002058 " IPV6_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002059#endif
2060#if defined(IP_FREEBIND)
Willy Tarreau80713382018-11-26 10:19:54 +01002061 " IP_FREEBIND"
Willy Tarreauba962912016-12-21 18:55:02 +01002062#endif
2063#if defined(IP_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002064 " IP_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002065#endif
2066#if defined(IPV6_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002067 " IPV6_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002068#endif
2069#if defined(SO_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002070 " SO_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002071#endif
Willy Tarreau80713382018-11-26 10:19:54 +01002072 "");
Willy Tarreaue6b98942007-10-29 01:09:36 +01002073
2074
2075/*
2076 * Local variables:
2077 * c-indent-level: 8
2078 * c-basic-offset: 8
2079 * End:
2080 */