blob: 57d6fc1fa54595137203a30ebce29940c651d8b3 [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 Tarreaue6b98942007-10-29 01:09:36 +010066
67/* Note: must not be declared <const> as its list will be overwritten */
68static struct protocol proto_tcpv4 = {
69 .name = "tcpv4",
70 .sock_domain = AF_INET,
71 .sock_type = SOCK_STREAM,
72 .sock_prot = IPPROTO_TCP,
73 .sock_family = AF_INET,
74 .sock_addrlen = sizeof(struct sockaddr_in),
75 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020076 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020077 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020078 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010079 .bind_all = tcp_bind_listeners,
80 .unbind_all = unbind_all_listeners,
81 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020082 .get_src = tcp_get_src,
83 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020084 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +020085 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010086 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
87 .nb_listeners = 0,
88};
89
90/* Note: must not be declared <const> as its list will be overwritten */
91static struct protocol proto_tcpv6 = {
92 .name = "tcpv6",
93 .sock_domain = AF_INET6,
94 .sock_type = SOCK_STREAM,
95 .sock_prot = IPPROTO_TCP,
96 .sock_family = AF_INET6,
97 .sock_addrlen = sizeof(struct sockaddr_in6),
98 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020099 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200100 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200101 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100102 .bind_all = tcp_bind_listeners,
103 .unbind_all = unbind_all_listeners,
104 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200105 .get_src = tcp_get_src,
106 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200107 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +0200108 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100109 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
110 .nb_listeners = 0,
111};
112
Olivier Houchard153659f2017-04-05 22:39:56 +0200113/* Default TCP parameters, got by opening a temporary TCP socket. */
114#ifdef TCP_MAXSEG
115static int default_tcp_maxseg = -1;
116static int default_tcp6_maxseg = -1;
117#endif
118
David du Colombier6f5ccb12011-03-10 22:26:24 +0100119/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100120 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
121 * - 0 : ignore remote address (may even be a NULL pointer)
122 * - 1 : use provided address
123 * - 2 : use provided port
124 * - 3 : use both
125 *
126 * The function supports multiple foreign binding methods :
127 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100128 * The second one can be used as a fallback for the first one.
129 * This function returns 0 when everything's OK, 1 if it could not bind, to the
130 * local address, 2 if it could not bind to the foreign address.
131 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100132int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100133{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100134 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100135 int foreign_ok = 0;
136 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100137 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200138 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200139
David du Colombier65c17962012-07-13 14:34:59 +0200140 switch (local->ss_family) {
141 case AF_INET:
142 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200143 /* This deserves some explanation. Some platforms will support
144 * multiple combinations of certain methods, so we try the
145 * supported ones until one succeeds.
146 */
147 if (0
148#if defined(IP_TRANSPARENT)
149 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
150#endif
151#if defined(IP_FREEBIND)
152 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
153#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200154#if defined(IP_BINDANY)
155 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
156#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200157#if defined(SO_BINDANY)
158 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
159#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200160 )
David du Colombier65c17962012-07-13 14:34:59 +0200161 foreign_ok = 1;
162 else
163 ip_transp_working = 0;
164 }
165 break;
166 case AF_INET6:
167 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200168 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200169#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200170 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
171#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100172#if defined(IP_FREEBIND)
173 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
174#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200175#if defined(IPV6_BINDANY)
176 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
177#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200178#if defined(SO_BINDANY)
179 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
180#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200181 )
David du Colombier65c17962012-07-13 14:34:59 +0200182 foreign_ok = 1;
183 else
184 ip6_transp_working = 0;
185 }
186 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100187 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200188
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100189 if (flags) {
190 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200191 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100192 switch (remote->ss_family) {
193 case AF_INET:
194 if (flags & 1)
195 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
196 if (flags & 2)
197 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
198 break;
199 case AF_INET6:
200 if (flags & 1)
201 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
202 if (flags & 2)
203 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
204 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100205 default:
206 /* we don't want to try to bind to an unknown address family */
207 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100208 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100209 }
210
Simon Hormande072bd2011-06-24 15:11:37 +0900211 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100212 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200213 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200214 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
215 if (ret < 0)
216 return 2;
217 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100218 }
219 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200220 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200221 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
222 if (ret < 0)
223 return 1;
224 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100225 }
226
227 if (!flags)
228 return 0;
229
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100230 if (!foreign_ok)
231 /* we could not bind to a foreign address */
232 return 2;
233
234 return 0;
235}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100236
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100237static int create_server_socket(struct connection *conn)
238{
Willy Tarreau529c1392014-12-24 13:47:55 +0100239 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100240
Willy Tarreau529c1392014-12-24 13:47:55 +0100241#ifdef CONFIG_HAP_NS
242 if (objt_server(conn->target)) {
243 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
244 ns = conn->proxy_netns;
245 else
246 ns = __objt_server(conn->target)->netns;
247 }
248#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100249 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
250}
Willy Tarreau9650f372009-08-16 14:02:45 +0200251
252/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200253 * This function initiates a TCP connection establishment to the target assigned
254 * to connection <conn> using (si->{target,addr.to}). A source address may be
255 * pointed to by conn->addr.from in case of transparent proxying. Normal source
256 * bind addresses are still determined locally (due to the possible need of a
257 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100258 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100259 * supported. The <data> parameter is a boolean indicating whether there are data
260 * waiting for being sent or not, in order to adjust data write polling and on
261 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
262 * allows the caller to force using a delayed ACK when establishing the connection :
263 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
264 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
265 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200266 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200267 * Note that a pending send_proxy message accounts for data.
268 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200269 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200270 * - SF_ERR_NONE if everything's OK
271 * - SF_ERR_SRVTO if there are no more servers
272 * - SF_ERR_SRVCL if the connection was refused by the server
273 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
274 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
275 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100276 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100277 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200278 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100279 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200280 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100281
Willy Tarreauf0837b22012-11-24 10:24:27 +0100282int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200283{
284 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100285 struct server *srv;
286 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100287 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100288
Willy Tarreau9ce70132014-01-24 16:08:19 +0100289 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
290
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100291 switch (obj_type(conn->target)) {
292 case OBJ_TYPE_PROXY:
293 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100294 srv = NULL;
295 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100296 case OBJ_TYPE_SERVER:
297 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100298 be = srv->proxy;
299 break;
300 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100301 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200302 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100303 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200304
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100305 fd = conn->t.sock.fd = create_server_socket(conn);
306
307 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200308 qfprintf(stderr, "Cannot get a server socket.\n");
309
Willy Tarreau9ce70132014-01-24 16:08:19 +0100310 if (errno == ENFILE) {
311 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200312 send_log(be, LOG_EMERG,
313 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
314 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100315 }
316 else if (errno == EMFILE) {
317 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200318 send_log(be, LOG_EMERG,
319 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
320 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100321 }
322 else if (errno == ENOBUFS || errno == ENOMEM) {
323 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200324 send_log(be, LOG_EMERG,
325 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
326 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100327 }
328 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
329 conn->err_code = CO_ER_NOPROTO;
330 }
331 else
332 conn->err_code = CO_ER_SOCK_ERR;
333
Willy Tarreau9650f372009-08-16 14:02:45 +0200334 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100335 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200336 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200337 }
338
339 if (fd >= global.maxsock) {
340 /* do not log anything there, it's a normal condition when this option
341 * is used to serialize connections to a server !
342 */
343 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
344 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100345 conn->err_code = CO_ER_CONF_FDLIM;
346 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200347 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200348 }
349
350 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900351 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
353 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100354 conn->err_code = CO_ER_SOCK_ERR;
355 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200356 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200357 }
358
359 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900360 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200361
Willy Tarreau9650f372009-08-16 14:02:45 +0200362 /* allow specific binding :
363 * - server-specific at first
364 * - proxy-specific next
365 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100366 if (srv && srv->conn_src.opts & CO_SRC_BIND)
367 src = &srv->conn_src;
368 else if (be->conn_src.opts & CO_SRC_BIND)
369 src = &be->conn_src;
370 else
371 src = NULL;
372
373 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200374 int ret, flags = 0;
375
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200376 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100377 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100378 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200379 conn->flags |= CO_FL_PRIVATE;
380 /* fall through */
381 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200382 flags = 3;
383 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100384 case CO_SRC_TPROXY_CIP:
385 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200386 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200387 flags = 1;
388 break;
389 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200390 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200391
Willy Tarreau9650f372009-08-16 14:02:45 +0200392#ifdef SO_BINDTODEVICE
393 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100394 if (src->iface_name)
395 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200396#endif
397
Willy Tarreaua4380b42012-12-08 22:49:11 +0100398 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200399 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100400 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200401
402 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200403 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200404
405 do {
406 /* note: in case of retry, we may have to release a previously
407 * allocated port, hence this loop's construct.
408 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200409 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
410 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200411
412 if (!attempts)
413 break;
414 attempts--;
415
Willy Tarreaua4380b42012-12-08 22:49:11 +0100416 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100417 if (!fdinfo[fd].local_port) {
418 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200419 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100420 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200421
Willy Tarreaua4380b42012-12-08 22:49:11 +0100422 fdinfo[fd].port_range = src->sport_range;
423 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200424
Willy Tarreaua4380b42012-12-08 22:49:11 +0100425 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100426 if (ret != 0)
427 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200428 } while (ret != 0); /* binding NOK */
429 }
430 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000431#ifdef IP_BIND_ADDRESS_NO_PORT
432 static int bind_address_no_port = 1;
433 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
434#endif
Willy Tarreaua4380b42012-12-08 22:49:11 +0100435 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100436 if (ret != 0)
437 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 }
439
Willy Tarreaua4380b42012-12-08 22:49:11 +0100440 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200441 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
442 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200443 close(fd);
444
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100446 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200447 be->id);
448 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100449 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200450 be->id);
451 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100452 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200453 be->id);
454 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100455 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200456 be->id);
457 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100458 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200459 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200460 }
461 }
462
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400463#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200464 /* disabling tcp quick ack now allows the first request to leave the
465 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100466 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200467 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200468 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900469 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200470#endif
471
Willy Tarreau163d4622015-10-13 16:16:41 +0200472#ifdef TCP_USER_TIMEOUT
473 /* there is not much more we can do here when it fails, it's still minor */
474 if (srv && srv->tcp_ut)
475 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
476#endif
Willy Tarreaue803de22010-01-21 17:43:04 +0100477 if (global.tune.server_sndbuf)
478 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
479
480 if (global.tune.server_rcvbuf)
481 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
482
Willy Tarreau819efbf2017-01-25 14:12:22 +0100483 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
484 if (errno == EINPROGRESS || errno == EALREADY) {
485 /* common case, let's wait for connect status */
486 conn->flags |= CO_FL_WAIT_L4_CONN;
487 }
488 else if (errno == EISCONN) {
489 /* should normally not happen but if so, indicates that it's OK */
490 conn->flags &= ~CO_FL_WAIT_L4_CONN;
491 }
492 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200493 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100494 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200495 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100496 conn->err_code = CO_ER_FREE_PORTS;
497 }
498 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200499 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100500 conn->err_code = CO_ER_ADDR_INUSE;
501 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200502
Willy Tarreaub1719512012-12-08 23:03:28 +0100503 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200504 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
505 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200506 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100507 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100508 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200509 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200510 } else if (errno == ETIMEDOUT) {
511 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200512 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
513 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200514 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100515 conn->err_code = CO_ER_SOCK_ERR;
516 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200517 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200518 } else {
519 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
520 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200521 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
522 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200523 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100524 conn->err_code = CO_ER_SOCK_ERR;
525 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200526 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200527 }
528 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100529 else {
530 /* connect() == 0, this is great! */
531 conn->flags &= ~CO_FL_WAIT_L4_CONN;
532 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200533
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100534 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200535
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200536 /* Prepare to send a few handshakes related to the on-wire protocol. */
537 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200538 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200539
Willy Tarreauf79c8172013-10-21 16:30:56 +0200540 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100541 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200542
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200543 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200544 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100545 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200546 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200547 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200548
Willy Tarreau819efbf2017-01-25 14:12:22 +0100549 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN)) {
550 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
551 }
552 else {
553 /* If there's no more handshake, we need to notify the data
554 * layer when the connection is already OK otherwise we'll have
555 * no other opportunity to do it later (eg: health checks).
556 */
557 data = 1;
558 }
559
Willy Tarreau14f8e862012-08-30 22:23:13 +0200560 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200561 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200562
Willy Tarreaue7dff022015-04-03 01:14:29 +0200563 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200564}
565
566
Willy Tarreau59b94792012-05-11 16:16:40 +0200567/*
568 * Retrieves the source address for the socket <fd>, with <dir> indicating
569 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
570 * success, -1 in case of error. The socket's source address is stored in
571 * <sa> for <salen> bytes.
572 */
573int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
574{
575 if (dir)
576 return getsockname(fd, sa, &salen);
577 else
578 return getpeername(fd, sa, &salen);
579}
580
581
582/*
583 * Retrieves the original destination address for the socket <fd>, with <dir>
584 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
585 * listener, if the original destination address was translated, the original
586 * address is retrieved. It returns 0 in case of success, -1 in case of error.
587 * The socket's source address is stored in <sa> for <salen> bytes.
588 */
589int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
590{
591 if (dir)
592 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100593 else {
594 int ret = getsockname(fd, sa, &salen);
595
596 if (ret < 0)
597 return ret;
598
Willy Tarreau59b94792012-05-11 16:16:40 +0200599#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100600 /* For TPROXY and Netfilter's NAT, we can retrieve the original
601 * IPv4 address before DNAT/REDIRECT. We must not do that with
602 * other families because v6-mapped IPv4 addresses are still
603 * reported as v4.
604 */
605 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
606 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
607 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200608#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100609 return ret;
610 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200611}
612
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200613/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100614 * receive shutdown. Returns positive if the shutdown was found, negative
615 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
616 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200617 */
618int tcp_drain(int fd)
619{
620 int turns = 2;
621 int len;
622
623 while (turns) {
624#ifdef MSG_TRUNC_CLEARS_INPUT
625 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
626 if (len == -1 && errno == EFAULT)
627#endif
628 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
629
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100630 if (len == 0) {
631 /* cool, shutdown received */
632 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200633 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100634 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200635
636 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100637 if (errno == EAGAIN) {
638 /* connection not closed yet */
639 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100640 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100641 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200642 if (errno == EINTR) /* oops, try again */
643 continue;
644 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100645 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200646 return 1;
647 }
648 /* OK we read some data, let's try again once */
649 turns--;
650 }
651 /* some data are still present, give up */
652 return 0;
653}
654
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200655/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100656 * and we have nothing to send. It updates the FD polling status. It returns 0
657 * if it fails in a fatal way or needs to poll to go further, otherwise it
658 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
659 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
660 * errno. The error checking is done in two passes in order to limit the number
661 * of syscalls in the normal case :
662 * - if POLL_ERR was reported by the poller, we check for a pending error on
663 * the socket before proceeding. If found, it's assigned to errno so that
664 * upper layers can see it.
665 * - otherwise connect() is used to check the connection state again, since
666 * the getsockopt return cannot reliably be used to know if the connection
667 * is still pending or ready. This one may often return an error as well,
668 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200669 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200670int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200671{
Willy Tarreau239d7182012-07-23 18:53:03 +0200672 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100673 socklen_t lskerr;
674 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200675
Willy Tarreau80184712012-07-06 14:54:49 +0200676 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200677 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200678
Willy Tarreau3c728722014-01-23 13:50:42 +0100679 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200680 return 0;
681
Willy Tarreau80184712012-07-06 14:54:49 +0200682 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200683 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200684
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100685 if (!fd_send_ready(fd))
686 return 0;
687
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100688 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
689 * without FD_POLL_IN also indicates a hangup without input data meaning
690 * there was no connection.
691 */
692 if (fdtab[fd].ev & FD_POLL_ERR ||
693 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
694 skerr = 0;
695 lskerr = sizeof(skerr);
696 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
697 errno = skerr;
698 if (errno == EAGAIN)
699 errno = 0;
700 if (errno)
701 goto out_error;
702 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200703
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100704 /* Use connect() to check the state of the socket. This has the
705 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200706 * - error
707 * - connecting (EALREADY, EINPROGRESS)
708 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200709 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200710 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200711 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100712 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100713 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200714 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200715 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200716
Willy Tarreau2c6be842012-07-06 17:12:34 +0200717 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200718 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200719
Willy Tarreau2c6be842012-07-06 17:12:34 +0200720 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200721 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200722
Willy Tarreau076be252012-07-06 16:02:29 +0200723 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200724 * forward the event to the transport layer which will notify the
725 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200726 */
Willy Tarreau80184712012-07-06 14:54:49 +0200727 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200728 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200729
730 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200731 /* Write error on the file descriptor. Report it to the connection
732 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200733 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100734 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100735 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100736 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200737 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200738}
739
Olivier Houchard153659f2017-04-05 22:39:56 +0200740/* XXX: Should probably be elsewhere */
741static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
742{
743 if (a->ss_family != b->ss_family) {
744 return (-1);
745 }
746 switch (a->ss_family) {
747 case AF_INET:
748 {
749 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
750 if (a4->sin_port != b4->sin_port)
751 return (-1);
752 return (memcmp(&a4->sin_addr, &b4->sin_addr,
753 sizeof(a4->sin_addr)));
754 }
755 case AF_INET6:
756 {
757 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
758 if (a6->sin6_port != b6->sin6_port)
759 return (-1);
760 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
761 sizeof(a6->sin6_addr)));
762 }
763 default:
764 return (-1);
765 }
766
767}
768
769#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
770/* When binding the listeners, check if a socket has been sent to us by the
771 * previous process that we could reuse, instead of creating a new one.
772 */
773static int tcp_find_compatible_fd(struct listener *l)
774{
775 struct xfer_sock_list *xfer_sock = xfer_sock_list;
776 int ret = -1;
777
778 while (xfer_sock) {
779 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
780 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
781 (l->interface != NULL && xfer_sock->iface != NULL &&
782 !strcmp(l->interface, xfer_sock->iface))) {
783 if ((l->options & LI_MANDATORY_FLAGS) ==
784 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
785 if ((xfer_sock->namespace == NULL &&
786 l->netns == NULL)
787#ifdef CONFIG_HAP_NS
788 || (xfer_sock->namespace != NULL &&
789 l->netns != NULL &&
790 !strcmp(xfer_sock->namespace,
791 l->netns->node.key))
792#endif
793 ) {
794 break;
795 }
796
797 }
798 }
799 }
800 xfer_sock = xfer_sock->next;
801 }
802 if (xfer_sock != NULL) {
803 ret = xfer_sock->fd;
804 if (xfer_sock == xfer_sock_list)
805 xfer_sock_list = xfer_sock->next;
806 if (xfer_sock->prev)
807 xfer_sock->prev->next = xfer_sock->next;
808 if (xfer_sock->next)
809 xfer_sock->next->prev = xfer_sock->prev;
810 free(xfer_sock->iface);
811 free(xfer_sock->namespace);
812 free(xfer_sock);
813 }
814 return ret;
815}
816#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200817
Willy Tarreaue6b98942007-10-29 01:09:36 +0100818/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100819 * an error message in <errmsg> if the message is at most <errlen> bytes long
820 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
821 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100822 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
823 * was alright and that no message was returned. ERR_RETRYABLE means that an
824 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700825 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100826 * the meaning of the error, but just indicate that a message is present which
827 * should be displayed with the respective level. Last, ERR_ABORT indicates
828 * that it's pointless to try to start other listeners. No error message is
829 * returned if errlen is NULL.
830 */
831int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
832{
833 __label__ tcp_return, tcp_close_return;
834 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100835 int ext, ready;
836 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100837 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200838#ifdef TCP_MAXSEG
839
840 /* Create a temporary TCP socket to get default parameters we can't
841 * guess.
842 * */
843 ready_len = sizeof(default_tcp_maxseg);
844 if (default_tcp_maxseg == -1) {
845 default_tcp_maxseg = -2;
846 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
847 if (fd < 0)
848 Warning("Failed to create a temporary socket!\n");
849 else {
850 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
851 &ready_len) == -1)
852 Warning("Failed to get the default value of TCP_MAXSEG\n");
853 }
854 close(fd);
855 }
856 if (default_tcp6_maxseg == -1) {
857 default_tcp6_maxseg = -2;
858 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
859 if (fd >= 0) {
860 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
861 &ready_len) == -1)
862 Warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
863 close(fd);
864 }
865 }
866#endif
867
Willy Tarreaue6b98942007-10-29 01:09:36 +0100868
869 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100870 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100871 *errmsg = 0;
872
873 if (listener->state != LI_ASSIGNED)
874 return ERR_NONE; /* already bound */
875
876 err = ERR_NONE;
877
Olivier Houchard153659f2017-04-05 22:39:56 +0200878 if (listener->fd == -1)
879 listener->fd = tcp_find_compatible_fd(listener);
880
Willy Tarreau40aa0702013-03-10 23:51:38 +0100881 /* if the listener already has an fd assigned, then we were offered the
882 * fd by an external process (most likely the parent), and we don't want
883 * to create a new socket. However we still want to set a few flags on
884 * the socket.
885 */
886 fd = listener->fd;
887 ext = (fd >= 0);
888
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100889 if (!ext) {
890 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
891
892 if (fd == -1) {
893 err |= ERR_RETRYABLE | ERR_ALERT;
894 msg = "cannot create listening socket";
895 goto tcp_return;
896 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100897 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100898
Willy Tarreaue6b98942007-10-29 01:09:36 +0100899 if (fd >= global.maxsock) {
900 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
901 msg = "not enough free sockets (raise '-n' parameter)";
902 goto tcp_close_return;
903 }
904
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200905 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100906 err |= ERR_FATAL | ERR_ALERT;
907 msg = "cannot make socket non-blocking";
908 goto tcp_close_return;
909 }
910
Willy Tarreau40aa0702013-03-10 23:51:38 +0100911 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100912 /* not fatal but should be reported */
913 msg = "cannot do so_reuseaddr";
914 err |= ERR_ALERT;
915 }
916
917 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900918 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200919 else {
920 struct linger tmplinger;
921 socklen_t len = sizeof(tmplinger);
922 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
923 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
924 tmplinger.l_onoff = 0;
925 tmplinger.l_linger = 0;
926 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
927 sizeof(tmplinger));
928 }
929 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100930
Willy Tarreaue6b98942007-10-29 01:09:36 +0100931#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000932 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
933 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100934 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000935 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100936 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100937#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200938
Willy Tarreau40aa0702013-03-10 23:51:38 +0100939 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200940 switch (listener->addr.ss_family) {
941 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200942 if (1
943#if defined(IP_TRANSPARENT)
944 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
945#endif
946#if defined(IP_FREEBIND)
947 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
948#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200949#if defined(IP_BINDANY)
950 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
951#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200952#if defined(SO_BINDANY)
953 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
954#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200955 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200956 msg = "cannot make listening socket transparent";
957 err |= ERR_ALERT;
958 }
959 break;
960 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200961 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200962#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200963 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
964#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100965#if defined(IP_FREEBIND)
966 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
967#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200968#if defined(IPV6_BINDANY)
969 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
970#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200971#if defined(SO_BINDANY)
972 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
973#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200974 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200975 msg = "cannot make listening socket transparent";
976 err |= ERR_ALERT;
977 }
978 break;
979 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100980 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200981
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100982#ifdef SO_BINDTODEVICE
983 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100984 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100985 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100986 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100987 msg = "cannot bind listener to device";
988 err |= ERR_WARN;
989 }
990 }
991#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400992#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100993 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400994 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200995 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
996 msg = "cannot set MSS";
997 err |= ERR_WARN;
998 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200999 } else if (ext) {
1000 int tmpmaxseg = -1;
1001 int defaultmss;
1002 socklen_t len = sizeof(tmpmaxseg);
1003
1004 if (listener->addr.ss_family == AF_INET)
1005 defaultmss = default_tcp_maxseg;
1006 else
1007 defaultmss = default_tcp6_maxseg;
1008
1009 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
1010 if (tmpmaxseg != defaultmss && setsockopt(fd, IPPROTO_TCP,
1011 TCP_MAXSEG, &defaultmss,
1012 sizeof(defaultmss)) == -1) {
1013 msg = "cannot set MSS";
1014 err |= ERR_WARN;
1015 }
Willy Tarreaube1b9182009-06-14 18:48:19 +02001016 }
1017#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001018#if defined(TCP_USER_TIMEOUT)
1019 if (listener->tcp_ut) {
1020 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
1021 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
1022 msg = "cannot set TCP User Timeout";
1023 err |= ERR_WARN;
1024 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001025 } else
1026 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
1027 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +01001028#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001029#if defined(TCP_DEFER_ACCEPT)
1030 if (listener->options & LI_O_DEF_ACCEPT) {
1031 /* defer accept by up to one second */
1032 int accept_delay = 1;
1033 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
1034 msg = "cannot enable DEFER_ACCEPT";
1035 err |= ERR_WARN;
1036 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001037 } else
1038 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
1039 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001040#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001041#if defined(TCP_FASTOPEN)
1042 if (listener->options & LI_O_TCP_FO) {
1043 /* TFO needs a queue length, let's use the configured backlog */
1044 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
1045 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
1046 msg = "cannot enable TCP_FASTOPEN";
1047 err |= ERR_WARN;
1048 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001049 } else {
1050 socklen_t len;
1051 int qlen;
1052 len = sizeof(qlen);
1053 /* Only disable fast open if it was enabled, we don't want
1054 * the kernel to create a fast open queue if there's none.
1055 */
1056 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
1057 qlen != 0) {
1058 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
1059 sizeof(zero)) == -1) {
1060 msg = "cannot disable TCP_FASTOPEN";
1061 err |= ERR_WARN;
1062 }
1063 }
Willy Tarreau1c862c52012-10-05 16:21:00 +02001064 }
1065#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001066#if defined(IPV6_V6ONLY)
1067 if (listener->options & LI_O_V6ONLY)
1068 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +01001069 else if (listener->options & LI_O_V4V6)
1070 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001071#endif
1072
Willy Tarreau40aa0702013-03-10 23:51:38 +01001073 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001074 err |= ERR_RETRYABLE | ERR_ALERT;
1075 msg = "cannot bind socket";
1076 goto tcp_close_return;
1077 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001078
Willy Tarreau40aa0702013-03-10 23:51:38 +01001079 ready = 0;
1080 ready_len = sizeof(ready);
1081 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
1082 ready = 0;
1083
1084 if (!(ext && ready) && /* only listen if not already done by external process */
1085 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001086 err |= ERR_RETRYABLE | ERR_ALERT;
1087 msg = "cannot listen to socket";
1088 goto tcp_close_return;
1089 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001090
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001091#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001092 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +09001093 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +02001094 else
1095 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001096#endif
1097
Willy Tarreaue6b98942007-10-29 01:09:36 +01001098 /* the socket is ready */
1099 listener->fd = fd;
1100 listener->state = LI_LISTEN;
1101
Willy Tarreaueabf3132008-08-29 23:36:51 +02001102 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +02001103 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +02001104 fd_insert(fd);
1105
Willy Tarreaue6b98942007-10-29 01:09:36 +01001106 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001107 if (msg && errlen) {
1108 char pn[INET6_ADDRSTRLEN];
1109
Willy Tarreau631f01c2011-09-05 00:36:48 +02001110 addr_to_str(&listener->addr, pn, sizeof(pn));
1111 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001112 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001113 return err;
1114
1115 tcp_close_return:
1116 close(fd);
1117 goto tcp_return;
1118}
1119
1120/* This function creates all TCP sockets bound to the protocol entry <proto>.
1121 * It is intended to be used as the protocol's bind_all() function.
1122 * The sockets will be registered but not added to any fd_set, in order not to
1123 * loose them across the fork(). A call to enable_all_listeners() is needed
1124 * to complete initialization. The return value is composed from ERR_*.
1125 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001126static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001127{
1128 struct listener *listener;
1129 int err = ERR_NONE;
1130
1131 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001132 err |= tcp_bind_listener(listener, errmsg, errlen);
1133 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001134 break;
1135 }
1136
1137 return err;
1138}
1139
1140/* Add listener to the list of tcpv4 listeners. The listener's state
1141 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1142 * listeners is updated. This is the function to use to add a new listener.
1143 */
1144void tcpv4_add_listener(struct listener *listener)
1145{
1146 if (listener->state != LI_INIT)
1147 return;
1148 listener->state = LI_ASSIGNED;
1149 listener->proto = &proto_tcpv4;
1150 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1151 proto_tcpv4.nb_listeners++;
1152}
1153
1154/* Add listener to the list of tcpv4 listeners. The listener's state
1155 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1156 * listeners is updated. This is the function to use to add a new listener.
1157 */
1158void tcpv6_add_listener(struct listener *listener)
1159{
1160 if (listener->state != LI_INIT)
1161 return;
1162 listener->state = LI_ASSIGNED;
1163 listener->proto = &proto_tcpv6;
1164 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1165 proto_tcpv6.nb_listeners++;
1166}
1167
Willy Tarreau092d8652014-07-07 20:22:12 +02001168/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1169 * was totally stopped, or > 0 if correctly paused.
1170 */
1171int tcp_pause_listener(struct listener *l)
1172{
1173 if (shutdown(l->fd, SHUT_WR) != 0)
1174 return -1; /* Solaris dies here */
1175
1176 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1177 return -1; /* OpenBSD dies here */
1178
1179 if (shutdown(l->fd, SHUT_RD) != 0)
1180 return -1; /* should always be OK */
1181 return 1;
1182}
1183
William Lallemand2e785f22016-05-25 01:48:42 +02001184/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001185 * Execute the "set-src" action. May be called from {tcp,http}request.
1186 * It only changes the address and tries to preserve the original port. If the
1187 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001188 */
1189enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1190 struct session *sess, struct stream *s, int flags)
1191{
1192 struct connection *cli_conn;
1193
1194 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1195 struct sample *smp;
1196
1197 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1198 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001199 int port = get_net_port(&cli_conn->addr.from);
1200
William Lallemand2e785f22016-05-25 01:48:42 +02001201 if (smp->data.type == SMP_T_IPV4) {
1202 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
1203 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001204 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001205 } else if (smp->data.type == SMP_T_IPV6) {
1206 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
1207 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 +02001208 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001209 }
1210 }
William Lallemand01252ed2016-05-25 02:33:16 +02001211 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001212 }
1213 return ACT_RET_CONT;
1214}
1215
William Lallemand44be6402016-05-25 01:51:35 +02001216/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001217 * Execute the "set-dst" action. May be called from {tcp,http}request.
1218 * It only changes the address and tries to preserve the original port. If the
1219 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001220 */
1221enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1222 struct session *sess, struct stream *s, int flags)
1223{
1224 struct connection *cli_conn;
1225
1226 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1227 struct sample *smp;
1228
1229 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1230 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001231 int port = get_net_port(&cli_conn->addr.to);
1232
William Lallemand13e9b0c2016-05-25 02:34:07 +02001233 if (smp->data.type == SMP_T_IPV4) {
1234 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_family = AF_INET;
1235 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1236 } else if (smp->data.type == SMP_T_IPV6) {
1237 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_family = AF_INET6;
1238 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 +02001239 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001240 }
1241 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1242 }
1243 }
1244 return ACT_RET_CONT;
1245}
1246
1247/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001248 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1249 * We must test the sin_family before setting the port. If the address family
1250 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1251 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001252 */
1253enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1254 struct session *sess, struct stream *s, int flags)
1255{
1256 struct connection *cli_conn;
1257
1258 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1259 struct sample *smp;
1260
1261 conn_get_from_addr(cli_conn);
1262
1263 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1264 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001265 if (cli_conn->addr.from.ss_family == AF_INET6) {
William Lallemand44be6402016-05-25 01:51:35 +02001266 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001267 } else {
1268 if (cli_conn->addr.from.ss_family != AF_INET) {
1269 cli_conn->addr.from.ss_family = AF_INET;
1270 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1271 }
1272 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001273 }
1274 }
1275 }
1276 return ACT_RET_CONT;
1277}
1278
William Lallemand13e9b0c2016-05-25 02:34:07 +02001279/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001280 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1281 * We must test the sin_family before setting the port. If the address family
1282 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1283 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001284 */
1285enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1286 struct session *sess, struct stream *s, int flags)
1287{
1288 struct connection *cli_conn;
1289
1290 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1291 struct sample *smp;
1292
1293 conn_get_to_addr(cli_conn);
1294
1295 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1296 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001297 if (cli_conn->addr.from.ss_family == AF_INET6) {
1298 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
1299 } else {
1300 if (cli_conn->addr.from.ss_family != AF_INET) {
1301 cli_conn->addr.from.ss_family = AF_INET;
1302 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1303 }
1304 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001305 }
1306 }
1307 }
1308 return ACT_RET_CONT;
1309}
1310
Willy Tarreau2d392c22015-08-24 01:43:45 +02001311/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1312static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1313{
1314 struct connection *conn = objt_conn(sess->origin);
1315
1316 if (!conn)
1317 goto out;
1318
1319 if (!conn_ctrl_ready(conn))
1320 goto out;
1321
Willy Tarreau2d392c22015-08-24 01:43:45 +02001322#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001323 /* drain is needed only to send the quick ACK */
1324 conn_sock_drain(conn);
1325
Willy Tarreau2d392c22015-08-24 01:43:45 +02001326 /* re-enable quickack if it was disabled to ack all data and avoid
1327 * retransmits from the client that might trigger a real reset.
1328 */
1329 setsockopt(conn->t.sock.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
1330#endif
1331 /* lingering must absolutely be disabled so that we don't send a
1332 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1333 * is present, returning with ERR will cause lingering to be disabled.
1334 */
1335 if (strm)
1336 strm->si[0].flags |= SI_FL_NOLINGER;
1337
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001338 /* We're on the client-facing side, we must force to disable lingering to
1339 * ensure we will use an RST exclusively and kill any pending data.
1340 */
1341 fdtab[conn->t.sock.fd].linger_risk = 1;
1342
Willy Tarreau2d392c22015-08-24 01:43:45 +02001343#ifdef TCP_REPAIR
1344 if (setsockopt(conn->t.sock.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
1345 /* socket will be quiet now */
1346 goto out;
1347 }
1348#endif
1349 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1350 * Let's fall back on the TTL trick, though it only works for routed
1351 * network and has no effect on local net.
1352 */
1353#ifdef IP_TTL
1354 setsockopt(conn->t.sock.fd, SOL_IP, IP_TTL, &one, sizeof(one));
1355#endif
1356 out:
1357 /* kill the stream if any */
1358 if (strm) {
1359 channel_abort(&strm->req);
1360 channel_abort(&strm->res);
1361 strm->req.analysers = 0;
1362 strm->res.analysers = 0;
1363 strm->be->be_counters.denied_req++;
1364 if (!(strm->flags & SF_ERR_MASK))
1365 strm->flags |= SF_ERR_PRXCOND;
1366 if (!(strm->flags & SF_FINST_MASK))
1367 strm->flags |= SF_FINST_R;
1368 }
1369
1370 sess->fe->fe_counters.denied_req++;
1371 if (sess->listener->counters)
1372 sess->listener->counters->denied_req++;
1373
1374 return ACT_RET_STOP;
1375}
1376
William Lallemand13e9b0c2016-05-25 02:34:07 +02001377/* parse "set-{src,dst}[-port]" action */
1378enum 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 +02001379{
1380 int cur_arg;
1381 struct sample_expr *expr;
1382 unsigned int where;
1383
1384 cur_arg = *orig_arg;
1385 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
1386 if (!expr)
1387 return ACT_RET_PRS_ERR;
1388
1389 where = 0;
1390 if (proxy->cap & PR_CAP_FE)
1391 where |= SMP_VAL_FE_HRQ_HDR;
1392 if (proxy->cap & PR_CAP_BE)
1393 where |= SMP_VAL_BE_HRQ_HDR;
1394
1395 if (!(expr->fetch->val & where)) {
1396 memprintf(err,
1397 "fetch method '%s' extracts information from '%s', none of which is available here",
1398 args[cur_arg-1], sample_src_names(expr->fetch->use));
1399 free(expr);
1400 return ACT_RET_PRS_ERR;
1401 }
1402 rule->arg.expr = expr;
1403 rule->action = ACT_CUSTOM;
1404
1405 if (!strcmp(args[*orig_arg-1], "set-src")) {
1406 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001407 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1408 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001409 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1410 rule->action_ptr = tcp_action_req_set_dst;
1411 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1412 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001413 } else {
1414 return ACT_RET_PRS_ERR;
1415 }
1416
1417 (*orig_arg)++;
1418
1419 return ACT_RET_PRS_OK;
1420}
1421
1422
Willy Tarreau2d392c22015-08-24 01:43:45 +02001423/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1424 * success, ACT_RET_PRS_ERR on error.
1425 */
1426static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1427 struct act_rule *rule, char **err)
1428{
1429 rule->action = ACT_CUSTOM;
1430 rule->action_ptr = tcp_exec_action_silent_drop;
1431 return ACT_RET_PRS_OK;
1432}
1433
Willy Tarreau645513a2010-05-24 20:55:15 +02001434
1435/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001436/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001437/************************************************************************/
1438
Willy Tarreau4a129812012-04-25 17:31:42 +02001439/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001440int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001441{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001442 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001443
1444 if (!cli_conn)
1445 return 0;
1446
1447 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001448 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001449 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001450 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001451 break;
1452 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001453 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001454 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001455 break;
1456 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001457 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001458 }
Emeric Brunf769f512010-10-22 17:14:01 +02001459
Willy Tarreau37406352012-04-23 16:16:37 +02001460 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001461 return 1;
1462}
1463
Willy Tarreaua5e37562011-12-16 17:06:15 +01001464/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001465static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001466smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001467{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001468 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001469
1470 if (!cli_conn)
1471 return 0;
1472
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001473 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001474 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001475 return 0;
1476
Willy Tarreau37406352012-04-23 16:16:37 +02001477 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001478 return 1;
1479}
1480
Willy Tarreau4a129812012-04-25 17:31:42 +02001481/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001482static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001483smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001484{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001485 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001486
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001487 if (!cli_conn)
1488 return 0;
1489
1490 conn_get_to_addr(cli_conn);
1491
1492 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001493 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001494 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001495 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001496 break;
1497 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001498 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001499 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001500 break;
1501 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001502 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001503 }
Emeric Brunf769f512010-10-22 17:14:01 +02001504
Willy Tarreau37406352012-04-23 16:16:37 +02001505 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001506 return 1;
1507}
1508
Willy Tarreau16e01562016-08-09 16:46:18 +02001509/* check if the destination address of the front connection is local to the
1510 * system or if it was intercepted.
1511 */
1512int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1513{
1514 struct connection *conn = objt_conn(smp->sess->origin);
1515 struct listener *li = smp->sess->listener;
1516
1517 if (!conn)
1518 return 0;
1519
1520 conn_get_to_addr(conn);
1521 if (!(conn->flags & CO_FL_ADDR_TO_SET))
1522 return 0;
1523
1524 smp->data.type = SMP_T_BOOL;
1525 smp->flags = 0;
1526 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.to);
1527 return smp->data.u.sint >= 0;
1528}
1529
1530/* check if the source address of the front connection is local to the system
1531 * or not.
1532 */
1533int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1534{
1535 struct connection *conn = objt_conn(smp->sess->origin);
1536 struct listener *li = smp->sess->listener;
1537
1538 if (!conn)
1539 return 0;
1540
1541 conn_get_from_addr(conn);
1542 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
1543 return 0;
1544
1545 smp->data.type = SMP_T_BOOL;
1546 smp->flags = 0;
1547 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.from);
1548 return smp->data.u.sint >= 0;
1549}
1550
Willy Tarreaua5e37562011-12-16 17:06:15 +01001551/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001552static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001553smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001554{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001555 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001556
1557 if (!cli_conn)
1558 return 0;
1559
1560 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001561
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001562 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001563 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001564 return 0;
1565
Willy Tarreau37406352012-04-23 16:16:37 +02001566 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001567 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001568}
1569
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001570#ifdef TCP_INFO
1571
1572/* Returns some tcp_info data is its avalaible. "dir" must be set to 0 if
1573 * the client connection is require, otherwise it is set to 1. "val" represents
1574 * the required value. Use 0 for rtt and 1 for rttavg. "unit" is the expected unit
1575 * by default, the rtt is in us. Id "unit" is set to 0, the unit is us, if it is
1576 * set to 1, the untis are milliseconds.
1577 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1578 */
1579static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1580 int dir, int val)
1581{
1582 struct connection *conn;
1583 struct tcp_info info;
1584 socklen_t optlen;
1585
1586 /* strm can be null. */
1587 if (!smp->strm)
1588 return 0;
1589
1590 /* get the object associated with the stream interface.The
1591 * object can be other thing than a connection. For example,
1592 * it be a appctx. */
1593 conn = objt_conn(smp->strm->si[dir].end);
1594 if (!conn)
1595 return 0;
1596
1597 /* The fd may not be avalaible for the tcp_info struct, and the
1598 syscal can fail. */
1599 optlen = sizeof(info);
1600 if (getsockopt(conn->t.sock.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
1601 return 0;
1602
1603 /* extract the value. */
1604 smp->data.type = SMP_T_SINT;
1605 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001606 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1607 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1608#if defined(__linux__)
1609 /* these ones are common to all Linux versions */
1610 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1611 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1612 case 4: smp->data.u.sint = info.tcpi_lost; break;
1613 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1614 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1615 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1616#elif defined(__FreeBSD__) || defined(__NetBSD__)
1617 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1618 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1619 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1620 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1621 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1622 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1623 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1624#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001625 default: return 0;
1626 }
1627
1628 /* Convert the value as expected. */
1629 if (args) {
1630 if (args[0].type == ARGT_STR) {
1631 if (strcmp(args[0].data.str.str, "us") == 0) {
1632 /* Do nothing. */
1633 } else if (strcmp(args[0].data.str.str, "ms") == 0) {
1634 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1635 } else
1636 return 0;
1637 } else if (args[0].type == ARGT_STOP) {
1638 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1639 } else
1640 return 0;
1641 }
1642
1643 return 1;
1644}
1645
1646/* get the mean rtt of a client connexion */
1647static int
1648smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1649{
1650 if (!get_tcp_info(args, smp, 0, 0))
1651 return 0;
1652 return 1;
1653}
1654
1655/* get the variance of the mean rtt of a client connexion */
1656static int
1657smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1658{
1659 if (!get_tcp_info(args, smp, 0, 1))
1660 return 0;
1661 return 1;
1662}
Joe Williams30fcd392016-08-10 07:06:44 -07001663
1664#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1665
1666/* get the unacked counter on a client connexion */
1667static int
1668smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1669{
1670 if (!get_tcp_info(args, smp, 0, 2))
1671 return 0;
1672 return 1;
1673}
1674
1675/* get the sacked counter on a client connexion */
1676static int
1677smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1678{
1679 if (!get_tcp_info(args, smp, 0, 3))
1680 return 0;
1681 return 1;
1682}
1683
1684/* get the lost counter on a client connexion */
1685static int
1686smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1687{
1688 if (!get_tcp_info(args, smp, 0, 4))
1689 return 0;
1690 return 1;
1691}
1692
1693/* get the retrans counter on a client connexion */
1694static int
1695smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1696{
1697 if (!get_tcp_info(args, smp, 0, 5))
1698 return 0;
1699 return 1;
1700}
1701
1702/* get the fackets counter on a client connexion */
1703static int
1704smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1705{
1706 if (!get_tcp_info(args, smp, 0, 6))
1707 return 0;
1708 return 1;
1709}
1710
1711/* get the reordering counter on a client connexion */
1712static int
1713smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1714{
1715 if (!get_tcp_info(args, smp, 0, 7))
1716 return 0;
1717 return 1;
1718}
1719#endif // linux || freebsd || netbsd
1720#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001721
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001722#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001723/* parse the "v4v6" bind keyword */
1724static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1725{
1726 struct listener *l;
1727
1728 list_for_each_entry(l, &conf->listeners, by_bind) {
1729 if (l->addr.ss_family == AF_INET6)
1730 l->options |= LI_O_V4V6;
1731 }
1732
1733 return 0;
1734}
1735
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001736/* parse the "v6only" bind keyword */
1737static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1738{
1739 struct listener *l;
1740
1741 list_for_each_entry(l, &conf->listeners, by_bind) {
1742 if (l->addr.ss_family == AF_INET6)
1743 l->options |= LI_O_V6ONLY;
1744 }
1745
1746 return 0;
1747}
1748#endif
1749
Pieter Baauwd551fb52013-05-08 22:49:23 +02001750#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001751/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001752static 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 +02001753{
1754 struct listener *l;
1755
Willy Tarreau4348fad2012-09-20 16:48:07 +02001756 list_for_each_entry(l, &conf->listeners, by_bind) {
1757 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1758 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001759 }
1760
Willy Tarreau44791242012-09-12 23:27:21 +02001761 return 0;
1762}
1763#endif
1764
1765#ifdef TCP_DEFER_ACCEPT
1766/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001767static 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 +02001768{
1769 struct listener *l;
1770
Willy Tarreau4348fad2012-09-20 16:48:07 +02001771 list_for_each_entry(l, &conf->listeners, by_bind) {
1772 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1773 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001774 }
1775
Willy Tarreau44791242012-09-12 23:27:21 +02001776 return 0;
1777}
1778#endif
1779
Willy Tarreau1c862c52012-10-05 16:21:00 +02001780#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001781/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001782static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1783{
1784 struct listener *l;
1785
1786 list_for_each_entry(l, &conf->listeners, by_bind) {
1787 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1788 l->options |= LI_O_TCP_FO;
1789 }
1790
1791 return 0;
1792}
1793#endif
1794
Willy Tarreau44791242012-09-12 23:27:21 +02001795#ifdef TCP_MAXSEG
1796/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001797static 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 +02001798{
1799 struct listener *l;
1800 int mss;
1801
Willy Tarreau44791242012-09-12 23:27:21 +02001802 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001803 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001804 return ERR_ALERT | ERR_FATAL;
1805 }
1806
1807 mss = atoi(args[cur_arg + 1]);
1808 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001809 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001810 return ERR_ALERT | ERR_FATAL;
1811 }
1812
Willy Tarreau4348fad2012-09-20 16:48:07 +02001813 list_for_each_entry(l, &conf->listeners, by_bind) {
1814 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1815 l->maxseg = mss;
1816 }
Willy Tarreau44791242012-09-12 23:27:21 +02001817
1818 return 0;
1819}
1820#endif
1821
Willy Tarreau2af207a2015-02-04 00:45:58 +01001822#ifdef TCP_USER_TIMEOUT
1823/* parse the "tcp-ut" bind keyword */
1824static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1825{
1826 const char *ptr = NULL;
1827 struct listener *l;
1828 unsigned int timeout;
1829
1830 if (!*args[cur_arg + 1]) {
1831 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1832 return ERR_ALERT | ERR_FATAL;
1833 }
1834
1835 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
1836 if (ptr) {
1837 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1838 return ERR_ALERT | ERR_FATAL;
1839 }
1840
1841 list_for_each_entry(l, &conf->listeners, by_bind) {
1842 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1843 l->tcp_ut = timeout;
1844 }
1845
1846 return 0;
1847}
1848#endif
1849
Willy Tarreau44791242012-09-12 23:27:21 +02001850#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001851/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001852static 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 +02001853{
1854 struct listener *l;
1855
Willy Tarreau44791242012-09-12 23:27:21 +02001856 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001857 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001858 return ERR_ALERT | ERR_FATAL;
1859 }
1860
Willy Tarreau4348fad2012-09-20 16:48:07 +02001861 list_for_each_entry(l, &conf->listeners, by_bind) {
1862 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1863 l->interface = strdup(args[cur_arg + 1]);
1864 }
Willy Tarreau44791242012-09-12 23:27:21 +02001865
Willy Tarreau44791242012-09-12 23:27:21 +02001866 return 0;
1867}
1868#endif
1869
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001870#ifdef CONFIG_HAP_NS
1871/* parse the "namespace" bind keyword */
1872static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1873{
1874 struct listener *l;
1875 char *namespace = NULL;
1876
1877 if (!*args[cur_arg + 1]) {
1878 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1879 return ERR_ALERT | ERR_FATAL;
1880 }
1881 namespace = args[cur_arg + 1];
1882
1883 list_for_each_entry(l, &conf->listeners, by_bind) {
1884 l->netns = netns_store_lookup(namespace, strlen(namespace));
1885
1886 if (l->netns == NULL)
1887 l->netns = netns_store_insert(namespace);
1888
1889 if (l->netns == NULL) {
1890 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1891 return ERR_ALERT | ERR_FATAL;
1892 }
1893 }
1894 return 0;
1895}
1896#endif
1897
Willy Tarreau163d4622015-10-13 16:16:41 +02001898#ifdef TCP_USER_TIMEOUT
1899/* parse the "tcp-ut" server keyword */
1900static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1901{
1902 const char *ptr = NULL;
1903 unsigned int timeout;
1904
1905 if (!*args[*cur_arg + 1]) {
1906 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1907 return ERR_ALERT | ERR_FATAL;
1908 }
1909
1910 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
1911 if (ptr) {
1912 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1913 return ERR_ALERT | ERR_FATAL;
1914 }
1915
1916 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1917 newsrv->tcp_ut = timeout;
1918
1919 return 0;
1920}
1921#endif
1922
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001923
Willy Tarreau4a129812012-04-25 17:31:42 +02001924/* Note: must not be declared <const> as its list will be overwritten.
1925 * Note: fetches that may return multiple types must be declared as the lowest
1926 * common denominator, the type that can be casted into all other ones. For
1927 * instance v4/v6 must be declared v4.
1928 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001929static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001930 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001931 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001932 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001933 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001934 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001935 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001936#ifdef TCP_INFO
Joe Williams30fcd392016-08-10 07:06:44 -07001937 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1938 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1939#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1940 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1941 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1942 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1943 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1944 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1945 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1946#endif // linux || freebsd || netbsd
1947#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001948 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001949}};
1950
Willy Tarreau44791242012-09-12 23:27:21 +02001951/************************************************************************/
1952/* All supported bind keywords must be declared here. */
1953/************************************************************************/
1954
1955/* Note: must not be declared <const> as its list will be overwritten.
1956 * Please take care of keeping this list alphabetically sorted, doing so helps
1957 * all code contributors.
1958 * Optional keywords are also declared with a NULL ->parse() function so that
1959 * the config parser can report an appropriate error when a known keyword was
1960 * not enabled.
1961 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001962static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001963#ifdef TCP_DEFER_ACCEPT
1964 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1965#endif
1966#ifdef SO_BINDTODEVICE
1967 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1968#endif
1969#ifdef TCP_MAXSEG
1970 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1971#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001972#ifdef TCP_USER_TIMEOUT
1973 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1974#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001975#ifdef TCP_FASTOPEN
1976 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1977#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001978#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001979 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1980#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001981#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001982 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001983 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1984#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001985#ifdef CONFIG_HAP_NS
1986 { "namespace", bind_parse_namespace, 1 },
1987#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001988 /* the versions with the NULL parse function*/
1989 { "defer-accept", NULL, 0 },
1990 { "interface", NULL, 1 },
1991 { "mss", NULL, 1 },
1992 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001993 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001994 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001995 { NULL, NULL, 0 },
1996}};
1997
Willy Tarreau163d4622015-10-13 16:16:41 +02001998static struct srv_kw_list srv_kws = { "TCP", { }, {
1999#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002000 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02002001#endif
2002 { NULL, NULL, 0 },
2003}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02002004
2005static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002006 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002007 { "set-src", tcp_parse_set_src_dst },
2008 { "set-src-port", tcp_parse_set_src_dst },
2009 { "set-dst" , tcp_parse_set_src_dst },
2010 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002011 { /* END */ }
2012}};
2013
Willy Tarreau620408f2016-10-21 16:37:51 +02002014static struct action_kw_list tcp_req_sess_actions = {ILH, {
2015 { "silent-drop", tcp_parse_silent_drop },
2016 { "set-src", tcp_parse_set_src_dst },
2017 { "set-src-port", tcp_parse_set_src_dst },
2018 { "set-dst" , tcp_parse_set_src_dst },
2019 { "set-dst-port", tcp_parse_set_src_dst },
2020 { /* END */ }
2021}};
2022
Willy Tarreau2d392c22015-08-24 01:43:45 +02002023static struct action_kw_list tcp_req_cont_actions = {ILH, {
2024 { "silent-drop", tcp_parse_silent_drop },
2025 { /* END */ }
2026}};
2027
2028static struct action_kw_list tcp_res_cont_actions = {ILH, {
2029 { "silent-drop", tcp_parse_silent_drop },
2030 { /* END */ }
2031}};
2032
2033static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002034 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002035 { "set-src", tcp_parse_set_src_dst },
2036 { "set-src-port", tcp_parse_set_src_dst },
2037 { "set-dst", tcp_parse_set_src_dst },
2038 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002039 { /* END */ }
2040}};
2041
2042static struct action_kw_list http_res_actions = {ILH, {
2043 { "silent-drop", tcp_parse_silent_drop },
2044 { /* END */ }
2045}};
2046
2047
Willy Tarreaue6b98942007-10-29 01:09:36 +01002048__attribute__((constructor))
2049static void __tcp_protocol_init(void)
2050{
2051 protocol_register(&proto_tcpv4);
2052 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002053 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau44791242012-09-12 23:27:21 +02002054 bind_register_keywords(&bind_kws);
Willy Tarreau163d4622015-10-13 16:16:41 +02002055 srv_register_keywords(&srv_kws);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002056 tcp_req_conn_keywords_register(&tcp_req_conn_actions);
Willy Tarreau620408f2016-10-21 16:37:51 +02002057 tcp_req_sess_keywords_register(&tcp_req_sess_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002058 tcp_req_cont_keywords_register(&tcp_req_cont_actions);
2059 tcp_res_cont_keywords_register(&tcp_res_cont_actions);
2060 http_req_keywords_register(&http_req_actions);
2061 http_res_keywords_register(&http_res_actions);
Willy Tarreauba962912016-12-21 18:55:02 +01002062
2063
2064 hap_register_build_opts("Built with transparent proxy support using:"
2065#if defined(IP_TRANSPARENT)
2066 " IP_TRANSPARENT"
2067#endif
2068#if defined(IPV6_TRANSPARENT)
2069 " IPV6_TRANSPARENT"
2070#endif
2071#if defined(IP_FREEBIND)
2072 " IP_FREEBIND"
2073#endif
2074#if defined(IP_BINDANY)
2075 " IP_BINDANY"
2076#endif
2077#if defined(IPV6_BINDANY)
2078 " IPV6_BINDANY"
2079#endif
2080#if defined(SO_BINDANY)
2081 " SO_BINDANY"
2082#endif
2083 "", 0);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002084}
2085
2086
2087/*
2088 * Local variables:
2089 * c-indent-level: 8
2090 * c-basic-offset: 8
2091 * End:
2092 */