blob: 737a32ee0920cdf050100caf52b4c2e79c9f13b7 [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_ADDR:
478 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200479 flags = 3;
480 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100481 case CO_SRC_TPROXY_CIP:
482 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200483 flags = 1;
484 break;
485 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200486 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200487
Willy Tarreau9650f372009-08-16 14:02:45 +0200488#ifdef SO_BINDTODEVICE
489 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100490 if (src->iface_name)
491 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200492#endif
493
Willy Tarreaua4380b42012-12-08 22:49:11 +0100494 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200495 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100496 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200497
498 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100499 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200500
501 do {
502 /* note: in case of retry, we may have to release a previously
503 * allocated port, hence this loop's construct.
504 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200505 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
506 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200507
508 if (!attempts)
509 break;
510 attempts--;
511
Willy Tarreaua4380b42012-12-08 22:49:11 +0100512 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100513 if (!fdinfo[fd].local_port) {
514 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200515 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100516 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200517
Willy Tarreaua4380b42012-12-08 22:49:11 +0100518 fdinfo[fd].port_range = src->sport_range;
519 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200520
Willy Tarreaua4380b42012-12-08 22:49:11 +0100521 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100522 if (ret != 0)
523 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200524 } while (ret != 0); /* binding NOK */
525 }
526 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100527 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100528 if (ret != 0)
529 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200530 }
531
Willy Tarreaua4380b42012-12-08 22:49:11 +0100532 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200533 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
534 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200535 close(fd);
536
Willy Tarreau9650f372009-08-16 14:02:45 +0200537 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100538 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200539 be->id);
540 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100541 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200542 be->id);
543 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100544 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200545 be->id);
546 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100547 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200548 be->id);
549 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100550 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200551 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200552 }
553 }
554
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400555#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200556 /* disabling tcp quick ack now allows the first request to leave the
557 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100558 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200559 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200560 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900561 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200562#endif
563
Willy Tarreaue803de22010-01-21 17:43:04 +0100564 if (global.tune.server_sndbuf)
565 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
566
567 if (global.tune.server_rcvbuf)
568 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
569
Willy Tarreau986a9d22012-08-30 21:11:38 +0200570 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200571 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
572
Willy Tarreaub1719512012-12-08 23:03:28 +0100573 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200574 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100575 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200576 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100577 conn->err_code = CO_ER_FREE_PORTS;
578 }
579 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200580 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100581 conn->err_code = CO_ER_ADDR_INUSE;
582 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200583
Willy Tarreaub1719512012-12-08 23:03:28 +0100584 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200585 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
586 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200587 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100588 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100589 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200590 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200591 } else if (errno == ETIMEDOUT) {
592 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200593 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
594 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200595 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100596 conn->err_code = CO_ER_SOCK_ERR;
597 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200598 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200599 } else {
600 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
601 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200602 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
603 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200604 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100605 conn->err_code = CO_ER_SOCK_ERR;
606 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200607 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200608 }
609 }
610
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100611 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200612
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200613 /* Prepare to send a few handshakes related to the on-wire protocol. */
614 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200615 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200616
Willy Tarreauf79c8172013-10-21 16:30:56 +0200617 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100618 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200619 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200620
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200621 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200622 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100623 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200624 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200625 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200626
Willy Tarreau14f8e862012-08-30 22:23:13 +0200627 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200628 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200629
Willy Tarreaue7dff022015-04-03 01:14:29 +0200630 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200631}
632
633
Willy Tarreau59b94792012-05-11 16:16:40 +0200634/*
635 * Retrieves the source address for the socket <fd>, with <dir> indicating
636 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
637 * success, -1 in case of error. The socket's source address is stored in
638 * <sa> for <salen> bytes.
639 */
640int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
641{
642 if (dir)
643 return getsockname(fd, sa, &salen);
644 else
645 return getpeername(fd, sa, &salen);
646}
647
648
649/*
650 * Retrieves the original destination address for the socket <fd>, with <dir>
651 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
652 * listener, if the original destination address was translated, the original
653 * address is retrieved. It returns 0 in case of success, -1 in case of error.
654 * The socket's source address is stored in <sa> for <salen> bytes.
655 */
656int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
657{
658 if (dir)
659 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100660 else {
661 int ret = getsockname(fd, sa, &salen);
662
663 if (ret < 0)
664 return ret;
665
Willy Tarreau59b94792012-05-11 16:16:40 +0200666#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100667 /* For TPROXY and Netfilter's NAT, we can retrieve the original
668 * IPv4 address before DNAT/REDIRECT. We must not do that with
669 * other families because v6-mapped IPv4 addresses are still
670 * reported as v4.
671 */
672 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
673 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
674 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200675#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100676 return ret;
677 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200678}
679
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200680/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100681 * receive shutdown. Returns positive if the shutdown was found, negative
682 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
683 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200684 */
685int tcp_drain(int fd)
686{
687 int turns = 2;
688 int len;
689
690 while (turns) {
691#ifdef MSG_TRUNC_CLEARS_INPUT
692 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
693 if (len == -1 && errno == EFAULT)
694#endif
695 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
696
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100697 if (len == 0) {
698 /* cool, shutdown received */
699 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200700 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100701 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200702
703 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100704 if (errno == EAGAIN) {
705 /* connection not closed yet */
706 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100707 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100708 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200709 if (errno == EINTR) /* oops, try again */
710 continue;
711 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100712 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200713 return 1;
714 }
715 /* OK we read some data, let's try again once */
716 turns--;
717 }
718 /* some data are still present, give up */
719 return 0;
720}
721
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200722/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100723 * and we have nothing to send. It updates the FD polling status. It returns 0
724 * if it fails in a fatal way or needs to poll to go further, otherwise it
725 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
726 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
727 * errno. The error checking is done in two passes in order to limit the number
728 * of syscalls in the normal case :
729 * - if POLL_ERR was reported by the poller, we check for a pending error on
730 * the socket before proceeding. If found, it's assigned to errno so that
731 * upper layers can see it.
732 * - otherwise connect() is used to check the connection state again, since
733 * the getsockopt return cannot reliably be used to know if the connection
734 * is still pending or ready. This one may often return an error as well,
735 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200736 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200737int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200738{
Willy Tarreau239d7182012-07-23 18:53:03 +0200739 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100740 socklen_t lskerr;
741 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200742
Willy Tarreau80184712012-07-06 14:54:49 +0200743 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200744 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200745
Willy Tarreau3c728722014-01-23 13:50:42 +0100746 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200747 return 0;
748
Willy Tarreau80184712012-07-06 14:54:49 +0200749 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200750 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200751
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100752 if (!fd_send_ready(fd))
753 return 0;
754
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100755 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
756 * without FD_POLL_IN also indicates a hangup without input data meaning
757 * there was no connection.
758 */
759 if (fdtab[fd].ev & FD_POLL_ERR ||
760 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
761 skerr = 0;
762 lskerr = sizeof(skerr);
763 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
764 errno = skerr;
765 if (errno == EAGAIN)
766 errno = 0;
767 if (errno)
768 goto out_error;
769 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200770
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100771 /* Use connect() to check the state of the socket. This has the
772 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200773 * - error
774 * - connecting (EALREADY, EINPROGRESS)
775 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200776 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200777 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200778 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100779 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100780 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200781 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200782 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200783
Willy Tarreau2c6be842012-07-06 17:12:34 +0200784 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200785 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200786
Willy Tarreau2c6be842012-07-06 17:12:34 +0200787 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200788 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200789
Willy Tarreau076be252012-07-06 16:02:29 +0200790 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200791 * forward the event to the transport layer which will notify the
792 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200793 */
Willy Tarreau80184712012-07-06 14:54:49 +0200794 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200795 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200796
797 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200798 /* Write error on the file descriptor. Report it to the connection
799 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200800 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100801 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100802 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100803 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200804 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200805}
806
Willy Tarreau59b94792012-05-11 16:16:40 +0200807
Willy Tarreaue6b98942007-10-29 01:09:36 +0100808/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100809 * an error message in <errmsg> if the message is at most <errlen> bytes long
810 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
811 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100812 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
813 * was alright and that no message was returned. ERR_RETRYABLE means that an
814 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700815 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100816 * the meaning of the error, but just indicate that a message is present which
817 * should be displayed with the respective level. Last, ERR_ABORT indicates
818 * that it's pointless to try to start other listeners. No error message is
819 * returned if errlen is NULL.
820 */
821int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
822{
823 __label__ tcp_return, tcp_close_return;
824 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100825 int ext, ready;
826 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100827 const char *msg = NULL;
828
829 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100830 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100831 *errmsg = 0;
832
833 if (listener->state != LI_ASSIGNED)
834 return ERR_NONE; /* already bound */
835
836 err = ERR_NONE;
837
Willy Tarreau40aa0702013-03-10 23:51:38 +0100838 /* if the listener already has an fd assigned, then we were offered the
839 * fd by an external process (most likely the parent), and we don't want
840 * to create a new socket. However we still want to set a few flags on
841 * the socket.
842 */
843 fd = listener->fd;
844 ext = (fd >= 0);
845
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100846 if (!ext) {
847 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
848
849 if (fd == -1) {
850 err |= ERR_RETRYABLE | ERR_ALERT;
851 msg = "cannot create listening socket";
852 goto tcp_return;
853 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100854 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100855
Willy Tarreaue6b98942007-10-29 01:09:36 +0100856 if (fd >= global.maxsock) {
857 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
858 msg = "not enough free sockets (raise '-n' parameter)";
859 goto tcp_close_return;
860 }
861
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200862 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100863 err |= ERR_FATAL | ERR_ALERT;
864 msg = "cannot make socket non-blocking";
865 goto tcp_close_return;
866 }
867
Willy Tarreau40aa0702013-03-10 23:51:38 +0100868 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100869 /* not fatal but should be reported */
870 msg = "cannot do so_reuseaddr";
871 err |= ERR_ALERT;
872 }
873
874 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900875 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100876
Willy Tarreaue6b98942007-10-29 01:09:36 +0100877#ifdef SO_REUSEPORT
878 /* OpenBSD supports this. As it's present in old libc versions of Linux,
879 * it might return an error that we will silently ignore.
880 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100881 if (!ext)
882 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100883#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200884
Willy Tarreau40aa0702013-03-10 23:51:38 +0100885 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200886 switch (listener->addr.ss_family) {
887 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200888 if (1
889#if defined(IP_TRANSPARENT)
890 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
891#endif
892#if defined(IP_FREEBIND)
893 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
894#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200895#if defined(IP_BINDANY)
896 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
897#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200898#if defined(SO_BINDANY)
899 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
900#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200901 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200902 msg = "cannot make listening socket transparent";
903 err |= ERR_ALERT;
904 }
905 break;
906 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200907 if (1
908#if defined(IPV6_TRANSPARENT)
909 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
910#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100911#if defined(IP_FREEBIND)
912 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
913#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200914#if defined(IPV6_BINDANY)
915 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
916#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200917#if defined(SO_BINDANY)
918 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
919#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200920 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200921 msg = "cannot make listening socket transparent";
922 err |= ERR_ALERT;
923 }
924 break;
925 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100926 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200927
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100928#ifdef SO_BINDTODEVICE
929 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100930 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100931 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100932 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100933 msg = "cannot bind listener to device";
934 err |= ERR_WARN;
935 }
936 }
937#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400938#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100939 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400940 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200941 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
942 msg = "cannot set MSS";
943 err |= ERR_WARN;
944 }
945 }
946#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100947#if defined(TCP_USER_TIMEOUT)
948 if (listener->tcp_ut) {
949 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
950 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
951 msg = "cannot set TCP User Timeout";
952 err |= ERR_WARN;
953 }
954 }
955#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200956#if defined(TCP_DEFER_ACCEPT)
957 if (listener->options & LI_O_DEF_ACCEPT) {
958 /* defer accept by up to one second */
959 int accept_delay = 1;
960 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
961 msg = "cannot enable DEFER_ACCEPT";
962 err |= ERR_WARN;
963 }
964 }
965#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200966#if defined(TCP_FASTOPEN)
967 if (listener->options & LI_O_TCP_FO) {
968 /* TFO needs a queue length, let's use the configured backlog */
969 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
970 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
971 msg = "cannot enable TCP_FASTOPEN";
972 err |= ERR_WARN;
973 }
974 }
975#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100976#if defined(IPV6_V6ONLY)
977 if (listener->options & LI_O_V6ONLY)
978 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100979 else if (listener->options & LI_O_V4V6)
980 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100981#endif
982
Willy Tarreau40aa0702013-03-10 23:51:38 +0100983 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100984 err |= ERR_RETRYABLE | ERR_ALERT;
985 msg = "cannot bind socket";
986 goto tcp_close_return;
987 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100988
Willy Tarreau40aa0702013-03-10 23:51:38 +0100989 ready = 0;
990 ready_len = sizeof(ready);
991 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
992 ready = 0;
993
994 if (!(ext && ready) && /* only listen if not already done by external process */
995 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100996 err |= ERR_RETRYABLE | ERR_ALERT;
997 msg = "cannot listen to socket";
998 goto tcp_close_return;
999 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001000
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +04001001#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001002 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +09001003 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +02001004#endif
1005
Willy Tarreaue6b98942007-10-29 01:09:36 +01001006 /* the socket is ready */
1007 listener->fd = fd;
1008 listener->state = LI_LISTEN;
1009
Willy Tarreaueabf3132008-08-29 23:36:51 +02001010 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +02001011 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +02001012 fd_insert(fd);
1013
Willy Tarreaue6b98942007-10-29 01:09:36 +01001014 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001015 if (msg && errlen) {
1016 char pn[INET6_ADDRSTRLEN];
1017
Willy Tarreau631f01c2011-09-05 00:36:48 +02001018 addr_to_str(&listener->addr, pn, sizeof(pn));
1019 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +01001020 }
Willy Tarreaue6b98942007-10-29 01:09:36 +01001021 return err;
1022
1023 tcp_close_return:
1024 close(fd);
1025 goto tcp_return;
1026}
1027
1028/* This function creates all TCP sockets bound to the protocol entry <proto>.
1029 * It is intended to be used as the protocol's bind_all() function.
1030 * The sockets will be registered but not added to any fd_set, in order not to
1031 * loose them across the fork(). A call to enable_all_listeners() is needed
1032 * to complete initialization. The return value is composed from ERR_*.
1033 */
Emeric Bruncf20bf12010-10-22 16:06:11 +02001034static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001035{
1036 struct listener *listener;
1037 int err = ERR_NONE;
1038
1039 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +02001040 err |= tcp_bind_listener(listener, errmsg, errlen);
1041 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +01001042 break;
1043 }
1044
1045 return err;
1046}
1047
1048/* Add listener to the list of tcpv4 listeners. The listener's state
1049 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1050 * listeners is updated. This is the function to use to add a new listener.
1051 */
1052void tcpv4_add_listener(struct listener *listener)
1053{
1054 if (listener->state != LI_INIT)
1055 return;
1056 listener->state = LI_ASSIGNED;
1057 listener->proto = &proto_tcpv4;
1058 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
1059 proto_tcpv4.nb_listeners++;
1060}
1061
1062/* Add listener to the list of tcpv4 listeners. The listener's state
1063 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
1064 * listeners is updated. This is the function to use to add a new listener.
1065 */
1066void tcpv6_add_listener(struct listener *listener)
1067{
1068 if (listener->state != LI_INIT)
1069 return;
1070 listener->state = LI_ASSIGNED;
1071 listener->proto = &proto_tcpv6;
1072 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1073 proto_tcpv6.nb_listeners++;
1074}
1075
Willy Tarreau092d8652014-07-07 20:22:12 +02001076/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1077 * was totally stopped, or > 0 if correctly paused.
1078 */
1079int tcp_pause_listener(struct listener *l)
1080{
1081 if (shutdown(l->fd, SHUT_WR) != 0)
1082 return -1; /* Solaris dies here */
1083
1084 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1085 return -1; /* OpenBSD dies here */
1086
1087 if (shutdown(l->fd, SHUT_RD) != 0)
1088 return -1; /* should always be OK */
1089 return 1;
1090}
1091
Willy Tarreauedcf6682008-11-30 23:15:34 +01001092/* This function performs the TCP request analysis on the current request. It
1093 * returns 1 if the processing can continue on next analysers, or zero if it
1094 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +02001095 * request. It relies on buffers flags, and updates s->req->analysers. The
1096 * function may be called for frontend rules and backend rules. It only relies
1097 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001098 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001099int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +01001100{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001101 struct session *sess = s->sess;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001102 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +02001103 struct stksess *ts;
1104 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001105 int partial;
1106
Willy Tarreau87b09662015-04-03 00:22:06 +02001107 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 +01001108 now_ms, __FUNCTION__,
1109 s,
1110 req,
1111 req->rex, req->wex,
1112 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001113 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +01001114 req->analysers);
1115
Willy Tarreauedcf6682008-11-30 23:15:34 +01001116 /* We don't know whether we have enough data, so must proceed
1117 * this way :
1118 * - iterate through all rules in their declaration order
1119 * - if one rule returns MISS, it means the inspect delay is
1120 * not over yet, then return immediately, otherwise consider
1121 * it as a non-match.
1122 * - if one rule returns OK, then return OK
1123 * - if one rule returns KO, then return KO
1124 */
1125
Willy Tarreau9b28e032012-10-12 23:49:43 +02001126 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001127 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001128 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001129 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001130 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001131
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001132 /* If "the current_rule_list" match the executed rule list, we are in
1133 * resume condition. If a resume is needed it is always in the action
1134 * and never in the ACL or converters. In this case, we initialise the
1135 * current rule, and go to the action execution point.
1136 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001137 if (s->current_rule) {
1138 rule = s->current_rule;
1139 s->current_rule = NULL;
1140 if (s->current_rule_list == &s->be->tcp_req.inspect_rules)
1141 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001142 }
1143 s->current_rule_list = &s->be->tcp_req.inspect_rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001144
Willy Tarreaufb356202010-08-03 14:02:05 +02001145 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001146 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001147
1148 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001149 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001150 if (ret == ACL_TEST_MISS)
1151 goto missing_data;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001152
1153 ret = acl_pass(ret);
1154 if (rule->cond->pol == ACL_COND_UNLESS)
1155 ret = !ret;
1156 }
1157
1158 if (ret) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001159resume_execution:
Willy Tarreauedcf6682008-11-30 23:15:34 +01001160 /* we have a matching rule. */
1161 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001162 channel_abort(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001163 channel_abort(&s->res);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001164 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001165
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001166 s->be->be_counters.denied_req++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001167 sess->fe->fe_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001168 if (sess->listener->counters)
1169 sess->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001170
Willy Tarreaue7dff022015-04-03 01:14:29 +02001171 if (!(s->flags & SF_ERR_MASK))
1172 s->flags |= SF_ERR_PRXCOND;
1173 if (!(s->flags & SF_FINST_MASK))
1174 s->flags |= SF_FINST_R;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001175 return 0;
1176 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001177 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001178 /* Note: only the first valid tracking parameter of each
1179 * applies.
1180 */
1181 struct stktable_key *key;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001182 struct sample smp;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001183
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001184 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001185 continue;
1186
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001187 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02001188 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 +02001189
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001190 if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL))
1191 goto missing_data; /* key might appear later */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001192
1193 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001194 stream_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001195 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001196 if (sess->fe != s->be)
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001197 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreaud1f96522010-08-03 19:34:32 +02001198 }
1199 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001200 else if (rule->action == TCP_ACT_CAPTURE) {
1201 struct sample *key;
1202 struct cap_hdr *h = rule->act_prm.cap.hdr;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001203 char **cap = s->req_cap;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001204 int len;
1205
Willy Tarreau192252e2015-04-04 01:47:55 +02001206 key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_REQ | partial, rule->act_prm.cap.expr);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001207 if (!key)
1208 continue;
1209
1210 if (key->flags & SMP_F_MAY_CHANGE)
1211 goto missing_data;
1212
1213 if (cap[h->index] == NULL)
1214 cap[h->index] = pool_alloc2(h->pool);
1215
1216 if (cap[h->index] == NULL) /* no more capture memory */
1217 continue;
1218
1219 len = key->data.str.len;
1220 if (len > h->len)
1221 len = h->len;
1222
1223 memcpy(cap[h->index], key->data.str.str, len);
1224 cap[h->index][len] = 0;
1225 }
Willy Tarreaud1f96522010-08-03 19:34:32 +02001226 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001227 /* Custom keywords. */
Willy Tarreaue91ffd02015-04-24 10:10:53 +02001228 if (rule->action_ptr && !rule->action_ptr(rule, s->be, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02001229 s->current_rule = rule;
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001230 goto missing_data;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001231 }
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001232
Thierry FOURNIER561a0f92015-05-29 17:29:14 +02001233 /* accept */
1234 if (rule->action == TCP_ACT_CUSTOM)
1235 break;
1236 /* otherwise continue */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001237 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001238 }
1239 }
1240
1241 /* if we get there, it means we have no rule which matches, or
1242 * we have an explicit accept, so we apply the default accept.
1243 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001244 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001245 req->analyse_exp = TICK_ETERNITY;
1246 return 1;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001247
1248 missing_data:
1249 channel_dont_connect(req);
1250 /* just set the request timeout once at the beginning of the request */
1251 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
1252 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
1253 return 0;
1254
Willy Tarreauedcf6682008-11-30 23:15:34 +01001255}
1256
Emeric Brun97679e72010-09-23 17:56:44 +02001257/* This function performs the TCP response analysis on the current response. It
1258 * returns 1 if the processing can continue on next analysers, or zero if it
1259 * needs more data, encounters an error, or wants to immediately abort the
1260 * response. It relies on buffers flags, and updates s->rep->analysers. The
1261 * function may be called for backend rules.
1262 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001263int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001264{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001265 struct session *sess = s->sess;
Emeric Brun97679e72010-09-23 17:56:44 +02001266 struct tcp_rule *rule;
1267 int partial;
1268
Willy Tarreau87b09662015-04-03 00:22:06 +02001269 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 +02001270 now_ms, __FUNCTION__,
1271 s,
1272 rep,
1273 rep->rex, rep->wex,
1274 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001275 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001276 rep->analysers);
1277
1278 /* We don't know whether we have enough data, so must proceed
1279 * this way :
1280 * - iterate through all rules in their declaration order
1281 * - if one rule returns MISS, it means the inspect delay is
1282 * not over yet, then return immediately, otherwise consider
1283 * it as a non-match.
1284 * - if one rule returns OK, then return OK
1285 * - if one rule returns KO, then return KO
1286 */
1287
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001288 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001289 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001290 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001291 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001292
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001293 /* If "the current_rule_list" match the executed rule list, we are in
1294 * resume condition. If a resume is needed it is always in the action
1295 * and never in the ACL or converters. In this case, we initialise the
1296 * current rule, and go to the action execution point.
1297 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001298 if (s->current_rule) {
1299 rule = s->current_rule;
1300 s->current_rule = NULL;
1301 if (s->current_rule_list == &s->be->tcp_rep.inspect_rules)
1302 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001303 }
1304 s->current_rule_list = &s->be->tcp_rep.inspect_rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001305
Emeric Brun97679e72010-09-23 17:56:44 +02001306 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001307 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001308
1309 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001310 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001311 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001312 /* just set the analyser timeout once at the beginning of the response */
1313 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001314 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001315 return 0;
1316 }
1317
1318 ret = acl_pass(ret);
1319 if (rule->cond->pol == ACL_COND_UNLESS)
1320 ret = !ret;
1321 }
1322
1323 if (ret) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001324resume_execution:
Emeric Brun97679e72010-09-23 17:56:44 +02001325 /* we have a matching rule. */
1326 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001327 channel_abort(rep);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001328 channel_abort(&s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001329 rep->analysers = 0;
1330
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001331 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001332 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001333 if (sess->listener->counters)
1334 sess->listener->counters->denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001335
Willy Tarreaue7dff022015-04-03 01:14:29 +02001336 if (!(s->flags & SF_ERR_MASK))
1337 s->flags |= SF_ERR_PRXCOND;
1338 if (!(s->flags & SF_FINST_MASK))
1339 s->flags |= SF_FINST_D;
Emeric Brun97679e72010-09-23 17:56:44 +02001340 return 0;
1341 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001342 else if (rule->action == TCP_ACT_CLOSE) {
Willy Tarreau73796532014-11-28 14:10:28 +01001343 chn_prod(rep)->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1344 si_shutr(chn_prod(rep));
1345 si_shutw(chn_prod(rep));
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001346 break;
1347 }
Emeric Brun97679e72010-09-23 17:56:44 +02001348 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001349 /* Custom keywords. */
Willy Tarreaue91ffd02015-04-24 10:10:53 +02001350 if (rule->action_ptr && !rule->action_ptr(rule, s->be, s)) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001351 channel_dont_close(rep);
Willy Tarreau152b81e2015-04-20 13:26:17 +02001352 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001353 return 0;
1354 }
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001355
Thierry FOURNIER561a0f92015-05-29 17:29:14 +02001356 /* accept */
1357 if (rule->action == TCP_ACT_CUSTOM)
1358 break;
1359 /* otherwise continue */
Emeric Brun97679e72010-09-23 17:56:44 +02001360 }
1361 }
1362 }
1363
1364 /* if we get there, it means we have no rule which matches, or
1365 * we have an explicit accept, so we apply the default accept.
1366 */
1367 rep->analysers &= ~an_bit;
1368 rep->analyse_exp = TICK_ETERNITY;
1369 return 1;
1370}
1371
1372
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001373/* This function performs the TCP layer4 analysis on the current request. It
1374 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1375 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001376 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001377 */
Willy Tarreaue73ef852015-04-04 16:41:45 +02001378int tcp_exec_req_rules(struct session *sess)
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001379{
1380 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001381 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001382 struct stktable *t = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001383 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001384 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001385 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001386
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001387 if (!conn)
1388 return result;
1389
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001390 list_for_each_entry(rule, &sess->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001391 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001392
1393 if (rule->cond) {
Willy Tarreaue73ef852015-04-04 16:41:45 +02001394 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001395 ret = acl_pass(ret);
1396 if (rule->cond->pol == ACL_COND_UNLESS)
1397 ret = !ret;
1398 }
1399
1400 if (ret) {
1401 /* we have a matching rule. */
1402 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001403 sess->fe->fe_counters.denied_conn++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001404 if (sess->listener->counters)
1405 sess->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001406
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001407 result = 0;
1408 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001409 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001410 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001411 /* Note: only the first valid tracking parameter of each
1412 * applies.
1413 */
1414 struct stktable_key *key;
1415
Willy Tarreau70f454e2015-04-04 16:38:07 +02001416 if (stkctr_entry(&sess->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001417 continue;
1418
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001419 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreaue73ef852015-04-04 16:41:45 +02001420 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 +01001421
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001422 if (key && (ts = stktable_get_entry(t, key)))
Willy Tarreau70f454e2015-04-04 16:38:07 +02001423 stream_track_stkctr(&sess->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001424 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001425 else if (rule->action == TCP_ACT_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001426 conn->flags |= CO_FL_ACCEPT_PROXY;
1427 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001428 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001429 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001430 /* Custom keywords. */
Willy Tarreaue91ffd02015-04-24 10:10:53 +02001431 if (rule->action_ptr)
1432 rule->action_ptr(rule, sess->fe, NULL);
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001433
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001434 /* otherwise it's an accept */
1435 break;
1436 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001437 }
1438 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001439 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001440}
1441
Emeric Brun97679e72010-09-23 17:56:44 +02001442/* Parse a tcp-response rule. Return a negative value in case of failure */
1443static int tcp_parse_response_rule(char **args, int arg, int section_type,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001444 struct proxy *curpx, struct proxy *defpx,
1445 struct tcp_rule *rule, char **err,
1446 unsigned int where,
1447 const char *file, int line)
Emeric Brun97679e72010-09-23 17:56:44 +02001448{
1449 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001450 memprintf(err, "%s %s is only allowed in 'backend' sections",
1451 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001452 return -1;
1453 }
1454
1455 if (strcmp(args[arg], "accept") == 0) {
1456 arg++;
1457 rule->action = TCP_ACT_ACCEPT;
1458 }
1459 else if (strcmp(args[arg], "reject") == 0) {
1460 arg++;
1461 rule->action = TCP_ACT_REJECT;
1462 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001463 else if (strcmp(args[arg], "close") == 0) {
1464 arg++;
1465 rule->action = TCP_ACT_CLOSE;
1466 }
Emeric Brun97679e72010-09-23 17:56:44 +02001467 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001468 struct tcp_action_kw *kw;
1469 kw = tcp_res_cont_action(args[arg]);
1470 if (kw) {
1471 arg++;
1472 if (!kw->parse((const char **)args, &arg, curpx, rule, err))
1473 return -1;
1474 } else {
1475 memprintf(err,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001476 "'%s %s' expects 'accept', 'close', 'reject' or 'set-var' in %s '%s' (got '%s')",
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001477 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
1478 return -1;
1479 }
Emeric Brun97679e72010-09-23 17:56:44 +02001480 }
1481
1482 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001483 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001484 memprintf(err,
1485 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1486 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001487 return -1;
1488 }
1489 }
1490 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001491 memprintf(err,
1492 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001493 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1494 return -1;
1495 }
1496 return 0;
1497}
1498
1499
1500
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001501/* Parse a tcp-request rule. Return a negative value in case of failure */
1502static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001503 struct proxy *curpx, struct proxy *defpx,
1504 struct tcp_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001505 unsigned int where, const char *file, int line)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001506{
1507 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001508 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1509 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001510 return -1;
1511 }
1512
1513 if (!strcmp(args[arg], "accept")) {
1514 arg++;
1515 rule->action = TCP_ACT_ACCEPT;
1516 }
1517 else if (!strcmp(args[arg], "reject")) {
1518 arg++;
1519 rule->action = TCP_ACT_REJECT;
1520 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001521 else if (strcmp(args[arg], "capture") == 0) {
1522 struct sample_expr *expr;
1523 struct cap_hdr *hdr;
1524 int kw = arg;
1525 int len = 0;
1526
1527 if (!(curpx->cap & PR_CAP_FE)) {
1528 memprintf(err,
1529 "'%s %s %s' : proxy '%s' has no frontend capability",
1530 args[0], args[1], args[kw], curpx->id);
1531 return -1;
1532 }
1533
1534 if (!(where & SMP_VAL_FE_REQ_CNT)) {
1535 memprintf(err,
1536 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1537 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1538 return -1;
1539 }
1540
1541 arg++;
1542
1543 curpx->conf.args.ctx = ARGC_CAP;
1544 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
1545 if (!expr) {
1546 memprintf(err,
1547 "'%s %s %s' : %s",
1548 args[0], args[1], args[kw], *err);
1549 return -1;
1550 }
1551
1552 if (!(expr->fetch->val & where)) {
1553 memprintf(err,
1554 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
1555 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
1556 free(expr);
1557 return -1;
1558 }
1559
1560 if (strcmp(args[arg], "len") == 0) {
1561 arg++;
1562 if (!args[arg]) {
1563 memprintf(err,
1564 "'%s %s %s' : missing length value",
1565 args[0], args[1], args[kw]);
1566 free(expr);
1567 return -1;
1568 }
1569 /* we copy the table name for now, it will be resolved later */
1570 len = atoi(args[arg]);
1571 if (len <= 0) {
1572 memprintf(err,
1573 "'%s %s %s' : length must be > 0",
1574 args[0], args[1], args[kw]);
1575 free(expr);
1576 return -1;
1577 }
1578 arg++;
1579 }
1580
1581 if (!len) {
1582 memprintf(err,
1583 "'%s %s %s' : a positive 'len' argument is mandatory",
1584 args[0], args[1], args[kw]);
1585 free(expr);
1586 return -1;
1587 }
1588
1589 hdr = calloc(sizeof(struct cap_hdr), 1);
1590 hdr->next = curpx->req_cap;
1591 hdr->name = NULL; /* not a header capture */
1592 hdr->namelen = 0;
1593 hdr->len = len;
1594 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
1595 hdr->index = curpx->nb_req_cap++;
1596
1597 curpx->req_cap = hdr;
1598 curpx->to_log |= LW_REQHDR;
1599
1600 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
1601 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1602
1603 rule->act_prm.cap.expr = expr;
1604 rule->act_prm.cap.hdr = hdr;
1605 rule->action = TCP_ACT_CAPTURE;
1606 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001607 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1608 args[arg][9] == '\0' && args[arg][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02001609 args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001610 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001611 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001612
1613 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001614
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001615 curpx->conf.args.ctx = ARGC_TRK;
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001616 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001617 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001618 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001619 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001620 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001621 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001622 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001623
Willy Tarreau80aca902013-01-07 15:42:20 +01001624 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001625 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001626 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001627 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001628 free(expr);
1629 return -1;
1630 }
1631
1632 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001633 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001634
1635 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001636 arg++;
1637 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001638 memprintf(err,
1639 "'%s %s %s' : missing table name",
1640 args[0], args[1], args[kw]);
1641 free(expr);
1642 return -1;
1643 }
1644 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001645 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001646 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001647 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001648 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001649 rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001650 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001651 else if (strcmp(args[arg], "expect-proxy") == 0) {
1652 if (strcmp(args[arg+1], "layer4") != 0) {
1653 memprintf(err,
1654 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1655 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1656 return -1;
1657 }
1658
1659 if (!(where & SMP_VAL_FE_CON_ACC)) {
1660 memprintf(err,
1661 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1662 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1663 return -1;
1664 }
1665
1666 arg += 2;
1667 rule->action = TCP_ACT_EXPECT_PX;
1668 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001669 else {
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001670 struct tcp_action_kw *kw;
1671 if (where & SMP_VAL_FE_CON_ACC)
1672 kw = tcp_req_conn_action(args[arg]);
1673 else
1674 kw = tcp_req_cont_action(args[arg]);
1675 if (kw) {
1676 arg++;
1677 if (!kw->parse((const char **)args, &arg, curpx, rule, err))
1678 return -1;
1679 } else {
1680 memprintf(err,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001681 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d', "
1682 " or 'set-var' in %s '%s' (got '%s')",
Thierry FOURNIERcc87a112014-12-12 19:41:33 +01001683 args[0], args[1], MAX_SESS_STKCTR-1, proxy_type_str(curpx),
1684 curpx->id, args[arg]);
1685 return -1;
1686 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001687 }
1688
1689 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001690 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001691 memprintf(err,
1692 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1693 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001694 return -1;
1695 }
1696 }
1697 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001698 memprintf(err,
1699 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001700 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1701 return -1;
1702 }
1703 return 0;
1704}
1705
Emeric Brun97679e72010-09-23 17:56:44 +02001706/* This function should be called to parse a line starting with the "tcp-response"
1707 * keyword.
1708 */
1709static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001710 struct proxy *defpx, const char *file, int line,
1711 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001712{
1713 const char *ptr = NULL;
1714 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001715 int warn = 0;
1716 int arg;
1717 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001718 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001719 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001720 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001721
1722 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001723 memprintf(err, "missing argument for '%s' in %s '%s'",
1724 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001725 return -1;
1726 }
1727
1728 if (strcmp(args[1], "inspect-delay") == 0) {
1729 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001730 memprintf(err, "%s %s is only allowed in 'backend' sections",
1731 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001732 return -1;
1733 }
1734
1735 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001736 memprintf(err,
1737 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1738 args[0], args[1], proxy_type_str(curpx), curpx->id);
1739 if (ptr)
1740 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001741 return -1;
1742 }
1743
1744 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001745 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1746 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001747 return 1;
1748 }
1749 curpx->tcp_rep.inspect_delay = val;
1750 return 0;
1751 }
1752
Simon Hormande072bd2011-06-24 15:11:37 +09001753 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001754 LIST_INIT(&rule->list);
1755 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001756 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001757
1758 if (strcmp(args[1], "content") == 0) {
1759 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001760
1761 if (curpx->cap & PR_CAP_FE)
1762 where |= SMP_VAL_FE_RES_CNT;
1763 if (curpx->cap & PR_CAP_BE)
1764 where |= SMP_VAL_BE_RES_CNT;
1765
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001766 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001767 goto error;
1768
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001769 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1770 if (acl) {
1771 if (acl->name && *acl->name)
1772 memprintf(err,
1773 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1774 acl->name, args[0], args[1], sample_ckp_names(where));
1775 else
1776 memprintf(err,
1777 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1778 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001779 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001780 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001781
Emeric Brun97679e72010-09-23 17:56:44 +02001782 warn++;
1783 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001784 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1785 if (acl->name && *acl->name)
1786 memprintf(err,
1787 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001788 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001789 else
1790 memprintf(err,
1791 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001792 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001793 warn++;
1794 }
Emeric Brun97679e72010-09-23 17:56:44 +02001795
1796 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1797 }
1798 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001799 memprintf(err,
1800 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1801 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001802 goto error;
1803 }
1804
1805 return warn;
1806 error:
1807 free(rule);
1808 return -1;
1809}
1810
1811
Willy Tarreaub6866442008-07-14 23:54:42 +02001812/* This function should be called to parse a line starting with the "tcp-request"
1813 * keyword.
1814 */
1815static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001816 struct proxy *defpx, const char *file, int line,
1817 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001818{
1819 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001820 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001821 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001822 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001823 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001824 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001825 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001826 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001827
1828 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001829 if (curpx == defpx)
1830 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1831 else
1832 memprintf(err, "missing argument for '%s' in %s '%s'",
1833 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001834 return -1;
1835 }
1836
1837 if (!strcmp(args[1], "inspect-delay")) {
1838 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001839 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1840 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001841 return -1;
1842 }
1843
Willy Tarreaub6866442008-07-14 23:54:42 +02001844 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001845 memprintf(err,
1846 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1847 args[0], args[1], proxy_type_str(curpx), curpx->id);
1848 if (ptr)
1849 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001850 return -1;
1851 }
1852
1853 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001854 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1855 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001856 return 1;
1857 }
1858 curpx->tcp_req.inspect_delay = val;
1859 return 0;
1860 }
1861
Simon Hormande072bd2011-06-24 15:11:37 +09001862 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001863 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001864 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001865 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001866
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001867 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001868 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001869
1870 if (curpx->cap & PR_CAP_FE)
1871 where |= SMP_VAL_FE_REQ_CNT;
1872 if (curpx->cap & PR_CAP_BE)
1873 where |= SMP_VAL_BE_REQ_CNT;
1874
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001875 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001876 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001877
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001878 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1879 if (acl) {
1880 if (acl->name && *acl->name)
1881 memprintf(err,
1882 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1883 acl->name, args[0], args[1], sample_ckp_names(where));
1884 else
1885 memprintf(err,
1886 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1887 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001888 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001889 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001890
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001891 warn++;
1892 }
1893 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1894 if (acl->name && *acl->name)
1895 memprintf(err,
1896 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001897 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001898 else
1899 memprintf(err,
1900 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001901 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001902 warn++;
1903 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001904
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001905 /* the following function directly emits the warning */
1906 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001907 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001908 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001909 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001910 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001911
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001912 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001913 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1914 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001915 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001916 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001917
Willy Tarreau80aca902013-01-07 15:42:20 +01001918 where |= SMP_VAL_FE_CON_ACC;
1919
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001920 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001921 goto error;
1922
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001923 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1924 if (acl) {
1925 if (acl->name && *acl->name)
1926 memprintf(err,
1927 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1928 acl->name, args[0], args[1], sample_ckp_names(where));
1929 else
1930 memprintf(err,
1931 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1932 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001933 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001934 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001935
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001936 warn++;
1937 }
1938 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1939 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001940 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001941 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001942 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001943 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001944 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001945 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001946 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001947 warn++;
1948 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001949
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001950 /* the following function directly emits the warning */
1951 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001952 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001953 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001954 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001955 if (curpx == defpx)
1956 memprintf(err,
1957 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1958 args[0], args[1]);
1959 else
1960 memprintf(err,
1961 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001962 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001963 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001964 }
1965
Willy Tarreau1a687942010-05-23 22:40:30 +02001966 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001967 error:
1968 free(rule);
1969 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001970}
1971
Willy Tarreau645513a2010-05-24 20:55:15 +02001972
1973/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001974/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001975/************************************************************************/
1976
Willy Tarreau4a129812012-04-25 17:31:42 +02001977/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001978static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001979smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001980{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001981 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001982
1983 if (!cli_conn)
1984 return 0;
1985
1986 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001987 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001988 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001989 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001990 break;
1991 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001992 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001993 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001994 break;
1995 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001996 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001997 }
Emeric Brunf769f512010-10-22 17:14:01 +02001998
Willy Tarreau37406352012-04-23 16:16:37 +02001999 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002000 return 1;
2001}
2002
Willy Tarreaua5e37562011-12-16 17:06:15 +01002003/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002004static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002005smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002006{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002007 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002008
2009 if (!cli_conn)
2010 return 0;
2011
Willy Tarreauf853c462012-04-23 18:53:56 +02002012 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002013 if (!(smp->data.uint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02002014 return 0;
2015
Willy Tarreau37406352012-04-23 16:16:37 +02002016 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002017 return 1;
2018}
2019
Willy Tarreau4a129812012-04-25 17:31:42 +02002020/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02002021static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002022smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002023{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002024 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02002025
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002026 if (!cli_conn)
2027 return 0;
2028
2029 conn_get_to_addr(cli_conn);
2030
2031 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01002032 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002033 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02002034 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002035 break;
2036 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002037 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02002038 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002039 break;
2040 default:
Emeric Brunf769f512010-10-22 17:14:01 +02002041 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01002042 }
Emeric Brunf769f512010-10-22 17:14:01 +02002043
Willy Tarreau37406352012-04-23 16:16:37 +02002044 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002045 return 1;
2046}
2047
Willy Tarreaua5e37562011-12-16 17:06:15 +01002048/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02002049static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002050smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02002051{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002052 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002053
2054 if (!cli_conn)
2055 return 0;
2056
2057 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02002058
Willy Tarreauf853c462012-04-23 18:53:56 +02002059 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002060 if (!(smp->data.uint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02002061 return 0;
2062
Willy Tarreau37406352012-04-23 16:16:37 +02002063 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02002064 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01002065}
2066
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002067#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002068/* parse the "v4v6" bind keyword */
2069static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2070{
2071 struct listener *l;
2072
2073 list_for_each_entry(l, &conf->listeners, by_bind) {
2074 if (l->addr.ss_family == AF_INET6)
2075 l->options |= LI_O_V4V6;
2076 }
2077
2078 return 0;
2079}
2080
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002081/* parse the "v6only" bind keyword */
2082static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2083{
2084 struct listener *l;
2085
2086 list_for_each_entry(l, &conf->listeners, by_bind) {
2087 if (l->addr.ss_family == AF_INET6)
2088 l->options |= LI_O_V6ONLY;
2089 }
2090
2091 return 0;
2092}
2093#endif
2094
Pieter Baauwd551fb52013-05-08 22:49:23 +02002095#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002096/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002097static 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 +02002098{
2099 struct listener *l;
2100
Willy Tarreau4348fad2012-09-20 16:48:07 +02002101 list_for_each_entry(l, &conf->listeners, by_bind) {
2102 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2103 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02002104 }
2105
Willy Tarreau44791242012-09-12 23:27:21 +02002106 return 0;
2107}
2108#endif
2109
2110#ifdef TCP_DEFER_ACCEPT
2111/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002112static 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 +02002113{
2114 struct listener *l;
2115
Willy Tarreau4348fad2012-09-20 16:48:07 +02002116 list_for_each_entry(l, &conf->listeners, by_bind) {
2117 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2118 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02002119 }
2120
Willy Tarreau44791242012-09-12 23:27:21 +02002121 return 0;
2122}
2123#endif
2124
Willy Tarreau1c862c52012-10-05 16:21:00 +02002125#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01002126/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02002127static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2128{
2129 struct listener *l;
2130
2131 list_for_each_entry(l, &conf->listeners, by_bind) {
2132 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2133 l->options |= LI_O_TCP_FO;
2134 }
2135
2136 return 0;
2137}
2138#endif
2139
Willy Tarreau44791242012-09-12 23:27:21 +02002140#ifdef TCP_MAXSEG
2141/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002142static 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 +02002143{
2144 struct listener *l;
2145 int mss;
2146
Willy Tarreau44791242012-09-12 23:27:21 +02002147 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002148 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002149 return ERR_ALERT | ERR_FATAL;
2150 }
2151
2152 mss = atoi(args[cur_arg + 1]);
2153 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002154 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002155 return ERR_ALERT | ERR_FATAL;
2156 }
2157
Willy Tarreau4348fad2012-09-20 16:48:07 +02002158 list_for_each_entry(l, &conf->listeners, by_bind) {
2159 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2160 l->maxseg = mss;
2161 }
Willy Tarreau44791242012-09-12 23:27:21 +02002162
2163 return 0;
2164}
2165#endif
2166
Willy Tarreau2af207a2015-02-04 00:45:58 +01002167#ifdef TCP_USER_TIMEOUT
2168/* parse the "tcp-ut" bind keyword */
2169static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2170{
2171 const char *ptr = NULL;
2172 struct listener *l;
2173 unsigned int timeout;
2174
2175 if (!*args[cur_arg + 1]) {
2176 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
2177 return ERR_ALERT | ERR_FATAL;
2178 }
2179
2180 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
2181 if (ptr) {
2182 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
2183 return ERR_ALERT | ERR_FATAL;
2184 }
2185
2186 list_for_each_entry(l, &conf->listeners, by_bind) {
2187 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2188 l->tcp_ut = timeout;
2189 }
2190
2191 return 0;
2192}
2193#endif
2194
Willy Tarreau44791242012-09-12 23:27:21 +02002195#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01002196/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002197static 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 +02002198{
2199 struct listener *l;
2200
Willy Tarreau44791242012-09-12 23:27:21 +02002201 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002202 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002203 return ERR_ALERT | ERR_FATAL;
2204 }
2205
Willy Tarreau4348fad2012-09-20 16:48:07 +02002206 list_for_each_entry(l, &conf->listeners, by_bind) {
2207 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2208 l->interface = strdup(args[cur_arg + 1]);
2209 }
Willy Tarreau44791242012-09-12 23:27:21 +02002210
2211 global.last_checks |= LSTCHK_NETADM;
2212 return 0;
2213}
2214#endif
2215
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002216#ifdef CONFIG_HAP_NS
2217/* parse the "namespace" bind keyword */
2218static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2219{
2220 struct listener *l;
2221 char *namespace = NULL;
2222
2223 if (!*args[cur_arg + 1]) {
2224 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
2225 return ERR_ALERT | ERR_FATAL;
2226 }
2227 namespace = args[cur_arg + 1];
2228
2229 list_for_each_entry(l, &conf->listeners, by_bind) {
2230 l->netns = netns_store_lookup(namespace, strlen(namespace));
2231
2232 if (l->netns == NULL)
2233 l->netns = netns_store_insert(namespace);
2234
2235 if (l->netns == NULL) {
2236 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
2237 return ERR_ALERT | ERR_FATAL;
2238 }
2239 }
2240 return 0;
2241}
2242#endif
2243
Willy Tarreaudc13c112013-06-21 23:16:39 +02002244static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002245 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02002246 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02002247 { 0, NULL, NULL },
2248}};
2249
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002250
Willy Tarreau61612d42012-04-19 18:42:05 +02002251/* Note: must not be declared <const> as its list will be overwritten.
2252 * Please take care of keeping this list alphabetically sorted.
2253 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002254static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002255 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02002256}};
2257
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002258
Willy Tarreau4a129812012-04-25 17:31:42 +02002259/* Note: must not be declared <const> as its list will be overwritten.
2260 * Note: fetches that may return multiple types must be declared as the lowest
2261 * common denominator, the type that can be casted into all other ones. For
2262 * instance v4/v6 must be declared v4.
2263 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002264static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002265 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
2266 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
2267 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
2268 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
2269 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002270}};
2271
Willy Tarreau44791242012-09-12 23:27:21 +02002272/************************************************************************/
2273/* All supported bind keywords must be declared here. */
2274/************************************************************************/
2275
2276/* Note: must not be declared <const> as its list will be overwritten.
2277 * Please take care of keeping this list alphabetically sorted, doing so helps
2278 * all code contributors.
2279 * Optional keywords are also declared with a NULL ->parse() function so that
2280 * the config parser can report an appropriate error when a known keyword was
2281 * not enabled.
2282 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002283static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02002284#ifdef TCP_DEFER_ACCEPT
2285 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
2286#endif
2287#ifdef SO_BINDTODEVICE
2288 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
2289#endif
2290#ifdef TCP_MAXSEG
2291 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
2292#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01002293#ifdef TCP_USER_TIMEOUT
2294 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
2295#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02002296#ifdef TCP_FASTOPEN
2297 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
2298#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02002299#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002300 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
2301#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002302#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002303 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002304 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
2305#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002306#ifdef CONFIG_HAP_NS
2307 { "namespace", bind_parse_namespace, 1 },
2308#endif
Willy Tarreau44791242012-09-12 23:27:21 +02002309 /* the versions with the NULL parse function*/
2310 { "defer-accept", NULL, 0 },
2311 { "interface", NULL, 1 },
2312 { "mss", NULL, 1 },
2313 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002314 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002315 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002316 { NULL, NULL, 0 },
2317}};
2318
Willy Tarreaue6b98942007-10-29 01:09:36 +01002319__attribute__((constructor))
2320static void __tcp_protocol_init(void)
2321{
2322 protocol_register(&proto_tcpv4);
2323 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002324 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02002325 cfg_register_keywords(&cfg_kws);
2326 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02002327 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002328}
2329
2330
2331/*
2332 * Local variables:
2333 * c-indent-level: 8
2334 * c-basic-offset: 8
2335 * End:
2336 */