blob: df10ccbd9c5defa2cf25511cad1fba34be515358 [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
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040027#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020028#include <netinet/in.h>
29#include <netinet/ip.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040030
Willy Tarreaub6866442008-07-14 23:54:42 +020031#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010032#include <common/compat.h>
33#include <common/config.h>
34#include <common/debug.h>
35#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010036#include <common/mini-clist.h>
37#include <common/standard.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010038#include <common/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039
Willy Tarreaue6b98942007-10-29 01:09:36 +010040#include <types/global.h>
Willy Tarreau18bf01e2014-06-13 16:18:52 +020041#include <types/capture.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020042#include <types/server.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020043#include <types/connection.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010044
45#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020046#include <proto/action.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020047#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020048#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020049#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020050#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020051#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020052#include <proto/log.h>
53#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020054#include <proto/protocol.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020055#include <proto/proto_http.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010056#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020057#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020058#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020059#include <proto/stream.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020060#include <proto/stick_table.h>
Willy Tarreaucc1e04b2013-09-11 23:20:29 +020061#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020062#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010063
Emeric Bruncf20bf12010-10-22 16:06:11 +020064static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
65static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010066
Thierry FOURNIERcc87a112014-12-12 19:41:33 +010067/* List head of all known action keywords for "tcp-request connection" */
68struct list tcp_req_conn_keywords = LIST_HEAD_INIT(tcp_req_conn_keywords);
69struct list tcp_req_cont_keywords = LIST_HEAD_INIT(tcp_req_cont_keywords);
70struct list tcp_res_cont_keywords = LIST_HEAD_INIT(tcp_res_cont_keywords);
71
Willy Tarreaue6b98942007-10-29 01:09:36 +010072/* Note: must not be declared <const> as its list will be overwritten */
73static struct protocol proto_tcpv4 = {
74 .name = "tcpv4",
75 .sock_domain = AF_INET,
76 .sock_type = SOCK_STREAM,
77 .sock_prot = IPPROTO_TCP,
78 .sock_family = AF_INET,
79 .sock_addrlen = sizeof(struct sockaddr_in),
80 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020081 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020082 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020083 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010084 .bind_all = tcp_bind_listeners,
85 .unbind_all = unbind_all_listeners,
86 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020087 .get_src = tcp_get_src,
88 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020089 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +020090 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010091 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
92 .nb_listeners = 0,
93};
94
95/* Note: must not be declared <const> as its list will be overwritten */
96static struct protocol proto_tcpv6 = {
97 .name = "tcpv6",
98 .sock_domain = AF_INET6,
99 .sock_type = SOCK_STREAM,
100 .sock_prot = IPPROTO_TCP,
101 .sock_family = AF_INET6,
102 .sock_addrlen = sizeof(struct sockaddr_in6),
103 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200104 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200105 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200106 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100107 .bind_all = tcp_bind_listeners,
108 .unbind_all = unbind_all_listeners,
109 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200110 .get_src = tcp_get_src,
111 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200112 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +0200113 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100114 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
115 .nb_listeners = 0,
116};
117
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100118/*
119 * Register keywords.
120 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200121void tcp_req_conn_keywords_register(struct action_kw_list *kw_list)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100122{
123 LIST_ADDQ(&tcp_req_conn_keywords, &kw_list->list);
124}
125
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200126void tcp_req_cont_keywords_register(struct action_kw_list *kw_list)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100127{
128 LIST_ADDQ(&tcp_req_cont_keywords, &kw_list->list);
129}
130
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200131void tcp_res_cont_keywords_register(struct action_kw_list *kw_list)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100132{
133 LIST_ADDQ(&tcp_res_cont_keywords, &kw_list->list);
134}
135
136/*
137 * Return the struct http_req_action_kw associated to a keyword.
138 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200139static struct action_kw *tcp_req_conn_action(const char *kw)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100140{
Thierry FOURNIER322a1242015-08-19 09:07:47 +0200141 return action_lookup(&tcp_req_conn_keywords, kw);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100142}
143
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200144static struct action_kw *tcp_req_cont_action(const char *kw)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100145{
Thierry FOURNIER322a1242015-08-19 09:07:47 +0200146 return action_lookup(&tcp_req_cont_keywords, kw);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100147}
148
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200149static struct action_kw *tcp_res_cont_action(const char *kw)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100150{
Thierry FOURNIER322a1242015-08-19 09:07:47 +0200151 return action_lookup(&tcp_res_cont_keywords, kw);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100152}
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100153
David du Colombier6f5ccb12011-03-10 22:26:24 +0100154/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100155 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
156 * - 0 : ignore remote address (may even be a NULL pointer)
157 * - 1 : use provided address
158 * - 2 : use provided port
159 * - 3 : use both
160 *
161 * The function supports multiple foreign binding methods :
162 * - linux_tproxy: we directly bind to the foreign address
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100163 * The second one can be used as a fallback for the first one.
164 * This function returns 0 when everything's OK, 1 if it could not bind, to the
165 * local address, 2 if it could not bind to the foreign address.
166 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100167int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100168{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100169 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100170 int foreign_ok = 0;
171 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100172 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200173 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200174
David du Colombier65c17962012-07-13 14:34:59 +0200175 switch (local->ss_family) {
176 case AF_INET:
177 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200178 /* This deserves some explanation. Some platforms will support
179 * multiple combinations of certain methods, so we try the
180 * supported ones until one succeeds.
181 */
182 if (0
183#if defined(IP_TRANSPARENT)
184 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
185#endif
186#if defined(IP_FREEBIND)
187 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
188#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200189#if defined(IP_BINDANY)
190 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
191#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200192#if defined(SO_BINDANY)
193 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
194#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200195 )
David du Colombier65c17962012-07-13 14:34:59 +0200196 foreign_ok = 1;
197 else
198 ip_transp_working = 0;
199 }
200 break;
201 case AF_INET6:
202 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200203 if (0
204#if defined(IPV6_TRANSPARENT)
205 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
206#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100207#if defined(IP_FREEBIND)
208 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
209#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200210#if defined(IPV6_BINDANY)
211 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
212#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200213#if defined(SO_BINDANY)
214 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
215#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200216 )
David du Colombier65c17962012-07-13 14:34:59 +0200217 foreign_ok = 1;
218 else
219 ip6_transp_working = 0;
220 }
221 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100222 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200223
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100224 if (flags) {
225 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200226 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100227 switch (remote->ss_family) {
228 case AF_INET:
229 if (flags & 1)
230 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
231 if (flags & 2)
232 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
233 break;
234 case AF_INET6:
235 if (flags & 1)
236 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
237 if (flags & 2)
238 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
239 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100240 default:
241 /* we don't want to try to bind to an unknown address family */
242 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100243 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100244 }
245
Simon Hormande072bd2011-06-24 15:11:37 +0900246 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100247 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200248 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200249 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
250 if (ret < 0)
251 return 2;
252 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100253 }
254 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200255 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200256 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
257 if (ret < 0)
258 return 1;
259 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100260 }
261
262 if (!flags)
263 return 0;
264
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100265 if (!foreign_ok)
266 /* we could not bind to a foreign address */
267 return 2;
268
269 return 0;
270}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100271
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100272static int create_server_socket(struct connection *conn)
273{
Willy Tarreau529c1392014-12-24 13:47:55 +0100274 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100275
Willy Tarreau529c1392014-12-24 13:47:55 +0100276#ifdef CONFIG_HAP_NS
277 if (objt_server(conn->target)) {
278 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
279 ns = conn->proxy_netns;
280 else
281 ns = __objt_server(conn->target)->netns;
282 }
283#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100284 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
285}
Willy Tarreau9650f372009-08-16 14:02:45 +0200286
287/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200288 * This function initiates a TCP connection establishment to the target assigned
289 * to connection <conn> using (si->{target,addr.to}). A source address may be
290 * pointed to by conn->addr.from in case of transparent proxying. Normal source
291 * bind addresses are still determined locally (due to the possible need of a
292 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100293 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100294 * supported. The <data> parameter is a boolean indicating whether there are data
295 * waiting for being sent or not, in order to adjust data write polling and on
296 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
297 * allows the caller to force using a delayed ACK when establishing the connection :
298 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
299 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
300 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200301 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200302 * Note that a pending send_proxy message accounts for data.
303 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200304 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200305 * - SF_ERR_NONE if everything's OK
306 * - SF_ERR_SRVTO if there are no more servers
307 * - SF_ERR_SRVCL if the connection was refused by the server
308 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
309 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
310 * - SF_ERR_INTERNAL for any other purely internal errors
311 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100312 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200313 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100314 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200315 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100316
Willy Tarreauf0837b22012-11-24 10:24:27 +0100317int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200318{
319 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100320 struct server *srv;
321 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100322 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100323
Willy Tarreau9ce70132014-01-24 16:08:19 +0100324 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
325
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100326 switch (obj_type(conn->target)) {
327 case OBJ_TYPE_PROXY:
328 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100329 srv = NULL;
330 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100331 case OBJ_TYPE_SERVER:
332 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100333 be = srv->proxy;
334 break;
335 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100336 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200337 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100338 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200339
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100340 fd = conn->t.sock.fd = create_server_socket(conn);
341
342 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200343 qfprintf(stderr, "Cannot get a server socket.\n");
344
Willy Tarreau9ce70132014-01-24 16:08:19 +0100345 if (errno == ENFILE) {
346 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200347 send_log(be, LOG_EMERG,
348 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
349 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100350 }
351 else if (errno == EMFILE) {
352 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200353 send_log(be, LOG_EMERG,
354 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
355 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100356 }
357 else if (errno == ENOBUFS || errno == ENOMEM) {
358 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200359 send_log(be, LOG_EMERG,
360 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
361 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100362 }
363 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
364 conn->err_code = CO_ER_NOPROTO;
365 }
366 else
367 conn->err_code = CO_ER_SOCK_ERR;
368
Willy Tarreau9650f372009-08-16 14:02:45 +0200369 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100370 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200371 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200372 }
373
374 if (fd >= global.maxsock) {
375 /* do not log anything there, it's a normal condition when this option
376 * is used to serialize connections to a server !
377 */
378 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
379 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100380 conn->err_code = CO_ER_CONF_FDLIM;
381 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200382 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200383 }
384
385 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900386 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200387 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
388 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100389 conn->err_code = CO_ER_SOCK_ERR;
390 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200391 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200392 }
393
394 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900395 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200396
Willy Tarreau9650f372009-08-16 14:02:45 +0200397 /* allow specific binding :
398 * - server-specific at first
399 * - proxy-specific next
400 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100401 if (srv && srv->conn_src.opts & CO_SRC_BIND)
402 src = &srv->conn_src;
403 else if (be->conn_src.opts & CO_SRC_BIND)
404 src = &be->conn_src;
405 else
406 src = NULL;
407
408 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200409 int ret, flags = 0;
410
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200411 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100412 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100413 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200414 conn->flags |= CO_FL_PRIVATE;
415 /* fall through */
416 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200417 flags = 3;
418 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100419 case CO_SRC_TPROXY_CIP:
420 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200421 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200422 flags = 1;
423 break;
424 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200425 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200426
Willy Tarreau9650f372009-08-16 14:02:45 +0200427#ifdef SO_BINDTODEVICE
428 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100429 if (src->iface_name)
430 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200431#endif
432
Willy Tarreaua4380b42012-12-08 22:49:11 +0100433 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200434 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100435 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200436
437 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100438 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200439
440 do {
441 /* note: in case of retry, we may have to release a previously
442 * allocated port, hence this loop's construct.
443 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200444 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
445 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200446
447 if (!attempts)
448 break;
449 attempts--;
450
Willy Tarreaua4380b42012-12-08 22:49:11 +0100451 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100452 if (!fdinfo[fd].local_port) {
453 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100455 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200456
Willy Tarreaua4380b42012-12-08 22:49:11 +0100457 fdinfo[fd].port_range = src->sport_range;
458 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200459
Willy Tarreaua4380b42012-12-08 22:49:11 +0100460 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100461 if (ret != 0)
462 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200463 } while (ret != 0); /* binding NOK */
464 }
465 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100466 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100467 if (ret != 0)
468 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 }
470
Willy Tarreaua4380b42012-12-08 22:49:11 +0100471 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200472 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
473 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200474 close(fd);
475
Willy Tarreau9650f372009-08-16 14:02:45 +0200476 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100477 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200478 be->id);
479 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100480 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200481 be->id);
482 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100483 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200484 be->id);
485 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100486 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200487 be->id);
488 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100489 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200490 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200491 }
492 }
493
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400494#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200495 /* disabling tcp quick ack now allows the first request to leave the
496 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100497 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200498 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200499 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900500 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200501#endif
502
Willy Tarreaue803de22010-01-21 17:43:04 +0100503 if (global.tune.server_sndbuf)
504 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
505
506 if (global.tune.server_rcvbuf)
507 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
508
Willy Tarreau986a9d22012-08-30 21:11:38 +0200509 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200510 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
511
Willy Tarreaub1719512012-12-08 23:03:28 +0100512 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200513 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100514 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200515 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100516 conn->err_code = CO_ER_FREE_PORTS;
517 }
518 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200519 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100520 conn->err_code = CO_ER_ADDR_INUSE;
521 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200522
Willy Tarreaub1719512012-12-08 23:03:28 +0100523 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200524 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
525 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200526 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100527 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100528 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200529 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200530 } else if (errno == ETIMEDOUT) {
531 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200532 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
533 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200534 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100535 conn->err_code = CO_ER_SOCK_ERR;
536 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200537 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200538 } else {
539 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
540 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200541 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
542 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200543 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100544 conn->err_code = CO_ER_SOCK_ERR;
545 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200546 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200547 }
548 }
549
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100550 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200551
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200552 /* Prepare to send a few handshakes related to the on-wire protocol. */
553 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200554 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200555
Willy Tarreauf79c8172013-10-21 16:30:56 +0200556 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100557 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200558 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200559
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200560 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200561 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100562 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200563 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200564 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200565
Willy Tarreau14f8e862012-08-30 22:23:13 +0200566 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200567 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200568
Willy Tarreaue7dff022015-04-03 01:14:29 +0200569 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200570}
571
572
Willy Tarreau59b94792012-05-11 16:16:40 +0200573/*
574 * Retrieves the source address for the socket <fd>, with <dir> indicating
575 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
576 * success, -1 in case of error. The socket's source address is stored in
577 * <sa> for <salen> bytes.
578 */
579int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
580{
581 if (dir)
582 return getsockname(fd, sa, &salen);
583 else
584 return getpeername(fd, sa, &salen);
585}
586
587
588/*
589 * Retrieves the original destination address for the socket <fd>, with <dir>
590 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
591 * listener, if the original destination address was translated, the original
592 * address is retrieved. It returns 0 in case of success, -1 in case of error.
593 * The socket's source address is stored in <sa> for <salen> bytes.
594 */
595int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
596{
597 if (dir)
598 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100599 else {
600 int ret = getsockname(fd, sa, &salen);
601
602 if (ret < 0)
603 return ret;
604
Willy Tarreau59b94792012-05-11 16:16:40 +0200605#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100606 /* For TPROXY and Netfilter's NAT, we can retrieve the original
607 * IPv4 address before DNAT/REDIRECT. We must not do that with
608 * other families because v6-mapped IPv4 addresses are still
609 * reported as v4.
610 */
611 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
612 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
613 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200614#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100615 return ret;
616 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200617}
618
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200619/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100620 * receive shutdown. Returns positive if the shutdown was found, negative
621 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
622 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200623 */
624int tcp_drain(int fd)
625{
626 int turns = 2;
627 int len;
628
629 while (turns) {
630#ifdef MSG_TRUNC_CLEARS_INPUT
631 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
632 if (len == -1 && errno == EFAULT)
633#endif
634 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
635
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100636 if (len == 0) {
637 /* cool, shutdown received */
638 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200639 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100640 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200641
642 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100643 if (errno == EAGAIN) {
644 /* connection not closed yet */
645 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100646 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100647 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200648 if (errno == EINTR) /* oops, try again */
649 continue;
650 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100651 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200652 return 1;
653 }
654 /* OK we read some data, let's try again once */
655 turns--;
656 }
657 /* some data are still present, give up */
658 return 0;
659}
660
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200661/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100662 * and we have nothing to send. It updates the FD polling status. It returns 0
663 * if it fails in a fatal way or needs to poll to go further, otherwise it
664 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
665 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
666 * errno. The error checking is done in two passes in order to limit the number
667 * of syscalls in the normal case :
668 * - if POLL_ERR was reported by the poller, we check for a pending error on
669 * the socket before proceeding. If found, it's assigned to errno so that
670 * upper layers can see it.
671 * - otherwise connect() is used to check the connection state again, since
672 * the getsockopt return cannot reliably be used to know if the connection
673 * is still pending or ready. This one may often return an error as well,
674 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200675 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200676int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200677{
Willy Tarreau239d7182012-07-23 18:53:03 +0200678 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100679 socklen_t lskerr;
680 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200681
Willy Tarreau80184712012-07-06 14:54:49 +0200682 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200683 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200684
Willy Tarreau3c728722014-01-23 13:50:42 +0100685 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200686 return 0;
687
Willy Tarreau80184712012-07-06 14:54:49 +0200688 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200689 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200690
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100691 if (!fd_send_ready(fd))
692 return 0;
693
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100694 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
695 * without FD_POLL_IN also indicates a hangup without input data meaning
696 * there was no connection.
697 */
698 if (fdtab[fd].ev & FD_POLL_ERR ||
699 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
700 skerr = 0;
701 lskerr = sizeof(skerr);
702 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
703 errno = skerr;
704 if (errno == EAGAIN)
705 errno = 0;
706 if (errno)
707 goto out_error;
708 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200709
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100710 /* Use connect() to check the state of the socket. This has the
711 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200712 * - error
713 * - connecting (EALREADY, EINPROGRESS)
714 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200715 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200716 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200717 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100718 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100719 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200720 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200721 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200722
Willy Tarreau2c6be842012-07-06 17:12:34 +0200723 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200724 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200725
Willy Tarreau2c6be842012-07-06 17:12:34 +0200726 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200727 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200728
Willy Tarreau076be252012-07-06 16:02:29 +0200729 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200730 * forward the event to the transport layer which will notify the
731 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200732 */
Willy Tarreau80184712012-07-06 14:54:49 +0200733 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200734 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200735
736 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200737 /* Write error on the file descriptor. Report it to the connection
738 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200739 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100740 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100741 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100742 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200743 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200744}
745
Willy Tarreau59b94792012-05-11 16:16:40 +0200746
Willy Tarreaue6b98942007-10-29 01:09:36 +0100747/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100748 * an error message in <errmsg> if the message is at most <errlen> bytes long
749 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
750 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100751 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
752 * was alright and that no message was returned. ERR_RETRYABLE means that an
753 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700754 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100755 * the meaning of the error, but just indicate that a message is present which
756 * should be displayed with the respective level. Last, ERR_ABORT indicates
757 * that it's pointless to try to start other listeners. No error message is
758 * returned if errlen is NULL.
759 */
760int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
761{
762 __label__ tcp_return, tcp_close_return;
763 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100764 int ext, ready;
765 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100766 const char *msg = NULL;
767
768 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100769 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100770 *errmsg = 0;
771
772 if (listener->state != LI_ASSIGNED)
773 return ERR_NONE; /* already bound */
774
775 err = ERR_NONE;
776
Willy Tarreau40aa0702013-03-10 23:51:38 +0100777 /* if the listener already has an fd assigned, then we were offered the
778 * fd by an external process (most likely the parent), and we don't want
779 * to create a new socket. However we still want to set a few flags on
780 * the socket.
781 */
782 fd = listener->fd;
783 ext = (fd >= 0);
784
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100785 if (!ext) {
786 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
787
788 if (fd == -1) {
789 err |= ERR_RETRYABLE | ERR_ALERT;
790 msg = "cannot create listening socket";
791 goto tcp_return;
792 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100793 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100794
Willy Tarreaue6b98942007-10-29 01:09:36 +0100795 if (fd >= global.maxsock) {
796 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
797 msg = "not enough free sockets (raise '-n' parameter)";
798 goto tcp_close_return;
799 }
800
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200801 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100802 err |= ERR_FATAL | ERR_ALERT;
803 msg = "cannot make socket non-blocking";
804 goto tcp_close_return;
805 }
806
Willy Tarreau40aa0702013-03-10 23:51:38 +0100807 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100808 /* not fatal but should be reported */
809 msg = "cannot do so_reuseaddr";
810 err |= ERR_ALERT;
811 }
812
813 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900814 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100815
Willy Tarreaue6b98942007-10-29 01:09:36 +0100816#ifdef SO_REUSEPORT
817 /* OpenBSD supports this. As it's present in old libc versions of Linux,
818 * it might return an error that we will silently ignore.
819 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100820 if (!ext)
821 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100822#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200823
Willy Tarreau40aa0702013-03-10 23:51:38 +0100824 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200825 switch (listener->addr.ss_family) {
826 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200827 if (1
828#if defined(IP_TRANSPARENT)
829 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
830#endif
831#if defined(IP_FREEBIND)
832 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
833#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200834#if defined(IP_BINDANY)
835 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
836#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200837#if defined(SO_BINDANY)
838 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
839#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200840 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200841 msg = "cannot make listening socket transparent";
842 err |= ERR_ALERT;
843 }
844 break;
845 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200846 if (1
847#if defined(IPV6_TRANSPARENT)
848 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
849#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100850#if defined(IP_FREEBIND)
851 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
852#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200853#if defined(IPV6_BINDANY)
854 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
855#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200856#if defined(SO_BINDANY)
857 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
858#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200859 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200860 msg = "cannot make listening socket transparent";
861 err |= ERR_ALERT;
862 }
863 break;
864 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100865 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200866
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100867#ifdef SO_BINDTODEVICE
868 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100869 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100870 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100871 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100872 msg = "cannot bind listener to device";
873 err |= ERR_WARN;
874 }
875 }
876#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400877#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100878 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400879 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200880 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
881 msg = "cannot set MSS";
882 err |= ERR_WARN;
883 }
884 }
885#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100886#if defined(TCP_USER_TIMEOUT)
887 if (listener->tcp_ut) {
888 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
889 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
890 msg = "cannot set TCP User Timeout";
891 err |= ERR_WARN;
892 }
893 }
894#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200895#if defined(TCP_DEFER_ACCEPT)
896 if (listener->options & LI_O_DEF_ACCEPT) {
897 /* defer accept by up to one second */
898 int accept_delay = 1;
899 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
900 msg = "cannot enable DEFER_ACCEPT";
901 err |= ERR_WARN;
902 }
903 }
904#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200905#if defined(TCP_FASTOPEN)
906 if (listener->options & LI_O_TCP_FO) {
907 /* TFO needs a queue length, let's use the configured backlog */
908 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
909 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
910 msg = "cannot enable TCP_FASTOPEN";
911 err |= ERR_WARN;
912 }
913 }
914#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100915#if defined(IPV6_V6ONLY)
916 if (listener->options & LI_O_V6ONLY)
917 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100918 else if (listener->options & LI_O_V4V6)
919 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100920#endif
921
Willy Tarreau40aa0702013-03-10 23:51:38 +0100922 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100923 err |= ERR_RETRYABLE | ERR_ALERT;
924 msg = "cannot bind socket";
925 goto tcp_close_return;
926 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100927
Willy Tarreau40aa0702013-03-10 23:51:38 +0100928 ready = 0;
929 ready_len = sizeof(ready);
930 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
931 ready = 0;
932
933 if (!(ext && ready) && /* only listen if not already done by external process */
934 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100935 err |= ERR_RETRYABLE | ERR_ALERT;
936 msg = "cannot listen to socket";
937 goto tcp_close_return;
938 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100939
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400940#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200941 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900942 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200943#endif
944
Willy Tarreaue6b98942007-10-29 01:09:36 +0100945 /* the socket is ready */
946 listener->fd = fd;
947 listener->state = LI_LISTEN;
948
Willy Tarreaueabf3132008-08-29 23:36:51 +0200949 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200950 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200951 fd_insert(fd);
952
Willy Tarreaue6b98942007-10-29 01:09:36 +0100953 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100954 if (msg && errlen) {
955 char pn[INET6_ADDRSTRLEN];
956
Willy Tarreau631f01c2011-09-05 00:36:48 +0200957 addr_to_str(&listener->addr, pn, sizeof(pn));
958 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100959 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100960 return err;
961
962 tcp_close_return:
963 close(fd);
964 goto tcp_return;
965}
966
967/* This function creates all TCP sockets bound to the protocol entry <proto>.
968 * It is intended to be used as the protocol's bind_all() function.
969 * The sockets will be registered but not added to any fd_set, in order not to
970 * loose them across the fork(). A call to enable_all_listeners() is needed
971 * to complete initialization. The return value is composed from ERR_*.
972 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200973static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100974{
975 struct listener *listener;
976 int err = ERR_NONE;
977
978 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200979 err |= tcp_bind_listener(listener, errmsg, errlen);
980 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100981 break;
982 }
983
984 return err;
985}
986
987/* Add listener to the list of tcpv4 listeners. The listener's state
988 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
989 * listeners is updated. This is the function to use to add a new listener.
990 */
991void tcpv4_add_listener(struct listener *listener)
992{
993 if (listener->state != LI_INIT)
994 return;
995 listener->state = LI_ASSIGNED;
996 listener->proto = &proto_tcpv4;
997 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
998 proto_tcpv4.nb_listeners++;
999}
1000
1001/* Add listener to the list of tcpv4 listeners. The listener's state
1002 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1003 * listeners is updated. This is the function to use to add a new listener.
1004 */
1005void tcpv6_add_listener(struct listener *listener)
1006{
1007 if (listener->state != LI_INIT)
1008 return;
1009 listener->state = LI_ASSIGNED;
1010 listener->proto = &proto_tcpv6;
1011 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1012 proto_tcpv6.nb_listeners++;
1013}
1014
Willy Tarreau092d8652014-07-07 20:22:12 +02001015/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1016 * was totally stopped, or > 0 if correctly paused.
1017 */
1018int tcp_pause_listener(struct listener *l)
1019{
1020 if (shutdown(l->fd, SHUT_WR) != 0)
1021 return -1; /* Solaris dies here */
1022
1023 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1024 return -1; /* OpenBSD dies here */
1025
1026 if (shutdown(l->fd, SHUT_RD) != 0)
1027 return -1; /* should always be OK */
1028 return 1;
1029}
1030
Willy Tarreauedcf6682008-11-30 23:15:34 +01001031/* This function performs the TCP request analysis on the current request. It
1032 * returns 1 if the processing can continue on next analysers, or zero if it
1033 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +02001034 * request. It relies on buffers flags, and updates s->req->analysers. The
1035 * function may be called for frontend rules and backend rules. It only relies
1036 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001037 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001038int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +01001039{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001040 struct session *sess = s->sess;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001041 struct act_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +02001042 struct stksess *ts;
1043 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001044 int partial;
Willy Tarreauacc98002015-09-27 23:34:39 +02001045 int act_flags = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001046
Willy Tarreau87b09662015-04-03 00:22:06 +02001047 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 +01001048 now_ms, __FUNCTION__,
1049 s,
1050 req,
1051 req->rex, req->wex,
1052 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001053 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +01001054 req->analysers);
1055
Willy Tarreauedcf6682008-11-30 23:15:34 +01001056 /* We don't know whether we have enough data, so must proceed
1057 * this way :
1058 * - iterate through all rules in their declaration order
1059 * - if one rule returns MISS, it means the inspect delay is
1060 * not over yet, then return immediately, otherwise consider
1061 * it as a non-match.
1062 * - if one rule returns OK, then return OK
1063 * - if one rule returns KO, then return KO
1064 */
1065
Willy Tarreau9b28e032012-10-12 23:49:43 +02001066 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001067 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001068 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001069 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001070 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001071
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001072 /* If "the current_rule_list" match the executed rule list, we are in
1073 * resume condition. If a resume is needed it is always in the action
1074 * and never in the ACL or converters. In this case, we initialise the
1075 * current rule, and go to the action execution point.
1076 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001077 if (s->current_rule) {
1078 rule = s->current_rule;
1079 s->current_rule = NULL;
1080 if (s->current_rule_list == &s->be->tcp_req.inspect_rules)
1081 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001082 }
1083 s->current_rule_list = &s->be->tcp_req.inspect_rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001084
Willy Tarreaufb356202010-08-03 14:02:05 +02001085 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001086 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001087
1088 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001089 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001090 if (ret == ACL_TEST_MISS)
1091 goto missing_data;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001092
1093 ret = acl_pass(ret);
1094 if (rule->cond->pol == ACL_COND_UNLESS)
1095 ret = !ret;
1096 }
1097
1098 if (ret) {
Willy Tarreauacc98002015-09-27 23:34:39 +02001099 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001100resume_execution:
Willy Tarreauedcf6682008-11-30 23:15:34 +01001101 /* we have a matching rule. */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001102 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreau27f78242015-07-04 11:36:30 +02001103 break;
1104 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001105 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001106 channel_abort(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001107 channel_abort(&s->res);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001108 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001109
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001110 s->be->be_counters.denied_req++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001111 sess->fe->fe_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001112 if (sess->listener->counters)
1113 sess->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001114
Willy Tarreaue7dff022015-04-03 01:14:29 +02001115 if (!(s->flags & SF_ERR_MASK))
1116 s->flags |= SF_ERR_PRXCOND;
1117 if (!(s->flags & SF_FINST_MASK))
1118 s->flags |= SF_FINST_R;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001119 return 0;
1120 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001121 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001122 /* Note: only the first valid tracking parameter of each
1123 * applies.
1124 */
1125 struct stktable_key *key;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001126 struct sample smp;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001127
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001128 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001129 continue;
1130
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001131 t = rule->arg.trk_ctr.table.t;
1132 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 +02001133
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001134 if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL))
1135 goto missing_data; /* key might appear later */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001136
1137 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001138 stream_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001139 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001140 if (sess->fe != s->be)
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001141 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreaud1f96522010-08-03 19:34:32 +02001142 }
1143 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001144 else if (rule->action == ACT_TCP_CAPTURE) {
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001145 struct sample *key;
Thierry FOURNIERd0d65ae2015-08-04 08:21:12 +02001146 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001147 char **cap = s->req_cap;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001148 int len;
1149
Thierry FOURNIERd0d65ae2015-08-04 08:21:12 +02001150 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 +02001151 if (!key)
1152 continue;
1153
1154 if (key->flags & SMP_F_MAY_CHANGE)
1155 goto missing_data;
1156
1157 if (cap[h->index] == NULL)
1158 cap[h->index] = pool_alloc2(h->pool);
1159
1160 if (cap[h->index] == NULL) /* no more capture memory */
1161 continue;
1162
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001163 len = key->data.u.str.len;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001164 if (len > h->len)
1165 len = h->len;
1166
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001167 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001168 cap[h->index][len] = 0;
1169 }
Willy Tarreaud1f96522010-08-03 19:34:32 +02001170 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001171 /* Custom keywords. */
Thierry FOURNIER42148732015-09-02 17:17:33 +02001172 if (!rule->action_ptr)
1173 continue;
Willy Tarreauc1b10d32015-09-27 10:06:24 +02001174
Willy Tarreauacc98002015-09-27 23:34:39 +02001175 if (partial & SMP_OPT_FINAL)
1176 act_flags |= ACT_FLAG_FINAL;
1177
1178 switch (rule->action_ptr(rule, s->be, s->sess, s, act_flags)) {
Thierry FOURNIER42148732015-09-02 17:17:33 +02001179 case ACT_RET_ERR:
1180 case ACT_RET_CONT:
1181 continue;
1182 case ACT_RET_STOP:
Thierry FOURNIER561a0f92015-05-29 17:29:14 +02001183 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001184 case ACT_RET_YIELD:
1185 s->current_rule = rule;
1186 goto missing_data;
1187 }
1188 break; /* ACT_RET_STOP */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001189 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001190 }
1191 }
1192
1193 /* if we get there, it means we have no rule which matches, or
1194 * we have an explicit accept, so we apply the default accept.
1195 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001196 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001197 req->analyse_exp = TICK_ETERNITY;
1198 return 1;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001199
1200 missing_data:
1201 channel_dont_connect(req);
1202 /* just set the request timeout once at the beginning of the request */
1203 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
1204 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
1205 return 0;
1206
Willy Tarreauedcf6682008-11-30 23:15:34 +01001207}
1208
Emeric Brun97679e72010-09-23 17:56:44 +02001209/* This function performs the TCP response analysis on the current response. It
1210 * returns 1 if the processing can continue on next analysers, or zero if it
1211 * needs more data, encounters an error, or wants to immediately abort the
1212 * response. It relies on buffers flags, and updates s->rep->analysers. The
1213 * function may be called for backend rules.
1214 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001215int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001216{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001217 struct session *sess = s->sess;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001218 struct act_rule *rule;
Emeric Brun97679e72010-09-23 17:56:44 +02001219 int partial;
Willy Tarreauacc98002015-09-27 23:34:39 +02001220 int act_flags = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001221
Willy Tarreau87b09662015-04-03 00:22:06 +02001222 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 +02001223 now_ms, __FUNCTION__,
1224 s,
1225 rep,
1226 rep->rex, rep->wex,
1227 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001228 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001229 rep->analysers);
1230
1231 /* We don't know whether we have enough data, so must proceed
1232 * this way :
1233 * - iterate through all rules in their declaration order
1234 * - if one rule returns MISS, it means the inspect delay is
1235 * not over yet, then return immediately, otherwise consider
1236 * it as a non-match.
1237 * - if one rule returns OK, then return OK
1238 * - if one rule returns KO, then return KO
1239 */
1240
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001241 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001242 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001243 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001244 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001245
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001246 /* If "the current_rule_list" match the executed rule list, we are in
1247 * resume condition. If a resume is needed it is always in the action
1248 * and never in the ACL or converters. In this case, we initialise the
1249 * current rule, and go to the action execution point.
1250 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001251 if (s->current_rule) {
1252 rule = s->current_rule;
1253 s->current_rule = NULL;
1254 if (s->current_rule_list == &s->be->tcp_rep.inspect_rules)
1255 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001256 }
1257 s->current_rule_list = &s->be->tcp_rep.inspect_rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001258
Emeric Brun97679e72010-09-23 17:56:44 +02001259 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001260 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001261
1262 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001263 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001264 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001265 /* just set the analyser timeout once at the beginning of the response */
1266 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001267 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001268 return 0;
1269 }
1270
1271 ret = acl_pass(ret);
1272 if (rule->cond->pol == ACL_COND_UNLESS)
1273 ret = !ret;
1274 }
1275
1276 if (ret) {
Willy Tarreauacc98002015-09-27 23:34:39 +02001277 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001278resume_execution:
Emeric Brun97679e72010-09-23 17:56:44 +02001279 /* we have a matching rule. */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001280 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreau27f78242015-07-04 11:36:30 +02001281 break;
1282 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001283 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001284 channel_abort(rep);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001285 channel_abort(&s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001286 rep->analysers = 0;
1287
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001288 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001289 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001290 if (sess->listener->counters)
1291 sess->listener->counters->denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001292
Willy Tarreaue7dff022015-04-03 01:14:29 +02001293 if (!(s->flags & SF_ERR_MASK))
1294 s->flags |= SF_ERR_PRXCOND;
1295 if (!(s->flags & SF_FINST_MASK))
1296 s->flags |= SF_FINST_D;
Emeric Brun97679e72010-09-23 17:56:44 +02001297 return 0;
1298 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001299 else if (rule->action == ACT_TCP_CLOSE) {
Willy Tarreau73796532014-11-28 14:10:28 +01001300 chn_prod(rep)->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1301 si_shutr(chn_prod(rep));
1302 si_shutw(chn_prod(rep));
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001303 break;
1304 }
Emeric Brun97679e72010-09-23 17:56:44 +02001305 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001306 /* Custom keywords. */
Thierry FOURNIER42148732015-09-02 17:17:33 +02001307 if (!rule->action_ptr)
1308 continue;
Willy Tarreauacc98002015-09-27 23:34:39 +02001309
1310 if (partial & SMP_OPT_FINAL)
1311 act_flags |= ACT_FLAG_FINAL;
1312
1313 switch (rule->action_ptr(rule, s->be, s->sess, s, act_flags)) {
Thierry FOURNIER42148732015-09-02 17:17:33 +02001314 case ACT_RET_ERR:
1315 case ACT_RET_CONT:
1316 continue;
1317 case ACT_RET_STOP:
Thierry FOURNIER561a0f92015-05-29 17:29:14 +02001318 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001319 case ACT_RET_YIELD:
1320 channel_dont_close(rep);
1321 s->current_rule = rule;
1322 return 0;
1323 }
1324 break; /* ACT_RET_STOP */
Emeric Brun97679e72010-09-23 17:56:44 +02001325 }
1326 }
1327 }
1328
1329 /* if we get there, it means we have no rule which matches, or
1330 * we have an explicit accept, so we apply the default accept.
1331 */
1332 rep->analysers &= ~an_bit;
1333 rep->analyse_exp = TICK_ETERNITY;
1334 return 1;
1335}
1336
1337
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001338/* This function performs the TCP layer4 analysis on the current request. It
1339 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1340 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001341 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001342 */
Willy Tarreaue73ef852015-04-04 16:41:45 +02001343int tcp_exec_req_rules(struct session *sess)
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001344{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001345 struct act_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001346 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001347 struct stktable *t = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001348 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001349 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001350 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001351
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001352 if (!conn)
1353 return result;
1354
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001355 list_for_each_entry(rule, &sess->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001356 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001357
1358 if (rule->cond) {
Willy Tarreaue73ef852015-04-04 16:41:45 +02001359 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001360 ret = acl_pass(ret);
1361 if (rule->cond->pol == ACL_COND_UNLESS)
1362 ret = !ret;
1363 }
1364
1365 if (ret) {
1366 /* we have a matching rule. */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001367 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreau27f78242015-07-04 11:36:30 +02001368 break;
1369 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001370 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001371 sess->fe->fe_counters.denied_conn++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001372 if (sess->listener->counters)
1373 sess->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001374
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001375 result = 0;
1376 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001377 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001378 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001379 /* Note: only the first valid tracking parameter of each
1380 * applies.
1381 */
1382 struct stktable_key *key;
1383
Willy Tarreau70f454e2015-04-04 16:38:07 +02001384 if (stkctr_entry(&sess->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001385 continue;
1386
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001387 t = rule->arg.trk_ctr.table.t;
1388 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 +01001389
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001390 if (key && (ts = stktable_get_entry(t, key)))
Willy Tarreau70f454e2015-04-04 16:38:07 +02001391 stream_track_stkctr(&sess->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001392 }
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001393 else if (rule->action == ACT_TCP_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001394 conn->flags |= CO_FL_ACCEPT_PROXY;
1395 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001396 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001397 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001398 /* Custom keywords. */
Willy Tarreau45059e92015-09-28 17:50:10 +02001399 if (!rule->action_ptr)
Thierry FOURNIER42148732015-09-02 17:17:33 +02001400 break;
Willy Tarreauacc98002015-09-27 23:34:39 +02001401 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_FLAG_FINAL | ACT_FLAG_FIRST)) {
Thierry FOURNIER42148732015-09-02 17:17:33 +02001402 case ACT_RET_YIELD:
1403 /* yield is not allowed at this point. If this return code is
1404 * used it is a bug, so I prefer to abort the process.
1405 */
1406 send_log(sess->fe, LOG_WARNING,
1407 "Internal error: yield not allowed with tcp-request connection actions.");
1408 case ACT_RET_STOP:
1409 break;
1410 case ACT_RET_CONT:
1411 continue;
1412 case ACT_RET_ERR:
1413 result = 0;
1414 break;
Thierry FOURNIERc89f4f52015-08-11 09:48:02 +02001415 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02001416 break; /* ACT_RET_STOP */
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001417 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001418 }
1419 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001420 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001421}
1422
Willy Tarreau2d392c22015-08-24 01:43:45 +02001423/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1424static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1425{
1426 struct connection *conn = objt_conn(sess->origin);
1427
1428 if (!conn)
1429 goto out;
1430
1431 if (!conn_ctrl_ready(conn))
1432 goto out;
1433
Willy Tarreau2d392c22015-08-24 01:43:45 +02001434#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001435 /* drain is needed only to send the quick ACK */
1436 conn_sock_drain(conn);
1437
Willy Tarreau2d392c22015-08-24 01:43:45 +02001438 /* re-enable quickack if it was disabled to ack all data and avoid
1439 * retransmits from the client that might trigger a real reset.
1440 */
1441 setsockopt(conn->t.sock.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
1442#endif
1443 /* lingering must absolutely be disabled so that we don't send a
1444 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1445 * is present, returning with ERR will cause lingering to be disabled.
1446 */
1447 if (strm)
1448 strm->si[0].flags |= SI_FL_NOLINGER;
1449
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001450 /* We're on the client-facing side, we must force to disable lingering to
1451 * ensure we will use an RST exclusively and kill any pending data.
1452 */
1453 fdtab[conn->t.sock.fd].linger_risk = 1;
1454
Willy Tarreau2d392c22015-08-24 01:43:45 +02001455#ifdef TCP_REPAIR
1456 if (setsockopt(conn->t.sock.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
1457 /* socket will be quiet now */
1458 goto out;
1459 }
1460#endif
1461 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1462 * Let's fall back on the TTL trick, though it only works for routed
1463 * network and has no effect on local net.
1464 */
1465#ifdef IP_TTL
1466 setsockopt(conn->t.sock.fd, SOL_IP, IP_TTL, &one, sizeof(one));
1467#endif
1468 out:
1469 /* kill the stream if any */
1470 if (strm) {
1471 channel_abort(&strm->req);
1472 channel_abort(&strm->res);
1473 strm->req.analysers = 0;
1474 strm->res.analysers = 0;
1475 strm->be->be_counters.denied_req++;
1476 if (!(strm->flags & SF_ERR_MASK))
1477 strm->flags |= SF_ERR_PRXCOND;
1478 if (!(strm->flags & SF_FINST_MASK))
1479 strm->flags |= SF_FINST_R;
1480 }
1481
1482 sess->fe->fe_counters.denied_req++;
1483 if (sess->listener->counters)
1484 sess->listener->counters->denied_req++;
1485
1486 return ACT_RET_STOP;
1487}
1488
Emeric Brun97679e72010-09-23 17:56:44 +02001489/* Parse a tcp-response rule. Return a negative value in case of failure */
1490static int tcp_parse_response_rule(char **args, int arg, int section_type,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001491 struct proxy *curpx, struct proxy *defpx,
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001492 struct act_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001493 unsigned int where,
1494 const char *file, int line)
Emeric Brun97679e72010-09-23 17:56:44 +02001495{
1496 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001497 memprintf(err, "%s %s is only allowed in 'backend' sections",
1498 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001499 return -1;
1500 }
1501
1502 if (strcmp(args[arg], "accept") == 0) {
1503 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001504 rule->action = ACT_ACTION_ALLOW;
Emeric Brun97679e72010-09-23 17:56:44 +02001505 }
1506 else if (strcmp(args[arg], "reject") == 0) {
1507 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001508 rule->action = ACT_ACTION_DENY;
Emeric Brun97679e72010-09-23 17:56:44 +02001509 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001510 else if (strcmp(args[arg], "close") == 0) {
1511 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001512 rule->action = ACT_TCP_CLOSE;
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001513 }
Emeric Brun97679e72010-09-23 17:56:44 +02001514 else {
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001515 struct action_kw *kw;
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001516 kw = tcp_res_cont_action(args[arg]);
1517 if (kw) {
1518 arg++;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001519 rule->from = ACT_F_TCP_RES_CNT;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02001520 rule->kw = kw;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02001521 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001522 return -1;
1523 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001524 action_build_list(&tcp_res_cont_keywords, &trash);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001525 memprintf(err,
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001526 "'%s %s' expects 'accept', 'close', 'reject', %s in %s '%s' (got '%s')",
1527 args[0], args[1], trash.str, proxy_type_str(curpx), curpx->id, args[arg]);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001528 return -1;
1529 }
Emeric Brun97679e72010-09-23 17:56:44 +02001530 }
1531
1532 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001533 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001534 memprintf(err,
1535 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1536 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001537 return -1;
1538 }
1539 }
1540 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001541 memprintf(err,
1542 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001543 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1544 return -1;
1545 }
1546 return 0;
1547}
1548
1549
1550
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001551/* Parse a tcp-request rule. Return a negative value in case of failure */
1552static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001553 struct proxy *curpx, struct proxy *defpx,
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001554 struct act_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001555 unsigned int where, const char *file, int line)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001556{
1557 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001558 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1559 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001560 return -1;
1561 }
1562
1563 if (!strcmp(args[arg], "accept")) {
1564 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001565 rule->action = ACT_ACTION_ALLOW;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001566 }
1567 else if (!strcmp(args[arg], "reject")) {
1568 arg++;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001569 rule->action = ACT_ACTION_DENY;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001570 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001571 else if (strcmp(args[arg], "capture") == 0) {
1572 struct sample_expr *expr;
1573 struct cap_hdr *hdr;
1574 int kw = arg;
1575 int len = 0;
1576
1577 if (!(curpx->cap & PR_CAP_FE)) {
1578 memprintf(err,
1579 "'%s %s %s' : proxy '%s' has no frontend capability",
1580 args[0], args[1], args[kw], curpx->id);
1581 return -1;
1582 }
1583
1584 if (!(where & SMP_VAL_FE_REQ_CNT)) {
1585 memprintf(err,
1586 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1587 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1588 return -1;
1589 }
1590
1591 arg++;
1592
1593 curpx->conf.args.ctx = ARGC_CAP;
1594 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
1595 if (!expr) {
1596 memprintf(err,
1597 "'%s %s %s' : %s",
1598 args[0], args[1], args[kw], *err);
1599 return -1;
1600 }
1601
1602 if (!(expr->fetch->val & where)) {
1603 memprintf(err,
1604 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
1605 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
1606 free(expr);
1607 return -1;
1608 }
1609
1610 if (strcmp(args[arg], "len") == 0) {
1611 arg++;
1612 if (!args[arg]) {
1613 memprintf(err,
1614 "'%s %s %s' : missing length value",
1615 args[0], args[1], args[kw]);
1616 free(expr);
1617 return -1;
1618 }
1619 /* we copy the table name for now, it will be resolved later */
1620 len = atoi(args[arg]);
1621 if (len <= 0) {
1622 memprintf(err,
1623 "'%s %s %s' : length must be > 0",
1624 args[0], args[1], args[kw]);
1625 free(expr);
1626 return -1;
1627 }
1628 arg++;
1629 }
1630
1631 if (!len) {
1632 memprintf(err,
1633 "'%s %s %s' : a positive 'len' argument is mandatory",
1634 args[0], args[1], args[kw]);
1635 free(expr);
1636 return -1;
1637 }
1638
1639 hdr = calloc(sizeof(struct cap_hdr), 1);
1640 hdr->next = curpx->req_cap;
1641 hdr->name = NULL; /* not a header capture */
1642 hdr->namelen = 0;
1643 hdr->len = len;
1644 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
1645 hdr->index = curpx->nb_req_cap++;
1646
1647 curpx->req_cap = hdr;
1648 curpx->to_log |= LW_REQHDR;
1649
1650 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
1651 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1652
Thierry FOURNIERd0d65ae2015-08-04 08:21:12 +02001653 rule->arg.cap.expr = expr;
1654 rule->arg.cap.hdr = hdr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001655 rule->action = ACT_TCP_CAPTURE;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001656 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001657 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1658 args[arg][9] == '\0' && args[arg][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02001659 args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001660 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001661 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001662
1663 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001664
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001665 curpx->conf.args.ctx = ARGC_TRK;
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001666 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001667 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001668 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001669 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001670 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001671 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001672 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001673
Willy Tarreau80aca902013-01-07 15:42:20 +01001674 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001675 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001676 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001677 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001678 free(expr);
1679 return -1;
1680 }
1681
1682 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001683 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001684
1685 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001686 arg++;
1687 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001688 memprintf(err,
1689 "'%s %s %s' : missing table name",
1690 args[0], args[1], args[kw]);
1691 free(expr);
1692 return -1;
1693 }
1694 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001695 rule->arg.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001696 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001697 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001698 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001699 rule->action = ACT_ACTION_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001700 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001701 else if (strcmp(args[arg], "expect-proxy") == 0) {
1702 if (strcmp(args[arg+1], "layer4") != 0) {
1703 memprintf(err,
1704 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1705 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1706 return -1;
1707 }
1708
1709 if (!(where & SMP_VAL_FE_CON_ACC)) {
1710 memprintf(err,
1711 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1712 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1713 return -1;
1714 }
1715
1716 arg += 2;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001717 rule->action = ACT_TCP_EXPECT_PX;
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001718 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001719 else {
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001720 struct action_kw *kw;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001721 if (where & SMP_VAL_FE_CON_ACC) {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001722 kw = tcp_req_conn_action(args[arg]);
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02001723 rule->kw = kw;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001724 rule->from = ACT_F_TCP_REQ_CON;
1725 } else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001726 kw = tcp_req_cont_action(args[arg]);
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02001727 rule->kw = kw;
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02001728 rule->from = ACT_F_TCP_REQ_CNT;
1729 }
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001730 if (kw) {
1731 arg++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02001732 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001733 return -1;
1734 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001735 if (where & SMP_VAL_FE_CON_ACC)
1736 action_build_list(&tcp_req_conn_keywords, &trash);
1737 else
1738 action_build_list(&tcp_req_cont_keywords, &trash);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001739 memprintf(err,
Thierry FOURNIERab95e652015-10-02 08:24:51 +02001740 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d', %s "
1741 "in %s '%s' (got '%s').\n",
1742 args[0], args[1], MAX_SESS_STKCTR-1, trash.str, proxy_type_str(curpx),
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001743 curpx->id, args[arg]);
1744 return -1;
1745 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001746 }
1747
1748 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001749 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001750 memprintf(err,
1751 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1752 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001753 return -1;
1754 }
1755 }
1756 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001757 memprintf(err,
1758 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001759 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1760 return -1;
1761 }
1762 return 0;
1763}
1764
Emeric Brun97679e72010-09-23 17:56:44 +02001765/* This function should be called to parse a line starting with the "tcp-response"
1766 * keyword.
1767 */
1768static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001769 struct proxy *defpx, const char *file, int line,
1770 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001771{
1772 const char *ptr = NULL;
1773 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001774 int warn = 0;
1775 int arg;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001776 struct act_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001777 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001778 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001779 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001780
1781 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001782 memprintf(err, "missing argument for '%s' in %s '%s'",
1783 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001784 return -1;
1785 }
1786
1787 if (strcmp(args[1], "inspect-delay") == 0) {
1788 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001789 memprintf(err, "%s %s is only allowed in 'backend' sections",
1790 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001791 return -1;
1792 }
1793
1794 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001795 memprintf(err,
1796 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1797 args[0], args[1], proxy_type_str(curpx), curpx->id);
1798 if (ptr)
1799 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001800 return -1;
1801 }
1802
1803 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001804 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1805 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001806 return 1;
1807 }
1808 curpx->tcp_rep.inspect_delay = val;
1809 return 0;
1810 }
1811
Simon Hormande072bd2011-06-24 15:11:37 +09001812 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001813 LIST_INIT(&rule->list);
1814 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001815 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001816
1817 if (strcmp(args[1], "content") == 0) {
1818 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001819
1820 if (curpx->cap & PR_CAP_FE)
1821 where |= SMP_VAL_FE_RES_CNT;
1822 if (curpx->cap & PR_CAP_BE)
1823 where |= SMP_VAL_BE_RES_CNT;
1824
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001825 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001826 goto error;
1827
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001828 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1829 if (acl) {
1830 if (acl->name && *acl->name)
1831 memprintf(err,
1832 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1833 acl->name, args[0], args[1], sample_ckp_names(where));
1834 else
1835 memprintf(err,
1836 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1837 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001838 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001839 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001840
Emeric Brun97679e72010-09-23 17:56:44 +02001841 warn++;
1842 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001843 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1844 if (acl->name && *acl->name)
1845 memprintf(err,
1846 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001847 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001848 else
1849 memprintf(err,
1850 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001851 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001852 warn++;
1853 }
Emeric Brun97679e72010-09-23 17:56:44 +02001854
1855 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1856 }
1857 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001858 memprintf(err,
1859 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1860 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001861 goto error;
1862 }
1863
1864 return warn;
1865 error:
1866 free(rule);
1867 return -1;
1868}
1869
1870
Willy Tarreaub6866442008-07-14 23:54:42 +02001871/* This function should be called to parse a line starting with the "tcp-request"
1872 * keyword.
1873 */
1874static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001875 struct proxy *defpx, const char *file, int line,
1876 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001877{
1878 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001879 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001880 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001881 int arg;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001882 struct act_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001883 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001884 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001885 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001886
1887 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001888 if (curpx == defpx)
1889 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1890 else
1891 memprintf(err, "missing argument for '%s' in %s '%s'",
1892 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001893 return -1;
1894 }
1895
1896 if (!strcmp(args[1], "inspect-delay")) {
1897 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001898 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1899 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001900 return -1;
1901 }
1902
Willy Tarreaub6866442008-07-14 23:54:42 +02001903 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001904 memprintf(err,
1905 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1906 args[0], args[1], proxy_type_str(curpx), curpx->id);
1907 if (ptr)
1908 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001909 return -1;
1910 }
1911
1912 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001913 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1914 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001915 return 1;
1916 }
1917 curpx->tcp_req.inspect_delay = val;
1918 return 0;
1919 }
1920
Simon Hormande072bd2011-06-24 15:11:37 +09001921 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001922 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001923 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001924 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001925
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001926 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001927 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001928
1929 if (curpx->cap & PR_CAP_FE)
1930 where |= SMP_VAL_FE_REQ_CNT;
1931 if (curpx->cap & PR_CAP_BE)
1932 where |= SMP_VAL_BE_REQ_CNT;
1933
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001934 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001935 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001936
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001937 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1938 if (acl) {
1939 if (acl->name && *acl->name)
1940 memprintf(err,
1941 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1942 acl->name, args[0], args[1], sample_ckp_names(where));
1943 else
1944 memprintf(err,
1945 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1946 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001947 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001948 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001949
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001950 warn++;
1951 }
1952 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1953 if (acl->name && *acl->name)
1954 memprintf(err,
1955 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001956 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001957 else
1958 memprintf(err,
1959 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001960 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001961 warn++;
1962 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001963
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001964 /* the following function directly emits the warning */
1965 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001966 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001967 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001968 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001969 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001970
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001971 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001972 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1973 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001974 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001975 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001976
Willy Tarreau80aca902013-01-07 15:42:20 +01001977 where |= SMP_VAL_FE_CON_ACC;
1978
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001979 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001980 goto error;
1981
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001982 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1983 if (acl) {
1984 if (acl->name && *acl->name)
1985 memprintf(err,
1986 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1987 acl->name, args[0], args[1], sample_ckp_names(where));
1988 else
1989 memprintf(err,
1990 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1991 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001992 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001993 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001994
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001995 warn++;
1996 }
1997 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1998 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001999 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002000 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002001 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002002 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002003 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002004 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002005 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02002006 warn++;
2007 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01002008
Willy Tarreau3986b9c2014-09-16 15:39:51 +02002009 /* the following function directly emits the warning */
2010 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02002011 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002012 }
Willy Tarreau1a687942010-05-23 22:40:30 +02002013 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02002014 if (curpx == defpx)
2015 memprintf(err,
2016 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
2017 args[0], args[1]);
2018 else
2019 memprintf(err,
2020 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02002021 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002022 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02002023 }
2024
Willy Tarreau1a687942010-05-23 22:40:30 +02002025 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02002026 error:
2027 free(rule);
2028 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02002029}
2030
Willy Tarreau2d392c22015-08-24 01:43:45 +02002031/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
2032 * success, ACT_RET_PRS_ERR on error.
2033 */
2034static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
2035 struct act_rule *rule, char **err)
2036{
2037 rule->action = ACT_CUSTOM;
2038 rule->action_ptr = tcp_exec_action_silent_drop;
2039 return ACT_RET_PRS_OK;
2040}
2041
Willy Tarreau645513a2010-05-24 20:55:15 +02002042
2043/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002044/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02002045/************************************************************************/
2046
Willy Tarreau4a129812012-04-25 17:31:42 +02002047/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02002048int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002049{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002050 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002051
2052 if (!cli_conn)
2053 return 0;
2054
2055 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01002056 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002057 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002058 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002059 break;
2060 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002061 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002062 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002063 break;
2064 default:
Emeric Brunf769f512010-10-22 17:14:01 +02002065 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002066 }
Emeric Brunf769f512010-10-22 17:14:01 +02002067
Willy Tarreau37406352012-04-23 16:16:37 +02002068 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002069 return 1;
2070}
2071
Willy Tarreaua5e37562011-12-16 17:06:15 +01002072/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002073static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002074smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002075{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002076 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002077
2078 if (!cli_conn)
2079 return 0;
2080
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002081 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002082 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02002083 return 0;
2084
Willy Tarreau37406352012-04-23 16:16:37 +02002085 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002086 return 1;
2087}
2088
Willy Tarreau4a129812012-04-25 17:31:42 +02002089/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02002090static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002091smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002092{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002093 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02002094
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002095 if (!cli_conn)
2096 return 0;
2097
2098 conn_get_to_addr(cli_conn);
2099
2100 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01002101 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002102 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002103 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002104 break;
2105 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002106 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002107 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002108 break;
2109 default:
Emeric Brunf769f512010-10-22 17:14:01 +02002110 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002111 }
Emeric Brunf769f512010-10-22 17:14:01 +02002112
Willy Tarreau37406352012-04-23 16:16:37 +02002113 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002114 return 1;
2115}
2116
Willy Tarreaua5e37562011-12-16 17:06:15 +01002117/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002118static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002119smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002120{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002121 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002122
2123 if (!cli_conn)
2124 return 0;
2125
2126 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02002127
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002128 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002129 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02002130 return 0;
2131
Willy Tarreau37406352012-04-23 16:16:37 +02002132 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002133 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01002134}
2135
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002136#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002137/* parse the "v4v6" bind keyword */
2138static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2139{
2140 struct listener *l;
2141
2142 list_for_each_entry(l, &conf->listeners, by_bind) {
2143 if (l->addr.ss_family == AF_INET6)
2144 l->options |= LI_O_V4V6;
2145 }
2146
2147 return 0;
2148}
2149
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002150/* parse the "v6only" bind keyword */
2151static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2152{
2153 struct listener *l;
2154
2155 list_for_each_entry(l, &conf->listeners, by_bind) {
2156 if (l->addr.ss_family == AF_INET6)
2157 l->options |= LI_O_V6ONLY;
2158 }
2159
2160 return 0;
2161}
2162#endif
2163
Pieter Baauwd551fb52013-05-08 22:49:23 +02002164#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002165/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002166static 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 +02002167{
2168 struct listener *l;
2169
Willy Tarreau4348fad2012-09-20 16:48:07 +02002170 list_for_each_entry(l, &conf->listeners, by_bind) {
2171 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2172 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02002173 }
2174
Willy Tarreau44791242012-09-12 23:27:21 +02002175 return 0;
2176}
2177#endif
2178
2179#ifdef TCP_DEFER_ACCEPT
2180/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002181static 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 +02002182{
2183 struct listener *l;
2184
Willy Tarreau4348fad2012-09-20 16:48:07 +02002185 list_for_each_entry(l, &conf->listeners, by_bind) {
2186 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2187 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02002188 }
2189
Willy Tarreau44791242012-09-12 23:27:21 +02002190 return 0;
2191}
2192#endif
2193
Willy Tarreau1c862c52012-10-05 16:21:00 +02002194#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01002195/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02002196static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2197{
2198 struct listener *l;
2199
2200 list_for_each_entry(l, &conf->listeners, by_bind) {
2201 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2202 l->options |= LI_O_TCP_FO;
2203 }
2204
2205 return 0;
2206}
2207#endif
2208
Willy Tarreau44791242012-09-12 23:27:21 +02002209#ifdef TCP_MAXSEG
2210/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002211static 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 +02002212{
2213 struct listener *l;
2214 int mss;
2215
Willy Tarreau44791242012-09-12 23:27:21 +02002216 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002217 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002218 return ERR_ALERT | ERR_FATAL;
2219 }
2220
2221 mss = atoi(args[cur_arg + 1]);
2222 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002223 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002224 return ERR_ALERT | ERR_FATAL;
2225 }
2226
Willy Tarreau4348fad2012-09-20 16:48:07 +02002227 list_for_each_entry(l, &conf->listeners, by_bind) {
2228 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2229 l->maxseg = mss;
2230 }
Willy Tarreau44791242012-09-12 23:27:21 +02002231
2232 return 0;
2233}
2234#endif
2235
Willy Tarreau2af207a2015-02-04 00:45:58 +01002236#ifdef TCP_USER_TIMEOUT
2237/* parse the "tcp-ut" bind keyword */
2238static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2239{
2240 const char *ptr = NULL;
2241 struct listener *l;
2242 unsigned int timeout;
2243
2244 if (!*args[cur_arg + 1]) {
2245 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
2246 return ERR_ALERT | ERR_FATAL;
2247 }
2248
2249 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
2250 if (ptr) {
2251 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
2252 return ERR_ALERT | ERR_FATAL;
2253 }
2254
2255 list_for_each_entry(l, &conf->listeners, by_bind) {
2256 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2257 l->tcp_ut = timeout;
2258 }
2259
2260 return 0;
2261}
2262#endif
2263
Willy Tarreau44791242012-09-12 23:27:21 +02002264#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01002265/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002266static 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 +02002267{
2268 struct listener *l;
2269
Willy Tarreau44791242012-09-12 23:27:21 +02002270 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002271 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002272 return ERR_ALERT | ERR_FATAL;
2273 }
2274
Willy Tarreau4348fad2012-09-20 16:48:07 +02002275 list_for_each_entry(l, &conf->listeners, by_bind) {
2276 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2277 l->interface = strdup(args[cur_arg + 1]);
2278 }
Willy Tarreau44791242012-09-12 23:27:21 +02002279
2280 global.last_checks |= LSTCHK_NETADM;
2281 return 0;
2282}
2283#endif
2284
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002285#ifdef CONFIG_HAP_NS
2286/* parse the "namespace" bind keyword */
2287static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2288{
2289 struct listener *l;
2290 char *namespace = NULL;
2291
2292 if (!*args[cur_arg + 1]) {
2293 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
2294 return ERR_ALERT | ERR_FATAL;
2295 }
2296 namespace = args[cur_arg + 1];
2297
2298 list_for_each_entry(l, &conf->listeners, by_bind) {
2299 l->netns = netns_store_lookup(namespace, strlen(namespace));
2300
2301 if (l->netns == NULL)
2302 l->netns = netns_store_insert(namespace);
2303
2304 if (l->netns == NULL) {
2305 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
2306 return ERR_ALERT | ERR_FATAL;
2307 }
2308 }
2309 return 0;
2310}
2311#endif
2312
Willy Tarreaudc13c112013-06-21 23:16:39 +02002313static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002314 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02002315 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02002316 { 0, NULL, NULL },
2317}};
2318
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002319
Willy Tarreau61612d42012-04-19 18:42:05 +02002320/* Note: must not be declared <const> as its list will be overwritten.
2321 * Please take care of keeping this list alphabetically sorted.
2322 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002323static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002324 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02002325}};
2326
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002327
Willy Tarreau4a129812012-04-25 17:31:42 +02002328/* Note: must not be declared <const> as its list will be overwritten.
2329 * Note: fetches that may return multiple types must be declared as the lowest
2330 * common denominator, the type that can be casted into all other ones. For
2331 * instance v4/v6 must be declared v4.
2332 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002333static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002334 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002335 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002336 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002337 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002338 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002339}};
2340
Willy Tarreau44791242012-09-12 23:27:21 +02002341/************************************************************************/
2342/* All supported bind keywords must be declared here. */
2343/************************************************************************/
2344
2345/* Note: must not be declared <const> as its list will be overwritten.
2346 * Please take care of keeping this list alphabetically sorted, doing so helps
2347 * all code contributors.
2348 * Optional keywords are also declared with a NULL ->parse() function so that
2349 * the config parser can report an appropriate error when a known keyword was
2350 * not enabled.
2351 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002352static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02002353#ifdef TCP_DEFER_ACCEPT
2354 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
2355#endif
2356#ifdef SO_BINDTODEVICE
2357 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
2358#endif
2359#ifdef TCP_MAXSEG
2360 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
2361#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01002362#ifdef TCP_USER_TIMEOUT
2363 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
2364#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02002365#ifdef TCP_FASTOPEN
2366 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
2367#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02002368#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002369 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
2370#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002371#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002372 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002373 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
2374#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002375#ifdef CONFIG_HAP_NS
2376 { "namespace", bind_parse_namespace, 1 },
2377#endif
Willy Tarreau44791242012-09-12 23:27:21 +02002378 /* the versions with the NULL parse function*/
2379 { "defer-accept", NULL, 0 },
2380 { "interface", NULL, 1 },
2381 { "mss", NULL, 1 },
2382 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002383 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002384 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002385 { NULL, NULL, 0 },
2386}};
2387
Willy Tarreau2d392c22015-08-24 01:43:45 +02002388
2389static struct action_kw_list tcp_req_conn_actions = {ILH, {
2390 { "silent-drop", tcp_parse_silent_drop },
2391 { /* END */ }
2392}};
2393
2394static struct action_kw_list tcp_req_cont_actions = {ILH, {
2395 { "silent-drop", tcp_parse_silent_drop },
2396 { /* END */ }
2397}};
2398
2399static struct action_kw_list tcp_res_cont_actions = {ILH, {
2400 { "silent-drop", tcp_parse_silent_drop },
2401 { /* END */ }
2402}};
2403
2404static struct action_kw_list http_req_actions = {ILH, {
2405 { "silent-drop", tcp_parse_silent_drop },
2406 { /* END */ }
2407}};
2408
2409static struct action_kw_list http_res_actions = {ILH, {
2410 { "silent-drop", tcp_parse_silent_drop },
2411 { /* END */ }
2412}};
2413
2414
Willy Tarreaue6b98942007-10-29 01:09:36 +01002415__attribute__((constructor))
2416static void __tcp_protocol_init(void)
2417{
2418 protocol_register(&proto_tcpv4);
2419 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002420 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02002421 cfg_register_keywords(&cfg_kws);
2422 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02002423 bind_register_keywords(&bind_kws);
Willy Tarreau2d392c22015-08-24 01:43:45 +02002424 tcp_req_conn_keywords_register(&tcp_req_conn_actions);
2425 tcp_req_cont_keywords_register(&tcp_req_cont_actions);
2426 tcp_res_cont_keywords_register(&tcp_res_cont_actions);
2427 http_req_keywords_register(&http_req_actions);
2428 http_res_keywords_register(&http_res_actions);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002429}
2430
2431
2432/*
2433 * Local variables:
2434 * c-indent-level: 8
2435 * c-basic-offset: 8
2436 * End:
2437 */