blob: 857ebe133cd276a1558acdcb1b4601ec412131f2 [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 Tarreau2b57cb82013-06-10 19:56:38 +020086 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +020087 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020088 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010089 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
90 .nb_listeners = 0,
91};
92
93/* Note: must not be declared <const> as its list will be overwritten */
94static struct protocol proto_tcpv6 = {
95 .name = "tcpv6",
96 .sock_domain = AF_INET6,
97 .sock_type = SOCK_STREAM,
98 .sock_prot = IPPROTO_TCP,
99 .sock_family = AF_INET6,
100 .sock_addrlen = sizeof(struct sockaddr_in6),
101 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200102 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200103 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200104 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100105 .bind_all = tcp_bind_listeners,
106 .unbind_all = unbind_all_listeners,
107 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200108 .get_src = tcp_get_src,
109 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200110 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +0200111 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200112 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100113 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
114 .nb_listeners = 0,
115};
116
Olivier Houchard153659f2017-04-05 22:39:56 +0200117/* Default TCP parameters, got by opening a temporary TCP socket. */
118#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100119static THREAD_LOCAL int default_tcp_maxseg = -1;
120static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200121#endif
122
David du Colombier6f5ccb12011-03-10 22:26:24 +0100123/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100124 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
125 * - 0 : ignore remote address (may even be a NULL pointer)
126 * - 1 : use provided address
127 * - 2 : use provided port
128 * - 3 : use both
129 *
130 * The function supports multiple foreign binding methods :
131 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100132 * The second one can be used as a fallback for the first one.
133 * This function returns 0 when everything's OK, 1 if it could not bind, to the
134 * local address, 2 if it could not bind to the foreign address.
135 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100136int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100137{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100138 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100139 int foreign_ok = 0;
140 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100141 static THREAD_LOCAL int ip_transp_working = 1;
142 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200143
David du Colombier65c17962012-07-13 14:34:59 +0200144 switch (local->ss_family) {
145 case AF_INET:
146 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200147 /* This deserves some explanation. Some platforms will support
148 * multiple combinations of certain methods, so we try the
149 * supported ones until one succeeds.
150 */
151 if (0
152#if defined(IP_TRANSPARENT)
153 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
154#endif
155#if defined(IP_FREEBIND)
156 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
157#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200158#if defined(IP_BINDANY)
159 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
160#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200161#if defined(SO_BINDANY)
162 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
163#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200164 )
David du Colombier65c17962012-07-13 14:34:59 +0200165 foreign_ok = 1;
166 else
167 ip_transp_working = 0;
168 }
169 break;
170 case AF_INET6:
171 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200172 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200173#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200174 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
175#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100176#if defined(IP_FREEBIND)
177 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
178#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200179#if defined(IPV6_BINDANY)
180 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
181#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200182#if defined(SO_BINDANY)
183 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
184#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200185 )
David du Colombier65c17962012-07-13 14:34:59 +0200186 foreign_ok = 1;
187 else
188 ip6_transp_working = 0;
189 }
190 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100191 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200192
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100193 if (flags) {
194 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200195 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100196 switch (remote->ss_family) {
197 case AF_INET:
198 if (flags & 1)
199 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
200 if (flags & 2)
201 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
202 break;
203 case AF_INET6:
204 if (flags & 1)
205 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
206 if (flags & 2)
207 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
208 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100209 default:
210 /* we don't want to try to bind to an unknown address family */
211 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100212 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100213 }
214
Simon Hormande072bd2011-06-24 15:11:37 +0900215 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100216 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200217 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200218 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
219 if (ret < 0)
220 return 2;
221 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100222 }
223 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200224 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200225 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
226 if (ret < 0)
227 return 1;
228 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100229 }
230
231 if (!flags)
232 return 0;
233
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100234 if (!foreign_ok)
235 /* we could not bind to a foreign address */
236 return 2;
237
238 return 0;
239}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100240
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100241static int create_server_socket(struct connection *conn)
242{
Willy Tarreau529c1392014-12-24 13:47:55 +0100243 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100244
Willy Tarreau529c1392014-12-24 13:47:55 +0100245#ifdef CONFIG_HAP_NS
246 if (objt_server(conn->target)) {
247 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
248 ns = conn->proxy_netns;
249 else
250 ns = __objt_server(conn->target)->netns;
251 }
252#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100253 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
254}
Willy Tarreau9650f372009-08-16 14:02:45 +0200255
256/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200257 * This function initiates a TCP connection establishment to the target assigned
258 * to connection <conn> using (si->{target,addr.to}). A source address may be
259 * pointed to by conn->addr.from in case of transparent proxying. Normal source
260 * bind addresses are still determined locally (due to the possible need of a
261 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100262 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100263 * supported. The <data> parameter is a boolean indicating whether there are data
264 * waiting for being sent or not, in order to adjust data write polling and on
265 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
266 * allows the caller to force using a delayed ACK when establishing the connection :
267 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
268 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
269 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200270 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200271 * Note that a pending send_proxy message accounts for data.
272 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200273 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200274 * - SF_ERR_NONE if everything's OK
275 * - SF_ERR_SRVTO if there are no more servers
276 * - SF_ERR_SRVCL if the connection was refused by the server
277 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
278 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
279 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100280 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100281 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200282 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100283 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200284 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100285
Willy Tarreauf0837b22012-11-24 10:24:27 +0100286int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200287{
288 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100289 struct server *srv;
290 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100291 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100292
Willy Tarreau9ce70132014-01-24 16:08:19 +0100293 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
294
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100295 switch (obj_type(conn->target)) {
296 case OBJ_TYPE_PROXY:
297 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100298 srv = NULL;
299 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100300 case OBJ_TYPE_SERVER:
301 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100302 be = srv->proxy;
303 break;
304 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100305 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200306 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100307 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200308
Willy Tarreau585744b2017-08-24 14:31:19 +0200309 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100310
311 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200312 qfprintf(stderr, "Cannot get a server socket.\n");
313
Willy Tarreau9ce70132014-01-24 16:08:19 +0100314 if (errno == ENFILE) {
315 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200316 send_log(be, LOG_EMERG,
317 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
318 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100319 }
320 else if (errno == EMFILE) {
321 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200322 send_log(be, LOG_EMERG,
323 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
324 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100325 }
326 else if (errno == ENOBUFS || errno == ENOMEM) {
327 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200328 send_log(be, LOG_EMERG,
329 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
330 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100331 }
332 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
333 conn->err_code = CO_ER_NOPROTO;
334 }
335 else
336 conn->err_code = CO_ER_SOCK_ERR;
337
Willy Tarreau9650f372009-08-16 14:02:45 +0200338 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100339 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200340 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200341 }
342
343 if (fd >= global.maxsock) {
344 /* do not log anything there, it's a normal condition when this option
345 * is used to serialize connections to a server !
346 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100347 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200348 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100349 conn->err_code = CO_ER_CONF_FDLIM;
350 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200351 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 }
353
354 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900355 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
357 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100358 conn->err_code = CO_ER_SOCK_ERR;
359 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200360 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200361 }
362
363 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900364 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200365
Willy Tarreau9650f372009-08-16 14:02:45 +0200366 /* allow specific binding :
367 * - server-specific at first
368 * - proxy-specific next
369 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100370 if (srv && srv->conn_src.opts & CO_SRC_BIND)
371 src = &srv->conn_src;
372 else if (be->conn_src.opts & CO_SRC_BIND)
373 src = &be->conn_src;
374 else
375 src = NULL;
376
377 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200378 int ret, flags = 0;
379
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200380 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100381 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100382 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200383 conn->flags |= CO_FL_PRIVATE;
384 /* fall through */
385 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200386 flags = 3;
387 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100388 case CO_SRC_TPROXY_CIP:
389 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200390 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200391 flags = 1;
392 break;
393 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200394 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200395
Willy Tarreau9650f372009-08-16 14:02:45 +0200396#ifdef SO_BINDTODEVICE
397 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100398 if (src->iface_name)
399 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200400#endif
401
Willy Tarreaua4380b42012-12-08 22:49:11 +0100402 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200403 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100404 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200405
406 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200407 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200408
409 do {
410 /* note: in case of retry, we may have to release a previously
411 * allocated port, hence this loop's construct.
412 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200413 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
414 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200415
416 if (!attempts)
417 break;
418 attempts--;
419
Willy Tarreaua4380b42012-12-08 22:49:11 +0100420 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100421 if (!fdinfo[fd].local_port) {
422 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200423 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100424 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200425
Willy Tarreaua4380b42012-12-08 22:49:11 +0100426 fdinfo[fd].port_range = src->sport_range;
427 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200428
Willy Tarreaua4380b42012-12-08 22:49:11 +0100429 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100430 if (ret != 0)
431 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200432 } while (ret != 0); /* binding NOK */
433 }
434 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000435#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100436 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000437 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
438#endif
Willy Tarreaua4380b42012-12-08 22:49:11 +0100439 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100440 if (ret != 0)
441 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 }
443
Willy Tarreaua4380b42012-12-08 22:49:11 +0100444 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200445 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
446 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200447 close(fd);
448
Willy Tarreau9650f372009-08-16 14:02:45 +0200449 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100450 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
451 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100453 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 be->id);
455 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100456 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
457 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100459 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200460 be->id);
461 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100462 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200463 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200464 }
465 }
466
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400467#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200468 /* disabling tcp quick ack now allows the first request to leave the
469 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100470 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200471 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200472 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900473 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200474#endif
475
Willy Tarreau163d4622015-10-13 16:16:41 +0200476#ifdef TCP_USER_TIMEOUT
477 /* there is not much more we can do here when it fails, it's still minor */
478 if (srv && srv->tcp_ut)
479 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
480#endif
Willy Tarreaue803de22010-01-21 17:43:04 +0100481 if (global.tune.server_sndbuf)
482 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
483
484 if (global.tune.server_rcvbuf)
485 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
486
Willy Tarreau819efbf2017-01-25 14:12:22 +0100487 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
488 if (errno == EINPROGRESS || errno == EALREADY) {
489 /* common case, let's wait for connect status */
490 conn->flags |= CO_FL_WAIT_L4_CONN;
491 }
492 else if (errno == EISCONN) {
493 /* should normally not happen but if so, indicates that it's OK */
494 conn->flags &= ~CO_FL_WAIT_L4_CONN;
495 }
496 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200497 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100498 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200499 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100500 conn->err_code = CO_ER_FREE_PORTS;
501 }
502 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200503 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100504 conn->err_code = CO_ER_ADDR_INUSE;
505 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200506
Willy Tarreaub1719512012-12-08 23:03:28 +0100507 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200508 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
509 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200510 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100511 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100512 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200513 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200514 } else if (errno == ETIMEDOUT) {
515 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200516 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
517 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200518 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100519 conn->err_code = CO_ER_SOCK_ERR;
520 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200521 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200522 } else {
523 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
524 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200525 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
526 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200527 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100528 conn->err_code = CO_ER_SOCK_ERR;
529 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200530 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200531 }
532 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100533 else {
534 /* connect() == 0, this is great! */
535 conn->flags &= ~CO_FL_WAIT_L4_CONN;
536 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200537
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100538 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200539
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200540 /* Prepare to send a few handshakes related to the on-wire protocol. */
541 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200542 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200543
Willy Tarreauf79c8172013-10-21 16:30:56 +0200544 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100545 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200546
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200547 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200548 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100549 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200550 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200551 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200552
Olivier Houchardc2aae742017-09-22 18:26:28 +0200553 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN | CO_FL_EARLY_SSL_HS)) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100554 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
Olivier Houchardc2aae742017-09-22 18:26:28 +0200555 if (conn->flags & CO_FL_EARLY_SSL_HS)
556 conn_xprt_want_send(conn);
Willy Tarreau819efbf2017-01-25 14:12:22 +0100557 }
558 else {
559 /* If there's no more handshake, we need to notify the data
560 * layer when the connection is already OK otherwise we'll have
561 * no other opportunity to do it later (eg: health checks).
562 */
563 data = 1;
564 }
565
Willy Tarreau14f8e862012-08-30 22:23:13 +0200566 if (data)
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200567 conn_xprt_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200568
Willy Tarreaue7dff022015-04-03 01:14:29 +0200569 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200570}
571
572
Willy Tarreau59b94792012-05-11 16:16:40 +0200573/*
574 * Retrieves the source address for the socket <fd>, with <dir> indicating
575 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
576 * success, -1 in case of error. The socket's source address is stored in
577 * <sa> for <salen> bytes.
578 */
579int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
580{
581 if (dir)
582 return getsockname(fd, sa, &salen);
583 else
584 return getpeername(fd, sa, &salen);
585}
586
587
588/*
589 * Retrieves the original destination address for the socket <fd>, with <dir>
590 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
591 * listener, if the original destination address was translated, the original
592 * address is retrieved. It returns 0 in case of success, -1 in case of error.
593 * The socket's source address is stored in <sa> for <salen> bytes.
594 */
595int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
596{
597 if (dir)
598 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100599 else {
600 int ret = getsockname(fd, sa, &salen);
601
602 if (ret < 0)
603 return ret;
604
Willy Tarreau59b94792012-05-11 16:16:40 +0200605#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100606 /* For TPROXY and Netfilter's NAT, we can retrieve the original
607 * IPv4 address before DNAT/REDIRECT. We must not do that with
608 * other families because v6-mapped IPv4 addresses are still
609 * reported as v4.
610 */
611 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
612 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
613 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200614#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100615 return ret;
616 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200617}
618
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200619/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100620 * receive shutdown. Returns positive if the shutdown was found, negative
621 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
622 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200623 */
624int tcp_drain(int fd)
625{
626 int turns = 2;
627 int len;
628
629 while (turns) {
630#ifdef MSG_TRUNC_CLEARS_INPUT
631 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
632 if (len == -1 && errno == EFAULT)
633#endif
634 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
635
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100636 if (len == 0) {
637 /* cool, shutdown received */
638 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200639 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100640 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200641
642 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100643 if (errno == EAGAIN) {
644 /* connection not closed yet */
645 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100646 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100647 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200648 if (errno == EINTR) /* oops, try again */
649 continue;
650 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100651 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200652 return 1;
653 }
654 /* OK we read some data, let's try again once */
655 turns--;
656 }
657 /* some data are still present, give up */
658 return 0;
659}
660
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200661/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100662 * and we have nothing to send. It updates the FD polling status. It returns 0
663 * if it fails in a fatal way or needs to poll to go further, otherwise it
664 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
665 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
666 * errno. The error checking is done in two passes in order to limit the number
667 * of syscalls in the normal case :
668 * - if POLL_ERR was reported by the poller, we check for a pending error on
669 * the socket before proceeding. If found, it's assigned to errno so that
670 * upper layers can see it.
671 * - otherwise connect() is used to check the connection state again, since
672 * the getsockopt return cannot reliably be used to know if the connection
673 * is still pending or ready. This one may often return an error as well,
674 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200675 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200676int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200677{
Willy Tarreau585744b2017-08-24 14:31:19 +0200678 int fd = conn->handle.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100679 socklen_t lskerr;
680 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200681
Willy Tarreau80184712012-07-06 14:54:49 +0200682 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200683 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200684
Willy Tarreau3c728722014-01-23 13:50:42 +0100685 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200686 return 0;
687
Willy Tarreau80184712012-07-06 14:54:49 +0200688 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200689 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200690
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100691 if (!fd_send_ready(fd))
692 return 0;
693
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100694 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
695 * without FD_POLL_IN also indicates a hangup without input data meaning
696 * there was no connection.
697 */
698 if (fdtab[fd].ev & FD_POLL_ERR ||
699 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
700 skerr = 0;
701 lskerr = sizeof(skerr);
702 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
703 errno = skerr;
704 if (errno == EAGAIN)
705 errno = 0;
706 if (errno)
707 goto out_error;
708 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200709
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100710 /* Use connect() to check the state of the socket. This has the
711 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200712 * - error
713 * - connecting (EALREADY, EINPROGRESS)
714 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200715 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200716 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200717 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100718 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100719 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200720 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200721 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200722
Willy Tarreau2c6be842012-07-06 17:12:34 +0200723 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200724 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200725
Willy Tarreau2c6be842012-07-06 17:12:34 +0200726 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200727 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200728
Willy Tarreau076be252012-07-06 16:02:29 +0200729 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200730 * forward the event to the transport layer which will notify the
731 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200732 */
Willy Tarreau80184712012-07-06 14:54:49 +0200733 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200734 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200735
736 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200737 /* Write error on the file descriptor. Report it to the connection
738 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200739 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100740 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100741 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100742 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200743 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200744}
745
Olivier Houchard153659f2017-04-05 22:39:56 +0200746/* XXX: Should probably be elsewhere */
747static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
748{
749 if (a->ss_family != b->ss_family) {
750 return (-1);
751 }
752 switch (a->ss_family) {
753 case AF_INET:
754 {
755 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
756 if (a4->sin_port != b4->sin_port)
757 return (-1);
758 return (memcmp(&a4->sin_addr, &b4->sin_addr,
759 sizeof(a4->sin_addr)));
760 }
761 case AF_INET6:
762 {
763 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
764 if (a6->sin6_port != b6->sin6_port)
765 return (-1);
766 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
767 sizeof(a6->sin6_addr)));
768 }
769 default:
770 return (-1);
771 }
772
773}
774
775#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
776/* When binding the listeners, check if a socket has been sent to us by the
777 * previous process that we could reuse, instead of creating a new one.
778 */
779static int tcp_find_compatible_fd(struct listener *l)
780{
781 struct xfer_sock_list *xfer_sock = xfer_sock_list;
782 int ret = -1;
783
784 while (xfer_sock) {
785 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
786 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
787 (l->interface != NULL && xfer_sock->iface != NULL &&
788 !strcmp(l->interface, xfer_sock->iface))) {
789 if ((l->options & LI_MANDATORY_FLAGS) ==
790 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
791 if ((xfer_sock->namespace == NULL &&
792 l->netns == NULL)
793#ifdef CONFIG_HAP_NS
794 || (xfer_sock->namespace != NULL &&
795 l->netns != NULL &&
796 !strcmp(xfer_sock->namespace,
797 l->netns->node.key))
798#endif
799 ) {
800 break;
801 }
802
803 }
804 }
805 }
806 xfer_sock = xfer_sock->next;
807 }
808 if (xfer_sock != NULL) {
809 ret = xfer_sock->fd;
810 if (xfer_sock == xfer_sock_list)
811 xfer_sock_list = xfer_sock->next;
812 if (xfer_sock->prev)
813 xfer_sock->prev->next = xfer_sock->next;
814 if (xfer_sock->next)
815 xfer_sock->next->prev = xfer_sock->prev;
816 free(xfer_sock->iface);
817 free(xfer_sock->namespace);
818 free(xfer_sock);
819 }
820 return ret;
821}
822#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200823
Willy Tarreaue6b98942007-10-29 01:09:36 +0100824/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100825 * an error message in <errmsg> if the message is at most <errlen> bytes long
826 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
827 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100828 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
829 * was alright and that no message was returned. ERR_RETRYABLE means that an
830 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700831 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100832 * the meaning of the error, but just indicate that a message is present which
833 * should be displayed with the respective level. Last, ERR_ABORT indicates
834 * that it's pointless to try to start other listeners. No error message is
835 * returned if errlen is NULL.
836 */
837int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
838{
839 __label__ tcp_return, tcp_close_return;
840 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100841 int ext, ready;
842 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100843 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200844#ifdef TCP_MAXSEG
845
846 /* Create a temporary TCP socket to get default parameters we can't
847 * guess.
848 * */
849 ready_len = sizeof(default_tcp_maxseg);
850 if (default_tcp_maxseg == -1) {
851 default_tcp_maxseg = -2;
852 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
853 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100854 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200855 else {
856 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
857 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100858 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200859 }
860 close(fd);
861 }
862 if (default_tcp6_maxseg == -1) {
863 default_tcp6_maxseg = -2;
864 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
865 if (fd >= 0) {
866 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
867 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100868 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200869 close(fd);
870 }
871 }
872#endif
873
Willy Tarreaue6b98942007-10-29 01:09:36 +0100874
875 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100876 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100877 *errmsg = 0;
878
879 if (listener->state != LI_ASSIGNED)
880 return ERR_NONE; /* already bound */
881
882 err = ERR_NONE;
883
Olivier Houchard153659f2017-04-05 22:39:56 +0200884 if (listener->fd == -1)
885 listener->fd = tcp_find_compatible_fd(listener);
886
Willy Tarreau40aa0702013-03-10 23:51:38 +0100887 /* if the listener already has an fd assigned, then we were offered the
888 * fd by an external process (most likely the parent), and we don't want
889 * to create a new socket. However we still want to set a few flags on
890 * the socket.
891 */
892 fd = listener->fd;
893 ext = (fd >= 0);
894
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100895 if (!ext) {
896 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
897
898 if (fd == -1) {
899 err |= ERR_RETRYABLE | ERR_ALERT;
900 msg = "cannot create listening socket";
901 goto tcp_return;
902 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100903 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100904
Willy Tarreaue6b98942007-10-29 01:09:36 +0100905 if (fd >= global.maxsock) {
906 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
907 msg = "not enough free sockets (raise '-n' parameter)";
908 goto tcp_close_return;
909 }
910
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200911 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100912 err |= ERR_FATAL | ERR_ALERT;
913 msg = "cannot make socket non-blocking";
914 goto tcp_close_return;
915 }
916
Willy Tarreau40aa0702013-03-10 23:51:38 +0100917 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100918 /* not fatal but should be reported */
919 msg = "cannot do so_reuseaddr";
920 err |= ERR_ALERT;
921 }
922
923 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900924 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200925 else {
926 struct linger tmplinger;
927 socklen_t len = sizeof(tmplinger);
928 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
929 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
930 tmplinger.l_onoff = 0;
931 tmplinger.l_linger = 0;
932 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
933 sizeof(tmplinger));
934 }
935 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100936
Willy Tarreaue6b98942007-10-29 01:09:36 +0100937#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000938 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
939 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100940 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000941 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100942 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100943#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200944
Willy Tarreau40aa0702013-03-10 23:51:38 +0100945 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200946 switch (listener->addr.ss_family) {
947 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200948 if (1
949#if defined(IP_TRANSPARENT)
950 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
951#endif
952#if defined(IP_FREEBIND)
953 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
954#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200955#if defined(IP_BINDANY)
956 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
957#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200958#if defined(SO_BINDANY)
959 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
960#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200961 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200962 msg = "cannot make listening socket transparent";
963 err |= ERR_ALERT;
964 }
965 break;
966 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200967 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200968#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200969 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
970#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100971#if defined(IP_FREEBIND)
972 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
973#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200974#if defined(IPV6_BINDANY)
975 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
976#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200977#if defined(SO_BINDANY)
978 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
979#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200980 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200981 msg = "cannot make listening socket transparent";
982 err |= ERR_ALERT;
983 }
984 break;
985 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100986 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200987
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100988#ifdef SO_BINDTODEVICE
989 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100990 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100991 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100992 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100993 msg = "cannot bind listener to device";
994 err |= ERR_WARN;
995 }
996 }
997#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400998#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100999 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001000 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +02001001 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
1002 msg = "cannot set MSS";
1003 err |= ERR_WARN;
1004 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001005 } else if (ext) {
1006 int tmpmaxseg = -1;
1007 int defaultmss;
1008 socklen_t len = sizeof(tmpmaxseg);
1009
1010 if (listener->addr.ss_family == AF_INET)
1011 defaultmss = default_tcp_maxseg;
1012 else
1013 defaultmss = default_tcp6_maxseg;
1014
1015 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
1016 if (tmpmaxseg != defaultmss && setsockopt(fd, IPPROTO_TCP,
1017 TCP_MAXSEG, &defaultmss,
1018 sizeof(defaultmss)) == -1) {
1019 msg = "cannot set MSS";
1020 err |= ERR_WARN;
1021 }
Willy Tarreaube1b9182009-06-14 18:48:19 +02001022 }
1023#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001024#if defined(TCP_USER_TIMEOUT)
1025 if (listener->tcp_ut) {
1026 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
1027 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
1028 msg = "cannot set TCP User Timeout";
1029 err |= ERR_WARN;
1030 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001031 } else
1032 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
1033 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +01001034#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001035#if defined(TCP_DEFER_ACCEPT)
1036 if (listener->options & LI_O_DEF_ACCEPT) {
1037 /* defer accept by up to one second */
1038 int accept_delay = 1;
1039 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
1040 msg = "cannot enable DEFER_ACCEPT";
1041 err |= ERR_WARN;
1042 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001043 } else
1044 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
1045 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001046#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001047#if defined(TCP_FASTOPEN)
1048 if (listener->options & LI_O_TCP_FO) {
1049 /* TFO needs a queue length, let's use the configured backlog */
1050 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
1051 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
1052 msg = "cannot enable TCP_FASTOPEN";
1053 err |= ERR_WARN;
1054 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001055 } else {
1056 socklen_t len;
1057 int qlen;
1058 len = sizeof(qlen);
1059 /* Only disable fast open if it was enabled, we don't want
1060 * the kernel to create a fast open queue if there's none.
1061 */
1062 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
1063 qlen != 0) {
1064 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
1065 sizeof(zero)) == -1) {
1066 msg = "cannot disable TCP_FASTOPEN";
1067 err |= ERR_WARN;
1068 }
1069 }
Willy Tarreau1c862c52012-10-05 16:21:00 +02001070 }
1071#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001072#if defined(IPV6_V6ONLY)
1073 if (listener->options & LI_O_V6ONLY)
1074 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +01001075 else if (listener->options & LI_O_V4V6)
1076 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001077#endif
1078
Willy Tarreau40aa0702013-03-10 23:51:38 +01001079 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001080 err |= ERR_RETRYABLE | ERR_ALERT;
1081 msg = "cannot bind socket";
1082 goto tcp_close_return;
1083 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001084
Willy Tarreau40aa0702013-03-10 23:51:38 +01001085 ready = 0;
1086 ready_len = sizeof(ready);
1087 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
1088 ready = 0;
1089
1090 if (!(ext && ready) && /* only listen if not already done by external process */
1091 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001092 err |= ERR_RETRYABLE | ERR_ALERT;
1093 msg = "cannot listen to socket";
1094 goto tcp_close_return;
1095 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001096
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001097#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001098 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +09001099 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +02001100 else
1101 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001102#endif
1103
Willy Tarreaue6b98942007-10-29 01:09:36 +01001104 /* the socket is ready */
1105 listener->fd = fd;
1106 listener->state = LI_LISTEN;
1107
Willy Tarreaueabf3132008-08-29 23:36:51 +02001108 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +02001109 fdtab[fd].iocb = listener->proto->accept;
Christopher Faulet165f07e2017-11-24 10:08:09 +01001110 if (listener->bind_conf->bind_thread[relative_pid-1])
1111 fd_insert(fd, listener->bind_conf->bind_thread[relative_pid-1]);
1112 else
1113 fd_insert(fd, MAX_THREADS_MASK);
Willy Tarreaueb472682010-05-28 18:46:57 +02001114
Willy Tarreaue6b98942007-10-29 01:09:36 +01001115 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001116 if (msg && errlen) {
1117 char pn[INET6_ADDRSTRLEN];
1118
Willy Tarreau631f01c2011-09-05 00:36:48 +02001119 addr_to_str(&listener->addr, pn, sizeof(pn));
1120 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001121 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001122 return err;
1123
1124 tcp_close_return:
1125 close(fd);
1126 goto tcp_return;
1127}
1128
1129/* This function creates all TCP sockets bound to the protocol entry <proto>.
1130 * It is intended to be used as the protocol's bind_all() function.
1131 * The sockets will be registered but not added to any fd_set, in order not to
1132 * loose them across the fork(). A call to enable_all_listeners() is needed
1133 * to complete initialization. The return value is composed from ERR_*.
1134 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001135static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001136{
1137 struct listener *listener;
1138 int err = ERR_NONE;
1139
1140 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001141 err |= tcp_bind_listener(listener, errmsg, errlen);
1142 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001143 break;
1144 }
1145
1146 return err;
1147}
1148
Willy Tarreau32282382017-09-15 07:44:44 +02001149/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1150 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1151 * The number of listeners for the protocol is updated.
Willy Tarreaue6b98942007-10-29 01:09:36 +01001152 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001153static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001154{
1155 if (listener->state != LI_INIT)
1156 return;
1157 listener->state = LI_ASSIGNED;
1158 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001159 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001160 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1161 proto_tcpv4.nb_listeners++;
1162}
1163
Willy Tarreau32282382017-09-15 07:44:44 +02001164/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1165 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1166 * The number of listeners for the protocol is updated.
Willy Tarreaue6b98942007-10-29 01:09:36 +01001167 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001168static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001169{
1170 if (listener->state != LI_INIT)
1171 return;
1172 listener->state = LI_ASSIGNED;
1173 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001174 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001175 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1176 proto_tcpv6.nb_listeners++;
1177}
1178
Willy Tarreau092d8652014-07-07 20:22:12 +02001179/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1180 * was totally stopped, or > 0 if correctly paused.
1181 */
1182int tcp_pause_listener(struct listener *l)
1183{
1184 if (shutdown(l->fd, SHUT_WR) != 0)
1185 return -1; /* Solaris dies here */
1186
1187 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1188 return -1; /* OpenBSD dies here */
1189
1190 if (shutdown(l->fd, SHUT_RD) != 0)
1191 return -1; /* should always be OK */
1192 return 1;
1193}
1194
William Lallemand2e785f22016-05-25 01:48:42 +02001195/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001196 * Execute the "set-src" action. May be called from {tcp,http}request.
1197 * It only changes the address and tries to preserve the original port. If the
1198 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001199 */
1200enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1201 struct session *sess, struct stream *s, int flags)
1202{
1203 struct connection *cli_conn;
1204
1205 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1206 struct sample *smp;
1207
1208 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1209 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001210 int port = get_net_port(&cli_conn->addr.from);
1211
William Lallemand2e785f22016-05-25 01:48:42 +02001212 if (smp->data.type == SMP_T_IPV4) {
1213 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
1214 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001215 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001216 } else if (smp->data.type == SMP_T_IPV6) {
1217 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
1218 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 +02001219 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001220 }
1221 }
William Lallemand01252ed2016-05-25 02:33:16 +02001222 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001223 }
1224 return ACT_RET_CONT;
1225}
1226
William Lallemand44be6402016-05-25 01:51:35 +02001227/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001228 * Execute the "set-dst" action. May be called from {tcp,http}request.
1229 * It only changes the address and tries to preserve the original port. If the
1230 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001231 */
1232enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1233 struct session *sess, struct stream *s, int flags)
1234{
1235 struct connection *cli_conn;
1236
1237 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1238 struct sample *smp;
1239
1240 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1241 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001242 int port = get_net_port(&cli_conn->addr.to);
1243
William Lallemand13e9b0c2016-05-25 02:34:07 +02001244 if (smp->data.type == SMP_T_IPV4) {
1245 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_family = AF_INET;
1246 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1247 } else if (smp->data.type == SMP_T_IPV6) {
1248 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_family = AF_INET6;
1249 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 +02001250 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001251 }
1252 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1253 }
1254 }
1255 return ACT_RET_CONT;
1256}
1257
1258/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001259 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1260 * We must test the sin_family before setting the port. If the address family
1261 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1262 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001263 */
1264enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1265 struct session *sess, struct stream *s, int flags)
1266{
1267 struct connection *cli_conn;
1268
1269 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1270 struct sample *smp;
1271
1272 conn_get_from_addr(cli_conn);
1273
1274 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1275 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001276 if (cli_conn->addr.from.ss_family == AF_INET6) {
William Lallemand44be6402016-05-25 01:51:35 +02001277 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001278 } else {
1279 if (cli_conn->addr.from.ss_family != AF_INET) {
1280 cli_conn->addr.from.ss_family = AF_INET;
1281 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1282 }
1283 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001284 }
1285 }
1286 }
1287 return ACT_RET_CONT;
1288}
1289
William Lallemand13e9b0c2016-05-25 02:34:07 +02001290/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001291 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1292 * We must test the sin_family before setting the port. If the address family
1293 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1294 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001295 */
1296enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1297 struct session *sess, struct stream *s, int flags)
1298{
1299 struct connection *cli_conn;
1300
1301 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1302 struct sample *smp;
1303
1304 conn_get_to_addr(cli_conn);
1305
1306 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1307 if (smp) {
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001308 if (cli_conn->addr.to.ss_family == AF_INET6) {
1309 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001310 } else {
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001311 if (cli_conn->addr.to.ss_family != AF_INET) {
1312 cli_conn->addr.to.ss_family = AF_INET;
1313 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001314 }
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001315 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001316 }
1317 }
1318 }
1319 return ACT_RET_CONT;
1320}
1321
Willy Tarreau2d392c22015-08-24 01:43:45 +02001322/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1323static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1324{
1325 struct connection *conn = objt_conn(sess->origin);
1326
1327 if (!conn)
1328 goto out;
1329
1330 if (!conn_ctrl_ready(conn))
1331 goto out;
1332
Willy Tarreau2d392c22015-08-24 01:43:45 +02001333#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001334 /* drain is needed only to send the quick ACK */
1335 conn_sock_drain(conn);
1336
Willy Tarreau2d392c22015-08-24 01:43:45 +02001337 /* re-enable quickack if it was disabled to ack all data and avoid
1338 * retransmits from the client that might trigger a real reset.
1339 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001340 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001341#endif
1342 /* lingering must absolutely be disabled so that we don't send a
1343 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1344 * is present, returning with ERR will cause lingering to be disabled.
1345 */
1346 if (strm)
1347 strm->si[0].flags |= SI_FL_NOLINGER;
1348
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001349 /* We're on the client-facing side, we must force to disable lingering to
1350 * ensure we will use an RST exclusively and kill any pending data.
1351 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001352 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001353
Willy Tarreau2d392c22015-08-24 01:43:45 +02001354#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001355 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001356 /* socket will be quiet now */
1357 goto out;
1358 }
1359#endif
1360 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1361 * Let's fall back on the TTL trick, though it only works for routed
1362 * network and has no effect on local net.
1363 */
1364#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001365 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001366#endif
1367 out:
1368 /* kill the stream if any */
1369 if (strm) {
1370 channel_abort(&strm->req);
1371 channel_abort(&strm->res);
1372 strm->req.analysers = 0;
1373 strm->res.analysers = 0;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001374 HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001375 if (!(strm->flags & SF_ERR_MASK))
1376 strm->flags |= SF_ERR_PRXCOND;
1377 if (!(strm->flags & SF_FINST_MASK))
1378 strm->flags |= SF_FINST_R;
1379 }
1380
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001381 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001382 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001383 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001384
1385 return ACT_RET_STOP;
1386}
1387
William Lallemand13e9b0c2016-05-25 02:34:07 +02001388/* parse "set-{src,dst}[-port]" action */
1389enum 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 +02001390{
1391 int cur_arg;
1392 struct sample_expr *expr;
1393 unsigned int where;
1394
1395 cur_arg = *orig_arg;
1396 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
1397 if (!expr)
1398 return ACT_RET_PRS_ERR;
1399
1400 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001401 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001402 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001403 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001404 where |= SMP_VAL_BE_HRQ_HDR;
1405
1406 if (!(expr->fetch->val & where)) {
1407 memprintf(err,
1408 "fetch method '%s' extracts information from '%s', none of which is available here",
1409 args[cur_arg-1], sample_src_names(expr->fetch->use));
1410 free(expr);
1411 return ACT_RET_PRS_ERR;
1412 }
1413 rule->arg.expr = expr;
1414 rule->action = ACT_CUSTOM;
1415
1416 if (!strcmp(args[*orig_arg-1], "set-src")) {
1417 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001418 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1419 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001420 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1421 rule->action_ptr = tcp_action_req_set_dst;
1422 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1423 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001424 } else {
1425 return ACT_RET_PRS_ERR;
1426 }
1427
1428 (*orig_arg)++;
1429
1430 return ACT_RET_PRS_OK;
1431}
1432
1433
Willy Tarreau2d392c22015-08-24 01:43:45 +02001434/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1435 * success, ACT_RET_PRS_ERR on error.
1436 */
1437static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1438 struct act_rule *rule, char **err)
1439{
1440 rule->action = ACT_CUSTOM;
1441 rule->action_ptr = tcp_exec_action_silent_drop;
1442 return ACT_RET_PRS_OK;
1443}
1444
Willy Tarreau645513a2010-05-24 20:55:15 +02001445
1446/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001447/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001448/************************************************************************/
1449
Willy Tarreau4a129812012-04-25 17:31:42 +02001450/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001451int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001452{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001453 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001454
1455 if (!cli_conn)
1456 return 0;
1457
1458 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001459 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001460 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001461 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001462 break;
1463 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001464 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001465 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001466 break;
1467 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001468 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001469 }
Emeric Brunf769f512010-10-22 17:14:01 +02001470
Willy Tarreau37406352012-04-23 16:16:37 +02001471 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001472 return 1;
1473}
1474
Willy Tarreaua5e37562011-12-16 17:06:15 +01001475/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001476static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001477smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001478{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001479 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001480
1481 if (!cli_conn)
1482 return 0;
1483
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001484 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001485 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001486 return 0;
1487
Willy Tarreau37406352012-04-23 16:16:37 +02001488 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001489 return 1;
1490}
1491
Willy Tarreau4a129812012-04-25 17:31:42 +02001492/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001493static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001494smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001495{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001496 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001497
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001498 if (!cli_conn)
1499 return 0;
1500
1501 conn_get_to_addr(cli_conn);
1502
1503 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001504 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001505 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001506 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001507 break;
1508 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001509 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001510 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001511 break;
1512 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001513 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001514 }
Emeric Brunf769f512010-10-22 17:14:01 +02001515
Willy Tarreau37406352012-04-23 16:16:37 +02001516 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001517 return 1;
1518}
1519
Willy Tarreau16e01562016-08-09 16:46:18 +02001520/* check if the destination address of the front connection is local to the
1521 * system or if it was intercepted.
1522 */
1523int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1524{
1525 struct connection *conn = objt_conn(smp->sess->origin);
1526 struct listener *li = smp->sess->listener;
1527
1528 if (!conn)
1529 return 0;
1530
1531 conn_get_to_addr(conn);
1532 if (!(conn->flags & CO_FL_ADDR_TO_SET))
1533 return 0;
1534
1535 smp->data.type = SMP_T_BOOL;
1536 smp->flags = 0;
1537 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.to);
1538 return smp->data.u.sint >= 0;
1539}
1540
1541/* check if the source address of the front connection is local to the system
1542 * or not.
1543 */
1544int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1545{
1546 struct connection *conn = objt_conn(smp->sess->origin);
1547 struct listener *li = smp->sess->listener;
1548
1549 if (!conn)
1550 return 0;
1551
1552 conn_get_from_addr(conn);
1553 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
1554 return 0;
1555
1556 smp->data.type = SMP_T_BOOL;
1557 smp->flags = 0;
1558 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.from);
1559 return smp->data.u.sint >= 0;
1560}
1561
Willy Tarreaua5e37562011-12-16 17:06:15 +01001562/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001563static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001564smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001565{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001566 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001567
1568 if (!cli_conn)
1569 return 0;
1570
1571 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001572
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001573 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001574 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001575 return 0;
1576
Willy Tarreau37406352012-04-23 16:16:37 +02001577 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001578 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001579}
1580
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001581#ifdef TCP_INFO
1582
1583/* Returns some tcp_info data is its avalaible. "dir" must be set to 0 if
1584 * the client connection is require, otherwise it is set to 1. "val" represents
1585 * the required value. Use 0 for rtt and 1 for rttavg. "unit" is the expected unit
1586 * by default, the rtt is in us. Id "unit" is set to 0, the unit is us, if it is
1587 * set to 1, the untis are milliseconds.
1588 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1589 */
1590static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1591 int dir, int val)
1592{
1593 struct connection *conn;
1594 struct tcp_info info;
1595 socklen_t optlen;
1596
1597 /* strm can be null. */
1598 if (!smp->strm)
1599 return 0;
1600
1601 /* get the object associated with the stream interface.The
1602 * object can be other thing than a connection. For example,
1603 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001604 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001605 if (!conn)
1606 return 0;
1607
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001608 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001609 syscal can fail. */
1610 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001611 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001612 return 0;
1613
1614 /* extract the value. */
1615 smp->data.type = SMP_T_SINT;
1616 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001617 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1618 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1619#if defined(__linux__)
1620 /* these ones are common to all Linux versions */
1621 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1622 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1623 case 4: smp->data.u.sint = info.tcpi_lost; break;
1624 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1625 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1626 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1627#elif defined(__FreeBSD__) || defined(__NetBSD__)
1628 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1629 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1630 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1631 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1632 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1633 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1634 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1635#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001636 default: return 0;
1637 }
1638
1639 /* Convert the value as expected. */
1640 if (args) {
1641 if (args[0].type == ARGT_STR) {
1642 if (strcmp(args[0].data.str.str, "us") == 0) {
1643 /* Do nothing. */
1644 } else if (strcmp(args[0].data.str.str, "ms") == 0) {
1645 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1646 } else
1647 return 0;
1648 } else if (args[0].type == ARGT_STOP) {
1649 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1650 } else
1651 return 0;
1652 }
1653
1654 return 1;
1655}
1656
1657/* get the mean rtt of a client connexion */
1658static int
1659smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1660{
1661 if (!get_tcp_info(args, smp, 0, 0))
1662 return 0;
1663 return 1;
1664}
1665
1666/* get the variance of the mean rtt of a client connexion */
1667static int
1668smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1669{
1670 if (!get_tcp_info(args, smp, 0, 1))
1671 return 0;
1672 return 1;
1673}
Joe Williams30fcd392016-08-10 07:06:44 -07001674
1675#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1676
1677/* get the unacked counter on a client connexion */
1678static int
1679smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1680{
1681 if (!get_tcp_info(args, smp, 0, 2))
1682 return 0;
1683 return 1;
1684}
1685
1686/* get the sacked counter on a client connexion */
1687static int
1688smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1689{
1690 if (!get_tcp_info(args, smp, 0, 3))
1691 return 0;
1692 return 1;
1693}
1694
1695/* get the lost counter on a client connexion */
1696static int
1697smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1698{
1699 if (!get_tcp_info(args, smp, 0, 4))
1700 return 0;
1701 return 1;
1702}
1703
1704/* get the retrans counter on a client connexion */
1705static int
1706smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1707{
1708 if (!get_tcp_info(args, smp, 0, 5))
1709 return 0;
1710 return 1;
1711}
1712
1713/* get the fackets counter on a client connexion */
1714static int
1715smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1716{
1717 if (!get_tcp_info(args, smp, 0, 6))
1718 return 0;
1719 return 1;
1720}
1721
1722/* get the reordering counter on a client connexion */
1723static int
1724smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1725{
1726 if (!get_tcp_info(args, smp, 0, 7))
1727 return 0;
1728 return 1;
1729}
1730#endif // linux || freebsd || netbsd
1731#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001732
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001733#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001734/* parse the "v4v6" bind keyword */
1735static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1736{
1737 struct listener *l;
1738
1739 list_for_each_entry(l, &conf->listeners, by_bind) {
1740 if (l->addr.ss_family == AF_INET6)
1741 l->options |= LI_O_V4V6;
1742 }
1743
1744 return 0;
1745}
1746
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001747/* parse the "v6only" bind keyword */
1748static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1749{
1750 struct listener *l;
1751
1752 list_for_each_entry(l, &conf->listeners, by_bind) {
1753 if (l->addr.ss_family == AF_INET6)
1754 l->options |= LI_O_V6ONLY;
1755 }
1756
1757 return 0;
1758}
1759#endif
1760
Pieter Baauwd551fb52013-05-08 22:49:23 +02001761#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001762/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001763static 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 +02001764{
1765 struct listener *l;
1766
Willy Tarreau4348fad2012-09-20 16:48:07 +02001767 list_for_each_entry(l, &conf->listeners, by_bind) {
1768 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1769 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001770 }
1771
Willy Tarreau44791242012-09-12 23:27:21 +02001772 return 0;
1773}
1774#endif
1775
1776#ifdef TCP_DEFER_ACCEPT
1777/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001778static 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 +02001779{
1780 struct listener *l;
1781
Willy Tarreau4348fad2012-09-20 16:48:07 +02001782 list_for_each_entry(l, &conf->listeners, by_bind) {
1783 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1784 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001785 }
1786
Willy Tarreau44791242012-09-12 23:27:21 +02001787 return 0;
1788}
1789#endif
1790
Willy Tarreau1c862c52012-10-05 16:21:00 +02001791#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001792/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001793static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1794{
1795 struct listener *l;
1796
1797 list_for_each_entry(l, &conf->listeners, by_bind) {
1798 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1799 l->options |= LI_O_TCP_FO;
1800 }
1801
1802 return 0;
1803}
1804#endif
1805
Willy Tarreau44791242012-09-12 23:27:21 +02001806#ifdef TCP_MAXSEG
1807/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001808static 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 +02001809{
1810 struct listener *l;
1811 int mss;
1812
Willy Tarreau44791242012-09-12 23:27:21 +02001813 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001814 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001815 return ERR_ALERT | ERR_FATAL;
1816 }
1817
1818 mss = atoi(args[cur_arg + 1]);
1819 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001820 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001821 return ERR_ALERT | ERR_FATAL;
1822 }
1823
Willy Tarreau4348fad2012-09-20 16:48:07 +02001824 list_for_each_entry(l, &conf->listeners, by_bind) {
1825 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1826 l->maxseg = mss;
1827 }
Willy Tarreau44791242012-09-12 23:27:21 +02001828
1829 return 0;
1830}
1831#endif
1832
Willy Tarreau2af207a2015-02-04 00:45:58 +01001833#ifdef TCP_USER_TIMEOUT
1834/* parse the "tcp-ut" bind keyword */
1835static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1836{
1837 const char *ptr = NULL;
1838 struct listener *l;
1839 unsigned int timeout;
1840
1841 if (!*args[cur_arg + 1]) {
1842 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1843 return ERR_ALERT | ERR_FATAL;
1844 }
1845
1846 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
1847 if (ptr) {
1848 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1849 return ERR_ALERT | ERR_FATAL;
1850 }
1851
1852 list_for_each_entry(l, &conf->listeners, by_bind) {
1853 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1854 l->tcp_ut = timeout;
1855 }
1856
1857 return 0;
1858}
1859#endif
1860
Willy Tarreau44791242012-09-12 23:27:21 +02001861#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001862/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001863static 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 +02001864{
1865 struct listener *l;
1866
Willy Tarreau44791242012-09-12 23:27:21 +02001867 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001868 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001869 return ERR_ALERT | ERR_FATAL;
1870 }
1871
Willy Tarreau4348fad2012-09-20 16:48:07 +02001872 list_for_each_entry(l, &conf->listeners, by_bind) {
1873 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1874 l->interface = strdup(args[cur_arg + 1]);
1875 }
Willy Tarreau44791242012-09-12 23:27:21 +02001876
Willy Tarreau44791242012-09-12 23:27:21 +02001877 return 0;
1878}
1879#endif
1880
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001881#ifdef CONFIG_HAP_NS
1882/* parse the "namespace" bind keyword */
1883static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1884{
1885 struct listener *l;
1886 char *namespace = NULL;
1887
1888 if (!*args[cur_arg + 1]) {
1889 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1890 return ERR_ALERT | ERR_FATAL;
1891 }
1892 namespace = args[cur_arg + 1];
1893
1894 list_for_each_entry(l, &conf->listeners, by_bind) {
1895 l->netns = netns_store_lookup(namespace, strlen(namespace));
1896
1897 if (l->netns == NULL)
1898 l->netns = netns_store_insert(namespace);
1899
1900 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001901 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001902 return ERR_ALERT | ERR_FATAL;
1903 }
1904 }
1905 return 0;
1906}
1907#endif
1908
Willy Tarreau163d4622015-10-13 16:16:41 +02001909#ifdef TCP_USER_TIMEOUT
1910/* parse the "tcp-ut" server keyword */
1911static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1912{
1913 const char *ptr = NULL;
1914 unsigned int timeout;
1915
1916 if (!*args[*cur_arg + 1]) {
1917 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1918 return ERR_ALERT | ERR_FATAL;
1919 }
1920
1921 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
1922 if (ptr) {
1923 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1924 return ERR_ALERT | ERR_FATAL;
1925 }
1926
1927 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1928 newsrv->tcp_ut = timeout;
1929
1930 return 0;
1931}
1932#endif
1933
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001934
Willy Tarreau4a129812012-04-25 17:31:42 +02001935/* Note: must not be declared <const> as its list will be overwritten.
1936 * Note: fetches that may return multiple types must be declared as the lowest
1937 * common denominator, the type that can be casted into all other ones. For
1938 * instance v4/v6 must be declared v4.
1939 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001940static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001941 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001942 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001943 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001944 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001945 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001946 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001947#ifdef TCP_INFO
Joe Williams30fcd392016-08-10 07:06:44 -07001948 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1949 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1950#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1951 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1952 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1953 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1954 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1955 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1956 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1957#endif // linux || freebsd || netbsd
1958#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001959 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001960}};
1961
Willy Tarreau44791242012-09-12 23:27:21 +02001962/************************************************************************/
1963/* All supported bind keywords must be declared here. */
1964/************************************************************************/
1965
1966/* Note: must not be declared <const> as its list will be overwritten.
1967 * Please take care of keeping this list alphabetically sorted, doing so helps
1968 * all code contributors.
1969 * Optional keywords are also declared with a NULL ->parse() function so that
1970 * the config parser can report an appropriate error when a known keyword was
1971 * not enabled.
1972 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001973static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001974#ifdef TCP_DEFER_ACCEPT
1975 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1976#endif
1977#ifdef SO_BINDTODEVICE
1978 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1979#endif
1980#ifdef TCP_MAXSEG
1981 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1982#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001983#ifdef TCP_USER_TIMEOUT
1984 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1985#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001986#ifdef TCP_FASTOPEN
1987 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1988#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001989#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001990 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1991#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001992#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001993 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001994 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1995#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001996#ifdef CONFIG_HAP_NS
1997 { "namespace", bind_parse_namespace, 1 },
1998#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001999 /* the versions with the NULL parse function*/
2000 { "defer-accept", NULL, 0 },
2001 { "interface", NULL, 1 },
2002 { "mss", NULL, 1 },
2003 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002004 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002005 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002006 { NULL, NULL, 0 },
2007}};
2008
Willy Tarreau163d4622015-10-13 16:16:41 +02002009static struct srv_kw_list srv_kws = { "TCP", { }, {
2010#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002011 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02002012#endif
2013 { NULL, NULL, 0 },
2014}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02002015
2016static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002017 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002018 { "set-src", tcp_parse_set_src_dst },
2019 { "set-src-port", tcp_parse_set_src_dst },
2020 { "set-dst" , tcp_parse_set_src_dst },
2021 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002022 { /* END */ }
2023}};
2024
Willy Tarreau620408f2016-10-21 16:37:51 +02002025static struct action_kw_list tcp_req_sess_actions = {ILH, {
2026 { "silent-drop", tcp_parse_silent_drop },
2027 { "set-src", tcp_parse_set_src_dst },
2028 { "set-src-port", tcp_parse_set_src_dst },
2029 { "set-dst" , tcp_parse_set_src_dst },
2030 { "set-dst-port", tcp_parse_set_src_dst },
2031 { /* END */ }
2032}};
2033
Willy Tarreau2d392c22015-08-24 01:43:45 +02002034static struct action_kw_list tcp_req_cont_actions = {ILH, {
2035 { "silent-drop", tcp_parse_silent_drop },
2036 { /* END */ }
2037}};
2038
2039static struct action_kw_list tcp_res_cont_actions = {ILH, {
2040 { "silent-drop", tcp_parse_silent_drop },
2041 { /* END */ }
2042}};
2043
2044static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002045 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002046 { "set-src", tcp_parse_set_src_dst },
2047 { "set-src-port", tcp_parse_set_src_dst },
2048 { "set-dst", tcp_parse_set_src_dst },
2049 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002050 { /* END */ }
2051}};
2052
2053static struct action_kw_list http_res_actions = {ILH, {
2054 { "silent-drop", tcp_parse_silent_drop },
2055 { /* END */ }
2056}};
2057
2058
Willy Tarreaue6b98942007-10-29 01:09:36 +01002059__attribute__((constructor))
2060static void __tcp_protocol_init(void)
2061{
2062 protocol_register(&proto_tcpv4);
2063 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002064 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau44791242012-09-12 23:27:21 +02002065 bind_register_keywords(&bind_kws);
Willy Tarreau163d4622015-10-13 16:16:41 +02002066 srv_register_keywords(&srv_kws);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002067 tcp_req_conn_keywords_register(&tcp_req_conn_actions);
Willy Tarreau620408f2016-10-21 16:37:51 +02002068 tcp_req_sess_keywords_register(&tcp_req_sess_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002069 tcp_req_cont_keywords_register(&tcp_req_cont_actions);
2070 tcp_res_cont_keywords_register(&tcp_res_cont_actions);
2071 http_req_keywords_register(&http_req_actions);
2072 http_res_keywords_register(&http_res_actions);
Willy Tarreauba962912016-12-21 18:55:02 +01002073
2074
2075 hap_register_build_opts("Built with transparent proxy support using:"
2076#if defined(IP_TRANSPARENT)
2077 " IP_TRANSPARENT"
2078#endif
2079#if defined(IPV6_TRANSPARENT)
2080 " IPV6_TRANSPARENT"
2081#endif
2082#if defined(IP_FREEBIND)
2083 " IP_FREEBIND"
2084#endif
2085#if defined(IP_BINDANY)
2086 " IP_BINDANY"
2087#endif
2088#if defined(IPV6_BINDANY)
2089 " IPV6_BINDANY"
2090#endif
2091#if defined(SO_BINDANY)
2092 " SO_BINDANY"
2093#endif
2094 "", 0);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002095}
2096
2097
2098/*
2099 * Local variables:
2100 * c-indent-level: 8
2101 * c-basic-offset: 8
2102 * End:
2103 */