blob: 6a67785a47196d25242f9334930e67c07f7ab866 [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>
28
Willy Tarreaub6866442008-07-14 23:54:42 +020029#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <common/mini-clist.h>
35#include <common/standard.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010036#include <common/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010037
Willy Tarreaue6b98942007-10-29 01:09:36 +010038#include <types/global.h>
Willy Tarreau18bf01e2014-06-13 16:18:52 +020039#include <types/capture.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020040#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010041
42#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020043#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020044#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020045#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020046#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020047#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020048#include <proto/log.h>
49#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/protocol.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010051#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020052#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020053#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020054#include <proto/stream.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020055#include <proto/stick_table.h>
Willy Tarreaucc1e04b2013-09-11 23:20:29 +020056#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020057#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010058
Willy Tarreaue8c66af2008-01-13 18:40:14 +010059#ifdef CONFIG_HAP_CTTPROXY
60#include <import/ip_tproxy.h>
61#endif
62
Emeric Bruncf20bf12010-10-22 16:06:11 +020063static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
64static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010065
Thierry FOURNIERcc87a112014-12-12 19:41:33 +010066/* List head of all known action keywords for "tcp-request connection" */
67struct list tcp_req_conn_keywords = LIST_HEAD_INIT(tcp_req_conn_keywords);
68struct list tcp_req_cont_keywords = LIST_HEAD_INIT(tcp_req_cont_keywords);
69struct list tcp_res_cont_keywords = LIST_HEAD_INIT(tcp_res_cont_keywords);
70
Willy Tarreaue6b98942007-10-29 01:09:36 +010071/* Note: must not be declared <const> as its list will be overwritten */
72static struct protocol proto_tcpv4 = {
73 .name = "tcpv4",
74 .sock_domain = AF_INET,
75 .sock_type = SOCK_STREAM,
76 .sock_prot = IPPROTO_TCP,
77 .sock_family = AF_INET,
78 .sock_addrlen = sizeof(struct sockaddr_in),
79 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020080 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020081 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020082 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010083 .bind_all = tcp_bind_listeners,
84 .unbind_all = unbind_all_listeners,
85 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020086 .get_src = tcp_get_src,
87 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020088 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +020089 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010090 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
91 .nb_listeners = 0,
92};
93
94/* Note: must not be declared <const> as its list will be overwritten */
95static struct protocol proto_tcpv6 = {
96 .name = "tcpv6",
97 .sock_domain = AF_INET6,
98 .sock_type = SOCK_STREAM,
99 .sock_prot = IPPROTO_TCP,
100 .sock_family = AF_INET6,
101 .sock_addrlen = sizeof(struct sockaddr_in6),
102 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +0200103 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200104 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200105 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100106 .bind_all = tcp_bind_listeners,
107 .unbind_all = unbind_all_listeners,
108 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200109 .get_src = tcp_get_src,
110 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200111 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +0200112 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100113 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
114 .nb_listeners = 0,
115};
116
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100117/*
118 * Register keywords.
119 */
120void tcp_req_conn_keywords_register(struct tcp_action_kw_list *kw_list)
121{
122 LIST_ADDQ(&tcp_req_conn_keywords, &kw_list->list);
123}
124
125void tcp_req_cont_keywords_register(struct tcp_action_kw_list *kw_list)
126{
127 LIST_ADDQ(&tcp_req_cont_keywords, &kw_list->list);
128}
129
130void tcp_res_cont_keywords_register(struct tcp_action_kw_list *kw_list)
131{
132 LIST_ADDQ(&tcp_res_cont_keywords, &kw_list->list);
133}
134
135/*
136 * Return the struct http_req_action_kw associated to a keyword.
137 */
138static struct tcp_action_kw *tcp_req_conn_action(const char *kw)
139{
140 struct tcp_action_kw_list *kw_list;
141 int i;
142
143 if (LIST_ISEMPTY(&tcp_req_conn_keywords))
144 return NULL;
145
146 list_for_each_entry(kw_list, &tcp_req_conn_keywords, list) {
147 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
148 if (!strcmp(kw, kw_list->kw[i].kw))
149 return &kw_list->kw[i];
150 }
151 }
152 return NULL;
153}
154
155static struct tcp_action_kw *tcp_req_cont_action(const char *kw)
156{
157 struct tcp_action_kw_list *kw_list;
158 int i;
159
160 if (LIST_ISEMPTY(&tcp_req_cont_keywords))
161 return NULL;
162
163 list_for_each_entry(kw_list, &tcp_req_cont_keywords, list) {
164 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
165 if (!strcmp(kw, kw_list->kw[i].kw))
166 return &kw_list->kw[i];
167 }
168 }
169 return NULL;
170}
171
172static struct tcp_action_kw *tcp_res_cont_action(const char *kw)
173{
174 struct tcp_action_kw_list *kw_list;
175 int i;
176
177 if (LIST_ISEMPTY(&tcp_res_cont_keywords))
178 return NULL;
179
180 list_for_each_entry(kw_list, &tcp_res_cont_keywords, list) {
181 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
182 if (!strcmp(kw, kw_list->kw[i].kw))
183 return &kw_list->kw[i];
184 }
185 }
186 return NULL;
187}
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100188
David du Colombier6f5ccb12011-03-10 22:26:24 +0100189/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100190 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
191 * - 0 : ignore remote address (may even be a NULL pointer)
192 * - 1 : use provided address
193 * - 2 : use provided port
194 * - 3 : use both
195 *
196 * The function supports multiple foreign binding methods :
197 * - linux_tproxy: we directly bind to the foreign address
198 * - cttproxy: we bind to a local address then nat.
199 * The second one can be used as a fallback for the first one.
200 * This function returns 0 when everything's OK, 1 if it could not bind, to the
201 * local address, 2 if it could not bind to the foreign address.
202 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100203int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100204{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100205 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100206 int foreign_ok = 0;
207 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100208 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200209 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200210
David du Colombier65c17962012-07-13 14:34:59 +0200211 switch (local->ss_family) {
212 case AF_INET:
213 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200214 /* This deserves some explanation. Some platforms will support
215 * multiple combinations of certain methods, so we try the
216 * supported ones until one succeeds.
217 */
218 if (0
219#if defined(IP_TRANSPARENT)
220 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
221#endif
222#if defined(IP_FREEBIND)
223 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
224#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200225#if defined(IP_BINDANY)
226 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
227#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200228#if defined(SO_BINDANY)
229 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
230#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200231 )
David du Colombier65c17962012-07-13 14:34:59 +0200232 foreign_ok = 1;
233 else
234 ip_transp_working = 0;
235 }
236 break;
237 case AF_INET6:
238 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200239 if (0
240#if defined(IPV6_TRANSPARENT)
241 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
242#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100243#if defined(IP_FREEBIND)
244 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
245#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200246#if defined(IPV6_BINDANY)
247 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
248#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200249#if defined(SO_BINDANY)
250 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
251#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200252 )
David du Colombier65c17962012-07-13 14:34:59 +0200253 foreign_ok = 1;
254 else
255 ip6_transp_working = 0;
256 }
257 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100258 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200259
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100260 if (flags) {
261 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200262 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100263 switch (remote->ss_family) {
264 case AF_INET:
265 if (flags & 1)
266 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
267 if (flags & 2)
268 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
269 break;
270 case AF_INET6:
271 if (flags & 1)
272 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
273 if (flags & 2)
274 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
275 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100276 default:
277 /* we don't want to try to bind to an unknown address family */
278 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100279 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100280 }
281
Simon Hormande072bd2011-06-24 15:11:37 +0900282 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100283 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200284 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200285 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
286 if (ret < 0)
287 return 2;
288 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100289 }
290 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200291 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200292 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
293 if (ret < 0)
294 return 1;
295 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100296 }
297
298 if (!flags)
299 return 0;
300
301#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100302 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100303 struct in_tproxy itp1, itp2;
304 memset(&itp1, 0, sizeof(itp1));
305
306 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100307 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
308 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100309
310 /* set connect flag on socket */
311 itp2.op = TPROXY_FLAGS;
312 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
313
314 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
315 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
316 foreign_ok = 1;
317 }
318 }
319#endif
320 if (!foreign_ok)
321 /* we could not bind to a foreign address */
322 return 2;
323
324 return 0;
325}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100326
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100327static int create_server_socket(struct connection *conn)
328{
Willy Tarreau529c1392014-12-24 13:47:55 +0100329 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100330
Willy Tarreau529c1392014-12-24 13:47:55 +0100331#ifdef CONFIG_HAP_NS
332 if (objt_server(conn->target)) {
333 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
334 ns = conn->proxy_netns;
335 else
336 ns = __objt_server(conn->target)->netns;
337 }
338#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100339 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
340}
Willy Tarreau9650f372009-08-16 14:02:45 +0200341
342/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200343 * This function initiates a TCP connection establishment to the target assigned
344 * to connection <conn> using (si->{target,addr.to}). A source address may be
345 * pointed to by conn->addr.from in case of transparent proxying. Normal source
346 * bind addresses are still determined locally (due to the possible need of a
347 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100348 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100349 * supported. The <data> parameter is a boolean indicating whether there are data
350 * waiting for being sent or not, in order to adjust data write polling and on
351 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
352 * allows the caller to force using a delayed ACK when establishing the connection :
353 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
354 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
355 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200356 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200357 * Note that a pending send_proxy message accounts for data.
358 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200359 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200360 * - SF_ERR_NONE if everything's OK
361 * - SF_ERR_SRVTO if there are no more servers
362 * - SF_ERR_SRVCL if the connection was refused by the server
363 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
364 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
365 * - SF_ERR_INTERNAL for any other purely internal errors
366 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100367 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200368 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100369 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200370 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100371
Willy Tarreauf0837b22012-11-24 10:24:27 +0100372int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200373{
374 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100375 struct server *srv;
376 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100377 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100378
Willy Tarreau9ce70132014-01-24 16:08:19 +0100379 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
380
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100381 switch (obj_type(conn->target)) {
382 case OBJ_TYPE_PROXY:
383 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100384 srv = NULL;
385 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100386 case OBJ_TYPE_SERVER:
387 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100388 be = srv->proxy;
389 break;
390 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100391 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200392 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100393 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200394
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100395 fd = conn->t.sock.fd = create_server_socket(conn);
396
397 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200398 qfprintf(stderr, "Cannot get a server socket.\n");
399
Willy Tarreau9ce70132014-01-24 16:08:19 +0100400 if (errno == ENFILE) {
401 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200402 send_log(be, LOG_EMERG,
403 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
404 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100405 }
406 else if (errno == EMFILE) {
407 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200408 send_log(be, LOG_EMERG,
409 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
410 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100411 }
412 else if (errno == ENOBUFS || errno == ENOMEM) {
413 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200414 send_log(be, LOG_EMERG,
415 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
416 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100417 }
418 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
419 conn->err_code = CO_ER_NOPROTO;
420 }
421 else
422 conn->err_code = CO_ER_SOCK_ERR;
423
Willy Tarreau9650f372009-08-16 14:02:45 +0200424 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100425 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200426 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200427 }
428
429 if (fd >= global.maxsock) {
430 /* do not log anything there, it's a normal condition when this option
431 * is used to serialize connections to a server !
432 */
433 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
434 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100435 conn->err_code = CO_ER_CONF_FDLIM;
436 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200437 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 }
439
440 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900441 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
443 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100444 conn->err_code = CO_ER_SOCK_ERR;
445 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200446 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200447 }
448
449 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900450 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200451
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 /* allow specific binding :
453 * - server-specific at first
454 * - proxy-specific next
455 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100456 if (srv && srv->conn_src.opts & CO_SRC_BIND)
457 src = &srv->conn_src;
458 else if (be->conn_src.opts & CO_SRC_BIND)
459 src = &be->conn_src;
460 else
461 src = NULL;
462
463 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200464 int ret, flags = 0;
465
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200466 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100467 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100468 case CO_SRC_TPROXY_ADDR:
469 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200470 flags = 3;
471 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100472 case CO_SRC_TPROXY_CIP:
473 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200474 flags = 1;
475 break;
476 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200477 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200478
Willy Tarreau9650f372009-08-16 14:02:45 +0200479#ifdef SO_BINDTODEVICE
480 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100481 if (src->iface_name)
482 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200483#endif
484
Willy Tarreaua4380b42012-12-08 22:49:11 +0100485 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200486 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100487 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200488
489 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100490 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200491
492 do {
493 /* note: in case of retry, we may have to release a previously
494 * allocated port, hence this loop's construct.
495 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200496 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
497 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200498
499 if (!attempts)
500 break;
501 attempts--;
502
Willy Tarreaua4380b42012-12-08 22:49:11 +0100503 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100504 if (!fdinfo[fd].local_port) {
505 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200506 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100507 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200508
Willy Tarreaua4380b42012-12-08 22:49:11 +0100509 fdinfo[fd].port_range = src->sport_range;
510 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200511
Willy Tarreaua4380b42012-12-08 22:49:11 +0100512 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100513 if (ret != 0)
514 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200515 } while (ret != 0); /* binding NOK */
516 }
517 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100518 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100519 if (ret != 0)
520 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200521 }
522
Willy Tarreaua4380b42012-12-08 22:49:11 +0100523 if (unlikely(ret != 0)) {
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);
527
Willy Tarreau9650f372009-08-16 14:02:45 +0200528 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100529 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200530 be->id);
531 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100532 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200533 be->id);
534 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100535 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200536 be->id);
537 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100538 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200539 be->id);
540 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100541 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200542 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200543 }
544 }
545
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400546#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200547 /* disabling tcp quick ack now allows the first request to leave the
548 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100549 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200550 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200551 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900552 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200553#endif
554
Willy Tarreaue803de22010-01-21 17:43:04 +0100555 if (global.tune.server_sndbuf)
556 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
557
558 if (global.tune.server_rcvbuf)
559 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
560
Willy Tarreau986a9d22012-08-30 21:11:38 +0200561 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200562 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
563
Willy Tarreaub1719512012-12-08 23:03:28 +0100564 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200565 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100566 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200567 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100568 conn->err_code = CO_ER_FREE_PORTS;
569 }
570 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200571 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100572 conn->err_code = CO_ER_ADDR_INUSE;
573 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200574
Willy Tarreaub1719512012-12-08 23:03:28 +0100575 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200576 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
577 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200578 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100579 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100580 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200581 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200582 } else if (errno == ETIMEDOUT) {
583 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200584 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
585 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200586 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100587 conn->err_code = CO_ER_SOCK_ERR;
588 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200589 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200590 } else {
591 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
592 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200593 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
594 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200595 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100596 conn->err_code = CO_ER_SOCK_ERR;
597 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200598 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200599 }
600 }
601
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100602 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200603
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200604 /* Prepare to send a few handshakes related to the on-wire protocol. */
605 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200606 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200607
Willy Tarreauf79c8172013-10-21 16:30:56 +0200608 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100609 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200610 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200611
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200612 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200613 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100614 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200615 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200616 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200617
Willy Tarreau14f8e862012-08-30 22:23:13 +0200618 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200619 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200620
Willy Tarreaue7dff022015-04-03 01:14:29 +0200621 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200622}
623
624
Willy Tarreau59b94792012-05-11 16:16:40 +0200625/*
626 * Retrieves the source address for the socket <fd>, with <dir> indicating
627 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
628 * success, -1 in case of error. The socket's source address is stored in
629 * <sa> for <salen> bytes.
630 */
631int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
632{
633 if (dir)
634 return getsockname(fd, sa, &salen);
635 else
636 return getpeername(fd, sa, &salen);
637}
638
639
640/*
641 * Retrieves the original destination address for the socket <fd>, with <dir>
642 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
643 * listener, if the original destination address was translated, the original
644 * address is retrieved. It returns 0 in case of success, -1 in case of error.
645 * The socket's source address is stored in <sa> for <salen> bytes.
646 */
647int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
648{
649 if (dir)
650 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100651 else {
652 int ret = getsockname(fd, sa, &salen);
653
654 if (ret < 0)
655 return ret;
656
Willy Tarreau59b94792012-05-11 16:16:40 +0200657#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100658 /* For TPROXY and Netfilter's NAT, we can retrieve the original
659 * IPv4 address before DNAT/REDIRECT. We must not do that with
660 * other families because v6-mapped IPv4 addresses are still
661 * reported as v4.
662 */
663 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
664 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
665 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200666#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100667 return ret;
668 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200669}
670
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200671/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100672 * receive shutdown. Returns positive if the shutdown was found, negative
673 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
674 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200675 */
676int tcp_drain(int fd)
677{
678 int turns = 2;
679 int len;
680
681 while (turns) {
682#ifdef MSG_TRUNC_CLEARS_INPUT
683 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
684 if (len == -1 && errno == EFAULT)
685#endif
686 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
687
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100688 if (len == 0) {
689 /* cool, shutdown received */
690 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200691 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100692 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200693
694 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100695 if (errno == EAGAIN) {
696 /* connection not closed yet */
697 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100698 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100699 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200700 if (errno == EINTR) /* oops, try again */
701 continue;
702 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100703 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200704 return 1;
705 }
706 /* OK we read some data, let's try again once */
707 turns--;
708 }
709 /* some data are still present, give up */
710 return 0;
711}
712
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200713/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100714 * and we have nothing to send. It updates the FD polling status. It returns 0
715 * if it fails in a fatal way or needs to poll to go further, otherwise it
716 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
717 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
718 * errno. The error checking is done in two passes in order to limit the number
719 * of syscalls in the normal case :
720 * - if POLL_ERR was reported by the poller, we check for a pending error on
721 * the socket before proceeding. If found, it's assigned to errno so that
722 * upper layers can see it.
723 * - otherwise connect() is used to check the connection state again, since
724 * the getsockopt return cannot reliably be used to know if the connection
725 * is still pending or ready. This one may often return an error as well,
726 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200727 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200728int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200729{
Willy Tarreau239d7182012-07-23 18:53:03 +0200730 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100731 socklen_t lskerr;
732 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200733
Willy Tarreau80184712012-07-06 14:54:49 +0200734 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200735 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200736
Willy Tarreau3c728722014-01-23 13:50:42 +0100737 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200738 return 0;
739
Willy Tarreau80184712012-07-06 14:54:49 +0200740 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200741 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200742
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100743 if (!fd_send_ready(fd))
744 return 0;
745
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100746 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
747 * without FD_POLL_IN also indicates a hangup without input data meaning
748 * there was no connection.
749 */
750 if (fdtab[fd].ev & FD_POLL_ERR ||
751 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
752 skerr = 0;
753 lskerr = sizeof(skerr);
754 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
755 errno = skerr;
756 if (errno == EAGAIN)
757 errno = 0;
758 if (errno)
759 goto out_error;
760 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200761
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100762 /* Use connect() to check the state of the socket. This has the
763 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200764 * - error
765 * - connecting (EALREADY, EINPROGRESS)
766 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200767 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200768 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200769 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100770 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100771 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200772 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200773 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200774
Willy Tarreau2c6be842012-07-06 17:12:34 +0200775 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200776 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200777
Willy Tarreau2c6be842012-07-06 17:12:34 +0200778 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200779 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200780
Willy Tarreau076be252012-07-06 16:02:29 +0200781 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200782 * forward the event to the transport layer which will notify the
783 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200784 */
Willy Tarreau80184712012-07-06 14:54:49 +0200785 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200786 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200787
788 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200789 /* Write error on the file descriptor. Report it to the connection
790 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200791 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100792 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100793 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100794 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200795 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200796}
797
Willy Tarreau59b94792012-05-11 16:16:40 +0200798
Willy Tarreaue6b98942007-10-29 01:09:36 +0100799/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100800 * an error message in <errmsg> if the message is at most <errlen> bytes long
801 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
802 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100803 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
804 * was alright and that no message was returned. ERR_RETRYABLE means that an
805 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700806 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100807 * the meaning of the error, but just indicate that a message is present which
808 * should be displayed with the respective level. Last, ERR_ABORT indicates
809 * that it's pointless to try to start other listeners. No error message is
810 * returned if errlen is NULL.
811 */
812int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
813{
814 __label__ tcp_return, tcp_close_return;
815 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100816 int ext, ready;
817 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100818 const char *msg = NULL;
819
820 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100821 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100822 *errmsg = 0;
823
824 if (listener->state != LI_ASSIGNED)
825 return ERR_NONE; /* already bound */
826
827 err = ERR_NONE;
828
Willy Tarreau40aa0702013-03-10 23:51:38 +0100829 /* if the listener already has an fd assigned, then we were offered the
830 * fd by an external process (most likely the parent), and we don't want
831 * to create a new socket. However we still want to set a few flags on
832 * the socket.
833 */
834 fd = listener->fd;
835 ext = (fd >= 0);
836
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100837 if (!ext) {
838 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
839
840 if (fd == -1) {
841 err |= ERR_RETRYABLE | ERR_ALERT;
842 msg = "cannot create listening socket";
843 goto tcp_return;
844 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100845 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100846
Willy Tarreaue6b98942007-10-29 01:09:36 +0100847 if (fd >= global.maxsock) {
848 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
849 msg = "not enough free sockets (raise '-n' parameter)";
850 goto tcp_close_return;
851 }
852
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200853 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100854 err |= ERR_FATAL | ERR_ALERT;
855 msg = "cannot make socket non-blocking";
856 goto tcp_close_return;
857 }
858
Willy Tarreau40aa0702013-03-10 23:51:38 +0100859 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100860 /* not fatal but should be reported */
861 msg = "cannot do so_reuseaddr";
862 err |= ERR_ALERT;
863 }
864
865 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900866 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100867
Willy Tarreaue6b98942007-10-29 01:09:36 +0100868#ifdef SO_REUSEPORT
869 /* OpenBSD supports this. As it's present in old libc versions of Linux,
870 * it might return an error that we will silently ignore.
871 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100872 if (!ext)
873 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100874#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200875
Willy Tarreau40aa0702013-03-10 23:51:38 +0100876 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200877 switch (listener->addr.ss_family) {
878 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200879 if (1
880#if defined(IP_TRANSPARENT)
881 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
882#endif
883#if defined(IP_FREEBIND)
884 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
885#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200886#if defined(IP_BINDANY)
887 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
888#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200889#if defined(SO_BINDANY)
890 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
891#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200892 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200893 msg = "cannot make listening socket transparent";
894 err |= ERR_ALERT;
895 }
896 break;
897 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200898 if (1
899#if defined(IPV6_TRANSPARENT)
900 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
901#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100902#if defined(IP_FREEBIND)
903 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
904#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200905#if defined(IPV6_BINDANY)
906 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
907#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200908#if defined(SO_BINDANY)
909 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
910#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200911 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200912 msg = "cannot make listening socket transparent";
913 err |= ERR_ALERT;
914 }
915 break;
916 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100917 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200918
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100919#ifdef SO_BINDTODEVICE
920 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100921 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100922 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100923 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100924 msg = "cannot bind listener to device";
925 err |= ERR_WARN;
926 }
927 }
928#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400929#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100930 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400931 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200932 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
933 msg = "cannot set MSS";
934 err |= ERR_WARN;
935 }
936 }
937#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100938#if defined(TCP_USER_TIMEOUT)
939 if (listener->tcp_ut) {
940 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
941 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
942 msg = "cannot set TCP User Timeout";
943 err |= ERR_WARN;
944 }
945 }
946#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200947#if defined(TCP_DEFER_ACCEPT)
948 if (listener->options & LI_O_DEF_ACCEPT) {
949 /* defer accept by up to one second */
950 int accept_delay = 1;
951 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
952 msg = "cannot enable DEFER_ACCEPT";
953 err |= ERR_WARN;
954 }
955 }
956#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200957#if defined(TCP_FASTOPEN)
958 if (listener->options & LI_O_TCP_FO) {
959 /* TFO needs a queue length, let's use the configured backlog */
960 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
961 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
962 msg = "cannot enable TCP_FASTOPEN";
963 err |= ERR_WARN;
964 }
965 }
966#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100967#if defined(IPV6_V6ONLY)
968 if (listener->options & LI_O_V6ONLY)
969 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100970 else if (listener->options & LI_O_V4V6)
971 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100972#endif
973
Willy Tarreau40aa0702013-03-10 23:51:38 +0100974 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100975 err |= ERR_RETRYABLE | ERR_ALERT;
976 msg = "cannot bind socket";
977 goto tcp_close_return;
978 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100979
Willy Tarreau40aa0702013-03-10 23:51:38 +0100980 ready = 0;
981 ready_len = sizeof(ready);
982 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
983 ready = 0;
984
985 if (!(ext && ready) && /* only listen if not already done by external process */
986 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100987 err |= ERR_RETRYABLE | ERR_ALERT;
988 msg = "cannot listen to socket";
989 goto tcp_close_return;
990 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100991
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400992#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200993 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900994 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200995#endif
996
Willy Tarreaue6b98942007-10-29 01:09:36 +0100997 /* the socket is ready */
998 listener->fd = fd;
999 listener->state = LI_LISTEN;
1000
Willy Tarreaueabf3132008-08-29 23:36:51 +02001001 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +02001002 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +02001003 fd_insert(fd);
1004
Willy Tarreaue6b98942007-10-29 01:09:36 +01001005 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001006 if (msg && errlen) {
1007 char pn[INET6_ADDRSTRLEN];
1008
Willy Tarreau631f01c2011-09-05 00:36:48 +02001009 addr_to_str(&listener->addr, pn, sizeof(pn));
1010 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001011 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001012 return err;
1013
1014 tcp_close_return:
1015 close(fd);
1016 goto tcp_return;
1017}
1018
1019/* This function creates all TCP sockets bound to the protocol entry <proto>.
1020 * It is intended to be used as the protocol's bind_all() function.
1021 * The sockets will be registered but not added to any fd_set, in order not to
1022 * loose them across the fork(). A call to enable_all_listeners() is needed
1023 * to complete initialization. The return value is composed from ERR_*.
1024 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001025static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001026{
1027 struct listener *listener;
1028 int err = ERR_NONE;
1029
1030 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001031 err |= tcp_bind_listener(listener, errmsg, errlen);
1032 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001033 break;
1034 }
1035
1036 return err;
1037}
1038
1039/* Add listener to the list of tcpv4 listeners. The listener's state
1040 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1041 * listeners is updated. This is the function to use to add a new listener.
1042 */
1043void tcpv4_add_listener(struct listener *listener)
1044{
1045 if (listener->state != LI_INIT)
1046 return;
1047 listener->state = LI_ASSIGNED;
1048 listener->proto = &proto_tcpv4;
1049 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1050 proto_tcpv4.nb_listeners++;
1051}
1052
1053/* Add listener to the list of tcpv4 listeners. The listener's state
1054 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1055 * listeners is updated. This is the function to use to add a new listener.
1056 */
1057void tcpv6_add_listener(struct listener *listener)
1058{
1059 if (listener->state != LI_INIT)
1060 return;
1061 listener->state = LI_ASSIGNED;
1062 listener->proto = &proto_tcpv6;
1063 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1064 proto_tcpv6.nb_listeners++;
1065}
1066
Willy Tarreau092d8652014-07-07 20:22:12 +02001067/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1068 * was totally stopped, or > 0 if correctly paused.
1069 */
1070int tcp_pause_listener(struct listener *l)
1071{
1072 if (shutdown(l->fd, SHUT_WR) != 0)
1073 return -1; /* Solaris dies here */
1074
1075 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1076 return -1; /* OpenBSD dies here */
1077
1078 if (shutdown(l->fd, SHUT_RD) != 0)
1079 return -1; /* should always be OK */
1080 return 1;
1081}
1082
Willy Tarreauedcf6682008-11-30 23:15:34 +01001083/* This function performs the TCP request analysis on the current request. It
1084 * returns 1 if the processing can continue on next analysers, or zero if it
1085 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +02001086 * request. It relies on buffers flags, and updates s->req->analysers. The
1087 * function may be called for frontend rules and backend rules. It only relies
1088 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001089 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001090int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +01001091{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001092 struct session *sess = s->sess;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001093 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +02001094 struct stksess *ts;
1095 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001096 int partial;
1097
Willy Tarreau87b09662015-04-03 00:22:06 +02001098 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 +01001099 now_ms, __FUNCTION__,
1100 s,
1101 req,
1102 req->rex, req->wex,
1103 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001104 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +01001105 req->analysers);
1106
Willy Tarreauedcf6682008-11-30 23:15:34 +01001107 /* We don't know whether we have enough data, so must proceed
1108 * this way :
1109 * - iterate through all rules in their declaration order
1110 * - if one rule returns MISS, it means the inspect delay is
1111 * not over yet, then return immediately, otherwise consider
1112 * it as a non-match.
1113 * - if one rule returns OK, then return OK
1114 * - if one rule returns KO, then return KO
1115 */
1116
Willy Tarreau9b28e032012-10-12 23:49:43 +02001117 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001118 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001119 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001120 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001121 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001122
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001123 /* If "the current_rule_list" match the executed rule list, we are in
1124 * resume condition. If a resume is needed it is always in the action
1125 * and never in the ACL or converters. In this case, we initialise the
1126 * current rule, and go to the action execution point.
1127 */
1128 if (s->current_rule_list == &s->be->tcp_req.inspect_rules) {
1129 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
1130 goto resume_execution;
1131 }
1132 s->current_rule_list = &s->be->tcp_req.inspect_rules;
Willy Tarreaufb356202010-08-03 14:02:05 +02001133 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001134 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001135
1136 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001137 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001138 if (ret == ACL_TEST_MISS)
1139 goto missing_data;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001140
1141 ret = acl_pass(ret);
1142 if (rule->cond->pol == ACL_COND_UNLESS)
1143 ret = !ret;
1144 }
1145
1146 if (ret) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001147
1148resume_execution:
1149
Willy Tarreauedcf6682008-11-30 23:15:34 +01001150 /* we have a matching rule. */
1151 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001152 channel_abort(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001153 channel_abort(&s->res);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001154 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001155
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001156 s->be->be_counters.denied_req++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001157 sess->fe->fe_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001158 if (sess->listener->counters)
1159 sess->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001160
Willy Tarreaue7dff022015-04-03 01:14:29 +02001161 if (!(s->flags & SF_ERR_MASK))
1162 s->flags |= SF_ERR_PRXCOND;
1163 if (!(s->flags & SF_FINST_MASK))
1164 s->flags |= SF_FINST_R;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001165 return 0;
1166 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001167 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001168 /* Note: only the first valid tracking parameter of each
1169 * applies.
1170 */
1171 struct stktable_key *key;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001172 struct sample smp;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001173
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001174 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001175 continue;
1176
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001177 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001178 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial, rule->act_prm.trk_ctr.expr, &smp);
1179
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001180 if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL))
1181 goto missing_data; /* key might appear later */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001182
1183 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001184 stream_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001185 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001186 if (sess->fe != s->be)
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001187 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreaud1f96522010-08-03 19:34:32 +02001188 }
1189 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001190 else if (rule->action == TCP_ACT_CAPTURE) {
1191 struct sample *key;
1192 struct cap_hdr *h = rule->act_prm.cap.hdr;
1193 char **cap = s->txn.req.cap;
1194 int len;
1195
1196 key = sample_fetch_string(s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial, rule->act_prm.cap.expr);
1197 if (!key)
1198 continue;
1199
1200 if (key->flags & SMP_F_MAY_CHANGE)
1201 goto missing_data;
1202
1203 if (cap[h->index] == NULL)
1204 cap[h->index] = pool_alloc2(h->pool);
1205
1206 if (cap[h->index] == NULL) /* no more capture memory */
1207 continue;
1208
1209 len = key->data.str.len;
1210 if (len > h->len)
1211 len = h->len;
1212
1213 memcpy(cap[h->index], key->data.str.str, len);
1214 cap[h->index][len] = 0;
1215 }
Willy Tarreaud1f96522010-08-03 19:34:32 +02001216 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001217 /* Custom keywords. */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001218 if (rule->action_ptr(rule, s->be, s) == 0) {
1219 s->current_rule = &rule->list;
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001220 goto missing_data;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001221 }
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001222
Willy Tarreauedcf6682008-11-30 23:15:34 +01001223 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001224 break;
1225 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001226 }
1227 }
1228
1229 /* if we get there, it means we have no rule which matches, or
1230 * we have an explicit accept, so we apply the default accept.
1231 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001232 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001233 req->analyse_exp = TICK_ETERNITY;
1234 return 1;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001235
1236 missing_data:
1237 channel_dont_connect(req);
1238 /* just set the request timeout once at the beginning of the request */
1239 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
1240 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
1241 return 0;
1242
Willy Tarreauedcf6682008-11-30 23:15:34 +01001243}
1244
Emeric Brun97679e72010-09-23 17:56:44 +02001245/* This function performs the TCP response analysis on the current response. It
1246 * returns 1 if the processing can continue on next analysers, or zero if it
1247 * needs more data, encounters an error, or wants to immediately abort the
1248 * response. It relies on buffers flags, and updates s->rep->analysers. The
1249 * function may be called for backend rules.
1250 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001251int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001252{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001253 struct session *sess = s->sess;
Emeric Brun97679e72010-09-23 17:56:44 +02001254 struct tcp_rule *rule;
1255 int partial;
1256
Willy Tarreau87b09662015-04-03 00:22:06 +02001257 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 +02001258 now_ms, __FUNCTION__,
1259 s,
1260 rep,
1261 rep->rex, rep->wex,
1262 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001263 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001264 rep->analysers);
1265
1266 /* We don't know whether we have enough data, so must proceed
1267 * this way :
1268 * - iterate through all rules in their declaration order
1269 * - if one rule returns MISS, it means the inspect delay is
1270 * not over yet, then return immediately, otherwise consider
1271 * it as a non-match.
1272 * - if one rule returns OK, then return OK
1273 * - if one rule returns KO, then return KO
1274 */
1275
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001276 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001277 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001278 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001279 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001280
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001281 /* If "the current_rule_list" match the executed rule list, we are in
1282 * resume condition. If a resume is needed it is always in the action
1283 * and never in the ACL or converters. In this case, we initialise the
1284 * current rule, and go to the action execution point.
1285 */
1286 if (s->current_rule_list == &s->be->tcp_rep.inspect_rules) {
1287 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
1288 goto resume_execution;
1289 }
1290 s->current_rule_list = &s->be->tcp_rep.inspect_rules;
Emeric Brun97679e72010-09-23 17:56:44 +02001291 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001292 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001293
1294 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001295 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001296 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001297 /* just set the analyser timeout once at the beginning of the response */
1298 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001299 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001300 return 0;
1301 }
1302
1303 ret = acl_pass(ret);
1304 if (rule->cond->pol == ACL_COND_UNLESS)
1305 ret = !ret;
1306 }
1307
1308 if (ret) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001309
1310resume_execution:
1311
Emeric Brun97679e72010-09-23 17:56:44 +02001312 /* we have a matching rule. */
1313 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001314 channel_abort(rep);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001315 channel_abort(&s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001316 rep->analysers = 0;
1317
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001318 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001319 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001320 if (sess->listener->counters)
1321 sess->listener->counters->denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001322
Willy Tarreaue7dff022015-04-03 01:14:29 +02001323 if (!(s->flags & SF_ERR_MASK))
1324 s->flags |= SF_ERR_PRXCOND;
1325 if (!(s->flags & SF_FINST_MASK))
1326 s->flags |= SF_FINST_D;
Emeric Brun97679e72010-09-23 17:56:44 +02001327 return 0;
1328 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001329 else if (rule->action == TCP_ACT_CLOSE) {
Willy Tarreau73796532014-11-28 14:10:28 +01001330 chn_prod(rep)->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1331 si_shutr(chn_prod(rep));
1332 si_shutw(chn_prod(rep));
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001333 break;
1334 }
Emeric Brun97679e72010-09-23 17:56:44 +02001335 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001336 /* Custom keywords. */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001337 if (!rule->action_ptr(rule, s->be, s)) {
1338 channel_dont_close(rep);
1339 s->current_rule = &rule->list;
1340 return 0;
1341 }
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001342
Emeric Brun97679e72010-09-23 17:56:44 +02001343 /* otherwise accept */
1344 break;
1345 }
1346 }
1347 }
1348
1349 /* if we get there, it means we have no rule which matches, or
1350 * we have an explicit accept, so we apply the default accept.
1351 */
1352 rep->analysers &= ~an_bit;
1353 rep->analyse_exp = TICK_ETERNITY;
1354 return 1;
1355}
1356
1357
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001358/* This function performs the TCP layer4 analysis on the current request. It
1359 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1360 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001361 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001362 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001363int tcp_exec_req_rules(struct stream *s)
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001364{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001365 struct session *sess = s->sess;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001366 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001367 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001368 struct stktable *t = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001369 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001370 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001371 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001372
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001373 if (!conn)
1374 return result;
1375
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001376 list_for_each_entry(rule, &sess->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001377 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001378
1379 if (rule->cond) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001380 ret = acl_exec_cond(rule->cond, sess->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001381 ret = acl_pass(ret);
1382 if (rule->cond->pol == ACL_COND_UNLESS)
1383 ret = !ret;
1384 }
1385
1386 if (ret) {
1387 /* we have a matching rule. */
1388 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001389 sess->fe->fe_counters.denied_conn++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001390 if (sess->listener->counters)
1391 sess->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001392
Willy Tarreaue7dff022015-04-03 01:14:29 +02001393 if (!(s->flags & SF_ERR_MASK))
1394 s->flags |= SF_ERR_PRXCOND;
1395 if (!(s->flags & SF_FINST_MASK))
1396 s->flags |= SF_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001397 result = 0;
1398 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001399 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001400 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001401 /* Note: only the first valid tracking parameter of each
1402 * applies.
1403 */
1404 struct stktable_key *key;
1405
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001406 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001407 continue;
1408
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001409 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreaub5975de2014-06-25 16:20:53 +02001410 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001411
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001412 if (key && (ts = stktable_get_entry(t, key)))
Willy Tarreau87b09662015-04-03 00:22:06 +02001413 stream_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001414 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001415 else if (rule->action == TCP_ACT_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001416 conn->flags |= CO_FL_ACCEPT_PROXY;
1417 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001418 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001419 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001420 /* Custom keywords. */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001421 rule->action_ptr(rule, sess->fe, s);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001422
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001423 /* otherwise it's an accept */
1424 break;
1425 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001426 }
1427 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001428 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001429}
1430
Emeric Brun97679e72010-09-23 17:56:44 +02001431/* Parse a tcp-response rule. Return a negative value in case of failure */
1432static int tcp_parse_response_rule(char **args, int arg, int section_type,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001433 struct proxy *curpx, struct proxy *defpx,
1434 struct tcp_rule *rule, char **err,
1435 unsigned int where,
1436 const char *file, int line)
Emeric Brun97679e72010-09-23 17:56:44 +02001437{
1438 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001439 memprintf(err, "%s %s is only allowed in 'backend' sections",
1440 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001441 return -1;
1442 }
1443
1444 if (strcmp(args[arg], "accept") == 0) {
1445 arg++;
1446 rule->action = TCP_ACT_ACCEPT;
1447 }
1448 else if (strcmp(args[arg], "reject") == 0) {
1449 arg++;
1450 rule->action = TCP_ACT_REJECT;
1451 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001452 else if (strcmp(args[arg], "close") == 0) {
1453 arg++;
1454 rule->action = TCP_ACT_CLOSE;
1455 }
Emeric Brun97679e72010-09-23 17:56:44 +02001456 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001457 struct tcp_action_kw *kw;
1458 kw = tcp_res_cont_action(args[arg]);
1459 if (kw) {
1460 arg++;
1461 if (!kw->parse((const char **)args, &arg, curpx, rule, err))
1462 return -1;
1463 } else {
1464 memprintf(err,
1465 "'%s %s' expects 'accept', 'close' or 'reject' in %s '%s' (got '%s')",
1466 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
1467 return -1;
1468 }
Emeric Brun97679e72010-09-23 17:56:44 +02001469 }
1470
1471 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001472 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001473 memprintf(err,
1474 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1475 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001476 return -1;
1477 }
1478 }
1479 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001480 memprintf(err,
1481 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001482 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1483 return -1;
1484 }
1485 return 0;
1486}
1487
1488
1489
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001490/* Parse a tcp-request rule. Return a negative value in case of failure */
1491static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001492 struct proxy *curpx, struct proxy *defpx,
1493 struct tcp_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001494 unsigned int where, const char *file, int line)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001495{
1496 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001497 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1498 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001499 return -1;
1500 }
1501
1502 if (!strcmp(args[arg], "accept")) {
1503 arg++;
1504 rule->action = TCP_ACT_ACCEPT;
1505 }
1506 else if (!strcmp(args[arg], "reject")) {
1507 arg++;
1508 rule->action = TCP_ACT_REJECT;
1509 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001510 else if (strcmp(args[arg], "capture") == 0) {
1511 struct sample_expr *expr;
1512 struct cap_hdr *hdr;
1513 int kw = arg;
1514 int len = 0;
1515
1516 if (!(curpx->cap & PR_CAP_FE)) {
1517 memprintf(err,
1518 "'%s %s %s' : proxy '%s' has no frontend capability",
1519 args[0], args[1], args[kw], curpx->id);
1520 return -1;
1521 }
1522
1523 if (!(where & SMP_VAL_FE_REQ_CNT)) {
1524 memprintf(err,
1525 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1526 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1527 return -1;
1528 }
1529
1530 arg++;
1531
1532 curpx->conf.args.ctx = ARGC_CAP;
1533 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
1534 if (!expr) {
1535 memprintf(err,
1536 "'%s %s %s' : %s",
1537 args[0], args[1], args[kw], *err);
1538 return -1;
1539 }
1540
1541 if (!(expr->fetch->val & where)) {
1542 memprintf(err,
1543 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
1544 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
1545 free(expr);
1546 return -1;
1547 }
1548
1549 if (strcmp(args[arg], "len") == 0) {
1550 arg++;
1551 if (!args[arg]) {
1552 memprintf(err,
1553 "'%s %s %s' : missing length value",
1554 args[0], args[1], args[kw]);
1555 free(expr);
1556 return -1;
1557 }
1558 /* we copy the table name for now, it will be resolved later */
1559 len = atoi(args[arg]);
1560 if (len <= 0) {
1561 memprintf(err,
1562 "'%s %s %s' : length must be > 0",
1563 args[0], args[1], args[kw]);
1564 free(expr);
1565 return -1;
1566 }
1567 arg++;
1568 }
1569
1570 if (!len) {
1571 memprintf(err,
1572 "'%s %s %s' : a positive 'len' argument is mandatory",
1573 args[0], args[1], args[kw]);
1574 free(expr);
1575 return -1;
1576 }
1577
1578 hdr = calloc(sizeof(struct cap_hdr), 1);
1579 hdr->next = curpx->req_cap;
1580 hdr->name = NULL; /* not a header capture */
1581 hdr->namelen = 0;
1582 hdr->len = len;
1583 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
1584 hdr->index = curpx->nb_req_cap++;
1585
1586 curpx->req_cap = hdr;
1587 curpx->to_log |= LW_REQHDR;
1588
1589 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
1590 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1591
1592 rule->act_prm.cap.expr = expr;
1593 rule->act_prm.cap.hdr = hdr;
1594 rule->action = TCP_ACT_CAPTURE;
1595 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001596 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1597 args[arg][9] == '\0' && args[arg][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02001598 args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001599 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001600 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001601
1602 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001603
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001604 curpx->conf.args.ctx = ARGC_TRK;
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001605 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001606 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001607 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001608 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001609 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001610 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001611 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001612
Willy Tarreau80aca902013-01-07 15:42:20 +01001613 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001614 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001615 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001616 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001617 free(expr);
1618 return -1;
1619 }
1620
1621 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001622 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001623
1624 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001625 arg++;
1626 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001627 memprintf(err,
1628 "'%s %s %s' : missing table name",
1629 args[0], args[1], args[kw]);
1630 free(expr);
1631 return -1;
1632 }
1633 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001634 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001635 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001636 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001637 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001638 rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001639 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001640 else if (strcmp(args[arg], "expect-proxy") == 0) {
1641 if (strcmp(args[arg+1], "layer4") != 0) {
1642 memprintf(err,
1643 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1644 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1645 return -1;
1646 }
1647
1648 if (!(where & SMP_VAL_FE_CON_ACC)) {
1649 memprintf(err,
1650 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1651 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1652 return -1;
1653 }
1654
1655 arg += 2;
1656 rule->action = TCP_ACT_EXPECT_PX;
1657 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001658 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001659 struct tcp_action_kw *kw;
1660 if (where & SMP_VAL_FE_CON_ACC)
1661 kw = tcp_req_conn_action(args[arg]);
1662 else
1663 kw = tcp_req_cont_action(args[arg]);
1664 if (kw) {
1665 arg++;
1666 if (!kw->parse((const char **)args, &arg, curpx, rule, err))
1667 return -1;
1668 } else {
1669 memprintf(err,
1670 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d' "
1671 " in %s '%s' (got '%s')",
1672 args[0], args[1], MAX_SESS_STKCTR-1, proxy_type_str(curpx),
1673 curpx->id, args[arg]);
1674 return -1;
1675 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001676 }
1677
1678 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001679 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001680 memprintf(err,
1681 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1682 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001683 return -1;
1684 }
1685 }
1686 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001687 memprintf(err,
1688 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001689 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1690 return -1;
1691 }
1692 return 0;
1693}
1694
Emeric Brun97679e72010-09-23 17:56:44 +02001695/* This function should be called to parse a line starting with the "tcp-response"
1696 * keyword.
1697 */
1698static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001699 struct proxy *defpx, const char *file, int line,
1700 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001701{
1702 const char *ptr = NULL;
1703 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001704 int warn = 0;
1705 int arg;
1706 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001707 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001708 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001709 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001710
1711 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001712 memprintf(err, "missing argument for '%s' in %s '%s'",
1713 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001714 return -1;
1715 }
1716
1717 if (strcmp(args[1], "inspect-delay") == 0) {
1718 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001719 memprintf(err, "%s %s is only allowed in 'backend' sections",
1720 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001721 return -1;
1722 }
1723
1724 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001725 memprintf(err,
1726 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1727 args[0], args[1], proxy_type_str(curpx), curpx->id);
1728 if (ptr)
1729 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001730 return -1;
1731 }
1732
1733 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001734 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1735 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001736 return 1;
1737 }
1738 curpx->tcp_rep.inspect_delay = val;
1739 return 0;
1740 }
1741
Simon Hormande072bd2011-06-24 15:11:37 +09001742 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001743 LIST_INIT(&rule->list);
1744 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001745 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001746
1747 if (strcmp(args[1], "content") == 0) {
1748 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001749
1750 if (curpx->cap & PR_CAP_FE)
1751 where |= SMP_VAL_FE_RES_CNT;
1752 if (curpx->cap & PR_CAP_BE)
1753 where |= SMP_VAL_BE_RES_CNT;
1754
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001755 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001756 goto error;
1757
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001758 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1759 if (acl) {
1760 if (acl->name && *acl->name)
1761 memprintf(err,
1762 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1763 acl->name, args[0], args[1], sample_ckp_names(where));
1764 else
1765 memprintf(err,
1766 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1767 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001768 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001769 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001770
Emeric Brun97679e72010-09-23 17:56:44 +02001771 warn++;
1772 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001773 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1774 if (acl->name && *acl->name)
1775 memprintf(err,
1776 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001777 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001778 else
1779 memprintf(err,
1780 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001781 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001782 warn++;
1783 }
Emeric Brun97679e72010-09-23 17:56:44 +02001784
1785 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1786 }
1787 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001788 memprintf(err,
1789 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1790 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001791 goto error;
1792 }
1793
1794 return warn;
1795 error:
1796 free(rule);
1797 return -1;
1798}
1799
1800
Willy Tarreaub6866442008-07-14 23:54:42 +02001801/* This function should be called to parse a line starting with the "tcp-request"
1802 * keyword.
1803 */
1804static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001805 struct proxy *defpx, const char *file, int line,
1806 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001807{
1808 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001809 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001810 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001811 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001812 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001813 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001814 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001815 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001816
1817 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001818 if (curpx == defpx)
1819 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1820 else
1821 memprintf(err, "missing argument for '%s' in %s '%s'",
1822 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001823 return -1;
1824 }
1825
1826 if (!strcmp(args[1], "inspect-delay")) {
1827 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001828 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1829 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001830 return -1;
1831 }
1832
Willy Tarreaub6866442008-07-14 23:54:42 +02001833 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001834 memprintf(err,
1835 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1836 args[0], args[1], proxy_type_str(curpx), curpx->id);
1837 if (ptr)
1838 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001839 return -1;
1840 }
1841
1842 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001843 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1844 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001845 return 1;
1846 }
1847 curpx->tcp_req.inspect_delay = val;
1848 return 0;
1849 }
1850
Simon Hormande072bd2011-06-24 15:11:37 +09001851 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001852 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001853 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001854 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001855
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001856 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001857 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001858
1859 if (curpx->cap & PR_CAP_FE)
1860 where |= SMP_VAL_FE_REQ_CNT;
1861 if (curpx->cap & PR_CAP_BE)
1862 where |= SMP_VAL_BE_REQ_CNT;
1863
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001864 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001865 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001866
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001867 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1868 if (acl) {
1869 if (acl->name && *acl->name)
1870 memprintf(err,
1871 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1872 acl->name, args[0], args[1], sample_ckp_names(where));
1873 else
1874 memprintf(err,
1875 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1876 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001877 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001878 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001879
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001880 warn++;
1881 }
1882 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1883 if (acl->name && *acl->name)
1884 memprintf(err,
1885 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001886 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001887 else
1888 memprintf(err,
1889 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001890 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001891 warn++;
1892 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001893
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001894 /* the following function directly emits the warning */
1895 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001896 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001897 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001898 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001899 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001900
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001901 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001902 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1903 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001904 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001905 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001906
Willy Tarreau80aca902013-01-07 15:42:20 +01001907 where |= SMP_VAL_FE_CON_ACC;
1908
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001909 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001910 goto error;
1911
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001912 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1913 if (acl) {
1914 if (acl->name && *acl->name)
1915 memprintf(err,
1916 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1917 acl->name, args[0], args[1], sample_ckp_names(where));
1918 else
1919 memprintf(err,
1920 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1921 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001922 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001923 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001924
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001925 warn++;
1926 }
1927 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1928 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001929 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001930 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001931 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001932 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001933 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001934 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001935 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001936 warn++;
1937 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001938
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001939 /* the following function directly emits the warning */
1940 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001941 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001942 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001943 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001944 if (curpx == defpx)
1945 memprintf(err,
1946 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1947 args[0], args[1]);
1948 else
1949 memprintf(err,
1950 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001951 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001952 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001953 }
1954
Willy Tarreau1a687942010-05-23 22:40:30 +02001955 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001956 error:
1957 free(rule);
1958 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001959}
1960
Willy Tarreau645513a2010-05-24 20:55:15 +02001961
1962/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001963/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001964/************************************************************************/
1965
Willy Tarreau4a129812012-04-25 17:31:42 +02001966/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001967static int
Willy Tarreau87b09662015-04-03 00:22:06 +02001968smp_fetch_src(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01001969 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001970{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001971 struct session *sess = strm_sess(l4);
1972 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001973
1974 if (!cli_conn)
1975 return 0;
1976
1977 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001978 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001979 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001980 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001981 break;
1982 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001983 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001984 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001985 break;
1986 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001987 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001988 }
Emeric Brunf769f512010-10-22 17:14:01 +02001989
Willy Tarreau37406352012-04-23 16:16:37 +02001990 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001991 return 1;
1992}
1993
Willy Tarreaua5e37562011-12-16 17:06:15 +01001994/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001995static int
Willy Tarreau87b09662015-04-03 00:22:06 +02001996smp_fetch_sport(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01001997 const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001998{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001999 struct session *sess = strm_sess(l4);
2000 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002001
2002 if (!cli_conn)
2003 return 0;
2004
Willy Tarreauf853c462012-04-23 18:53:56 +02002005 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002006 if (!(smp->data.uint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02002007 return 0;
2008
Willy Tarreau37406352012-04-23 16:16:37 +02002009 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002010 return 1;
2011}
2012
Willy Tarreau4a129812012-04-25 17:31:42 +02002013/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02002014static int
Willy Tarreau87b09662015-04-03 00:22:06 +02002015smp_fetch_dst(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002016 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002017{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002018 struct session *sess = strm_sess(l4);
2019 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02002020
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002021 if (!cli_conn)
2022 return 0;
2023
2024 conn_get_to_addr(cli_conn);
2025
2026 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01002027 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002028 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02002029 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002030 break;
2031 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002032 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02002033 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002034 break;
2035 default:
Emeric Brunf769f512010-10-22 17:14:01 +02002036 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002037 }
Emeric Brunf769f512010-10-22 17:14:01 +02002038
Willy Tarreau37406352012-04-23 16:16:37 +02002039 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002040 return 1;
2041}
2042
Willy Tarreaua5e37562011-12-16 17:06:15 +01002043/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002044static int
Willy Tarreau87b09662015-04-03 00:22:06 +02002045smp_fetch_dport(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002046 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002047{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002048 struct session *sess = strm_sess(l4);
2049 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002050
2051 if (!cli_conn)
2052 return 0;
2053
2054 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02002055
Willy Tarreauf853c462012-04-23 18:53:56 +02002056 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002057 if (!(smp->data.uint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02002058 return 0;
2059
Willy Tarreau37406352012-04-23 16:16:37 +02002060 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002061 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01002062}
2063
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002064#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002065/* parse the "v4v6" bind keyword */
2066static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2067{
2068 struct listener *l;
2069
2070 list_for_each_entry(l, &conf->listeners, by_bind) {
2071 if (l->addr.ss_family == AF_INET6)
2072 l->options |= LI_O_V4V6;
2073 }
2074
2075 return 0;
2076}
2077
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002078/* parse the "v6only" bind keyword */
2079static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2080{
2081 struct listener *l;
2082
2083 list_for_each_entry(l, &conf->listeners, by_bind) {
2084 if (l->addr.ss_family == AF_INET6)
2085 l->options |= LI_O_V6ONLY;
2086 }
2087
2088 return 0;
2089}
2090#endif
2091
Pieter Baauwd551fb52013-05-08 22:49:23 +02002092#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002093/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002094static 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 +02002095{
2096 struct listener *l;
2097
Willy Tarreau4348fad2012-09-20 16:48:07 +02002098 list_for_each_entry(l, &conf->listeners, by_bind) {
2099 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2100 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02002101 }
2102
Willy Tarreau44791242012-09-12 23:27:21 +02002103 return 0;
2104}
2105#endif
2106
2107#ifdef TCP_DEFER_ACCEPT
2108/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002109static 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 +02002110{
2111 struct listener *l;
2112
Willy Tarreau4348fad2012-09-20 16:48:07 +02002113 list_for_each_entry(l, &conf->listeners, by_bind) {
2114 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2115 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02002116 }
2117
Willy Tarreau44791242012-09-12 23:27:21 +02002118 return 0;
2119}
2120#endif
2121
Willy Tarreau1c862c52012-10-05 16:21:00 +02002122#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01002123/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02002124static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2125{
2126 struct listener *l;
2127
2128 list_for_each_entry(l, &conf->listeners, by_bind) {
2129 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2130 l->options |= LI_O_TCP_FO;
2131 }
2132
2133 return 0;
2134}
2135#endif
2136
Willy Tarreau44791242012-09-12 23:27:21 +02002137#ifdef TCP_MAXSEG
2138/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002139static 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 +02002140{
2141 struct listener *l;
2142 int mss;
2143
Willy Tarreau44791242012-09-12 23:27:21 +02002144 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002145 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002146 return ERR_ALERT | ERR_FATAL;
2147 }
2148
2149 mss = atoi(args[cur_arg + 1]);
2150 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002151 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002152 return ERR_ALERT | ERR_FATAL;
2153 }
2154
Willy Tarreau4348fad2012-09-20 16:48:07 +02002155 list_for_each_entry(l, &conf->listeners, by_bind) {
2156 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2157 l->maxseg = mss;
2158 }
Willy Tarreau44791242012-09-12 23:27:21 +02002159
2160 return 0;
2161}
2162#endif
2163
Willy Tarreau2af207a2015-02-04 00:45:58 +01002164#ifdef TCP_USER_TIMEOUT
2165/* parse the "tcp-ut" bind keyword */
2166static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2167{
2168 const char *ptr = NULL;
2169 struct listener *l;
2170 unsigned int timeout;
2171
2172 if (!*args[cur_arg + 1]) {
2173 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
2174 return ERR_ALERT | ERR_FATAL;
2175 }
2176
2177 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
2178 if (ptr) {
2179 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
2180 return ERR_ALERT | ERR_FATAL;
2181 }
2182
2183 list_for_each_entry(l, &conf->listeners, by_bind) {
2184 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2185 l->tcp_ut = timeout;
2186 }
2187
2188 return 0;
2189}
2190#endif
2191
Willy Tarreau44791242012-09-12 23:27:21 +02002192#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01002193/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002194static 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 +02002195{
2196 struct listener *l;
2197
Willy Tarreau44791242012-09-12 23:27:21 +02002198 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002199 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002200 return ERR_ALERT | ERR_FATAL;
2201 }
2202
Willy Tarreau4348fad2012-09-20 16:48:07 +02002203 list_for_each_entry(l, &conf->listeners, by_bind) {
2204 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2205 l->interface = strdup(args[cur_arg + 1]);
2206 }
Willy Tarreau44791242012-09-12 23:27:21 +02002207
2208 global.last_checks |= LSTCHK_NETADM;
2209 return 0;
2210}
2211#endif
2212
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002213#ifdef CONFIG_HAP_NS
2214/* parse the "namespace" bind keyword */
2215static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2216{
2217 struct listener *l;
2218 char *namespace = NULL;
2219
2220 if (!*args[cur_arg + 1]) {
2221 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
2222 return ERR_ALERT | ERR_FATAL;
2223 }
2224 namespace = args[cur_arg + 1];
2225
2226 list_for_each_entry(l, &conf->listeners, by_bind) {
2227 l->netns = netns_store_lookup(namespace, strlen(namespace));
2228
2229 if (l->netns == NULL)
2230 l->netns = netns_store_insert(namespace);
2231
2232 if (l->netns == NULL) {
2233 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
2234 return ERR_ALERT | ERR_FATAL;
2235 }
2236 }
2237 return 0;
2238}
2239#endif
2240
Willy Tarreaudc13c112013-06-21 23:16:39 +02002241static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002242 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02002243 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02002244 { 0, NULL, NULL },
2245}};
2246
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002247
Willy Tarreau61612d42012-04-19 18:42:05 +02002248/* Note: must not be declared <const> as its list will be overwritten.
2249 * Please take care of keeping this list alphabetically sorted.
2250 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002251static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002252 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02002253}};
2254
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002255
Willy Tarreau4a129812012-04-25 17:31:42 +02002256/* Note: must not be declared <const> as its list will be overwritten.
2257 * Note: fetches that may return multiple types must be declared as the lowest
2258 * common denominator, the type that can be casted into all other ones. For
2259 * instance v4/v6 must be declared v4.
2260 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002261static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002262 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
2263 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
2264 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
2265 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
2266 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002267}};
2268
Willy Tarreau44791242012-09-12 23:27:21 +02002269/************************************************************************/
2270/* All supported bind keywords must be declared here. */
2271/************************************************************************/
2272
2273/* Note: must not be declared <const> as its list will be overwritten.
2274 * Please take care of keeping this list alphabetically sorted, doing so helps
2275 * all code contributors.
2276 * Optional keywords are also declared with a NULL ->parse() function so that
2277 * the config parser can report an appropriate error when a known keyword was
2278 * not enabled.
2279 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002280static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02002281#ifdef TCP_DEFER_ACCEPT
2282 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
2283#endif
2284#ifdef SO_BINDTODEVICE
2285 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
2286#endif
2287#ifdef TCP_MAXSEG
2288 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
2289#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01002290#ifdef TCP_USER_TIMEOUT
2291 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
2292#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02002293#ifdef TCP_FASTOPEN
2294 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
2295#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02002296#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002297 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
2298#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002299#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002300 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002301 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
2302#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002303#ifdef CONFIG_HAP_NS
2304 { "namespace", bind_parse_namespace, 1 },
2305#endif
Willy Tarreau44791242012-09-12 23:27:21 +02002306 /* the versions with the NULL parse function*/
2307 { "defer-accept", NULL, 0 },
2308 { "interface", NULL, 1 },
2309 { "mss", NULL, 1 },
2310 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002311 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002312 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002313 { NULL, NULL, 0 },
2314}};
2315
Willy Tarreaue6b98942007-10-29 01:09:36 +01002316__attribute__((constructor))
2317static void __tcp_protocol_init(void)
2318{
2319 protocol_register(&proto_tcpv4);
2320 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002321 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02002322 cfg_register_keywords(&cfg_kws);
2323 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02002324 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002325}
2326
2327
2328/*
2329 * Local variables:
2330 * c-indent-level: 8
2331 * c-basic-offset: 8
2332 * End:
2333 */