blob: 4ca6fc2392e5590478ed818cb70070a7fcb1e22b [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>
Willy Tarreaue6b98942007-10-29 01:09:36 +010028#include <sys/types.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010029
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040030#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020031#include <netinet/in.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040032
Willy Tarreaue6b98942007-10-29 01:09:36 +010033#include <common/compat.h>
34#include <common/config.h>
35#include <common/debug.h>
36#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010037#include <common/initcall.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010038#include <common/mini-clist.h>
39#include <common/standard.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010040#include <common/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010041
Willy Tarreau39713102016-11-25 15:49:32 +010042#include <types/action.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020043#include <types/connection.h>
Willy Tarreau39713102016-11-25 15:49:32 +010044#include <types/global.h>
45#include <types/stream.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010046
Willy Tarreau9fcb9842012-04-20 14:45:49 +020047#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020048#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020049#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020050#include <proto/fd.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020051#include <proto/http_rules.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020052#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020053#include <proto/log.h>
54#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020055#include <proto/protocol.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020056#include <proto/proto_http.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010057#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020059#include <proto/sample.h>
Willy Tarreau163d4622015-10-13 16:16:41 +020060#include <proto/server.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020061#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010062#include <proto/tcp_rules.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010063
Emeric Bruncf20bf12010-10-22 16:06:11 +020064static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
65static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020066static void tcpv4_add_listener(struct listener *listener, int port);
67static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010068
69/* Note: must not be declared <const> as its list will be overwritten */
70static struct protocol proto_tcpv4 = {
71 .name = "tcpv4",
72 .sock_domain = AF_INET,
73 .sock_type = SOCK_STREAM,
74 .sock_prot = IPPROTO_TCP,
75 .sock_family = AF_INET,
76 .sock_addrlen = sizeof(struct sockaddr_in),
77 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020078 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020079 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020080 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010081 .bind_all = tcp_bind_listeners,
82 .unbind_all = unbind_all_listeners,
83 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020084 .get_src = tcp_get_src,
85 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020086 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020087 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010088 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
89 .nb_listeners = 0,
90};
91
Willy Tarreau0108d902018-11-25 19:14:37 +010092INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
93
Willy Tarreaue6b98942007-10-29 01:09:36 +010094/* Note: must not be declared <const> as its list will be overwritten */
95static struct protocol proto_tcpv6 = {
96 .name = "tcpv6",
97 .sock_domain = AF_INET6,
98 .sock_type = SOCK_STREAM,
99 .sock_prot = IPPROTO_TCP,
100 .sock_family = AF_INET6,
101 .sock_addrlen = sizeof(struct sockaddr_in6),
102 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200103 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200104 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200105 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100106 .bind_all = tcp_bind_listeners,
107 .unbind_all = unbind_all_listeners,
108 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200109 .get_src = tcp_get_src,
110 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +0200111 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200112 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100113 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
114 .nb_listeners = 0,
115};
116
Willy Tarreau0108d902018-11-25 19:14:37 +0100117INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
118
Olivier Houchard153659f2017-04-05 22:39:56 +0200119/* Default TCP parameters, got by opening a temporary TCP socket. */
120#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100121static THREAD_LOCAL int default_tcp_maxseg = -1;
122static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200123#endif
124
David du Colombier6f5ccb12011-03-10 22:26:24 +0100125/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100126 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
127 * - 0 : ignore remote address (may even be a NULL pointer)
128 * - 1 : use provided address
129 * - 2 : use provided port
130 * - 3 : use both
131 *
132 * The function supports multiple foreign binding methods :
133 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100134 * The second one can be used as a fallback for the first one.
135 * This function returns 0 when everything's OK, 1 if it could not bind, to the
136 * local address, 2 if it could not bind to the foreign address.
137 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100138int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100139{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100140 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100141 int foreign_ok = 0;
142 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100143 static THREAD_LOCAL int ip_transp_working = 1;
144 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200145
David du Colombier65c17962012-07-13 14:34:59 +0200146 switch (local->ss_family) {
147 case AF_INET:
148 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200149 /* This deserves some explanation. Some platforms will support
150 * multiple combinations of certain methods, so we try the
151 * supported ones until one succeeds.
152 */
153 if (0
154#if defined(IP_TRANSPARENT)
155 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
156#endif
157#if defined(IP_FREEBIND)
158 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
159#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200160#if defined(IP_BINDANY)
161 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
162#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200163#if defined(SO_BINDANY)
164 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
165#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200166 )
David du Colombier65c17962012-07-13 14:34:59 +0200167 foreign_ok = 1;
168 else
169 ip_transp_working = 0;
170 }
171 break;
172 case AF_INET6:
173 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200174 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200175#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200176 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
177#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100178#if defined(IP_FREEBIND)
179 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
180#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200181#if defined(IPV6_BINDANY)
182 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
183#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200184#if defined(SO_BINDANY)
185 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
186#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200187 )
David du Colombier65c17962012-07-13 14:34:59 +0200188 foreign_ok = 1;
189 else
190 ip6_transp_working = 0;
191 }
192 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100193 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200194
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100195 if (flags) {
196 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200197 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100198 switch (remote->ss_family) {
199 case AF_INET:
200 if (flags & 1)
201 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
202 if (flags & 2)
203 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
204 break;
205 case AF_INET6:
206 if (flags & 1)
207 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
208 if (flags & 2)
209 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
210 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100211 default:
212 /* we don't want to try to bind to an unknown address family */
213 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100214 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100215 }
216
Simon Hormande072bd2011-06-24 15:11:37 +0900217 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100218 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200219 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200220 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
221 if (ret < 0)
222 return 2;
223 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100224 }
225 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200226 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200227 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
228 if (ret < 0)
229 return 1;
230 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100231 }
232
233 if (!flags)
234 return 0;
235
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100236 if (!foreign_ok)
237 /* we could not bind to a foreign address */
238 return 2;
239
240 return 0;
241}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100242
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100243static int create_server_socket(struct connection *conn)
244{
Willy Tarreau529c1392014-12-24 13:47:55 +0100245 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100246
Willy Tarreaue5733232019-05-22 19:24:06 +0200247#ifdef USE_NS
Willy Tarreau529c1392014-12-24 13:47:55 +0100248 if (objt_server(conn->target)) {
249 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
250 ns = conn->proxy_netns;
251 else
252 ns = __objt_server(conn->target)->netns;
253 }
254#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100255 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
256}
Willy Tarreau9650f372009-08-16 14:02:45 +0200257
258/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200259 * This function initiates a TCP connection establishment to the target assigned
260 * to connection <conn> using (si->{target,addr.to}). A source address may be
261 * pointed to by conn->addr.from in case of transparent proxying. Normal source
262 * bind addresses are still determined locally (due to the possible need of a
263 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100264 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100265 * supported. The <data> parameter is a boolean indicating whether there are data
266 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200267 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
268 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100269 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200270 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
271 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200272 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200273 * Note that a pending send_proxy message accounts for data.
274 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200275 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200276 * - SF_ERR_NONE if everything's OK
277 * - SF_ERR_SRVTO if there are no more servers
278 * - SF_ERR_SRVCL if the connection was refused by the server
279 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
280 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
281 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100282 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100283 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200284 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100285 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200286 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100287
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200288int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200289{
290 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100291 struct server *srv;
292 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100293 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100294 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800295 struct sockaddr_storage *addr;
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;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100307 /* Make sure we check that we have data before activating
308 * TFO, or we could trigger a kernel issue whereby after
309 * a successful connect() == 0, any subsequent connect()
310 * will return EINPROGRESS instead of EISCONN.
311 */
312 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
313 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
314 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100315 break;
316 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100317 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200318 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100319 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200320
Willy Tarreau585744b2017-08-24 14:31:19 +0200321 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100322
323 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200324 qfprintf(stderr, "Cannot get a server socket.\n");
325
Willy Tarreau9ce70132014-01-24 16:08:19 +0100326 if (errno == ENFILE) {
327 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200328 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100329 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
330 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100331 }
332 else if (errno == EMFILE) {
333 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200334 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100335 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
336 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100337 }
338 else if (errno == ENOBUFS || errno == ENOMEM) {
339 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200340 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100341 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
342 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100343 }
344 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
345 conn->err_code = CO_ER_NOPROTO;
346 }
347 else
348 conn->err_code = CO_ER_SOCK_ERR;
349
Willy Tarreau9650f372009-08-16 14:02:45 +0200350 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100351 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200352 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200353 }
354
355 if (fd >= global.maxsock) {
356 /* do not log anything there, it's a normal condition when this option
357 * is used to serialize connections to a server !
358 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100359 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200360 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100361 conn->err_code = CO_ER_CONF_FDLIM;
362 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200363 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200364 }
365
366 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900367 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200368 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
369 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100370 conn->err_code = CO_ER_SOCK_ERR;
371 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200372 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200373 }
374
William Lallemandc03eb012018-11-27 12:02:37 +0100375 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
376 ha_alert("Cannot set CLOEXEC on client socket.\n");
377 close(fd);
378 conn->err_code = CO_ER_SOCK_ERR;
379 conn->flags |= CO_FL_ERROR;
380 return SF_ERR_INTERNAL;
381 }
382
Willy Tarreau9650f372009-08-16 14:02:45 +0200383 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900384 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200385
Willy Tarreau9650f372009-08-16 14:02:45 +0200386 /* allow specific binding :
387 * - server-specific at first
388 * - proxy-specific next
389 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100390 if (srv && srv->conn_src.opts & CO_SRC_BIND)
391 src = &srv->conn_src;
392 else if (be->conn_src.opts & CO_SRC_BIND)
393 src = &be->conn_src;
394 else
395 src = NULL;
396
397 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200398 int ret, flags = 0;
399
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200400 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100401 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100402 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200403 conn->flags |= CO_FL_PRIVATE;
404 /* fall through */
405 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200406 flags = 3;
407 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100408 case CO_SRC_TPROXY_CIP:
409 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200410 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200411 flags = 1;
412 break;
413 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200414 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200415
Willy Tarreau9650f372009-08-16 14:02:45 +0200416#ifdef SO_BINDTODEVICE
417 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100418 if (src->iface_name)
419 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200420#endif
421
Willy Tarreaua4380b42012-12-08 22:49:11 +0100422 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200423 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100424 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200425
426 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200427 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200428
429 do {
430 /* note: in case of retry, we may have to release a previously
431 * allocated port, hence this loop's construct.
432 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200433 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
434 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200435
436 if (!attempts)
437 break;
438 attempts--;
439
Willy Tarreaua4380b42012-12-08 22:49:11 +0100440 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100441 if (!fdinfo[fd].local_port) {
442 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200443 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100444 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200445
Willy Tarreaua4380b42012-12-08 22:49:11 +0100446 fdinfo[fd].port_range = src->sport_range;
447 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200448
Willy Tarreaua4380b42012-12-08 22:49:11 +0100449 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100450 if (ret != 0)
451 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 } while (ret != 0); /* binding NOK */
453 }
454 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000455#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100456 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000457 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
458#endif
Willy Tarreaua4380b42012-12-08 22:49:11 +0100459 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100460 if (ret != 0)
461 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200462 }
463
Willy Tarreaua4380b42012-12-08 22:49:11 +0100464 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200465 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
466 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200467 close(fd);
468
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100470 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
471 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200472 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100473 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200474 be->id);
475 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100476 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
477 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200478 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100479 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200480 be->id);
481 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100482 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200483 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200484 }
485 }
486
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400487#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200488 /* disabling tcp quick ack now allows the first request to leave the
489 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100490 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200491 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200492 if (flags & (CONNECT_DELACK_ALWAYS) ||
493 ((flags & CONNECT_DELACK_SMART_CONNECT ||
494 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
495 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900496 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200497#endif
498
Willy Tarreau163d4622015-10-13 16:16:41 +0200499#ifdef TCP_USER_TIMEOUT
500 /* there is not much more we can do here when it fails, it's still minor */
501 if (srv && srv->tcp_ut)
502 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
503#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100504
505 if (use_fastopen) {
506#if defined(TCP_FASTOPEN_CONNECT)
507 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
508#endif
509 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100510 if (global.tune.server_sndbuf)
511 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
512
513 if (global.tune.server_rcvbuf)
514 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
515
Alexander Liu2a54bb72019-05-22 19:44:48 +0800516 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : &conn->addr.to;
517 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100518 if (errno == EINPROGRESS || errno == EALREADY) {
519 /* common case, let's wait for connect status */
520 conn->flags |= CO_FL_WAIT_L4_CONN;
521 }
522 else if (errno == EISCONN) {
523 /* should normally not happen but if so, indicates that it's OK */
524 conn->flags &= ~CO_FL_WAIT_L4_CONN;
525 }
526 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200527 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100528 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200529 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100530 conn->err_code = CO_ER_FREE_PORTS;
531 }
532 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200533 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100534 conn->err_code = CO_ER_ADDR_INUSE;
535 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200536
Willy Tarreaub1719512012-12-08 23:03:28 +0100537 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200538 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
539 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200540 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100541 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100542 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200543 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200544 } else if (errno == ETIMEDOUT) {
545 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200546 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
547 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200548 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100549 conn->err_code = CO_ER_SOCK_ERR;
550 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200551 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200552 } else {
553 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
554 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200555 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
556 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200557 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100558 conn->err_code = CO_ER_SOCK_ERR;
559 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200560 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200561 }
562 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100563 else {
564 /* connect() == 0, this is great! */
565 conn->flags &= ~CO_FL_WAIT_L4_CONN;
566 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200567
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100568 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200569
Willy Tarreauf79c8172013-10-21 16:30:56 +0200570 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100571 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200572
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200573 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200574 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100575 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200576 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200577 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200578
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200579 conn_xprt_want_send(conn); /* for connect status, proxy protocol or SSL */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200580 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200581}
582
583
Willy Tarreau59b94792012-05-11 16:16:40 +0200584/*
585 * Retrieves the source address for the socket <fd>, with <dir> indicating
586 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
587 * success, -1 in case of error. The socket's source address is stored in
588 * <sa> for <salen> bytes.
589 */
590int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
591{
592 if (dir)
593 return getsockname(fd, sa, &salen);
594 else
595 return getpeername(fd, sa, &salen);
596}
597
598
599/*
600 * Retrieves the original destination address for the socket <fd>, with <dir>
601 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
602 * listener, if the original destination address was translated, the original
603 * address is retrieved. It returns 0 in case of success, -1 in case of error.
604 * The socket's source address is stored in <sa> for <salen> bytes.
605 */
606int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
607{
608 if (dir)
609 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100610 else {
611 int ret = getsockname(fd, sa, &salen);
612
613 if (ret < 0)
614 return ret;
615
Willy Tarreaue5733232019-05-22 19:24:06 +0200616#if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100617 /* For TPROXY and Netfilter's NAT, we can retrieve the original
618 * IPv4 address before DNAT/REDIRECT. We must not do that with
619 * other families because v6-mapped IPv4 addresses are still
620 * reported as v4.
621 */
622 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
623 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
624 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200625#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100626 return ret;
627 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200628}
629
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200630/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100631 * and we have nothing to send. It updates the FD polling status. It returns 0
632 * if it fails in a fatal way or needs to poll to go further, otherwise it
633 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
634 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
635 * errno. The error checking is done in two passes in order to limit the number
636 * of syscalls in the normal case :
637 * - if POLL_ERR was reported by the poller, we check for a pending error on
638 * the socket before proceeding. If found, it's assigned to errno so that
639 * upper layers can see it.
640 * - otherwise connect() is used to check the connection state again, since
641 * the getsockopt return cannot reliably be used to know if the connection
642 * is still pending or ready. This one may often return an error as well,
643 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200644 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200645int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200646{
Alexander Liu2a54bb72019-05-22 19:44:48 +0800647 struct sockaddr_storage *addr;
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 */
Alexander Liu2a54bb72019-05-22 19:44:48 +0800686 addr = &conn->addr.to;
687 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
688 addr = &objt_server(conn->target)->socks4_addr;
689
690 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200691 if (errno == EALREADY || errno == EINPROGRESS) {
Olivier Houchard7b3a79f2019-06-06 18:15:01 +0200692 __conn_xprt_want_send(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100693 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200694 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200695 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200696
Willy Tarreau2c6be842012-07-06 17:12:34 +0200697 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200698 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200699
Willy Tarreau2c6be842012-07-06 17:12:34 +0200700 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200701 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200702
Willy Tarreau076be252012-07-06 16:02:29 +0200703 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200704 * forward the event to the transport layer which will notify the
705 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200706 */
Willy Tarreau80184712012-07-06 14:54:49 +0200707 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200708 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200709
710 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200711 /* Write error on the file descriptor. Report it to the connection
712 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200713 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100714 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100715 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200716 __conn_xprt_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200717 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200718}
719
Olivier Houchard153659f2017-04-05 22:39:56 +0200720/* XXX: Should probably be elsewhere */
721static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
722{
723 if (a->ss_family != b->ss_family) {
724 return (-1);
725 }
726 switch (a->ss_family) {
727 case AF_INET:
728 {
729 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
730 if (a4->sin_port != b4->sin_port)
731 return (-1);
732 return (memcmp(&a4->sin_addr, &b4->sin_addr,
733 sizeof(a4->sin_addr)));
734 }
735 case AF_INET6:
736 {
737 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
738 if (a6->sin6_port != b6->sin6_port)
739 return (-1);
740 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
741 sizeof(a6->sin6_addr)));
742 }
743 default:
744 return (-1);
745 }
746
747}
748
749#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
750/* When binding the listeners, check if a socket has been sent to us by the
751 * previous process that we could reuse, instead of creating a new one.
752 */
753static int tcp_find_compatible_fd(struct listener *l)
754{
755 struct xfer_sock_list *xfer_sock = xfer_sock_list;
756 int ret = -1;
757
758 while (xfer_sock) {
759 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
760 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
761 (l->interface != NULL && xfer_sock->iface != NULL &&
762 !strcmp(l->interface, xfer_sock->iface))) {
763 if ((l->options & LI_MANDATORY_FLAGS) ==
764 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
765 if ((xfer_sock->namespace == NULL &&
766 l->netns == NULL)
Willy Tarreaue5733232019-05-22 19:24:06 +0200767#ifdef USE_NS
Olivier Houchard153659f2017-04-05 22:39:56 +0200768 || (xfer_sock->namespace != NULL &&
769 l->netns != NULL &&
770 !strcmp(xfer_sock->namespace,
771 l->netns->node.key))
772#endif
773 ) {
774 break;
775 }
776
777 }
778 }
779 }
780 xfer_sock = xfer_sock->next;
781 }
782 if (xfer_sock != NULL) {
783 ret = xfer_sock->fd;
784 if (xfer_sock == xfer_sock_list)
785 xfer_sock_list = xfer_sock->next;
786 if (xfer_sock->prev)
787 xfer_sock->prev->next = xfer_sock->next;
788 if (xfer_sock->next)
789 xfer_sock->next->prev = xfer_sock->prev;
790 free(xfer_sock->iface);
791 free(xfer_sock->namespace);
792 free(xfer_sock);
793 }
794 return ret;
795}
796#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200797
Willy Tarreaue6b98942007-10-29 01:09:36 +0100798/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100799 * an error message in <errmsg> if the message is at most <errlen> bytes long
800 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
801 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100802 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
803 * was alright and that no message was returned. ERR_RETRYABLE means that an
804 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700805 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100806 * the meaning of the error, but just indicate that a message is present which
807 * should be displayed with the respective level. Last, ERR_ABORT indicates
808 * that it's pointless to try to start other listeners. No error message is
809 * returned if errlen is NULL.
810 */
811int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
812{
813 __label__ tcp_return, tcp_close_return;
814 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100815 int ext, ready;
816 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100817 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200818#ifdef TCP_MAXSEG
819
820 /* Create a temporary TCP socket to get default parameters we can't
821 * guess.
822 * */
823 ready_len = sizeof(default_tcp_maxseg);
824 if (default_tcp_maxseg == -1) {
825 default_tcp_maxseg = -2;
826 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
827 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100828 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200829 else {
830 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
831 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100832 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200833 }
834 close(fd);
835 }
836 if (default_tcp6_maxseg == -1) {
837 default_tcp6_maxseg = -2;
838 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
839 if (fd >= 0) {
840 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
841 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100842 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200843 close(fd);
844 }
845 }
846#endif
847
Willy Tarreaue6b98942007-10-29 01:09:36 +0100848
849 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100850 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100851 *errmsg = 0;
852
853 if (listener->state != LI_ASSIGNED)
854 return ERR_NONE; /* already bound */
855
856 err = ERR_NONE;
857
Olivier Houchard153659f2017-04-05 22:39:56 +0200858 if (listener->fd == -1)
859 listener->fd = tcp_find_compatible_fd(listener);
860
Willy Tarreau40aa0702013-03-10 23:51:38 +0100861 /* if the listener already has an fd assigned, then we were offered the
862 * fd by an external process (most likely the parent), and we don't want
863 * to create a new socket. However we still want to set a few flags on
864 * the socket.
865 */
866 fd = listener->fd;
867 ext = (fd >= 0);
868
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100869 if (!ext) {
870 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
871
872 if (fd == -1) {
873 err |= ERR_RETRYABLE | ERR_ALERT;
874 msg = "cannot create listening socket";
875 goto tcp_return;
876 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100877 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100878
Willy Tarreaue6b98942007-10-29 01:09:36 +0100879 if (fd >= global.maxsock) {
880 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
881 msg = "not enough free sockets (raise '-n' parameter)";
882 goto tcp_close_return;
883 }
884
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200885 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100886 err |= ERR_FATAL | ERR_ALERT;
887 msg = "cannot make socket non-blocking";
888 goto tcp_close_return;
889 }
890
Willy Tarreau40aa0702013-03-10 23:51:38 +0100891 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100892 /* not fatal but should be reported */
893 msg = "cannot do so_reuseaddr";
894 err |= ERR_ALERT;
895 }
896
897 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900898 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200899 else {
900 struct linger tmplinger;
901 socklen_t len = sizeof(tmplinger);
902 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
903 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
904 tmplinger.l_onoff = 0;
905 tmplinger.l_linger = 0;
906 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
907 sizeof(tmplinger));
908 }
909 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100910
Willy Tarreaue6b98942007-10-29 01:09:36 +0100911#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000912 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
913 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100914 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000915 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100916 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100917#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200918
Willy Tarreau40aa0702013-03-10 23:51:38 +0100919 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200920 switch (listener->addr.ss_family) {
921 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200922 if (1
923#if defined(IP_TRANSPARENT)
924 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
925#endif
926#if defined(IP_FREEBIND)
927 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
928#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200929#if defined(IP_BINDANY)
930 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
931#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200932#if defined(SO_BINDANY)
933 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
934#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200935 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200936 msg = "cannot make listening socket transparent";
937 err |= ERR_ALERT;
938 }
939 break;
940 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200941 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200942#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200943 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
944#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100945#if defined(IP_FREEBIND)
946 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
947#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200948#if defined(IPV6_BINDANY)
949 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
950#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200951#if defined(SO_BINDANY)
952 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
953#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200954 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200955 msg = "cannot make listening socket transparent";
956 err |= ERR_ALERT;
957 }
958 break;
959 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100960 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200961
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100962#ifdef SO_BINDTODEVICE
963 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100964 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100965 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100966 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100967 msg = "cannot bind listener to device";
968 err |= ERR_WARN;
969 }
970 }
971#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400972#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100973 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400974 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200975 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
976 msg = "cannot set MSS";
977 err |= ERR_WARN;
978 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200979 } else if (ext) {
980 int tmpmaxseg = -1;
981 int defaultmss;
982 socklen_t len = sizeof(tmpmaxseg);
983
984 if (listener->addr.ss_family == AF_INET)
985 defaultmss = default_tcp_maxseg;
986 else
987 defaultmss = default_tcp6_maxseg;
988
989 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
990 if (tmpmaxseg != defaultmss && setsockopt(fd, IPPROTO_TCP,
991 TCP_MAXSEG, &defaultmss,
992 sizeof(defaultmss)) == -1) {
993 msg = "cannot set MSS";
994 err |= ERR_WARN;
995 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200996 }
997#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100998#if defined(TCP_USER_TIMEOUT)
999 if (listener->tcp_ut) {
1000 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
1001 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
1002 msg = "cannot set TCP User Timeout";
1003 err |= ERR_WARN;
1004 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001005 } else
1006 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
1007 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +01001008#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001009#if defined(TCP_DEFER_ACCEPT)
1010 if (listener->options & LI_O_DEF_ACCEPT) {
1011 /* defer accept by up to one second */
1012 int accept_delay = 1;
1013 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
1014 msg = "cannot enable DEFER_ACCEPT";
1015 err |= ERR_WARN;
1016 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001017 } else
1018 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
1019 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001020#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001021#if defined(TCP_FASTOPEN)
1022 if (listener->options & LI_O_TCP_FO) {
1023 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +01001024 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +02001025 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
1026 msg = "cannot enable TCP_FASTOPEN";
1027 err |= ERR_WARN;
1028 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001029 } else {
1030 socklen_t len;
1031 int qlen;
1032 len = sizeof(qlen);
1033 /* Only disable fast open if it was enabled, we don't want
1034 * the kernel to create a fast open queue if there's none.
1035 */
1036 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
1037 qlen != 0) {
1038 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
1039 sizeof(zero)) == -1) {
1040 msg = "cannot disable TCP_FASTOPEN";
1041 err |= ERR_WARN;
1042 }
1043 }
Willy Tarreau1c862c52012-10-05 16:21:00 +02001044 }
1045#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001046#if defined(IPV6_V6ONLY)
1047 if (listener->options & LI_O_V6ONLY)
1048 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +01001049 else if (listener->options & LI_O_V4V6)
1050 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001051#endif
1052
Willy Tarreau40aa0702013-03-10 23:51:38 +01001053 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001054 err |= ERR_RETRYABLE | ERR_ALERT;
1055 msg = "cannot bind socket";
1056 goto tcp_close_return;
1057 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001058
Willy Tarreau40aa0702013-03-10 23:51:38 +01001059 ready = 0;
1060 ready_len = sizeof(ready);
1061 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
1062 ready = 0;
1063
1064 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +01001065 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001066 err |= ERR_RETRYABLE | ERR_ALERT;
1067 msg = "cannot listen to socket";
1068 goto tcp_close_return;
1069 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001070
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001071#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001072 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +09001073 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +02001074 else
1075 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001076#endif
1077
Willy Tarreaue6b98942007-10-29 01:09:36 +01001078 /* the socket is ready */
1079 listener->fd = fd;
1080 listener->state = LI_LISTEN;
1081
Willy Tarreaua9786b62018-01-25 07:22:13 +01001082 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreaua36b3242019-02-02 13:14:34 +01001083 thread_mask(listener->bind_conf->bind_thread));
Willy Tarreaueb472682010-05-28 18:46:57 +02001084
Willy Tarreaue6b98942007-10-29 01:09:36 +01001085 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001086 if (msg && errlen) {
1087 char pn[INET6_ADDRSTRLEN];
1088
Willy Tarreau631f01c2011-09-05 00:36:48 +02001089 addr_to_str(&listener->addr, pn, sizeof(pn));
1090 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001091 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001092 return err;
1093
1094 tcp_close_return:
1095 close(fd);
1096 goto tcp_return;
1097}
1098
1099/* This function creates all TCP sockets bound to the protocol entry <proto>.
1100 * It is intended to be used as the protocol's bind_all() function.
1101 * The sockets will be registered but not added to any fd_set, in order not to
1102 * loose them across the fork(). A call to enable_all_listeners() is needed
1103 * to complete initialization. The return value is composed from ERR_*.
1104 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001105static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001106{
1107 struct listener *listener;
1108 int err = ERR_NONE;
1109
1110 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001111 err |= tcp_bind_listener(listener, errmsg, errlen);
1112 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001113 break;
1114 }
1115
1116 return err;
1117}
1118
Willy Tarreau32282382017-09-15 07:44:44 +02001119/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1120 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1121 * The number of listeners for the protocol is updated.
Willy Tarreaue6b98942007-10-29 01:09:36 +01001122 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001123static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001124{
1125 if (listener->state != LI_INIT)
1126 return;
1127 listener->state = LI_ASSIGNED;
1128 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001129 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001130 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1131 proto_tcpv4.nb_listeners++;
1132}
1133
Willy Tarreau32282382017-09-15 07:44:44 +02001134/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1135 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1136 * The number of listeners for the protocol is updated.
Willy Tarreaue6b98942007-10-29 01:09:36 +01001137 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001138static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001139{
1140 if (listener->state != LI_INIT)
1141 return;
1142 listener->state = LI_ASSIGNED;
1143 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001144 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001145 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1146 proto_tcpv6.nb_listeners++;
1147}
1148
Willy Tarreau092d8652014-07-07 20:22:12 +02001149/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1150 * was totally stopped, or > 0 if correctly paused.
1151 */
1152int tcp_pause_listener(struct listener *l)
1153{
1154 if (shutdown(l->fd, SHUT_WR) != 0)
1155 return -1; /* Solaris dies here */
1156
Willy Tarreaue2711c72019-02-27 15:39:41 +01001157 if (listen(l->fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +02001158 return -1; /* OpenBSD dies here */
1159
1160 if (shutdown(l->fd, SHUT_RD) != 0)
1161 return -1; /* should always be OK */
1162 return 1;
1163}
1164
William Lallemand2e785f22016-05-25 01:48:42 +02001165/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001166 * Execute the "set-src" action. May be called from {tcp,http}request.
1167 * It only changes the address and tries to preserve the original port. If the
1168 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001169 */
1170enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1171 struct session *sess, struct stream *s, int flags)
1172{
1173 struct connection *cli_conn;
1174
1175 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1176 struct sample *smp;
1177
1178 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1179 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001180 int port = get_net_port(&cli_conn->addr.from);
1181
William Lallemand2e785f22016-05-25 01:48:42 +02001182 if (smp->data.type == SMP_T_IPV4) {
1183 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
1184 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001185 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001186 } else if (smp->data.type == SMP_T_IPV6) {
1187 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
1188 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 +02001189 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001190 }
1191 }
William Lallemand01252ed2016-05-25 02:33:16 +02001192 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001193 }
1194 return ACT_RET_CONT;
1195}
1196
William Lallemand44be6402016-05-25 01:51:35 +02001197/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001198 * Execute the "set-dst" action. May be called from {tcp,http}request.
1199 * It only changes the address and tries to preserve the original port. If the
1200 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001201 */
1202enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1203 struct session *sess, struct stream *s, int flags)
1204{
1205 struct connection *cli_conn;
1206
1207 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1208 struct sample *smp;
1209
1210 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1211 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001212 int port = get_net_port(&cli_conn->addr.to);
1213
William Lallemand13e9b0c2016-05-25 02:34:07 +02001214 if (smp->data.type == SMP_T_IPV4) {
1215 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_family = AF_INET;
1216 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1217 } else if (smp->data.type == SMP_T_IPV6) {
1218 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_family = AF_INET6;
1219 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 +02001220 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001221 }
1222 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1223 }
1224 }
1225 return ACT_RET_CONT;
1226}
1227
1228/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001229 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1230 * We must test the sin_family before setting the port. If the address family
1231 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1232 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001233 */
1234enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1235 struct session *sess, struct stream *s, int flags)
1236{
1237 struct connection *cli_conn;
1238
1239 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1240 struct sample *smp;
1241
1242 conn_get_from_addr(cli_conn);
1243
1244 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1245 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001246 if (cli_conn->addr.from.ss_family == AF_INET6) {
William Lallemand44be6402016-05-25 01:51:35 +02001247 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001248 } else {
1249 if (cli_conn->addr.from.ss_family != AF_INET) {
1250 cli_conn->addr.from.ss_family = AF_INET;
1251 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1252 }
1253 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001254 }
1255 }
1256 }
1257 return ACT_RET_CONT;
1258}
1259
William Lallemand13e9b0c2016-05-25 02:34:07 +02001260/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001261 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1262 * We must test the sin_family before setting the port. If the address family
1263 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1264 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001265 */
1266enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1267 struct session *sess, struct stream *s, int flags)
1268{
1269 struct connection *cli_conn;
1270
1271 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1272 struct sample *smp;
1273
1274 conn_get_to_addr(cli_conn);
1275
1276 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1277 if (smp) {
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001278 if (cli_conn->addr.to.ss_family == AF_INET6) {
1279 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001280 } else {
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001281 if (cli_conn->addr.to.ss_family != AF_INET) {
1282 cli_conn->addr.to.ss_family = AF_INET;
1283 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001284 }
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001285 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001286 }
1287 }
1288 }
1289 return ACT_RET_CONT;
1290}
1291
Willy Tarreau2d392c22015-08-24 01:43:45 +02001292/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1293static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1294{
1295 struct connection *conn = objt_conn(sess->origin);
1296
1297 if (!conn)
1298 goto out;
1299
1300 if (!conn_ctrl_ready(conn))
1301 goto out;
1302
Willy Tarreau2d392c22015-08-24 01:43:45 +02001303#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001304 /* drain is needed only to send the quick ACK */
1305 conn_sock_drain(conn);
1306
Willy Tarreau2d392c22015-08-24 01:43:45 +02001307 /* re-enable quickack if it was disabled to ack all data and avoid
1308 * retransmits from the client that might trigger a real reset.
1309 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001310 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001311#endif
1312 /* lingering must absolutely be disabled so that we don't send a
1313 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1314 * is present, returning with ERR will cause lingering to be disabled.
1315 */
1316 if (strm)
1317 strm->si[0].flags |= SI_FL_NOLINGER;
1318
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001319 /* We're on the client-facing side, we must force to disable lingering to
1320 * ensure we will use an RST exclusively and kill any pending data.
1321 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001322 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001323
Willy Tarreau2d392c22015-08-24 01:43:45 +02001324#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001325 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001326 /* socket will be quiet now */
1327 goto out;
1328 }
1329#endif
1330 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1331 * Let's fall back on the TTL trick, though it only works for routed
1332 * network and has no effect on local net.
1333 */
1334#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001335 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001336#endif
1337 out:
1338 /* kill the stream if any */
1339 if (strm) {
1340 channel_abort(&strm->req);
1341 channel_abort(&strm->res);
1342 strm->req.analysers = 0;
1343 strm->res.analysers = 0;
Olivier Houchard40514102019-03-08 18:54:04 +01001344 _HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001345 if (!(strm->flags & SF_ERR_MASK))
1346 strm->flags |= SF_ERR_PRXCOND;
1347 if (!(strm->flags & SF_FINST_MASK))
1348 strm->flags |= SF_FINST_R;
1349 }
1350
Olivier Houchard40514102019-03-08 18:54:04 +01001351 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001352 if (sess->listener->counters)
Olivier Houchard40514102019-03-08 18:54:04 +01001353 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001354
1355 return ACT_RET_STOP;
1356}
1357
William Lallemand13e9b0c2016-05-25 02:34:07 +02001358/* parse "set-{src,dst}[-port]" action */
1359enum 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 +02001360{
1361 int cur_arg;
1362 struct sample_expr *expr;
1363 unsigned int where;
1364
1365 cur_arg = *orig_arg;
1366 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
1367 if (!expr)
1368 return ACT_RET_PRS_ERR;
1369
1370 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001371 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001372 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001373 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001374 where |= SMP_VAL_BE_HRQ_HDR;
1375
1376 if (!(expr->fetch->val & where)) {
1377 memprintf(err,
1378 "fetch method '%s' extracts information from '%s', none of which is available here",
1379 args[cur_arg-1], sample_src_names(expr->fetch->use));
1380 free(expr);
1381 return ACT_RET_PRS_ERR;
1382 }
1383 rule->arg.expr = expr;
1384 rule->action = ACT_CUSTOM;
1385
1386 if (!strcmp(args[*orig_arg-1], "set-src")) {
1387 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001388 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1389 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001390 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1391 rule->action_ptr = tcp_action_req_set_dst;
1392 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1393 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001394 } else {
1395 return ACT_RET_PRS_ERR;
1396 }
1397
1398 (*orig_arg)++;
1399
1400 return ACT_RET_PRS_OK;
1401}
1402
1403
Willy Tarreau2d392c22015-08-24 01:43:45 +02001404/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1405 * success, ACT_RET_PRS_ERR on error.
1406 */
1407static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1408 struct act_rule *rule, char **err)
1409{
1410 rule->action = ACT_CUSTOM;
1411 rule->action_ptr = tcp_exec_action_silent_drop;
1412 return ACT_RET_PRS_OK;
1413}
1414
Willy Tarreau645513a2010-05-24 20:55:15 +02001415
1416/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001417/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001418/************************************************************************/
1419
Willy Tarreau4a129812012-04-25 17:31:42 +02001420/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001421int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001422{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001423 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001424
1425 if (!cli_conn)
1426 return 0;
1427
1428 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001429 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001430 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001431 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001432 break;
1433 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001434 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001435 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001436 break;
1437 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001438 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001439 }
Emeric Brunf769f512010-10-22 17:14:01 +02001440
Willy Tarreau37406352012-04-23 16:16:37 +02001441 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001442 return 1;
1443}
1444
Willy Tarreaua5e37562011-12-16 17:06:15 +01001445/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001446static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001447smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001448{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001449 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001450
1451 if (!cli_conn)
1452 return 0;
1453
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001454 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001455 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001456 return 0;
1457
Willy Tarreau37406352012-04-23 16:16:37 +02001458 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001459 return 1;
1460}
1461
Willy Tarreau4a129812012-04-25 17:31:42 +02001462/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001463static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001464smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001465{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001466 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001467
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001468 if (!cli_conn)
1469 return 0;
1470
1471 conn_get_to_addr(cli_conn);
1472
1473 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001474 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001475 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001476 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001477 break;
1478 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001479 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001480 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001481 break;
1482 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001483 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001484 }
Emeric Brunf769f512010-10-22 17:14:01 +02001485
Willy Tarreau37406352012-04-23 16:16:37 +02001486 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001487 return 1;
1488}
1489
Willy Tarreau16e01562016-08-09 16:46:18 +02001490/* check if the destination address of the front connection is local to the
1491 * system or if it was intercepted.
1492 */
1493int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1494{
1495 struct connection *conn = objt_conn(smp->sess->origin);
1496 struct listener *li = smp->sess->listener;
1497
1498 if (!conn)
1499 return 0;
1500
1501 conn_get_to_addr(conn);
1502 if (!(conn->flags & CO_FL_ADDR_TO_SET))
1503 return 0;
1504
1505 smp->data.type = SMP_T_BOOL;
1506 smp->flags = 0;
1507 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.to);
1508 return smp->data.u.sint >= 0;
1509}
1510
1511/* check if the source address of the front connection is local to the system
1512 * or not.
1513 */
1514int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1515{
1516 struct connection *conn = objt_conn(smp->sess->origin);
1517 struct listener *li = smp->sess->listener;
1518
1519 if (!conn)
1520 return 0;
1521
1522 conn_get_from_addr(conn);
1523 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
1524 return 0;
1525
1526 smp->data.type = SMP_T_BOOL;
1527 smp->flags = 0;
1528 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.from);
1529 return smp->data.u.sint >= 0;
1530}
1531
Willy Tarreaua5e37562011-12-16 17:06:15 +01001532/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001533static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001534smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001535{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001536 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001537
1538 if (!cli_conn)
1539 return 0;
1540
1541 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001542
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001543 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001544 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001545 return 0;
1546
Willy Tarreau37406352012-04-23 16:16:37 +02001547 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001548 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001549}
1550
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001551#ifdef TCP_INFO
1552
Joseph Herlanta6331472018-11-25 12:59:12 -08001553/* Returns some tcp_info data if it's available. "dir" must be set to 0 if
1554 * the client connection is required, otherwise it is set to 1. "val" represents
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001555 * the required value. Use 0 for rtt and 1 for rttavg. "unit" is the expected unit
1556 * by default, the rtt is in us. Id "unit" is set to 0, the unit is us, if it is
Joseph Herlanta6331472018-11-25 12:59:12 -08001557 * set to 1, the units are milliseconds.
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001558 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1559 */
1560static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1561 int dir, int val)
1562{
1563 struct connection *conn;
1564 struct tcp_info info;
1565 socklen_t optlen;
1566
1567 /* strm can be null. */
1568 if (!smp->strm)
1569 return 0;
1570
1571 /* get the object associated with the stream interface.The
1572 * object can be other thing than a connection. For example,
1573 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001574 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001575 if (!conn)
1576 return 0;
1577
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001578 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001579 syscal can fail. */
1580 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001581 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001582 return 0;
1583
1584 /* extract the value. */
1585 smp->data.type = SMP_T_SINT;
1586 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001587 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1588 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1589#if defined(__linux__)
1590 /* these ones are common to all Linux versions */
1591 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1592 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1593 case 4: smp->data.u.sint = info.tcpi_lost; break;
1594 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1595 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1596 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1597#elif defined(__FreeBSD__) || defined(__NetBSD__)
1598 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1599 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1600 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1601 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1602 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1603 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1604 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1605#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001606 default: return 0;
1607 }
1608
1609 /* Convert the value as expected. */
1610 if (args) {
1611 if (args[0].type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001612 if (strcmp(args[0].data.str.area, "us") == 0) {
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001613 /* Do nothing. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001614 } else if (strcmp(args[0].data.str.area, "ms") == 0) {
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001615 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1616 } else
1617 return 0;
1618 } else if (args[0].type == ARGT_STOP) {
1619 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1620 } else
1621 return 0;
1622 }
1623
1624 return 1;
1625}
1626
1627/* get the mean rtt of a client connexion */
1628static int
1629smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1630{
1631 if (!get_tcp_info(args, smp, 0, 0))
1632 return 0;
1633 return 1;
1634}
1635
1636/* get the variance of the mean rtt of a client connexion */
1637static int
1638smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1639{
1640 if (!get_tcp_info(args, smp, 0, 1))
1641 return 0;
1642 return 1;
1643}
Joe Williams30fcd392016-08-10 07:06:44 -07001644
1645#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1646
1647/* get the unacked counter on a client connexion */
1648static int
1649smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1650{
1651 if (!get_tcp_info(args, smp, 0, 2))
1652 return 0;
1653 return 1;
1654}
1655
1656/* get the sacked counter on a client connexion */
1657static int
1658smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1659{
1660 if (!get_tcp_info(args, smp, 0, 3))
1661 return 0;
1662 return 1;
1663}
1664
1665/* get the lost counter on a client connexion */
1666static int
1667smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1668{
1669 if (!get_tcp_info(args, smp, 0, 4))
1670 return 0;
1671 return 1;
1672}
1673
1674/* get the retrans counter on a client connexion */
1675static int
1676smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1677{
1678 if (!get_tcp_info(args, smp, 0, 5))
1679 return 0;
1680 return 1;
1681}
1682
1683/* get the fackets counter on a client connexion */
1684static int
1685smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1686{
1687 if (!get_tcp_info(args, smp, 0, 6))
1688 return 0;
1689 return 1;
1690}
1691
1692/* get the reordering counter on a client connexion */
1693static int
1694smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1695{
1696 if (!get_tcp_info(args, smp, 0, 7))
1697 return 0;
1698 return 1;
1699}
1700#endif // linux || freebsd || netbsd
1701#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001702
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001703#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001704/* parse the "v4v6" bind keyword */
1705static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1706{
1707 struct listener *l;
1708
1709 list_for_each_entry(l, &conf->listeners, by_bind) {
1710 if (l->addr.ss_family == AF_INET6)
1711 l->options |= LI_O_V4V6;
1712 }
1713
1714 return 0;
1715}
1716
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001717/* parse the "v6only" bind keyword */
1718static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1719{
1720 struct listener *l;
1721
1722 list_for_each_entry(l, &conf->listeners, by_bind) {
1723 if (l->addr.ss_family == AF_INET6)
1724 l->options |= LI_O_V6ONLY;
1725 }
1726
1727 return 0;
1728}
1729#endif
1730
Pieter Baauwd551fb52013-05-08 22:49:23 +02001731#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001732/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001733static 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 +02001734{
1735 struct listener *l;
1736
Willy Tarreau4348fad2012-09-20 16:48:07 +02001737 list_for_each_entry(l, &conf->listeners, by_bind) {
1738 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1739 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001740 }
1741
Willy Tarreau44791242012-09-12 23:27:21 +02001742 return 0;
1743}
1744#endif
1745
1746#ifdef TCP_DEFER_ACCEPT
1747/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001748static 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 +02001749{
1750 struct listener *l;
1751
Willy Tarreau4348fad2012-09-20 16:48:07 +02001752 list_for_each_entry(l, &conf->listeners, by_bind) {
1753 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1754 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001755 }
1756
Willy Tarreau44791242012-09-12 23:27:21 +02001757 return 0;
1758}
1759#endif
1760
Willy Tarreau1c862c52012-10-05 16:21:00 +02001761#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001762/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001763static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1764{
1765 struct listener *l;
1766
1767 list_for_each_entry(l, &conf->listeners, by_bind) {
1768 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1769 l->options |= LI_O_TCP_FO;
1770 }
1771
1772 return 0;
1773}
1774#endif
1775
Willy Tarreau44791242012-09-12 23:27:21 +02001776#ifdef TCP_MAXSEG
1777/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001778static 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 +02001779{
1780 struct listener *l;
1781 int mss;
1782
Willy Tarreau44791242012-09-12 23:27:21 +02001783 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001784 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001785 return ERR_ALERT | ERR_FATAL;
1786 }
1787
1788 mss = atoi(args[cur_arg + 1]);
1789 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001790 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001791 return ERR_ALERT | ERR_FATAL;
1792 }
1793
Willy Tarreau4348fad2012-09-20 16:48:07 +02001794 list_for_each_entry(l, &conf->listeners, by_bind) {
1795 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1796 l->maxseg = mss;
1797 }
Willy Tarreau44791242012-09-12 23:27:21 +02001798
1799 return 0;
1800}
1801#endif
1802
Willy Tarreau2af207a2015-02-04 00:45:58 +01001803#ifdef TCP_USER_TIMEOUT
1804/* parse the "tcp-ut" bind keyword */
1805static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1806{
1807 const char *ptr = NULL;
1808 struct listener *l;
1809 unsigned int timeout;
1810
1811 if (!*args[cur_arg + 1]) {
1812 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1813 return ERR_ALERT | ERR_FATAL;
1814 }
1815
1816 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001817 if (ptr == PARSE_TIME_OVER) {
1818 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1819 args[cur_arg+1], args[cur_arg]);
1820 return ERR_ALERT | ERR_FATAL;
1821 }
1822 else if (ptr == PARSE_TIME_UNDER) {
1823 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1824 args[cur_arg+1], args[cur_arg]);
1825 return ERR_ALERT | ERR_FATAL;
1826 }
1827 else if (ptr) {
Willy Tarreau2af207a2015-02-04 00:45:58 +01001828 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1829 return ERR_ALERT | ERR_FATAL;
1830 }
1831
1832 list_for_each_entry(l, &conf->listeners, by_bind) {
1833 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1834 l->tcp_ut = timeout;
1835 }
1836
1837 return 0;
1838}
1839#endif
1840
Willy Tarreau44791242012-09-12 23:27:21 +02001841#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001842/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001843static 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 +02001844{
1845 struct listener *l;
1846
Willy Tarreau44791242012-09-12 23:27:21 +02001847 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001848 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001849 return ERR_ALERT | ERR_FATAL;
1850 }
1851
Willy Tarreau4348fad2012-09-20 16:48:07 +02001852 list_for_each_entry(l, &conf->listeners, by_bind) {
1853 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1854 l->interface = strdup(args[cur_arg + 1]);
1855 }
Willy Tarreau44791242012-09-12 23:27:21 +02001856
Willy Tarreau44791242012-09-12 23:27:21 +02001857 return 0;
1858}
1859#endif
1860
Willy Tarreaue5733232019-05-22 19:24:06 +02001861#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001862/* parse the "namespace" bind keyword */
1863static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1864{
1865 struct listener *l;
1866 char *namespace = NULL;
1867
1868 if (!*args[cur_arg + 1]) {
1869 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1870 return ERR_ALERT | ERR_FATAL;
1871 }
1872 namespace = args[cur_arg + 1];
1873
1874 list_for_each_entry(l, &conf->listeners, by_bind) {
1875 l->netns = netns_store_lookup(namespace, strlen(namespace));
1876
1877 if (l->netns == NULL)
1878 l->netns = netns_store_insert(namespace);
1879
1880 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001881 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001882 return ERR_ALERT | ERR_FATAL;
1883 }
1884 }
1885 return 0;
1886}
1887#endif
1888
Willy Tarreau163d4622015-10-13 16:16:41 +02001889#ifdef TCP_USER_TIMEOUT
1890/* parse the "tcp-ut" server keyword */
1891static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1892{
1893 const char *ptr = NULL;
1894 unsigned int timeout;
1895
1896 if (!*args[*cur_arg + 1]) {
1897 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1898 return ERR_ALERT | ERR_FATAL;
1899 }
1900
1901 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001902 if (ptr == PARSE_TIME_OVER) {
1903 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1904 args[*cur_arg+1], args[*cur_arg]);
1905 return ERR_ALERT | ERR_FATAL;
1906 }
1907 else if (ptr == PARSE_TIME_UNDER) {
1908 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1909 args[*cur_arg+1], args[*cur_arg]);
1910 return ERR_ALERT | ERR_FATAL;
1911 }
1912 else if (ptr) {
Willy Tarreau163d4622015-10-13 16:16:41 +02001913 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1914 return ERR_ALERT | ERR_FATAL;
1915 }
1916
1917 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1918 newsrv->tcp_ut = timeout;
1919
1920 return 0;
1921}
1922#endif
1923
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001924
Willy Tarreau4a129812012-04-25 17:31:42 +02001925/* Note: must not be declared <const> as its list will be overwritten.
1926 * Note: fetches that may return multiple types must be declared as the lowest
1927 * common denominator, the type that can be casted into all other ones. For
1928 * instance v4/v6 must be declared v4.
1929 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001930static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001931 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001932 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001933 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001934 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001935 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001936 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001937#ifdef TCP_INFO
Joe Williams30fcd392016-08-10 07:06:44 -07001938 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1939 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1940#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1941 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1942 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1943 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1944 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1945 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1946 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1947#endif // linux || freebsd || netbsd
1948#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001949 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001950}};
1951
Willy Tarreau0108d902018-11-25 19:14:37 +01001952INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1953
Willy Tarreau44791242012-09-12 23:27:21 +02001954/************************************************************************/
1955/* All supported bind keywords must be declared here. */
1956/************************************************************************/
1957
1958/* Note: must not be declared <const> as its list will be overwritten.
1959 * Please take care of keeping this list alphabetically sorted, doing so helps
1960 * all code contributors.
1961 * Optional keywords are also declared with a NULL ->parse() function so that
1962 * the config parser can report an appropriate error when a known keyword was
1963 * not enabled.
1964 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001965static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001966#ifdef TCP_DEFER_ACCEPT
1967 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1968#endif
1969#ifdef SO_BINDTODEVICE
1970 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1971#endif
1972#ifdef TCP_MAXSEG
1973 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1974#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001975#ifdef TCP_USER_TIMEOUT
1976 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1977#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001978#ifdef TCP_FASTOPEN
1979 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1980#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001981#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001982 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1983#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001984#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001985 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001986 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1987#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02001988#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001989 { "namespace", bind_parse_namespace, 1 },
1990#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001991 /* the versions with the NULL parse function*/
1992 { "defer-accept", NULL, 0 },
1993 { "interface", NULL, 1 },
1994 { "mss", NULL, 1 },
1995 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001996 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001997 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001998 { NULL, NULL, 0 },
1999}};
2000
Willy Tarreau0108d902018-11-25 19:14:37 +01002001INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
2002
Willy Tarreau163d4622015-10-13 16:16:41 +02002003static struct srv_kw_list srv_kws = { "TCP", { }, {
2004#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002005 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02002006#endif
2007 { NULL, NULL, 0 },
2008}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02002009
Willy Tarreau0108d902018-11-25 19:14:37 +01002010INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
2011
Willy Tarreau2d392c22015-08-24 01:43:45 +02002012static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand13e9b0c2016-05-25 02:34:07 +02002013 { "set-src", tcp_parse_set_src_dst },
2014 { "set-src-port", tcp_parse_set_src_dst },
2015 { "set-dst" , tcp_parse_set_src_dst },
2016 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002017 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002018 { /* END */ }
2019}};
2020
Willy Tarreau0108d902018-11-25 19:14:37 +01002021INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions);
2022
Willy Tarreau620408f2016-10-21 16:37:51 +02002023static struct action_kw_list tcp_req_sess_actions = {ILH, {
Willy Tarreau620408f2016-10-21 16:37:51 +02002024 { "set-src", tcp_parse_set_src_dst },
2025 { "set-src-port", tcp_parse_set_src_dst },
2026 { "set-dst" , tcp_parse_set_src_dst },
2027 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002028 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau620408f2016-10-21 16:37:51 +02002029 { /* END */ }
2030}};
2031
Willy Tarreau0108d902018-11-25 19:14:37 +01002032INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions);
2033
Willy Tarreau2d392c22015-08-24 01:43:45 +02002034static struct action_kw_list tcp_req_cont_actions = {ILH, {
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002035 { "set-dst" , tcp_parse_set_src_dst },
2036 { "set-dst-port", tcp_parse_set_src_dst },
2037 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002038 { /* END */ }
2039}};
2040
Willy Tarreau0108d902018-11-25 19:14:37 +01002041INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2042
Willy Tarreau2d392c22015-08-24 01:43:45 +02002043static struct action_kw_list tcp_res_cont_actions = {ILH, {
2044 { "silent-drop", tcp_parse_silent_drop },
2045 { /* END */ }
2046}};
2047
Willy Tarreau0108d902018-11-25 19:14:37 +01002048INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
2049
Willy Tarreau2d392c22015-08-24 01:43:45 +02002050static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002051 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002052 { "set-src", tcp_parse_set_src_dst },
2053 { "set-src-port", tcp_parse_set_src_dst },
2054 { "set-dst", tcp_parse_set_src_dst },
2055 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002056 { /* END */ }
2057}};
2058
Willy Tarreau0108d902018-11-25 19:14:37 +01002059INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2060
Willy Tarreau2d392c22015-08-24 01:43:45 +02002061static struct action_kw_list http_res_actions = {ILH, {
2062 { "silent-drop", tcp_parse_silent_drop },
2063 { /* END */ }
2064}};
2065
Willy Tarreau0108d902018-11-25 19:14:37 +01002066INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002067
Willy Tarreau80713382018-11-26 10:19:54 +01002068REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
Willy Tarreauba962912016-12-21 18:55:02 +01002069#if defined(IP_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002070 " IP_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002071#endif
2072#if defined(IPV6_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002073 " IPV6_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002074#endif
2075#if defined(IP_FREEBIND)
Willy Tarreau80713382018-11-26 10:19:54 +01002076 " IP_FREEBIND"
Willy Tarreauba962912016-12-21 18:55:02 +01002077#endif
2078#if defined(IP_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002079 " IP_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002080#endif
2081#if defined(IPV6_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002082 " IPV6_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002083#endif
2084#if defined(SO_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002085 " SO_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002086#endif
Willy Tarreau80713382018-11-26 10:19:54 +01002087 "");
Willy Tarreaue6b98942007-10-29 01:09:36 +01002088
2089
2090/*
2091 * Local variables:
2092 * c-indent-level: 8
2093 * c-basic-offset: 8
2094 * End:
2095 */