blob: 6b1505a3a5ff2ac949eb2fc4dfc5a964a01f8759 [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
Willy Tarreau585744b2017-08-24 14:31:19 +0200305 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100306
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 Tarreau585744b2017-08-24 14:31:19 +0200672 int fd = conn->handle.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
Willy Tarreau32282382017-09-15 07:44:44 +02001140/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1141 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1142 * The number of listeners for the protocol is updated.
Willy Tarreaue6b98942007-10-29 01:09:36 +01001143 */
Willy Tarreau32282382017-09-15 07:44:44 +02001144void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001145{
1146 if (listener->state != LI_INIT)
1147 return;
1148 listener->state = LI_ASSIGNED;
1149 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001150 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001151 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1152 proto_tcpv4.nb_listeners++;
1153}
1154
Willy Tarreau32282382017-09-15 07:44:44 +02001155/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1156 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1157 * The number of listeners for the protocol is updated.
Willy Tarreaue6b98942007-10-29 01:09:36 +01001158 */
Willy Tarreau32282382017-09-15 07:44:44 +02001159void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001160{
1161 if (listener->state != LI_INIT)
1162 return;
1163 listener->state = LI_ASSIGNED;
1164 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001165 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001166 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1167 proto_tcpv6.nb_listeners++;
1168}
1169
Willy Tarreau092d8652014-07-07 20:22:12 +02001170/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1171 * was totally stopped, or > 0 if correctly paused.
1172 */
1173int tcp_pause_listener(struct listener *l)
1174{
1175 if (shutdown(l->fd, SHUT_WR) != 0)
1176 return -1; /* Solaris dies here */
1177
1178 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1179 return -1; /* OpenBSD dies here */
1180
1181 if (shutdown(l->fd, SHUT_RD) != 0)
1182 return -1; /* should always be OK */
1183 return 1;
1184}
1185
William Lallemand2e785f22016-05-25 01:48:42 +02001186/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001187 * Execute the "set-src" action. May be called from {tcp,http}request.
1188 * It only changes the address and tries to preserve the original port. If the
1189 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001190 */
1191enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1192 struct session *sess, struct stream *s, int flags)
1193{
1194 struct connection *cli_conn;
1195
1196 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1197 struct sample *smp;
1198
1199 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1200 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001201 int port = get_net_port(&cli_conn->addr.from);
1202
William Lallemand2e785f22016-05-25 01:48:42 +02001203 if (smp->data.type == SMP_T_IPV4) {
1204 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
1205 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001206 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001207 } else if (smp->data.type == SMP_T_IPV6) {
1208 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
1209 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 +02001210 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001211 }
1212 }
William Lallemand01252ed2016-05-25 02:33:16 +02001213 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001214 }
1215 return ACT_RET_CONT;
1216}
1217
William Lallemand44be6402016-05-25 01:51:35 +02001218/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001219 * Execute the "set-dst" action. May be called from {tcp,http}request.
1220 * It only changes the address and tries to preserve the original port. If the
1221 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001222 */
1223enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1224 struct session *sess, struct stream *s, int flags)
1225{
1226 struct connection *cli_conn;
1227
1228 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1229 struct sample *smp;
1230
1231 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1232 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001233 int port = get_net_port(&cli_conn->addr.to);
1234
William Lallemand13e9b0c2016-05-25 02:34:07 +02001235 if (smp->data.type == SMP_T_IPV4) {
1236 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_family = AF_INET;
1237 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1238 } else if (smp->data.type == SMP_T_IPV6) {
1239 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_family = AF_INET6;
1240 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 +02001241 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001242 }
1243 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1244 }
1245 }
1246 return ACT_RET_CONT;
1247}
1248
1249/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001250 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1251 * We must test the sin_family before setting the port. If the address family
1252 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1253 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001254 */
1255enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1256 struct session *sess, struct stream *s, int flags)
1257{
1258 struct connection *cli_conn;
1259
1260 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1261 struct sample *smp;
1262
1263 conn_get_from_addr(cli_conn);
1264
1265 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1266 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001267 if (cli_conn->addr.from.ss_family == AF_INET6) {
William Lallemand44be6402016-05-25 01:51:35 +02001268 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001269 } else {
1270 if (cli_conn->addr.from.ss_family != AF_INET) {
1271 cli_conn->addr.from.ss_family = AF_INET;
1272 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1273 }
1274 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001275 }
1276 }
1277 }
1278 return ACT_RET_CONT;
1279}
1280
William Lallemand13e9b0c2016-05-25 02:34:07 +02001281/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001282 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1283 * We must test the sin_family before setting the port. If the address family
1284 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1285 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001286 */
1287enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1288 struct session *sess, struct stream *s, int flags)
1289{
1290 struct connection *cli_conn;
1291
1292 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1293 struct sample *smp;
1294
1295 conn_get_to_addr(cli_conn);
1296
1297 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1298 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001299 if (cli_conn->addr.from.ss_family == AF_INET6) {
1300 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
1301 } else {
1302 if (cli_conn->addr.from.ss_family != AF_INET) {
1303 cli_conn->addr.from.ss_family = AF_INET;
1304 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1305 }
1306 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001307 }
1308 }
1309 }
1310 return ACT_RET_CONT;
1311}
1312
Willy Tarreau2d392c22015-08-24 01:43:45 +02001313/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1314static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1315{
1316 struct connection *conn = objt_conn(sess->origin);
1317
1318 if (!conn)
1319 goto out;
1320
1321 if (!conn_ctrl_ready(conn))
1322 goto out;
1323
Willy Tarreau2d392c22015-08-24 01:43:45 +02001324#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001325 /* drain is needed only to send the quick ACK */
1326 conn_sock_drain(conn);
1327
Willy Tarreau2d392c22015-08-24 01:43:45 +02001328 /* re-enable quickack if it was disabled to ack all data and avoid
1329 * retransmits from the client that might trigger a real reset.
1330 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001331 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001332#endif
1333 /* lingering must absolutely be disabled so that we don't send a
1334 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1335 * is present, returning with ERR will cause lingering to be disabled.
1336 */
1337 if (strm)
1338 strm->si[0].flags |= SI_FL_NOLINGER;
1339
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001340 /* We're on the client-facing side, we must force to disable lingering to
1341 * ensure we will use an RST exclusively and kill any pending data.
1342 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001343 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001344
Willy Tarreau2d392c22015-08-24 01:43:45 +02001345#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001346 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001347 /* socket will be quiet now */
1348 goto out;
1349 }
1350#endif
1351 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1352 * Let's fall back on the TTL trick, though it only works for routed
1353 * network and has no effect on local net.
1354 */
1355#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001356 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001357#endif
1358 out:
1359 /* kill the stream if any */
1360 if (strm) {
1361 channel_abort(&strm->req);
1362 channel_abort(&strm->res);
1363 strm->req.analysers = 0;
1364 strm->res.analysers = 0;
1365 strm->be->be_counters.denied_req++;
1366 if (!(strm->flags & SF_ERR_MASK))
1367 strm->flags |= SF_ERR_PRXCOND;
1368 if (!(strm->flags & SF_FINST_MASK))
1369 strm->flags |= SF_FINST_R;
1370 }
1371
1372 sess->fe->fe_counters.denied_req++;
1373 if (sess->listener->counters)
1374 sess->listener->counters->denied_req++;
1375
1376 return ACT_RET_STOP;
1377}
1378
William Lallemand13e9b0c2016-05-25 02:34:07 +02001379/* parse "set-{src,dst}[-port]" action */
1380enum 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 +02001381{
1382 int cur_arg;
1383 struct sample_expr *expr;
1384 unsigned int where;
1385
1386 cur_arg = *orig_arg;
1387 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
1388 if (!expr)
1389 return ACT_RET_PRS_ERR;
1390
1391 where = 0;
1392 if (proxy->cap & PR_CAP_FE)
1393 where |= SMP_VAL_FE_HRQ_HDR;
1394 if (proxy->cap & PR_CAP_BE)
1395 where |= SMP_VAL_BE_HRQ_HDR;
1396
1397 if (!(expr->fetch->val & where)) {
1398 memprintf(err,
1399 "fetch method '%s' extracts information from '%s', none of which is available here",
1400 args[cur_arg-1], sample_src_names(expr->fetch->use));
1401 free(expr);
1402 return ACT_RET_PRS_ERR;
1403 }
1404 rule->arg.expr = expr;
1405 rule->action = ACT_CUSTOM;
1406
1407 if (!strcmp(args[*orig_arg-1], "set-src")) {
1408 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001409 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1410 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001411 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1412 rule->action_ptr = tcp_action_req_set_dst;
1413 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1414 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001415 } else {
1416 return ACT_RET_PRS_ERR;
1417 }
1418
1419 (*orig_arg)++;
1420
1421 return ACT_RET_PRS_OK;
1422}
1423
1424
Willy Tarreau2d392c22015-08-24 01:43:45 +02001425/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1426 * success, ACT_RET_PRS_ERR on error.
1427 */
1428static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1429 struct act_rule *rule, char **err)
1430{
1431 rule->action = ACT_CUSTOM;
1432 rule->action_ptr = tcp_exec_action_silent_drop;
1433 return ACT_RET_PRS_OK;
1434}
1435
Willy Tarreau645513a2010-05-24 20:55:15 +02001436
1437/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001438/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001439/************************************************************************/
1440
Willy Tarreau4a129812012-04-25 17:31:42 +02001441/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001442int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001443{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001444 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001445
1446 if (!cli_conn)
1447 return 0;
1448
1449 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001450 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001451 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001452 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001453 break;
1454 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001455 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001456 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001457 break;
1458 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001459 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001460 }
Emeric Brunf769f512010-10-22 17:14:01 +02001461
Willy Tarreau37406352012-04-23 16:16:37 +02001462 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001463 return 1;
1464}
1465
Willy Tarreaua5e37562011-12-16 17:06:15 +01001466/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001467static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001468smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001469{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001470 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001471
1472 if (!cli_conn)
1473 return 0;
1474
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001475 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001476 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001477 return 0;
1478
Willy Tarreau37406352012-04-23 16:16:37 +02001479 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001480 return 1;
1481}
1482
Willy Tarreau4a129812012-04-25 17:31:42 +02001483/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001484static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001485smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001486{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001487 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001488
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001489 if (!cli_conn)
1490 return 0;
1491
1492 conn_get_to_addr(cli_conn);
1493
1494 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001495 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001496 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001497 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001498 break;
1499 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001500 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001501 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001502 break;
1503 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001504 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001505 }
Emeric Brunf769f512010-10-22 17:14:01 +02001506
Willy Tarreau37406352012-04-23 16:16:37 +02001507 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001508 return 1;
1509}
1510
Willy Tarreau16e01562016-08-09 16:46:18 +02001511/* check if the destination address of the front connection is local to the
1512 * system or if it was intercepted.
1513 */
1514int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1515{
1516 struct connection *conn = objt_conn(smp->sess->origin);
1517 struct listener *li = smp->sess->listener;
1518
1519 if (!conn)
1520 return 0;
1521
1522 conn_get_to_addr(conn);
1523 if (!(conn->flags & CO_FL_ADDR_TO_SET))
1524 return 0;
1525
1526 smp->data.type = SMP_T_BOOL;
1527 smp->flags = 0;
1528 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.to);
1529 return smp->data.u.sint >= 0;
1530}
1531
1532/* check if the source address of the front connection is local to the system
1533 * or not.
1534 */
1535int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1536{
1537 struct connection *conn = objt_conn(smp->sess->origin);
1538 struct listener *li = smp->sess->listener;
1539
1540 if (!conn)
1541 return 0;
1542
1543 conn_get_from_addr(conn);
1544 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
1545 return 0;
1546
1547 smp->data.type = SMP_T_BOOL;
1548 smp->flags = 0;
1549 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.from);
1550 return smp->data.u.sint >= 0;
1551}
1552
Willy Tarreaua5e37562011-12-16 17:06:15 +01001553/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001554static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001555smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001556{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001557 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001558
1559 if (!cli_conn)
1560 return 0;
1561
1562 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001563
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001564 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001565 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001566 return 0;
1567
Willy Tarreau37406352012-04-23 16:16:37 +02001568 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001569 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001570}
1571
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001572#ifdef TCP_INFO
1573
1574/* Returns some tcp_info data is its avalaible. "dir" must be set to 0 if
1575 * the client connection is require, otherwise it is set to 1. "val" represents
1576 * the required value. Use 0 for rtt and 1 for rttavg. "unit" is the expected unit
1577 * by default, the rtt is in us. Id "unit" is set to 0, the unit is us, if it is
1578 * set to 1, the untis are milliseconds.
1579 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1580 */
1581static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1582 int dir, int val)
1583{
1584 struct connection *conn;
1585 struct tcp_info info;
1586 socklen_t optlen;
1587
1588 /* strm can be null. */
1589 if (!smp->strm)
1590 return 0;
1591
1592 /* get the object associated with the stream interface.The
1593 * object can be other thing than a connection. For example,
1594 * it be a appctx. */
1595 conn = objt_conn(smp->strm->si[dir].end);
1596 if (!conn)
1597 return 0;
1598
1599 /* The fd may not be avalaible for the tcp_info struct, and the
1600 syscal can fail. */
1601 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001602 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001603 return 0;
1604
1605 /* extract the value. */
1606 smp->data.type = SMP_T_SINT;
1607 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001608 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1609 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1610#if defined(__linux__)
1611 /* these ones are common to all Linux versions */
1612 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1613 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1614 case 4: smp->data.u.sint = info.tcpi_lost; break;
1615 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1616 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1617 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1618#elif defined(__FreeBSD__) || defined(__NetBSD__)
1619 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1620 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1621 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1622 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1623 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1624 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1625 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1626#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001627 default: return 0;
1628 }
1629
1630 /* Convert the value as expected. */
1631 if (args) {
1632 if (args[0].type == ARGT_STR) {
1633 if (strcmp(args[0].data.str.str, "us") == 0) {
1634 /* Do nothing. */
1635 } else if (strcmp(args[0].data.str.str, "ms") == 0) {
1636 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1637 } else
1638 return 0;
1639 } else if (args[0].type == ARGT_STOP) {
1640 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1641 } else
1642 return 0;
1643 }
1644
1645 return 1;
1646}
1647
1648/* get the mean rtt of a client connexion */
1649static int
1650smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1651{
1652 if (!get_tcp_info(args, smp, 0, 0))
1653 return 0;
1654 return 1;
1655}
1656
1657/* get the variance of the mean rtt of a client connexion */
1658static int
1659smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1660{
1661 if (!get_tcp_info(args, smp, 0, 1))
1662 return 0;
1663 return 1;
1664}
Joe Williams30fcd392016-08-10 07:06:44 -07001665
1666#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1667
1668/* get the unacked counter on a client connexion */
1669static int
1670smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1671{
1672 if (!get_tcp_info(args, smp, 0, 2))
1673 return 0;
1674 return 1;
1675}
1676
1677/* get the sacked counter on a client connexion */
1678static int
1679smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1680{
1681 if (!get_tcp_info(args, smp, 0, 3))
1682 return 0;
1683 return 1;
1684}
1685
1686/* get the lost counter on a client connexion */
1687static int
1688smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1689{
1690 if (!get_tcp_info(args, smp, 0, 4))
1691 return 0;
1692 return 1;
1693}
1694
1695/* get the retrans counter on a client connexion */
1696static int
1697smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1698{
1699 if (!get_tcp_info(args, smp, 0, 5))
1700 return 0;
1701 return 1;
1702}
1703
1704/* get the fackets counter on a client connexion */
1705static int
1706smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1707{
1708 if (!get_tcp_info(args, smp, 0, 6))
1709 return 0;
1710 return 1;
1711}
1712
1713/* get the reordering counter on a client connexion */
1714static int
1715smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1716{
1717 if (!get_tcp_info(args, smp, 0, 7))
1718 return 0;
1719 return 1;
1720}
1721#endif // linux || freebsd || netbsd
1722#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001723
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001724#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001725/* parse the "v4v6" bind keyword */
1726static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1727{
1728 struct listener *l;
1729
1730 list_for_each_entry(l, &conf->listeners, by_bind) {
1731 if (l->addr.ss_family == AF_INET6)
1732 l->options |= LI_O_V4V6;
1733 }
1734
1735 return 0;
1736}
1737
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001738/* parse the "v6only" bind keyword */
1739static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1740{
1741 struct listener *l;
1742
1743 list_for_each_entry(l, &conf->listeners, by_bind) {
1744 if (l->addr.ss_family == AF_INET6)
1745 l->options |= LI_O_V6ONLY;
1746 }
1747
1748 return 0;
1749}
1750#endif
1751
Pieter Baauwd551fb52013-05-08 22:49:23 +02001752#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001753/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001754static 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 +02001755{
1756 struct listener *l;
1757
Willy Tarreau4348fad2012-09-20 16:48:07 +02001758 list_for_each_entry(l, &conf->listeners, by_bind) {
1759 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1760 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001761 }
1762
Willy Tarreau44791242012-09-12 23:27:21 +02001763 return 0;
1764}
1765#endif
1766
1767#ifdef TCP_DEFER_ACCEPT
1768/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001769static 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 +02001770{
1771 struct listener *l;
1772
Willy Tarreau4348fad2012-09-20 16:48:07 +02001773 list_for_each_entry(l, &conf->listeners, by_bind) {
1774 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1775 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001776 }
1777
Willy Tarreau44791242012-09-12 23:27:21 +02001778 return 0;
1779}
1780#endif
1781
Willy Tarreau1c862c52012-10-05 16:21:00 +02001782#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001783/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001784static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1785{
1786 struct listener *l;
1787
1788 list_for_each_entry(l, &conf->listeners, by_bind) {
1789 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1790 l->options |= LI_O_TCP_FO;
1791 }
1792
1793 return 0;
1794}
1795#endif
1796
Willy Tarreau44791242012-09-12 23:27:21 +02001797#ifdef TCP_MAXSEG
1798/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001799static 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 +02001800{
1801 struct listener *l;
1802 int mss;
1803
Willy Tarreau44791242012-09-12 23:27:21 +02001804 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001805 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001806 return ERR_ALERT | ERR_FATAL;
1807 }
1808
1809 mss = atoi(args[cur_arg + 1]);
1810 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001811 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001812 return ERR_ALERT | ERR_FATAL;
1813 }
1814
Willy Tarreau4348fad2012-09-20 16:48:07 +02001815 list_for_each_entry(l, &conf->listeners, by_bind) {
1816 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1817 l->maxseg = mss;
1818 }
Willy Tarreau44791242012-09-12 23:27:21 +02001819
1820 return 0;
1821}
1822#endif
1823
Willy Tarreau2af207a2015-02-04 00:45:58 +01001824#ifdef TCP_USER_TIMEOUT
1825/* parse the "tcp-ut" bind keyword */
1826static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1827{
1828 const char *ptr = NULL;
1829 struct listener *l;
1830 unsigned int timeout;
1831
1832 if (!*args[cur_arg + 1]) {
1833 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1834 return ERR_ALERT | ERR_FATAL;
1835 }
1836
1837 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
1838 if (ptr) {
1839 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1840 return ERR_ALERT | ERR_FATAL;
1841 }
1842
1843 list_for_each_entry(l, &conf->listeners, by_bind) {
1844 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1845 l->tcp_ut = timeout;
1846 }
1847
1848 return 0;
1849}
1850#endif
1851
Willy Tarreau44791242012-09-12 23:27:21 +02001852#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001853/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001854static 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 +02001855{
1856 struct listener *l;
1857
Willy Tarreau44791242012-09-12 23:27:21 +02001858 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001859 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001860 return ERR_ALERT | ERR_FATAL;
1861 }
1862
Willy Tarreau4348fad2012-09-20 16:48:07 +02001863 list_for_each_entry(l, &conf->listeners, by_bind) {
1864 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1865 l->interface = strdup(args[cur_arg + 1]);
1866 }
Willy Tarreau44791242012-09-12 23:27:21 +02001867
Willy Tarreau44791242012-09-12 23:27:21 +02001868 return 0;
1869}
1870#endif
1871
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001872#ifdef CONFIG_HAP_NS
1873/* parse the "namespace" bind keyword */
1874static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1875{
1876 struct listener *l;
1877 char *namespace = NULL;
1878
1879 if (!*args[cur_arg + 1]) {
1880 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1881 return ERR_ALERT | ERR_FATAL;
1882 }
1883 namespace = args[cur_arg + 1];
1884
1885 list_for_each_entry(l, &conf->listeners, by_bind) {
1886 l->netns = netns_store_lookup(namespace, strlen(namespace));
1887
1888 if (l->netns == NULL)
1889 l->netns = netns_store_insert(namespace);
1890
1891 if (l->netns == NULL) {
1892 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1893 return ERR_ALERT | ERR_FATAL;
1894 }
1895 }
1896 return 0;
1897}
1898#endif
1899
Willy Tarreau163d4622015-10-13 16:16:41 +02001900#ifdef TCP_USER_TIMEOUT
1901/* parse the "tcp-ut" server keyword */
1902static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1903{
1904 const char *ptr = NULL;
1905 unsigned int timeout;
1906
1907 if (!*args[*cur_arg + 1]) {
1908 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1909 return ERR_ALERT | ERR_FATAL;
1910 }
1911
1912 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
1913 if (ptr) {
1914 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1915 return ERR_ALERT | ERR_FATAL;
1916 }
1917
1918 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1919 newsrv->tcp_ut = timeout;
1920
1921 return 0;
1922}
1923#endif
1924
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001925
Willy Tarreau4a129812012-04-25 17:31:42 +02001926/* Note: must not be declared <const> as its list will be overwritten.
1927 * Note: fetches that may return multiple types must be declared as the lowest
1928 * common denominator, the type that can be casted into all other ones. For
1929 * instance v4/v6 must be declared v4.
1930 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001931static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001932 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001933 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001934 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001935 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001936 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001937 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001938#ifdef TCP_INFO
Joe Williams30fcd392016-08-10 07:06:44 -07001939 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1940 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1941#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1942 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1943 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1944 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1945 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1946 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1947 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1948#endif // linux || freebsd || netbsd
1949#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001950 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001951}};
1952
Willy Tarreau44791242012-09-12 23:27:21 +02001953/************************************************************************/
1954/* All supported bind keywords must be declared here. */
1955/************************************************************************/
1956
1957/* Note: must not be declared <const> as its list will be overwritten.
1958 * Please take care of keeping this list alphabetically sorted, doing so helps
1959 * all code contributors.
1960 * Optional keywords are also declared with a NULL ->parse() function so that
1961 * the config parser can report an appropriate error when a known keyword was
1962 * not enabled.
1963 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001964static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001965#ifdef TCP_DEFER_ACCEPT
1966 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1967#endif
1968#ifdef SO_BINDTODEVICE
1969 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1970#endif
1971#ifdef TCP_MAXSEG
1972 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1973#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001974#ifdef TCP_USER_TIMEOUT
1975 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1976#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001977#ifdef TCP_FASTOPEN
1978 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1979#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001980#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001981 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1982#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001983#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001984 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001985 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1986#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001987#ifdef CONFIG_HAP_NS
1988 { "namespace", bind_parse_namespace, 1 },
1989#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001990 /* the versions with the NULL parse function*/
1991 { "defer-accept", NULL, 0 },
1992 { "interface", NULL, 1 },
1993 { "mss", NULL, 1 },
1994 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001995 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001996 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001997 { NULL, NULL, 0 },
1998}};
1999
Willy Tarreau163d4622015-10-13 16:16:41 +02002000static struct srv_kw_list srv_kws = { "TCP", { }, {
2001#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002002 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02002003#endif
2004 { NULL, NULL, 0 },
2005}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02002006
2007static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002008 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002009 { "set-src", tcp_parse_set_src_dst },
2010 { "set-src-port", tcp_parse_set_src_dst },
2011 { "set-dst" , tcp_parse_set_src_dst },
2012 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002013 { /* END */ }
2014}};
2015
Willy Tarreau620408f2016-10-21 16:37:51 +02002016static struct action_kw_list tcp_req_sess_actions = {ILH, {
2017 { "silent-drop", tcp_parse_silent_drop },
2018 { "set-src", tcp_parse_set_src_dst },
2019 { "set-src-port", tcp_parse_set_src_dst },
2020 { "set-dst" , tcp_parse_set_src_dst },
2021 { "set-dst-port", tcp_parse_set_src_dst },
2022 { /* END */ }
2023}};
2024
Willy Tarreau2d392c22015-08-24 01:43:45 +02002025static struct action_kw_list tcp_req_cont_actions = {ILH, {
2026 { "silent-drop", tcp_parse_silent_drop },
2027 { /* END */ }
2028}};
2029
2030static struct action_kw_list tcp_res_cont_actions = {ILH, {
2031 { "silent-drop", tcp_parse_silent_drop },
2032 { /* END */ }
2033}};
2034
2035static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002036 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002037 { "set-src", tcp_parse_set_src_dst },
2038 { "set-src-port", tcp_parse_set_src_dst },
2039 { "set-dst", tcp_parse_set_src_dst },
2040 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002041 { /* END */ }
2042}};
2043
2044static struct action_kw_list http_res_actions = {ILH, {
2045 { "silent-drop", tcp_parse_silent_drop },
2046 { /* END */ }
2047}};
2048
2049
Willy Tarreaue6b98942007-10-29 01:09:36 +01002050__attribute__((constructor))
2051static void __tcp_protocol_init(void)
2052{
2053 protocol_register(&proto_tcpv4);
2054 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002055 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau44791242012-09-12 23:27:21 +02002056 bind_register_keywords(&bind_kws);
Willy Tarreau163d4622015-10-13 16:16:41 +02002057 srv_register_keywords(&srv_kws);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002058 tcp_req_conn_keywords_register(&tcp_req_conn_actions);
Willy Tarreau620408f2016-10-21 16:37:51 +02002059 tcp_req_sess_keywords_register(&tcp_req_sess_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002060 tcp_req_cont_keywords_register(&tcp_req_cont_actions);
2061 tcp_res_cont_keywords_register(&tcp_res_cont_actions);
2062 http_req_keywords_register(&http_req_actions);
2063 http_res_keywords_register(&http_res_actions);
Willy Tarreauba962912016-12-21 18:55:02 +01002064
2065
2066 hap_register_build_opts("Built with transparent proxy support using:"
2067#if defined(IP_TRANSPARENT)
2068 " IP_TRANSPARENT"
2069#endif
2070#if defined(IPV6_TRANSPARENT)
2071 " IPV6_TRANSPARENT"
2072#endif
2073#if defined(IP_FREEBIND)
2074 " IP_FREEBIND"
2075#endif
2076#if defined(IP_BINDANY)
2077 " IP_BINDANY"
2078#endif
2079#if defined(IPV6_BINDANY)
2080 " IPV6_BINDANY"
2081#endif
2082#if defined(SO_BINDANY)
2083 " SO_BINDANY"
2084#endif
2085 "", 0);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002086}
2087
2088
2089/*
2090 * Local variables:
2091 * c-indent-level: 8
2092 * c-basic-offset: 8
2093 * End:
2094 */