blob: 9bc048b9c823a5d268201a3fdb02bff4186db84e [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 Tarreau0108d902018-11-25 19:14:37 +010039#include <common/initcall.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010040#include <common/mini-clist.h>
41#include <common/standard.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010042#include <common/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010043
Willy Tarreau39713102016-11-25 15:49:32 +010044#include <types/action.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020045#include <types/connection.h>
Willy Tarreau39713102016-11-25 15:49:32 +010046#include <types/global.h>
47#include <types/stream.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010048
Willy Tarreau9fcb9842012-04-20 14:45:49 +020049#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020050#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020051#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020052#include <proto/fd.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020053#include <proto/http_rules.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020054#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020055#include <proto/log.h>
56#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020057#include <proto/protocol.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020058#include <proto/proto_http.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010059#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020060#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020061#include <proto/sample.h>
Willy Tarreau163d4622015-10-13 16:16:41 +020062#include <proto/server.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020063#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010064#include <proto/tcp_rules.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010065
Emeric Bruncf20bf12010-10-22 16:06:11 +020066static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
67static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020068static void tcpv4_add_listener(struct listener *listener, int port);
69static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010070
71/* Note: must not be declared <const> as its list will be overwritten */
72static struct protocol proto_tcpv4 = {
73 .name = "tcpv4",
74 .sock_domain = AF_INET,
75 .sock_type = SOCK_STREAM,
76 .sock_prot = IPPROTO_TCP,
77 .sock_family = AF_INET,
78 .sock_addrlen = sizeof(struct sockaddr_in),
79 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020080 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020081 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020082 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010083 .bind_all = tcp_bind_listeners,
84 .unbind_all = unbind_all_listeners,
85 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020086 .get_src = tcp_get_src,
87 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020088 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020089 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010090 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
91 .nb_listeners = 0,
92};
93
Willy Tarreau0108d902018-11-25 19:14:37 +010094INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
95
Willy Tarreaue6b98942007-10-29 01:09:36 +010096/* Note: must not be declared <const> as its list will be overwritten */
97static struct protocol proto_tcpv6 = {
98 .name = "tcpv6",
99 .sock_domain = AF_INET6,
100 .sock_type = SOCK_STREAM,
101 .sock_prot = IPPROTO_TCP,
102 .sock_family = AF_INET6,
103 .sock_addrlen = sizeof(struct sockaddr_in6),
104 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200105 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200106 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200107 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100108 .bind_all = tcp_bind_listeners,
109 .unbind_all = unbind_all_listeners,
110 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200111 .get_src = tcp_get_src,
112 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +0200113 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200114 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100115 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
116 .nb_listeners = 0,
117};
118
Willy Tarreau0108d902018-11-25 19:14:37 +0100119INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
120
Olivier Houchard153659f2017-04-05 22:39:56 +0200121/* Default TCP parameters, got by opening a temporary TCP socket. */
122#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100123static THREAD_LOCAL int default_tcp_maxseg = -1;
124static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200125#endif
126
David du Colombier6f5ccb12011-03-10 22:26:24 +0100127/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100128 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
129 * - 0 : ignore remote address (may even be a NULL pointer)
130 * - 1 : use provided address
131 * - 2 : use provided port
132 * - 3 : use both
133 *
134 * The function supports multiple foreign binding methods :
135 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100136 * The second one can be used as a fallback for the first one.
137 * This function returns 0 when everything's OK, 1 if it could not bind, to the
138 * local address, 2 if it could not bind to the foreign address.
139 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100140int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100141{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100142 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100143 int foreign_ok = 0;
144 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100145 static THREAD_LOCAL int ip_transp_working = 1;
146 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200147
David du Colombier65c17962012-07-13 14:34:59 +0200148 switch (local->ss_family) {
149 case AF_INET:
150 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200151 /* This deserves some explanation. Some platforms will support
152 * multiple combinations of certain methods, so we try the
153 * supported ones until one succeeds.
154 */
155 if (0
156#if defined(IP_TRANSPARENT)
157 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
158#endif
159#if defined(IP_FREEBIND)
160 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
161#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200162#if defined(IP_BINDANY)
163 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
164#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200165#if defined(SO_BINDANY)
166 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
167#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200168 )
David du Colombier65c17962012-07-13 14:34:59 +0200169 foreign_ok = 1;
170 else
171 ip_transp_working = 0;
172 }
173 break;
174 case AF_INET6:
175 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200176 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200177#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200178 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
179#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100180#if defined(IP_FREEBIND)
181 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
182#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200183#if defined(IPV6_BINDANY)
184 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
185#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200186#if defined(SO_BINDANY)
187 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
188#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200189 )
David du Colombier65c17962012-07-13 14:34:59 +0200190 foreign_ok = 1;
191 else
192 ip6_transp_working = 0;
193 }
194 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100195 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200196
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100197 if (flags) {
198 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200199 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100200 switch (remote->ss_family) {
201 case AF_INET:
202 if (flags & 1)
203 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
204 if (flags & 2)
205 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
206 break;
207 case AF_INET6:
208 if (flags & 1)
209 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
210 if (flags & 2)
211 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
212 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100213 default:
214 /* we don't want to try to bind to an unknown address family */
215 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100216 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100217 }
218
Simon Hormande072bd2011-06-24 15:11:37 +0900219 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100220 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200221 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200222 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
223 if (ret < 0)
224 return 2;
225 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100226 }
227 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200228 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200229 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
230 if (ret < 0)
231 return 1;
232 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100233 }
234
235 if (!flags)
236 return 0;
237
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100238 if (!foreign_ok)
239 /* we could not bind to a foreign address */
240 return 2;
241
242 return 0;
243}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100244
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100245static int create_server_socket(struct connection *conn)
246{
Willy Tarreau529c1392014-12-24 13:47:55 +0100247 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100248
Willy Tarreau529c1392014-12-24 13:47:55 +0100249#ifdef CONFIG_HAP_NS
250 if (objt_server(conn->target)) {
251 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
252 ns = conn->proxy_netns;
253 else
254 ns = __objt_server(conn->target)->netns;
255 }
256#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100257 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
258}
Willy Tarreau9650f372009-08-16 14:02:45 +0200259
260/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200261 * This function initiates a TCP connection establishment to the target assigned
262 * to connection <conn> using (si->{target,addr.to}). A source address may be
263 * pointed to by conn->addr.from in case of transparent proxying. Normal source
264 * bind addresses are still determined locally (due to the possible need of a
265 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100266 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100267 * supported. The <data> parameter is a boolean indicating whether there are data
268 * waiting for being sent or not, in order to adjust data write polling and on
269 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
270 * allows the caller to force using a delayed ACK when establishing the connection :
271 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
272 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
273 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200274 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200275 * Note that a pending send_proxy message accounts for data.
276 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200277 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200278 * - SF_ERR_NONE if everything's OK
279 * - SF_ERR_SRVTO if there are no more servers
280 * - SF_ERR_SRVCL if the connection was refused by the server
281 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
282 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
283 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100284 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100285 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200286 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100287 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200288 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100289
Willy Tarreauf0837b22012-11-24 10:24:27 +0100290int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200291{
292 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100293 struct server *srv;
294 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100295 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100296
Olivier Houchard637b6952018-11-23 14:23:07 +0100297 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100298
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100299 switch (obj_type(conn->target)) {
300 case OBJ_TYPE_PROXY:
301 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100302 srv = NULL;
303 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100304 case OBJ_TYPE_SERVER:
305 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100306 be = srv->proxy;
307 break;
308 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100309 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200310 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100311 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200312
Willy Tarreau585744b2017-08-24 14:31:19 +0200313 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100314
315 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200316 qfprintf(stderr, "Cannot get a server socket.\n");
317
Willy Tarreau9ce70132014-01-24 16:08:19 +0100318 if (errno == ENFILE) {
319 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200320 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100321 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
322 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100323 }
324 else if (errno == EMFILE) {
325 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200326 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100327 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
328 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100329 }
330 else if (errno == ENOBUFS || errno == ENOMEM) {
331 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200332 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100333 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
334 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100335 }
336 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
337 conn->err_code = CO_ER_NOPROTO;
338 }
339 else
340 conn->err_code = CO_ER_SOCK_ERR;
341
Willy Tarreau9650f372009-08-16 14:02:45 +0200342 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100343 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200344 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200345 }
346
347 if (fd >= global.maxsock) {
348 /* do not log anything there, it's a normal condition when this option
349 * is used to serialize connections to a server !
350 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100351 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100353 conn->err_code = CO_ER_CONF_FDLIM;
354 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200355 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 }
357
358 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900359 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200360 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
361 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100362 conn->err_code = CO_ER_SOCK_ERR;
363 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200364 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200365 }
366
William Lallemandc03eb012018-11-27 12:02:37 +0100367 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
368 ha_alert("Cannot set CLOEXEC on client socket.\n");
369 close(fd);
370 conn->err_code = CO_ER_SOCK_ERR;
371 conn->flags |= CO_FL_ERROR;
372 return SF_ERR_INTERNAL;
373 }
374
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900376 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200377
Willy Tarreau9650f372009-08-16 14:02:45 +0200378 /* allow specific binding :
379 * - server-specific at first
380 * - proxy-specific next
381 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100382 if (srv && srv->conn_src.opts & CO_SRC_BIND)
383 src = &srv->conn_src;
384 else if (be->conn_src.opts & CO_SRC_BIND)
385 src = &be->conn_src;
386 else
387 src = NULL;
388
389 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200390 int ret, flags = 0;
391
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200392 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100393 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100394 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200395 conn->flags |= CO_FL_PRIVATE;
396 /* fall through */
397 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200398 flags = 3;
399 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100400 case CO_SRC_TPROXY_CIP:
401 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200402 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200403 flags = 1;
404 break;
405 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200406 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200407
Willy Tarreau9650f372009-08-16 14:02:45 +0200408#ifdef SO_BINDTODEVICE
409 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100410 if (src->iface_name)
411 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200412#endif
413
Willy Tarreaua4380b42012-12-08 22:49:11 +0100414 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200415 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100416 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200417
418 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200419 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200420
421 do {
422 /* note: in case of retry, we may have to release a previously
423 * allocated port, hence this loop's construct.
424 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200425 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
426 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200427
428 if (!attempts)
429 break;
430 attempts--;
431
Willy Tarreaua4380b42012-12-08 22:49:11 +0100432 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100433 if (!fdinfo[fd].local_port) {
434 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200435 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100436 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200437
Willy Tarreaua4380b42012-12-08 22:49:11 +0100438 fdinfo[fd].port_range = src->sport_range;
439 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200440
Willy Tarreaua4380b42012-12-08 22:49:11 +0100441 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100442 if (ret != 0)
443 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200444 } while (ret != 0); /* binding NOK */
445 }
446 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000447#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100448 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000449 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
450#endif
Willy Tarreaua4380b42012-12-08 22:49:11 +0100451 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100452 if (ret != 0)
453 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 }
455
Willy Tarreaua4380b42012-12-08 22:49:11 +0100456 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200457 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
458 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200459 close(fd);
460
Willy Tarreau9650f372009-08-16 14:02:45 +0200461 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100462 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
463 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200464 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100465 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200466 be->id);
467 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100468 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
469 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200470 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100471 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200472 be->id);
473 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100474 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200475 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200476 }
477 }
478
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400479#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200480 /* disabling tcp quick ack now allows the first request to leave the
481 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100482 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200483 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200484 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900485 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200486#endif
487
Willy Tarreau163d4622015-10-13 16:16:41 +0200488#ifdef TCP_USER_TIMEOUT
489 /* there is not much more we can do here when it fails, it's still minor */
490 if (srv && srv->tcp_ut)
491 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
492#endif
Willy Tarreaue803de22010-01-21 17:43:04 +0100493 if (global.tune.server_sndbuf)
494 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
495
496 if (global.tune.server_rcvbuf)
497 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
498
Willy Tarreau819efbf2017-01-25 14:12:22 +0100499 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
500 if (errno == EINPROGRESS || errno == EALREADY) {
501 /* common case, let's wait for connect status */
502 conn->flags |= CO_FL_WAIT_L4_CONN;
503 }
504 else if (errno == EISCONN) {
505 /* should normally not happen but if so, indicates that it's OK */
506 conn->flags &= ~CO_FL_WAIT_L4_CONN;
507 }
508 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200509 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100510 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200511 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100512 conn->err_code = CO_ER_FREE_PORTS;
513 }
514 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200515 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100516 conn->err_code = CO_ER_ADDR_INUSE;
517 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200518
Willy Tarreaub1719512012-12-08 23:03:28 +0100519 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200520 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
521 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200522 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100523 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100524 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200525 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200526 } else if (errno == ETIMEDOUT) {
527 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200528 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
529 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200530 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100531 conn->err_code = CO_ER_SOCK_ERR;
532 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200533 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200534 } else {
535 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
536 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200537 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
538 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200539 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100540 conn->err_code = CO_ER_SOCK_ERR;
541 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200542 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200543 }
544 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100545 else {
546 /* connect() == 0, this is great! */
547 conn->flags &= ~CO_FL_WAIT_L4_CONN;
548 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200549
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100550 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200551
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200552 /* Prepare to send a few handshakes related to the on-wire protocol. */
553 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200554 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200555
Willy Tarreauf79c8172013-10-21 16:30:56 +0200556 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100557 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200558
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200559 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200560 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100561 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200562 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200563 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200564
Olivier Houchardc2aae742017-09-22 18:26:28 +0200565 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN | CO_FL_EARLY_SSL_HS)) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100566 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
Olivier Houchardc2aae742017-09-22 18:26:28 +0200567 if (conn->flags & CO_FL_EARLY_SSL_HS)
568 conn_xprt_want_send(conn);
Willy Tarreau819efbf2017-01-25 14:12:22 +0100569 }
570 else {
571 /* If there's no more handshake, we need to notify the data
572 * layer when the connection is already OK otherwise we'll have
573 * no other opportunity to do it later (eg: health checks).
574 */
575 data = 1;
576 }
577
Willy Tarreau14f8e862012-08-30 22:23:13 +0200578 if (data)
Olivier Houchard1a0545f2017-09-13 18:30:23 +0200579 conn_xprt_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200580
Willy Tarreaue7dff022015-04-03 01:14:29 +0200581 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200582}
583
584
Willy Tarreau59b94792012-05-11 16:16:40 +0200585/*
586 * Retrieves the source address for the socket <fd>, with <dir> indicating
587 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
588 * success, -1 in case of error. The socket's source address is stored in
589 * <sa> for <salen> bytes.
590 */
591int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
592{
593 if (dir)
594 return getsockname(fd, sa, &salen);
595 else
596 return getpeername(fd, sa, &salen);
597}
598
599
600/*
601 * Retrieves the original destination address for the socket <fd>, with <dir>
602 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
603 * listener, if the original destination address was translated, the original
604 * address is retrieved. It returns 0 in case of success, -1 in case of error.
605 * The socket's source address is stored in <sa> for <salen> bytes.
606 */
607int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
608{
609 if (dir)
610 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100611 else {
612 int ret = getsockname(fd, sa, &salen);
613
614 if (ret < 0)
615 return ret;
616
Willy Tarreau59b94792012-05-11 16:16:40 +0200617#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100618 /* For TPROXY and Netfilter's NAT, we can retrieve the original
619 * IPv4 address before DNAT/REDIRECT. We must not do that with
620 * other families because v6-mapped IPv4 addresses are still
621 * reported as v4.
622 */
623 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
624 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
625 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200626#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100627 return ret;
628 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200629}
630
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200631/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100632 * and we have nothing to send. It updates the FD polling status. It returns 0
633 * if it fails in a fatal way or needs to poll to go further, otherwise it
634 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
635 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
636 * errno. The error checking is done in two passes in order to limit the number
637 * of syscalls in the normal case :
638 * - if POLL_ERR was reported by the poller, we check for a pending error on
639 * the socket before proceeding. If found, it's assigned to errno so that
640 * upper layers can see it.
641 * - otherwise connect() is used to check the connection state again, since
642 * the getsockopt return cannot reliably be used to know if the connection
643 * is still pending or ready. This one may often return an error as well,
644 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200645 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200646int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200647{
Willy Tarreau585744b2017-08-24 14:31:19 +0200648 int fd = conn->handle.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100649 socklen_t lskerr;
650 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200651
Willy Tarreau80184712012-07-06 14:54:49 +0200652 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200653 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200654
Willy Tarreau3c728722014-01-23 13:50:42 +0100655 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200656 return 0;
657
Willy Tarreau80184712012-07-06 14:54:49 +0200658 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200659 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200660
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100661 if (!fd_send_ready(fd))
662 return 0;
663
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100664 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
665 * without FD_POLL_IN also indicates a hangup without input data meaning
666 * there was no connection.
667 */
668 if (fdtab[fd].ev & FD_POLL_ERR ||
669 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
670 skerr = 0;
671 lskerr = sizeof(skerr);
672 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
673 errno = skerr;
674 if (errno == EAGAIN)
675 errno = 0;
676 if (errno)
677 goto out_error;
678 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200679
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100680 /* Use connect() to check the state of the socket. This has the
681 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200682 * - error
683 * - connecting (EALREADY, EINPROGRESS)
684 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200685 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200686 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200687 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100688 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100689 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200690 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200691 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200692
Willy Tarreau2c6be842012-07-06 17:12:34 +0200693 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200694 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200695
Willy Tarreau2c6be842012-07-06 17:12:34 +0200696 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200697 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200698
Willy Tarreau076be252012-07-06 16:02:29 +0200699 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200700 * forward the event to the transport layer which will notify the
701 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200702 */
Willy Tarreau80184712012-07-06 14:54:49 +0200703 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200704 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200705
706 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200707 /* Write error on the file descriptor. Report it to the connection
708 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200709 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100710 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100711 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100712 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200713 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200714}
715
Olivier Houchard153659f2017-04-05 22:39:56 +0200716/* XXX: Should probably be elsewhere */
717static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
718{
719 if (a->ss_family != b->ss_family) {
720 return (-1);
721 }
722 switch (a->ss_family) {
723 case AF_INET:
724 {
725 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
726 if (a4->sin_port != b4->sin_port)
727 return (-1);
728 return (memcmp(&a4->sin_addr, &b4->sin_addr,
729 sizeof(a4->sin_addr)));
730 }
731 case AF_INET6:
732 {
733 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
734 if (a6->sin6_port != b6->sin6_port)
735 return (-1);
736 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
737 sizeof(a6->sin6_addr)));
738 }
739 default:
740 return (-1);
741 }
742
743}
744
745#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
746/* When binding the listeners, check if a socket has been sent to us by the
747 * previous process that we could reuse, instead of creating a new one.
748 */
749static int tcp_find_compatible_fd(struct listener *l)
750{
751 struct xfer_sock_list *xfer_sock = xfer_sock_list;
752 int ret = -1;
753
754 while (xfer_sock) {
755 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
756 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
757 (l->interface != NULL && xfer_sock->iface != NULL &&
758 !strcmp(l->interface, xfer_sock->iface))) {
759 if ((l->options & LI_MANDATORY_FLAGS) ==
760 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
761 if ((xfer_sock->namespace == NULL &&
762 l->netns == NULL)
763#ifdef CONFIG_HAP_NS
764 || (xfer_sock->namespace != NULL &&
765 l->netns != NULL &&
766 !strcmp(xfer_sock->namespace,
767 l->netns->node.key))
768#endif
769 ) {
770 break;
771 }
772
773 }
774 }
775 }
776 xfer_sock = xfer_sock->next;
777 }
778 if (xfer_sock != NULL) {
779 ret = xfer_sock->fd;
780 if (xfer_sock == xfer_sock_list)
781 xfer_sock_list = xfer_sock->next;
782 if (xfer_sock->prev)
783 xfer_sock->prev->next = xfer_sock->next;
784 if (xfer_sock->next)
785 xfer_sock->next->prev = xfer_sock->prev;
786 free(xfer_sock->iface);
787 free(xfer_sock->namespace);
788 free(xfer_sock);
789 }
790 return ret;
791}
792#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200793
Willy Tarreaue6b98942007-10-29 01:09:36 +0100794/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100795 * an error message in <errmsg> if the message is at most <errlen> bytes long
796 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
797 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100798 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
799 * was alright and that no message was returned. ERR_RETRYABLE means that an
800 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700801 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100802 * the meaning of the error, but just indicate that a message is present which
803 * should be displayed with the respective level. Last, ERR_ABORT indicates
804 * that it's pointless to try to start other listeners. No error message is
805 * returned if errlen is NULL.
806 */
807int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
808{
809 __label__ tcp_return, tcp_close_return;
810 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100811 int ext, ready;
812 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100813 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200814#ifdef TCP_MAXSEG
815
816 /* Create a temporary TCP socket to get default parameters we can't
817 * guess.
818 * */
819 ready_len = sizeof(default_tcp_maxseg);
820 if (default_tcp_maxseg == -1) {
821 default_tcp_maxseg = -2;
822 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
823 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100824 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200825 else {
826 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
827 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100828 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200829 }
830 close(fd);
831 }
832 if (default_tcp6_maxseg == -1) {
833 default_tcp6_maxseg = -2;
834 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
835 if (fd >= 0) {
836 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
837 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100838 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200839 close(fd);
840 }
841 }
842#endif
843
Willy Tarreaue6b98942007-10-29 01:09:36 +0100844
845 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100846 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100847 *errmsg = 0;
848
849 if (listener->state != LI_ASSIGNED)
850 return ERR_NONE; /* already bound */
851
852 err = ERR_NONE;
853
Olivier Houchard153659f2017-04-05 22:39:56 +0200854 if (listener->fd == -1)
855 listener->fd = tcp_find_compatible_fd(listener);
856
Willy Tarreau40aa0702013-03-10 23:51:38 +0100857 /* if the listener already has an fd assigned, then we were offered the
858 * fd by an external process (most likely the parent), and we don't want
859 * to create a new socket. However we still want to set a few flags on
860 * the socket.
861 */
862 fd = listener->fd;
863 ext = (fd >= 0);
864
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100865 if (!ext) {
866 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
867
868 if (fd == -1) {
869 err |= ERR_RETRYABLE | ERR_ALERT;
870 msg = "cannot create listening socket";
871 goto tcp_return;
872 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100873 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100874
Willy Tarreaue6b98942007-10-29 01:09:36 +0100875 if (fd >= global.maxsock) {
876 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
877 msg = "not enough free sockets (raise '-n' parameter)";
878 goto tcp_close_return;
879 }
880
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200881 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100882 err |= ERR_FATAL | ERR_ALERT;
883 msg = "cannot make socket non-blocking";
884 goto tcp_close_return;
885 }
886
Willy Tarreau40aa0702013-03-10 23:51:38 +0100887 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100888 /* not fatal but should be reported */
889 msg = "cannot do so_reuseaddr";
890 err |= ERR_ALERT;
891 }
892
893 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900894 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200895 else {
896 struct linger tmplinger;
897 socklen_t len = sizeof(tmplinger);
898 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
899 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
900 tmplinger.l_onoff = 0;
901 tmplinger.l_linger = 0;
902 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
903 sizeof(tmplinger));
904 }
905 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100906
Willy Tarreaue6b98942007-10-29 01:09:36 +0100907#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000908 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
909 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100910 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000911 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100912 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100913#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200914
Willy Tarreau40aa0702013-03-10 23:51:38 +0100915 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200916 switch (listener->addr.ss_family) {
917 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200918 if (1
919#if defined(IP_TRANSPARENT)
920 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
921#endif
922#if defined(IP_FREEBIND)
923 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
924#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200925#if defined(IP_BINDANY)
926 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
927#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200928#if defined(SO_BINDANY)
929 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
930#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200931 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200932 msg = "cannot make listening socket transparent";
933 err |= ERR_ALERT;
934 }
935 break;
936 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200937 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200938#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200939 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
940#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100941#if defined(IP_FREEBIND)
942 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
943#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200944#if defined(IPV6_BINDANY)
945 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
946#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200947#if defined(SO_BINDANY)
948 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
949#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200950 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200951 msg = "cannot make listening socket transparent";
952 err |= ERR_ALERT;
953 }
954 break;
955 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100956 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200957
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100958#ifdef SO_BINDTODEVICE
959 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100960 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100961 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100962 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100963 msg = "cannot bind listener to device";
964 err |= ERR_WARN;
965 }
966 }
967#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400968#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100969 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400970 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200971 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
972 msg = "cannot set MSS";
973 err |= ERR_WARN;
974 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200975 } else if (ext) {
976 int tmpmaxseg = -1;
977 int defaultmss;
978 socklen_t len = sizeof(tmpmaxseg);
979
980 if (listener->addr.ss_family == AF_INET)
981 defaultmss = default_tcp_maxseg;
982 else
983 defaultmss = default_tcp6_maxseg;
984
985 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
986 if (tmpmaxseg != defaultmss && setsockopt(fd, IPPROTO_TCP,
987 TCP_MAXSEG, &defaultmss,
988 sizeof(defaultmss)) == -1) {
989 msg = "cannot set MSS";
990 err |= ERR_WARN;
991 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200992 }
993#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100994#if defined(TCP_USER_TIMEOUT)
995 if (listener->tcp_ut) {
996 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
997 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
998 msg = "cannot set TCP User Timeout";
999 err |= ERR_WARN;
1000 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001001 } else
1002 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
1003 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +01001004#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001005#if defined(TCP_DEFER_ACCEPT)
1006 if (listener->options & LI_O_DEF_ACCEPT) {
1007 /* defer accept by up to one second */
1008 int accept_delay = 1;
1009 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
1010 msg = "cannot enable DEFER_ACCEPT";
1011 err |= ERR_WARN;
1012 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001013 } else
1014 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
1015 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001016#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001017#if defined(TCP_FASTOPEN)
1018 if (listener->options & LI_O_TCP_FO) {
1019 /* TFO needs a queue length, let's use the configured backlog */
1020 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
1021 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
1022 msg = "cannot enable TCP_FASTOPEN";
1023 err |= ERR_WARN;
1024 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001025 } else {
1026 socklen_t len;
1027 int qlen;
1028 len = sizeof(qlen);
1029 /* Only disable fast open if it was enabled, we don't want
1030 * the kernel to create a fast open queue if there's none.
1031 */
1032 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
1033 qlen != 0) {
1034 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
1035 sizeof(zero)) == -1) {
1036 msg = "cannot disable TCP_FASTOPEN";
1037 err |= ERR_WARN;
1038 }
1039 }
Willy Tarreau1c862c52012-10-05 16:21:00 +02001040 }
1041#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001042#if defined(IPV6_V6ONLY)
1043 if (listener->options & LI_O_V6ONLY)
1044 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +01001045 else if (listener->options & LI_O_V4V6)
1046 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001047#endif
1048
Willy Tarreau40aa0702013-03-10 23:51:38 +01001049 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001050 err |= ERR_RETRYABLE | ERR_ALERT;
1051 msg = "cannot bind socket";
1052 goto tcp_close_return;
1053 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001054
Willy Tarreau40aa0702013-03-10 23:51:38 +01001055 ready = 0;
1056 ready_len = sizeof(ready);
1057 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
1058 ready = 0;
1059
1060 if (!(ext && ready) && /* only listen if not already done by external process */
1061 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001062 err |= ERR_RETRYABLE | ERR_ALERT;
1063 msg = "cannot listen to socket";
1064 goto tcp_close_return;
1065 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001066
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001067#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001068 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +09001069 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +02001070 else
1071 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001072#endif
1073
Willy Tarreaue6b98942007-10-29 01:09:36 +01001074 /* the socket is ready */
1075 listener->fd = fd;
1076 listener->state = LI_LISTEN;
1077
Willy Tarreaua9786b62018-01-25 07:22:13 +01001078 fd_insert(fd, listener, listener->proto->accept,
1079 listener->bind_conf->bind_thread[relative_pid-1] ?
1080 listener->bind_conf->bind_thread[relative_pid-1] : MAX_THREADS_MASK);
Willy Tarreaueb472682010-05-28 18:46:57 +02001081
Willy Tarreaue6b98942007-10-29 01:09:36 +01001082 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001083 if (msg && errlen) {
1084 char pn[INET6_ADDRSTRLEN];
1085
Willy Tarreau631f01c2011-09-05 00:36:48 +02001086 addr_to_str(&listener->addr, pn, sizeof(pn));
1087 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001088 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001089 return err;
1090
1091 tcp_close_return:
1092 close(fd);
1093 goto tcp_return;
1094}
1095
1096/* This function creates all TCP sockets bound to the protocol entry <proto>.
1097 * It is intended to be used as the protocol's bind_all() function.
1098 * The sockets will be registered but not added to any fd_set, in order not to
1099 * loose them across the fork(). A call to enable_all_listeners() is needed
1100 * to complete initialization. The return value is composed from ERR_*.
1101 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001102static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001103{
1104 struct listener *listener;
1105 int err = ERR_NONE;
1106
1107 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001108 err |= tcp_bind_listener(listener, errmsg, errlen);
1109 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001110 break;
1111 }
1112
1113 return err;
1114}
1115
Willy Tarreau32282382017-09-15 07:44:44 +02001116/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1117 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1118 * The number of listeners for the protocol is updated.
Willy Tarreaue6b98942007-10-29 01:09:36 +01001119 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001120static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001121{
1122 if (listener->state != LI_INIT)
1123 return;
1124 listener->state = LI_ASSIGNED;
1125 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001126 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001127 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1128 proto_tcpv4.nb_listeners++;
1129}
1130
Willy Tarreau32282382017-09-15 07:44:44 +02001131/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1132 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1133 * The number of listeners for the protocol is updated.
Willy Tarreaue6b98942007-10-29 01:09:36 +01001134 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001135static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001136{
1137 if (listener->state != LI_INIT)
1138 return;
1139 listener->state = LI_ASSIGNED;
1140 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001141 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001142 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1143 proto_tcpv6.nb_listeners++;
1144}
1145
Willy Tarreau092d8652014-07-07 20:22:12 +02001146/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1147 * was totally stopped, or > 0 if correctly paused.
1148 */
1149int tcp_pause_listener(struct listener *l)
1150{
1151 if (shutdown(l->fd, SHUT_WR) != 0)
1152 return -1; /* Solaris dies here */
1153
1154 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1155 return -1; /* OpenBSD dies here */
1156
1157 if (shutdown(l->fd, SHUT_RD) != 0)
1158 return -1; /* should always be OK */
1159 return 1;
1160}
1161
William Lallemand2e785f22016-05-25 01:48:42 +02001162/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001163 * Execute the "set-src" action. May be called from {tcp,http}request.
1164 * It only changes the address and tries to preserve the original port. If the
1165 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001166 */
1167enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1168 struct session *sess, struct stream *s, int flags)
1169{
1170 struct connection *cli_conn;
1171
1172 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1173 struct sample *smp;
1174
1175 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1176 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001177 int port = get_net_port(&cli_conn->addr.from);
1178
William Lallemand2e785f22016-05-25 01:48:42 +02001179 if (smp->data.type == SMP_T_IPV4) {
1180 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
1181 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001182 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001183 } else if (smp->data.type == SMP_T_IPV6) {
1184 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
1185 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 +02001186 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001187 }
1188 }
William Lallemand01252ed2016-05-25 02:33:16 +02001189 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001190 }
1191 return ACT_RET_CONT;
1192}
1193
William Lallemand44be6402016-05-25 01:51:35 +02001194/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001195 * Execute the "set-dst" action. May be called from {tcp,http}request.
1196 * It only changes the address and tries to preserve the original port. If the
1197 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001198 */
1199enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1200 struct session *sess, struct stream *s, int flags)
1201{
1202 struct connection *cli_conn;
1203
1204 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1205 struct sample *smp;
1206
1207 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1208 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001209 int port = get_net_port(&cli_conn->addr.to);
1210
William Lallemand13e9b0c2016-05-25 02:34:07 +02001211 if (smp->data.type == SMP_T_IPV4) {
1212 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_family = AF_INET;
1213 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1214 } else if (smp->data.type == SMP_T_IPV6) {
1215 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_family = AF_INET6;
1216 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 +02001217 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001218 }
1219 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1220 }
1221 }
1222 return ACT_RET_CONT;
1223}
1224
1225/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001226 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1227 * We must test the sin_family before setting the port. If the address family
1228 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1229 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001230 */
1231enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1232 struct session *sess, struct stream *s, int flags)
1233{
1234 struct connection *cli_conn;
1235
1236 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1237 struct sample *smp;
1238
1239 conn_get_from_addr(cli_conn);
1240
1241 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1242 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001243 if (cli_conn->addr.from.ss_family == AF_INET6) {
William Lallemand44be6402016-05-25 01:51:35 +02001244 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001245 } else {
1246 if (cli_conn->addr.from.ss_family != AF_INET) {
1247 cli_conn->addr.from.ss_family = AF_INET;
1248 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1249 }
1250 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001251 }
1252 }
1253 }
1254 return ACT_RET_CONT;
1255}
1256
William Lallemand13e9b0c2016-05-25 02:34:07 +02001257/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001258 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1259 * We must test the sin_family before setting the port. If the address family
1260 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1261 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001262 */
1263enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1264 struct session *sess, struct stream *s, int flags)
1265{
1266 struct connection *cli_conn;
1267
1268 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1269 struct sample *smp;
1270
1271 conn_get_to_addr(cli_conn);
1272
1273 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1274 if (smp) {
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001275 if (cli_conn->addr.to.ss_family == AF_INET6) {
1276 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001277 } else {
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001278 if (cli_conn->addr.to.ss_family != AF_INET) {
1279 cli_conn->addr.to.ss_family = AF_INET;
1280 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001281 }
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001282 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001283 }
1284 }
1285 }
1286 return ACT_RET_CONT;
1287}
1288
Willy Tarreau2d392c22015-08-24 01:43:45 +02001289/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1290static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1291{
1292 struct connection *conn = objt_conn(sess->origin);
1293
1294 if (!conn)
1295 goto out;
1296
1297 if (!conn_ctrl_ready(conn))
1298 goto out;
1299
Willy Tarreau2d392c22015-08-24 01:43:45 +02001300#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001301 /* drain is needed only to send the quick ACK */
1302 conn_sock_drain(conn);
1303
Willy Tarreau2d392c22015-08-24 01:43:45 +02001304 /* re-enable quickack if it was disabled to ack all data and avoid
1305 * retransmits from the client that might trigger a real reset.
1306 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001307 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001308#endif
1309 /* lingering must absolutely be disabled so that we don't send a
1310 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1311 * is present, returning with ERR will cause lingering to be disabled.
1312 */
1313 if (strm)
1314 strm->si[0].flags |= SI_FL_NOLINGER;
1315
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001316 /* We're on the client-facing side, we must force to disable lingering to
1317 * ensure we will use an RST exclusively and kill any pending data.
1318 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001319 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001320
Willy Tarreau2d392c22015-08-24 01:43:45 +02001321#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001322 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001323 /* socket will be quiet now */
1324 goto out;
1325 }
1326#endif
1327 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1328 * Let's fall back on the TTL trick, though it only works for routed
1329 * network and has no effect on local net.
1330 */
1331#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001332 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001333#endif
1334 out:
1335 /* kill the stream if any */
1336 if (strm) {
1337 channel_abort(&strm->req);
1338 channel_abort(&strm->res);
1339 strm->req.analysers = 0;
1340 strm->res.analysers = 0;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001341 HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001342 if (!(strm->flags & SF_ERR_MASK))
1343 strm->flags |= SF_ERR_PRXCOND;
1344 if (!(strm->flags & SF_FINST_MASK))
1345 strm->flags |= SF_FINST_R;
1346 }
1347
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001348 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001349 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001350 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001351
1352 return ACT_RET_STOP;
1353}
1354
William Lallemand13e9b0c2016-05-25 02:34:07 +02001355/* parse "set-{src,dst}[-port]" action */
1356enum 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 +02001357{
1358 int cur_arg;
1359 struct sample_expr *expr;
1360 unsigned int where;
1361
1362 cur_arg = *orig_arg;
1363 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
1364 if (!expr)
1365 return ACT_RET_PRS_ERR;
1366
1367 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001368 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001369 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001370 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001371 where |= SMP_VAL_BE_HRQ_HDR;
1372
1373 if (!(expr->fetch->val & where)) {
1374 memprintf(err,
1375 "fetch method '%s' extracts information from '%s', none of which is available here",
1376 args[cur_arg-1], sample_src_names(expr->fetch->use));
1377 free(expr);
1378 return ACT_RET_PRS_ERR;
1379 }
1380 rule->arg.expr = expr;
1381 rule->action = ACT_CUSTOM;
1382
1383 if (!strcmp(args[*orig_arg-1], "set-src")) {
1384 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001385 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1386 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001387 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1388 rule->action_ptr = tcp_action_req_set_dst;
1389 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1390 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001391 } else {
1392 return ACT_RET_PRS_ERR;
1393 }
1394
1395 (*orig_arg)++;
1396
1397 return ACT_RET_PRS_OK;
1398}
1399
1400
Willy Tarreau2d392c22015-08-24 01:43:45 +02001401/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1402 * success, ACT_RET_PRS_ERR on error.
1403 */
1404static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1405 struct act_rule *rule, char **err)
1406{
1407 rule->action = ACT_CUSTOM;
1408 rule->action_ptr = tcp_exec_action_silent_drop;
1409 return ACT_RET_PRS_OK;
1410}
1411
Willy Tarreau645513a2010-05-24 20:55:15 +02001412
1413/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001414/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001415/************************************************************************/
1416
Willy Tarreau4a129812012-04-25 17:31:42 +02001417/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001418int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001419{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001420 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001421
1422 if (!cli_conn)
1423 return 0;
1424
1425 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001426 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001427 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001428 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001429 break;
1430 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001431 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001432 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001433 break;
1434 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001435 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001436 }
Emeric Brunf769f512010-10-22 17:14:01 +02001437
Willy Tarreau37406352012-04-23 16:16:37 +02001438 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001439 return 1;
1440}
1441
Willy Tarreaua5e37562011-12-16 17:06:15 +01001442/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001443static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001444smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001445{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001446 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001447
1448 if (!cli_conn)
1449 return 0;
1450
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001451 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001452 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001453 return 0;
1454
Willy Tarreau37406352012-04-23 16:16:37 +02001455 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001456 return 1;
1457}
1458
Willy Tarreau4a129812012-04-25 17:31:42 +02001459/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001460static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001461smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001462{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001463 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001464
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001465 if (!cli_conn)
1466 return 0;
1467
1468 conn_get_to_addr(cli_conn);
1469
1470 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001471 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001472 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001473 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001474 break;
1475 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001476 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001477 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001478 break;
1479 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001480 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001481 }
Emeric Brunf769f512010-10-22 17:14:01 +02001482
Willy Tarreau37406352012-04-23 16:16:37 +02001483 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001484 return 1;
1485}
1486
Willy Tarreau16e01562016-08-09 16:46:18 +02001487/* check if the destination address of the front connection is local to the
1488 * system or if it was intercepted.
1489 */
1490int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1491{
1492 struct connection *conn = objt_conn(smp->sess->origin);
1493 struct listener *li = smp->sess->listener;
1494
1495 if (!conn)
1496 return 0;
1497
1498 conn_get_to_addr(conn);
1499 if (!(conn->flags & CO_FL_ADDR_TO_SET))
1500 return 0;
1501
1502 smp->data.type = SMP_T_BOOL;
1503 smp->flags = 0;
1504 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.to);
1505 return smp->data.u.sint >= 0;
1506}
1507
1508/* check if the source address of the front connection is local to the system
1509 * or not.
1510 */
1511int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1512{
1513 struct connection *conn = objt_conn(smp->sess->origin);
1514 struct listener *li = smp->sess->listener;
1515
1516 if (!conn)
1517 return 0;
1518
1519 conn_get_from_addr(conn);
1520 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
1521 return 0;
1522
1523 smp->data.type = SMP_T_BOOL;
1524 smp->flags = 0;
1525 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.from);
1526 return smp->data.u.sint >= 0;
1527}
1528
Willy Tarreaua5e37562011-12-16 17:06:15 +01001529/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001530static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001531smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001532{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001533 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001534
1535 if (!cli_conn)
1536 return 0;
1537
1538 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001539
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001540 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001541 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001542 return 0;
1543
Willy Tarreau37406352012-04-23 16:16:37 +02001544 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001545 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001546}
1547
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001548#ifdef TCP_INFO
1549
1550/* Returns some tcp_info data is its avalaible. "dir" must be set to 0 if
1551 * the client connection is require, otherwise it is set to 1. "val" represents
1552 * the required value. Use 0 for rtt and 1 for rttavg. "unit" is the expected unit
1553 * by default, the rtt is in us. Id "unit" is set to 0, the unit is us, if it is
1554 * set to 1, the untis are milliseconds.
1555 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1556 */
1557static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1558 int dir, int val)
1559{
1560 struct connection *conn;
1561 struct tcp_info info;
1562 socklen_t optlen;
1563
1564 /* strm can be null. */
1565 if (!smp->strm)
1566 return 0;
1567
1568 /* get the object associated with the stream interface.The
1569 * object can be other thing than a connection. For example,
1570 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001571 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001572 if (!conn)
1573 return 0;
1574
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001575 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001576 syscal can fail. */
1577 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001578 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001579 return 0;
1580
1581 /* extract the value. */
1582 smp->data.type = SMP_T_SINT;
1583 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001584 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1585 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1586#if defined(__linux__)
1587 /* these ones are common to all Linux versions */
1588 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1589 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1590 case 4: smp->data.u.sint = info.tcpi_lost; break;
1591 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1592 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1593 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1594#elif defined(__FreeBSD__) || defined(__NetBSD__)
1595 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1596 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1597 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1598 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1599 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1600 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1601 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1602#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001603 default: return 0;
1604 }
1605
1606 /* Convert the value as expected. */
1607 if (args) {
1608 if (args[0].type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001609 if (strcmp(args[0].data.str.area, "us") == 0) {
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001610 /* Do nothing. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001611 } else if (strcmp(args[0].data.str.area, "ms") == 0) {
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001612 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1613 } else
1614 return 0;
1615 } else if (args[0].type == ARGT_STOP) {
1616 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1617 } else
1618 return 0;
1619 }
1620
1621 return 1;
1622}
1623
1624/* get the mean rtt of a client connexion */
1625static int
1626smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1627{
1628 if (!get_tcp_info(args, smp, 0, 0))
1629 return 0;
1630 return 1;
1631}
1632
1633/* get the variance of the mean rtt of a client connexion */
1634static int
1635smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1636{
1637 if (!get_tcp_info(args, smp, 0, 1))
1638 return 0;
1639 return 1;
1640}
Joe Williams30fcd392016-08-10 07:06:44 -07001641
1642#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1643
1644/* get the unacked counter on a client connexion */
1645static int
1646smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1647{
1648 if (!get_tcp_info(args, smp, 0, 2))
1649 return 0;
1650 return 1;
1651}
1652
1653/* get the sacked counter on a client connexion */
1654static int
1655smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1656{
1657 if (!get_tcp_info(args, smp, 0, 3))
1658 return 0;
1659 return 1;
1660}
1661
1662/* get the lost counter on a client connexion */
1663static int
1664smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1665{
1666 if (!get_tcp_info(args, smp, 0, 4))
1667 return 0;
1668 return 1;
1669}
1670
1671/* get the retrans counter on a client connexion */
1672static int
1673smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1674{
1675 if (!get_tcp_info(args, smp, 0, 5))
1676 return 0;
1677 return 1;
1678}
1679
1680/* get the fackets counter on a client connexion */
1681static int
1682smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1683{
1684 if (!get_tcp_info(args, smp, 0, 6))
1685 return 0;
1686 return 1;
1687}
1688
1689/* get the reordering counter on a client connexion */
1690static int
1691smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1692{
1693 if (!get_tcp_info(args, smp, 0, 7))
1694 return 0;
1695 return 1;
1696}
1697#endif // linux || freebsd || netbsd
1698#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001699
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001700#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001701/* parse the "v4v6" bind keyword */
1702static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1703{
1704 struct listener *l;
1705
1706 list_for_each_entry(l, &conf->listeners, by_bind) {
1707 if (l->addr.ss_family == AF_INET6)
1708 l->options |= LI_O_V4V6;
1709 }
1710
1711 return 0;
1712}
1713
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001714/* parse the "v6only" bind keyword */
1715static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1716{
1717 struct listener *l;
1718
1719 list_for_each_entry(l, &conf->listeners, by_bind) {
1720 if (l->addr.ss_family == AF_INET6)
1721 l->options |= LI_O_V6ONLY;
1722 }
1723
1724 return 0;
1725}
1726#endif
1727
Pieter Baauwd551fb52013-05-08 22:49:23 +02001728#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001729/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001730static 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 +02001731{
1732 struct listener *l;
1733
Willy Tarreau4348fad2012-09-20 16:48:07 +02001734 list_for_each_entry(l, &conf->listeners, by_bind) {
1735 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1736 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001737 }
1738
Willy Tarreau44791242012-09-12 23:27:21 +02001739 return 0;
1740}
1741#endif
1742
1743#ifdef TCP_DEFER_ACCEPT
1744/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001745static 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 +02001746{
1747 struct listener *l;
1748
Willy Tarreau4348fad2012-09-20 16:48:07 +02001749 list_for_each_entry(l, &conf->listeners, by_bind) {
1750 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1751 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001752 }
1753
Willy Tarreau44791242012-09-12 23:27:21 +02001754 return 0;
1755}
1756#endif
1757
Willy Tarreau1c862c52012-10-05 16:21:00 +02001758#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001759/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001760static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1761{
1762 struct listener *l;
1763
1764 list_for_each_entry(l, &conf->listeners, by_bind) {
1765 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1766 l->options |= LI_O_TCP_FO;
1767 }
1768
1769 return 0;
1770}
1771#endif
1772
Willy Tarreau44791242012-09-12 23:27:21 +02001773#ifdef TCP_MAXSEG
1774/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001775static 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 +02001776{
1777 struct listener *l;
1778 int mss;
1779
Willy Tarreau44791242012-09-12 23:27:21 +02001780 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001781 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001782 return ERR_ALERT | ERR_FATAL;
1783 }
1784
1785 mss = atoi(args[cur_arg + 1]);
1786 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001787 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001788 return ERR_ALERT | ERR_FATAL;
1789 }
1790
Willy Tarreau4348fad2012-09-20 16:48:07 +02001791 list_for_each_entry(l, &conf->listeners, by_bind) {
1792 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1793 l->maxseg = mss;
1794 }
Willy Tarreau44791242012-09-12 23:27:21 +02001795
1796 return 0;
1797}
1798#endif
1799
Willy Tarreau2af207a2015-02-04 00:45:58 +01001800#ifdef TCP_USER_TIMEOUT
1801/* parse the "tcp-ut" bind keyword */
1802static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1803{
1804 const char *ptr = NULL;
1805 struct listener *l;
1806 unsigned int timeout;
1807
1808 if (!*args[cur_arg + 1]) {
1809 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1810 return ERR_ALERT | ERR_FATAL;
1811 }
1812
1813 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
1814 if (ptr) {
1815 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1816 return ERR_ALERT | ERR_FATAL;
1817 }
1818
1819 list_for_each_entry(l, &conf->listeners, by_bind) {
1820 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1821 l->tcp_ut = timeout;
1822 }
1823
1824 return 0;
1825}
1826#endif
1827
Willy Tarreau44791242012-09-12 23:27:21 +02001828#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001829/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001830static 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 +02001831{
1832 struct listener *l;
1833
Willy Tarreau44791242012-09-12 23:27:21 +02001834 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001835 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001836 return ERR_ALERT | ERR_FATAL;
1837 }
1838
Willy Tarreau4348fad2012-09-20 16:48:07 +02001839 list_for_each_entry(l, &conf->listeners, by_bind) {
1840 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1841 l->interface = strdup(args[cur_arg + 1]);
1842 }
Willy Tarreau44791242012-09-12 23:27:21 +02001843
Willy Tarreau44791242012-09-12 23:27:21 +02001844 return 0;
1845}
1846#endif
1847
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001848#ifdef CONFIG_HAP_NS
1849/* parse the "namespace" bind keyword */
1850static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1851{
1852 struct listener *l;
1853 char *namespace = NULL;
1854
1855 if (!*args[cur_arg + 1]) {
1856 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1857 return ERR_ALERT | ERR_FATAL;
1858 }
1859 namespace = args[cur_arg + 1];
1860
1861 list_for_each_entry(l, &conf->listeners, by_bind) {
1862 l->netns = netns_store_lookup(namespace, strlen(namespace));
1863
1864 if (l->netns == NULL)
1865 l->netns = netns_store_insert(namespace);
1866
1867 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001868 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001869 return ERR_ALERT | ERR_FATAL;
1870 }
1871 }
1872 return 0;
1873}
1874#endif
1875
Willy Tarreau163d4622015-10-13 16:16:41 +02001876#ifdef TCP_USER_TIMEOUT
1877/* parse the "tcp-ut" server keyword */
1878static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1879{
1880 const char *ptr = NULL;
1881 unsigned int timeout;
1882
1883 if (!*args[*cur_arg + 1]) {
1884 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1885 return ERR_ALERT | ERR_FATAL;
1886 }
1887
1888 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
1889 if (ptr) {
1890 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1891 return ERR_ALERT | ERR_FATAL;
1892 }
1893
1894 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1895 newsrv->tcp_ut = timeout;
1896
1897 return 0;
1898}
1899#endif
1900
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001901
Willy Tarreau4a129812012-04-25 17:31:42 +02001902/* Note: must not be declared <const> as its list will be overwritten.
1903 * Note: fetches that may return multiple types must be declared as the lowest
1904 * common denominator, the type that can be casted into all other ones. For
1905 * instance v4/v6 must be declared v4.
1906 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001907static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001908 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001909 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001910 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001911 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001912 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001913 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001914#ifdef TCP_INFO
Joe Williams30fcd392016-08-10 07:06:44 -07001915 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1916 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1917#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1918 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1919 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1920 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1921 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1922 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1923 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1924#endif // linux || freebsd || netbsd
1925#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001926 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001927}};
1928
Willy Tarreau0108d902018-11-25 19:14:37 +01001929INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1930
Willy Tarreau44791242012-09-12 23:27:21 +02001931/************************************************************************/
1932/* All supported bind keywords must be declared here. */
1933/************************************************************************/
1934
1935/* Note: must not be declared <const> as its list will be overwritten.
1936 * Please take care of keeping this list alphabetically sorted, doing so helps
1937 * all code contributors.
1938 * Optional keywords are also declared with a NULL ->parse() function so that
1939 * the config parser can report an appropriate error when a known keyword was
1940 * not enabled.
1941 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001942static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001943#ifdef TCP_DEFER_ACCEPT
1944 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1945#endif
1946#ifdef SO_BINDTODEVICE
1947 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1948#endif
1949#ifdef TCP_MAXSEG
1950 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1951#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001952#ifdef TCP_USER_TIMEOUT
1953 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1954#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001955#ifdef TCP_FASTOPEN
1956 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1957#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001958#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001959 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1960#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001961#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001962 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001963 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1964#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001965#ifdef CONFIG_HAP_NS
1966 { "namespace", bind_parse_namespace, 1 },
1967#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001968 /* the versions with the NULL parse function*/
1969 { "defer-accept", NULL, 0 },
1970 { "interface", NULL, 1 },
1971 { "mss", NULL, 1 },
1972 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001973 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001974 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001975 { NULL, NULL, 0 },
1976}};
1977
Willy Tarreau0108d902018-11-25 19:14:37 +01001978INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
1979
Willy Tarreau163d4622015-10-13 16:16:41 +02001980static struct srv_kw_list srv_kws = { "TCP", { }, {
1981#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001982 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02001983#endif
1984 { NULL, NULL, 0 },
1985}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02001986
Willy Tarreau0108d902018-11-25 19:14:37 +01001987INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
1988
Willy Tarreau2d392c22015-08-24 01:43:45 +02001989static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02001990 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02001991 { "set-src", tcp_parse_set_src_dst },
1992 { "set-src-port", tcp_parse_set_src_dst },
1993 { "set-dst" , tcp_parse_set_src_dst },
1994 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02001995 { /* END */ }
1996}};
1997
Willy Tarreau0108d902018-11-25 19:14:37 +01001998INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions);
1999
Willy Tarreau620408f2016-10-21 16:37:51 +02002000static struct action_kw_list tcp_req_sess_actions = {ILH, {
2001 { "silent-drop", tcp_parse_silent_drop },
2002 { "set-src", tcp_parse_set_src_dst },
2003 { "set-src-port", tcp_parse_set_src_dst },
2004 { "set-dst" , tcp_parse_set_src_dst },
2005 { "set-dst-port", tcp_parse_set_src_dst },
2006 { /* END */ }
2007}};
2008
Willy Tarreau0108d902018-11-25 19:14:37 +01002009INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions);
2010
Willy Tarreau2d392c22015-08-24 01:43:45 +02002011static struct action_kw_list tcp_req_cont_actions = {ILH, {
2012 { "silent-drop", tcp_parse_silent_drop },
2013 { /* END */ }
2014}};
2015
Willy Tarreau0108d902018-11-25 19:14:37 +01002016INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2017
Willy Tarreau2d392c22015-08-24 01:43:45 +02002018static struct action_kw_list tcp_res_cont_actions = {ILH, {
2019 { "silent-drop", tcp_parse_silent_drop },
2020 { /* END */ }
2021}};
2022
Willy Tarreau0108d902018-11-25 19:14:37 +01002023INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
2024
Willy Tarreau2d392c22015-08-24 01:43:45 +02002025static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002026 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002027 { "set-src", tcp_parse_set_src_dst },
2028 { "set-src-port", tcp_parse_set_src_dst },
2029 { "set-dst", tcp_parse_set_src_dst },
2030 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002031 { /* END */ }
2032}};
2033
Willy Tarreau0108d902018-11-25 19:14:37 +01002034INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2035
Willy Tarreau2d392c22015-08-24 01:43:45 +02002036static struct action_kw_list http_res_actions = {ILH, {
2037 { "silent-drop", tcp_parse_silent_drop },
2038 { /* END */ }
2039}};
2040
Willy Tarreau0108d902018-11-25 19:14:37 +01002041INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002042
Willy Tarreau80713382018-11-26 10:19:54 +01002043REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
Willy Tarreauba962912016-12-21 18:55:02 +01002044#if defined(IP_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002045 " IP_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002046#endif
2047#if defined(IPV6_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002048 " IPV6_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002049#endif
2050#if defined(IP_FREEBIND)
Willy Tarreau80713382018-11-26 10:19:54 +01002051 " IP_FREEBIND"
Willy Tarreauba962912016-12-21 18:55:02 +01002052#endif
2053#if defined(IP_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002054 " IP_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002055#endif
2056#if defined(IPV6_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002057 " IPV6_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002058#endif
2059#if defined(SO_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002060 " SO_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002061#endif
Willy Tarreau80713382018-11-26 10:19:54 +01002062 "");
Willy Tarreaue6b98942007-10-29 01:09:36 +01002063
2064
2065/*
2066 * Local variables:
2067 * c-indent-level: 8
2068 * c-basic-offset: 8
2069 * End:
2070 */