blob: 077bc13dff4a6601081af38c123864720bc9233b [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++) {
Thierry FOURNIER0e118632015-06-04 11:44:06 +0200148 if (kw_list->kw[i].match_pfx &&
149 strncmp(kw, kw_list->kw[i].kw, strlen(kw_list->kw[i].kw)) == 0)
150 return &kw_list->kw[i];
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100151 if (!strcmp(kw, kw_list->kw[i].kw))
152 return &kw_list->kw[i];
153 }
154 }
155 return NULL;
156}
157
158static struct tcp_action_kw *tcp_req_cont_action(const char *kw)
159{
160 struct tcp_action_kw_list *kw_list;
161 int i;
162
163 if (LIST_ISEMPTY(&tcp_req_cont_keywords))
164 return NULL;
165
166 list_for_each_entry(kw_list, &tcp_req_cont_keywords, list) {
167 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
Thierry FOURNIER0e118632015-06-04 11:44:06 +0200168 if (kw_list->kw[i].match_pfx &&
169 strncmp(kw, kw_list->kw[i].kw, strlen(kw_list->kw[i].kw)) == 0)
170 return &kw_list->kw[i];
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100171 if (!strcmp(kw, kw_list->kw[i].kw))
172 return &kw_list->kw[i];
173 }
174 }
175 return NULL;
176}
177
178static struct tcp_action_kw *tcp_res_cont_action(const char *kw)
179{
180 struct tcp_action_kw_list *kw_list;
181 int i;
182
183 if (LIST_ISEMPTY(&tcp_res_cont_keywords))
184 return NULL;
185
186 list_for_each_entry(kw_list, &tcp_res_cont_keywords, list) {
187 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
Thierry FOURNIER0e118632015-06-04 11:44:06 +0200188 if (kw_list->kw[i].match_pfx &&
189 strncmp(kw, kw_list->kw[i].kw, strlen(kw_list->kw[i].kw)) == 0)
190 return &kw_list->kw[i];
Thierry FOURNIERcc87a112014-12-12 19:41:33 +0100191 if (!strcmp(kw, kw_list->kw[i].kw))
192 return &kw_list->kw[i];
193 }
194 }
195 return NULL;
196}
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100197
David du Colombier6f5ccb12011-03-10 22:26:24 +0100198/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100199 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
200 * - 0 : ignore remote address (may even be a NULL pointer)
201 * - 1 : use provided address
202 * - 2 : use provided port
203 * - 3 : use both
204 *
205 * The function supports multiple foreign binding methods :
206 * - linux_tproxy: we directly bind to the foreign address
207 * - cttproxy: we bind to a local address then nat.
208 * The second one can be used as a fallback for the first one.
209 * This function returns 0 when everything's OK, 1 if it could not bind, to the
210 * local address, 2 if it could not bind to the foreign address.
211 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100212int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100213{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100214 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100215 int foreign_ok = 0;
216 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100217 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200218 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200219
David du Colombier65c17962012-07-13 14:34:59 +0200220 switch (local->ss_family) {
221 case AF_INET:
222 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200223 /* This deserves some explanation. Some platforms will support
224 * multiple combinations of certain methods, so we try the
225 * supported ones until one succeeds.
226 */
227 if (0
228#if defined(IP_TRANSPARENT)
229 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
230#endif
231#if defined(IP_FREEBIND)
232 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
233#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200234#if defined(IP_BINDANY)
235 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
236#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200237#if defined(SO_BINDANY)
238 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
239#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200240 )
David du Colombier65c17962012-07-13 14:34:59 +0200241 foreign_ok = 1;
242 else
243 ip_transp_working = 0;
244 }
245 break;
246 case AF_INET6:
247 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200248 if (0
249#if defined(IPV6_TRANSPARENT)
250 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
251#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100252#if defined(IP_FREEBIND)
253 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
254#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200255#if defined(IPV6_BINDANY)
256 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
257#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200258#if defined(SO_BINDANY)
259 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
260#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200261 )
David du Colombier65c17962012-07-13 14:34:59 +0200262 foreign_ok = 1;
263 else
264 ip6_transp_working = 0;
265 }
266 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100267 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200268
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100269 if (flags) {
270 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200271 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100272 switch (remote->ss_family) {
273 case AF_INET:
274 if (flags & 1)
275 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
276 if (flags & 2)
277 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
278 break;
279 case AF_INET6:
280 if (flags & 1)
281 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
282 if (flags & 2)
283 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
284 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100285 default:
286 /* we don't want to try to bind to an unknown address family */
287 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100288 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100289 }
290
Simon Hormande072bd2011-06-24 15:11:37 +0900291 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100292 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200293 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200294 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
295 if (ret < 0)
296 return 2;
297 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100298 }
299 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200300 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200301 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
302 if (ret < 0)
303 return 1;
304 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100305 }
306
307 if (!flags)
308 return 0;
309
310#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100311 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100312 struct in_tproxy itp1, itp2;
313 memset(&itp1, 0, sizeof(itp1));
314
315 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100316 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
317 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100318
319 /* set connect flag on socket */
320 itp2.op = TPROXY_FLAGS;
321 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
322
323 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
324 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
325 foreign_ok = 1;
326 }
327 }
328#endif
329 if (!foreign_ok)
330 /* we could not bind to a foreign address */
331 return 2;
332
333 return 0;
334}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100335
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100336static int create_server_socket(struct connection *conn)
337{
Willy Tarreau529c1392014-12-24 13:47:55 +0100338 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100339
Willy Tarreau529c1392014-12-24 13:47:55 +0100340#ifdef CONFIG_HAP_NS
341 if (objt_server(conn->target)) {
342 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
343 ns = conn->proxy_netns;
344 else
345 ns = __objt_server(conn->target)->netns;
346 }
347#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100348 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
349}
Willy Tarreau9650f372009-08-16 14:02:45 +0200350
351/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200352 * This function initiates a TCP connection establishment to the target assigned
353 * to connection <conn> using (si->{target,addr.to}). A source address may be
354 * pointed to by conn->addr.from in case of transparent proxying. Normal source
355 * bind addresses are still determined locally (due to the possible need of a
356 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100357 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100358 * supported. The <data> parameter is a boolean indicating whether there are data
359 * waiting for being sent or not, in order to adjust data write polling and on
360 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
361 * allows the caller to force using a delayed ACK when establishing the connection :
362 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
363 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
364 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200365 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200366 * Note that a pending send_proxy message accounts for data.
367 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200368 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200369 * - SF_ERR_NONE if everything's OK
370 * - SF_ERR_SRVTO if there are no more servers
371 * - SF_ERR_SRVCL if the connection was refused by the server
372 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
373 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
374 * - SF_ERR_INTERNAL for any other purely internal errors
375 * Additionnally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100376 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200377 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100378 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200379 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100380
Willy Tarreauf0837b22012-11-24 10:24:27 +0100381int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200382{
383 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100384 struct server *srv;
385 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100386 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100387
Willy Tarreau9ce70132014-01-24 16:08:19 +0100388 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
389
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100390 switch (obj_type(conn->target)) {
391 case OBJ_TYPE_PROXY:
392 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100393 srv = NULL;
394 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100395 case OBJ_TYPE_SERVER:
396 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100397 be = srv->proxy;
398 break;
399 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100400 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200401 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100402 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200403
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100404 fd = conn->t.sock.fd = create_server_socket(conn);
405
406 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200407 qfprintf(stderr, "Cannot get a server socket.\n");
408
Willy Tarreau9ce70132014-01-24 16:08:19 +0100409 if (errno == ENFILE) {
410 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200411 send_log(be, LOG_EMERG,
412 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
413 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100414 }
415 else if (errno == EMFILE) {
416 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200417 send_log(be, LOG_EMERG,
418 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
419 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100420 }
421 else if (errno == ENOBUFS || errno == ENOMEM) {
422 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200423 send_log(be, LOG_EMERG,
424 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
425 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100426 }
427 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
428 conn->err_code = CO_ER_NOPROTO;
429 }
430 else
431 conn->err_code = CO_ER_SOCK_ERR;
432
Willy Tarreau9650f372009-08-16 14:02:45 +0200433 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100434 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200435 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200436 }
437
438 if (fd >= global.maxsock) {
439 /* do not log anything there, it's a normal condition when this option
440 * is used to serialize connections to a server !
441 */
442 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
443 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100444 conn->err_code = CO_ER_CONF_FDLIM;
445 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200446 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200447 }
448
449 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900450 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200451 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
452 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100453 conn->err_code = CO_ER_SOCK_ERR;
454 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200455 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200456 }
457
458 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900459 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200460
Willy Tarreau9650f372009-08-16 14:02:45 +0200461 /* allow specific binding :
462 * - server-specific at first
463 * - proxy-specific next
464 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100465 if (srv && srv->conn_src.opts & CO_SRC_BIND)
466 src = &srv->conn_src;
467 else if (be->conn_src.opts & CO_SRC_BIND)
468 src = &be->conn_src;
469 else
470 src = NULL;
471
472 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200473 int ret, flags = 0;
474
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200475 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100476 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100477 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200478 conn->flags |= CO_FL_PRIVATE;
479 /* fall through */
480 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200481 flags = 3;
482 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100483 case CO_SRC_TPROXY_CIP:
484 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200485 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200486 flags = 1;
487 break;
488 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200489 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200490
Willy Tarreau9650f372009-08-16 14:02:45 +0200491#ifdef SO_BINDTODEVICE
492 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100493 if (src->iface_name)
494 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200495#endif
496
Willy Tarreaua4380b42012-12-08 22:49:11 +0100497 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200498 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100499 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200500
501 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100502 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200503
504 do {
505 /* note: in case of retry, we may have to release a previously
506 * allocated port, hence this loop's construct.
507 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200508 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
509 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200510
511 if (!attempts)
512 break;
513 attempts--;
514
Willy Tarreaua4380b42012-12-08 22:49:11 +0100515 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100516 if (!fdinfo[fd].local_port) {
517 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200518 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100519 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200520
Willy Tarreaua4380b42012-12-08 22:49:11 +0100521 fdinfo[fd].port_range = src->sport_range;
522 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200523
Willy Tarreaua4380b42012-12-08 22:49:11 +0100524 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100525 if (ret != 0)
526 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200527 } while (ret != 0); /* binding NOK */
528 }
529 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100530 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100531 if (ret != 0)
532 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200533 }
534
Willy Tarreaua4380b42012-12-08 22:49:11 +0100535 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200536 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
537 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200538 close(fd);
539
Willy Tarreau9650f372009-08-16 14:02:45 +0200540 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100541 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200542 be->id);
543 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100544 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200545 be->id);
546 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100547 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200548 be->id);
549 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100550 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200551 be->id);
552 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100553 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200554 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200555 }
556 }
557
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400558#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200559 /* disabling tcp quick ack now allows the first request to leave the
560 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100561 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200562 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200563 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900564 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200565#endif
566
Willy Tarreaue803de22010-01-21 17:43:04 +0100567 if (global.tune.server_sndbuf)
568 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
569
570 if (global.tune.server_rcvbuf)
571 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
572
Willy Tarreau986a9d22012-08-30 21:11:38 +0200573 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200574 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
575
Willy Tarreaub1719512012-12-08 23:03:28 +0100576 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200577 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100578 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200579 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100580 conn->err_code = CO_ER_FREE_PORTS;
581 }
582 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200583 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100584 conn->err_code = CO_ER_ADDR_INUSE;
585 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200586
Willy Tarreaub1719512012-12-08 23:03:28 +0100587 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200588 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
589 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200590 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100591 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100592 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200593 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200594 } else if (errno == ETIMEDOUT) {
595 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200596 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
597 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200598 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100599 conn->err_code = CO_ER_SOCK_ERR;
600 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200601 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200602 } else {
603 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
604 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200605 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
606 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200607 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100608 conn->err_code = CO_ER_SOCK_ERR;
609 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200610 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200611 }
612 }
613
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100614 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200615
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200616 /* Prepare to send a few handshakes related to the on-wire protocol. */
617 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200618 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200619
Willy Tarreauf79c8172013-10-21 16:30:56 +0200620 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100621 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200622 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200623
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200624 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200625 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100626 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200627 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200628 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200629
Willy Tarreau14f8e862012-08-30 22:23:13 +0200630 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200631 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200632
Willy Tarreaue7dff022015-04-03 01:14:29 +0200633 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200634}
635
636
Willy Tarreau59b94792012-05-11 16:16:40 +0200637/*
638 * Retrieves the source address for the socket <fd>, with <dir> indicating
639 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
640 * success, -1 in case of error. The socket's source address is stored in
641 * <sa> for <salen> bytes.
642 */
643int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
644{
645 if (dir)
646 return getsockname(fd, sa, &salen);
647 else
648 return getpeername(fd, sa, &salen);
649}
650
651
652/*
653 * Retrieves the original destination address for the socket <fd>, with <dir>
654 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
655 * listener, if the original destination address was translated, the original
656 * address is retrieved. It returns 0 in case of success, -1 in case of error.
657 * The socket's source address is stored in <sa> for <salen> bytes.
658 */
659int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
660{
661 if (dir)
662 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100663 else {
664 int ret = getsockname(fd, sa, &salen);
665
666 if (ret < 0)
667 return ret;
668
Willy Tarreau59b94792012-05-11 16:16:40 +0200669#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100670 /* For TPROXY and Netfilter's NAT, we can retrieve the original
671 * IPv4 address before DNAT/REDIRECT. We must not do that with
672 * other families because v6-mapped IPv4 addresses are still
673 * reported as v4.
674 */
675 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
676 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
677 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200678#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100679 return ret;
680 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200681}
682
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200683/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100684 * receive shutdown. Returns positive if the shutdown was found, negative
685 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
686 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200687 */
688int tcp_drain(int fd)
689{
690 int turns = 2;
691 int len;
692
693 while (turns) {
694#ifdef MSG_TRUNC_CLEARS_INPUT
695 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
696 if (len == -1 && errno == EFAULT)
697#endif
698 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
699
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100700 if (len == 0) {
701 /* cool, shutdown received */
702 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200703 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100704 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200705
706 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100707 if (errno == EAGAIN) {
708 /* connection not closed yet */
709 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100710 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100711 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200712 if (errno == EINTR) /* oops, try again */
713 continue;
714 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100715 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200716 return 1;
717 }
718 /* OK we read some data, let's try again once */
719 turns--;
720 }
721 /* some data are still present, give up */
722 return 0;
723}
724
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200725/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100726 * and we have nothing to send. It updates the FD polling status. It returns 0
727 * if it fails in a fatal way or needs to poll to go further, otherwise it
728 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
729 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
730 * errno. The error checking is done in two passes in order to limit the number
731 * of syscalls in the normal case :
732 * - if POLL_ERR was reported by the poller, we check for a pending error on
733 * the socket before proceeding. If found, it's assigned to errno so that
734 * upper layers can see it.
735 * - otherwise connect() is used to check the connection state again, since
736 * the getsockopt return cannot reliably be used to know if the connection
737 * is still pending or ready. This one may often return an error as well,
738 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200739 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200740int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200741{
Willy Tarreau239d7182012-07-23 18:53:03 +0200742 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100743 socklen_t lskerr;
744 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200745
Willy Tarreau80184712012-07-06 14:54:49 +0200746 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200747 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200748
Willy Tarreau3c728722014-01-23 13:50:42 +0100749 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200750 return 0;
751
Willy Tarreau80184712012-07-06 14:54:49 +0200752 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200753 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200754
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100755 if (!fd_send_ready(fd))
756 return 0;
757
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100758 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
759 * without FD_POLL_IN also indicates a hangup without input data meaning
760 * there was no connection.
761 */
762 if (fdtab[fd].ev & FD_POLL_ERR ||
763 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
764 skerr = 0;
765 lskerr = sizeof(skerr);
766 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
767 errno = skerr;
768 if (errno == EAGAIN)
769 errno = 0;
770 if (errno)
771 goto out_error;
772 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200773
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100774 /* Use connect() to check the state of the socket. This has the
775 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200776 * - error
777 * - connecting (EALREADY, EINPROGRESS)
778 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200779 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200780 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200781 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100782 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100783 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200784 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200785 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200786
Willy Tarreau2c6be842012-07-06 17:12:34 +0200787 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200788 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200789
Willy Tarreau2c6be842012-07-06 17:12:34 +0200790 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200791 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200792
Willy Tarreau076be252012-07-06 16:02:29 +0200793 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200794 * forward the event to the transport layer which will notify the
795 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200796 */
Willy Tarreau80184712012-07-06 14:54:49 +0200797 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200798 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200799
800 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200801 /* Write error on the file descriptor. Report it to the connection
802 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200803 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100804 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100805 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100806 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200807 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200808}
809
Willy Tarreau59b94792012-05-11 16:16:40 +0200810
Willy Tarreaue6b98942007-10-29 01:09:36 +0100811/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100812 * an error message in <errmsg> if the message is at most <errlen> bytes long
813 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
814 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100815 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
816 * was alright and that no message was returned. ERR_RETRYABLE means that an
817 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700818 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100819 * the meaning of the error, but just indicate that a message is present which
820 * should be displayed with the respective level. Last, ERR_ABORT indicates
821 * that it's pointless to try to start other listeners. No error message is
822 * returned if errlen is NULL.
823 */
824int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
825{
826 __label__ tcp_return, tcp_close_return;
827 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100828 int ext, ready;
829 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100830 const char *msg = NULL;
831
832 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100833 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100834 *errmsg = 0;
835
836 if (listener->state != LI_ASSIGNED)
837 return ERR_NONE; /* already bound */
838
839 err = ERR_NONE;
840
Willy Tarreau40aa0702013-03-10 23:51:38 +0100841 /* if the listener already has an fd assigned, then we were offered the
842 * fd by an external process (most likely the parent), and we don't want
843 * to create a new socket. However we still want to set a few flags on
844 * the socket.
845 */
846 fd = listener->fd;
847 ext = (fd >= 0);
848
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100849 if (!ext) {
850 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
851
852 if (fd == -1) {
853 err |= ERR_RETRYABLE | ERR_ALERT;
854 msg = "cannot create listening socket";
855 goto tcp_return;
856 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100857 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100858
Willy Tarreaue6b98942007-10-29 01:09:36 +0100859 if (fd >= global.maxsock) {
860 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
861 msg = "not enough free sockets (raise '-n' parameter)";
862 goto tcp_close_return;
863 }
864
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200865 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100866 err |= ERR_FATAL | ERR_ALERT;
867 msg = "cannot make socket non-blocking";
868 goto tcp_close_return;
869 }
870
Willy Tarreau40aa0702013-03-10 23:51:38 +0100871 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100872 /* not fatal but should be reported */
873 msg = "cannot do so_reuseaddr";
874 err |= ERR_ALERT;
875 }
876
877 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900878 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100879
Willy Tarreaue6b98942007-10-29 01:09:36 +0100880#ifdef SO_REUSEPORT
881 /* OpenBSD supports this. As it's present in old libc versions of Linux,
882 * it might return an error that we will silently ignore.
883 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100884 if (!ext)
885 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100886#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200887
Willy Tarreau40aa0702013-03-10 23:51:38 +0100888 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200889 switch (listener->addr.ss_family) {
890 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200891 if (1
892#if defined(IP_TRANSPARENT)
893 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
894#endif
895#if defined(IP_FREEBIND)
896 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
897#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200898#if defined(IP_BINDANY)
899 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
900#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200901#if defined(SO_BINDANY)
902 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
903#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200904 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200905 msg = "cannot make listening socket transparent";
906 err |= ERR_ALERT;
907 }
908 break;
909 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200910 if (1
911#if defined(IPV6_TRANSPARENT)
912 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
913#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100914#if defined(IP_FREEBIND)
915 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
916#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200917#if defined(IPV6_BINDANY)
918 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
919#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200920#if defined(SO_BINDANY)
921 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
922#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200923 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200924 msg = "cannot make listening socket transparent";
925 err |= ERR_ALERT;
926 }
927 break;
928 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100929 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200930
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100931#ifdef SO_BINDTODEVICE
932 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100933 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100934 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100935 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100936 msg = "cannot bind listener to device";
937 err |= ERR_WARN;
938 }
939 }
940#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400941#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100942 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400943 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200944 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
945 msg = "cannot set MSS";
946 err |= ERR_WARN;
947 }
948 }
949#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100950#if defined(TCP_USER_TIMEOUT)
951 if (listener->tcp_ut) {
952 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
953 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
954 msg = "cannot set TCP User Timeout";
955 err |= ERR_WARN;
956 }
957 }
958#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200959#if defined(TCP_DEFER_ACCEPT)
960 if (listener->options & LI_O_DEF_ACCEPT) {
961 /* defer accept by up to one second */
962 int accept_delay = 1;
963 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
964 msg = "cannot enable DEFER_ACCEPT";
965 err |= ERR_WARN;
966 }
967 }
968#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200969#if defined(TCP_FASTOPEN)
970 if (listener->options & LI_O_TCP_FO) {
971 /* TFO needs a queue length, let's use the configured backlog */
972 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
973 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
974 msg = "cannot enable TCP_FASTOPEN";
975 err |= ERR_WARN;
976 }
977 }
978#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100979#if defined(IPV6_V6ONLY)
980 if (listener->options & LI_O_V6ONLY)
981 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100982 else if (listener->options & LI_O_V4V6)
983 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100984#endif
985
Willy Tarreau40aa0702013-03-10 23:51:38 +0100986 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100987 err |= ERR_RETRYABLE | ERR_ALERT;
988 msg = "cannot bind socket";
989 goto tcp_close_return;
990 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100991
Willy Tarreau40aa0702013-03-10 23:51:38 +0100992 ready = 0;
993 ready_len = sizeof(ready);
994 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
995 ready = 0;
996
997 if (!(ext && ready) && /* only listen if not already done by external process */
998 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100999 err |= ERR_RETRYABLE | ERR_ALERT;
1000 msg = "cannot listen to socket";
1001 goto tcp_close_return;
1002 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001003
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001004#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001005 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +09001006 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001007#endif
1008
Willy Tarreaue6b98942007-10-29 01:09:36 +01001009 /* the socket is ready */
1010 listener->fd = fd;
1011 listener->state = LI_LISTEN;
1012
Willy Tarreaueabf3132008-08-29 23:36:51 +02001013 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +02001014 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +02001015 fd_insert(fd);
1016
Willy Tarreaue6b98942007-10-29 01:09:36 +01001017 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001018 if (msg && errlen) {
1019 char pn[INET6_ADDRSTRLEN];
1020
Willy Tarreau631f01c2011-09-05 00:36:48 +02001021 addr_to_str(&listener->addr, pn, sizeof(pn));
1022 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001023 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001024 return err;
1025
1026 tcp_close_return:
1027 close(fd);
1028 goto tcp_return;
1029}
1030
1031/* This function creates all TCP sockets bound to the protocol entry <proto>.
1032 * It is intended to be used as the protocol's bind_all() function.
1033 * The sockets will be registered but not added to any fd_set, in order not to
1034 * loose them across the fork(). A call to enable_all_listeners() is needed
1035 * to complete initialization. The return value is composed from ERR_*.
1036 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001037static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001038{
1039 struct listener *listener;
1040 int err = ERR_NONE;
1041
1042 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001043 err |= tcp_bind_listener(listener, errmsg, errlen);
1044 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001045 break;
1046 }
1047
1048 return err;
1049}
1050
1051/* Add listener to the list of tcpv4 listeners. The listener's state
1052 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1053 * listeners is updated. This is the function to use to add a new listener.
1054 */
1055void tcpv4_add_listener(struct listener *listener)
1056{
1057 if (listener->state != LI_INIT)
1058 return;
1059 listener->state = LI_ASSIGNED;
1060 listener->proto = &proto_tcpv4;
1061 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1062 proto_tcpv4.nb_listeners++;
1063}
1064
1065/* Add listener to the list of tcpv4 listeners. The listener's state
1066 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1067 * listeners is updated. This is the function to use to add a new listener.
1068 */
1069void tcpv6_add_listener(struct listener *listener)
1070{
1071 if (listener->state != LI_INIT)
1072 return;
1073 listener->state = LI_ASSIGNED;
1074 listener->proto = &proto_tcpv6;
1075 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1076 proto_tcpv6.nb_listeners++;
1077}
1078
Willy Tarreau092d8652014-07-07 20:22:12 +02001079/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1080 * was totally stopped, or > 0 if correctly paused.
1081 */
1082int tcp_pause_listener(struct listener *l)
1083{
1084 if (shutdown(l->fd, SHUT_WR) != 0)
1085 return -1; /* Solaris dies here */
1086
1087 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1088 return -1; /* OpenBSD dies here */
1089
1090 if (shutdown(l->fd, SHUT_RD) != 0)
1091 return -1; /* should always be OK */
1092 return 1;
1093}
1094
Willy Tarreauedcf6682008-11-30 23:15:34 +01001095/* This function performs the TCP request analysis on the current request. It
1096 * returns 1 if the processing can continue on next analysers, or zero if it
1097 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +02001098 * request. It relies on buffers flags, and updates s->req->analysers. The
1099 * function may be called for frontend rules and backend rules. It only relies
1100 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001101 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001102int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +01001103{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001104 struct session *sess = s->sess;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001105 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +02001106 struct stksess *ts;
1107 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001108 int partial;
1109
Willy Tarreau87b09662015-04-03 00:22:06 +02001110 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 +01001111 now_ms, __FUNCTION__,
1112 s,
1113 req,
1114 req->rex, req->wex,
1115 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001116 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +01001117 req->analysers);
1118
Willy Tarreauedcf6682008-11-30 23:15:34 +01001119 /* We don't know whether we have enough data, so must proceed
1120 * this way :
1121 * - iterate through all rules in their declaration order
1122 * - if one rule returns MISS, it means the inspect delay is
1123 * not over yet, then return immediately, otherwise consider
1124 * it as a non-match.
1125 * - if one rule returns OK, then return OK
1126 * - if one rule returns KO, then return KO
1127 */
1128
Willy Tarreau9b28e032012-10-12 23:49:43 +02001129 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001130 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001131 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001132 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001133 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001134
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001135 /* If "the current_rule_list" match the executed rule list, we are in
1136 * resume condition. If a resume is needed it is always in the action
1137 * and never in the ACL or converters. In this case, we initialise the
1138 * current rule, and go to the action execution point.
1139 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001140 if (s->current_rule) {
1141 rule = s->current_rule;
1142 s->current_rule = NULL;
1143 if (s->current_rule_list == &s->be->tcp_req.inspect_rules)
1144 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001145 }
1146 s->current_rule_list = &s->be->tcp_req.inspect_rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001147
Willy Tarreaufb356202010-08-03 14:02:05 +02001148 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001149 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001150
1151 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001152 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001153 if (ret == ACL_TEST_MISS)
1154 goto missing_data;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001155
1156 ret = acl_pass(ret);
1157 if (rule->cond->pol == ACL_COND_UNLESS)
1158 ret = !ret;
1159 }
1160
1161 if (ret) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001162resume_execution:
Willy Tarreauedcf6682008-11-30 23:15:34 +01001163 /* we have a matching rule. */
Willy Tarreau27f78242015-07-04 11:36:30 +02001164 if (rule->action == TCP_ACT_ACCEPT) {
1165 break;
1166 }
1167 else if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001168 channel_abort(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001169 channel_abort(&s->res);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001170 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001171
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001172 s->be->be_counters.denied_req++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001173 sess->fe->fe_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001174 if (sess->listener->counters)
1175 sess->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001176
Willy Tarreaue7dff022015-04-03 01:14:29 +02001177 if (!(s->flags & SF_ERR_MASK))
1178 s->flags |= SF_ERR_PRXCOND;
1179 if (!(s->flags & SF_FINST_MASK))
1180 s->flags |= SF_FINST_R;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001181 return 0;
1182 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001183 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001184 /* Note: only the first valid tracking parameter of each
1185 * applies.
1186 */
1187 struct stktable_key *key;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001188 struct sample smp;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001189
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001190 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001191 continue;
1192
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001193 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02001194 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | partial, rule->act_prm.trk_ctr.expr, &smp);
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001195
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001196 if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL))
1197 goto missing_data; /* key might appear later */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001198
1199 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001200 stream_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001201 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001202 if (sess->fe != s->be)
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001203 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreaud1f96522010-08-03 19:34:32 +02001204 }
1205 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001206 else if (rule->action == TCP_ACT_CAPTURE) {
1207 struct sample *key;
1208 struct cap_hdr *h = rule->act_prm.cap.hdr;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001209 char **cap = s->req_cap;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001210 int len;
1211
Adis Nezirovic79beb242015-07-06 15:41:02 +02001212 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ | partial, rule->act_prm.cap.expr, SMP_T_STR);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001213 if (!key)
1214 continue;
1215
1216 if (key->flags & SMP_F_MAY_CHANGE)
1217 goto missing_data;
1218
1219 if (cap[h->index] == NULL)
1220 cap[h->index] = pool_alloc2(h->pool);
1221
1222 if (cap[h->index] == NULL) /* no more capture memory */
1223 continue;
1224
1225 len = key->data.str.len;
1226 if (len > h->len)
1227 len = h->len;
1228
1229 memcpy(cap[h->index], key->data.str.str, len);
1230 cap[h->index][len] = 0;
1231 }
Willy Tarreaud1f96522010-08-03 19:34:32 +02001232 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001233 /* Custom keywords. */
Thierry FOURNIER422a3af2015-08-10 18:30:18 +02001234 if (rule->action_ptr && !rule->action_ptr(rule, s->be, s->sess, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02001235 s->current_rule = rule;
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001236 goto missing_data;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001237 }
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001238
Thierry FOURNIER561a0f92015-05-29 17:29:14 +02001239 /* accept */
1240 if (rule->action == TCP_ACT_CUSTOM)
1241 break;
1242 /* otherwise continue */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001243 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001244 }
1245 }
1246
1247 /* if we get there, it means we have no rule which matches, or
1248 * we have an explicit accept, so we apply the default accept.
1249 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001250 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001251 req->analyse_exp = TICK_ETERNITY;
1252 return 1;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001253
1254 missing_data:
1255 channel_dont_connect(req);
1256 /* just set the request timeout once at the beginning of the request */
1257 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
1258 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
1259 return 0;
1260
Willy Tarreauedcf6682008-11-30 23:15:34 +01001261}
1262
Emeric Brun97679e72010-09-23 17:56:44 +02001263/* This function performs the TCP response analysis on the current response. It
1264 * returns 1 if the processing can continue on next analysers, or zero if it
1265 * needs more data, encounters an error, or wants to immediately abort the
1266 * response. It relies on buffers flags, and updates s->rep->analysers. The
1267 * function may be called for backend rules.
1268 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001269int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001270{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001271 struct session *sess = s->sess;
Emeric Brun97679e72010-09-23 17:56:44 +02001272 struct tcp_rule *rule;
1273 int partial;
1274
Willy Tarreau87b09662015-04-03 00:22:06 +02001275 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 +02001276 now_ms, __FUNCTION__,
1277 s,
1278 rep,
1279 rep->rex, rep->wex,
1280 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001281 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001282 rep->analysers);
1283
1284 /* We don't know whether we have enough data, so must proceed
1285 * this way :
1286 * - iterate through all rules in their declaration order
1287 * - if one rule returns MISS, it means the inspect delay is
1288 * not over yet, then return immediately, otherwise consider
1289 * it as a non-match.
1290 * - if one rule returns OK, then return OK
1291 * - if one rule returns KO, then return KO
1292 */
1293
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001294 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001295 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001296 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001297 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001298
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001299 /* If "the current_rule_list" match the executed rule list, we are in
1300 * resume condition. If a resume is needed it is always in the action
1301 * and never in the ACL or converters. In this case, we initialise the
1302 * current rule, and go to the action execution point.
1303 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001304 if (s->current_rule) {
1305 rule = s->current_rule;
1306 s->current_rule = NULL;
1307 if (s->current_rule_list == &s->be->tcp_rep.inspect_rules)
1308 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001309 }
1310 s->current_rule_list = &s->be->tcp_rep.inspect_rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001311
Emeric Brun97679e72010-09-23 17:56:44 +02001312 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001313 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001314
1315 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001316 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001317 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001318 /* just set the analyser timeout once at the beginning of the response */
1319 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001320 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001321 return 0;
1322 }
1323
1324 ret = acl_pass(ret);
1325 if (rule->cond->pol == ACL_COND_UNLESS)
1326 ret = !ret;
1327 }
1328
1329 if (ret) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001330resume_execution:
Emeric Brun97679e72010-09-23 17:56:44 +02001331 /* we have a matching rule. */
Willy Tarreau27f78242015-07-04 11:36:30 +02001332 if (rule->action == TCP_ACT_ACCEPT) {
1333 break;
1334 }
1335 else if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001336 channel_abort(rep);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001337 channel_abort(&s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001338 rep->analysers = 0;
1339
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001340 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001341 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001342 if (sess->listener->counters)
1343 sess->listener->counters->denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001344
Willy Tarreaue7dff022015-04-03 01:14:29 +02001345 if (!(s->flags & SF_ERR_MASK))
1346 s->flags |= SF_ERR_PRXCOND;
1347 if (!(s->flags & SF_FINST_MASK))
1348 s->flags |= SF_FINST_D;
Emeric Brun97679e72010-09-23 17:56:44 +02001349 return 0;
1350 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001351 else if (rule->action == TCP_ACT_CLOSE) {
Willy Tarreau73796532014-11-28 14:10:28 +01001352 chn_prod(rep)->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1353 si_shutr(chn_prod(rep));
1354 si_shutw(chn_prod(rep));
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001355 break;
1356 }
Emeric Brun97679e72010-09-23 17:56:44 +02001357 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001358 /* Custom keywords. */
Thierry FOURNIER422a3af2015-08-10 18:30:18 +02001359 if (rule->action_ptr && !rule->action_ptr(rule, s->be, s->sess, s)) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001360 channel_dont_close(rep);
Willy Tarreau152b81e2015-04-20 13:26:17 +02001361 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001362 return 0;
1363 }
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001364
Thierry FOURNIER561a0f92015-05-29 17:29:14 +02001365 /* accept */
1366 if (rule->action == TCP_ACT_CUSTOM)
1367 break;
1368 /* otherwise continue */
Emeric Brun97679e72010-09-23 17:56:44 +02001369 }
1370 }
1371 }
1372
1373 /* if we get there, it means we have no rule which matches, or
1374 * we have an explicit accept, so we apply the default accept.
1375 */
1376 rep->analysers &= ~an_bit;
1377 rep->analyse_exp = TICK_ETERNITY;
1378 return 1;
1379}
1380
1381
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001382/* This function performs the TCP layer4 analysis on the current request. It
1383 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1384 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001385 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001386 */
Willy Tarreaue73ef852015-04-04 16:41:45 +02001387int tcp_exec_req_rules(struct session *sess)
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001388{
1389 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001390 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001391 struct stktable *t = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001392 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001393 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001394 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001395
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001396 if (!conn)
1397 return result;
1398
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001399 list_for_each_entry(rule, &sess->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001400 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001401
1402 if (rule->cond) {
Willy Tarreaue73ef852015-04-04 16:41:45 +02001403 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001404 ret = acl_pass(ret);
1405 if (rule->cond->pol == ACL_COND_UNLESS)
1406 ret = !ret;
1407 }
1408
1409 if (ret) {
1410 /* we have a matching rule. */
Willy Tarreau27f78242015-07-04 11:36:30 +02001411 if (rule->action == TCP_ACT_ACCEPT) {
1412 break;
1413 }
1414 else if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001415 sess->fe->fe_counters.denied_conn++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001416 if (sess->listener->counters)
1417 sess->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001418
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001419 result = 0;
1420 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001421 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001422 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001423 /* Note: only the first valid tracking parameter of each
1424 * applies.
1425 */
1426 struct stktable_key *key;
1427
Willy Tarreau70f454e2015-04-04 16:38:07 +02001428 if (stkctr_entry(&sess->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001429 continue;
1430
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001431 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreaue73ef852015-04-04 16:41:45 +02001432 key = stktable_fetch_key(t, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001433
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001434 if (key && (ts = stktable_get_entry(t, key)))
Willy Tarreau70f454e2015-04-04 16:38:07 +02001435 stream_track_stkctr(&sess->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001436 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001437 else if (rule->action == TCP_ACT_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001438 conn->flags |= CO_FL_ACCEPT_PROXY;
1439 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001440 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001441 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001442 /* Custom keywords. */
Thierry FOURNIERc89f4f52015-08-11 09:48:02 +02001443 if (rule->action_ptr) {
Thierry FOURNIER422a3af2015-08-10 18:30:18 +02001444 rule->action_ptr(rule, sess->fe, sess, NULL);
Thierry FOURNIERc89f4f52015-08-11 09:48:02 +02001445 if (rule->action == TCP_ACT_CUSTOM_CONT)
1446 continue;
1447 }
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001448
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001449 /* otherwise it's an accept */
1450 break;
1451 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001452 }
1453 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001454 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001455}
1456
Emeric Brun97679e72010-09-23 17:56:44 +02001457/* Parse a tcp-response rule. Return a negative value in case of failure */
1458static int tcp_parse_response_rule(char **args, int arg, int section_type,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001459 struct proxy *curpx, struct proxy *defpx,
1460 struct tcp_rule *rule, char **err,
1461 unsigned int where,
1462 const char *file, int line)
Emeric Brun97679e72010-09-23 17:56:44 +02001463{
1464 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001465 memprintf(err, "%s %s is only allowed in 'backend' sections",
1466 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001467 return -1;
1468 }
1469
1470 if (strcmp(args[arg], "accept") == 0) {
1471 arg++;
1472 rule->action = TCP_ACT_ACCEPT;
1473 }
1474 else if (strcmp(args[arg], "reject") == 0) {
1475 arg++;
1476 rule->action = TCP_ACT_REJECT;
1477 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001478 else if (strcmp(args[arg], "close") == 0) {
1479 arg++;
1480 rule->action = TCP_ACT_CLOSE;
1481 }
Emeric Brun97679e72010-09-23 17:56:44 +02001482 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001483 struct tcp_action_kw *kw;
1484 kw = tcp_res_cont_action(args[arg]);
1485 if (kw) {
1486 arg++;
1487 if (!kw->parse((const char **)args, &arg, curpx, rule, err))
1488 return -1;
1489 } else {
1490 memprintf(err,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001491 "'%s %s' expects 'accept', 'close', 'reject' or 'set-var' in %s '%s' (got '%s')",
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001492 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
1493 return -1;
1494 }
Emeric Brun97679e72010-09-23 17:56:44 +02001495 }
1496
1497 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001498 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001499 memprintf(err,
1500 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1501 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001502 return -1;
1503 }
1504 }
1505 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001506 memprintf(err,
1507 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001508 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1509 return -1;
1510 }
1511 return 0;
1512}
1513
1514
1515
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001516/* Parse a tcp-request rule. Return a negative value in case of failure */
1517static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001518 struct proxy *curpx, struct proxy *defpx,
1519 struct tcp_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001520 unsigned int where, const char *file, int line)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001521{
1522 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001523 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1524 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001525 return -1;
1526 }
1527
1528 if (!strcmp(args[arg], "accept")) {
1529 arg++;
1530 rule->action = TCP_ACT_ACCEPT;
1531 }
1532 else if (!strcmp(args[arg], "reject")) {
1533 arg++;
1534 rule->action = TCP_ACT_REJECT;
1535 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001536 else if (strcmp(args[arg], "capture") == 0) {
1537 struct sample_expr *expr;
1538 struct cap_hdr *hdr;
1539 int kw = arg;
1540 int len = 0;
1541
1542 if (!(curpx->cap & PR_CAP_FE)) {
1543 memprintf(err,
1544 "'%s %s %s' : proxy '%s' has no frontend capability",
1545 args[0], args[1], args[kw], curpx->id);
1546 return -1;
1547 }
1548
1549 if (!(where & SMP_VAL_FE_REQ_CNT)) {
1550 memprintf(err,
1551 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1552 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1553 return -1;
1554 }
1555
1556 arg++;
1557
1558 curpx->conf.args.ctx = ARGC_CAP;
1559 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
1560 if (!expr) {
1561 memprintf(err,
1562 "'%s %s %s' : %s",
1563 args[0], args[1], args[kw], *err);
1564 return -1;
1565 }
1566
1567 if (!(expr->fetch->val & where)) {
1568 memprintf(err,
1569 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
1570 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
1571 free(expr);
1572 return -1;
1573 }
1574
1575 if (strcmp(args[arg], "len") == 0) {
1576 arg++;
1577 if (!args[arg]) {
1578 memprintf(err,
1579 "'%s %s %s' : missing length value",
1580 args[0], args[1], args[kw]);
1581 free(expr);
1582 return -1;
1583 }
1584 /* we copy the table name for now, it will be resolved later */
1585 len = atoi(args[arg]);
1586 if (len <= 0) {
1587 memprintf(err,
1588 "'%s %s %s' : length must be > 0",
1589 args[0], args[1], args[kw]);
1590 free(expr);
1591 return -1;
1592 }
1593 arg++;
1594 }
1595
1596 if (!len) {
1597 memprintf(err,
1598 "'%s %s %s' : a positive 'len' argument is mandatory",
1599 args[0], args[1], args[kw]);
1600 free(expr);
1601 return -1;
1602 }
1603
1604 hdr = calloc(sizeof(struct cap_hdr), 1);
1605 hdr->next = curpx->req_cap;
1606 hdr->name = NULL; /* not a header capture */
1607 hdr->namelen = 0;
1608 hdr->len = len;
1609 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
1610 hdr->index = curpx->nb_req_cap++;
1611
1612 curpx->req_cap = hdr;
1613 curpx->to_log |= LW_REQHDR;
1614
1615 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
1616 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1617
1618 rule->act_prm.cap.expr = expr;
1619 rule->act_prm.cap.hdr = hdr;
1620 rule->action = TCP_ACT_CAPTURE;
1621 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001622 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1623 args[arg][9] == '\0' && args[arg][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02001624 args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001625 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001626 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001627
1628 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001629
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001630 curpx->conf.args.ctx = ARGC_TRK;
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001631 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001632 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001633 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001634 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001635 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001636 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001637 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001638
Willy Tarreau80aca902013-01-07 15:42:20 +01001639 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001640 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001641 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001642 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001643 free(expr);
1644 return -1;
1645 }
1646
1647 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001648 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001649
1650 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001651 arg++;
1652 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001653 memprintf(err,
1654 "'%s %s %s' : missing table name",
1655 args[0], args[1], args[kw]);
1656 free(expr);
1657 return -1;
1658 }
1659 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001660 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001661 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001662 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001663 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001664 rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001665 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001666 else if (strcmp(args[arg], "expect-proxy") == 0) {
1667 if (strcmp(args[arg+1], "layer4") != 0) {
1668 memprintf(err,
1669 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1670 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1671 return -1;
1672 }
1673
1674 if (!(where & SMP_VAL_FE_CON_ACC)) {
1675 memprintf(err,
1676 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1677 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1678 return -1;
1679 }
1680
1681 arg += 2;
1682 rule->action = TCP_ACT_EXPECT_PX;
1683 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001684 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001685 struct tcp_action_kw *kw;
1686 if (where & SMP_VAL_FE_CON_ACC)
1687 kw = tcp_req_conn_action(args[arg]);
1688 else
1689 kw = tcp_req_cont_action(args[arg]);
1690 if (kw) {
1691 arg++;
1692 if (!kw->parse((const char **)args, &arg, curpx, rule, err))
1693 return -1;
1694 } else {
1695 memprintf(err,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001696 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d', "
1697 " or 'set-var' in %s '%s' (got '%s')",
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001698 args[0], args[1], MAX_SESS_STKCTR-1, proxy_type_str(curpx),
1699 curpx->id, args[arg]);
1700 return -1;
1701 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001702 }
1703
1704 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001705 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001706 memprintf(err,
1707 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1708 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001709 return -1;
1710 }
1711 }
1712 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001713 memprintf(err,
1714 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001715 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1716 return -1;
1717 }
1718 return 0;
1719}
1720
Emeric Brun97679e72010-09-23 17:56:44 +02001721/* This function should be called to parse a line starting with the "tcp-response"
1722 * keyword.
1723 */
1724static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001725 struct proxy *defpx, const char *file, int line,
1726 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001727{
1728 const char *ptr = NULL;
1729 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001730 int warn = 0;
1731 int arg;
1732 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001733 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001734 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001735 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001736
1737 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001738 memprintf(err, "missing argument for '%s' in %s '%s'",
1739 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001740 return -1;
1741 }
1742
1743 if (strcmp(args[1], "inspect-delay") == 0) {
1744 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001745 memprintf(err, "%s %s is only allowed in 'backend' sections",
1746 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001747 return -1;
1748 }
1749
1750 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001751 memprintf(err,
1752 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1753 args[0], args[1], proxy_type_str(curpx), curpx->id);
1754 if (ptr)
1755 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001756 return -1;
1757 }
1758
1759 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001760 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1761 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001762 return 1;
1763 }
1764 curpx->tcp_rep.inspect_delay = val;
1765 return 0;
1766 }
1767
Simon Hormande072bd2011-06-24 15:11:37 +09001768 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001769 LIST_INIT(&rule->list);
1770 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001771 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001772
1773 if (strcmp(args[1], "content") == 0) {
1774 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001775
1776 if (curpx->cap & PR_CAP_FE)
1777 where |= SMP_VAL_FE_RES_CNT;
1778 if (curpx->cap & PR_CAP_BE)
1779 where |= SMP_VAL_BE_RES_CNT;
1780
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001781 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001782 goto error;
1783
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001784 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1785 if (acl) {
1786 if (acl->name && *acl->name)
1787 memprintf(err,
1788 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1789 acl->name, args[0], args[1], sample_ckp_names(where));
1790 else
1791 memprintf(err,
1792 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1793 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001794 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001795 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001796
Emeric Brun97679e72010-09-23 17:56:44 +02001797 warn++;
1798 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001799 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1800 if (acl->name && *acl->name)
1801 memprintf(err,
1802 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001803 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001804 else
1805 memprintf(err,
1806 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001807 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001808 warn++;
1809 }
Emeric Brun97679e72010-09-23 17:56:44 +02001810
1811 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1812 }
1813 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001814 memprintf(err,
1815 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1816 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001817 goto error;
1818 }
1819
1820 return warn;
1821 error:
1822 free(rule);
1823 return -1;
1824}
1825
1826
Willy Tarreaub6866442008-07-14 23:54:42 +02001827/* This function should be called to parse a line starting with the "tcp-request"
1828 * keyword.
1829 */
1830static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001831 struct proxy *defpx, const char *file, int line,
1832 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001833{
1834 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001835 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001836 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001837 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001838 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001839 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001840 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001841 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001842
1843 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001844 if (curpx == defpx)
1845 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1846 else
1847 memprintf(err, "missing argument for '%s' in %s '%s'",
1848 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001849 return -1;
1850 }
1851
1852 if (!strcmp(args[1], "inspect-delay")) {
1853 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001854 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1855 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001856 return -1;
1857 }
1858
Willy Tarreaub6866442008-07-14 23:54:42 +02001859 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001860 memprintf(err,
1861 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1862 args[0], args[1], proxy_type_str(curpx), curpx->id);
1863 if (ptr)
1864 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001865 return -1;
1866 }
1867
1868 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001869 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1870 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001871 return 1;
1872 }
1873 curpx->tcp_req.inspect_delay = val;
1874 return 0;
1875 }
1876
Simon Hormande072bd2011-06-24 15:11:37 +09001877 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001878 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001879 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001880 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001881
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001882 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001883 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001884
1885 if (curpx->cap & PR_CAP_FE)
1886 where |= SMP_VAL_FE_REQ_CNT;
1887 if (curpx->cap & PR_CAP_BE)
1888 where |= SMP_VAL_BE_REQ_CNT;
1889
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001890 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001891 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001892
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001893 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1894 if (acl) {
1895 if (acl->name && *acl->name)
1896 memprintf(err,
1897 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1898 acl->name, args[0], args[1], sample_ckp_names(where));
1899 else
1900 memprintf(err,
1901 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1902 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001903 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001904 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001905
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001906 warn++;
1907 }
1908 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1909 if (acl->name && *acl->name)
1910 memprintf(err,
1911 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001912 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001913 else
1914 memprintf(err,
1915 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001916 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001917 warn++;
1918 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001919
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001920 /* the following function directly emits the warning */
1921 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001922 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001923 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001924 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001925 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001926
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001927 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001928 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1929 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001930 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001931 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001932
Willy Tarreau80aca902013-01-07 15:42:20 +01001933 where |= SMP_VAL_FE_CON_ACC;
1934
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001935 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001936 goto error;
1937
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001938 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1939 if (acl) {
1940 if (acl->name && *acl->name)
1941 memprintf(err,
1942 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1943 acl->name, args[0], args[1], sample_ckp_names(where));
1944 else
1945 memprintf(err,
1946 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1947 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001948 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001949 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001950
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001951 warn++;
1952 }
1953 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1954 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001955 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001956 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001957 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001958 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001959 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001960 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001961 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001962 warn++;
1963 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001964
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001965 /* the following function directly emits the warning */
1966 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001967 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001968 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001969 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001970 if (curpx == defpx)
1971 memprintf(err,
1972 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1973 args[0], args[1]);
1974 else
1975 memprintf(err,
1976 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001977 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001978 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001979 }
1980
Willy Tarreau1a687942010-05-23 22:40:30 +02001981 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001982 error:
1983 free(rule);
1984 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001985}
1986
Willy Tarreau645513a2010-05-24 20:55:15 +02001987
1988/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001989/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001990/************************************************************************/
1991
Willy Tarreau4a129812012-04-25 17:31:42 +02001992/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001993int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001994{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001995 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001996
1997 if (!cli_conn)
1998 return 0;
1999
2000 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01002001 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002002 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02002003 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002004 break;
2005 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002006 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02002007 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002008 break;
2009 default:
Emeric Brunf769f512010-10-22 17:14:01 +02002010 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002011 }
Emeric Brunf769f512010-10-22 17:14:01 +02002012
Willy Tarreau37406352012-04-23 16:16:37 +02002013 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002014 return 1;
2015}
2016
Willy Tarreaua5e37562011-12-16 17:06:15 +01002017/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002018static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002019smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002020{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002021 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002022
2023 if (!cli_conn)
2024 return 0;
2025
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002026 smp->type = SMP_T_SINT;
2027 if (!(smp->data.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02002028 return 0;
2029
Willy Tarreau37406352012-04-23 16:16:37 +02002030 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002031 return 1;
2032}
2033
Willy Tarreau4a129812012-04-25 17:31:42 +02002034/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02002035static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002036smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002037{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002038 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02002039
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002040 if (!cli_conn)
2041 return 0;
2042
2043 conn_get_to_addr(cli_conn);
2044
2045 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01002046 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002047 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02002048 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002049 break;
2050 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002051 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02002052 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002053 break;
2054 default:
Emeric Brunf769f512010-10-22 17:14:01 +02002055 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002056 }
Emeric Brunf769f512010-10-22 17:14:01 +02002057
Willy Tarreau37406352012-04-23 16:16:37 +02002058 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002059 return 1;
2060}
2061
Willy Tarreaua5e37562011-12-16 17:06:15 +01002062/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002063static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002064smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002065{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002066 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002067
2068 if (!cli_conn)
2069 return 0;
2070
2071 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02002072
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002073 smp->type = SMP_T_SINT;
2074 if (!(smp->data.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02002075 return 0;
2076
Willy Tarreau37406352012-04-23 16:16:37 +02002077 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002078 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01002079}
2080
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002081#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002082/* parse the "v4v6" bind keyword */
2083static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2084{
2085 struct listener *l;
2086
2087 list_for_each_entry(l, &conf->listeners, by_bind) {
2088 if (l->addr.ss_family == AF_INET6)
2089 l->options |= LI_O_V4V6;
2090 }
2091
2092 return 0;
2093}
2094
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002095/* parse the "v6only" bind keyword */
2096static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2097{
2098 struct listener *l;
2099
2100 list_for_each_entry(l, &conf->listeners, by_bind) {
2101 if (l->addr.ss_family == AF_INET6)
2102 l->options |= LI_O_V6ONLY;
2103 }
2104
2105 return 0;
2106}
2107#endif
2108
Pieter Baauwd551fb52013-05-08 22:49:23 +02002109#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002110/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002111static 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 +02002112{
2113 struct listener *l;
2114
Willy Tarreau4348fad2012-09-20 16:48:07 +02002115 list_for_each_entry(l, &conf->listeners, by_bind) {
2116 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2117 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02002118 }
2119
Willy Tarreau44791242012-09-12 23:27:21 +02002120 return 0;
2121}
2122#endif
2123
2124#ifdef TCP_DEFER_ACCEPT
2125/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002126static 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 +02002127{
2128 struct listener *l;
2129
Willy Tarreau4348fad2012-09-20 16:48:07 +02002130 list_for_each_entry(l, &conf->listeners, by_bind) {
2131 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2132 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02002133 }
2134
Willy Tarreau44791242012-09-12 23:27:21 +02002135 return 0;
2136}
2137#endif
2138
Willy Tarreau1c862c52012-10-05 16:21:00 +02002139#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01002140/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02002141static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2142{
2143 struct listener *l;
2144
2145 list_for_each_entry(l, &conf->listeners, by_bind) {
2146 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2147 l->options |= LI_O_TCP_FO;
2148 }
2149
2150 return 0;
2151}
2152#endif
2153
Willy Tarreau44791242012-09-12 23:27:21 +02002154#ifdef TCP_MAXSEG
2155/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002156static 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 +02002157{
2158 struct listener *l;
2159 int mss;
2160
Willy Tarreau44791242012-09-12 23:27:21 +02002161 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002162 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002163 return ERR_ALERT | ERR_FATAL;
2164 }
2165
2166 mss = atoi(args[cur_arg + 1]);
2167 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002168 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002169 return ERR_ALERT | ERR_FATAL;
2170 }
2171
Willy Tarreau4348fad2012-09-20 16:48:07 +02002172 list_for_each_entry(l, &conf->listeners, by_bind) {
2173 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2174 l->maxseg = mss;
2175 }
Willy Tarreau44791242012-09-12 23:27:21 +02002176
2177 return 0;
2178}
2179#endif
2180
Willy Tarreau2af207a2015-02-04 00:45:58 +01002181#ifdef TCP_USER_TIMEOUT
2182/* parse the "tcp-ut" bind keyword */
2183static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2184{
2185 const char *ptr = NULL;
2186 struct listener *l;
2187 unsigned int timeout;
2188
2189 if (!*args[cur_arg + 1]) {
2190 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
2191 return ERR_ALERT | ERR_FATAL;
2192 }
2193
2194 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
2195 if (ptr) {
2196 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
2197 return ERR_ALERT | ERR_FATAL;
2198 }
2199
2200 list_for_each_entry(l, &conf->listeners, by_bind) {
2201 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2202 l->tcp_ut = timeout;
2203 }
2204
2205 return 0;
2206}
2207#endif
2208
Willy Tarreau44791242012-09-12 23:27:21 +02002209#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01002210/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002211static 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 +02002212{
2213 struct listener *l;
2214
Willy Tarreau44791242012-09-12 23:27:21 +02002215 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002216 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002217 return ERR_ALERT | ERR_FATAL;
2218 }
2219
Willy Tarreau4348fad2012-09-20 16:48:07 +02002220 list_for_each_entry(l, &conf->listeners, by_bind) {
2221 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2222 l->interface = strdup(args[cur_arg + 1]);
2223 }
Willy Tarreau44791242012-09-12 23:27:21 +02002224
2225 global.last_checks |= LSTCHK_NETADM;
2226 return 0;
2227}
2228#endif
2229
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002230#ifdef CONFIG_HAP_NS
2231/* parse the "namespace" bind keyword */
2232static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2233{
2234 struct listener *l;
2235 char *namespace = NULL;
2236
2237 if (!*args[cur_arg + 1]) {
2238 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
2239 return ERR_ALERT | ERR_FATAL;
2240 }
2241 namespace = args[cur_arg + 1];
2242
2243 list_for_each_entry(l, &conf->listeners, by_bind) {
2244 l->netns = netns_store_lookup(namespace, strlen(namespace));
2245
2246 if (l->netns == NULL)
2247 l->netns = netns_store_insert(namespace);
2248
2249 if (l->netns == NULL) {
2250 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
2251 return ERR_ALERT | ERR_FATAL;
2252 }
2253 }
2254 return 0;
2255}
2256#endif
2257
Willy Tarreaudc13c112013-06-21 23:16:39 +02002258static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002259 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02002260 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02002261 { 0, NULL, NULL },
2262}};
2263
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002264
Willy Tarreau61612d42012-04-19 18:42:05 +02002265/* Note: must not be declared <const> as its list will be overwritten.
2266 * Please take care of keeping this list alphabetically sorted.
2267 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002268static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002269 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02002270}};
2271
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002272
Willy Tarreau4a129812012-04-25 17:31:42 +02002273/* Note: must not be declared <const> as its list will be overwritten.
2274 * Note: fetches that may return multiple types must be declared as the lowest
2275 * common denominator, the type that can be casted into all other ones. For
2276 * instance v4/v6 must be declared v4.
2277 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002278static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002279 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002280 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002281 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002282 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002283 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002284}};
2285
Willy Tarreau44791242012-09-12 23:27:21 +02002286/************************************************************************/
2287/* All supported bind keywords must be declared here. */
2288/************************************************************************/
2289
2290/* Note: must not be declared <const> as its list will be overwritten.
2291 * Please take care of keeping this list alphabetically sorted, doing so helps
2292 * all code contributors.
2293 * Optional keywords are also declared with a NULL ->parse() function so that
2294 * the config parser can report an appropriate error when a known keyword was
2295 * not enabled.
2296 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002297static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02002298#ifdef TCP_DEFER_ACCEPT
2299 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
2300#endif
2301#ifdef SO_BINDTODEVICE
2302 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
2303#endif
2304#ifdef TCP_MAXSEG
2305 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
2306#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01002307#ifdef TCP_USER_TIMEOUT
2308 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
2309#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02002310#ifdef TCP_FASTOPEN
2311 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
2312#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02002313#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002314 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
2315#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002316#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002317 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002318 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
2319#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002320#ifdef CONFIG_HAP_NS
2321 { "namespace", bind_parse_namespace, 1 },
2322#endif
Willy Tarreau44791242012-09-12 23:27:21 +02002323 /* the versions with the NULL parse function*/
2324 { "defer-accept", NULL, 0 },
2325 { "interface", NULL, 1 },
2326 { "mss", NULL, 1 },
2327 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002328 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002329 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002330 { NULL, NULL, 0 },
2331}};
2332
Willy Tarreaue6b98942007-10-29 01:09:36 +01002333__attribute__((constructor))
2334static void __tcp_protocol_init(void)
2335{
2336 protocol_register(&proto_tcpv4);
2337 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002338 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02002339 cfg_register_keywords(&cfg_kws);
2340 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02002341 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002342}
2343
2344
2345/*
2346 * Local variables:
2347 * c-indent-level: 8
2348 * c-basic-offset: 8
2349 * End:
2350 */