blob: f379fd1402563ebebfa4706189289d42a6bc2eb4 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Baptiste Assmann39a5f222016-08-08 14:12:08 +020013/* this is to have tcp_info defined on systems using musl
14 * library, such as Alpine Linux
15 */
16#define _GNU_SOURCE
17
Willy Tarreaue6b98942007-10-29 01:09:36 +010018#include <ctype.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <time.h>
25
26#include <sys/param.h>
27#include <sys/socket.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <sys/un.h>
31
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040032#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020033#include <netinet/in.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040034
Willy Tarreaue6b98942007-10-29 01:09:36 +010035#include <common/compat.h>
36#include <common/config.h>
37#include <common/debug.h>
38#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010039#include <common/initcall.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010040#include <common/mini-clist.h>
41#include <common/standard.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010042#include <common/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010043
Willy Tarreau39713102016-11-25 15:49:32 +010044#include <types/action.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020045#include <types/connection.h>
Willy Tarreau39713102016-11-25 15:49:32 +010046#include <types/global.h>
47#include <types/stream.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010048
Willy Tarreau9fcb9842012-04-20 14:45:49 +020049#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020050#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020051#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020052#include <proto/fd.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020053#include <proto/http_rules.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020054#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020055#include <proto/log.h>
56#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020057#include <proto/protocol.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020058#include <proto/proto_http.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010059#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020060#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020061#include <proto/sample.h>
Willy Tarreau163d4622015-10-13 16:16:41 +020062#include <proto/server.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020063#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010064#include <proto/tcp_rules.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010065
Emeric Bruncf20bf12010-10-22 16:06:11 +020066static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
67static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020068static void tcpv4_add_listener(struct listener *listener, int port);
69static void tcpv6_add_listener(struct listener *listener, int port);
Willy Tarreaue6b98942007-10-29 01:09:36 +010070
71/* Note: must not be declared <const> as its list will be overwritten */
72static struct protocol proto_tcpv4 = {
73 .name = "tcpv4",
74 .sock_domain = AF_INET,
75 .sock_type = SOCK_STREAM,
76 .sock_prot = IPPROTO_TCP,
77 .sock_family = AF_INET,
78 .sock_addrlen = sizeof(struct sockaddr_in),
79 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020080 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020081 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020082 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010083 .bind_all = tcp_bind_listeners,
84 .unbind_all = unbind_all_listeners,
85 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020086 .get_src = tcp_get_src,
87 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +020088 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020089 .add = tcpv4_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010090 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
91 .nb_listeners = 0,
92};
93
Willy Tarreau0108d902018-11-25 19:14:37 +010094INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4);
95
Willy Tarreaue6b98942007-10-29 01:09:36 +010096/* Note: must not be declared <const> as its list will be overwritten */
97static struct protocol proto_tcpv6 = {
98 .name = "tcpv6",
99 .sock_domain = AF_INET6,
100 .sock_type = SOCK_STREAM,
101 .sock_prot = IPPROTO_TCP,
102 .sock_family = AF_INET6,
103 .sock_addrlen = sizeof(struct sockaddr_in6),
104 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200105 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200106 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200107 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100108 .bind_all = tcp_bind_listeners,
109 .unbind_all = unbind_all_listeners,
110 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200111 .get_src = tcp_get_src,
112 .get_dst = tcp_get_dst,
Willy Tarreau092d8652014-07-07 20:22:12 +0200113 .pause = tcp_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200114 .add = tcpv6_add_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100115 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
116 .nb_listeners = 0,
117};
118
Willy Tarreau0108d902018-11-25 19:14:37 +0100119INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6);
120
Olivier Houchard153659f2017-04-05 22:39:56 +0200121/* Default TCP parameters, got by opening a temporary TCP socket. */
122#ifdef TCP_MAXSEG
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100123static THREAD_LOCAL int default_tcp_maxseg = -1;
124static THREAD_LOCAL int default_tcp6_maxseg = -1;
Olivier Houchard153659f2017-04-05 22:39:56 +0200125#endif
126
David du Colombier6f5ccb12011-03-10 22:26:24 +0100127/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100128 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
129 * - 0 : ignore remote address (may even be a NULL pointer)
130 * - 1 : use provided address
131 * - 2 : use provided port
132 * - 3 : use both
133 *
134 * The function supports multiple foreign binding methods :
135 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100136 * The second one can be used as a fallback for the first one.
137 * This function returns 0 when everything's OK, 1 if it could not bind, to the
138 * local address, 2 if it could not bind to the foreign address.
139 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100140int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100141{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100142 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100143 int foreign_ok = 0;
144 int ret;
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100145 static THREAD_LOCAL int ip_transp_working = 1;
146 static THREAD_LOCAL int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200147
David du Colombier65c17962012-07-13 14:34:59 +0200148 switch (local->ss_family) {
149 case AF_INET:
150 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200151 /* This deserves some explanation. Some platforms will support
152 * multiple combinations of certain methods, so we try the
153 * supported ones until one succeeds.
154 */
155 if (0
156#if defined(IP_TRANSPARENT)
157 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
158#endif
159#if defined(IP_FREEBIND)
160 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
161#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200162#if defined(IP_BINDANY)
163 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
164#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200165#if defined(SO_BINDANY)
166 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
167#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200168 )
David du Colombier65c17962012-07-13 14:34:59 +0200169 foreign_ok = 1;
170 else
171 ip_transp_working = 0;
172 }
173 break;
174 case AF_INET6:
175 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200176 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200177#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200178 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
179#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100180#if defined(IP_FREEBIND)
181 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
182#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200183#if defined(IPV6_BINDANY)
184 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
185#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200186#if defined(SO_BINDANY)
187 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
188#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200189 )
David du Colombier65c17962012-07-13 14:34:59 +0200190 foreign_ok = 1;
191 else
192 ip6_transp_working = 0;
193 }
194 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100195 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200196
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100197 if (flags) {
198 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200199 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100200 switch (remote->ss_family) {
201 case AF_INET:
202 if (flags & 1)
203 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
204 if (flags & 2)
205 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
206 break;
207 case AF_INET6:
208 if (flags & 1)
209 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
210 if (flags & 2)
211 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
212 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100213 default:
214 /* we don't want to try to bind to an unknown address family */
215 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100216 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100217 }
218
Simon Hormande072bd2011-06-24 15:11:37 +0900219 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100220 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200221 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200222 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
223 if (ret < 0)
224 return 2;
225 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100226 }
227 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200228 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200229 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
230 if (ret < 0)
231 return 1;
232 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100233 }
234
235 if (!flags)
236 return 0;
237
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100238 if (!foreign_ok)
239 /* we could not bind to a foreign address */
240 return 2;
241
242 return 0;
243}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100244
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100245static int create_server_socket(struct connection *conn)
246{
Willy Tarreau529c1392014-12-24 13:47:55 +0100247 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100248
Willy Tarreaue5733232019-05-22 19:24:06 +0200249#ifdef USE_NS
Willy Tarreau529c1392014-12-24 13:47:55 +0100250 if (objt_server(conn->target)) {
251 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
252 ns = conn->proxy_netns;
253 else
254 ns = __objt_server(conn->target)->netns;
255 }
256#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100257 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
258}
Willy Tarreau9650f372009-08-16 14:02:45 +0200259
260/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200261 * This function initiates a TCP connection establishment to the target assigned
262 * to connection <conn> using (si->{target,addr.to}). A source address may be
263 * pointed to by conn->addr.from in case of transparent proxying. Normal source
264 * bind addresses are still determined locally (due to the possible need of a
265 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100266 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100267 * supported. The <data> parameter is a boolean indicating whether there are data
268 * waiting for being sent or not, in order to adjust data write polling and on
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200269 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
270 * allows the caller to force using a delayed ACK when establishing the connection
Willy Tarreauf0837b22012-11-24 10:24:27 +0100271 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200272 * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data
273 * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200274 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200275 * Note that a pending send_proxy message accounts for data.
276 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200277 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200278 * - SF_ERR_NONE if everything's OK
279 * - SF_ERR_SRVTO if there are no more servers
280 * - SF_ERR_SRVCL if the connection was refused by the server
281 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
282 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
283 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100284 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100285 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200286 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100287 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200288 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100289
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200290int tcp_connect_server(struct connection *conn, int flags)
Willy Tarreau9650f372009-08-16 14:02:45 +0200291{
292 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100293 struct server *srv;
294 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100295 struct conn_src *src;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100296 int use_fastopen = 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800297 struct sockaddr_storage *addr;
Willy Tarreauac825402011-03-04 22:04:29 +0100298
Olivier Houchard637b6952018-11-23 14:23:07 +0100299 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100300
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100301 switch (obj_type(conn->target)) {
302 case OBJ_TYPE_PROXY:
303 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100304 srv = NULL;
305 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100306 case OBJ_TYPE_SERVER:
307 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100308 be = srv->proxy;
Willy Tarreau034c88c2017-01-23 23:36:45 +0100309 /* Make sure we check that we have data before activating
310 * TFO, or we could trigger a kernel issue whereby after
311 * a successful connect() == 0, any subsequent connect()
312 * will return EINPROGRESS instead of EISCONN.
313 */
314 use_fastopen = (srv->flags & SRV_F_FASTOPEN) &&
315 ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) ==
316 (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA));
Willy Tarreauac825402011-03-04 22:04:29 +0100317 break;
318 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100319 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200320 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100321 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200322
Willy Tarreau585744b2017-08-24 14:31:19 +0200323 fd = conn->handle.fd = create_server_socket(conn);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100324
325 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200326 qfprintf(stderr, "Cannot get a server socket.\n");
327
Willy Tarreau9ce70132014-01-24 16:08:19 +0100328 if (errno == ENFILE) {
329 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200330 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100331 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
332 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100333 }
334 else if (errno == EMFILE) {
335 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200336 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100337 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
338 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100339 }
340 else if (errno == ENOBUFS || errno == ENOMEM) {
341 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200342 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100343 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
344 be->id, global.maxsock);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100345 }
346 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
347 conn->err_code = CO_ER_NOPROTO;
348 }
349 else
350 conn->err_code = CO_ER_SOCK_ERR;
351
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100353 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200354 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200355 }
356
357 if (fd >= global.maxsock) {
358 /* do not log anything there, it's a normal condition when this option
359 * is used to serialize connections to a server !
360 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100361 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau9650f372009-08-16 14:02:45 +0200362 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100363 conn->err_code = CO_ER_CONF_FDLIM;
364 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200365 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200366 }
367
368 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900369 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200370 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
371 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100372 conn->err_code = CO_ER_SOCK_ERR;
373 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200374 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 }
376
William Lallemandc03eb012018-11-27 12:02:37 +0100377 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
378 ha_alert("Cannot set CLOEXEC on client socket.\n");
379 close(fd);
380 conn->err_code = CO_ER_SOCK_ERR;
381 conn->flags |= CO_FL_ERROR;
382 return SF_ERR_INTERNAL;
383 }
384
Willy Tarreau9650f372009-08-16 14:02:45 +0200385 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900386 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200387
Willy Tarreau9650f372009-08-16 14:02:45 +0200388 /* allow specific binding :
389 * - server-specific at first
390 * - proxy-specific next
391 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100392 if (srv && srv->conn_src.opts & CO_SRC_BIND)
393 src = &srv->conn_src;
394 else if (be->conn_src.opts & CO_SRC_BIND)
395 src = &be->conn_src;
396 else
397 src = NULL;
398
399 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200400 int ret, flags = 0;
401
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200402 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100403 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100404 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200405 conn->flags |= CO_FL_PRIVATE;
406 /* fall through */
407 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200408 flags = 3;
409 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100410 case CO_SRC_TPROXY_CIP:
411 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200412 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200413 flags = 1;
414 break;
415 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200416 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200417
Willy Tarreau9650f372009-08-16 14:02:45 +0200418#ifdef SO_BINDTODEVICE
419 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100420 if (src->iface_name)
421 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200422#endif
423
Willy Tarreaua4380b42012-12-08 22:49:11 +0100424 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200425 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100426 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200427
428 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200429 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200430
431 do {
432 /* note: in case of retry, we may have to release a previously
433 * allocated port, hence this loop's construct.
434 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200435 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
436 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200437
438 if (!attempts)
439 break;
440 attempts--;
441
Willy Tarreaua4380b42012-12-08 22:49:11 +0100442 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100443 if (!fdinfo[fd].local_port) {
444 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100446 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200447
Willy Tarreaua4380b42012-12-08 22:49:11 +0100448 fdinfo[fd].port_range = src->sport_range;
449 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200450
Willy Tarreaua4380b42012-12-08 22:49:11 +0100451 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100452 if (ret != 0)
453 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 } while (ret != 0); /* binding NOK */
455 }
456 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000457#ifdef IP_BIND_ADDRESS_NO_PORT
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100458 static THREAD_LOCAL int bind_address_no_port = 1;
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000459 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
460#endif
Willy Tarreaua4380b42012-12-08 22:49:11 +0100461 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100462 if (ret != 0)
463 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200464 }
465
Willy Tarreaua4380b42012-12-08 22:49:11 +0100466 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200467 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
468 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 close(fd);
470
Willy Tarreau9650f372009-08-16 14:02:45 +0200471 if (ret == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100472 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
473 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200474 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100475 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200476 be->id);
477 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100478 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
479 be->id);
Willy Tarreau9650f372009-08-16 14:02:45 +0200480 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100481 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200482 be->id);
483 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100484 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200485 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200486 }
487 }
488
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400489#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200490 /* disabling tcp quick ack now allows the first request to leave the
491 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100492 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200493 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200494 if (flags & (CONNECT_DELACK_ALWAYS) ||
495 ((flags & CONNECT_DELACK_SMART_CONNECT ||
496 (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) &&
497 (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900498 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200499#endif
500
Willy Tarreau163d4622015-10-13 16:16:41 +0200501#ifdef TCP_USER_TIMEOUT
502 /* there is not much more we can do here when it fails, it's still minor */
503 if (srv && srv->tcp_ut)
504 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
505#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100506
507 if (use_fastopen) {
508#if defined(TCP_FASTOPEN_CONNECT)
509 setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
510#endif
511 }
Willy Tarreaue803de22010-01-21 17:43:04 +0100512 if (global.tune.server_sndbuf)
513 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
514
515 if (global.tune.server_rcvbuf)
516 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
517
Alexander Liu2a54bb72019-05-22 19:44:48 +0800518 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : &conn->addr.to;
519 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreau819efbf2017-01-25 14:12:22 +0100520 if (errno == EINPROGRESS || errno == EALREADY) {
521 /* common case, let's wait for connect status */
522 conn->flags |= CO_FL_WAIT_L4_CONN;
523 }
524 else if (errno == EISCONN) {
525 /* should normally not happen but if so, indicates that it's OK */
526 conn->flags &= ~CO_FL_WAIT_L4_CONN;
527 }
528 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200529 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100530 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200531 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100532 conn->err_code = CO_ER_FREE_PORTS;
533 }
534 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200535 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100536 conn->err_code = CO_ER_ADDR_INUSE;
537 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200538
Willy Tarreaub1719512012-12-08 23:03:28 +0100539 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200540 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
541 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200542 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100543 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100544 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200545 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200546 } else if (errno == ETIMEDOUT) {
547 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200548 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
549 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200550 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100551 conn->err_code = CO_ER_SOCK_ERR;
552 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200553 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200554 } else {
555 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
556 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200557 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
558 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200559 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100560 conn->err_code = CO_ER_SOCK_ERR;
561 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200562 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200563 }
564 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100565 else {
566 /* connect() == 0, this is great! */
567 conn->flags &= ~CO_FL_WAIT_L4_CONN;
568 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200569
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100570 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200571
Willy Tarreauf79c8172013-10-21 16:30:56 +0200572 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100573 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200574
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200575 if (conn_xprt_init(conn) < 0) {
Willy Tarreau3f2770b2017-10-05 18:01:29 +0200576 conn_full_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100577 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200578 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200579 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200580
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200581 conn_xprt_want_send(conn); /* for connect status, proxy protocol or SSL */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200582 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200583}
584
585
Willy Tarreau59b94792012-05-11 16:16:40 +0200586/*
587 * Retrieves the source address for the socket <fd>, with <dir> indicating
588 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
589 * success, -1 in case of error. The socket's source address is stored in
590 * <sa> for <salen> bytes.
591 */
592int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
593{
594 if (dir)
595 return getsockname(fd, sa, &salen);
596 else
597 return getpeername(fd, sa, &salen);
598}
599
600
601/*
602 * Retrieves the original destination address for the socket <fd>, with <dir>
603 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
604 * listener, if the original destination address was translated, the original
605 * address is retrieved. It returns 0 in case of success, -1 in case of error.
606 * The socket's source address is stored in <sa> for <salen> bytes.
607 */
608int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
609{
610 if (dir)
611 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100612 else {
613 int ret = getsockname(fd, sa, &salen);
614
615 if (ret < 0)
616 return ret;
617
Willy Tarreaue5733232019-05-22 19:24:06 +0200618#if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100619 /* For TPROXY and Netfilter's NAT, we can retrieve the original
620 * IPv4 address before DNAT/REDIRECT. We must not do that with
621 * other families because v6-mapped IPv4 addresses are still
622 * reported as v4.
623 */
624 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
625 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
626 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200627#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100628 return ret;
629 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200630}
631
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200632/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100633 * and we have nothing to send. It updates the FD polling status. It returns 0
634 * if it fails in a fatal way or needs to poll to go further, otherwise it
635 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
636 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
637 * errno. The error checking is done in two passes in order to limit the number
638 * of syscalls in the normal case :
639 * - if POLL_ERR was reported by the poller, we check for a pending error on
640 * the socket before proceeding. If found, it's assigned to errno so that
641 * upper layers can see it.
642 * - otherwise connect() is used to check the connection state again, since
643 * the getsockopt return cannot reliably be used to know if the connection
644 * is still pending or ready. This one may often return an error as well,
645 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200646 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200647int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200648{
Alexander Liu2a54bb72019-05-22 19:44:48 +0800649 struct sockaddr_storage *addr;
Willy Tarreau585744b2017-08-24 14:31:19 +0200650 int fd = conn->handle.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100651 socklen_t lskerr;
652 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200653
Willy Tarreau80184712012-07-06 14:54:49 +0200654 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200655 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200656
Willy Tarreau3c728722014-01-23 13:50:42 +0100657 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200658 return 0;
659
Willy Tarreau80184712012-07-06 14:54:49 +0200660 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200661 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200662
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100663 if (!fd_send_ready(fd))
664 return 0;
665
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100666 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
667 * without FD_POLL_IN also indicates a hangup without input data meaning
668 * there was no connection.
669 */
670 if (fdtab[fd].ev & FD_POLL_ERR ||
671 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
672 skerr = 0;
673 lskerr = sizeof(skerr);
674 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
675 errno = skerr;
676 if (errno == EAGAIN)
677 errno = 0;
678 if (errno)
679 goto out_error;
680 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200681
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100682 /* Use connect() to check the state of the socket. This has the
683 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200684 * - error
685 * - connecting (EALREADY, EINPROGRESS)
686 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200687 */
Alexander Liu2a54bb72019-05-22 19:44:48 +0800688 addr = &conn->addr.to;
689 if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER)
690 addr = &objt_server(conn->target)->socks4_addr;
691
692 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200693 if (errno == EALREADY || errno == EINPROGRESS) {
Olivier Houchard7b3a79f2019-06-06 18:15:01 +0200694 __conn_xprt_want_send(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100695 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200696 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200697 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200698
Willy Tarreau2c6be842012-07-06 17:12:34 +0200699 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200700 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200701
Willy Tarreau2c6be842012-07-06 17:12:34 +0200702 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200703 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200704
Willy Tarreau076be252012-07-06 16:02:29 +0200705 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200706 * forward the event to the transport layer which will notify the
707 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200708 */
Willy Tarreau80184712012-07-06 14:54:49 +0200709 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200710 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200711
712 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200713 /* Write error on the file descriptor. Report it to the connection
714 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200715 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100716 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100717 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200718 __conn_xprt_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200719 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200720}
721
Olivier Houchard153659f2017-04-05 22:39:56 +0200722/* XXX: Should probably be elsewhere */
723static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b)
724{
725 if (a->ss_family != b->ss_family) {
726 return (-1);
727 }
728 switch (a->ss_family) {
729 case AF_INET:
730 {
731 struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b;
732 if (a4->sin_port != b4->sin_port)
733 return (-1);
734 return (memcmp(&a4->sin_addr, &b4->sin_addr,
735 sizeof(a4->sin_addr)));
736 }
737 case AF_INET6:
738 {
739 struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b;
740 if (a6->sin6_port != b6->sin6_port)
741 return (-1);
742 return (memcmp(&a6->sin6_addr, &b6->sin6_addr,
743 sizeof(a6->sin6_addr)));
744 }
745 default:
746 return (-1);
747 }
748
749}
750
751#define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6)
752/* When binding the listeners, check if a socket has been sent to us by the
753 * previous process that we could reuse, instead of creating a new one.
754 */
755static int tcp_find_compatible_fd(struct listener *l)
756{
757 struct xfer_sock_list *xfer_sock = xfer_sock_list;
758 int ret = -1;
759
760 while (xfer_sock) {
761 if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) {
762 if ((l->interface == NULL && xfer_sock->iface == NULL) ||
763 (l->interface != NULL && xfer_sock->iface != NULL &&
764 !strcmp(l->interface, xfer_sock->iface))) {
765 if ((l->options & LI_MANDATORY_FLAGS) ==
766 (xfer_sock->options & LI_MANDATORY_FLAGS)) {
767 if ((xfer_sock->namespace == NULL &&
768 l->netns == NULL)
Willy Tarreaue5733232019-05-22 19:24:06 +0200769#ifdef USE_NS
Olivier Houchard153659f2017-04-05 22:39:56 +0200770 || (xfer_sock->namespace != NULL &&
771 l->netns != NULL &&
772 !strcmp(xfer_sock->namespace,
773 l->netns->node.key))
774#endif
775 ) {
776 break;
777 }
778
779 }
780 }
781 }
782 xfer_sock = xfer_sock->next;
783 }
784 if (xfer_sock != NULL) {
785 ret = xfer_sock->fd;
786 if (xfer_sock == xfer_sock_list)
787 xfer_sock_list = xfer_sock->next;
788 if (xfer_sock->prev)
789 xfer_sock->prev->next = xfer_sock->next;
790 if (xfer_sock->next)
791 xfer_sock->next->prev = xfer_sock->prev;
792 free(xfer_sock->iface);
793 free(xfer_sock->namespace);
794 free(xfer_sock);
795 }
796 return ret;
797}
798#undef L1_MANDATORY_FLAGS
Willy Tarreau59b94792012-05-11 16:16:40 +0200799
Willy Tarreaue6b98942007-10-29 01:09:36 +0100800/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100801 * an error message in <errmsg> if the message is at most <errlen> bytes long
802 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
803 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100804 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
805 * was alright and that no message was returned. ERR_RETRYABLE means that an
806 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700807 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100808 * the meaning of the error, but just indicate that a message is present which
809 * should be displayed with the respective level. Last, ERR_ABORT indicates
810 * that it's pointless to try to start other listeners. No error message is
811 * returned if errlen is NULL.
812 */
813int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
814{
815 __label__ tcp_return, tcp_close_return;
816 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100817 int ext, ready;
818 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100819 const char *msg = NULL;
Olivier Houchard153659f2017-04-05 22:39:56 +0200820#ifdef TCP_MAXSEG
821
822 /* Create a temporary TCP socket to get default parameters we can't
823 * guess.
824 * */
825 ready_len = sizeof(default_tcp_maxseg);
826 if (default_tcp_maxseg == -1) {
827 default_tcp_maxseg = -2;
828 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
829 if (fd < 0)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100830 ha_warning("Failed to create a temporary socket!\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200831 else {
832 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg,
833 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100834 ha_warning("Failed to get the default value of TCP_MAXSEG\n");
William Dauchyb3d52812020-02-12 10:09:14 +0100835 close(fd);
Olivier Houchard153659f2017-04-05 22:39:56 +0200836 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200837 }
838 if (default_tcp6_maxseg == -1) {
839 default_tcp6_maxseg = -2;
840 fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
841 if (fd >= 0) {
842 if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg,
843 &ready_len) == -1)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100844 ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n");
Olivier Houchard153659f2017-04-05 22:39:56 +0200845 close(fd);
846 }
847 }
848#endif
849
Willy Tarreaue6b98942007-10-29 01:09:36 +0100850
851 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100852 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100853 *errmsg = 0;
854
855 if (listener->state != LI_ASSIGNED)
856 return ERR_NONE; /* already bound */
857
858 err = ERR_NONE;
859
Olivier Houchard153659f2017-04-05 22:39:56 +0200860 if (listener->fd == -1)
861 listener->fd = tcp_find_compatible_fd(listener);
862
Willy Tarreau40aa0702013-03-10 23:51:38 +0100863 /* if the listener already has an fd assigned, then we were offered the
864 * fd by an external process (most likely the parent), and we don't want
865 * to create a new socket. However we still want to set a few flags on
866 * the socket.
867 */
868 fd = listener->fd;
869 ext = (fd >= 0);
870
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100871 if (!ext) {
872 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
873
874 if (fd == -1) {
875 err |= ERR_RETRYABLE | ERR_ALERT;
876 msg = "cannot create listening socket";
877 goto tcp_return;
878 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100879 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100880
Willy Tarreaue6b98942007-10-29 01:09:36 +0100881 if (fd >= global.maxsock) {
882 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
883 msg = "not enough free sockets (raise '-n' parameter)";
884 goto tcp_close_return;
885 }
886
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200887 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100888 err |= ERR_FATAL | ERR_ALERT;
889 msg = "cannot make socket non-blocking";
890 goto tcp_close_return;
891 }
892
Willy Tarreau40aa0702013-03-10 23:51:38 +0100893 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100894 /* not fatal but should be reported */
895 msg = "cannot do so_reuseaddr";
896 err |= ERR_ALERT;
897 }
898
899 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900900 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Olivier Houchard153659f2017-04-05 22:39:56 +0200901 else {
902 struct linger tmplinger;
903 socklen_t len = sizeof(tmplinger);
904 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 &&
905 (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) {
906 tmplinger.l_onoff = 0;
907 tmplinger.l_linger = 0;
908 setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger,
909 sizeof(tmplinger));
910 }
911 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100912
Willy Tarreaue6b98942007-10-29 01:09:36 +0100913#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000914 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
915 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100916 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000917 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100918 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100919#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200920
Willy Tarreau40aa0702013-03-10 23:51:38 +0100921 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200922 switch (listener->addr.ss_family) {
923 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200924 if (1
925#if defined(IP_TRANSPARENT)
926 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
927#endif
928#if defined(IP_FREEBIND)
929 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
930#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200931#if defined(IP_BINDANY)
932 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
933#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200934#if defined(SO_BINDANY)
935 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
936#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200937 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200938 msg = "cannot make listening socket transparent";
939 err |= ERR_ALERT;
940 }
941 break;
942 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200943 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200944#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200945 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
946#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100947#if defined(IP_FREEBIND)
948 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
949#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200950#if defined(IPV6_BINDANY)
951 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
952#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200953#if defined(SO_BINDANY)
954 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
955#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200956 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200957 msg = "cannot make listening socket transparent";
958 err |= ERR_ALERT;
959 }
960 break;
961 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100962 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200963
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100964#ifdef SO_BINDTODEVICE
965 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100966 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100967 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100968 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100969 msg = "cannot bind listener to device";
970 err |= ERR_WARN;
971 }
972 }
973#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400974#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100975 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400976 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200977 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
978 msg = "cannot set MSS";
979 err |= ERR_WARN;
980 }
Olivier Houchard153659f2017-04-05 22:39:56 +0200981 } else if (ext) {
982 int tmpmaxseg = -1;
983 int defaultmss;
984 socklen_t len = sizeof(tmpmaxseg);
985
986 if (listener->addr.ss_family == AF_INET)
987 defaultmss = default_tcp_maxseg;
988 else
989 defaultmss = default_tcp6_maxseg;
990
991 getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len);
William Dauchyd0aea562020-02-12 15:53:04 +0100992 if (defaultmss > 0 &&
993 tmpmaxseg != defaultmss &&
994 setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) {
Olivier Houchard153659f2017-04-05 22:39:56 +0200995 msg = "cannot set MSS";
996 err |= ERR_WARN;
997 }
Willy Tarreaube1b9182009-06-14 18:48:19 +0200998 }
999#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001000#if defined(TCP_USER_TIMEOUT)
1001 if (listener->tcp_ut) {
1002 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
1003 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
1004 msg = "cannot set TCP User Timeout";
1005 err |= ERR_WARN;
1006 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001007 } else
1008 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero,
1009 sizeof(zero));
Willy Tarreau2af207a2015-02-04 00:45:58 +01001010#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001011#if defined(TCP_DEFER_ACCEPT)
1012 if (listener->options & LI_O_DEF_ACCEPT) {
1013 /* defer accept by up to one second */
1014 int accept_delay = 1;
1015 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
1016 msg = "cannot enable DEFER_ACCEPT";
1017 err |= ERR_WARN;
1018 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001019 } else
1020 setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero,
1021 sizeof(zero));
Willy Tarreaucb6cd432009-10-13 07:34:14 +02001022#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001023#if defined(TCP_FASTOPEN)
1024 if (listener->options & LI_O_TCP_FO) {
1025 /* TFO needs a queue length, let's use the configured backlog */
Willy Tarreaue2711c72019-02-27 15:39:41 +01001026 int qlen = listener_backlog(listener);
Willy Tarreau1c862c52012-10-05 16:21:00 +02001027 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
1028 msg = "cannot enable TCP_FASTOPEN";
1029 err |= ERR_WARN;
1030 }
Olivier Houchard153659f2017-04-05 22:39:56 +02001031 } else {
1032 socklen_t len;
1033 int qlen;
1034 len = sizeof(qlen);
1035 /* Only disable fast open if it was enabled, we don't want
1036 * the kernel to create a fast open queue if there's none.
1037 */
1038 if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 &&
1039 qlen != 0) {
1040 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero,
1041 sizeof(zero)) == -1) {
1042 msg = "cannot disable TCP_FASTOPEN";
1043 err |= ERR_WARN;
1044 }
1045 }
Willy Tarreau1c862c52012-10-05 16:21:00 +02001046 }
1047#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001048#if defined(IPV6_V6ONLY)
1049 if (listener->options & LI_O_V6ONLY)
1050 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +01001051 else if (listener->options & LI_O_V4V6)
1052 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001053#endif
1054
Willy Tarreau40aa0702013-03-10 23:51:38 +01001055 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001056 err |= ERR_RETRYABLE | ERR_ALERT;
1057 msg = "cannot bind socket";
1058 goto tcp_close_return;
1059 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001060
Willy Tarreau40aa0702013-03-10 23:51:38 +01001061 ready = 0;
1062 ready_len = sizeof(ready);
1063 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
1064 ready = 0;
1065
1066 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +01001067 listen(fd, listener_backlog(listener)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +01001068 err |= ERR_RETRYABLE | ERR_ALERT;
1069 msg = "cannot listen to socket";
1070 goto tcp_close_return;
1071 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001072
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001073#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001074 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +09001075 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Olivier Houchard153659f2017-04-05 22:39:56 +02001076 else
1077 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001078#endif
1079
Willy Tarreaue6b98942007-10-29 01:09:36 +01001080 /* the socket is ready */
1081 listener->fd = fd;
1082 listener->state = LI_LISTEN;
1083
Willy Tarreaua9786b62018-01-25 07:22:13 +01001084 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau3f1199d2020-02-12 10:15:34 +01001085 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaueb472682010-05-28 18:46:57 +02001086
Willy Tarreaue6b98942007-10-29 01:09:36 +01001087 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001088 if (msg && errlen) {
1089 char pn[INET6_ADDRSTRLEN];
1090
Willy Tarreau631f01c2011-09-05 00:36:48 +02001091 addr_to_str(&listener->addr, pn, sizeof(pn));
1092 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001093 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001094 return err;
1095
1096 tcp_close_return:
1097 close(fd);
1098 goto tcp_return;
1099}
1100
1101/* This function creates all TCP sockets bound to the protocol entry <proto>.
1102 * It is intended to be used as the protocol's bind_all() function.
1103 * The sockets will be registered but not added to any fd_set, in order not to
1104 * loose them across the fork(). A call to enable_all_listeners() is needed
1105 * to complete initialization. The return value is composed from ERR_*.
Willy Tarreau937604b2019-07-24 16:45:02 +02001106 *
1107 * Must be called with proto_lock held.
1108 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001109 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001110static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001111{
1112 struct listener *listener;
1113 int err = ERR_NONE;
1114
1115 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001116 err |= tcp_bind_listener(listener, errmsg, errlen);
1117 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001118 break;
1119 }
1120
1121 return err;
1122}
1123
Willy Tarreau32282382017-09-15 07:44:44 +02001124/* Add <listener> to the list of tcpv4 listeners, on port <port>. The
1125 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1126 * The number of listeners for the protocol is updated.
Willy Tarreau937604b2019-07-24 16:45:02 +02001127 *
1128 * Must be called with proto_lock held.
1129 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001130 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001131static void tcpv4_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001132{
1133 if (listener->state != LI_INIT)
1134 return;
1135 listener->state = LI_ASSIGNED;
1136 listener->proto = &proto_tcpv4;
Willy Tarreau32282382017-09-15 07:44:44 +02001137 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001138 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1139 proto_tcpv4.nb_listeners++;
1140}
1141
Willy Tarreau32282382017-09-15 07:44:44 +02001142/* Add <listener> to the list of tcpv6 listeners, on port <port>. The
1143 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
1144 * The number of listeners for the protocol is updated.
Willy Tarreau937604b2019-07-24 16:45:02 +02001145 *
1146 * Must be called with proto_lock held.
1147 *
Willy Tarreaue6b98942007-10-29 01:09:36 +01001148 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +02001149static void tcpv6_add_listener(struct listener *listener, int port)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001150{
1151 if (listener->state != LI_INIT)
1152 return;
1153 listener->state = LI_ASSIGNED;
1154 listener->proto = &proto_tcpv6;
Willy Tarreau32282382017-09-15 07:44:44 +02001155 ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001156 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1157 proto_tcpv6.nb_listeners++;
1158}
1159
Willy Tarreau092d8652014-07-07 20:22:12 +02001160/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1161 * was totally stopped, or > 0 if correctly paused.
1162 */
1163int tcp_pause_listener(struct listener *l)
1164{
1165 if (shutdown(l->fd, SHUT_WR) != 0)
1166 return -1; /* Solaris dies here */
1167
Willy Tarreaue2711c72019-02-27 15:39:41 +01001168 if (listen(l->fd, listener_backlog(l)) != 0)
Willy Tarreau092d8652014-07-07 20:22:12 +02001169 return -1; /* OpenBSD dies here */
1170
1171 if (shutdown(l->fd, SHUT_RD) != 0)
1172 return -1; /* should always be OK */
1173 return 1;
1174}
1175
William Lallemand2e785f22016-05-25 01:48:42 +02001176/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001177 * Execute the "set-src" action. May be called from {tcp,http}request.
1178 * It only changes the address and tries to preserve the original port. If the
1179 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001180 */
1181enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1182 struct session *sess, struct stream *s, int flags)
1183{
1184 struct connection *cli_conn;
1185
1186 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1187 struct sample *smp;
1188
1189 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1190 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001191 int port = get_net_port(&cli_conn->addr.from);
1192
William Lallemand2e785f22016-05-25 01:48:42 +02001193 if (smp->data.type == SMP_T_IPV4) {
1194 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
1195 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001196 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001197 } else if (smp->data.type == SMP_T_IPV6) {
1198 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
1199 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 +02001200 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001201 }
1202 }
William Lallemand01252ed2016-05-25 02:33:16 +02001203 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001204 }
1205 return ACT_RET_CONT;
1206}
1207
William Lallemand44be6402016-05-25 01:51:35 +02001208/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001209 * Execute the "set-dst" action. May be called from {tcp,http}request.
1210 * It only changes the address and tries to preserve the original port. If the
1211 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001212 */
1213enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1214 struct session *sess, struct stream *s, int flags)
1215{
1216 struct connection *cli_conn;
1217
1218 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1219 struct sample *smp;
1220
1221 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1222 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001223 int port = get_net_port(&cli_conn->addr.to);
1224
William Lallemand13e9b0c2016-05-25 02:34:07 +02001225 if (smp->data.type == SMP_T_IPV4) {
1226 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_family = AF_INET;
1227 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Christopher Faulet4eff12f2021-03-01 11:21:14 +01001228 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001229 } else if (smp->data.type == SMP_T_IPV6) {
1230 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_family = AF_INET6;
1231 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 +02001232 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001233 }
1234 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1235 }
1236 }
1237 return ACT_RET_CONT;
1238}
1239
1240/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001241 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1242 * We must test the sin_family before setting the port. If the address family
1243 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1244 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001245 */
1246enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1247 struct session *sess, struct stream *s, int flags)
1248{
1249 struct connection *cli_conn;
1250
1251 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1252 struct sample *smp;
1253
1254 conn_get_from_addr(cli_conn);
1255
1256 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1257 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001258 if (cli_conn->addr.from.ss_family == AF_INET6) {
William Lallemand44be6402016-05-25 01:51:35 +02001259 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001260 } else {
1261 if (cli_conn->addr.from.ss_family != AF_INET) {
1262 cli_conn->addr.from.ss_family = AF_INET;
1263 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1264 }
1265 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001266 }
1267 }
1268 }
1269 return ACT_RET_CONT;
1270}
1271
William Lallemand13e9b0c2016-05-25 02:34:07 +02001272/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001273 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1274 * We must test the sin_family before setting the port. If the address family
1275 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1276 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001277 */
1278enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1279 struct session *sess, struct stream *s, int flags)
1280{
1281 struct connection *cli_conn;
1282
1283 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1284 struct sample *smp;
1285
1286 conn_get_to_addr(cli_conn);
1287
1288 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1289 if (smp) {
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001290 if (cli_conn->addr.to.ss_family == AF_INET6) {
1291 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001292 } else {
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001293 if (cli_conn->addr.to.ss_family != AF_INET) {
1294 cli_conn->addr.to.ss_family = AF_INET;
1295 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = 0;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001296 }
Baptiste Assmann46392fd2017-10-03 23:16:36 +02001297 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001298 }
1299 }
1300 }
1301 return ACT_RET_CONT;
1302}
1303
Willy Tarreau2d392c22015-08-24 01:43:45 +02001304/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1305static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1306{
1307 struct connection *conn = objt_conn(sess->origin);
1308
1309 if (!conn)
1310 goto out;
1311
1312 if (!conn_ctrl_ready(conn))
1313 goto out;
1314
Willy Tarreau2d392c22015-08-24 01:43:45 +02001315#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001316 /* drain is needed only to send the quick ACK */
1317 conn_sock_drain(conn);
1318
Willy Tarreau2d392c22015-08-24 01:43:45 +02001319 /* re-enable quickack if it was disabled to ack all data and avoid
1320 * retransmits from the client that might trigger a real reset.
1321 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001322 setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001323#endif
1324 /* lingering must absolutely be disabled so that we don't send a
1325 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1326 * is present, returning with ERR will cause lingering to be disabled.
1327 */
1328 if (strm)
1329 strm->si[0].flags |= SI_FL_NOLINGER;
1330
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001331 /* We're on the client-facing side, we must force to disable lingering to
1332 * ensure we will use an RST exclusively and kill any pending data.
1333 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001334 fdtab[conn->handle.fd].linger_risk = 1;
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001335
Willy Tarreau2d392c22015-08-24 01:43:45 +02001336#ifdef TCP_REPAIR
Willy Tarreau585744b2017-08-24 14:31:19 +02001337 if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
Willy Tarreau2d392c22015-08-24 01:43:45 +02001338 /* socket will be quiet now */
1339 goto out;
1340 }
1341#endif
1342 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1343 * Let's fall back on the TTL trick, though it only works for routed
1344 * network and has no effect on local net.
1345 */
1346#ifdef IP_TTL
Willy Tarreau585744b2017-08-24 14:31:19 +02001347 setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one));
Willy Tarreau2d392c22015-08-24 01:43:45 +02001348#endif
1349 out:
1350 /* kill the stream if any */
1351 if (strm) {
1352 channel_abort(&strm->req);
1353 channel_abort(&strm->res);
Christopher Fauletc70f1332020-03-06 15:10:46 +01001354 strm->req.analysers &= AN_REQ_FLT_END;
1355 strm->res.analysers &= AN_RES_FLT_END;
Christopher Fauletef804e42020-03-06 15:23:18 +01001356 if (strm->flags & SF_BE_ASSIGNED)
1357 _HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001358 if (!(strm->flags & SF_ERR_MASK))
1359 strm->flags |= SF_ERR_PRXCOND;
1360 if (!(strm->flags & SF_FINST_MASK))
1361 strm->flags |= SF_FINST_R;
1362 }
1363
Olivier Houchard40514102019-03-08 18:54:04 +01001364 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001365 if (sess->listener->counters)
Olivier Houchard40514102019-03-08 18:54:04 +01001366 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001367
1368 return ACT_RET_STOP;
1369}
1370
William Lallemand13e9b0c2016-05-25 02:34:07 +02001371/* parse "set-{src,dst}[-port]" action */
1372enum 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 +02001373{
1374 int cur_arg;
1375 struct sample_expr *expr;
1376 unsigned int where;
1377
1378 cur_arg = *orig_arg;
1379 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
1380 if (!expr)
1381 return ACT_RET_PRS_ERR;
1382
1383 where = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001384 if (px->cap & PR_CAP_FE)
William Lallemand2e785f22016-05-25 01:48:42 +02001385 where |= SMP_VAL_FE_HRQ_HDR;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001386 if (px->cap & PR_CAP_BE)
William Lallemand2e785f22016-05-25 01:48:42 +02001387 where |= SMP_VAL_BE_HRQ_HDR;
1388
1389 if (!(expr->fetch->val & where)) {
1390 memprintf(err,
1391 "fetch method '%s' extracts information from '%s', none of which is available here",
1392 args[cur_arg-1], sample_src_names(expr->fetch->use));
1393 free(expr);
1394 return ACT_RET_PRS_ERR;
1395 }
1396 rule->arg.expr = expr;
1397 rule->action = ACT_CUSTOM;
1398
1399 if (!strcmp(args[*orig_arg-1], "set-src")) {
1400 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001401 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1402 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001403 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1404 rule->action_ptr = tcp_action_req_set_dst;
1405 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1406 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001407 } else {
1408 return ACT_RET_PRS_ERR;
1409 }
1410
1411 (*orig_arg)++;
1412
1413 return ACT_RET_PRS_OK;
1414}
1415
1416
Willy Tarreau2d392c22015-08-24 01:43:45 +02001417/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1418 * success, ACT_RET_PRS_ERR on error.
1419 */
1420static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1421 struct act_rule *rule, char **err)
1422{
1423 rule->action = ACT_CUSTOM;
1424 rule->action_ptr = tcp_exec_action_silent_drop;
1425 return ACT_RET_PRS_OK;
1426}
1427
Willy Tarreau645513a2010-05-24 20:55:15 +02001428
1429/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001430/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001431/************************************************************************/
1432
Willy Tarreau4a129812012-04-25 17:31:42 +02001433/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001434int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001435{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001436 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001437
1438 if (!cli_conn)
1439 return 0;
1440
1441 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001442 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001443 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001444 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001445 break;
1446 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001447 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001448 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001449 break;
1450 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001451 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001452 }
Emeric Brunf769f512010-10-22 17:14:01 +02001453
Willy Tarreau37406352012-04-23 16:16:37 +02001454 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001455 return 1;
1456}
1457
Willy Tarreaua5e37562011-12-16 17:06:15 +01001458/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001459static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001460smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001461{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001462 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001463
1464 if (!cli_conn)
1465 return 0;
1466
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001467 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001468 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001469 return 0;
1470
Willy Tarreau37406352012-04-23 16:16:37 +02001471 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001472 return 1;
1473}
1474
Willy Tarreau4a129812012-04-25 17:31:42 +02001475/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001476static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001477smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001478{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001479 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001480
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001481 if (!cli_conn)
1482 return 0;
1483
1484 conn_get_to_addr(cli_conn);
1485
1486 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001487 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001488 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001489 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001490 break;
1491 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001492 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001493 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001494 break;
1495 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001496 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001497 }
Emeric Brunf769f512010-10-22 17:14:01 +02001498
Willy Tarreau37406352012-04-23 16:16:37 +02001499 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001500 return 1;
1501}
1502
Willy Tarreau16e01562016-08-09 16:46:18 +02001503/* check if the destination address of the front connection is local to the
1504 * system or if it was intercepted.
1505 */
1506int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1507{
1508 struct connection *conn = objt_conn(smp->sess->origin);
1509 struct listener *li = smp->sess->listener;
1510
1511 if (!conn)
1512 return 0;
1513
1514 conn_get_to_addr(conn);
1515 if (!(conn->flags & CO_FL_ADDR_TO_SET))
1516 return 0;
1517
1518 smp->data.type = SMP_T_BOOL;
1519 smp->flags = 0;
1520 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.to);
1521 return smp->data.u.sint >= 0;
1522}
1523
1524/* check if the source address of the front connection is local to the system
1525 * or not.
1526 */
1527int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1528{
1529 struct connection *conn = objt_conn(smp->sess->origin);
1530 struct listener *li = smp->sess->listener;
1531
1532 if (!conn)
1533 return 0;
1534
1535 conn_get_from_addr(conn);
1536 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
1537 return 0;
1538
1539 smp->data.type = SMP_T_BOOL;
1540 smp->flags = 0;
1541 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.from);
1542 return smp->data.u.sint >= 0;
1543}
1544
Willy Tarreaua5e37562011-12-16 17:06:15 +01001545/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001546static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001547smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001548{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001549 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001550
1551 if (!cli_conn)
1552 return 0;
1553
1554 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001555
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001556 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001557 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001558 return 0;
1559
Willy Tarreau37406352012-04-23 16:16:37 +02001560 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001561 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001562}
1563
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001564#ifdef TCP_INFO
1565
Christopher Faulet297df182019-10-17 14:40:48 +02001566
1567/* Validates the arguments passed to "fc_*" fetch keywords returning a time
1568 * value. These keywords support an optional string representing the unit of the
1569 * result: "us" for microseconds and "ms" for milliseconds". Returns 0 on error
1570 * and non-zero if OK.
1571 */
1572static int val_fc_time_value(struct arg *args, char **err)
1573{
Christopher Faulet297df182019-10-17 14:40:48 +02001574 if (args[0].type == ARGT_STR) {
1575 if (strcmp(args[0].data.str.area, "us") == 0) {
1576 free(args[0].data.str.area);
1577 args[0].type = ARGT_SINT;
1578 args[0].data.sint = TIME_UNIT_US;
1579 }
1580 else if (strcmp(args[0].data.str.area, "ms") == 0) {
1581 free(args[0].data.str.area);
1582 args[0].type = ARGT_SINT;
1583 args[0].data.sint = TIME_UNIT_MS;
1584 }
1585 else {
1586 memprintf(err, "expects 'us' or 'ms', got '%s'",
1587 args[0].data.str.area);
1588 return 0;
1589 }
1590 }
1591 else {
1592 memprintf(err, "Unexpected arg type");
1593 return 0;
1594 }
1595
1596 return 1;
1597}
1598
1599/* Validates the arguments passed to "fc_*" fetch keywords returning a
1600 * counter. These keywords should be used without any keyword, but because of a
1601 * bug in previous versions, an optional string argument may be passed. In such
1602 * case, the argument is ignored and a warning is emitted. Returns 0 on error
1603 * and non-zero if OK.
1604 */
1605static int var_fc_counter(struct arg *args, char **err)
1606{
1607 if (args[0].type != ARGT_STOP) {
1608 ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n");
1609 if (args[0].type == ARGT_STR)
1610 free(args[0].data.str.area);
1611 args[0].type = ARGT_STOP;
1612 }
1613
1614 return 1;
1615}
1616
Joseph Herlanta6331472018-11-25 12:59:12 -08001617/* Returns some tcp_info data if it's available. "dir" must be set to 0 if
1618 * the client connection is required, otherwise it is set to 1. "val" represents
Christopher Faulet297df182019-10-17 14:40:48 +02001619 * the required value.
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001620 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1621 */
1622static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1623 int dir, int val)
1624{
1625 struct connection *conn;
1626 struct tcp_info info;
1627 socklen_t optlen;
1628
1629 /* strm can be null. */
1630 if (!smp->strm)
1631 return 0;
1632
1633 /* get the object associated with the stream interface.The
1634 * object can be other thing than a connection. For example,
1635 * it be a appctx. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001636 conn = cs_conn(objt_cs(smp->strm->si[dir].end));
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001637 if (!conn)
1638 return 0;
1639
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001640 /* The fd may not be available for the tcp_info struct, and the
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001641 syscal can fail. */
1642 optlen = sizeof(info);
Willy Tarreau585744b2017-08-24 14:31:19 +02001643 if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001644 return 0;
1645
1646 /* extract the value. */
1647 smp->data.type = SMP_T_SINT;
1648 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001649 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1650 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1651#if defined(__linux__)
1652 /* these ones are common to all Linux versions */
1653 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1654 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1655 case 4: smp->data.u.sint = info.tcpi_lost; break;
1656 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1657 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1658 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1659#elif defined(__FreeBSD__) || defined(__NetBSD__)
1660 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1661 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1662 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1663 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1664 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1665 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1666 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1667#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001668 default: return 0;
1669 }
1670
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001671 return 1;
1672}
1673
1674/* get the mean rtt of a client connexion */
1675static int
1676smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1677{
1678 if (!get_tcp_info(args, smp, 0, 0))
1679 return 0;
Christopher Faulet297df182019-10-17 14:40:48 +02001680
1681 /* By default or if explicitly specified, convert rtt to ms */
1682 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1683 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1684
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001685 return 1;
1686}
1687
1688/* get the variance of the mean rtt of a client connexion */
1689static int
1690smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1691{
1692 if (!get_tcp_info(args, smp, 0, 1))
1693 return 0;
Christopher Faulet297df182019-10-17 14:40:48 +02001694
1695 /* By default or if explicitly specified, convert rttvar to ms */
1696 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
1697 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1698
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001699 return 1;
1700}
Joe Williams30fcd392016-08-10 07:06:44 -07001701
1702#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1703
1704/* get the unacked counter on a client connexion */
1705static int
1706smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1707{
1708 if (!get_tcp_info(args, smp, 0, 2))
1709 return 0;
1710 return 1;
1711}
1712
1713/* get the sacked counter on a client connexion */
1714static int
1715smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1716{
1717 if (!get_tcp_info(args, smp, 0, 3))
1718 return 0;
1719 return 1;
1720}
1721
1722/* get the lost counter on a client connexion */
1723static int
1724smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1725{
1726 if (!get_tcp_info(args, smp, 0, 4))
1727 return 0;
1728 return 1;
1729}
1730
1731/* get the retrans counter on a client connexion */
1732static int
1733smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1734{
1735 if (!get_tcp_info(args, smp, 0, 5))
1736 return 0;
1737 return 1;
1738}
1739
1740/* get the fackets counter on a client connexion */
1741static int
1742smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1743{
1744 if (!get_tcp_info(args, smp, 0, 6))
1745 return 0;
1746 return 1;
1747}
1748
1749/* get the reordering counter on a client connexion */
1750static int
1751smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1752{
1753 if (!get_tcp_info(args, smp, 0, 7))
1754 return 0;
1755 return 1;
1756}
1757#endif // linux || freebsd || netbsd
1758#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001759
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001760#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001761/* parse the "v4v6" bind keyword */
1762static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1763{
1764 struct listener *l;
1765
1766 list_for_each_entry(l, &conf->listeners, by_bind) {
1767 if (l->addr.ss_family == AF_INET6)
1768 l->options |= LI_O_V4V6;
1769 }
1770
1771 return 0;
1772}
1773
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001774/* parse the "v6only" bind keyword */
1775static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1776{
1777 struct listener *l;
1778
1779 list_for_each_entry(l, &conf->listeners, by_bind) {
1780 if (l->addr.ss_family == AF_INET6)
1781 l->options |= LI_O_V6ONLY;
1782 }
1783
1784 return 0;
1785}
1786#endif
1787
Pieter Baauwd551fb52013-05-08 22:49:23 +02001788#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001789/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001790static 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 +02001791{
1792 struct listener *l;
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->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001797 }
1798
Willy Tarreau44791242012-09-12 23:27:21 +02001799 return 0;
1800}
1801#endif
1802
1803#ifdef TCP_DEFER_ACCEPT
1804/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001805static 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 +02001806{
1807 struct listener *l;
1808
Willy Tarreau4348fad2012-09-20 16:48:07 +02001809 list_for_each_entry(l, &conf->listeners, by_bind) {
1810 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1811 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001812 }
1813
Willy Tarreau44791242012-09-12 23:27:21 +02001814 return 0;
1815}
1816#endif
1817
Willy Tarreau1c862c52012-10-05 16:21:00 +02001818#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001819/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001820static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1821{
1822 struct listener *l;
1823
1824 list_for_each_entry(l, &conf->listeners, by_bind) {
1825 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1826 l->options |= LI_O_TCP_FO;
1827 }
1828
1829 return 0;
1830}
1831#endif
1832
Willy Tarreau44791242012-09-12 23:27:21 +02001833#ifdef TCP_MAXSEG
1834/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001835static 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 +02001836{
1837 struct listener *l;
1838 int mss;
1839
Willy Tarreau44791242012-09-12 23:27:21 +02001840 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001841 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001842 return ERR_ALERT | ERR_FATAL;
1843 }
1844
1845 mss = atoi(args[cur_arg + 1]);
1846 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001847 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001848 return ERR_ALERT | ERR_FATAL;
1849 }
1850
Willy Tarreau4348fad2012-09-20 16:48:07 +02001851 list_for_each_entry(l, &conf->listeners, by_bind) {
1852 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1853 l->maxseg = mss;
1854 }
Willy Tarreau44791242012-09-12 23:27:21 +02001855
1856 return 0;
1857}
1858#endif
1859
Willy Tarreau2af207a2015-02-04 00:45:58 +01001860#ifdef TCP_USER_TIMEOUT
1861/* parse the "tcp-ut" bind keyword */
1862static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1863{
1864 const char *ptr = NULL;
1865 struct listener *l;
1866 unsigned int timeout;
1867
1868 if (!*args[cur_arg + 1]) {
1869 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1870 return ERR_ALERT | ERR_FATAL;
1871 }
1872
1873 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001874 if (ptr == PARSE_TIME_OVER) {
1875 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1876 args[cur_arg+1], args[cur_arg]);
1877 return ERR_ALERT | ERR_FATAL;
1878 }
1879 else if (ptr == PARSE_TIME_UNDER) {
1880 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1881 args[cur_arg+1], args[cur_arg]);
1882 return ERR_ALERT | ERR_FATAL;
1883 }
1884 else if (ptr) {
Willy Tarreau2af207a2015-02-04 00:45:58 +01001885 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1886 return ERR_ALERT | ERR_FATAL;
1887 }
1888
1889 list_for_each_entry(l, &conf->listeners, by_bind) {
1890 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1891 l->tcp_ut = timeout;
1892 }
1893
1894 return 0;
1895}
1896#endif
1897
Willy Tarreau44791242012-09-12 23:27:21 +02001898#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001899/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001900static 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 +02001901{
1902 struct listener *l;
1903
Willy Tarreau44791242012-09-12 23:27:21 +02001904 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001905 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001906 return ERR_ALERT | ERR_FATAL;
1907 }
1908
Willy Tarreau4348fad2012-09-20 16:48:07 +02001909 list_for_each_entry(l, &conf->listeners, by_bind) {
1910 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1911 l->interface = strdup(args[cur_arg + 1]);
1912 }
Willy Tarreau44791242012-09-12 23:27:21 +02001913
Willy Tarreau44791242012-09-12 23:27:21 +02001914 return 0;
1915}
1916#endif
1917
Willy Tarreaue5733232019-05-22 19:24:06 +02001918#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001919/* parse the "namespace" bind keyword */
1920static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1921{
1922 struct listener *l;
1923 char *namespace = NULL;
1924
1925 if (!*args[cur_arg + 1]) {
1926 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1927 return ERR_ALERT | ERR_FATAL;
1928 }
1929 namespace = args[cur_arg + 1];
1930
1931 list_for_each_entry(l, &conf->listeners, by_bind) {
1932 l->netns = netns_store_lookup(namespace, strlen(namespace));
1933
1934 if (l->netns == NULL)
1935 l->netns = netns_store_insert(namespace);
1936
1937 if (l->netns == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001938 ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001939 return ERR_ALERT | ERR_FATAL;
1940 }
1941 }
1942 return 0;
1943}
1944#endif
1945
Willy Tarreau163d4622015-10-13 16:16:41 +02001946#ifdef TCP_USER_TIMEOUT
1947/* parse the "tcp-ut" server keyword */
1948static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1949{
1950 const char *ptr = NULL;
1951 unsigned int timeout;
1952
1953 if (!*args[*cur_arg + 1]) {
1954 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1955 return ERR_ALERT | ERR_FATAL;
1956 }
1957
1958 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001959 if (ptr == PARSE_TIME_OVER) {
1960 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1961 args[*cur_arg+1], args[*cur_arg]);
1962 return ERR_ALERT | ERR_FATAL;
1963 }
1964 else if (ptr == PARSE_TIME_UNDER) {
1965 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1966 args[*cur_arg+1], args[*cur_arg]);
1967 return ERR_ALERT | ERR_FATAL;
1968 }
1969 else if (ptr) {
Willy Tarreau163d4622015-10-13 16:16:41 +02001970 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1971 return ERR_ALERT | ERR_FATAL;
1972 }
1973
1974 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1975 newsrv->tcp_ut = timeout;
1976
1977 return 0;
1978}
1979#endif
1980
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001981
Willy Tarreau4a129812012-04-25 17:31:42 +02001982/* Note: must not be declared <const> as its list will be overwritten.
1983 * Note: fetches that may return multiple types must be declared as the lowest
1984 * common denominator, the type that can be casted into all other ones. For
1985 * instance v4/v6 must be declared v4.
1986 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001987static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001988 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001989 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001990 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001991 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001992 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001993 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001994#ifdef TCP_INFO
Christopher Faulet297df182019-10-17 14:40:48 +02001995 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
1996 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
Joe Williams30fcd392016-08-10 07:06:44 -07001997#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Christopher Faulet297df182019-10-17 14:40:48 +02001998 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
1999 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
2000 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
2001 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
2002 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
2003 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
Joe Williams30fcd392016-08-10 07:06:44 -07002004#endif // linux || freebsd || netbsd
2005#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002006 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002007}};
2008
Willy Tarreau0108d902018-11-25 19:14:37 +01002009INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
2010
Willy Tarreau44791242012-09-12 23:27:21 +02002011/************************************************************************/
2012/* All supported bind keywords must be declared here. */
2013/************************************************************************/
2014
2015/* Note: must not be declared <const> as its list will be overwritten.
2016 * Please take care of keeping this list alphabetically sorted, doing so helps
2017 * all code contributors.
2018 * Optional keywords are also declared with a NULL ->parse() function so that
2019 * the config parser can report an appropriate error when a known keyword was
2020 * not enabled.
2021 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002022static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02002023#ifdef TCP_DEFER_ACCEPT
2024 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
2025#endif
2026#ifdef SO_BINDTODEVICE
2027 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
2028#endif
2029#ifdef TCP_MAXSEG
2030 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
2031#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01002032#ifdef TCP_USER_TIMEOUT
2033 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
2034#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02002035#ifdef TCP_FASTOPEN
2036 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
2037#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02002038#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002039 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
2040#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002041#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002042 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002043 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
2044#endif
Willy Tarreaue5733232019-05-22 19:24:06 +02002045#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002046 { "namespace", bind_parse_namespace, 1 },
2047#endif
Willy Tarreau44791242012-09-12 23:27:21 +02002048 /* the versions with the NULL parse function*/
2049 { "defer-accept", NULL, 0 },
2050 { "interface", NULL, 1 },
2051 { "mss", NULL, 1 },
2052 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002053 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002054 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002055 { NULL, NULL, 0 },
2056}};
2057
Willy Tarreau0108d902018-11-25 19:14:37 +01002058INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
2059
Willy Tarreau163d4622015-10-13 16:16:41 +02002060static struct srv_kw_list srv_kws = { "TCP", { }, {
2061#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002062 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02002063#endif
2064 { NULL, NULL, 0 },
2065}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02002066
Willy Tarreau0108d902018-11-25 19:14:37 +01002067INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
2068
Willy Tarreau2d392c22015-08-24 01:43:45 +02002069static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand13e9b0c2016-05-25 02:34:07 +02002070 { "set-src", tcp_parse_set_src_dst },
2071 { "set-src-port", tcp_parse_set_src_dst },
2072 { "set-dst" , tcp_parse_set_src_dst },
2073 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002074 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002075 { /* END */ }
2076}};
2077
Willy Tarreau0108d902018-11-25 19:14:37 +01002078INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions);
2079
Willy Tarreau620408f2016-10-21 16:37:51 +02002080static struct action_kw_list tcp_req_sess_actions = {ILH, {
Willy Tarreau620408f2016-10-21 16:37:51 +02002081 { "set-src", tcp_parse_set_src_dst },
2082 { "set-src-port", tcp_parse_set_src_dst },
2083 { "set-dst" , tcp_parse_set_src_dst },
2084 { "set-dst-port", tcp_parse_set_src_dst },
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002085 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau620408f2016-10-21 16:37:51 +02002086 { /* END */ }
2087}};
2088
Willy Tarreau0108d902018-11-25 19:14:37 +01002089INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions);
2090
Willy Tarreau2d392c22015-08-24 01:43:45 +02002091static struct action_kw_list tcp_req_cont_actions = {ILH, {
Baptiste Assmanne1afd4f2019-04-18 16:21:13 +02002092 { "set-dst" , tcp_parse_set_src_dst },
2093 { "set-dst-port", tcp_parse_set_src_dst },
2094 { "silent-drop", tcp_parse_silent_drop },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002095 { /* END */ }
2096}};
2097
Willy Tarreau0108d902018-11-25 19:14:37 +01002098INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2099
Willy Tarreau2d392c22015-08-24 01:43:45 +02002100static struct action_kw_list tcp_res_cont_actions = {ILH, {
2101 { "silent-drop", tcp_parse_silent_drop },
2102 { /* END */ }
2103}};
2104
Willy Tarreau0108d902018-11-25 19:14:37 +01002105INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
2106
Willy Tarreau2d392c22015-08-24 01:43:45 +02002107static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002108 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002109 { "set-src", tcp_parse_set_src_dst },
2110 { "set-src-port", tcp_parse_set_src_dst },
2111 { "set-dst", tcp_parse_set_src_dst },
2112 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002113 { /* END */ }
2114}};
2115
Willy Tarreau0108d902018-11-25 19:14:37 +01002116INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2117
Willy Tarreau2d392c22015-08-24 01:43:45 +02002118static struct action_kw_list http_res_actions = {ILH, {
2119 { "silent-drop", tcp_parse_silent_drop },
2120 { /* END */ }
2121}};
2122
Willy Tarreau0108d902018-11-25 19:14:37 +01002123INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002124
Willy Tarreau80713382018-11-26 10:19:54 +01002125REGISTER_BUILD_OPTS("Built with transparent proxy support using:"
Willy Tarreauba962912016-12-21 18:55:02 +01002126#if defined(IP_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002127 " IP_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002128#endif
2129#if defined(IPV6_TRANSPARENT)
Willy Tarreau80713382018-11-26 10:19:54 +01002130 " IPV6_TRANSPARENT"
Willy Tarreauba962912016-12-21 18:55:02 +01002131#endif
2132#if defined(IP_FREEBIND)
Willy Tarreau80713382018-11-26 10:19:54 +01002133 " IP_FREEBIND"
Willy Tarreauba962912016-12-21 18:55:02 +01002134#endif
2135#if defined(IP_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002136 " IP_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002137#endif
2138#if defined(IPV6_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002139 " IPV6_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002140#endif
2141#if defined(SO_BINDANY)
Willy Tarreau80713382018-11-26 10:19:54 +01002142 " SO_BINDANY"
Willy Tarreauba962912016-12-21 18:55:02 +01002143#endif
Willy Tarreau80713382018-11-26 10:19:54 +01002144 "");
Willy Tarreaue6b98942007-10-29 01:09:36 +01002145
2146
2147/*
2148 * Local variables:
2149 * c-indent-level: 8
2150 * c-basic-offset: 8
2151 * End:
2152 */