blob: c000602b64f624a73f421b39e9c52f0c4061c04d [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>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <sys/un.h>
31
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040032#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020033#include <netinet/in.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040034
Willy Tarreaue6b98942007-10-29 01:09:36 +010035#include <common/compat.h>
36#include <common/config.h>
37#include <common/debug.h>
38#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039#include <common/mini-clist.h>
40#include <common/standard.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010041#include <common/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010042
Willy Tarreau39713102016-11-25 15:49:32 +010043#include <types/action.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020044#include <types/connection.h>
Willy Tarreau39713102016-11-25 15:49:32 +010045#include <types/global.h>
46#include <types/stream.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010047
Willy Tarreau9fcb9842012-04-20 14:45:49 +020048#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020050#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020051#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020052#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020053#include <proto/log.h>
54#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020055#include <proto/protocol.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020056#include <proto/proto_http.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010057#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020059#include <proto/sample.h>
Willy Tarreau163d4622015-10-13 16:16:41 +020060#include <proto/server.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020061#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010062#include <proto/tcp_rules.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010063
Emeric Bruncf20bf12010-10-22 16:06:11 +020064static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
65static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020066static void tcpv4_add_listener(struct listener *listener, int port);
67static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010068
69/* Note: must not be declared <const> as its list will be overwritten */
70static struct protocol proto_tcpv4 = {
71 .name = "tcpv4",
72 .sock_domain = AF_INET,
73 .sock_type = SOCK_STREAM,
74 .sock_prot = IPPROTO_TCP,
75 .sock_family = AF_INET,
76 .sock_addrlen = sizeof(struct sockaddr_in),
77 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020078 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020079 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020080 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010081 .bind_all = tcp_bind_listeners,
82 .unbind_all = unbind_all_listeners,
83 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020084 .get_src = tcp_get_src,
85 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020086 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020087 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010088 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
89 .nb_listeners = 0,
90};
91
92/* Note: must not be declared <const> as its list will be overwritten */
93static struct protocol proto_tcpv6 = {
94 .name = "tcpv6",
95 .sock_domain = AF_INET6,
96 .sock_type = SOCK_STREAM,
97 .sock_prot = IPPROTO_TCP,
98 .sock_family = AF_INET6,
99 .sock_addrlen = sizeof(struct sockaddr_in6),
100 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200101 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200102 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200103 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100104 .bind_all = tcp_bind_listeners,
105 .unbind_all = unbind_all_listeners,
106 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200107 .get_src = tcp_get_src,
108 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +0200109 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200110 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100111 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
112 .nb_listeners = 0,
113};
114
Olivier Houchard153659f2017-04-05 22:39:56 +0200115/* Default TCP parameters, got by opening a temporary TCP socket. */
116#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100117static THREAD_LOCAL int default_tcp_maxseg = -1;
118static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200119#endif
120
David du Colombier6f5ccb12011-03-10 22:26:24 +0100121/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100122 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
123 * - 0 : ignore remote address (may even be a NULL pointer)
124 * - 1 : use provided address
125 * - 2 : use provided port
126 * - 3 : use both
127 *
128 * The function supports multiple foreign binding methods :
129 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100130 * The second one can be used as a fallback for the first one.
131 * This function returns 0 when everything's OK, 1 if it could not bind, to the
132 * local address, 2 if it could not bind to the foreign address.
133 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100134int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100135{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100136 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100137 int foreign_ok = 0;
138 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100139 static THREAD_LOCAL int ip_transp_working = 1;
140 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200141
David du Colombier65c17962012-07-13 14:34:59 +0200142 switch (local->ss_family) {
143 case AF_INET:
144 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200145 /* This deserves some explanation. Some platforms will support
146 * multiple combinations of certain methods, so we try the
147 * supported ones until one succeeds.
148 */
149 if (0
150#if defined(IP_TRANSPARENT)
151 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
152#endif
153#if defined(IP_FREEBIND)
154 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
155#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200156#if defined(IP_BINDANY)
157 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
158#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200159#if defined(SO_BINDANY)
160 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
161#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200162 )
David du Colombier65c17962012-07-13 14:34:59 +0200163 foreign_ok = 1;
164 else
165 ip_transp_working = 0;
166 }
167 break;
168 case AF_INET6:
169 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200170 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200171#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200172 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
173#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100174#if defined(IP_FREEBIND)
175 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
176#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200177#if defined(IPV6_BINDANY)
178 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
179#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200180#if defined(SO_BINDANY)
181 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
182#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200183 )
David du Colombier65c17962012-07-13 14:34:59 +0200184 foreign_ok = 1;
185 else
186 ip6_transp_working = 0;
187 }
188 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100189 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200190
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100191 if (flags) {
192 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200193 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100194 switch (remote->ss_family) {
195 case AF_INET:
196 if (flags & 1)
197 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
198 if (flags & 2)
199 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
200 break;
201 case AF_INET6:
202 if (flags & 1)
203 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
204 if (flags & 2)
205 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
206 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100207 default:
208 /* we don't want to try to bind to an unknown address family */
209 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100210 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100211 }
212
Simon Hormande072bd2011-06-24 15:11:37 +0900213 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100214 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200215 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200216 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
217 if (ret < 0)
218 return 2;
219 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100220 }
221 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200222 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200223 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
224 if (ret < 0)
225 return 1;
226 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100227 }
228
229 if (!flags)
230 return 0;
231
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100232 if (!foreign_ok)
233 /* we could not bind to a foreign address */
234 return 2;
235
236 return 0;
237}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100238
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100239static int create_server_socket(struct connection *conn)
240{
Willy Tarreau529c1392014-12-24 13:47:55 +0100241 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100242
Willy Tarreau529c1392014-12-24 13:47:55 +0100243#ifdef CONFIG_HAP_NS
244 if (objt_server(conn->target)) {
245 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
246 ns = conn->proxy_netns;
247 else
248 ns = __objt_server(conn->target)->netns;
249 }
250#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100251 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
252}
Willy Tarreau9650f372009-08-16 14:02:45 +0200253
254/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200255 * This function initiates a TCP connection establishment to the target assigned
256 * to connection <conn> using (si->{target,addr.to}). A source address may be
257 * pointed to by conn->addr.from in case of transparent proxying. Normal source
258 * bind addresses are still determined locally (due to the possible need of a
259 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100260 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100261 * supported. The <data> parameter is a boolean indicating whether there are data
262 * waiting for being sent or not, in order to adjust data write polling and on
263 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
264 * allows the caller to force using a delayed ACK when establishing the connection :
265 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
266 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
267 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200268 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200269 * Note that a pending send_proxy message accounts for data.
270 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200271 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200272 * - SF_ERR_NONE if everything's OK
273 * - SF_ERR_SRVTO if there are no more servers
274 * - SF_ERR_SRVCL if the connection was refused by the server
275 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
276 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
277 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100278 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100279 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200280 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100281 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200282 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100283
Willy Tarreauf0837b22012-11-24 10:24:27 +0100284int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200285{
286 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100287 struct server *srv;
288 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100289 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100290
Willy Tarreau9ce70132014-01-24 16:08:19 +0100291 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
292
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;
301 break;
302 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100303 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200304 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100305 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200306
Willy Tarreau585744b2017-08-24 14:31:19 +0200307 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100308
309 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200310 qfprintf(stderr, "Cannot get a server socket.\n");
311
Willy Tarreau9ce70132014-01-24 16:08:19 +0100312 if (errno == ENFILE) {
313 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200314 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100315 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
316 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100317 }
318 else if (errno == EMFILE) {
319 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200320 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100321 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
322 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100323 }
324 else if (errno == ENOBUFS || errno == ENOMEM) {
325 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200326 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100327 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
328 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100329 }
330 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
331 conn->err_code = CO_ER_NOPROTO;
332 }
333 else
334 conn->err_code = CO_ER_SOCK_ERR;
335
Willy Tarreau9650f372009-08-16 14:02:45 +0200336 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100337 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200338 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200339 }
340
341 if (fd >= global.maxsock) {
342 /* do not log anything there, it's a normal condition when this option
343 * is used to serialize connections to a server !
344 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100345 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200346 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100347 conn->err_code = CO_ER_CONF_FDLIM;
348 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200349 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200350 }
351
352 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900353 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200354 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
355 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100356 conn->err_code = CO_ER_SOCK_ERR;
357 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200358 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200359 }
360
361 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900362 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200363
Willy Tarreau9650f372009-08-16 14:02:45 +0200364 /* allow specific binding :
365 * - server-specific at first
366 * - proxy-specific next
367 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100368 if (srv && srv->conn_src.opts & CO_SRC_BIND)
369 src = &srv->conn_src;
370 else if (be->conn_src.opts & CO_SRC_BIND)
371 src = &be->conn_src;
372 else
373 src = NULL;
374
375 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200376 int ret, flags = 0;
377
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200378 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100379 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100380 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200381 conn->flags |= CO_FL_PRIVATE;
382 /* fall through */
383 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200384 flags = 3;
385 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100386 case CO_SRC_TPROXY_CIP:
387 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200388 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200389 flags = 1;
390 break;
391 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200392 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200393
Willy Tarreau9650f372009-08-16 14:02:45 +0200394#ifdef SO_BINDTODEVICE
395 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100396 if (src->iface_name)
397 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200398#endif
399
Willy Tarreaua4380b42012-12-08 22:49:11 +0100400 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200401 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100402 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200403
404 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200405 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200406
407 do {
408 /* note: in case of retry, we may have to release a previously
409 * allocated port, hence this loop's construct.
410 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200411 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
412 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200413
414 if (!attempts)
415 break;
416 attempts--;
417
Willy Tarreaua4380b42012-12-08 22:49:11 +0100418 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100419 if (!fdinfo[fd].local_port) {
420 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200421 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100422 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200423
Willy Tarreaua4380b42012-12-08 22:49:11 +0100424 fdinfo[fd].port_range = src->sport_range;
425 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200426
Willy Tarreaua4380b42012-12-08 22:49:11 +0100427 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100428 if (ret != 0)
429 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200430 } while (ret != 0); /* binding NOK */
431 }
432 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000433#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100434 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000435 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
436#endif
Willy Tarreaua4380b42012-12-08 22:49:11 +0100437 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100438 if (ret != 0)
439 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200440 }
441
Willy Tarreaua4380b42012-12-08 22:49:11 +0100442 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200443 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
444 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 close(fd);
446
Willy Tarreau9650f372009-08-16 14:02:45 +0200447 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100448 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
449 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200450 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100451 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 be->id);
453 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100454 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
455 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200456 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100457 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 be->id);
459 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100460 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200461 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200462 }
463 }
464
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400465#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200466 /* disabling tcp quick ack now allows the first request to leave the
467 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100468 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200470 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900471 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200472#endif
473
Willy Tarreau163d4622015-10-13 16:16:41 +0200474#ifdef TCP_USER_TIMEOUT
475 /* there is not much more we can do here when it fails, it's still minor */
476 if (srv && srv->tcp_ut)
477 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
478#endif
Willy Tarreaue803de22010-01-21 17:43:04 +0100479 if (global.tune.server_sndbuf)
480 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
481
482 if (global.tune.server_rcvbuf)
483 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
484
Willy Tarreau819efbf2017-01-25 14:12:22 +0100485 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
486 if (errno == EINPROGRESS || errno == EALREADY) {
487 /* common case, let's wait for connect status */
488 conn->flags |= CO_FL_WAIT_L4_CONN;
489 }
490 else if (errno == EISCONN) {
491 /* should normally not happen but if so, indicates that it's OK */
492 conn->flags &= ~CO_FL_WAIT_L4_CONN;
493 }
494 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200495 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100496 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200497 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100498 conn->err_code = CO_ER_FREE_PORTS;
499 }
500 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200501 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100502 conn->err_code = CO_ER_ADDR_INUSE;
503 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200504
Willy Tarreaub1719512012-12-08 23:03:28 +0100505 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200506 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
507 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200508 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100509 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100510 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200511 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200512 } else if (errno == ETIMEDOUT) {
513 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200514 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
515 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200516 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100517 conn->err_code = CO_ER_SOCK_ERR;
518 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200519 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200520 } else {
521 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
522 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200523 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
524 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200525 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100526 conn->err_code = CO_ER_SOCK_ERR;
527 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200528 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200529 }
530 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100531 else {
532 /* connect() == 0, this is great! */
533 conn->flags &= ~CO_FL_WAIT_L4_CONN;
534 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200535
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100536 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200537
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200538 /* Prepare to send a few handshakes related to the on-wire protocol. */
539 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200540 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200541
Willy Tarreauf79c8172013-10-21 16:30:56 +0200542 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100543 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200544
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200545 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200546 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100547 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200548 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200549 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200550
Olivier Houchardc2aae742017-09-22 18:26:28 +0200551 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN | CO_FL_EARLY_SSL_HS)) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100552 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
Olivier Houchardc2aae742017-09-22 18:26:28 +0200553 if (conn->flags & CO_FL_EARLY_SSL_HS)
554 conn_xprt_want_send(conn);
Willy Tarreau819efbf2017-01-25 14:12:22 +0100555 }
556 else {
557 /* If there's no more handshake, we need to notify the data
558 * layer when the connection is already OK otherwise we'll have
559 * no other opportunity to do it later (eg: health checks).
560 */
561 data = 1;
562 }
563
Willy Tarreau14f8e862012-08-30 22:23:13 +0200564 if (data)
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200565 conn_xprt_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200566
Willy Tarreaue7dff022015-04-03 01:14:29 +0200567 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200568}
569
570
Willy Tarreau59b94792012-05-11 16:16:40 +0200571/*
572 * Retrieves the source address for the socket <fd>, with <dir> indicating
573 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
574 * success, -1 in case of error. The socket's source address is stored in
575 * <sa> for <salen> bytes.
576 */
577int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
578{
579 if (dir)
580 return getsockname(fd, sa, &salen);
581 else
582 return getpeername(fd, sa, &salen);
583}
584
585
586/*
587 * Retrieves the original destination address for the socket <fd>, with <dir>
588 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
589 * listener, if the original destination address was translated, the original
590 * address is retrieved. It returns 0 in case of success, -1 in case of error.
591 * The socket's source address is stored in <sa> for <salen> bytes.
592 */
593int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
594{
595 if (dir)
596 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100597 else {
598 int ret = getsockname(fd, sa, &salen);
599
600 if (ret < 0)
601 return ret;
602
Willy Tarreau59b94792012-05-11 16:16:40 +0200603#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100604 /* For TPROXY and Netfilter's NAT, we can retrieve the original
605 * IPv4 address before DNAT/REDIRECT. We must not do that with
606 * other families because v6-mapped IPv4 addresses are still
607 * reported as v4.
608 */
609 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
610 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
611 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200612#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100613 return ret;
614 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200615}
616
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200617/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100618 * and we have nothing to send. It updates the FD polling status. It returns 0
619 * if it fails in a fatal way or needs to poll to go further, otherwise it
620 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
621 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
622 * errno. The error checking is done in two passes in order to limit the number
623 * of syscalls in the normal case :
624 * - if POLL_ERR was reported by the poller, we check for a pending error on
625 * the socket before proceeding. If found, it's assigned to errno so that
626 * upper layers can see it.
627 * - otherwise connect() is used to check the connection state again, since
628 * the getsockopt return cannot reliably be used to know if the connection
629 * is still pending or ready. This one may often return an error as well,
630 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200631 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200632int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200633{
Willy Tarreau585744b2017-08-24 14:31:19 +0200634 int fd = conn->handle.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100635 socklen_t lskerr;
636 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200637
Willy Tarreau80184712012-07-06 14:54:49 +0200638 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200639 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200640
Willy Tarreau3c728722014-01-23 13:50:42 +0100641 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200642 return 0;
643
Willy Tarreau80184712012-07-06 14:54:49 +0200644 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200645 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200646
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100647 if (!fd_send_ready(fd))
648 return 0;
649
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100650 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
651 * without FD_POLL_IN also indicates a hangup without input data meaning
652 * there was no connection.
653 */
654 if (fdtab[fd].ev & FD_POLL_ERR ||
655 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
656 skerr = 0;
657 lskerr = sizeof(skerr);
658 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
659 errno = skerr;
660 if (errno == EAGAIN)
661 errno = 0;
662 if (errno)
663 goto out_error;
664 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200665
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100666 /* Use connect() to check the state of the socket. This has the
667 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200668 * - error
669 * - connecting (EALREADY, EINPROGRESS)
670 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200671 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200672 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200673 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100674 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100675 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200676 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200677 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200678
Willy Tarreau2c6be842012-07-06 17:12:34 +0200679 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200680 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200681
Willy Tarreau2c6be842012-07-06 17:12:34 +0200682 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200683 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200684
Willy Tarreau076be252012-07-06 16:02:29 +0200685 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200686 * forward the event to the transport layer which will notify the
687 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200688 */
Willy Tarreau80184712012-07-06 14:54:49 +0200689 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200690 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200691
692 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200693 /* Write error on the file descriptor. Report it to the connection
694 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200695 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100696 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100697 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100698 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200699 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200700}
701
Olivier Houchard153659f2017-04-05 22:39:56 +0200702/* XXX: Should probably be elsewhere */
703static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
704{
705 if (a->ss_family != b->ss_family) {
706 return (-1);
707 }
708 switch (a->ss_family) {
709 case AF_INET:
710 {
711 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
712 if (a4->sin_port != b4->sin_port)
713 return (-1);
714 return (memcmp(&a4->sin_addr, &b4->sin_addr,
715 sizeof(a4->sin_addr)));
716 }
717 case AF_INET6:
718 {
719 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
720 if (a6->sin6_port != b6->sin6_port)
721 return (-1);
722 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
723 sizeof(a6->sin6_addr)));
724 }
725 default:
726 return (-1);
727 }
728
729}
730
731#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
732/* When binding the listeners, check if a socket has been sent to us by the
733 * previous process that we could reuse, instead of creating a new one.
734 */
735static int tcp_find_compatible_fd(struct listener *l)
736{
737 struct xfer_sock_list *xfer_sock = xfer_sock_list;
738 int ret = -1;
739
740 while (xfer_sock) {
741 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
742 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
743 (l->interface != NULL && xfer_sock->iface != NULL &&
744 !strcmp(l->interface, xfer_sock->iface))) {
745 if ((l->options & LI_MANDATORY_FLAGS) ==
746 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
747 if ((xfer_sock->namespace == NULL &&
748 l->netns == NULL)
749#ifdef CONFIG_HAP_NS
750 || (xfer_sock->namespace != NULL &&
751 l->netns != NULL &&
752 !strcmp(xfer_sock->namespace,
753 l->netns->node.key))
754#endif
755 ) {
756 break;
757 }
758
759 }
760 }
761 }
762 xfer_sock = xfer_sock->next;
763 }
764 if (xfer_sock != NULL) {
765 ret = xfer_sock->fd;
766 if (xfer_sock == xfer_sock_list)
767 xfer_sock_list = xfer_sock->next;
768 if (xfer_sock->prev)
769 xfer_sock->prev->next = xfer_sock->next;
770 if (xfer_sock->next)
771 xfer_sock->next->prev = xfer_sock->prev;
772 free(xfer_sock->iface);
773 free(xfer_sock->namespace);
774 free(xfer_sock);
775 }
776 return ret;
777}
778#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200779
Willy Tarreaue6b98942007-10-29 01:09:36 +0100780/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100781 * an error message in <errmsg> if the message is at most <errlen> bytes long
782 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
783 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100784 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
785 * was alright and that no message was returned. ERR_RETRYABLE means that an
786 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700787 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100788 * the meaning of the error, but just indicate that a message is present which
789 * should be displayed with the respective level. Last, ERR_ABORT indicates
790 * that it's pointless to try to start other listeners. No error message is
791 * returned if errlen is NULL.
792 */
793int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
794{
795 __label__ tcp_return, tcp_close_return;
796 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100797 int ext, ready;
798 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100799 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200800#ifdef TCP_MAXSEG
801
802 /* Create a temporary TCP socket to get default parameters we can't
803 * guess.
804 * */
805 ready_len = sizeof(default_tcp_maxseg);
806 if (default_tcp_maxseg == -1) {
807 default_tcp_maxseg = -2;
808 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
809 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100810 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200811 else {
812 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
813 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100814 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200815 }
816 close(fd);
817 }
818 if (default_tcp6_maxseg == -1) {
819 default_tcp6_maxseg = -2;
820 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
821 if (fd >= 0) {
822 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
823 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100824 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200825 close(fd);
826 }
827 }
828#endif
829
Willy Tarreaue6b98942007-10-29 01:09:36 +0100830
831 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100832 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100833 *errmsg = 0;
834
835 if (listener->state != LI_ASSIGNED)
836 return ERR_NONE; /* already bound */
837
838 err = ERR_NONE;
839
Olivier Houchard153659f2017-04-05 22:39:56 +0200840 if (listener->fd == -1)
841 listener->fd = tcp_find_compatible_fd(listener);
842
Willy Tarreau40aa0702013-03-10 23:51:38 +0100843 /* if the listener already has an fd assigned, then we were offered the
844 * fd by an external process (most likely the parent), and we don't want
845 * to create a new socket. However we still want to set a few flags on
846 * the socket.
847 */
848 fd = listener->fd;
849 ext = (fd >= 0);
850
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100851 if (!ext) {
852 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
853
854 if (fd == -1) {
855 err |= ERR_RETRYABLE | ERR_ALERT;
856 msg = "cannot create listening socket";
857 goto tcp_return;
858 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100859 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100860
Willy Tarreaue6b98942007-10-29 01:09:36 +0100861 if (fd >= global.maxsock) {
862 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
863 msg = "not enough free sockets (raise '-n' parameter)";
864 goto tcp_close_return;
865 }
866
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200867 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100868 err |= ERR_FATAL | ERR_ALERT;
869 msg = "cannot make socket non-blocking";
870 goto tcp_close_return;
871 }
872
Willy Tarreau40aa0702013-03-10 23:51:38 +0100873 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100874 /* not fatal but should be reported */
875 msg = "cannot do so_reuseaddr";
876 err |= ERR_ALERT;
877 }
878
879 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900880 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200881 else {
882 struct linger tmplinger;
883 socklen_t len = sizeof(tmplinger);
884 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
885 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
886 tmplinger.l_onoff = 0;
887 tmplinger.l_linger = 0;
888 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
889 sizeof(tmplinger));
890 }
891 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100892
Willy Tarreaue6b98942007-10-29 01:09:36 +0100893#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000894 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
895 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100896 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000897 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100898 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100899#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200900
Willy Tarreau40aa0702013-03-10 23:51:38 +0100901 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200902 switch (listener->addr.ss_family) {
903 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200904 if (1
905#if defined(IP_TRANSPARENT)
906 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
907#endif
908#if defined(IP_FREEBIND)
909 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
910#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200911#if defined(IP_BINDANY)
912 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
913#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200914#if defined(SO_BINDANY)
915 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
916#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200917 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200918 msg = "cannot make listening socket transparent";
919 err |= ERR_ALERT;
920 }
921 break;
922 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200923 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200924#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200925 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
926#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100927#if defined(IP_FREEBIND)
928 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
929#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200930#if defined(IPV6_BINDANY)
931 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
932#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200933#if defined(SO_BINDANY)
934 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
935#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200936 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200937 msg = "cannot make listening socket transparent";
938 err |= ERR_ALERT;
939 }
940 break;
941 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100942 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200943
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100944#ifdef SO_BINDTODEVICE
945 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100946 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100947 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100948 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100949 msg = "cannot bind listener to device";
950 err |= ERR_WARN;
951 }
952 }
953#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400954#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100955 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400956 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200957 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
958 msg = "cannot set MSS";
959 err |= ERR_WARN;
960 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200961 } else if (ext) {
962 int tmpmaxseg = -1;
963 int defaultmss;
964 socklen_t len = sizeof(tmpmaxseg);
965
966 if (listener->addr.ss_family == AF_INET)
967 defaultmss = default_tcp_maxseg;
968 else
969 defaultmss = default_tcp6_maxseg;
970
971 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
972 if (tmpmaxseg != defaultmss && setsockopt(fd, IPPROTO_TCP,
973 TCP_MAXSEG, &defaultmss,
974 sizeof(defaultmss)) == -1) {
975 msg = "cannot set MSS";
976 err |= ERR_WARN;
977 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200978 }
979#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100980#if defined(TCP_USER_TIMEOUT)
981 if (listener->tcp_ut) {
982 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
983 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
984 msg = "cannot set TCP User Timeout";
985 err |= ERR_WARN;
986 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200987 } else
988 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
989 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +0100990#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200991#if defined(TCP_DEFER_ACCEPT)
992 if (listener->options & LI_O_DEF_ACCEPT) {
993 /* defer accept by up to one second */
994 int accept_delay = 1;
995 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
996 msg = "cannot enable DEFER_ACCEPT";
997 err |= ERR_WARN;
998 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200999 } else
1000 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
1001 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001002#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001003#if defined(TCP_FASTOPEN)
1004 if (listener->options & LI_O_TCP_FO) {
1005 /* TFO needs a queue length, let's use the configured backlog */
1006 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
1007 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
1008 msg = "cannot enable TCP_FASTOPEN";
1009 err |= ERR_WARN;
1010 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001011 } else {
1012 socklen_t len;
1013 int qlen;
1014 len = sizeof(qlen);
1015 /* Only disable fast open if it was enabled, we don't want
1016 * the kernel to create a fast open queue if there's none.
1017 */
1018 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
1019 qlen != 0) {
1020 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
1021 sizeof(zero)) == -1) {
1022 msg = "cannot disable TCP_FASTOPEN";
1023 err |= ERR_WARN;
1024 }
1025 }
Willy Tarreau1c862c52012-10-05 16:21:00 +02001026 }
1027#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001028#if defined(IPV6_V6ONLY)
1029 if (listener->options & LI_O_V6ONLY)
1030 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +01001031 else if (listener->options & LI_O_V4V6)
1032 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001033#endif
1034
Willy Tarreau40aa0702013-03-10 23:51:38 +01001035 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001036 err |= ERR_RETRYABLE | ERR_ALERT;
1037 msg = "cannot bind socket";
1038 goto tcp_close_return;
1039 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001040
Willy Tarreau40aa0702013-03-10 23:51:38 +01001041 ready = 0;
1042 ready_len = sizeof(ready);
1043 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
1044 ready = 0;
1045
1046 if (!(ext && ready) && /* only listen if not already done by external process */
1047 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001048 err |= ERR_RETRYABLE | ERR_ALERT;
1049 msg = "cannot listen to socket";
1050 goto tcp_close_return;
1051 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001052
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001053#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001054 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +09001055 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +02001056 else
1057 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001058#endif
1059
Willy Tarreaue6b98942007-10-29 01:09:36 +01001060 /* the socket is ready */
1061 listener->fd = fd;
1062 listener->state = LI_LISTEN;
1063
Willy Tarreaua9786b62018-01-25 07:22:13 +01001064 fd_insert(fd, listener, listener->proto->accept,
1065 listener->bind_conf->bind_thread[relative_pid-1] ?
1066 listener->bind_conf->bind_thread[relative_pid-1] : MAX_THREADS_MASK);
Willy Tarreaueb472682010-05-28 18:46:57 +02001067
Willy Tarreaue6b98942007-10-29 01:09:36 +01001068 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001069 if (msg && errlen) {
1070 char pn[INET6_ADDRSTRLEN];
1071
Willy Tarreau631f01c2011-09-05 00:36:48 +02001072 addr_to_str(&listener->addr, pn, sizeof(pn));
1073 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001074 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001075 return err;
1076
1077 tcp_close_return:
1078 close(fd);
1079 goto tcp_return;
1080}
1081
1082/* This function creates all TCP sockets bound to the protocol entry <proto>.
1083 * It is intended to be used as the protocol's bind_all() function.
1084 * The sockets will be registered but not added to any fd_set, in order not to
1085 * loose them across the fork(). A call to enable_all_listeners() is needed
1086 * to complete initialization. The return value is composed from ERR_*.
1087 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001088static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001089{
1090 struct listener *listener;
1091 int err = ERR_NONE;
1092
1093 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001094 err |= tcp_bind_listener(listener, errmsg, errlen);
1095 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001096 break;
1097 }
1098
1099 return err;
1100}
1101
Willy Tarreau32282382017-09-15 07:44:44 +02001102/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1103 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1104 * The number of listeners for the protocol is updated.
Willy Tarreaue6b98942007-10-29 01:09:36 +01001105 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001106static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001107{
1108 if (listener->state != LI_INIT)
1109 return;
1110 listener->state = LI_ASSIGNED;
1111 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001112 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001113 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1114 proto_tcpv4.nb_listeners++;
1115}
1116
Willy Tarreau32282382017-09-15 07:44:44 +02001117/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1118 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1119 * The number of listeners for the protocol is updated.
Willy Tarreaue6b98942007-10-29 01:09:36 +01001120 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001121static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001122{
1123 if (listener->state != LI_INIT)
1124 return;
1125 listener->state = LI_ASSIGNED;
1126 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001127 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001128 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1129 proto_tcpv6.nb_listeners++;
1130}
1131
Willy Tarreau092d8652014-07-07 20:22:12 +02001132/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1133 * was totally stopped, or > 0 if correctly paused.
1134 */
1135int tcp_pause_listener(struct listener *l)
1136{
1137 if (shutdown(l->fd, SHUT_WR) != 0)
1138 return -1; /* Solaris dies here */
1139
1140 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1141 return -1; /* OpenBSD dies here */
1142
1143 if (shutdown(l->fd, SHUT_RD) != 0)
1144 return -1; /* should always be OK */
1145 return 1;
1146}
1147
William Lallemand2e785f22016-05-25 01:48:42 +02001148/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001149 * Execute the "set-src" action. May be called from {tcp,http}request.
1150 * It only changes the address and tries to preserve the original port. If the
1151 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001152 */
1153enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1154 struct session *sess, struct stream *s, int flags)
1155{
1156 struct connection *cli_conn;
1157
1158 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1159 struct sample *smp;
1160
1161 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1162 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001163 int port = get_net_port(&cli_conn->addr.from);
1164
William Lallemand2e785f22016-05-25 01:48:42 +02001165 if (smp->data.type == SMP_T_IPV4) {
1166 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
1167 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001168 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001169 } else if (smp->data.type == SMP_T_IPV6) {
1170 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
1171 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Willy Tarreau00005ce2016-10-21 15:07:45 +02001172 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001173 }
1174 }
William Lallemand01252ed2016-05-25 02:33:16 +02001175 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001176 }
1177 return ACT_RET_CONT;
1178}
1179
William Lallemand44be6402016-05-25 01:51:35 +02001180/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001181 * Execute the "set-dst" action. May be called from {tcp,http}request.
1182 * It only changes the address and tries to preserve the original port. If the
1183 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001184 */
1185enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1186 struct session *sess, struct stream *s, int flags)
1187{
1188 struct connection *cli_conn;
1189
1190 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1191 struct sample *smp;
1192
1193 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1194 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001195 int port = get_net_port(&cli_conn->addr.to);
1196
William Lallemand13e9b0c2016-05-25 02:34:07 +02001197 if (smp->data.type == SMP_T_IPV4) {
1198 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_family = AF_INET;
1199 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1200 } else if (smp->data.type == SMP_T_IPV6) {
1201 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_family = AF_INET6;
1202 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Willy Tarreau00005ce2016-10-21 15:07:45 +02001203 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001204 }
1205 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1206 }
1207 }
1208 return ACT_RET_CONT;
1209}
1210
1211/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001212 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1213 * We must test the sin_family before setting the port. If the address family
1214 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1215 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001216 */
1217enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1218 struct session *sess, struct stream *s, int flags)
1219{
1220 struct connection *cli_conn;
1221
1222 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1223 struct sample *smp;
1224
1225 conn_get_from_addr(cli_conn);
1226
1227 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1228 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001229 if (cli_conn->addr.from.ss_family == AF_INET6) {
William Lallemand44be6402016-05-25 01:51:35 +02001230 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001231 } else {
1232 if (cli_conn->addr.from.ss_family != AF_INET) {
1233 cli_conn->addr.from.ss_family = AF_INET;
1234 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1235 }
1236 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001237 }
1238 }
1239 }
1240 return ACT_RET_CONT;
1241}
1242
William Lallemand13e9b0c2016-05-25 02:34:07 +02001243/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001244 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1245 * We must test the sin_family before setting the port. If the address family
1246 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1247 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001248 */
1249enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1250 struct session *sess, struct stream *s, int flags)
1251{
1252 struct connection *cli_conn;
1253
1254 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1255 struct sample *smp;
1256
1257 conn_get_to_addr(cli_conn);
1258
1259 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1260 if (smp) {
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001261 if (cli_conn->addr.to.ss_family == AF_INET6) {
1262 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001263 } else {
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001264 if (cli_conn->addr.to.ss_family != AF_INET) {
1265 cli_conn->addr.to.ss_family = AF_INET;
1266 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001267 }
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001268 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001269 }
1270 }
1271 }
1272 return ACT_RET_CONT;
1273}
1274
Willy Tarreau2d392c22015-08-24 01:43:45 +02001275/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1276static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1277{
1278 struct connection *conn = objt_conn(sess->origin);
1279
1280 if (!conn)
1281 goto out;
1282
1283 if (!conn_ctrl_ready(conn))
1284 goto out;
1285
Willy Tarreau2d392c22015-08-24 01:43:45 +02001286#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001287 /* drain is needed only to send the quick ACK */
1288 conn_sock_drain(conn);
1289
Willy Tarreau2d392c22015-08-24 01:43:45 +02001290 /* re-enable quickack if it was disabled to ack all data and avoid
1291 * retransmits from the client that might trigger a real reset.
1292 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001293 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001294#endif
1295 /* lingering must absolutely be disabled so that we don't send a
1296 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1297 * is present, returning with ERR will cause lingering to be disabled.
1298 */
1299 if (strm)
1300 strm->si[0].flags |= SI_FL_NOLINGER;
1301
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001302 /* We're on the client-facing side, we must force to disable lingering to
1303 * ensure we will use an RST exclusively and kill any pending data.
1304 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001305 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001306
Willy Tarreau2d392c22015-08-24 01:43:45 +02001307#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001308 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001309 /* socket will be quiet now */
1310 goto out;
1311 }
1312#endif
1313 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1314 * Let's fall back on the TTL trick, though it only works for routed
1315 * network and has no effect on local net.
1316 */
1317#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001318 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001319#endif
1320 out:
1321 /* kill the stream if any */
1322 if (strm) {
1323 channel_abort(&strm->req);
1324 channel_abort(&strm->res);
1325 strm->req.analysers = 0;
1326 strm->res.analysers = 0;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001327 HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001328 if (!(strm->flags & SF_ERR_MASK))
1329 strm->flags |= SF_ERR_PRXCOND;
1330 if (!(strm->flags & SF_FINST_MASK))
1331 strm->flags |= SF_FINST_R;
1332 }
1333
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001334 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001335 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001336 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001337
1338 return ACT_RET_STOP;
1339}
1340
William Lallemand13e9b0c2016-05-25 02:34:07 +02001341/* parse "set-{src,dst}[-port]" action */
1342enum 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 +02001343{
1344 int cur_arg;
1345 struct sample_expr *expr;
1346 unsigned int where;
1347
1348 cur_arg = *orig_arg;
1349 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
1350 if (!expr)
1351 return ACT_RET_PRS_ERR;
1352
1353 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001354 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001355 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001356 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001357 where |= SMP_VAL_BE_HRQ_HDR;
1358
1359 if (!(expr->fetch->val & where)) {
1360 memprintf(err,
1361 "fetch method '%s' extracts information from '%s', none of which is available here",
1362 args[cur_arg-1], sample_src_names(expr->fetch->use));
1363 free(expr);
1364 return ACT_RET_PRS_ERR;
1365 }
1366 rule->arg.expr = expr;
1367 rule->action = ACT_CUSTOM;
1368
1369 if (!strcmp(args[*orig_arg-1], "set-src")) {
1370 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001371 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1372 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001373 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1374 rule->action_ptr = tcp_action_req_set_dst;
1375 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1376 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001377 } else {
1378 return ACT_RET_PRS_ERR;
1379 }
1380
1381 (*orig_arg)++;
1382
1383 return ACT_RET_PRS_OK;
1384}
1385
1386
Willy Tarreau2d392c22015-08-24 01:43:45 +02001387/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1388 * success, ACT_RET_PRS_ERR on error.
1389 */
1390static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1391 struct act_rule *rule, char **err)
1392{
1393 rule->action = ACT_CUSTOM;
1394 rule->action_ptr = tcp_exec_action_silent_drop;
1395 return ACT_RET_PRS_OK;
1396}
1397
Willy Tarreau645513a2010-05-24 20:55:15 +02001398
1399/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001400/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001401/************************************************************************/
1402
Willy Tarreau4a129812012-04-25 17:31:42 +02001403/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001404int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001405{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001406 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001407
1408 if (!cli_conn)
1409 return 0;
1410
1411 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001412 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001413 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001414 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001415 break;
1416 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001417 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001418 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001419 break;
1420 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001421 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001422 }
Emeric Brunf769f512010-10-22 17:14:01 +02001423
Willy Tarreau37406352012-04-23 16:16:37 +02001424 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001425 return 1;
1426}
1427
Willy Tarreaua5e37562011-12-16 17:06:15 +01001428/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001429static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001430smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001431{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001432 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001433
1434 if (!cli_conn)
1435 return 0;
1436
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001437 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001438 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001439 return 0;
1440
Willy Tarreau37406352012-04-23 16:16:37 +02001441 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001442 return 1;
1443}
1444
Willy Tarreau4a129812012-04-25 17:31:42 +02001445/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001446static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001447smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001448{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001449 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001450
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001451 if (!cli_conn)
1452 return 0;
1453
1454 conn_get_to_addr(cli_conn);
1455
1456 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001457 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001458 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001459 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001460 break;
1461 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001462 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001463 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001464 break;
1465 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001466 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001467 }
Emeric Brunf769f512010-10-22 17:14:01 +02001468
Willy Tarreau37406352012-04-23 16:16:37 +02001469 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001470 return 1;
1471}
1472
Willy Tarreau16e01562016-08-09 16:46:18 +02001473/* check if the destination address of the front connection is local to the
1474 * system or if it was intercepted.
1475 */
1476int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1477{
1478 struct connection *conn = objt_conn(smp->sess->origin);
1479 struct listener *li = smp->sess->listener;
1480
1481 if (!conn)
1482 return 0;
1483
1484 conn_get_to_addr(conn);
1485 if (!(conn->flags & CO_FL_ADDR_TO_SET))
1486 return 0;
1487
1488 smp->data.type = SMP_T_BOOL;
1489 smp->flags = 0;
1490 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.to);
1491 return smp->data.u.sint >= 0;
1492}
1493
1494/* check if the source address of the front connection is local to the system
1495 * or not.
1496 */
1497int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1498{
1499 struct connection *conn = objt_conn(smp->sess->origin);
1500 struct listener *li = smp->sess->listener;
1501
1502 if (!conn)
1503 return 0;
1504
1505 conn_get_from_addr(conn);
1506 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
1507 return 0;
1508
1509 smp->data.type = SMP_T_BOOL;
1510 smp->flags = 0;
1511 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.from);
1512 return smp->data.u.sint >= 0;
1513}
1514
Willy Tarreaua5e37562011-12-16 17:06:15 +01001515/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001516static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001517smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001518{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001519 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001520
1521 if (!cli_conn)
1522 return 0;
1523
1524 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001525
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001526 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001527 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001528 return 0;
1529
Willy Tarreau37406352012-04-23 16:16:37 +02001530 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001531 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001532}
1533
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001534#ifdef TCP_INFO
1535
1536/* Returns some tcp_info data is its avalaible. "dir" must be set to 0 if
1537 * the client connection is require, otherwise it is set to 1. "val" represents
1538 * the required value. Use 0 for rtt and 1 for rttavg. "unit" is the expected unit
1539 * by default, the rtt is in us. Id "unit" is set to 0, the unit is us, if it is
1540 * set to 1, the untis are milliseconds.
1541 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1542 */
1543static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1544 int dir, int val)
1545{
1546 struct connection *conn;
1547 struct tcp_info info;
1548 socklen_t optlen;
1549
1550 /* strm can be null. */
1551 if (!smp->strm)
1552 return 0;
1553
1554 /* get the object associated with the stream interface.The
1555 * object can be other thing than a connection. For example,
1556 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001557 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001558 if (!conn)
1559 return 0;
1560
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001561 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001562 syscal can fail. */
1563 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001564 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001565 return 0;
1566
1567 /* extract the value. */
1568 smp->data.type = SMP_T_SINT;
1569 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001570 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1571 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1572#if defined(__linux__)
1573 /* these ones are common to all Linux versions */
1574 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1575 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1576 case 4: smp->data.u.sint = info.tcpi_lost; break;
1577 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1578 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1579 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1580#elif defined(__FreeBSD__) || defined(__NetBSD__)
1581 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1582 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1583 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1584 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1585 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1586 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1587 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1588#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001589 default: return 0;
1590 }
1591
1592 /* Convert the value as expected. */
1593 if (args) {
1594 if (args[0].type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001595 if (strcmp(args[0].data.str.area, "us") == 0) {
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001596 /* Do nothing. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001597 } else if (strcmp(args[0].data.str.area, "ms") == 0) {
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001598 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1599 } else
1600 return 0;
1601 } else if (args[0].type == ARGT_STOP) {
1602 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1603 } else
1604 return 0;
1605 }
1606
1607 return 1;
1608}
1609
1610/* get the mean rtt of a client connexion */
1611static int
1612smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1613{
1614 if (!get_tcp_info(args, smp, 0, 0))
1615 return 0;
1616 return 1;
1617}
1618
1619/* get the variance of the mean rtt of a client connexion */
1620static int
1621smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1622{
1623 if (!get_tcp_info(args, smp, 0, 1))
1624 return 0;
1625 return 1;
1626}
Joe Williams30fcd392016-08-10 07:06:44 -07001627
1628#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1629
1630/* get the unacked counter on a client connexion */
1631static int
1632smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1633{
1634 if (!get_tcp_info(args, smp, 0, 2))
1635 return 0;
1636 return 1;
1637}
1638
1639/* get the sacked counter on a client connexion */
1640static int
1641smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1642{
1643 if (!get_tcp_info(args, smp, 0, 3))
1644 return 0;
1645 return 1;
1646}
1647
1648/* get the lost counter on a client connexion */
1649static int
1650smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1651{
1652 if (!get_tcp_info(args, smp, 0, 4))
1653 return 0;
1654 return 1;
1655}
1656
1657/* get the retrans counter on a client connexion */
1658static int
1659smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1660{
1661 if (!get_tcp_info(args, smp, 0, 5))
1662 return 0;
1663 return 1;
1664}
1665
1666/* get the fackets counter on a client connexion */
1667static int
1668smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1669{
1670 if (!get_tcp_info(args, smp, 0, 6))
1671 return 0;
1672 return 1;
1673}
1674
1675/* get the reordering counter on a client connexion */
1676static int
1677smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1678{
1679 if (!get_tcp_info(args, smp, 0, 7))
1680 return 0;
1681 return 1;
1682}
1683#endif // linux || freebsd || netbsd
1684#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001685
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001686#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001687/* parse the "v4v6" bind keyword */
1688static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1689{
1690 struct listener *l;
1691
1692 list_for_each_entry(l, &conf->listeners, by_bind) {
1693 if (l->addr.ss_family == AF_INET6)
1694 l->options |= LI_O_V4V6;
1695 }
1696
1697 return 0;
1698}
1699
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001700/* parse the "v6only" bind keyword */
1701static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1702{
1703 struct listener *l;
1704
1705 list_for_each_entry(l, &conf->listeners, by_bind) {
1706 if (l->addr.ss_family == AF_INET6)
1707 l->options |= LI_O_V6ONLY;
1708 }
1709
1710 return 0;
1711}
1712#endif
1713
Pieter Baauwd551fb52013-05-08 22:49:23 +02001714#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001715/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001716static 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 +02001717{
1718 struct listener *l;
1719
Willy Tarreau4348fad2012-09-20 16:48:07 +02001720 list_for_each_entry(l, &conf->listeners, by_bind) {
1721 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1722 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001723 }
1724
Willy Tarreau44791242012-09-12 23:27:21 +02001725 return 0;
1726}
1727#endif
1728
1729#ifdef TCP_DEFER_ACCEPT
1730/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001731static 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 +02001732{
1733 struct listener *l;
1734
Willy Tarreau4348fad2012-09-20 16:48:07 +02001735 list_for_each_entry(l, &conf->listeners, by_bind) {
1736 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1737 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001738 }
1739
Willy Tarreau44791242012-09-12 23:27:21 +02001740 return 0;
1741}
1742#endif
1743
Willy Tarreau1c862c52012-10-05 16:21:00 +02001744#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001745/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001746static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1747{
1748 struct listener *l;
1749
1750 list_for_each_entry(l, &conf->listeners, by_bind) {
1751 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1752 l->options |= LI_O_TCP_FO;
1753 }
1754
1755 return 0;
1756}
1757#endif
1758
Willy Tarreau44791242012-09-12 23:27:21 +02001759#ifdef TCP_MAXSEG
1760/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001761static 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 +02001762{
1763 struct listener *l;
1764 int mss;
1765
Willy Tarreau44791242012-09-12 23:27:21 +02001766 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001767 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001768 return ERR_ALERT | ERR_FATAL;
1769 }
1770
1771 mss = atoi(args[cur_arg + 1]);
1772 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001773 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001774 return ERR_ALERT | ERR_FATAL;
1775 }
1776
Willy Tarreau4348fad2012-09-20 16:48:07 +02001777 list_for_each_entry(l, &conf->listeners, by_bind) {
1778 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1779 l->maxseg = mss;
1780 }
Willy Tarreau44791242012-09-12 23:27:21 +02001781
1782 return 0;
1783}
1784#endif
1785
Willy Tarreau2af207a2015-02-04 00:45:58 +01001786#ifdef TCP_USER_TIMEOUT
1787/* parse the "tcp-ut" bind keyword */
1788static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1789{
1790 const char *ptr = NULL;
1791 struct listener *l;
1792 unsigned int timeout;
1793
1794 if (!*args[cur_arg + 1]) {
1795 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1796 return ERR_ALERT | ERR_FATAL;
1797 }
1798
1799 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
1800 if (ptr) {
1801 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1802 return ERR_ALERT | ERR_FATAL;
1803 }
1804
1805 list_for_each_entry(l, &conf->listeners, by_bind) {
1806 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1807 l->tcp_ut = timeout;
1808 }
1809
1810 return 0;
1811}
1812#endif
1813
Willy Tarreau44791242012-09-12 23:27:21 +02001814#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001815/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001816static int bind_parse_interface(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001817{
1818 struct listener *l;
1819
Willy Tarreau44791242012-09-12 23:27:21 +02001820 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001821 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001822 return ERR_ALERT | ERR_FATAL;
1823 }
1824
Willy Tarreau4348fad2012-09-20 16:48:07 +02001825 list_for_each_entry(l, &conf->listeners, by_bind) {
1826 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1827 l->interface = strdup(args[cur_arg + 1]);
1828 }
Willy Tarreau44791242012-09-12 23:27:21 +02001829
Willy Tarreau44791242012-09-12 23:27:21 +02001830 return 0;
1831}
1832#endif
1833
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001834#ifdef CONFIG_HAP_NS
1835/* parse the "namespace" bind keyword */
1836static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1837{
1838 struct listener *l;
1839 char *namespace = NULL;
1840
1841 if (!*args[cur_arg + 1]) {
1842 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1843 return ERR_ALERT | ERR_FATAL;
1844 }
1845 namespace = args[cur_arg + 1];
1846
1847 list_for_each_entry(l, &conf->listeners, by_bind) {
1848 l->netns = netns_store_lookup(namespace, strlen(namespace));
1849
1850 if (l->netns == NULL)
1851 l->netns = netns_store_insert(namespace);
1852
1853 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001854 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001855 return ERR_ALERT | ERR_FATAL;
1856 }
1857 }
1858 return 0;
1859}
1860#endif
1861
Willy Tarreau163d4622015-10-13 16:16:41 +02001862#ifdef TCP_USER_TIMEOUT
1863/* parse the "tcp-ut" server keyword */
1864static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1865{
1866 const char *ptr = NULL;
1867 unsigned int timeout;
1868
1869 if (!*args[*cur_arg + 1]) {
1870 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1871 return ERR_ALERT | ERR_FATAL;
1872 }
1873
1874 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
1875 if (ptr) {
1876 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1877 return ERR_ALERT | ERR_FATAL;
1878 }
1879
1880 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1881 newsrv->tcp_ut = timeout;
1882
1883 return 0;
1884}
1885#endif
1886
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001887
Willy Tarreau4a129812012-04-25 17:31:42 +02001888/* Note: must not be declared <const> as its list will be overwritten.
1889 * Note: fetches that may return multiple types must be declared as the lowest
1890 * common denominator, the type that can be casted into all other ones. For
1891 * instance v4/v6 must be declared v4.
1892 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001893static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001894 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001895 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001896 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001897 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001898 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001899 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001900#ifdef TCP_INFO
Joe Williams30fcd392016-08-10 07:06:44 -07001901 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1902 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1903#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1904 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1905 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1906 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1907 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1908 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1909 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1910#endif // linux || freebsd || netbsd
1911#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001912 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001913}};
1914
Willy Tarreau44791242012-09-12 23:27:21 +02001915/************************************************************************/
1916/* All supported bind keywords must be declared here. */
1917/************************************************************************/
1918
1919/* Note: must not be declared <const> as its list will be overwritten.
1920 * Please take care of keeping this list alphabetically sorted, doing so helps
1921 * all code contributors.
1922 * Optional keywords are also declared with a NULL ->parse() function so that
1923 * the config parser can report an appropriate error when a known keyword was
1924 * not enabled.
1925 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001926static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001927#ifdef TCP_DEFER_ACCEPT
1928 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1929#endif
1930#ifdef SO_BINDTODEVICE
1931 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1932#endif
1933#ifdef TCP_MAXSEG
1934 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1935#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001936#ifdef TCP_USER_TIMEOUT
1937 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1938#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001939#ifdef TCP_FASTOPEN
1940 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1941#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001942#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001943 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1944#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001945#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001946 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001947 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1948#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001949#ifdef CONFIG_HAP_NS
1950 { "namespace", bind_parse_namespace, 1 },
1951#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001952 /* the versions with the NULL parse function*/
1953 { "defer-accept", NULL, 0 },
1954 { "interface", NULL, 1 },
1955 { "mss", NULL, 1 },
1956 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001957 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001958 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001959 { NULL, NULL, 0 },
1960}};
1961
Willy Tarreau163d4622015-10-13 16:16:41 +02001962static struct srv_kw_list srv_kws = { "TCP", { }, {
1963#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001964 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02001965#endif
1966 { NULL, NULL, 0 },
1967}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02001968
1969static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02001970 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02001971 { "set-src", tcp_parse_set_src_dst },
1972 { "set-src-port", tcp_parse_set_src_dst },
1973 { "set-dst" , tcp_parse_set_src_dst },
1974 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02001975 { /* END */ }
1976}};
1977
Willy Tarreau620408f2016-10-21 16:37:51 +02001978static struct action_kw_list tcp_req_sess_actions = {ILH, {
1979 { "silent-drop", tcp_parse_silent_drop },
1980 { "set-src", tcp_parse_set_src_dst },
1981 { "set-src-port", tcp_parse_set_src_dst },
1982 { "set-dst" , tcp_parse_set_src_dst },
1983 { "set-dst-port", tcp_parse_set_src_dst },
1984 { /* END */ }
1985}};
1986
Willy Tarreau2d392c22015-08-24 01:43:45 +02001987static struct action_kw_list tcp_req_cont_actions = {ILH, {
1988 { "silent-drop", tcp_parse_silent_drop },
1989 { /* END */ }
1990}};
1991
1992static struct action_kw_list tcp_res_cont_actions = {ILH, {
1993 { "silent-drop", tcp_parse_silent_drop },
1994 { /* END */ }
1995}};
1996
1997static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02001998 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02001999 { "set-src", tcp_parse_set_src_dst },
2000 { "set-src-port", tcp_parse_set_src_dst },
2001 { "set-dst", tcp_parse_set_src_dst },
2002 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002003 { /* END */ }
2004}};
2005
2006static struct action_kw_list http_res_actions = {ILH, {
2007 { "silent-drop", tcp_parse_silent_drop },
2008 { /* END */ }
2009}};
2010
2011
Willy Tarreaue6b98942007-10-29 01:09:36 +01002012__attribute__((constructor))
2013static void __tcp_protocol_init(void)
2014{
2015 protocol_register(&proto_tcpv4);
2016 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002017 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau44791242012-09-12 23:27:21 +02002018 bind_register_keywords(&bind_kws);
Willy Tarreau163d4622015-10-13 16:16:41 +02002019 srv_register_keywords(&srv_kws);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002020 tcp_req_conn_keywords_register(&tcp_req_conn_actions);
Willy Tarreau620408f2016-10-21 16:37:51 +02002021 tcp_req_sess_keywords_register(&tcp_req_sess_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002022 tcp_req_cont_keywords_register(&tcp_req_cont_actions);
2023 tcp_res_cont_keywords_register(&tcp_res_cont_actions);
2024 http_req_keywords_register(&http_req_actions);
2025 http_res_keywords_register(&http_res_actions);
Willy Tarreauba962912016-12-21 18:55:02 +01002026
2027
2028 hap_register_build_opts("Built with transparent proxy support using:"
2029#if defined(IP_TRANSPARENT)
2030 " IP_TRANSPARENT"
2031#endif
2032#if defined(IPV6_TRANSPARENT)
2033 " IPV6_TRANSPARENT"
2034#endif
2035#if defined(IP_FREEBIND)
2036 " IP_FREEBIND"
2037#endif
2038#if defined(IP_BINDANY)
2039 " IP_BINDANY"
2040#endif
2041#if defined(IPV6_BINDANY)
2042 " IPV6_BINDANY"
2043#endif
2044#if defined(SO_BINDANY)
2045 " SO_BINDANY"
2046#endif
2047 "", 0);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002048}
2049
2050
2051/*
2052 * Local variables:
2053 * c-indent-level: 8
2054 * c-basic-offset: 8
2055 * End:
2056 */