blob: 8b3d546fd1e8bd819844d2d220f9dcedb47aba45 [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);
Willy Tarreau620408f2016-10-21 16:37:51 +020073struct list tcp_req_sess_keywords = LIST_HEAD_INIT(tcp_req_sess_keywords);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +010074struct list tcp_req_cont_keywords = LIST_HEAD_INIT(tcp_req_cont_keywords);
75struct list tcp_res_cont_keywords = LIST_HEAD_INIT(tcp_res_cont_keywords);
76
Willy Tarreaue6b98942007-10-29 01:09:36 +010077/* Note: must not be declared <const> as its list will be overwritten */
78static struct protocol proto_tcpv4 = {
79 .name = "tcpv4",
80 .sock_domain = AF_INET,
81 .sock_type = SOCK_STREAM,
82 .sock_prot = IPPROTO_TCP,
83 .sock_family = AF_INET,
84 .sock_addrlen = sizeof(struct sockaddr_in),
85 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020086 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020087 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020088 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010089 .bind_all = tcp_bind_listeners,
90 .unbind_all = unbind_all_listeners,
91 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020092 .get_src = tcp_get_src,
93 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020094 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +020095 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010096 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
97 .nb_listeners = 0,
98};
99
100/* Note: must not be declared <const> as its list will be overwritten */
101static struct protocol proto_tcpv6 = {
102 .name = "tcpv6",
103 .sock_domain = AF_INET6,
104 .sock_type = SOCK_STREAM,
105 .sock_prot = IPPROTO_TCP,
106 .sock_family = AF_INET6,
107 .sock_addrlen = sizeof(struct sockaddr_in6),
108 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200109 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200110 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200111 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100112 .bind_all = tcp_bind_listeners,
113 .unbind_all = unbind_all_listeners,
114 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200115 .get_src = tcp_get_src,
116 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200117 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +0200118 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100119 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
120 .nb_listeners = 0,
121};
122
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100123/*
124 * Register keywords.
125 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200126void tcp_req_conn_keywords_register(struct action_kw_list *kw_list)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100127{
128 LIST_ADDQ(&tcp_req_conn_keywords, &kw_list->list);
129}
130
Willy Tarreau620408f2016-10-21 16:37:51 +0200131void tcp_req_sess_keywords_register(struct action_kw_list *kw_list)
132{
133 LIST_ADDQ(&tcp_req_sess_keywords, &kw_list->list);
134}
135
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200136void tcp_req_cont_keywords_register(struct action_kw_list *kw_list)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100137{
138 LIST_ADDQ(&tcp_req_cont_keywords, &kw_list->list);
139}
140
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200141void tcp_res_cont_keywords_register(struct action_kw_list *kw_list)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100142{
143 LIST_ADDQ(&tcp_res_cont_keywords, &kw_list->list);
144}
145
146/*
Willy Tarreau620408f2016-10-21 16:37:51 +0200147 * Return the struct tcp_req_action_kw associated to a keyword.
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100148 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200149static struct action_kw *tcp_req_conn_action(const char *kw)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100150{
Thierry FOURNIER322a1242015-08-19 09:07:47 +0200151 return action_lookup(&tcp_req_conn_keywords, kw);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100152}
153
Willy Tarreau620408f2016-10-21 16:37:51 +0200154static struct action_kw *tcp_req_sess_action(const char *kw)
155{
156 return action_lookup(&tcp_req_sess_keywords, kw);
157}
158
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200159static struct action_kw *tcp_req_cont_action(const char *kw)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100160{
Thierry FOURNIER322a1242015-08-19 09:07:47 +0200161 return action_lookup(&tcp_req_cont_keywords, kw);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100162}
163
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200164static struct action_kw *tcp_res_cont_action(const char *kw)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100165{
Thierry FOURNIER322a1242015-08-19 09:07:47 +0200166 return action_lookup(&tcp_res_cont_keywords, kw);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100167}
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100168
David du Colombier6f5ccb12011-03-10 22:26:24 +0100169/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100170 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
171 * - 0 : ignore remote address (may even be a NULL pointer)
172 * - 1 : use provided address
173 * - 2 : use provided port
174 * - 3 : use both
175 *
176 * The function supports multiple foreign binding methods :
177 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100178 * The second one can be used as a fallback for the first one.
179 * This function returns 0 when everything's OK, 1 if it could not bind, to the
180 * local address, 2 if it could not bind to the foreign address.
181 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100182int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100183{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100184 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100185 int foreign_ok = 0;
186 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100187 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200188 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200189
David du Colombier65c17962012-07-13 14:34:59 +0200190 switch (local->ss_family) {
191 case AF_INET:
192 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200193 /* This deserves some explanation. Some platforms will support
194 * multiple combinations of certain methods, so we try the
195 * supported ones until one succeeds.
196 */
197 if (0
198#if defined(IP_TRANSPARENT)
199 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
200#endif
201#if defined(IP_FREEBIND)
202 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
203#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200204#if defined(IP_BINDANY)
205 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
206#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200207#if defined(SO_BINDANY)
208 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
209#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200210 )
David du Colombier65c17962012-07-13 14:34:59 +0200211 foreign_ok = 1;
212 else
213 ip_transp_working = 0;
214 }
215 break;
216 case AF_INET6:
217 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200218 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200219#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200220 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
221#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100222#if defined(IP_FREEBIND)
223 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
224#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200225#if defined(IPV6_BINDANY)
226 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
227#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200228#if defined(SO_BINDANY)
229 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
230#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200231 )
David du Colombier65c17962012-07-13 14:34:59 +0200232 foreign_ok = 1;
233 else
234 ip6_transp_working = 0;
235 }
236 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100237 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200238
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100239 if (flags) {
240 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200241 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100242 switch (remote->ss_family) {
243 case AF_INET:
244 if (flags & 1)
245 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
246 if (flags & 2)
247 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
248 break;
249 case AF_INET6:
250 if (flags & 1)
251 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
252 if (flags & 2)
253 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
254 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100255 default:
256 /* we don't want to try to bind to an unknown address family */
257 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100258 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100259 }
260
Simon Hormande072bd2011-06-24 15:11:37 +0900261 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100262 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200263 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200264 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
265 if (ret < 0)
266 return 2;
267 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100268 }
269 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200270 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200271 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
272 if (ret < 0)
273 return 1;
274 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100275 }
276
277 if (!flags)
278 return 0;
279
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100280 if (!foreign_ok)
281 /* we could not bind to a foreign address */
282 return 2;
283
284 return 0;
285}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100286
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100287static int create_server_socket(struct connection *conn)
288{
Willy Tarreau529c1392014-12-24 13:47:55 +0100289 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100290
Willy Tarreau529c1392014-12-24 13:47:55 +0100291#ifdef CONFIG_HAP_NS
292 if (objt_server(conn->target)) {
293 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
294 ns = conn->proxy_netns;
295 else
296 ns = __objt_server(conn->target)->netns;
297 }
298#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100299 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
300}
Willy Tarreau9650f372009-08-16 14:02:45 +0200301
302/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200303 * This function initiates a TCP connection establishment to the target assigned
304 * to connection <conn> using (si->{target,addr.to}). A source address may be
305 * pointed to by conn->addr.from in case of transparent proxying. Normal source
306 * bind addresses are still determined locally (due to the possible need of a
307 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100308 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100309 * supported. The <data> parameter is a boolean indicating whether there are data
310 * waiting for being sent or not, in order to adjust data write polling and on
311 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
312 * allows the caller to force using a delayed ACK when establishing the connection :
313 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
314 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
315 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200316 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200317 * Note that a pending send_proxy message accounts for data.
318 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200319 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200320 * - SF_ERR_NONE if everything's OK
321 * - SF_ERR_SRVTO if there are no more servers
322 * - SF_ERR_SRVCL if the connection was refused by the server
323 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
324 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
325 * - SF_ERR_INTERNAL for any other purely internal errors
326 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100327 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200328 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100329 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200330 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100331
Willy Tarreauf0837b22012-11-24 10:24:27 +0100332int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200333{
334 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100335 struct server *srv;
336 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100337 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100338
Willy Tarreau9ce70132014-01-24 16:08:19 +0100339 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
340
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100341 switch (obj_type(conn->target)) {
342 case OBJ_TYPE_PROXY:
343 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100344 srv = NULL;
345 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100346 case OBJ_TYPE_SERVER:
347 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100348 be = srv->proxy;
349 break;
350 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100351 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200352 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100353 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200354
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100355 fd = conn->t.sock.fd = create_server_socket(conn);
356
357 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200358 qfprintf(stderr, "Cannot get a server socket.\n");
359
Willy Tarreau9ce70132014-01-24 16:08:19 +0100360 if (errno == ENFILE) {
361 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200362 send_log(be, LOG_EMERG,
363 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
364 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100365 }
366 else if (errno == EMFILE) {
367 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200368 send_log(be, LOG_EMERG,
369 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
370 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100371 }
372 else if (errno == ENOBUFS || errno == ENOMEM) {
373 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200374 send_log(be, LOG_EMERG,
375 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
376 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100377 }
378 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
379 conn->err_code = CO_ER_NOPROTO;
380 }
381 else
382 conn->err_code = CO_ER_SOCK_ERR;
383
Willy Tarreau9650f372009-08-16 14:02:45 +0200384 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100385 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200386 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200387 }
388
389 if (fd >= global.maxsock) {
390 /* do not log anything there, it's a normal condition when this option
391 * is used to serialize connections to a server !
392 */
393 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
394 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100395 conn->err_code = CO_ER_CONF_FDLIM;
396 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200397 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200398 }
399
400 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900401 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200402 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
403 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100404 conn->err_code = CO_ER_SOCK_ERR;
405 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200406 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200407 }
408
409 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900410 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200411
Willy Tarreau9650f372009-08-16 14:02:45 +0200412 /* allow specific binding :
413 * - server-specific at first
414 * - proxy-specific next
415 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100416 if (srv && srv->conn_src.opts & CO_SRC_BIND)
417 src = &srv->conn_src;
418 else if (be->conn_src.opts & CO_SRC_BIND)
419 src = &be->conn_src;
420 else
421 src = NULL;
422
423 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200424 int ret, flags = 0;
425
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200426 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100427 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100428 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200429 conn->flags |= CO_FL_PRIVATE;
430 /* fall through */
431 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200432 flags = 3;
433 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100434 case CO_SRC_TPROXY_CIP:
435 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200436 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200437 flags = 1;
438 break;
439 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200440 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200441
Willy Tarreau9650f372009-08-16 14:02:45 +0200442#ifdef SO_BINDTODEVICE
443 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100444 if (src->iface_name)
445 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200446#endif
447
Willy Tarreaua4380b42012-12-08 22:49:11 +0100448 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200449 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100450 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200451
452 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200453 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200454
455 do {
456 /* note: in case of retry, we may have to release a previously
457 * allocated port, hence this loop's construct.
458 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200459 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
460 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200461
462 if (!attempts)
463 break;
464 attempts--;
465
Willy Tarreaua4380b42012-12-08 22:49:11 +0100466 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100467 if (!fdinfo[fd].local_port) {
468 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100470 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200471
Willy Tarreaua4380b42012-12-08 22:49:11 +0100472 fdinfo[fd].port_range = src->sport_range;
473 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200474
Willy Tarreaua4380b42012-12-08 22:49:11 +0100475 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100476 if (ret != 0)
477 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200478 } while (ret != 0); /* binding NOK */
479 }
480 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000481#ifdef IP_BIND_ADDRESS_NO_PORT
482 static int bind_address_no_port = 1;
483 setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
484#endif
Willy Tarreaua4380b42012-12-08 22:49:11 +0100485 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100486 if (ret != 0)
487 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200488 }
489
Willy Tarreaua4380b42012-12-08 22:49:11 +0100490 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200491 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
492 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200493 close(fd);
494
Willy Tarreau9650f372009-08-16 14:02:45 +0200495 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100496 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200497 be->id);
498 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100499 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200500 be->id);
501 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100502 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200503 be->id);
504 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100505 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200506 be->id);
507 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100508 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200509 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200510 }
511 }
512
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400513#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200514 /* disabling tcp quick ack now allows the first request to leave the
515 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100516 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200517 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200518 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900519 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200520#endif
521
Willy Tarreau163d4622015-10-13 16:16:41 +0200522#ifdef TCP_USER_TIMEOUT
523 /* there is not much more we can do here when it fails, it's still minor */
524 if (srv && srv->tcp_ut)
525 setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut));
526#endif
Willy Tarreaue803de22010-01-21 17:43:04 +0100527 if (global.tune.server_sndbuf)
528 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
529
530 if (global.tune.server_rcvbuf)
531 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
532
Willy Tarreau986a9d22012-08-30 21:11:38 +0200533 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200534 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
535
Willy Tarreaub1719512012-12-08 23:03:28 +0100536 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200537 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100538 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200539 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100540 conn->err_code = CO_ER_FREE_PORTS;
541 }
542 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200543 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100544 conn->err_code = CO_ER_ADDR_INUSE;
545 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200546
Willy Tarreaub1719512012-12-08 23:03:28 +0100547 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200548 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
549 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200550 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100551 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100552 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200553 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200554 } else if (errno == ETIMEDOUT) {
555 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200556 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
557 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200558 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100559 conn->err_code = CO_ER_SOCK_ERR;
560 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200561 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200562 } else {
563 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
564 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200565 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
566 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200567 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100568 conn->err_code = CO_ER_SOCK_ERR;
569 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200570 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200571 }
572 }
573
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100574 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200575
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200576 /* Prepare to send a few handshakes related to the on-wire protocol. */
577 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200578 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200579
Willy Tarreauf79c8172013-10-21 16:30:56 +0200580 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100581 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200582 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200583
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200584 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200585 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100586 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200587 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200588 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200589
Willy Tarreau14f8e862012-08-30 22:23:13 +0200590 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200591 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200592
Willy Tarreaue7dff022015-04-03 01:14:29 +0200593 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200594}
595
596
Willy Tarreau59b94792012-05-11 16:16:40 +0200597/*
598 * Retrieves the source address for the socket <fd>, with <dir> indicating
599 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
600 * success, -1 in case of error. The socket's source address is stored in
601 * <sa> for <salen> bytes.
602 */
603int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
604{
605 if (dir)
606 return getsockname(fd, sa, &salen);
607 else
608 return getpeername(fd, sa, &salen);
609}
610
611
612/*
613 * Retrieves the original destination address for the socket <fd>, with <dir>
614 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
615 * listener, if the original destination address was translated, the original
616 * address is retrieved. It returns 0 in case of success, -1 in case of error.
617 * The socket's source address is stored in <sa> for <salen> bytes.
618 */
619int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
620{
621 if (dir)
622 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100623 else {
624 int ret = getsockname(fd, sa, &salen);
625
626 if (ret < 0)
627 return ret;
628
Willy Tarreau59b94792012-05-11 16:16:40 +0200629#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100630 /* For TPROXY and Netfilter's NAT, we can retrieve the original
631 * IPv4 address before DNAT/REDIRECT. We must not do that with
632 * other families because v6-mapped IPv4 addresses are still
633 * reported as v4.
634 */
635 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
636 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
637 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200638#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100639 return ret;
640 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200641}
642
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200643/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100644 * receive shutdown. Returns positive if the shutdown was found, negative
645 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
646 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200647 */
648int tcp_drain(int fd)
649{
650 int turns = 2;
651 int len;
652
653 while (turns) {
654#ifdef MSG_TRUNC_CLEARS_INPUT
655 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
656 if (len == -1 && errno == EFAULT)
657#endif
658 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
659
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100660 if (len == 0) {
661 /* cool, shutdown received */
662 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200663 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100664 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200665
666 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100667 if (errno == EAGAIN) {
668 /* connection not closed yet */
669 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100670 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100671 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200672 if (errno == EINTR) /* oops, try again */
673 continue;
674 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100675 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200676 return 1;
677 }
678 /* OK we read some data, let's try again once */
679 turns--;
680 }
681 /* some data are still present, give up */
682 return 0;
683}
684
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200685/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100686 * and we have nothing to send. It updates the FD polling status. It returns 0
687 * if it fails in a fatal way or needs to poll to go further, otherwise it
688 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
689 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
690 * errno. The error checking is done in two passes in order to limit the number
691 * of syscalls in the normal case :
692 * - if POLL_ERR was reported by the poller, we check for a pending error on
693 * the socket before proceeding. If found, it's assigned to errno so that
694 * upper layers can see it.
695 * - otherwise connect() is used to check the connection state again, since
696 * the getsockopt return cannot reliably be used to know if the connection
697 * is still pending or ready. This one may often return an error as well,
698 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200699 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200700int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200701{
Willy Tarreau239d7182012-07-23 18:53:03 +0200702 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100703 socklen_t lskerr;
704 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200705
Willy Tarreau80184712012-07-06 14:54:49 +0200706 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200707 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200708
Willy Tarreau3c728722014-01-23 13:50:42 +0100709 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200710 return 0;
711
Willy Tarreau80184712012-07-06 14:54:49 +0200712 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200713 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200714
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100715 if (!fd_send_ready(fd))
716 return 0;
717
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100718 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
719 * without FD_POLL_IN also indicates a hangup without input data meaning
720 * there was no connection.
721 */
722 if (fdtab[fd].ev & FD_POLL_ERR ||
723 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
724 skerr = 0;
725 lskerr = sizeof(skerr);
726 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
727 errno = skerr;
728 if (errno == EAGAIN)
729 errno = 0;
730 if (errno)
731 goto out_error;
732 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200733
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100734 /* Use connect() to check the state of the socket. This has the
735 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200736 * - error
737 * - connecting (EALREADY, EINPROGRESS)
738 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200739 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200740 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200741 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100742 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100743 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200744 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200745 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200746
Willy Tarreau2c6be842012-07-06 17:12:34 +0200747 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200748 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200749
Willy Tarreau2c6be842012-07-06 17:12:34 +0200750 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200751 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200752
Willy Tarreau076be252012-07-06 16:02:29 +0200753 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200754 * forward the event to the transport layer which will notify the
755 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200756 */
Willy Tarreau80184712012-07-06 14:54:49 +0200757 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200758 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200759
760 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200761 /* Write error on the file descriptor. Report it to the connection
762 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200763 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100764 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100765 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100766 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200767 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200768}
769
Willy Tarreau59b94792012-05-11 16:16:40 +0200770
Willy Tarreaue6b98942007-10-29 01:09:36 +0100771/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100772 * an error message in <errmsg> if the message is at most <errlen> bytes long
773 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
774 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100775 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
776 * was alright and that no message was returned. ERR_RETRYABLE means that an
777 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700778 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100779 * the meaning of the error, but just indicate that a message is present which
780 * should be displayed with the respective level. Last, ERR_ABORT indicates
781 * that it's pointless to try to start other listeners. No error message is
782 * returned if errlen is NULL.
783 */
784int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
785{
786 __label__ tcp_return, tcp_close_return;
787 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100788 int ext, ready;
789 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100790 const char *msg = NULL;
791
792 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100793 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100794 *errmsg = 0;
795
796 if (listener->state != LI_ASSIGNED)
797 return ERR_NONE; /* already bound */
798
799 err = ERR_NONE;
800
Willy Tarreau40aa0702013-03-10 23:51:38 +0100801 /* if the listener already has an fd assigned, then we were offered the
802 * fd by an external process (most likely the parent), and we don't want
803 * to create a new socket. However we still want to set a few flags on
804 * the socket.
805 */
806 fd = listener->fd;
807 ext = (fd >= 0);
808
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100809 if (!ext) {
810 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
811
812 if (fd == -1) {
813 err |= ERR_RETRYABLE | ERR_ALERT;
814 msg = "cannot create listening socket";
815 goto tcp_return;
816 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100817 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100818
Willy Tarreaue6b98942007-10-29 01:09:36 +0100819 if (fd >= global.maxsock) {
820 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
821 msg = "not enough free sockets (raise '-n' parameter)";
822 goto tcp_close_return;
823 }
824
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200825 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100826 err |= ERR_FATAL | ERR_ALERT;
827 msg = "cannot make socket non-blocking";
828 goto tcp_close_return;
829 }
830
Willy Tarreau40aa0702013-03-10 23:51:38 +0100831 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100832 /* not fatal but should be reported */
833 msg = "cannot do so_reuseaddr";
834 err |= ERR_ALERT;
835 }
836
837 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900838 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100839
Willy Tarreaue6b98942007-10-29 01:09:36 +0100840#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000841 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
842 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100843 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000844 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100845 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100846#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200847
Willy Tarreau40aa0702013-03-10 23:51:38 +0100848 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200849 switch (listener->addr.ss_family) {
850 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200851 if (1
852#if defined(IP_TRANSPARENT)
853 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
854#endif
855#if defined(IP_FREEBIND)
856 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
857#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200858#if defined(IP_BINDANY)
859 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
860#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200861#if defined(SO_BINDANY)
862 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
863#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200864 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200865 msg = "cannot make listening socket transparent";
866 err |= ERR_ALERT;
867 }
868 break;
869 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200870 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200871#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200872 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
873#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100874#if defined(IP_FREEBIND)
875 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
876#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200877#if defined(IPV6_BINDANY)
878 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
879#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200880#if defined(SO_BINDANY)
881 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
882#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200883 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200884 msg = "cannot make listening socket transparent";
885 err |= ERR_ALERT;
886 }
887 break;
888 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100889 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200890
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100891#ifdef SO_BINDTODEVICE
892 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100893 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100894 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100895 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100896 msg = "cannot bind listener to device";
897 err |= ERR_WARN;
898 }
899 }
900#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400901#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100902 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400903 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200904 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
905 msg = "cannot set MSS";
906 err |= ERR_WARN;
907 }
908 }
909#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100910#if defined(TCP_USER_TIMEOUT)
911 if (listener->tcp_ut) {
912 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
913 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
914 msg = "cannot set TCP User Timeout";
915 err |= ERR_WARN;
916 }
917 }
918#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200919#if defined(TCP_DEFER_ACCEPT)
920 if (listener->options & LI_O_DEF_ACCEPT) {
921 /* defer accept by up to one second */
922 int accept_delay = 1;
923 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
924 msg = "cannot enable DEFER_ACCEPT";
925 err |= ERR_WARN;
926 }
927 }
928#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200929#if defined(TCP_FASTOPEN)
930 if (listener->options & LI_O_TCP_FO) {
931 /* TFO needs a queue length, let's use the configured backlog */
932 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
933 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
934 msg = "cannot enable TCP_FASTOPEN";
935 err |= ERR_WARN;
936 }
937 }
938#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100939#if defined(IPV6_V6ONLY)
940 if (listener->options & LI_O_V6ONLY)
941 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100942 else if (listener->options & LI_O_V4V6)
943 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100944#endif
945
Willy Tarreau40aa0702013-03-10 23:51:38 +0100946 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100947 err |= ERR_RETRYABLE | ERR_ALERT;
948 msg = "cannot bind socket";
949 goto tcp_close_return;
950 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100951
Willy Tarreau40aa0702013-03-10 23:51:38 +0100952 ready = 0;
953 ready_len = sizeof(ready);
954 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
955 ready = 0;
956
957 if (!(ext && ready) && /* only listen if not already done by external process */
958 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100959 err |= ERR_RETRYABLE | ERR_ALERT;
960 msg = "cannot listen to socket";
961 goto tcp_close_return;
962 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100963
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400964#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200965 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900966 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200967#endif
968
Willy Tarreaue6b98942007-10-29 01:09:36 +0100969 /* the socket is ready */
970 listener->fd = fd;
971 listener->state = LI_LISTEN;
972
Willy Tarreaueabf3132008-08-29 23:36:51 +0200973 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200974 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200975 fd_insert(fd);
976
Willy Tarreaue6b98942007-10-29 01:09:36 +0100977 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100978 if (msg && errlen) {
979 char pn[INET6_ADDRSTRLEN];
980
Willy Tarreau631f01c2011-09-05 00:36:48 +0200981 addr_to_str(&listener->addr, pn, sizeof(pn));
982 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100983 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100984 return err;
985
986 tcp_close_return:
987 close(fd);
988 goto tcp_return;
989}
990
991/* This function creates all TCP sockets bound to the protocol entry <proto>.
992 * It is intended to be used as the protocol's bind_all() function.
993 * The sockets will be registered but not added to any fd_set, in order not to
994 * loose them across the fork(). A call to enable_all_listeners() is needed
995 * to complete initialization. The return value is composed from ERR_*.
996 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200997static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100998{
999 struct listener *listener;
1000 int err = ERR_NONE;
1001
1002 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001003 err |= tcp_bind_listener(listener, errmsg, errlen);
1004 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001005 break;
1006 }
1007
1008 return err;
1009}
1010
1011/* Add listener to the list of tcpv4 listeners. The listener's state
1012 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1013 * listeners is updated. This is the function to use to add a new listener.
1014 */
1015void tcpv4_add_listener(struct listener *listener)
1016{
1017 if (listener->state != LI_INIT)
1018 return;
1019 listener->state = LI_ASSIGNED;
1020 listener->proto = &proto_tcpv4;
1021 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1022 proto_tcpv4.nb_listeners++;
1023}
1024
1025/* Add listener to the list of tcpv4 listeners. The listener's state
1026 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1027 * listeners is updated. This is the function to use to add a new listener.
1028 */
1029void tcpv6_add_listener(struct listener *listener)
1030{
1031 if (listener->state != LI_INIT)
1032 return;
1033 listener->state = LI_ASSIGNED;
1034 listener->proto = &proto_tcpv6;
1035 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1036 proto_tcpv6.nb_listeners++;
1037}
1038
Willy Tarreau092d8652014-07-07 20:22:12 +02001039/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1040 * was totally stopped, or > 0 if correctly paused.
1041 */
1042int tcp_pause_listener(struct listener *l)
1043{
1044 if (shutdown(l->fd, SHUT_WR) != 0)
1045 return -1; /* Solaris dies here */
1046
1047 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1048 return -1; /* OpenBSD dies here */
1049
1050 if (shutdown(l->fd, SHUT_RD) != 0)
1051 return -1; /* should always be OK */
1052 return 1;
1053}
1054
Willy Tarreauedcf6682008-11-30 23:15:34 +01001055/* This function performs the TCP request analysis on the current request. It
1056 * returns 1 if the processing can continue on next analysers, or zero if it
1057 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +02001058 * request. It relies on buffers flags, and updates s->req->analysers. The
1059 * function may be called for frontend rules and backend rules. It only relies
1060 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001061 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001062int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +01001063{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001064 struct session *sess = s->sess;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001065 struct act_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +02001066 struct stksess *ts;
1067 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001068 int partial;
Willy Tarreauacc98002015-09-27 23:34:39 +02001069 int act_flags = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001070
Willy Tarreau87b09662015-04-03 00:22:06 +02001071 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 +01001072 now_ms, __FUNCTION__,
1073 s,
1074 req,
1075 req->rex, req->wex,
1076 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001077 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +01001078 req->analysers);
1079
Willy Tarreauedcf6682008-11-30 23:15:34 +01001080 /* We don't know whether we have enough data, so must proceed
1081 * this way :
1082 * - iterate through all rules in their declaration order
1083 * - if one rule returns MISS, it means the inspect delay is
1084 * not over yet, then return immediately, otherwise consider
1085 * it as a non-match.
1086 * - if one rule returns OK, then return OK
1087 * - if one rule returns KO, then return KO
1088 */
1089
Willy Tarreau9b28e032012-10-12 23:49:43 +02001090 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001091 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001092 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001093 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001094 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001095
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001096 /* If "the current_rule_list" match the executed rule list, we are in
1097 * resume condition. If a resume is needed it is always in the action
1098 * and never in the ACL or converters. In this case, we initialise the
1099 * current rule, and go to the action execution point.
1100 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001101 if (s->current_rule) {
1102 rule = s->current_rule;
1103 s->current_rule = NULL;
1104 if (s->current_rule_list == &s->be->tcp_req.inspect_rules)
1105 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001106 }
1107 s->current_rule_list = &s->be->tcp_req.inspect_rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001108
Willy Tarreaufb356202010-08-03 14:02:05 +02001109 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001110 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001111
1112 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001113 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001114 if (ret == ACL_TEST_MISS)
1115 goto missing_data;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001116
1117 ret = acl_pass(ret);
1118 if (rule->cond->pol == ACL_COND_UNLESS)
1119 ret = !ret;
1120 }
1121
1122 if (ret) {
Willy Tarreauacc98002015-09-27 23:34:39 +02001123 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001124resume_execution:
Willy Tarreauedcf6682008-11-30 23:15:34 +01001125 /* we have a matching rule. */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001126 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreau27f78242015-07-04 11:36:30 +02001127 break;
1128 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001129 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001130 channel_abort(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001131 channel_abort(&s->res);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001132 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001133
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001134 s->be->be_counters.denied_req++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001135 sess->fe->fe_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001136 if (sess->listener->counters)
1137 sess->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001138
Willy Tarreaue7dff022015-04-03 01:14:29 +02001139 if (!(s->flags & SF_ERR_MASK))
1140 s->flags |= SF_ERR_PRXCOND;
1141 if (!(s->flags & SF_FINST_MASK))
1142 s->flags |= SF_FINST_R;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001143 return 0;
1144 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001145 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001146 /* Note: only the first valid tracking parameter of each
1147 * applies.
1148 */
1149 struct stktable_key *key;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001150 struct sample smp;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001151
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001152 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001153 continue;
1154
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001155 t = rule->arg.trk_ctr.table.t;
1156 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 +02001157
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001158 if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL))
1159 goto missing_data; /* key might appear later */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001160
1161 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001162 stream_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001163 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001164 if (sess->fe != s->be)
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001165 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreaud1f96522010-08-03 19:34:32 +02001166 }
1167 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001168 else if (rule->action == ACT_TCP_CAPTURE) {
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001169 struct sample *key;
Thierry FOURNIERd0d65ae2015-08-04 08:21:12 +02001170 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001171 char **cap = s->req_cap;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001172 int len;
1173
Thierry FOURNIERd0d65ae2015-08-04 08:21:12 +02001174 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 +02001175 if (!key)
1176 continue;
1177
1178 if (key->flags & SMP_F_MAY_CHANGE)
1179 goto missing_data;
1180
1181 if (cap[h->index] == NULL)
1182 cap[h->index] = pool_alloc2(h->pool);
1183
1184 if (cap[h->index] == NULL) /* no more capture memory */
1185 continue;
1186
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001187 len = key->data.u.str.len;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001188 if (len > h->len)
1189 len = h->len;
1190
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001191 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001192 cap[h->index][len] = 0;
1193 }
Willy Tarreaud1f96522010-08-03 19:34:32 +02001194 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001195 /* Custom keywords. */
Thierry FOURNIER42148732015-09-02 17:17:33 +02001196 if (!rule->action_ptr)
1197 continue;
Willy Tarreauc1b10d32015-09-27 10:06:24 +02001198
Willy Tarreauacc98002015-09-27 23:34:39 +02001199 if (partial & SMP_OPT_FINAL)
1200 act_flags |= ACT_FLAG_FINAL;
1201
1202 switch (rule->action_ptr(rule, s->be, s->sess, s, act_flags)) {
Thierry FOURNIER42148732015-09-02 17:17:33 +02001203 case ACT_RET_ERR:
1204 case ACT_RET_CONT:
1205 continue;
1206 case ACT_RET_STOP:
Thierry FOURNIER561a0f92015-05-29 17:29:14 +02001207 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001208 case ACT_RET_YIELD:
1209 s->current_rule = rule;
1210 goto missing_data;
1211 }
1212 break; /* ACT_RET_STOP */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001213 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001214 }
1215 }
1216
1217 /* if we get there, it means we have no rule which matches, or
1218 * we have an explicit accept, so we apply the default accept.
1219 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001220 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001221 req->analyse_exp = TICK_ETERNITY;
1222 return 1;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001223
1224 missing_data:
1225 channel_dont_connect(req);
1226 /* just set the request timeout once at the beginning of the request */
1227 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
1228 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
1229 return 0;
1230
Willy Tarreauedcf6682008-11-30 23:15:34 +01001231}
1232
Emeric Brun97679e72010-09-23 17:56:44 +02001233/* This function performs the TCP response analysis on the current response. It
1234 * returns 1 if the processing can continue on next analysers, or zero if it
1235 * needs more data, encounters an error, or wants to immediately abort the
1236 * response. It relies on buffers flags, and updates s->rep->analysers. The
1237 * function may be called for backend rules.
1238 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001239int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001240{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001241 struct session *sess = s->sess;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001242 struct act_rule *rule;
Emeric Brun97679e72010-09-23 17:56:44 +02001243 int partial;
Willy Tarreauacc98002015-09-27 23:34:39 +02001244 int act_flags = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001245
Willy Tarreau87b09662015-04-03 00:22:06 +02001246 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 +02001247 now_ms, __FUNCTION__,
1248 s,
1249 rep,
1250 rep->rex, rep->wex,
1251 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001252 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001253 rep->analysers);
1254
1255 /* We don't know whether we have enough data, so must proceed
1256 * this way :
1257 * - iterate through all rules in their declaration order
1258 * - if one rule returns MISS, it means the inspect delay is
1259 * not over yet, then return immediately, otherwise consider
1260 * it as a non-match.
1261 * - if one rule returns OK, then return OK
1262 * - if one rule returns KO, then return KO
1263 */
1264
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001265 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001266 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001267 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001268 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001269
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001270 /* If "the current_rule_list" match the executed rule list, we are in
1271 * resume condition. If a resume is needed it is always in the action
1272 * and never in the ACL or converters. In this case, we initialise the
1273 * current rule, and go to the action execution point.
1274 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001275 if (s->current_rule) {
1276 rule = s->current_rule;
1277 s->current_rule = NULL;
1278 if (s->current_rule_list == &s->be->tcp_rep.inspect_rules)
1279 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001280 }
1281 s->current_rule_list = &s->be->tcp_rep.inspect_rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001282
Emeric Brun97679e72010-09-23 17:56:44 +02001283 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001284 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001285
1286 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001287 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001288 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001289 /* just set the analyser timeout once at the beginning of the response */
1290 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001291 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001292 return 0;
1293 }
1294
1295 ret = acl_pass(ret);
1296 if (rule->cond->pol == ACL_COND_UNLESS)
1297 ret = !ret;
1298 }
1299
1300 if (ret) {
Willy Tarreauacc98002015-09-27 23:34:39 +02001301 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001302resume_execution:
Emeric Brun97679e72010-09-23 17:56:44 +02001303 /* we have a matching rule. */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001304 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreau27f78242015-07-04 11:36:30 +02001305 break;
1306 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001307 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001308 channel_abort(rep);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001309 channel_abort(&s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001310 rep->analysers = 0;
1311
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001312 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001313 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001314 if (sess->listener->counters)
1315 sess->listener->counters->denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001316
Willy Tarreaue7dff022015-04-03 01:14:29 +02001317 if (!(s->flags & SF_ERR_MASK))
1318 s->flags |= SF_ERR_PRXCOND;
1319 if (!(s->flags & SF_FINST_MASK))
1320 s->flags |= SF_FINST_D;
Emeric Brun97679e72010-09-23 17:56:44 +02001321 return 0;
1322 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001323 else if (rule->action == ACT_TCP_CLOSE) {
Willy Tarreau73796532014-11-28 14:10:28 +01001324 chn_prod(rep)->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1325 si_shutr(chn_prod(rep));
1326 si_shutw(chn_prod(rep));
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001327 break;
1328 }
Emeric Brun97679e72010-09-23 17:56:44 +02001329 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001330 /* Custom keywords. */
Thierry FOURNIER42148732015-09-02 17:17:33 +02001331 if (!rule->action_ptr)
1332 continue;
Willy Tarreauacc98002015-09-27 23:34:39 +02001333
1334 if (partial & SMP_OPT_FINAL)
1335 act_flags |= ACT_FLAG_FINAL;
1336
1337 switch (rule->action_ptr(rule, s->be, s->sess, s, act_flags)) {
Thierry FOURNIER42148732015-09-02 17:17:33 +02001338 case ACT_RET_ERR:
1339 case ACT_RET_CONT:
1340 continue;
1341 case ACT_RET_STOP:
Thierry FOURNIER561a0f92015-05-29 17:29:14 +02001342 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001343 case ACT_RET_YIELD:
1344 channel_dont_close(rep);
1345 s->current_rule = rule;
1346 return 0;
1347 }
1348 break; /* ACT_RET_STOP */
Emeric Brun97679e72010-09-23 17:56:44 +02001349 }
1350 }
1351 }
1352
1353 /* if we get there, it means we have no rule which matches, or
1354 * we have an explicit accept, so we apply the default accept.
1355 */
1356 rep->analysers &= ~an_bit;
1357 rep->analyse_exp = TICK_ETERNITY;
1358 return 1;
1359}
1360
1361
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001362/* This function performs the TCP layer4 analysis on the current request. It
1363 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1364 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001365 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001366 */
Willy Tarreau7d9736f2016-10-21 16:34:21 +02001367int tcp_exec_l4_rules(struct session *sess)
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001368{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001369 struct act_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001370 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001371 struct stktable *t = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001372 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001373 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001374 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001375
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001376 if (!conn)
1377 return result;
1378
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001379 list_for_each_entry(rule, &sess->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001380 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001381
1382 if (rule->cond) {
Willy Tarreaue73ef852015-04-04 16:41:45 +02001383 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001384 ret = acl_pass(ret);
1385 if (rule->cond->pol == ACL_COND_UNLESS)
1386 ret = !ret;
1387 }
1388
1389 if (ret) {
1390 /* we have a matching rule. */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001391 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreau27f78242015-07-04 11:36:30 +02001392 break;
1393 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001394 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001395 sess->fe->fe_counters.denied_conn++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001396 if (sess->listener->counters)
1397 sess->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001398
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001399 result = 0;
1400 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001401 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001402 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001403 /* Note: only the first valid tracking parameter of each
1404 * applies.
1405 */
1406 struct stktable_key *key;
1407
Willy Tarreau70f454e2015-04-04 16:38:07 +02001408 if (stkctr_entry(&sess->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001409 continue;
1410
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001411 t = rule->arg.trk_ctr.table.t;
1412 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 +01001413
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001414 if (key && (ts = stktable_get_entry(t, key)))
Willy Tarreau70f454e2015-04-04 16:38:07 +02001415 stream_track_stkctr(&sess->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001416 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001417 else if (rule->action == ACT_TCP_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001418 conn->flags |= CO_FL_ACCEPT_PROXY;
1419 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001420 }
Bertrand Jacquin90759682016-06-06 15:35:39 +01001421 else if (rule->action == ACT_TCP_EXPECT_CIP) {
1422 conn->flags |= CO_FL_ACCEPT_CIP;
1423 conn_sock_want_recv(conn);
1424 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001425 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001426 /* Custom keywords. */
Willy Tarreau45059e92015-09-28 17:50:10 +02001427 if (!rule->action_ptr)
Thierry FOURNIER42148732015-09-02 17:17:33 +02001428 break;
Willy Tarreauacc98002015-09-27 23:34:39 +02001429 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_FLAG_FINAL | ACT_FLAG_FIRST)) {
Thierry FOURNIER42148732015-09-02 17:17:33 +02001430 case ACT_RET_YIELD:
1431 /* yield is not allowed at this point. If this return code is
1432 * used it is a bug, so I prefer to abort the process.
1433 */
1434 send_log(sess->fe, LOG_WARNING,
1435 "Internal error: yield not allowed with tcp-request connection actions.");
1436 case ACT_RET_STOP:
1437 break;
1438 case ACT_RET_CONT:
1439 continue;
1440 case ACT_RET_ERR:
1441 result = 0;
1442 break;
Thierry FOURNIERc89f4f52015-08-11 09:48:02 +02001443 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02001444 break; /* ACT_RET_STOP */
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001445 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001446 }
1447 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001448 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001449}
1450
Willy Tarreau620408f2016-10-21 16:37:51 +02001451/* This function performs the TCP layer5 analysis on the current request. It
1452 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1453 * matches or if no more rule matches. It can only use rules which don't need
1454 * any data. This only works on session-based client-facing stream interfaces.
1455 * An example of valid use case is to track a stick-counter on the source
1456 * address extracted from the proxy protocol.
1457 */
1458int tcp_exec_l5_rules(struct session *sess)
1459{
1460 struct act_rule *rule;
1461 struct stksess *ts;
1462 struct stktable *t = NULL;
1463 int result = 1;
1464 enum acl_test_res ret;
1465
1466 list_for_each_entry(rule, &sess->fe->tcp_req.l5_rules, list) {
1467 ret = ACL_TEST_PASS;
1468
1469 if (rule->cond) {
1470 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
1471 ret = acl_pass(ret);
1472 if (rule->cond->pol == ACL_COND_UNLESS)
1473 ret = !ret;
1474 }
1475
1476 if (ret) {
1477 /* we have a matching rule. */
1478 if (rule->action == ACT_ACTION_ALLOW) {
1479 break;
1480 }
1481 else if (rule->action == ACT_ACTION_DENY) {
1482 sess->fe->fe_counters.denied_sess++;
1483 if (sess->listener->counters)
1484 sess->listener->counters->denied_sess++;
1485
1486 result = 0;
1487 break;
1488 }
1489 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
1490 /* Note: only the first valid tracking parameter of each
1491 * applies.
1492 */
1493 struct stktable_key *key;
1494
1495 if (stkctr_entry(&sess->stkctr[tcp_trk_idx(rule->action)]))
1496 continue;
1497
1498 t = rule->arg.trk_ctr.table.t;
1499 key = stktable_fetch_key(t, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
1500
1501 if (key && (ts = stktable_get_entry(t, key)))
1502 stream_track_stkctr(&sess->stkctr[tcp_trk_idx(rule->action)], t, ts);
1503 }
1504 else {
1505 /* Custom keywords. */
1506 if (!rule->action_ptr)
1507 break;
1508 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_FLAG_FINAL | ACT_FLAG_FIRST)) {
1509 case ACT_RET_YIELD:
1510 /* yield is not allowed at this point. If this return code is
1511 * used it is a bug, so I prefer to abort the process.
1512 */
1513 send_log(sess->fe, LOG_WARNING,
1514 "Internal error: yield not allowed with tcp-request session actions.");
1515 case ACT_RET_STOP:
1516 break;
1517 case ACT_RET_CONT:
1518 continue;
1519 case ACT_RET_ERR:
1520 result = 0;
1521 break;
1522 }
1523 break; /* ACT_RET_STOP */
1524 }
1525 }
1526 }
1527 return result;
1528}
1529
William Lallemand2e785f22016-05-25 01:48:42 +02001530/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001531 * Execute the "set-src" action. May be called from {tcp,http}request.
1532 * It only changes the address and tries to preserve the original port. If the
1533 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001534 */
1535enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1536 struct session *sess, struct stream *s, int flags)
1537{
1538 struct connection *cli_conn;
1539
1540 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1541 struct sample *smp;
1542
1543 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1544 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001545 int port = get_net_port(&cli_conn->addr.from);
1546
William Lallemand2e785f22016-05-25 01:48:42 +02001547 if (smp->data.type == SMP_T_IPV4) {
1548 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
1549 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001550 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001551 } else if (smp->data.type == SMP_T_IPV6) {
1552 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
1553 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Willy Tarreau00005ce2016-10-21 15:07:45 +02001554 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001555 }
1556 }
William Lallemand01252ed2016-05-25 02:33:16 +02001557 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001558 }
1559 return ACT_RET_CONT;
1560}
1561
William Lallemand44be6402016-05-25 01:51:35 +02001562/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001563 * Execute the "set-dst" action. May be called from {tcp,http}request.
1564 * It only changes the address and tries to preserve the original port. If the
1565 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001566 */
1567enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1568 struct session *sess, struct stream *s, int flags)
1569{
1570 struct connection *cli_conn;
1571
1572 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1573 struct sample *smp;
1574
1575 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1576 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001577 int port = get_net_port(&cli_conn->addr.to);
1578
William Lallemand13e9b0c2016-05-25 02:34:07 +02001579 if (smp->data.type == SMP_T_IPV4) {
1580 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_family = AF_INET;
1581 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1582 } else if (smp->data.type == SMP_T_IPV6) {
1583 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_family = AF_INET6;
1584 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Willy Tarreau00005ce2016-10-21 15:07:45 +02001585 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001586 }
1587 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1588 }
1589 }
1590 return ACT_RET_CONT;
1591}
1592
1593/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001594 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1595 * We must test the sin_family before setting the port. If the address family
1596 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1597 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001598 */
1599enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1600 struct session *sess, struct stream *s, int flags)
1601{
1602 struct connection *cli_conn;
1603
1604 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1605 struct sample *smp;
1606
1607 conn_get_from_addr(cli_conn);
1608
1609 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1610 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001611 if (cli_conn->addr.from.ss_family == AF_INET6) {
William Lallemand44be6402016-05-25 01:51:35 +02001612 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001613 } else {
1614 if (cli_conn->addr.from.ss_family != AF_INET) {
1615 cli_conn->addr.from.ss_family = AF_INET;
1616 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1617 }
1618 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001619 }
1620 }
1621 }
1622 return ACT_RET_CONT;
1623}
1624
William Lallemand13e9b0c2016-05-25 02:34:07 +02001625/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001626 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1627 * We must test the sin_family before setting the port. If the address family
1628 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1629 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001630 */
1631enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1632 struct session *sess, struct stream *s, int flags)
1633{
1634 struct connection *cli_conn;
1635
1636 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1637 struct sample *smp;
1638
1639 conn_get_to_addr(cli_conn);
1640
1641 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1642 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001643 if (cli_conn->addr.from.ss_family == AF_INET6) {
1644 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
1645 } else {
1646 if (cli_conn->addr.from.ss_family != AF_INET) {
1647 cli_conn->addr.from.ss_family = AF_INET;
1648 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1649 }
1650 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001651 }
1652 }
1653 }
1654 return ACT_RET_CONT;
1655}
1656
Willy Tarreau2d392c22015-08-24 01:43:45 +02001657/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1658static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1659{
1660 struct connection *conn = objt_conn(sess->origin);
1661
1662 if (!conn)
1663 goto out;
1664
1665 if (!conn_ctrl_ready(conn))
1666 goto out;
1667
Willy Tarreau2d392c22015-08-24 01:43:45 +02001668#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001669 /* drain is needed only to send the quick ACK */
1670 conn_sock_drain(conn);
1671
Willy Tarreau2d392c22015-08-24 01:43:45 +02001672 /* re-enable quickack if it was disabled to ack all data and avoid
1673 * retransmits from the client that might trigger a real reset.
1674 */
1675 setsockopt(conn->t.sock.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
1676#endif
1677 /* lingering must absolutely be disabled so that we don't send a
1678 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1679 * is present, returning with ERR will cause lingering to be disabled.
1680 */
1681 if (strm)
1682 strm->si[0].flags |= SI_FL_NOLINGER;
1683
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001684 /* We're on the client-facing side, we must force to disable lingering to
1685 * ensure we will use an RST exclusively and kill any pending data.
1686 */
1687 fdtab[conn->t.sock.fd].linger_risk = 1;
1688
Willy Tarreau2d392c22015-08-24 01:43:45 +02001689#ifdef TCP_REPAIR
1690 if (setsockopt(conn->t.sock.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
1691 /* socket will be quiet now */
1692 goto out;
1693 }
1694#endif
1695 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1696 * Let's fall back on the TTL trick, though it only works for routed
1697 * network and has no effect on local net.
1698 */
1699#ifdef IP_TTL
1700 setsockopt(conn->t.sock.fd, SOL_IP, IP_TTL, &one, sizeof(one));
1701#endif
1702 out:
1703 /* kill the stream if any */
1704 if (strm) {
1705 channel_abort(&strm->req);
1706 channel_abort(&strm->res);
1707 strm->req.analysers = 0;
1708 strm->res.analysers = 0;
1709 strm->be->be_counters.denied_req++;
1710 if (!(strm->flags & SF_ERR_MASK))
1711 strm->flags |= SF_ERR_PRXCOND;
1712 if (!(strm->flags & SF_FINST_MASK))
1713 strm->flags |= SF_FINST_R;
1714 }
1715
1716 sess->fe->fe_counters.denied_req++;
1717 if (sess->listener->counters)
1718 sess->listener->counters->denied_req++;
1719
1720 return ACT_RET_STOP;
1721}
1722
Emeric Brun97679e72010-09-23 17:56:44 +02001723/* Parse a tcp-response rule. Return a negative value in case of failure */
1724static int tcp_parse_response_rule(char **args, int arg, int section_type,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001725 struct proxy *curpx, struct proxy *defpx,
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001726 struct act_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001727 unsigned int where,
1728 const char *file, int line)
Emeric Brun97679e72010-09-23 17:56:44 +02001729{
1730 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001731 memprintf(err, "%s %s is only allowed in 'backend' sections",
1732 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001733 return -1;
1734 }
1735
1736 if (strcmp(args[arg], "accept") == 0) {
1737 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001738 rule->action = ACT_ACTION_ALLOW;
Emeric Brun97679e72010-09-23 17:56:44 +02001739 }
1740 else if (strcmp(args[arg], "reject") == 0) {
1741 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001742 rule->action = ACT_ACTION_DENY;
Emeric Brun97679e72010-09-23 17:56:44 +02001743 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001744 else if (strcmp(args[arg], "close") == 0) {
1745 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001746 rule->action = ACT_TCP_CLOSE;
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001747 }
Emeric Brun97679e72010-09-23 17:56:44 +02001748 else {
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001749 struct action_kw *kw;
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001750 kw = tcp_res_cont_action(args[arg]);
1751 if (kw) {
1752 arg++;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001753 rule->from = ACT_F_TCP_RES_CNT;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02001754 rule->kw = kw;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02001755 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001756 return -1;
1757 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001758 action_build_list(&tcp_res_cont_keywords, &trash);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001759 memprintf(err,
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001760 "'%s %s' expects 'accept', 'close', 'reject', %s in %s '%s' (got '%s')",
1761 args[0], args[1], trash.str, proxy_type_str(curpx), curpx->id, args[arg]);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001762 return -1;
1763 }
Emeric Brun97679e72010-09-23 17:56:44 +02001764 }
1765
1766 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001767 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001768 memprintf(err,
1769 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1770 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001771 return -1;
1772 }
1773 }
1774 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001775 memprintf(err,
1776 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001777 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1778 return -1;
1779 }
1780 return 0;
1781}
1782
1783
1784
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001785/* Parse a tcp-request rule. Return a negative value in case of failure */
1786static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001787 struct proxy *curpx, struct proxy *defpx,
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001788 struct act_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001789 unsigned int where, const char *file, int line)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001790{
1791 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001792 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1793 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001794 return -1;
1795 }
1796
1797 if (!strcmp(args[arg], "accept")) {
1798 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001799 rule->action = ACT_ACTION_ALLOW;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001800 }
1801 else if (!strcmp(args[arg], "reject")) {
1802 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001803 rule->action = ACT_ACTION_DENY;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001804 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001805 else if (strcmp(args[arg], "capture") == 0) {
1806 struct sample_expr *expr;
1807 struct cap_hdr *hdr;
1808 int kw = arg;
1809 int len = 0;
1810
1811 if (!(curpx->cap & PR_CAP_FE)) {
1812 memprintf(err,
1813 "'%s %s %s' : proxy '%s' has no frontend capability",
1814 args[0], args[1], args[kw], curpx->id);
1815 return -1;
1816 }
1817
1818 if (!(where & SMP_VAL_FE_REQ_CNT)) {
1819 memprintf(err,
1820 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1821 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1822 return -1;
1823 }
1824
1825 arg++;
1826
1827 curpx->conf.args.ctx = ARGC_CAP;
1828 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
1829 if (!expr) {
1830 memprintf(err,
1831 "'%s %s %s' : %s",
1832 args[0], args[1], args[kw], *err);
1833 return -1;
1834 }
1835
1836 if (!(expr->fetch->val & where)) {
1837 memprintf(err,
1838 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
1839 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
1840 free(expr);
1841 return -1;
1842 }
1843
1844 if (strcmp(args[arg], "len") == 0) {
1845 arg++;
1846 if (!args[arg]) {
1847 memprintf(err,
1848 "'%s %s %s' : missing length value",
1849 args[0], args[1], args[kw]);
1850 free(expr);
1851 return -1;
1852 }
1853 /* we copy the table name for now, it will be resolved later */
1854 len = atoi(args[arg]);
1855 if (len <= 0) {
1856 memprintf(err,
1857 "'%s %s %s' : length must be > 0",
1858 args[0], args[1], args[kw]);
1859 free(expr);
1860 return -1;
1861 }
1862 arg++;
1863 }
1864
1865 if (!len) {
1866 memprintf(err,
1867 "'%s %s %s' : a positive 'len' argument is mandatory",
1868 args[0], args[1], args[kw]);
1869 free(expr);
1870 return -1;
1871 }
1872
Vincent Bernat02779b62016-04-03 13:48:43 +02001873 hdr = calloc(1, sizeof(*hdr));
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001874 hdr->next = curpx->req_cap;
1875 hdr->name = NULL; /* not a header capture */
1876 hdr->namelen = 0;
1877 hdr->len = len;
1878 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
1879 hdr->index = curpx->nb_req_cap++;
1880
1881 curpx->req_cap = hdr;
1882 curpx->to_log |= LW_REQHDR;
1883
1884 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
1885 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1886
Thierry FOURNIERd0d65ae2015-08-04 08:21:12 +02001887 rule->arg.cap.expr = expr;
1888 rule->arg.cap.hdr = hdr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001889 rule->action = ACT_TCP_CAPTURE;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001890 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001891 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1892 args[arg][9] == '\0' && args[arg][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02001893 args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001894 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001895 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001896
1897 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001898
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001899 curpx->conf.args.ctx = ARGC_TRK;
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001900 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001901 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001902 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001903 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001904 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001905 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001906 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001907
Willy Tarreau80aca902013-01-07 15:42:20 +01001908 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001909 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001910 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001911 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001912 free(expr);
1913 return -1;
1914 }
1915
1916 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001917 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001918
1919 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001920 arg++;
1921 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001922 memprintf(err,
1923 "'%s %s %s' : missing table name",
1924 args[0], args[1], args[kw]);
1925 free(expr);
1926 return -1;
1927 }
1928 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001929 rule->arg.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001930 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001931 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001932 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001933 rule->action = ACT_ACTION_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001934 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001935 else if (strcmp(args[arg], "expect-proxy") == 0) {
1936 if (strcmp(args[arg+1], "layer4") != 0) {
1937 memprintf(err,
1938 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1939 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1940 return -1;
1941 }
1942
1943 if (!(where & SMP_VAL_FE_CON_ACC)) {
1944 memprintf(err,
1945 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1946 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1947 return -1;
1948 }
1949
1950 arg += 2;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001951 rule->action = ACT_TCP_EXPECT_PX;
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001952 }
Bertrand Jacquin90759682016-06-06 15:35:39 +01001953 else if (strcmp(args[arg], "expect-netscaler-cip") == 0) {
1954 if (strcmp(args[arg+1], "layer4") != 0) {
1955 memprintf(err,
1956 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1957 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1958 return -1;
1959 }
1960
1961 if (!(where & SMP_VAL_FE_CON_ACC)) {
1962 memprintf(err,
1963 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1964 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1965 return -1;
1966 }
1967
1968 arg += 2;
1969 rule->action = ACT_TCP_EXPECT_CIP;
1970 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001971 else {
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001972 struct action_kw *kw;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001973 if (where & SMP_VAL_FE_CON_ACC) {
Willy Tarreau7d9736f2016-10-21 16:34:21 +02001974 /* L4 */
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001975 kw = tcp_req_conn_action(args[arg]);
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02001976 rule->kw = kw;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001977 rule->from = ACT_F_TCP_REQ_CON;
Willy Tarreau620408f2016-10-21 16:37:51 +02001978 } else if (where & SMP_VAL_FE_SES_ACC) {
1979 /* L5 */
1980 kw = tcp_req_sess_action(args[arg]);
1981 rule->kw = kw;
1982 rule->from = ACT_F_TCP_REQ_SES;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001983 } else {
Willy Tarreau7d9736f2016-10-21 16:34:21 +02001984 /* L6 */
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001985 kw = tcp_req_cont_action(args[arg]);
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02001986 rule->kw = kw;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001987 rule->from = ACT_F_TCP_REQ_CNT;
1988 }
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001989 if (kw) {
1990 arg++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02001991 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001992 return -1;
1993 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001994 if (where & SMP_VAL_FE_CON_ACC)
1995 action_build_list(&tcp_req_conn_keywords, &trash);
Willy Tarreau620408f2016-10-21 16:37:51 +02001996 else if (where & SMP_VAL_FE_SES_ACC)
1997 action_build_list(&tcp_req_sess_keywords, &trash);
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001998 else
1999 action_build_list(&tcp_req_cont_keywords, &trash);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01002000 memprintf(err,
Thierry FOURNIERab95e652015-10-02 08:24:51 +02002001 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d', %s "
2002 "in %s '%s' (got '%s').\n",
2003 args[0], args[1], MAX_SESS_STKCTR-1, trash.str, proxy_type_str(curpx),
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01002004 curpx->id, args[arg]);
2005 return -1;
2006 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002007 }
2008
2009 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01002010 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002011 memprintf(err,
2012 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
2013 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002014 return -1;
2015 }
2016 }
2017 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002018 memprintf(err,
2019 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002020 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
2021 return -1;
2022 }
2023 return 0;
2024}
2025
Emeric Brun97679e72010-09-23 17:56:44 +02002026/* This function should be called to parse a line starting with the "tcp-response"
2027 * keyword.
2028 */
2029static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02002030 struct proxy *defpx, const char *file, int line,
2031 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02002032{
2033 const char *ptr = NULL;
2034 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02002035 int warn = 0;
2036 int arg;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002037 struct act_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01002038 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002039 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02002040 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02002041
2042 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002043 memprintf(err, "missing argument for '%s' in %s '%s'",
2044 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02002045 return -1;
2046 }
2047
2048 if (strcmp(args[1], "inspect-delay") == 0) {
2049 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002050 memprintf(err, "%s %s is only allowed in 'backend' sections",
2051 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02002052 return -1;
2053 }
2054
2055 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002056 memprintf(err,
2057 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
2058 args[0], args[1], proxy_type_str(curpx), curpx->id);
2059 if (ptr)
2060 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02002061 return -1;
2062 }
2063
2064 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002065 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
2066 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02002067 return 1;
2068 }
2069 curpx->tcp_rep.inspect_delay = val;
2070 return 0;
2071 }
2072
Simon Hormande072bd2011-06-24 15:11:37 +09002073 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02002074 LIST_INIT(&rule->list);
2075 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01002076 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02002077
2078 if (strcmp(args[1], "content") == 0) {
2079 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01002080
2081 if (curpx->cap & PR_CAP_FE)
2082 where |= SMP_VAL_FE_RES_CNT;
2083 if (curpx->cap & PR_CAP_BE)
2084 where |= SMP_VAL_BE_RES_CNT;
2085
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01002086 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02002087 goto error;
2088
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002089 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
2090 if (acl) {
2091 if (acl->name && *acl->name)
2092 memprintf(err,
2093 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
2094 acl->name, args[0], args[1], sample_ckp_names(where));
2095 else
2096 memprintf(err,
2097 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
2098 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02002099 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002100 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02002101
Emeric Brun97679e72010-09-23 17:56:44 +02002102 warn++;
2103 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002104 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
2105 if (acl->name && *acl->name)
2106 memprintf(err,
2107 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002108 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002109 else
2110 memprintf(err,
2111 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002112 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002113 warn++;
2114 }
Emeric Brun97679e72010-09-23 17:56:44 +02002115
2116 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
2117 }
2118 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002119 memprintf(err,
2120 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
2121 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02002122 goto error;
2123 }
2124
2125 return warn;
2126 error:
2127 free(rule);
2128 return -1;
2129}
2130
2131
Willy Tarreaub6866442008-07-14 23:54:42 +02002132/* This function should be called to parse a line starting with the "tcp-request"
2133 * keyword.
2134 */
2135static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02002136 struct proxy *defpx, const char *file, int line,
2137 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02002138{
2139 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02002140 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02002141 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002142 int arg;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002143 struct act_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01002144 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002145 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02002146 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02002147
2148 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002149 if (curpx == defpx)
2150 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
2151 else
2152 memprintf(err, "missing argument for '%s' in %s '%s'",
2153 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02002154 return -1;
2155 }
2156
2157 if (!strcmp(args[1], "inspect-delay")) {
2158 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002159 memprintf(err, "%s %s is not allowed in 'defaults' sections",
2160 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02002161 return -1;
2162 }
2163
Willy Tarreaub6866442008-07-14 23:54:42 +02002164 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002165 memprintf(err,
2166 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
2167 args[0], args[1], proxy_type_str(curpx), curpx->id);
2168 if (ptr)
2169 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02002170 return -1;
2171 }
2172
2173 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002174 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
2175 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02002176 return 1;
2177 }
2178 curpx->tcp_req.inspect_delay = val;
2179 return 0;
2180 }
2181
Simon Hormande072bd2011-06-24 15:11:37 +09002182 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02002183 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002184 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01002185 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002186
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002187 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02002188 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01002189
2190 if (curpx->cap & PR_CAP_FE)
2191 where |= SMP_VAL_FE_REQ_CNT;
2192 if (curpx->cap & PR_CAP_BE)
2193 where |= SMP_VAL_BE_REQ_CNT;
2194
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01002195 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002196 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02002197
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002198 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
2199 if (acl) {
2200 if (acl->name && *acl->name)
2201 memprintf(err,
2202 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
2203 acl->name, args[0], args[1], sample_ckp_names(where));
2204 else
2205 memprintf(err,
2206 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
2207 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02002208 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002209 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002210
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002211 warn++;
2212 }
2213 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
2214 if (acl->name && *acl->name)
2215 memprintf(err,
2216 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002217 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002218 else
2219 memprintf(err,
2220 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002221 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002222 warn++;
2223 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01002224
Willy Tarreau3986b9c2014-09-16 15:39:51 +02002225 /* the following function directly emits the warning */
2226 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02002227 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002228 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002229 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002230 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002231
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002232 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002233 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
2234 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09002235 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002236 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002237
Willy Tarreau80aca902013-01-07 15:42:20 +01002238 where |= SMP_VAL_FE_CON_ACC;
2239
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01002240 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002241 goto error;
2242
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002243 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
2244 if (acl) {
2245 if (acl->name && *acl->name)
2246 memprintf(err,
2247 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
2248 acl->name, args[0], args[1], sample_ckp_names(where));
2249 else
2250 memprintf(err,
2251 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
2252 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02002253 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002254 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002255
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002256 warn++;
2257 }
2258 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
2259 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002260 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002261 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002262 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002263 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002264 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002265 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002266 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002267 warn++;
2268 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01002269
Willy Tarreau3986b9c2014-09-16 15:39:51 +02002270 /* the following function directly emits the warning */
2271 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02002272 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002273 }
Willy Tarreau620408f2016-10-21 16:37:51 +02002274 else if (strcmp(args[1], "session") == 0) {
2275 arg++;
2276
2277 if (!(curpx->cap & PR_CAP_FE)) {
2278 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
2279 args[0], args[1], proxy_type_str(curpx), curpx->id);
2280 goto error;
2281 }
2282
2283 where |= SMP_VAL_FE_SES_ACC;
2284
2285 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
2286 goto error;
2287
2288 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
2289 if (acl) {
2290 if (acl->name && *acl->name)
2291 memprintf(err,
2292 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
2293 acl->name, args[0], args[1], sample_ckp_names(where));
2294 else
2295 memprintf(err,
2296 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
2297 args[0], args[1],
2298 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
2299 sample_ckp_names(where));
2300 warn++;
2301 }
2302 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
2303 if (acl->name && *acl->name)
2304 memprintf(err,
2305 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
2306 acl->name, kw, sample_ckp_names(where));
2307 else
2308 memprintf(err,
2309 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
2310 kw, sample_ckp_names(where));
2311 warn++;
2312 }
2313
2314 /* the following function directly emits the warning */
2315 warnif_misplaced_tcp_sess(curpx, file, line, args[0]);
2316 LIST_ADDQ(&curpx->tcp_req.l5_rules, &rule->list);
2317 }
Willy Tarreau1a687942010-05-23 22:40:30 +02002318 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002319 if (curpx == defpx)
2320 memprintf(err,
2321 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
2322 args[0], args[1]);
2323 else
2324 memprintf(err,
2325 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02002326 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002327 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02002328 }
2329
Willy Tarreau1a687942010-05-23 22:40:30 +02002330 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002331 error:
2332 free(rule);
2333 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02002334}
2335
William Lallemand13e9b0c2016-05-25 02:34:07 +02002336/* parse "set-{src,dst}[-port]" action */
2337enum 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 +02002338{
2339 int cur_arg;
2340 struct sample_expr *expr;
2341 unsigned int where;
2342
2343 cur_arg = *orig_arg;
2344 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
2345 if (!expr)
2346 return ACT_RET_PRS_ERR;
2347
2348 where = 0;
2349 if (proxy->cap & PR_CAP_FE)
2350 where |= SMP_VAL_FE_HRQ_HDR;
2351 if (proxy->cap & PR_CAP_BE)
2352 where |= SMP_VAL_BE_HRQ_HDR;
2353
2354 if (!(expr->fetch->val & where)) {
2355 memprintf(err,
2356 "fetch method '%s' extracts information from '%s', none of which is available here",
2357 args[cur_arg-1], sample_src_names(expr->fetch->use));
2358 free(expr);
2359 return ACT_RET_PRS_ERR;
2360 }
2361 rule->arg.expr = expr;
2362 rule->action = ACT_CUSTOM;
2363
2364 if (!strcmp(args[*orig_arg-1], "set-src")) {
2365 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02002366 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
2367 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02002368 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
2369 rule->action_ptr = tcp_action_req_set_dst;
2370 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
2371 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02002372 } else {
2373 return ACT_RET_PRS_ERR;
2374 }
2375
2376 (*orig_arg)++;
2377
2378 return ACT_RET_PRS_OK;
2379}
2380
2381
Willy Tarreau2d392c22015-08-24 01:43:45 +02002382/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
2383 * success, ACT_RET_PRS_ERR on error.
2384 */
2385static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
2386 struct act_rule *rule, char **err)
2387{
2388 rule->action = ACT_CUSTOM;
2389 rule->action_ptr = tcp_exec_action_silent_drop;
2390 return ACT_RET_PRS_OK;
2391}
2392
Willy Tarreau645513a2010-05-24 20:55:15 +02002393
2394/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002395/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02002396/************************************************************************/
2397
Willy Tarreau4a129812012-04-25 17:31:42 +02002398/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02002399int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002400{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002401 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002402
2403 if (!cli_conn)
2404 return 0;
2405
2406 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01002407 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002408 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002409 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002410 break;
2411 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002412 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002413 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002414 break;
2415 default:
Emeric Brunf769f512010-10-22 17:14:01 +02002416 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002417 }
Emeric Brunf769f512010-10-22 17:14:01 +02002418
Willy Tarreau37406352012-04-23 16:16:37 +02002419 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002420 return 1;
2421}
2422
Willy Tarreaua5e37562011-12-16 17:06:15 +01002423/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002424static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002425smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002426{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002427 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002428
2429 if (!cli_conn)
2430 return 0;
2431
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002432 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002433 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02002434 return 0;
2435
Willy Tarreau37406352012-04-23 16:16:37 +02002436 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002437 return 1;
2438}
2439
Willy Tarreau4a129812012-04-25 17:31:42 +02002440/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02002441static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002442smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002443{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002444 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02002445
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002446 if (!cli_conn)
2447 return 0;
2448
2449 conn_get_to_addr(cli_conn);
2450
2451 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01002452 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002453 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002454 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002455 break;
2456 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002457 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002458 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002459 break;
2460 default:
Emeric Brunf769f512010-10-22 17:14:01 +02002461 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002462 }
Emeric Brunf769f512010-10-22 17:14:01 +02002463
Willy Tarreau37406352012-04-23 16:16:37 +02002464 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002465 return 1;
2466}
2467
Willy Tarreau16e01562016-08-09 16:46:18 +02002468/* check if the destination address of the front connection is local to the
2469 * system or if it was intercepted.
2470 */
2471int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
2472{
2473 struct connection *conn = objt_conn(smp->sess->origin);
2474 struct listener *li = smp->sess->listener;
2475
2476 if (!conn)
2477 return 0;
2478
2479 conn_get_to_addr(conn);
2480 if (!(conn->flags & CO_FL_ADDR_TO_SET))
2481 return 0;
2482
2483 smp->data.type = SMP_T_BOOL;
2484 smp->flags = 0;
2485 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.to);
2486 return smp->data.u.sint >= 0;
2487}
2488
2489/* check if the source address of the front connection is local to the system
2490 * or not.
2491 */
2492int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
2493{
2494 struct connection *conn = objt_conn(smp->sess->origin);
2495 struct listener *li = smp->sess->listener;
2496
2497 if (!conn)
2498 return 0;
2499
2500 conn_get_from_addr(conn);
2501 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
2502 return 0;
2503
2504 smp->data.type = SMP_T_BOOL;
2505 smp->flags = 0;
2506 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.from);
2507 return smp->data.u.sint >= 0;
2508}
2509
Willy Tarreaua5e37562011-12-16 17:06:15 +01002510/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002511static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002512smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002513{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002514 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002515
2516 if (!cli_conn)
2517 return 0;
2518
2519 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02002520
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002521 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002522 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02002523 return 0;
2524
Willy Tarreau37406352012-04-23 16:16:37 +02002525 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002526 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01002527}
2528
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02002529#ifdef TCP_INFO
2530
2531/* Returns some tcp_info data is its avalaible. "dir" must be set to 0 if
2532 * the client connection is require, otherwise it is set to 1. "val" represents
2533 * the required value. Use 0 for rtt and 1 for rttavg. "unit" is the expected unit
2534 * by default, the rtt is in us. Id "unit" is set to 0, the unit is us, if it is
2535 * set to 1, the untis are milliseconds.
2536 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
2537 */
2538static inline int get_tcp_info(const struct arg *args, struct sample *smp,
2539 int dir, int val)
2540{
2541 struct connection *conn;
2542 struct tcp_info info;
2543 socklen_t optlen;
2544
2545 /* strm can be null. */
2546 if (!smp->strm)
2547 return 0;
2548
2549 /* get the object associated with the stream interface.The
2550 * object can be other thing than a connection. For example,
2551 * it be a appctx. */
2552 conn = objt_conn(smp->strm->si[dir].end);
2553 if (!conn)
2554 return 0;
2555
2556 /* The fd may not be avalaible for the tcp_info struct, and the
2557 syscal can fail. */
2558 optlen = sizeof(info);
2559 if (getsockopt(conn->t.sock.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
2560 return 0;
2561
2562 /* extract the value. */
2563 smp->data.type = SMP_T_SINT;
2564 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07002565 case 0: smp->data.u.sint = info.tcpi_rtt; break;
2566 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
2567#if defined(__linux__)
2568 /* these ones are common to all Linux versions */
2569 case 2: smp->data.u.sint = info.tcpi_unacked; break;
2570 case 3: smp->data.u.sint = info.tcpi_sacked; break;
2571 case 4: smp->data.u.sint = info.tcpi_lost; break;
2572 case 5: smp->data.u.sint = info.tcpi_retrans; break;
2573 case 6: smp->data.u.sint = info.tcpi_fackets; break;
2574 case 7: smp->data.u.sint = info.tcpi_reordering; break;
2575#elif defined(__FreeBSD__) || defined(__NetBSD__)
2576 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
2577 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
2578 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
2579 case 4: smp->data.u.sint = info.__tcpi_lost; break;
2580 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
2581 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
2582 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
2583#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02002584 default: return 0;
2585 }
2586
2587 /* Convert the value as expected. */
2588 if (args) {
2589 if (args[0].type == ARGT_STR) {
2590 if (strcmp(args[0].data.str.str, "us") == 0) {
2591 /* Do nothing. */
2592 } else if (strcmp(args[0].data.str.str, "ms") == 0) {
2593 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
2594 } else
2595 return 0;
2596 } else if (args[0].type == ARGT_STOP) {
2597 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
2598 } else
2599 return 0;
2600 }
2601
2602 return 1;
2603}
2604
2605/* get the mean rtt of a client connexion */
2606static int
2607smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2608{
2609 if (!get_tcp_info(args, smp, 0, 0))
2610 return 0;
2611 return 1;
2612}
2613
2614/* get the variance of the mean rtt of a client connexion */
2615static int
2616smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
2617{
2618 if (!get_tcp_info(args, smp, 0, 1))
2619 return 0;
2620 return 1;
2621}
Joe Williams30fcd392016-08-10 07:06:44 -07002622
2623#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
2624
2625/* get the unacked counter on a client connexion */
2626static int
2627smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
2628{
2629 if (!get_tcp_info(args, smp, 0, 2))
2630 return 0;
2631 return 1;
2632}
2633
2634/* get the sacked counter on a client connexion */
2635static int
2636smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
2637{
2638 if (!get_tcp_info(args, smp, 0, 3))
2639 return 0;
2640 return 1;
2641}
2642
2643/* get the lost counter on a client connexion */
2644static int
2645smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
2646{
2647 if (!get_tcp_info(args, smp, 0, 4))
2648 return 0;
2649 return 1;
2650}
2651
2652/* get the retrans counter on a client connexion */
2653static int
2654smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
2655{
2656 if (!get_tcp_info(args, smp, 0, 5))
2657 return 0;
2658 return 1;
2659}
2660
2661/* get the fackets counter on a client connexion */
2662static int
2663smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
2664{
2665 if (!get_tcp_info(args, smp, 0, 6))
2666 return 0;
2667 return 1;
2668}
2669
2670/* get the reordering counter on a client connexion */
2671static int
2672smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
2673{
2674 if (!get_tcp_info(args, smp, 0, 7))
2675 return 0;
2676 return 1;
2677}
2678#endif // linux || freebsd || netbsd
2679#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02002680
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002681#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002682/* parse the "v4v6" bind keyword */
2683static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2684{
2685 struct listener *l;
2686
2687 list_for_each_entry(l, &conf->listeners, by_bind) {
2688 if (l->addr.ss_family == AF_INET6)
2689 l->options |= LI_O_V4V6;
2690 }
2691
2692 return 0;
2693}
2694
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002695/* parse the "v6only" bind keyword */
2696static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2697{
2698 struct listener *l;
2699
2700 list_for_each_entry(l, &conf->listeners, by_bind) {
2701 if (l->addr.ss_family == AF_INET6)
2702 l->options |= LI_O_V6ONLY;
2703 }
2704
2705 return 0;
2706}
2707#endif
2708
Pieter Baauwd551fb52013-05-08 22:49:23 +02002709#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002710/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002711static 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 +02002712{
2713 struct listener *l;
2714
Willy Tarreau4348fad2012-09-20 16:48:07 +02002715 list_for_each_entry(l, &conf->listeners, by_bind) {
2716 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2717 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02002718 }
2719
Willy Tarreau44791242012-09-12 23:27:21 +02002720 return 0;
2721}
2722#endif
2723
2724#ifdef TCP_DEFER_ACCEPT
2725/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002726static 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 +02002727{
2728 struct listener *l;
2729
Willy Tarreau4348fad2012-09-20 16:48:07 +02002730 list_for_each_entry(l, &conf->listeners, by_bind) {
2731 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2732 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02002733 }
2734
Willy Tarreau44791242012-09-12 23:27:21 +02002735 return 0;
2736}
2737#endif
2738
Willy Tarreau1c862c52012-10-05 16:21:00 +02002739#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01002740/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02002741static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2742{
2743 struct listener *l;
2744
2745 list_for_each_entry(l, &conf->listeners, by_bind) {
2746 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2747 l->options |= LI_O_TCP_FO;
2748 }
2749
2750 return 0;
2751}
2752#endif
2753
Willy Tarreau44791242012-09-12 23:27:21 +02002754#ifdef TCP_MAXSEG
2755/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002756static 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 +02002757{
2758 struct listener *l;
2759 int mss;
2760
Willy Tarreau44791242012-09-12 23:27:21 +02002761 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002762 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002763 return ERR_ALERT | ERR_FATAL;
2764 }
2765
2766 mss = atoi(args[cur_arg + 1]);
2767 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002768 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002769 return ERR_ALERT | ERR_FATAL;
2770 }
2771
Willy Tarreau4348fad2012-09-20 16:48:07 +02002772 list_for_each_entry(l, &conf->listeners, by_bind) {
2773 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2774 l->maxseg = mss;
2775 }
Willy Tarreau44791242012-09-12 23:27:21 +02002776
2777 return 0;
2778}
2779#endif
2780
Willy Tarreau2af207a2015-02-04 00:45:58 +01002781#ifdef TCP_USER_TIMEOUT
2782/* parse the "tcp-ut" bind keyword */
2783static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2784{
2785 const char *ptr = NULL;
2786 struct listener *l;
2787 unsigned int timeout;
2788
2789 if (!*args[cur_arg + 1]) {
2790 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
2791 return ERR_ALERT | ERR_FATAL;
2792 }
2793
2794 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
2795 if (ptr) {
2796 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
2797 return ERR_ALERT | ERR_FATAL;
2798 }
2799
2800 list_for_each_entry(l, &conf->listeners, by_bind) {
2801 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2802 l->tcp_ut = timeout;
2803 }
2804
2805 return 0;
2806}
2807#endif
2808
Willy Tarreau44791242012-09-12 23:27:21 +02002809#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01002810/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002811static 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 +02002812{
2813 struct listener *l;
2814
Willy Tarreau44791242012-09-12 23:27:21 +02002815 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002816 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002817 return ERR_ALERT | ERR_FATAL;
2818 }
2819
Willy Tarreau4348fad2012-09-20 16:48:07 +02002820 list_for_each_entry(l, &conf->listeners, by_bind) {
2821 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2822 l->interface = strdup(args[cur_arg + 1]);
2823 }
Willy Tarreau44791242012-09-12 23:27:21 +02002824
2825 global.last_checks |= LSTCHK_NETADM;
2826 return 0;
2827}
2828#endif
2829
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002830#ifdef CONFIG_HAP_NS
2831/* parse the "namespace" bind keyword */
2832static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2833{
2834 struct listener *l;
2835 char *namespace = NULL;
2836
2837 if (!*args[cur_arg + 1]) {
2838 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
2839 return ERR_ALERT | ERR_FATAL;
2840 }
2841 namespace = args[cur_arg + 1];
2842
2843 list_for_each_entry(l, &conf->listeners, by_bind) {
2844 l->netns = netns_store_lookup(namespace, strlen(namespace));
2845
2846 if (l->netns == NULL)
2847 l->netns = netns_store_insert(namespace);
2848
2849 if (l->netns == NULL) {
2850 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
2851 return ERR_ALERT | ERR_FATAL;
2852 }
2853 }
2854 return 0;
2855}
2856#endif
2857
Willy Tarreau163d4622015-10-13 16:16:41 +02002858#ifdef TCP_USER_TIMEOUT
2859/* parse the "tcp-ut" server keyword */
2860static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
2861{
2862 const char *ptr = NULL;
2863 unsigned int timeout;
2864
2865 if (!*args[*cur_arg + 1]) {
2866 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
2867 return ERR_ALERT | ERR_FATAL;
2868 }
2869
2870 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
2871 if (ptr) {
2872 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
2873 return ERR_ALERT | ERR_FATAL;
2874 }
2875
2876 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
2877 newsrv->tcp_ut = timeout;
2878
2879 return 0;
2880}
2881#endif
2882
Willy Tarreaudc13c112013-06-21 23:16:39 +02002883static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002884 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02002885 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02002886 { 0, NULL, NULL },
2887}};
2888
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002889
Willy Tarreau61612d42012-04-19 18:42:05 +02002890/* Note: must not be declared <const> as its list will be overwritten.
2891 * Please take care of keeping this list alphabetically sorted.
2892 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002893static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002894 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02002895}};
2896
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002897
Willy Tarreau4a129812012-04-25 17:31:42 +02002898/* Note: must not be declared <const> as its list will be overwritten.
2899 * Note: fetches that may return multiple types must be declared as the lowest
2900 * common denominator, the type that can be casted into all other ones. For
2901 * instance v4/v6 must be declared v4.
2902 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002903static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002904 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02002905 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002906 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002907 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02002908 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002909 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02002910#ifdef TCP_INFO
Joe Williams30fcd392016-08-10 07:06:44 -07002911 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2912 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2913#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
2914 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2915 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2916 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2917 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2918 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2919 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
2920#endif // linux || freebsd || netbsd
2921#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002922 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002923}};
2924
Willy Tarreau44791242012-09-12 23:27:21 +02002925/************************************************************************/
2926/* All supported bind keywords must be declared here. */
2927/************************************************************************/
2928
2929/* Note: must not be declared <const> as its list will be overwritten.
2930 * Please take care of keeping this list alphabetically sorted, doing so helps
2931 * all code contributors.
2932 * Optional keywords are also declared with a NULL ->parse() function so that
2933 * the config parser can report an appropriate error when a known keyword was
2934 * not enabled.
2935 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002936static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02002937#ifdef TCP_DEFER_ACCEPT
2938 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
2939#endif
2940#ifdef SO_BINDTODEVICE
2941 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
2942#endif
2943#ifdef TCP_MAXSEG
2944 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
2945#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01002946#ifdef TCP_USER_TIMEOUT
2947 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
2948#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02002949#ifdef TCP_FASTOPEN
2950 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
2951#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02002952#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002953 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
2954#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002955#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002956 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002957 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
2958#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002959#ifdef CONFIG_HAP_NS
2960 { "namespace", bind_parse_namespace, 1 },
2961#endif
Willy Tarreau44791242012-09-12 23:27:21 +02002962 /* the versions with the NULL parse function*/
2963 { "defer-accept", NULL, 0 },
2964 { "interface", NULL, 1 },
2965 { "mss", NULL, 1 },
2966 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002967 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002968 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002969 { NULL, NULL, 0 },
2970}};
2971
Willy Tarreau163d4622015-10-13 16:16:41 +02002972static struct srv_kw_list srv_kws = { "TCP", { }, {
2973#ifdef TCP_USER_TIMEOUT
2974 { "tcp-ut", srv_parse_tcp_ut, 1, 0 }, /* set TCP user timeout on server */
2975#endif
2976 { NULL, NULL, 0 },
2977}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02002978
2979static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02002980 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02002981 { "set-src", tcp_parse_set_src_dst },
2982 { "set-src-port", tcp_parse_set_src_dst },
2983 { "set-dst" , tcp_parse_set_src_dst },
2984 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02002985 { /* END */ }
2986}};
2987
Willy Tarreau620408f2016-10-21 16:37:51 +02002988static struct action_kw_list tcp_req_sess_actions = {ILH, {
2989 { "silent-drop", tcp_parse_silent_drop },
2990 { "set-src", tcp_parse_set_src_dst },
2991 { "set-src-port", tcp_parse_set_src_dst },
2992 { "set-dst" , tcp_parse_set_src_dst },
2993 { "set-dst-port", tcp_parse_set_src_dst },
2994 { /* END */ }
2995}};
2996
Willy Tarreau2d392c22015-08-24 01:43:45 +02002997static struct action_kw_list tcp_req_cont_actions = {ILH, {
2998 { "silent-drop", tcp_parse_silent_drop },
2999 { /* END */ }
3000}};
3001
3002static struct action_kw_list tcp_res_cont_actions = {ILH, {
3003 { "silent-drop", tcp_parse_silent_drop },
3004 { /* END */ }
3005}};
3006
3007static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02003008 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02003009 { "set-src", tcp_parse_set_src_dst },
3010 { "set-src-port", tcp_parse_set_src_dst },
3011 { "set-dst", tcp_parse_set_src_dst },
3012 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02003013 { /* END */ }
3014}};
3015
3016static struct action_kw_list http_res_actions = {ILH, {
3017 { "silent-drop", tcp_parse_silent_drop },
3018 { /* END */ }
3019}};
3020
3021
Willy Tarreaue6b98942007-10-29 01:09:36 +01003022__attribute__((constructor))
3023static void __tcp_protocol_init(void)
3024{
3025 protocol_register(&proto_tcpv4);
3026 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02003027 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02003028 cfg_register_keywords(&cfg_kws);
3029 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02003030 bind_register_keywords(&bind_kws);
Willy Tarreau163d4622015-10-13 16:16:41 +02003031 srv_register_keywords(&srv_kws);
Willy Tarreau2d392c22015-08-24 01:43:45 +02003032 tcp_req_conn_keywords_register(&tcp_req_conn_actions);
Willy Tarreau620408f2016-10-21 16:37:51 +02003033 tcp_req_sess_keywords_register(&tcp_req_sess_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02003034 tcp_req_cont_keywords_register(&tcp_req_cont_actions);
3035 tcp_res_cont_keywords_register(&tcp_res_cont_actions);
3036 http_req_keywords_register(&http_req_actions);
3037 http_res_keywords_register(&http_res_actions);
Willy Tarreaue6b98942007-10-29 01:09:36 +01003038}
3039
3040
3041/*
3042 * Local variables:
3043 * c-indent-level: 8
3044 * c-basic-offset: 8
3045 * End:
3046 */