blob: b614e6b5900098d938c56f8928412a0bd060a7ef [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 Tarreaub6866442008-07-14 23:54:42 +020035#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010036#include <common/compat.h>
37#include <common/config.h>
38#include <common/debug.h>
39#include <common/errors.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 Tarreaue6b98942007-10-29 01:09:36 +010044#include <types/global.h>
Willy Tarreau18bf01e2014-06-13 16:18:52 +020045#include <types/capture.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020046#include <types/connection.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010047
48#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020049#include <proto/action.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020050#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020051#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020052#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020053#include <proto/fd.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 Tarreau87b09662015-04-03 00:22:06 +020063#include <proto/stream.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020064#include <proto/stick_table.h>
Willy Tarreaucc1e04b2013-09-11 23:20:29 +020065#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020066#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010067
Emeric Bruncf20bf12010-10-22 16:06:11 +020068static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
69static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010070
Thierry FOURNIERcc87a112014-12-12 19:41:33 +010071/* List head of all known action keywords for "tcp-request connection" */
72struct list tcp_req_conn_keywords = LIST_HEAD_INIT(tcp_req_conn_keywords);
73struct list tcp_req_cont_keywords = LIST_HEAD_INIT(tcp_req_cont_keywords);
74struct list tcp_res_cont_keywords = LIST_HEAD_INIT(tcp_res_cont_keywords);
75
Willy Tarreaue6b98942007-10-29 01:09:36 +010076/* Note: must not be declared <const> as its list will be overwritten */
77static struct protocol proto_tcpv4 = {
78 .name = "tcpv4",
79 .sock_domain = AF_INET,
80 .sock_type = SOCK_STREAM,
81 .sock_prot = IPPROTO_TCP,
82 .sock_family = AF_INET,
83 .sock_addrlen = sizeof(struct sockaddr_in),
84 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020085 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020086 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020087 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010088 .bind_all = tcp_bind_listeners,
89 .unbind_all = unbind_all_listeners,
90 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020091 .get_src = tcp_get_src,
92 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020093 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +020094 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010095 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
96 .nb_listeners = 0,
97};
98
99/* Note: must not be declared <const> as its list will be overwritten */
100static struct protocol proto_tcpv6 = {
101 .name = "tcpv6",
102 .sock_domain = AF_INET6,
103 .sock_type = SOCK_STREAM,
104 .sock_prot = IPPROTO_TCP,
105 .sock_family = AF_INET6,
106 .sock_addrlen = sizeof(struct sockaddr_in6),
107 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200108 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200109 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200110 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100111 .bind_all = tcp_bind_listeners,
112 .unbind_all = unbind_all_listeners,
113 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200114 .get_src = tcp_get_src,
115 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200116 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +0200117 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100118 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
119 .nb_listeners = 0,
120};
121
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100122/*
123 * Register keywords.
124 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200125void tcp_req_conn_keywords_register(struct action_kw_list *kw_list)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100126{
127 LIST_ADDQ(&tcp_req_conn_keywords, &kw_list->list);
128}
129
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200130void tcp_req_cont_keywords_register(struct action_kw_list *kw_list)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100131{
132 LIST_ADDQ(&tcp_req_cont_keywords, &kw_list->list);
133}
134
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200135void tcp_res_cont_keywords_register(struct action_kw_list *kw_list)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100136{
137 LIST_ADDQ(&tcp_res_cont_keywords, &kw_list->list);
138}
139
140/*
141 * Return the struct http_req_action_kw associated to a keyword.
142 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200143static struct action_kw *tcp_req_conn_action(const char *kw)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100144{
Thierry FOURNIER322a1242015-08-19 09:07:47 +0200145 return action_lookup(&tcp_req_conn_keywords, kw);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100146}
147
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200148static struct action_kw *tcp_req_cont_action(const char *kw)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100149{
Thierry FOURNIER322a1242015-08-19 09:07:47 +0200150 return action_lookup(&tcp_req_cont_keywords, kw);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100151}
152
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200153static struct action_kw *tcp_res_cont_action(const char *kw)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100154{
Thierry FOURNIER322a1242015-08-19 09:07:47 +0200155 return action_lookup(&tcp_res_cont_keywords, kw);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100156}
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100157
David du Colombier6f5ccb12011-03-10 22:26:24 +0100158/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100159 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
160 * - 0 : ignore remote address (may even be a NULL pointer)
161 * - 1 : use provided address
162 * - 2 : use provided port
163 * - 3 : use both
164 *
165 * The function supports multiple foreign binding methods :
166 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100167 * The second one can be used as a fallback for the first one.
168 * This function returns 0 when everything's OK, 1 if it could not bind, to the
169 * local address, 2 if it could not bind to the foreign address.
170 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100171int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100172{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100173 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100174 int foreign_ok = 0;
175 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100176 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200177 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200178
David du Colombier65c17962012-07-13 14:34:59 +0200179 switch (local->ss_family) {
180 case AF_INET:
181 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200182 /* This deserves some explanation. Some platforms will support
183 * multiple combinations of certain methods, so we try the
184 * supported ones until one succeeds.
185 */
186 if (0
187#if defined(IP_TRANSPARENT)
188 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
189#endif
190#if defined(IP_FREEBIND)
191 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
192#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200193#if defined(IP_BINDANY)
194 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
195#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200196#if defined(SO_BINDANY)
197 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
198#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200199 )
David du Colombier65c17962012-07-13 14:34:59 +0200200 foreign_ok = 1;
201 else
202 ip_transp_working = 0;
203 }
204 break;
205 case AF_INET6:
206 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200207 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200208#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200209 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
210#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100211#if defined(IP_FREEBIND)
212 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
213#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200214#if defined(IPV6_BINDANY)
215 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
216#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200217#if defined(SO_BINDANY)
218 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
219#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200220 )
David du Colombier65c17962012-07-13 14:34:59 +0200221 foreign_ok = 1;
222 else
223 ip6_transp_working = 0;
224 }
225 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100226 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200227
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100228 if (flags) {
229 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200230 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100231 switch (remote->ss_family) {
232 case AF_INET:
233 if (flags & 1)
234 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
235 if (flags & 2)
236 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
237 break;
238 case AF_INET6:
239 if (flags & 1)
240 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
241 if (flags & 2)
242 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
243 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100244 default:
245 /* we don't want to try to bind to an unknown address family */
246 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100247 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100248 }
249
Simon Hormande072bd2011-06-24 15:11:37 +0900250 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100251 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200252 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200253 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
254 if (ret < 0)
255 return 2;
256 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100257 }
258 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200259 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200260 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
261 if (ret < 0)
262 return 1;
263 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100264 }
265
266 if (!flags)
267 return 0;
268
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100269 if (!foreign_ok)
270 /* we could not bind to a foreign address */
271 return 2;
272
273 return 0;
274}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100275
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100276static int create_server_socket(struct connection *conn)
277{
Willy Tarreau529c1392014-12-24 13:47:55 +0100278 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100279
Willy Tarreau529c1392014-12-24 13:47:55 +0100280#ifdef CONFIG_HAP_NS
281 if (objt_server(conn->target)) {
282 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
283 ns = conn->proxy_netns;
284 else
285 ns = __objt_server(conn->target)->netns;
286 }
287#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100288 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
289}
Willy Tarreau9650f372009-08-16 14:02:45 +0200290
291/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200292 * This function initiates a TCP connection establishment to the target assigned
293 * to connection <conn> using (si->{target,addr.to}). A source address may be
294 * pointed to by conn->addr.from in case of transparent proxying. Normal source
295 * bind addresses are still determined locally (due to the possible need of a
296 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100297 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100298 * supported. The <data> parameter is a boolean indicating whether there are data
299 * waiting for being sent or not, in order to adjust data write polling and on
300 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
301 * allows the caller to force using a delayed ACK when establishing the connection :
302 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
303 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
304 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200305 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200306 * Note that a pending send_proxy message accounts for data.
307 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200308 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200309 * - SF_ERR_NONE if everything's OK
310 * - SF_ERR_SRVTO if there are no more servers
311 * - SF_ERR_SRVCL if the connection was refused by the server
312 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
313 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
314 * - SF_ERR_INTERNAL for any other purely internal errors
315 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100316 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200317 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100318 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200319 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100320
Willy Tarreauf0837b22012-11-24 10:24:27 +0100321int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200322{
323 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100324 struct server *srv;
325 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100326 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100327
Willy Tarreau9ce70132014-01-24 16:08:19 +0100328 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
329
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100330 switch (obj_type(conn->target)) {
331 case OBJ_TYPE_PROXY:
332 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100333 srv = NULL;
334 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100335 case OBJ_TYPE_SERVER:
336 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100337 be = srv->proxy;
338 break;
339 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100340 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200341 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100342 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200343
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100344 fd = conn->t.sock.fd = create_server_socket(conn);
345
346 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200347 qfprintf(stderr, "Cannot get a server socket.\n");
348
Willy Tarreau9ce70132014-01-24 16:08:19 +0100349 if (errno == ENFILE) {
350 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200351 send_log(be, LOG_EMERG,
352 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
353 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100354 }
355 else if (errno == EMFILE) {
356 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200357 send_log(be, LOG_EMERG,
358 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
359 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100360 }
361 else if (errno == ENOBUFS || errno == ENOMEM) {
362 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200363 send_log(be, LOG_EMERG,
364 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
365 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100366 }
367 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
368 conn->err_code = CO_ER_NOPROTO;
369 }
370 else
371 conn->err_code = CO_ER_SOCK_ERR;
372
Willy Tarreau9650f372009-08-16 14:02:45 +0200373 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100374 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200375 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200376 }
377
378 if (fd >= global.maxsock) {
379 /* do not log anything there, it's a normal condition when this option
380 * is used to serialize connections to a server !
381 */
382 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
383 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100384 conn->err_code = CO_ER_CONF_FDLIM;
385 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200386 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200387 }
388
389 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900390 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200391 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
392 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100393 conn->err_code = CO_ER_SOCK_ERR;
394 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200395 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200396 }
397
398 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900399 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200400
Willy Tarreau9650f372009-08-16 14:02:45 +0200401 /* allow specific binding :
402 * - server-specific at first
403 * - proxy-specific next
404 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100405 if (srv && srv->conn_src.opts & CO_SRC_BIND)
406 src = &srv->conn_src;
407 else if (be->conn_src.opts & CO_SRC_BIND)
408 src = &be->conn_src;
409 else
410 src = NULL;
411
412 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200413 int ret, flags = 0;
414
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200415 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100416 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100417 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200418 conn->flags |= CO_FL_PRIVATE;
419 /* fall through */
420 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200421 flags = 3;
422 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100423 case CO_SRC_TPROXY_CIP:
424 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200425 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200426 flags = 1;
427 break;
428 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200429 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200430
Willy Tarreau9650f372009-08-16 14:02:45 +0200431#ifdef SO_BINDTODEVICE
432 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100433 if (src->iface_name)
434 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200435#endif
436
Willy Tarreaua4380b42012-12-08 22:49:11 +0100437 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100439 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200440
441 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200442 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200443
444 do {
445 /* note: in case of retry, we may have to release a previously
446 * allocated port, hence this loop's construct.
447 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200448 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
449 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200450
451 if (!attempts)
452 break;
453 attempts--;
454
Willy Tarreaua4380b42012-12-08 22:49:11 +0100455 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100456 if (!fdinfo[fd].local_port) {
457 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100459 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200460
Willy Tarreaua4380b42012-12-08 22:49:11 +0100461 fdinfo[fd].port_range = src->sport_range;
462 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200463
Willy Tarreaua4380b42012-12-08 22:49:11 +0100464 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100465 if (ret != 0)
466 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200467 } while (ret != 0); /* binding NOK */
468 }
469 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100470 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100471 if (ret != 0)
472 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200473 }
474
Willy Tarreaua4380b42012-12-08 22:49:11 +0100475 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200476 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
477 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200478 close(fd);
479
Willy Tarreau9650f372009-08-16 14:02:45 +0200480 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100481 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200482 be->id);
483 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100484 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200485 be->id);
486 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100487 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200488 be->id);
489 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100490 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200491 be->id);
492 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100493 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200494 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200495 }
496 }
497
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400498#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200499 /* disabling tcp quick ack now allows the first request to leave the
500 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100501 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200502 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200503 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900504 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200505#endif
506
Willy Tarreau163d4622015-10-13 16:16:41 +0200507#ifdef TCP_USER_TIMEOUT
508 /* there is not much more we can do here when it fails, it's still minor */
509 if (srv && srv->tcp_ut)
510 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
511#endif
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
Willy Tarreau986a9d22012-08-30 21:11:38 +0200518 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200519 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
520
Willy Tarreaub1719512012-12-08 23:03:28 +0100521 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200522 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100523 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200524 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100525 conn->err_code = CO_ER_FREE_PORTS;
526 }
527 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200528 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100529 conn->err_code = CO_ER_ADDR_INUSE;
530 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200531
Willy Tarreaub1719512012-12-08 23:03:28 +0100532 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200533 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
534 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200535 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100536 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100537 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200538 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200539 } else if (errno == ETIMEDOUT) {
540 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200541 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
542 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200543 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100544 conn->err_code = CO_ER_SOCK_ERR;
545 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200546 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200547 } else {
548 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
549 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200550 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
551 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200552 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100553 conn->err_code = CO_ER_SOCK_ERR;
554 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200555 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200556 }
557 }
558
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100559 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200560
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200561 /* Prepare to send a few handshakes related to the on-wire protocol. */
562 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200563 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200564
Willy Tarreauf79c8172013-10-21 16:30:56 +0200565 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100566 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200567 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200568
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200569 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200570 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100571 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200572 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200573 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200574
Willy Tarreau14f8e862012-08-30 22:23:13 +0200575 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200576 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200577
Willy Tarreaue7dff022015-04-03 01:14:29 +0200578 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200579}
580
581
Willy Tarreau59b94792012-05-11 16:16:40 +0200582/*
583 * Retrieves the source address for the socket <fd>, with <dir> indicating
584 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
585 * success, -1 in case of error. The socket's source address is stored in
586 * <sa> for <salen> bytes.
587 */
588int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
589{
590 if (dir)
591 return getsockname(fd, sa, &salen);
592 else
593 return getpeername(fd, sa, &salen);
594}
595
596
597/*
598 * Retrieves the original destination address for the socket <fd>, with <dir>
599 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
600 * listener, if the original destination address was translated, the original
601 * address is retrieved. It returns 0 in case of success, -1 in case of error.
602 * The socket's source address is stored in <sa> for <salen> bytes.
603 */
604int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
605{
606 if (dir)
607 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100608 else {
609 int ret = getsockname(fd, sa, &salen);
610
611 if (ret < 0)
612 return ret;
613
Willy Tarreau59b94792012-05-11 16:16:40 +0200614#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100615 /* For TPROXY and Netfilter's NAT, we can retrieve the original
616 * IPv4 address before DNAT/REDIRECT. We must not do that with
617 * other families because v6-mapped IPv4 addresses are still
618 * reported as v4.
619 */
620 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
621 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
622 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200623#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100624 return ret;
625 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200626}
627
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200628/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100629 * receive shutdown. Returns positive if the shutdown was found, negative
630 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
631 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200632 */
633int tcp_drain(int fd)
634{
635 int turns = 2;
636 int len;
637
638 while (turns) {
639#ifdef MSG_TRUNC_CLEARS_INPUT
640 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
641 if (len == -1 && errno == EFAULT)
642#endif
643 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
644
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100645 if (len == 0) {
646 /* cool, shutdown received */
647 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200648 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100649 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200650
651 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100652 if (errno == EAGAIN) {
653 /* connection not closed yet */
654 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100655 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100656 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200657 if (errno == EINTR) /* oops, try again */
658 continue;
659 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100660 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200661 return 1;
662 }
663 /* OK we read some data, let's try again once */
664 turns--;
665 }
666 /* some data are still present, give up */
667 return 0;
668}
669
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200670/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100671 * and we have nothing to send. It updates the FD polling status. It returns 0
672 * if it fails in a fatal way or needs to poll to go further, otherwise it
673 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
674 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
675 * errno. The error checking is done in two passes in order to limit the number
676 * of syscalls in the normal case :
677 * - if POLL_ERR was reported by the poller, we check for a pending error on
678 * the socket before proceeding. If found, it's assigned to errno so that
679 * upper layers can see it.
680 * - otherwise connect() is used to check the connection state again, since
681 * the getsockopt return cannot reliably be used to know if the connection
682 * is still pending or ready. This one may often return an error as well,
683 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200684 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200685int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200686{
Willy Tarreau239d7182012-07-23 18:53:03 +0200687 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100688 socklen_t lskerr;
689 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200690
Willy Tarreau80184712012-07-06 14:54:49 +0200691 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200692 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200693
Willy Tarreau3c728722014-01-23 13:50:42 +0100694 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200695 return 0;
696
Willy Tarreau80184712012-07-06 14:54:49 +0200697 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200698 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200699
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100700 if (!fd_send_ready(fd))
701 return 0;
702
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100703 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
704 * without FD_POLL_IN also indicates a hangup without input data meaning
705 * there was no connection.
706 */
707 if (fdtab[fd].ev & FD_POLL_ERR ||
708 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
709 skerr = 0;
710 lskerr = sizeof(skerr);
711 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
712 errno = skerr;
713 if (errno == EAGAIN)
714 errno = 0;
715 if (errno)
716 goto out_error;
717 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200718
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100719 /* Use connect() to check the state of the socket. This has the
720 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200721 * - error
722 * - connecting (EALREADY, EINPROGRESS)
723 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200724 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200725 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200726 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100727 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100728 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200729 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200730 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200731
Willy Tarreau2c6be842012-07-06 17:12:34 +0200732 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200733 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200734
Willy Tarreau2c6be842012-07-06 17:12:34 +0200735 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200736 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200737
Willy Tarreau076be252012-07-06 16:02:29 +0200738 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200739 * forward the event to the transport layer which will notify the
740 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200741 */
Willy Tarreau80184712012-07-06 14:54:49 +0200742 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200743 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200744
745 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200746 /* Write error on the file descriptor. Report it to the connection
747 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200748 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100749 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100750 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100751 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200752 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200753}
754
Willy Tarreau59b94792012-05-11 16:16:40 +0200755
Willy Tarreaue6b98942007-10-29 01:09:36 +0100756/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100757 * an error message in <errmsg> if the message is at most <errlen> bytes long
758 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
759 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100760 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
761 * was alright and that no message was returned. ERR_RETRYABLE means that an
762 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700763 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100764 * the meaning of the error, but just indicate that a message is present which
765 * should be displayed with the respective level. Last, ERR_ABORT indicates
766 * that it's pointless to try to start other listeners. No error message is
767 * returned if errlen is NULL.
768 */
769int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
770{
771 __label__ tcp_return, tcp_close_return;
772 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100773 int ext, ready;
774 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100775 const char *msg = NULL;
776
777 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100778 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100779 *errmsg = 0;
780
781 if (listener->state != LI_ASSIGNED)
782 return ERR_NONE; /* already bound */
783
784 err = ERR_NONE;
785
Willy Tarreau40aa0702013-03-10 23:51:38 +0100786 /* if the listener already has an fd assigned, then we were offered the
787 * fd by an external process (most likely the parent), and we don't want
788 * to create a new socket. However we still want to set a few flags on
789 * the socket.
790 */
791 fd = listener->fd;
792 ext = (fd >= 0);
793
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100794 if (!ext) {
795 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
796
797 if (fd == -1) {
798 err |= ERR_RETRYABLE | ERR_ALERT;
799 msg = "cannot create listening socket";
800 goto tcp_return;
801 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100802 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100803
Willy Tarreaue6b98942007-10-29 01:09:36 +0100804 if (fd >= global.maxsock) {
805 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
806 msg = "not enough free sockets (raise '-n' parameter)";
807 goto tcp_close_return;
808 }
809
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200810 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100811 err |= ERR_FATAL | ERR_ALERT;
812 msg = "cannot make socket non-blocking";
813 goto tcp_close_return;
814 }
815
Willy Tarreau40aa0702013-03-10 23:51:38 +0100816 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100817 /* not fatal but should be reported */
818 msg = "cannot do so_reuseaddr";
819 err |= ERR_ALERT;
820 }
821
822 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900823 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100824
Willy Tarreaue6b98942007-10-29 01:09:36 +0100825#ifdef SO_REUSEPORT
826 /* OpenBSD supports this. As it's present in old libc versions of Linux,
827 * it might return an error that we will silently ignore.
828 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100829 if (!ext)
830 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100831#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200832
Willy Tarreau40aa0702013-03-10 23:51:38 +0100833 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200834 switch (listener->addr.ss_family) {
835 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200836 if (1
837#if defined(IP_TRANSPARENT)
838 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
839#endif
840#if defined(IP_FREEBIND)
841 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
842#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200843#if defined(IP_BINDANY)
844 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
845#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200846#if defined(SO_BINDANY)
847 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
848#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200849 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200850 msg = "cannot make listening socket transparent";
851 err |= ERR_ALERT;
852 }
853 break;
854 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200855 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200856#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200857 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
858#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100859#if defined(IP_FREEBIND)
860 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
861#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200862#if defined(IPV6_BINDANY)
863 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
864#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200865#if defined(SO_BINDANY)
866 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
867#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200868 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200869 msg = "cannot make listening socket transparent";
870 err |= ERR_ALERT;
871 }
872 break;
873 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100874 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200875
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100876#ifdef SO_BINDTODEVICE
877 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100878 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100879 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100880 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100881 msg = "cannot bind listener to device";
882 err |= ERR_WARN;
883 }
884 }
885#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400886#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100887 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400888 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200889 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
890 msg = "cannot set MSS";
891 err |= ERR_WARN;
892 }
893 }
894#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100895#if defined(TCP_USER_TIMEOUT)
896 if (listener->tcp_ut) {
897 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
898 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
899 msg = "cannot set TCP User Timeout";
900 err |= ERR_WARN;
901 }
902 }
903#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200904#if defined(TCP_DEFER_ACCEPT)
905 if (listener->options & LI_O_DEF_ACCEPT) {
906 /* defer accept by up to one second */
907 int accept_delay = 1;
908 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
909 msg = "cannot enable DEFER_ACCEPT";
910 err |= ERR_WARN;
911 }
912 }
913#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200914#if defined(TCP_FASTOPEN)
915 if (listener->options & LI_O_TCP_FO) {
916 /* TFO needs a queue length, let's use the configured backlog */
917 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
918 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
919 msg = "cannot enable TCP_FASTOPEN";
920 err |= ERR_WARN;
921 }
922 }
923#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100924#if defined(IPV6_V6ONLY)
925 if (listener->options & LI_O_V6ONLY)
926 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100927 else if (listener->options & LI_O_V4V6)
928 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100929#endif
930
Willy Tarreau40aa0702013-03-10 23:51:38 +0100931 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100932 err |= ERR_RETRYABLE | ERR_ALERT;
933 msg = "cannot bind socket";
934 goto tcp_close_return;
935 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100936
Willy Tarreau40aa0702013-03-10 23:51:38 +0100937 ready = 0;
938 ready_len = sizeof(ready);
939 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
940 ready = 0;
941
942 if (!(ext && ready) && /* only listen if not already done by external process */
943 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100944 err |= ERR_RETRYABLE | ERR_ALERT;
945 msg = "cannot listen to socket";
946 goto tcp_close_return;
947 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100948
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400949#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200950 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900951 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200952#endif
953
Willy Tarreaue6b98942007-10-29 01:09:36 +0100954 /* the socket is ready */
955 listener->fd = fd;
956 listener->state = LI_LISTEN;
957
Willy Tarreaueabf3132008-08-29 23:36:51 +0200958 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200959 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200960 fd_insert(fd);
961
Willy Tarreaue6b98942007-10-29 01:09:36 +0100962 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100963 if (msg && errlen) {
964 char pn[INET6_ADDRSTRLEN];
965
Willy Tarreau631f01c2011-09-05 00:36:48 +0200966 addr_to_str(&listener->addr, pn, sizeof(pn));
967 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100968 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100969 return err;
970
971 tcp_close_return:
972 close(fd);
973 goto tcp_return;
974}
975
976/* This function creates all TCP sockets bound to the protocol entry <proto>.
977 * It is intended to be used as the protocol's bind_all() function.
978 * The sockets will be registered but not added to any fd_set, in order not to
979 * loose them across the fork(). A call to enable_all_listeners() is needed
980 * to complete initialization. The return value is composed from ERR_*.
981 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200982static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100983{
984 struct listener *listener;
985 int err = ERR_NONE;
986
987 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200988 err |= tcp_bind_listener(listener, errmsg, errlen);
989 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100990 break;
991 }
992
993 return err;
994}
995
996/* Add listener to the list of tcpv4 listeners. The listener's state
997 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
998 * listeners is updated. This is the function to use to add a new listener.
999 */
1000void tcpv4_add_listener(struct listener *listener)
1001{
1002 if (listener->state != LI_INIT)
1003 return;
1004 listener->state = LI_ASSIGNED;
1005 listener->proto = &proto_tcpv4;
1006 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1007 proto_tcpv4.nb_listeners++;
1008}
1009
1010/* Add listener to the list of tcpv4 listeners. The listener's state
1011 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1012 * listeners is updated. This is the function to use to add a new listener.
1013 */
1014void tcpv6_add_listener(struct listener *listener)
1015{
1016 if (listener->state != LI_INIT)
1017 return;
1018 listener->state = LI_ASSIGNED;
1019 listener->proto = &proto_tcpv6;
1020 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1021 proto_tcpv6.nb_listeners++;
1022}
1023
Willy Tarreau092d8652014-07-07 20:22:12 +02001024/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1025 * was totally stopped, or > 0 if correctly paused.
1026 */
1027int tcp_pause_listener(struct listener *l)
1028{
1029 if (shutdown(l->fd, SHUT_WR) != 0)
1030 return -1; /* Solaris dies here */
1031
1032 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1033 return -1; /* OpenBSD dies here */
1034
1035 if (shutdown(l->fd, SHUT_RD) != 0)
1036 return -1; /* should always be OK */
1037 return 1;
1038}
1039
Willy Tarreauedcf6682008-11-30 23:15:34 +01001040/* This function performs the TCP request analysis on the current request. It
1041 * returns 1 if the processing can continue on next analysers, or zero if it
1042 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +02001043 * request. It relies on buffers flags, and updates s->req->analysers. The
1044 * function may be called for frontend rules and backend rules. It only relies
1045 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001046 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001047int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +01001048{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001049 struct session *sess = s->sess;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001050 struct act_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +02001051 struct stksess *ts;
1052 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001053 int partial;
Willy Tarreauacc98002015-09-27 23:34:39 +02001054 int act_flags = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001055
Willy Tarreau87b09662015-04-03 00:22:06 +02001056 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreauedcf6682008-11-30 23:15:34 +01001057 now_ms, __FUNCTION__,
1058 s,
1059 req,
1060 req->rex, req->wex,
1061 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001062 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +01001063 req->analysers);
1064
Willy Tarreauedcf6682008-11-30 23:15:34 +01001065 /* We don't know whether we have enough data, so must proceed
1066 * this way :
1067 * - iterate through all rules in their declaration order
1068 * - if one rule returns MISS, it means the inspect delay is
1069 * not over yet, then return immediately, otherwise consider
1070 * it as a non-match.
1071 * - if one rule returns OK, then return OK
1072 * - if one rule returns KO, then return KO
1073 */
1074
Willy Tarreau9b28e032012-10-12 23:49:43 +02001075 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001076 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001077 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001078 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001079 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001080
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001081 /* If "the current_rule_list" match the executed rule list, we are in
1082 * resume condition. If a resume is needed it is always in the action
1083 * and never in the ACL or converters. In this case, we initialise the
1084 * current rule, and go to the action execution point.
1085 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001086 if (s->current_rule) {
1087 rule = s->current_rule;
1088 s->current_rule = NULL;
1089 if (s->current_rule_list == &s->be->tcp_req.inspect_rules)
1090 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001091 }
1092 s->current_rule_list = &s->be->tcp_req.inspect_rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001093
Willy Tarreaufb356202010-08-03 14:02:05 +02001094 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001095 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001096
1097 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001098 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001099 if (ret == ACL_TEST_MISS)
1100 goto missing_data;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001101
1102 ret = acl_pass(ret);
1103 if (rule->cond->pol == ACL_COND_UNLESS)
1104 ret = !ret;
1105 }
1106
1107 if (ret) {
Willy Tarreauacc98002015-09-27 23:34:39 +02001108 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001109resume_execution:
Willy Tarreauedcf6682008-11-30 23:15:34 +01001110 /* we have a matching rule. */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001111 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreau27f78242015-07-04 11:36:30 +02001112 break;
1113 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001114 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001115 channel_abort(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001116 channel_abort(&s->res);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001117 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001118
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001119 s->be->be_counters.denied_req++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001120 sess->fe->fe_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001121 if (sess->listener->counters)
1122 sess->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001123
Willy Tarreaue7dff022015-04-03 01:14:29 +02001124 if (!(s->flags & SF_ERR_MASK))
1125 s->flags |= SF_ERR_PRXCOND;
1126 if (!(s->flags & SF_FINST_MASK))
1127 s->flags |= SF_FINST_R;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001128 return 0;
1129 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001130 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001131 /* Note: only the first valid tracking parameter of each
1132 * applies.
1133 */
1134 struct stktable_key *key;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001135 struct sample smp;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001136
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001137 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001138 continue;
1139
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001140 t = rule->arg.trk_ctr.table.t;
1141 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | partial, rule->arg.trk_ctr.expr, &smp);
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001142
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001143 if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL))
1144 goto missing_data; /* key might appear later */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001145
1146 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001147 stream_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001148 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001149 if (sess->fe != s->be)
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001150 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreaud1f96522010-08-03 19:34:32 +02001151 }
1152 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001153 else if (rule->action == ACT_TCP_CAPTURE) {
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001154 struct sample *key;
Thierry FOURNIERd0d65ae2015-08-04 08:21:12 +02001155 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001156 char **cap = s->req_cap;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001157 int len;
1158
Thierry FOURNIERd0d65ae2015-08-04 08:21:12 +02001159 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ | partial, rule->arg.cap.expr, SMP_T_STR);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001160 if (!key)
1161 continue;
1162
1163 if (key->flags & SMP_F_MAY_CHANGE)
1164 goto missing_data;
1165
1166 if (cap[h->index] == NULL)
1167 cap[h->index] = pool_alloc2(h->pool);
1168
1169 if (cap[h->index] == NULL) /* no more capture memory */
1170 continue;
1171
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001172 len = key->data.u.str.len;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001173 if (len > h->len)
1174 len = h->len;
1175
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001176 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001177 cap[h->index][len] = 0;
1178 }
Willy Tarreaud1f96522010-08-03 19:34:32 +02001179 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001180 /* Custom keywords. */
Thierry FOURNIER42148732015-09-02 17:17:33 +02001181 if (!rule->action_ptr)
1182 continue;
Willy Tarreauc1b10d32015-09-27 10:06:24 +02001183
Willy Tarreauacc98002015-09-27 23:34:39 +02001184 if (partial & SMP_OPT_FINAL)
1185 act_flags |= ACT_FLAG_FINAL;
1186
1187 switch (rule->action_ptr(rule, s->be, s->sess, s, act_flags)) {
Thierry FOURNIER42148732015-09-02 17:17:33 +02001188 case ACT_RET_ERR:
1189 case ACT_RET_CONT:
1190 continue;
1191 case ACT_RET_STOP:
Thierry FOURNIER561a0f92015-05-29 17:29:14 +02001192 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001193 case ACT_RET_YIELD:
1194 s->current_rule = rule;
1195 goto missing_data;
1196 }
1197 break; /* ACT_RET_STOP */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001198 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001199 }
1200 }
1201
1202 /* if we get there, it means we have no rule which matches, or
1203 * we have an explicit accept, so we apply the default accept.
1204 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001205 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001206 req->analyse_exp = TICK_ETERNITY;
1207 return 1;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001208
1209 missing_data:
1210 channel_dont_connect(req);
1211 /* just set the request timeout once at the beginning of the request */
1212 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
1213 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
1214 return 0;
1215
Willy Tarreauedcf6682008-11-30 23:15:34 +01001216}
1217
Emeric Brun97679e72010-09-23 17:56:44 +02001218/* This function performs the TCP response analysis on the current response. It
1219 * returns 1 if the processing can continue on next analysers, or zero if it
1220 * needs more data, encounters an error, or wants to immediately abort the
1221 * response. It relies on buffers flags, and updates s->rep->analysers. The
1222 * function may be called for backend rules.
1223 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001224int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001225{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001226 struct session *sess = s->sess;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001227 struct act_rule *rule;
Emeric Brun97679e72010-09-23 17:56:44 +02001228 int partial;
Willy Tarreauacc98002015-09-27 23:34:39 +02001229 int act_flags = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001230
Willy Tarreau87b09662015-04-03 00:22:06 +02001231 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun97679e72010-09-23 17:56:44 +02001232 now_ms, __FUNCTION__,
1233 s,
1234 rep,
1235 rep->rex, rep->wex,
1236 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001237 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001238 rep->analysers);
1239
1240 /* We don't know whether we have enough data, so must proceed
1241 * this way :
1242 * - iterate through all rules in their declaration order
1243 * - if one rule returns MISS, it means the inspect delay is
1244 * not over yet, then return immediately, otherwise consider
1245 * it as a non-match.
1246 * - if one rule returns OK, then return OK
1247 * - if one rule returns KO, then return KO
1248 */
1249
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001250 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001251 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001252 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001253 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001254
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001255 /* If "the current_rule_list" match the executed rule list, we are in
1256 * resume condition. If a resume is needed it is always in the action
1257 * and never in the ACL or converters. In this case, we initialise the
1258 * current rule, and go to the action execution point.
1259 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001260 if (s->current_rule) {
1261 rule = s->current_rule;
1262 s->current_rule = NULL;
1263 if (s->current_rule_list == &s->be->tcp_rep.inspect_rules)
1264 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001265 }
1266 s->current_rule_list = &s->be->tcp_rep.inspect_rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001267
Emeric Brun97679e72010-09-23 17:56:44 +02001268 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001269 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001270
1271 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001272 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001273 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001274 /* just set the analyser timeout once at the beginning of the response */
1275 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001276 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001277 return 0;
1278 }
1279
1280 ret = acl_pass(ret);
1281 if (rule->cond->pol == ACL_COND_UNLESS)
1282 ret = !ret;
1283 }
1284
1285 if (ret) {
Willy Tarreauacc98002015-09-27 23:34:39 +02001286 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001287resume_execution:
Emeric Brun97679e72010-09-23 17:56:44 +02001288 /* we have a matching rule. */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001289 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreau27f78242015-07-04 11:36:30 +02001290 break;
1291 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001292 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001293 channel_abort(rep);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001294 channel_abort(&s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001295 rep->analysers = 0;
1296
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001297 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001298 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001299 if (sess->listener->counters)
1300 sess->listener->counters->denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001301
Willy Tarreaue7dff022015-04-03 01:14:29 +02001302 if (!(s->flags & SF_ERR_MASK))
1303 s->flags |= SF_ERR_PRXCOND;
1304 if (!(s->flags & SF_FINST_MASK))
1305 s->flags |= SF_FINST_D;
Emeric Brun97679e72010-09-23 17:56:44 +02001306 return 0;
1307 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001308 else if (rule->action == ACT_TCP_CLOSE) {
Willy Tarreau73796532014-11-28 14:10:28 +01001309 chn_prod(rep)->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1310 si_shutr(chn_prod(rep));
1311 si_shutw(chn_prod(rep));
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001312 break;
1313 }
Emeric Brun97679e72010-09-23 17:56:44 +02001314 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001315 /* Custom keywords. */
Thierry FOURNIER42148732015-09-02 17:17:33 +02001316 if (!rule->action_ptr)
1317 continue;
Willy Tarreauacc98002015-09-27 23:34:39 +02001318
1319 if (partial & SMP_OPT_FINAL)
1320 act_flags |= ACT_FLAG_FINAL;
1321
1322 switch (rule->action_ptr(rule, s->be, s->sess, s, act_flags)) {
Thierry FOURNIER42148732015-09-02 17:17:33 +02001323 case ACT_RET_ERR:
1324 case ACT_RET_CONT:
1325 continue;
1326 case ACT_RET_STOP:
Thierry FOURNIER561a0f92015-05-29 17:29:14 +02001327 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001328 case ACT_RET_YIELD:
1329 channel_dont_close(rep);
1330 s->current_rule = rule;
1331 return 0;
1332 }
1333 break; /* ACT_RET_STOP */
Emeric Brun97679e72010-09-23 17:56:44 +02001334 }
1335 }
1336 }
1337
1338 /* if we get there, it means we have no rule which matches, or
1339 * we have an explicit accept, so we apply the default accept.
1340 */
1341 rep->analysers &= ~an_bit;
1342 rep->analyse_exp = TICK_ETERNITY;
1343 return 1;
1344}
1345
1346
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001347/* This function performs the TCP layer4 analysis on the current request. It
1348 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1349 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001350 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001351 */
Willy Tarreaue73ef852015-04-04 16:41:45 +02001352int tcp_exec_req_rules(struct session *sess)
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001353{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001354 struct act_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001355 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001356 struct stktable *t = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001357 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001358 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001359 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001360
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001361 if (!conn)
1362 return result;
1363
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001364 list_for_each_entry(rule, &sess->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001365 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001366
1367 if (rule->cond) {
Willy Tarreaue73ef852015-04-04 16:41:45 +02001368 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001369 ret = acl_pass(ret);
1370 if (rule->cond->pol == ACL_COND_UNLESS)
1371 ret = !ret;
1372 }
1373
1374 if (ret) {
1375 /* we have a matching rule. */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001376 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreau27f78242015-07-04 11:36:30 +02001377 break;
1378 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001379 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001380 sess->fe->fe_counters.denied_conn++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001381 if (sess->listener->counters)
1382 sess->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001383
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001384 result = 0;
1385 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001386 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001387 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001388 /* Note: only the first valid tracking parameter of each
1389 * applies.
1390 */
1391 struct stktable_key *key;
1392
Willy Tarreau70f454e2015-04-04 16:38:07 +02001393 if (stkctr_entry(&sess->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001394 continue;
1395
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001396 t = rule->arg.trk_ctr.table.t;
1397 key = stktable_fetch_key(t, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001398
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001399 if (key && (ts = stktable_get_entry(t, key)))
Willy Tarreau70f454e2015-04-04 16:38:07 +02001400 stream_track_stkctr(&sess->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001401 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001402 else if (rule->action == ACT_TCP_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001403 conn->flags |= CO_FL_ACCEPT_PROXY;
1404 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001405 }
Bertrand Jacquin90759682016-06-06 15:35:39 +01001406 else if (rule->action == ACT_TCP_EXPECT_CIP) {
1407 conn->flags |= CO_FL_ACCEPT_CIP;
1408 conn_sock_want_recv(conn);
1409 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001410 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001411 /* Custom keywords. */
Willy Tarreau45059e92015-09-28 17:50:10 +02001412 if (!rule->action_ptr)
Thierry FOURNIER42148732015-09-02 17:17:33 +02001413 break;
Willy Tarreauacc98002015-09-27 23:34:39 +02001414 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_FLAG_FINAL | ACT_FLAG_FIRST)) {
Thierry FOURNIER42148732015-09-02 17:17:33 +02001415 case ACT_RET_YIELD:
1416 /* yield is not allowed at this point. If this return code is
1417 * used it is a bug, so I prefer to abort the process.
1418 */
1419 send_log(sess->fe, LOG_WARNING,
1420 "Internal error: yield not allowed with tcp-request connection actions.");
1421 case ACT_RET_STOP:
1422 break;
1423 case ACT_RET_CONT:
1424 continue;
1425 case ACT_RET_ERR:
1426 result = 0;
1427 break;
Thierry FOURNIERc89f4f52015-08-11 09:48:02 +02001428 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02001429 break; /* ACT_RET_STOP */
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001430 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001431 }
1432 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001433 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001434}
1435
William Lallemand2e785f22016-05-25 01:48:42 +02001436/*
1437 * Execute the "set-src" action. May be called from {tcp,http}request
1438 */
1439enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1440 struct session *sess, struct stream *s, int flags)
1441{
1442 struct connection *cli_conn;
1443
1444 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1445 struct sample *smp;
1446
1447 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1448 if (smp) {
1449 if (smp->data.type == SMP_T_IPV4) {
1450 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
1451 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1452 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
1453 } else if (smp->data.type == SMP_T_IPV6) {
1454 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
1455 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1456 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
1457 }
1458 }
William Lallemand01252ed2016-05-25 02:33:16 +02001459 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001460 }
1461 return ACT_RET_CONT;
1462}
1463
William Lallemand44be6402016-05-25 01:51:35 +02001464/*
William Lallemand13e9b0c2016-05-25 02:34:07 +02001465 * Execute the "set-dst" action. May be called from {tcp,http}request
1466 */
1467enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1468 struct session *sess, struct stream *s, int flags)
1469{
1470 struct connection *cli_conn;
1471
1472 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1473 struct sample *smp;
1474
1475 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1476 if (smp) {
1477 if (smp->data.type == SMP_T_IPV4) {
1478 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_family = AF_INET;
1479 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1480 } else if (smp->data.type == SMP_T_IPV6) {
1481 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_family = AF_INET6;
1482 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
1483 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = 0;
1484 }
1485 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1486 }
1487 }
1488 return ACT_RET_CONT;
1489}
1490
1491/*
William Lallemand44be6402016-05-25 01:51:35 +02001492 * Execute the "set-src-port" action. May be called from {tcp,http}request
1493 * We must test the sin_family before setting the port
1494 */
1495enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1496 struct session *sess, struct stream *s, int flags)
1497{
1498 struct connection *cli_conn;
1499
1500 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1501 struct sample *smp;
1502
1503 conn_get_from_addr(cli_conn);
1504
1505 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1506 if (smp) {
1507 if (((struct sockaddr_storage *)&cli_conn->addr.from)->ss_family == AF_INET) {
1508 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
1509 } else if (((struct sockaddr_storage *)&cli_conn->addr.from)->ss_family == AF_INET6) {
1510 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
1511 }
1512 }
1513 }
1514 return ACT_RET_CONT;
1515}
1516
William Lallemand13e9b0c2016-05-25 02:34:07 +02001517/*
1518 * Execute the "set-dst-port" action. May be called from {tcp,http}request
1519 * We must test the sin_family before setting the port
1520 */
1521enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1522 struct session *sess, struct stream *s, int flags)
1523{
1524 struct connection *cli_conn;
1525
1526 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1527 struct sample *smp;
1528
1529 conn_get_to_addr(cli_conn);
1530
1531 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1532 if (smp) {
1533 if (((struct sockaddr_storage *)&cli_conn->addr.to)->ss_family == AF_INET) {
1534 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_port = htons(smp->data.u.sint);
1535 } else if (((struct sockaddr_storage *)&cli_conn->addr.to)->ss_family == AF_INET6) {
1536 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = htons(smp->data.u.sint);
1537 }
1538 }
1539 }
1540 return ACT_RET_CONT;
1541}
1542
Willy Tarreau2d392c22015-08-24 01:43:45 +02001543/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1544static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1545{
1546 struct connection *conn = objt_conn(sess->origin);
1547
1548 if (!conn)
1549 goto out;
1550
1551 if (!conn_ctrl_ready(conn))
1552 goto out;
1553
Willy Tarreau2d392c22015-08-24 01:43:45 +02001554#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001555 /* drain is needed only to send the quick ACK */
1556 conn_sock_drain(conn);
1557
Willy Tarreau2d392c22015-08-24 01:43:45 +02001558 /* re-enable quickack if it was disabled to ack all data and avoid
1559 * retransmits from the client that might trigger a real reset.
1560 */
1561 setsockopt(conn->t.sock.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
1562#endif
1563 /* lingering must absolutely be disabled so that we don't send a
1564 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1565 * is present, returning with ERR will cause lingering to be disabled.
1566 */
1567 if (strm)
1568 strm->si[0].flags |= SI_FL_NOLINGER;
1569
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001570 /* We're on the client-facing side, we must force to disable lingering to
1571 * ensure we will use an RST exclusively and kill any pending data.
1572 */
1573 fdtab[conn->t.sock.fd].linger_risk = 1;
1574
Willy Tarreau2d392c22015-08-24 01:43:45 +02001575#ifdef TCP_REPAIR
1576 if (setsockopt(conn->t.sock.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
1577 /* socket will be quiet now */
1578 goto out;
1579 }
1580#endif
1581 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1582 * Let's fall back on the TTL trick, though it only works for routed
1583 * network and has no effect on local net.
1584 */
1585#ifdef IP_TTL
1586 setsockopt(conn->t.sock.fd, SOL_IP, IP_TTL, &one, sizeof(one));
1587#endif
1588 out:
1589 /* kill the stream if any */
1590 if (strm) {
1591 channel_abort(&strm->req);
1592 channel_abort(&strm->res);
1593 strm->req.analysers = 0;
1594 strm->res.analysers = 0;
1595 strm->be->be_counters.denied_req++;
1596 if (!(strm->flags & SF_ERR_MASK))
1597 strm->flags |= SF_ERR_PRXCOND;
1598 if (!(strm->flags & SF_FINST_MASK))
1599 strm->flags |= SF_FINST_R;
1600 }
1601
1602 sess->fe->fe_counters.denied_req++;
1603 if (sess->listener->counters)
1604 sess->listener->counters->denied_req++;
1605
1606 return ACT_RET_STOP;
1607}
1608
Emeric Brun97679e72010-09-23 17:56:44 +02001609/* Parse a tcp-response rule. Return a negative value in case of failure */
1610static int tcp_parse_response_rule(char **args, int arg, int section_type,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001611 struct proxy *curpx, struct proxy *defpx,
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001612 struct act_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001613 unsigned int where,
1614 const char *file, int line)
Emeric Brun97679e72010-09-23 17:56:44 +02001615{
1616 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001617 memprintf(err, "%s %s is only allowed in 'backend' sections",
1618 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001619 return -1;
1620 }
1621
1622 if (strcmp(args[arg], "accept") == 0) {
1623 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001624 rule->action = ACT_ACTION_ALLOW;
Emeric Brun97679e72010-09-23 17:56:44 +02001625 }
1626 else if (strcmp(args[arg], "reject") == 0) {
1627 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001628 rule->action = ACT_ACTION_DENY;
Emeric Brun97679e72010-09-23 17:56:44 +02001629 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001630 else if (strcmp(args[arg], "close") == 0) {
1631 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001632 rule->action = ACT_TCP_CLOSE;
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001633 }
Emeric Brun97679e72010-09-23 17:56:44 +02001634 else {
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001635 struct action_kw *kw;
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001636 kw = tcp_res_cont_action(args[arg]);
1637 if (kw) {
1638 arg++;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001639 rule->from = ACT_F_TCP_RES_CNT;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02001640 rule->kw = kw;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02001641 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001642 return -1;
1643 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001644 action_build_list(&tcp_res_cont_keywords, &trash);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001645 memprintf(err,
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001646 "'%s %s' expects 'accept', 'close', 'reject', %s in %s '%s' (got '%s')",
1647 args[0], args[1], trash.str, proxy_type_str(curpx), curpx->id, args[arg]);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001648 return -1;
1649 }
Emeric Brun97679e72010-09-23 17:56:44 +02001650 }
1651
1652 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001653 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001654 memprintf(err,
1655 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1656 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001657 return -1;
1658 }
1659 }
1660 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001661 memprintf(err,
1662 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001663 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1664 return -1;
1665 }
1666 return 0;
1667}
1668
1669
1670
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001671/* Parse a tcp-request rule. Return a negative value in case of failure */
1672static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001673 struct proxy *curpx, struct proxy *defpx,
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001674 struct act_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001675 unsigned int where, const char *file, int line)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001676{
1677 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001678 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1679 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001680 return -1;
1681 }
1682
1683 if (!strcmp(args[arg], "accept")) {
1684 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001685 rule->action = ACT_ACTION_ALLOW;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001686 }
1687 else if (!strcmp(args[arg], "reject")) {
1688 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001689 rule->action = ACT_ACTION_DENY;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001690 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001691 else if (strcmp(args[arg], "capture") == 0) {
1692 struct sample_expr *expr;
1693 struct cap_hdr *hdr;
1694 int kw = arg;
1695 int len = 0;
1696
1697 if (!(curpx->cap & PR_CAP_FE)) {
1698 memprintf(err,
1699 "'%s %s %s' : proxy '%s' has no frontend capability",
1700 args[0], args[1], args[kw], curpx->id);
1701 return -1;
1702 }
1703
1704 if (!(where & SMP_VAL_FE_REQ_CNT)) {
1705 memprintf(err,
1706 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1707 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1708 return -1;
1709 }
1710
1711 arg++;
1712
1713 curpx->conf.args.ctx = ARGC_CAP;
1714 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
1715 if (!expr) {
1716 memprintf(err,
1717 "'%s %s %s' : %s",
1718 args[0], args[1], args[kw], *err);
1719 return -1;
1720 }
1721
1722 if (!(expr->fetch->val & where)) {
1723 memprintf(err,
1724 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
1725 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
1726 free(expr);
1727 return -1;
1728 }
1729
1730 if (strcmp(args[arg], "len") == 0) {
1731 arg++;
1732 if (!args[arg]) {
1733 memprintf(err,
1734 "'%s %s %s' : missing length value",
1735 args[0], args[1], args[kw]);
1736 free(expr);
1737 return -1;
1738 }
1739 /* we copy the table name for now, it will be resolved later */
1740 len = atoi(args[arg]);
1741 if (len <= 0) {
1742 memprintf(err,
1743 "'%s %s %s' : length must be > 0",
1744 args[0], args[1], args[kw]);
1745 free(expr);
1746 return -1;
1747 }
1748 arg++;
1749 }
1750
1751 if (!len) {
1752 memprintf(err,
1753 "'%s %s %s' : a positive 'len' argument is mandatory",
1754 args[0], args[1], args[kw]);
1755 free(expr);
1756 return -1;
1757 }
1758
Vincent Bernat02779b62016-04-03 13:48:43 +02001759 hdr = calloc(1, sizeof(*hdr));
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001760 hdr->next = curpx->req_cap;
1761 hdr->name = NULL; /* not a header capture */
1762 hdr->namelen = 0;
1763 hdr->len = len;
1764 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
1765 hdr->index = curpx->nb_req_cap++;
1766
1767 curpx->req_cap = hdr;
1768 curpx->to_log |= LW_REQHDR;
1769
1770 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
1771 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1772
Thierry FOURNIERd0d65ae2015-08-04 08:21:12 +02001773 rule->arg.cap.expr = expr;
1774 rule->arg.cap.hdr = hdr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001775 rule->action = ACT_TCP_CAPTURE;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001776 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001777 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1778 args[arg][9] == '\0' && args[arg][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02001779 args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001780 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001781 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001782
1783 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001784
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001785 curpx->conf.args.ctx = ARGC_TRK;
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001786 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001787 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001788 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001789 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001790 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001791 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001792 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001793
Willy Tarreau80aca902013-01-07 15:42:20 +01001794 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001795 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001796 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001797 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001798 free(expr);
1799 return -1;
1800 }
1801
1802 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001803 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001804
1805 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001806 arg++;
1807 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001808 memprintf(err,
1809 "'%s %s %s' : missing table name",
1810 args[0], args[1], args[kw]);
1811 free(expr);
1812 return -1;
1813 }
1814 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001815 rule->arg.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001816 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001817 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001818 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001819 rule->action = ACT_ACTION_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001820 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001821 else if (strcmp(args[arg], "expect-proxy") == 0) {
1822 if (strcmp(args[arg+1], "layer4") != 0) {
1823 memprintf(err,
1824 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1825 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1826 return -1;
1827 }
1828
1829 if (!(where & SMP_VAL_FE_CON_ACC)) {
1830 memprintf(err,
1831 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1832 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1833 return -1;
1834 }
1835
1836 arg += 2;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001837 rule->action = ACT_TCP_EXPECT_PX;
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001838 }
Bertrand Jacquin90759682016-06-06 15:35:39 +01001839 else if (strcmp(args[arg], "expect-netscaler-cip") == 0) {
1840 if (strcmp(args[arg+1], "layer4") != 0) {
1841 memprintf(err,
1842 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1843 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1844 return -1;
1845 }
1846
1847 if (!(where & SMP_VAL_FE_CON_ACC)) {
1848 memprintf(err,
1849 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1850 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1851 return -1;
1852 }
1853
1854 arg += 2;
1855 rule->action = ACT_TCP_EXPECT_CIP;
1856 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001857 else {
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001858 struct action_kw *kw;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001859 if (where & SMP_VAL_FE_CON_ACC) {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001860 kw = tcp_req_conn_action(args[arg]);
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02001861 rule->kw = kw;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001862 rule->from = ACT_F_TCP_REQ_CON;
1863 } else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001864 kw = tcp_req_cont_action(args[arg]);
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02001865 rule->kw = kw;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001866 rule->from = ACT_F_TCP_REQ_CNT;
1867 }
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001868 if (kw) {
1869 arg++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02001870 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001871 return -1;
1872 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001873 if (where & SMP_VAL_FE_CON_ACC)
1874 action_build_list(&tcp_req_conn_keywords, &trash);
1875 else
1876 action_build_list(&tcp_req_cont_keywords, &trash);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001877 memprintf(err,
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001878 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d', %s "
1879 "in %s '%s' (got '%s').\n",
1880 args[0], args[1], MAX_SESS_STKCTR-1, trash.str, proxy_type_str(curpx),
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001881 curpx->id, args[arg]);
1882 return -1;
1883 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001884 }
1885
1886 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001887 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001888 memprintf(err,
1889 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1890 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001891 return -1;
1892 }
1893 }
1894 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001895 memprintf(err,
1896 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001897 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1898 return -1;
1899 }
1900 return 0;
1901}
1902
Emeric Brun97679e72010-09-23 17:56:44 +02001903/* This function should be called to parse a line starting with the "tcp-response"
1904 * keyword.
1905 */
1906static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001907 struct proxy *defpx, const char *file, int line,
1908 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001909{
1910 const char *ptr = NULL;
1911 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001912 int warn = 0;
1913 int arg;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001914 struct act_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001915 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001916 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001917 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001918
1919 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001920 memprintf(err, "missing argument for '%s' in %s '%s'",
1921 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001922 return -1;
1923 }
1924
1925 if (strcmp(args[1], "inspect-delay") == 0) {
1926 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001927 memprintf(err, "%s %s is only allowed in 'backend' sections",
1928 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001929 return -1;
1930 }
1931
1932 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001933 memprintf(err,
1934 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1935 args[0], args[1], proxy_type_str(curpx), curpx->id);
1936 if (ptr)
1937 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001938 return -1;
1939 }
1940
1941 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001942 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1943 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001944 return 1;
1945 }
1946 curpx->tcp_rep.inspect_delay = val;
1947 return 0;
1948 }
1949
Simon Hormande072bd2011-06-24 15:11:37 +09001950 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001951 LIST_INIT(&rule->list);
1952 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001953 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001954
1955 if (strcmp(args[1], "content") == 0) {
1956 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001957
1958 if (curpx->cap & PR_CAP_FE)
1959 where |= SMP_VAL_FE_RES_CNT;
1960 if (curpx->cap & PR_CAP_BE)
1961 where |= SMP_VAL_BE_RES_CNT;
1962
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001963 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001964 goto error;
1965
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001966 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1967 if (acl) {
1968 if (acl->name && *acl->name)
1969 memprintf(err,
1970 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1971 acl->name, args[0], args[1], sample_ckp_names(where));
1972 else
1973 memprintf(err,
1974 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1975 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001976 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001977 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001978
Emeric Brun97679e72010-09-23 17:56:44 +02001979 warn++;
1980 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001981 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1982 if (acl->name && *acl->name)
1983 memprintf(err,
1984 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001985 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001986 else
1987 memprintf(err,
1988 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001989 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001990 warn++;
1991 }
Emeric Brun97679e72010-09-23 17:56:44 +02001992
1993 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1994 }
1995 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001996 memprintf(err,
1997 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1998 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001999 goto error;
2000 }
2001
2002 return warn;
2003 error:
2004 free(rule);
2005 return -1;
2006}
2007
2008
Willy Tarreaub6866442008-07-14 23:54:42 +02002009/* This function should be called to parse a line starting with the "tcp-request"
2010 * keyword.
2011 */
2012static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02002013 struct proxy *defpx, const char *file, int line,
2014 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02002015{
2016 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02002017 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02002018 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002019 int arg;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002020 struct act_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01002021 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002022 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02002023 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02002024
2025 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002026 if (curpx == defpx)
2027 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
2028 else
2029 memprintf(err, "missing argument for '%s' in %s '%s'",
2030 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02002031 return -1;
2032 }
2033
2034 if (!strcmp(args[1], "inspect-delay")) {
2035 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002036 memprintf(err, "%s %s is not allowed in 'defaults' sections",
2037 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02002038 return -1;
2039 }
2040
Willy Tarreaub6866442008-07-14 23:54:42 +02002041 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002042 memprintf(err,
2043 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
2044 args[0], args[1], proxy_type_str(curpx), curpx->id);
2045 if (ptr)
2046 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02002047 return -1;
2048 }
2049
2050 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002051 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
2052 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02002053 return 1;
2054 }
2055 curpx->tcp_req.inspect_delay = val;
2056 return 0;
2057 }
2058
Simon Hormande072bd2011-06-24 15:11:37 +09002059 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02002060 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002061 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01002062 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002063
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002064 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02002065 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01002066
2067 if (curpx->cap & PR_CAP_FE)
2068 where |= SMP_VAL_FE_REQ_CNT;
2069 if (curpx->cap & PR_CAP_BE)
2070 where |= SMP_VAL_BE_REQ_CNT;
2071
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01002072 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002073 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02002074
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002075 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
2076 if (acl) {
2077 if (acl->name && *acl->name)
2078 memprintf(err,
2079 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
2080 acl->name, args[0], args[1], sample_ckp_names(where));
2081 else
2082 memprintf(err,
2083 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
2084 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02002085 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002086 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002087
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002088 warn++;
2089 }
2090 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
2091 if (acl->name && *acl->name)
2092 memprintf(err,
2093 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002094 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002095 else
2096 memprintf(err,
2097 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002098 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002099 warn++;
2100 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01002101
Willy Tarreau3986b9c2014-09-16 15:39:51 +02002102 /* the following function directly emits the warning */
2103 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02002104 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002105 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002106 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002107 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002108
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002109 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002110 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
2111 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09002112 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002113 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002114
Willy Tarreau80aca902013-01-07 15:42:20 +01002115 where |= SMP_VAL_FE_CON_ACC;
2116
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01002117 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002118 goto error;
2119
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002120 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
2121 if (acl) {
2122 if (acl->name && *acl->name)
2123 memprintf(err,
2124 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
2125 acl->name, args[0], args[1], sample_ckp_names(where));
2126 else
2127 memprintf(err,
2128 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
2129 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02002130 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002131 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002132
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002133 warn++;
2134 }
2135 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
2136 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002137 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002138 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002139 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002140 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002141 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002142 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002143 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002144 warn++;
2145 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01002146
Willy Tarreau3986b9c2014-09-16 15:39:51 +02002147 /* the following function directly emits the warning */
2148 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02002149 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002150 }
Willy Tarreau1a687942010-05-23 22:40:30 +02002151 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002152 if (curpx == defpx)
2153 memprintf(err,
2154 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
2155 args[0], args[1]);
2156 else
2157 memprintf(err,
2158 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02002159 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002160 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02002161 }
2162
Willy Tarreau1a687942010-05-23 22:40:30 +02002163 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002164 error:
2165 free(rule);
2166 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02002167}
2168
William Lallemand13e9b0c2016-05-25 02:34:07 +02002169/* parse "set-{src,dst}[-port]" action */
2170enum 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 +02002171{
2172 int cur_arg;
2173 struct sample_expr *expr;
2174 unsigned int where;
2175
2176 cur_arg = *orig_arg;
2177 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
2178 if (!expr)
2179 return ACT_RET_PRS_ERR;
2180
2181 where = 0;
2182 if (proxy->cap & PR_CAP_FE)
2183 where |= SMP_VAL_FE_HRQ_HDR;
2184 if (proxy->cap & PR_CAP_BE)
2185 where |= SMP_VAL_BE_HRQ_HDR;
2186
2187 if (!(expr->fetch->val & where)) {
2188 memprintf(err,
2189 "fetch method '%s' extracts information from '%s', none of which is available here",
2190 args[cur_arg-1], sample_src_names(expr->fetch->use));
2191 free(expr);
2192 return ACT_RET_PRS_ERR;
2193 }
2194 rule->arg.expr = expr;
2195 rule->action = ACT_CUSTOM;
2196
2197 if (!strcmp(args[*orig_arg-1], "set-src")) {
2198 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02002199 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
2200 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02002201 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
2202 rule->action_ptr = tcp_action_req_set_dst;
2203 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
2204 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02002205 } else {
2206 return ACT_RET_PRS_ERR;
2207 }
2208
2209 (*orig_arg)++;
2210
2211 return ACT_RET_PRS_OK;
2212}
2213
2214
Willy Tarreau2d392c22015-08-24 01:43:45 +02002215/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
2216 * success, ACT_RET_PRS_ERR on error.
2217 */
2218static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
2219 struct act_rule *rule, char **err)
2220{
2221 rule->action = ACT_CUSTOM;
2222 rule->action_ptr = tcp_exec_action_silent_drop;
2223 return ACT_RET_PRS_OK;
2224}
2225
Willy Tarreau645513a2010-05-24 20:55:15 +02002226
2227/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002228/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02002229/************************************************************************/
2230
Willy Tarreau4a129812012-04-25 17:31:42 +02002231/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02002232int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002233{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002234 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002235
2236 if (!cli_conn)
2237 return 0;
2238
2239 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01002240 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002241 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002242 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002243 break;
2244 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002245 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002246 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002247 break;
2248 default:
Emeric Brunf769f512010-10-22 17:14:01 +02002249 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002250 }
Emeric Brunf769f512010-10-22 17:14:01 +02002251
Willy Tarreau37406352012-04-23 16:16:37 +02002252 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002253 return 1;
2254}
2255
Willy Tarreaua5e37562011-12-16 17:06:15 +01002256/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002257static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002258smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002259{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002260 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002261
2262 if (!cli_conn)
2263 return 0;
2264
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002265 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002266 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02002267 return 0;
2268
Willy Tarreau37406352012-04-23 16:16:37 +02002269 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002270 return 1;
2271}
2272
Willy Tarreau4a129812012-04-25 17:31:42 +02002273/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02002274static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002275smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002276{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002277 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02002278
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002279 if (!cli_conn)
2280 return 0;
2281
2282 conn_get_to_addr(cli_conn);
2283
2284 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01002285 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002286 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002287 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002288 break;
2289 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002290 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002291 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002292 break;
2293 default:
Emeric Brunf769f512010-10-22 17:14:01 +02002294 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002295 }
Emeric Brunf769f512010-10-22 17:14:01 +02002296
Willy Tarreau37406352012-04-23 16:16:37 +02002297 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002298 return 1;
2299}
2300
Willy Tarreau16e01562016-08-09 16:46:18 +02002301/* check if the destination address of the front connection is local to the
2302 * system or if it was intercepted.
2303 */
2304int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
2305{
2306 struct connection *conn = objt_conn(smp->sess->origin);
2307 struct listener *li = smp->sess->listener;
2308
2309 if (!conn)
2310 return 0;
2311
2312 conn_get_to_addr(conn);
2313 if (!(conn->flags & CO_FL_ADDR_TO_SET))
2314 return 0;
2315
2316 smp->data.type = SMP_T_BOOL;
2317 smp->flags = 0;
2318 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.to);
2319 return smp->data.u.sint >= 0;
2320}
2321
2322/* check if the source address of the front connection is local to the system
2323 * or not.
2324 */
2325int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
2326{
2327 struct connection *conn = objt_conn(smp->sess->origin);
2328 struct listener *li = smp->sess->listener;
2329
2330 if (!conn)
2331 return 0;
2332
2333 conn_get_from_addr(conn);
2334 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
2335 return 0;
2336
2337 smp->data.type = SMP_T_BOOL;
2338 smp->flags = 0;
2339 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.from);
2340 return smp->data.u.sint >= 0;
2341}
2342
Willy Tarreaua5e37562011-12-16 17:06:15 +01002343/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002344static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002345smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002346{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002347 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002348
2349 if (!cli_conn)
2350 return 0;
2351
2352 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02002353
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002354 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002355 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02002356 return 0;
2357
Willy Tarreau37406352012-04-23 16:16:37 +02002358 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002359 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01002360}
2361
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02002362#ifdef TCP_INFO
2363
2364/* Returns some tcp_info data is its avalaible. "dir" must be set to 0 if
2365 * the client connection is require, otherwise it is set to 1. "val" represents
2366 * the required value. Use 0 for rtt and 1 for rttavg. "unit" is the expected unit
2367 * by default, the rtt is in us. Id "unit" is set to 0, the unit is us, if it is
2368 * set to 1, the untis are milliseconds.
2369 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
2370 */
2371static inline int get_tcp_info(const struct arg *args, struct sample *smp,
2372 int dir, int val)
2373{
2374 struct connection *conn;
2375 struct tcp_info info;
2376 socklen_t optlen;
2377
2378 /* strm can be null. */
2379 if (!smp->strm)
2380 return 0;
2381
2382 /* get the object associated with the stream interface.The
2383 * object can be other thing than a connection. For example,
2384 * it be a appctx. */
2385 conn = objt_conn(smp->strm->si[dir].end);
2386 if (!conn)
2387 return 0;
2388
2389 /* The fd may not be avalaible for the tcp_info struct, and the
2390 syscal can fail. */
2391 optlen = sizeof(info);
2392 if (getsockopt(conn->t.sock.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
2393 return 0;
2394
2395 /* extract the value. */
2396 smp->data.type = SMP_T_SINT;
2397 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07002398 case 0: smp->data.u.sint = info.tcpi_rtt; break;
2399 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
2400#if defined(__linux__)
2401 /* these ones are common to all Linux versions */
2402 case 2: smp->data.u.sint = info.tcpi_unacked; break;
2403 case 3: smp->data.u.sint = info.tcpi_sacked; break;
2404 case 4: smp->data.u.sint = info.tcpi_lost; break;
2405 case 5: smp->data.u.sint = info.tcpi_retrans; break;
2406 case 6: smp->data.u.sint = info.tcpi_fackets; break;
2407 case 7: smp->data.u.sint = info.tcpi_reordering; break;
2408#elif defined(__FreeBSD__) || defined(__NetBSD__)
2409 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
2410 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
2411 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
2412 case 4: smp->data.u.sint = info.__tcpi_lost; break;
2413 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
2414 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
2415 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
2416#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02002417 default: return 0;
2418 }
2419
2420 /* Convert the value as expected. */
2421 if (args) {
2422 if (args[0].type == ARGT_STR) {
2423 if (strcmp(args[0].data.str.str, "us") == 0) {
2424 /* Do nothing. */
2425 } else if (strcmp(args[0].data.str.str, "ms") == 0) {
2426 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
2427 } else
2428 return 0;
2429 } else if (args[0].type == ARGT_STOP) {
2430 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
2431 } else
2432 return 0;
2433 }
2434
2435 return 1;
2436}
2437
2438/* get the mean rtt of a client connexion */
2439static int
2440smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2441{
2442 if (!get_tcp_info(args, smp, 0, 0))
2443 return 0;
2444 return 1;
2445}
2446
2447/* get the variance of the mean rtt of a client connexion */
2448static int
2449smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
2450{
2451 if (!get_tcp_info(args, smp, 0, 1))
2452 return 0;
2453 return 1;
2454}
Joe Williams30fcd392016-08-10 07:06:44 -07002455
2456#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
2457
2458/* get the unacked counter on a client connexion */
2459static int
2460smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
2461{
2462 if (!get_tcp_info(args, smp, 0, 2))
2463 return 0;
2464 return 1;
2465}
2466
2467/* get the sacked counter on a client connexion */
2468static int
2469smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
2470{
2471 if (!get_tcp_info(args, smp, 0, 3))
2472 return 0;
2473 return 1;
2474}
2475
2476/* get the lost counter on a client connexion */
2477static int
2478smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
2479{
2480 if (!get_tcp_info(args, smp, 0, 4))
2481 return 0;
2482 return 1;
2483}
2484
2485/* get the retrans counter on a client connexion */
2486static int
2487smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
2488{
2489 if (!get_tcp_info(args, smp, 0, 5))
2490 return 0;
2491 return 1;
2492}
2493
2494/* get the fackets counter on a client connexion */
2495static int
2496smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
2497{
2498 if (!get_tcp_info(args, smp, 0, 6))
2499 return 0;
2500 return 1;
2501}
2502
2503/* get the reordering counter on a client connexion */
2504static int
2505smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
2506{
2507 if (!get_tcp_info(args, smp, 0, 7))
2508 return 0;
2509 return 1;
2510}
2511#endif // linux || freebsd || netbsd
2512#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02002513
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002514#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002515/* parse the "v4v6" bind keyword */
2516static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2517{
2518 struct listener *l;
2519
2520 list_for_each_entry(l, &conf->listeners, by_bind) {
2521 if (l->addr.ss_family == AF_INET6)
2522 l->options |= LI_O_V4V6;
2523 }
2524
2525 return 0;
2526}
2527
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002528/* parse the "v6only" bind keyword */
2529static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2530{
2531 struct listener *l;
2532
2533 list_for_each_entry(l, &conf->listeners, by_bind) {
2534 if (l->addr.ss_family == AF_INET6)
2535 l->options |= LI_O_V6ONLY;
2536 }
2537
2538 return 0;
2539}
2540#endif
2541
Pieter Baauwd551fb52013-05-08 22:49:23 +02002542#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002543/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002544static 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 +02002545{
2546 struct listener *l;
2547
Willy Tarreau4348fad2012-09-20 16:48:07 +02002548 list_for_each_entry(l, &conf->listeners, by_bind) {
2549 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2550 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02002551 }
2552
Willy Tarreau44791242012-09-12 23:27:21 +02002553 return 0;
2554}
2555#endif
2556
2557#ifdef TCP_DEFER_ACCEPT
2558/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002559static 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 +02002560{
2561 struct listener *l;
2562
Willy Tarreau4348fad2012-09-20 16:48:07 +02002563 list_for_each_entry(l, &conf->listeners, by_bind) {
2564 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2565 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02002566 }
2567
Willy Tarreau44791242012-09-12 23:27:21 +02002568 return 0;
2569}
2570#endif
2571
Willy Tarreau1c862c52012-10-05 16:21:00 +02002572#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01002573/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02002574static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2575{
2576 struct listener *l;
2577
2578 list_for_each_entry(l, &conf->listeners, by_bind) {
2579 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2580 l->options |= LI_O_TCP_FO;
2581 }
2582
2583 return 0;
2584}
2585#endif
2586
Willy Tarreau44791242012-09-12 23:27:21 +02002587#ifdef TCP_MAXSEG
2588/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002589static 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 +02002590{
2591 struct listener *l;
2592 int mss;
2593
Willy Tarreau44791242012-09-12 23:27:21 +02002594 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002595 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002596 return ERR_ALERT | ERR_FATAL;
2597 }
2598
2599 mss = atoi(args[cur_arg + 1]);
2600 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002601 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002602 return ERR_ALERT | ERR_FATAL;
2603 }
2604
Willy Tarreau4348fad2012-09-20 16:48:07 +02002605 list_for_each_entry(l, &conf->listeners, by_bind) {
2606 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2607 l->maxseg = mss;
2608 }
Willy Tarreau44791242012-09-12 23:27:21 +02002609
2610 return 0;
2611}
2612#endif
2613
Willy Tarreau2af207a2015-02-04 00:45:58 +01002614#ifdef TCP_USER_TIMEOUT
2615/* parse the "tcp-ut" bind keyword */
2616static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2617{
2618 const char *ptr = NULL;
2619 struct listener *l;
2620 unsigned int timeout;
2621
2622 if (!*args[cur_arg + 1]) {
2623 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
2624 return ERR_ALERT | ERR_FATAL;
2625 }
2626
2627 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
2628 if (ptr) {
2629 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
2630 return ERR_ALERT | ERR_FATAL;
2631 }
2632
2633 list_for_each_entry(l, &conf->listeners, by_bind) {
2634 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2635 l->tcp_ut = timeout;
2636 }
2637
2638 return 0;
2639}
2640#endif
2641
Willy Tarreau44791242012-09-12 23:27:21 +02002642#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01002643/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002644static 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 +02002645{
2646 struct listener *l;
2647
Willy Tarreau44791242012-09-12 23:27:21 +02002648 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002649 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002650 return ERR_ALERT | ERR_FATAL;
2651 }
2652
Willy Tarreau4348fad2012-09-20 16:48:07 +02002653 list_for_each_entry(l, &conf->listeners, by_bind) {
2654 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2655 l->interface = strdup(args[cur_arg + 1]);
2656 }
Willy Tarreau44791242012-09-12 23:27:21 +02002657
2658 global.last_checks |= LSTCHK_NETADM;
2659 return 0;
2660}
2661#endif
2662
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002663#ifdef CONFIG_HAP_NS
2664/* parse the "namespace" bind keyword */
2665static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2666{
2667 struct listener *l;
2668 char *namespace = NULL;
2669
2670 if (!*args[cur_arg + 1]) {
2671 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
2672 return ERR_ALERT | ERR_FATAL;
2673 }
2674 namespace = args[cur_arg + 1];
2675
2676 list_for_each_entry(l, &conf->listeners, by_bind) {
2677 l->netns = netns_store_lookup(namespace, strlen(namespace));
2678
2679 if (l->netns == NULL)
2680 l->netns = netns_store_insert(namespace);
2681
2682 if (l->netns == NULL) {
2683 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
2684 return ERR_ALERT | ERR_FATAL;
2685 }
2686 }
2687 return 0;
2688}
2689#endif
2690
Willy Tarreau163d4622015-10-13 16:16:41 +02002691#ifdef TCP_USER_TIMEOUT
2692/* parse the "tcp-ut" server keyword */
2693static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2694{
2695 const char *ptr = NULL;
2696 unsigned int timeout;
2697
2698 if (!*args[*cur_arg + 1]) {
2699 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
2700 return ERR_ALERT | ERR_FATAL;
2701 }
2702
2703 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
2704 if (ptr) {
2705 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
2706 return ERR_ALERT | ERR_FATAL;
2707 }
2708
2709 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
2710 newsrv->tcp_ut = timeout;
2711
2712 return 0;
2713}
2714#endif
2715
Willy Tarreaudc13c112013-06-21 23:16:39 +02002716static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002717 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02002718 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02002719 { 0, NULL, NULL },
2720}};
2721
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002722
Willy Tarreau61612d42012-04-19 18:42:05 +02002723/* Note: must not be declared <const> as its list will be overwritten.
2724 * Please take care of keeping this list alphabetically sorted.
2725 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002726static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002727 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02002728}};
2729
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002730
Willy Tarreau4a129812012-04-25 17:31:42 +02002731/* Note: must not be declared <const> as its list will be overwritten.
2732 * Note: fetches that may return multiple types must be declared as the lowest
2733 * common denominator, the type that can be casted into all other ones. For
2734 * instance v4/v6 must be declared v4.
2735 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002736static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002737 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02002738 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002739 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002740 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02002741 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002742 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02002743#ifdef TCP_INFO
Joe Williams30fcd392016-08-10 07:06:44 -07002744 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2745 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2746#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
2747 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2748 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2749 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2750 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2751 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2752 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2753#endif // linux || freebsd || netbsd
2754#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002755 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002756}};
2757
Willy Tarreau44791242012-09-12 23:27:21 +02002758/************************************************************************/
2759/* All supported bind keywords must be declared here. */
2760/************************************************************************/
2761
2762/* Note: must not be declared <const> as its list will be overwritten.
2763 * Please take care of keeping this list alphabetically sorted, doing so helps
2764 * all code contributors.
2765 * Optional keywords are also declared with a NULL ->parse() function so that
2766 * the config parser can report an appropriate error when a known keyword was
2767 * not enabled.
2768 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002769static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02002770#ifdef TCP_DEFER_ACCEPT
2771 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
2772#endif
2773#ifdef SO_BINDTODEVICE
2774 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
2775#endif
2776#ifdef TCP_MAXSEG
2777 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
2778#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01002779#ifdef TCP_USER_TIMEOUT
2780 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
2781#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02002782#ifdef TCP_FASTOPEN
2783 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
2784#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02002785#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002786 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
2787#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002788#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002789 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002790 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
2791#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002792#ifdef CONFIG_HAP_NS
2793 { "namespace", bind_parse_namespace, 1 },
2794#endif
Willy Tarreau44791242012-09-12 23:27:21 +02002795 /* the versions with the NULL parse function*/
2796 { "defer-accept", NULL, 0 },
2797 { "interface", NULL, 1 },
2798 { "mss", NULL, 1 },
2799 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002800 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002801 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002802 { NULL, NULL, 0 },
2803}};
2804
Willy Tarreau163d4622015-10-13 16:16:41 +02002805static struct srv_kw_list srv_kws = { "TCP", { }, {
2806#ifdef TCP_USER_TIMEOUT
2807 { "tcp-ut", srv_parse_tcp_ut, 1, 0 }, /* set TCP user timeout on server */
2808#endif
2809 { NULL, NULL, 0 },
2810}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02002811
2812static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002813 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002814 { "set-src", tcp_parse_set_src_dst },
2815 { "set-src-port", tcp_parse_set_src_dst },
2816 { "set-dst" , tcp_parse_set_src_dst },
2817 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002818 { /* END */ }
2819}};
2820
2821static struct action_kw_list tcp_req_cont_actions = {ILH, {
2822 { "silent-drop", tcp_parse_silent_drop },
2823 { /* END */ }
2824}};
2825
2826static struct action_kw_list tcp_res_cont_actions = {ILH, {
2827 { "silent-drop", tcp_parse_silent_drop },
2828 { /* END */ }
2829}};
2830
2831static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002832 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002833 { "set-src", tcp_parse_set_src_dst },
2834 { "set-src-port", tcp_parse_set_src_dst },
2835 { "set-dst", tcp_parse_set_src_dst },
2836 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002837 { /* END */ }
2838}};
2839
2840static struct action_kw_list http_res_actions = {ILH, {
2841 { "silent-drop", tcp_parse_silent_drop },
2842 { /* END */ }
2843}};
2844
2845
Willy Tarreaue6b98942007-10-29 01:09:36 +01002846__attribute__((constructor))
2847static void __tcp_protocol_init(void)
2848{
2849 protocol_register(&proto_tcpv4);
2850 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002851 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02002852 cfg_register_keywords(&cfg_kws);
2853 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02002854 bind_register_keywords(&bind_kws);
Willy Tarreau163d4622015-10-13 16:16:41 +02002855 srv_register_keywords(&srv_kws);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002856 tcp_req_conn_keywords_register(&tcp_req_conn_actions);
2857 tcp_req_cont_keywords_register(&tcp_req_cont_actions);
2858 tcp_res_cont_keywords_register(&tcp_res_cont_actions);
2859 http_req_keywords_register(&http_req_actions);
2860 http_res_keywords_register(&http_res_actions);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002861}
2862
2863
2864/*
2865 * Local variables:
2866 * c-indent-level: 8
2867 * c-basic-offset: 8
2868 * End:
2869 */