blob: f709e736557eb9d0d650611e7c5db15b9dbbf66c [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreau1a687942010-05-23 22:40:30 +02004 * Copyright 2000-2010 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>
Willy Tarreaue6b98942007-10-29 01:09:36 +010036
Willy Tarreaue6b98942007-10-29 01:09:36 +010037#include <types/global.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020038#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039
40#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020041#include <proto/arg.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010042#include <proto/buffers.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020043#include <proto/frontend.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020044#include <proto/log.h>
45#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010046#include <proto/protocols.h>
47#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020049#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020050#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020051#include <proto/stick_table.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010052#include <proto/stream_sock.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/task.h>
Emeric Brun97679e72010-09-23 17:56:44 +020054#include <proto/buffers.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010055
Willy Tarreaue8c66af2008-01-13 18:40:14 +010056#ifdef CONFIG_HAP_CTTPROXY
57#include <import/ip_tproxy.h>
58#endif
59
Emeric Bruncf20bf12010-10-22 16:06:11 +020060static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
61static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010062
63/* Note: must not be declared <const> as its list will be overwritten */
64static struct protocol proto_tcpv4 = {
65 .name = "tcpv4",
66 .sock_domain = AF_INET,
67 .sock_type = SOCK_STREAM,
68 .sock_prot = IPPROTO_TCP,
69 .sock_family = AF_INET,
70 .sock_addrlen = sizeof(struct sockaddr_in),
71 .l3_addrlen = 32/8,
Willy Tarreaueb472682010-05-28 18:46:57 +020072 .accept = &stream_sock_accept,
Emeric Bruncf20bf12010-10-22 16:06:11 +020073 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010074 .bind_all = tcp_bind_listeners,
75 .unbind_all = unbind_all_listeners,
76 .enable_all = enable_all_listeners,
77 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
78 .nb_listeners = 0,
79};
80
81/* Note: must not be declared <const> as its list will be overwritten */
82static struct protocol proto_tcpv6 = {
83 .name = "tcpv6",
84 .sock_domain = AF_INET6,
85 .sock_type = SOCK_STREAM,
86 .sock_prot = IPPROTO_TCP,
87 .sock_family = AF_INET6,
88 .sock_addrlen = sizeof(struct sockaddr_in6),
89 .l3_addrlen = 128/8,
Willy Tarreaueb472682010-05-28 18:46:57 +020090 .accept = &stream_sock_accept,
Emeric Bruncf20bf12010-10-22 16:06:11 +020091 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010092 .bind_all = tcp_bind_listeners,
93 .unbind_all = unbind_all_listeners,
94 .enable_all = enable_all_listeners,
95 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
96 .nb_listeners = 0,
97};
98
Willy Tarreaue8c66af2008-01-13 18:40:14 +010099
David du Colombier6f5ccb12011-03-10 22:26:24 +0100100/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100101 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
102 * - 0 : ignore remote address (may even be a NULL pointer)
103 * - 1 : use provided address
104 * - 2 : use provided port
105 * - 3 : use both
106 *
107 * The function supports multiple foreign binding methods :
108 * - linux_tproxy: we directly bind to the foreign address
109 * - cttproxy: we bind to a local address then nat.
110 * The second one can be used as a fallback for the first one.
111 * This function returns 0 when everything's OK, 1 if it could not bind, to the
112 * local address, 2 if it could not bind to the foreign address.
113 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100114int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100115{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100116 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100117 int foreign_ok = 0;
118 int ret;
119
120#ifdef CONFIG_HAP_LINUX_TPROXY
121 static int ip_transp_working = 1;
122 if (flags && ip_transp_working) {
Simon Hormande072bd2011-06-24 15:11:37 +0900123 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
124 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100125 foreign_ok = 1;
126 else
127 ip_transp_working = 0;
128 }
129#endif
130 if (flags) {
131 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200132 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100133 switch (remote->ss_family) {
134 case AF_INET:
135 if (flags & 1)
136 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
137 if (flags & 2)
138 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
139 break;
140 case AF_INET6:
141 if (flags & 1)
142 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
143 if (flags & 2)
144 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
145 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100146 default:
147 /* we don't want to try to bind to an unknown address family */
148 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100149 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100150 }
151
Simon Hormande072bd2011-06-24 15:11:37 +0900152 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100153 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200154 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100155 if (ret < 0)
156 return 2;
157 }
158 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200159 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100160 if (ret < 0)
161 return 1;
162 }
163
164 if (!flags)
165 return 0;
166
167#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100168 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100169 struct in_tproxy itp1, itp2;
170 memset(&itp1, 0, sizeof(itp1));
171
172 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100173 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
174 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100175
176 /* set connect flag on socket */
177 itp2.op = TPROXY_FLAGS;
178 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
179
180 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
181 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
182 foreign_ok = 1;
183 }
184 }
185#endif
186 if (!foreign_ok)
187 /* we could not bind to a foreign address */
188 return 2;
189
190 return 0;
191}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100192
Willy Tarreau9650f372009-08-16 14:02:45 +0200193
194/*
Willy Tarreauac825402011-03-04 22:04:29 +0100195 * This function initiates a connection to the target assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200196 * (si->{target,addr.to}). A source address may be pointed to by si->addr.from
Willy Tarreauac825402011-03-04 22:04:29 +0100197 * in case of transparent proxying. Normal source bind addresses are still
198 * determined locally (due to the possible need of a source port).
199 * si->target may point either to a valid server or to a backend, depending
200 * on si->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are supported.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200201 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200202 * It can return one of :
203 * - SN_ERR_NONE if everything's OK
204 * - SN_ERR_SRVTO if there are no more servers
205 * - SN_ERR_SRVCL if the connection was refused by the server
206 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
207 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
208 * - SN_ERR_INTERNAL for any other purely internal errors
209 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
210 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100211
David du Colombier6f5ccb12011-03-10 22:26:24 +0100212int tcp_connect_server(struct stream_interface *si)
Willy Tarreau9650f372009-08-16 14:02:45 +0200213{
214 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100215 struct server *srv;
216 struct proxy *be;
217
218 switch (si->target.type) {
219 case TARG_TYPE_PROXY:
220 be = si->target.ptr.p;
221 srv = NULL;
222 break;
223 case TARG_TYPE_SERVER:
224 srv = si->target.ptr.s;
225 be = srv->proxy;
226 break;
227 default:
228 return SN_ERR_INTERNAL;
229 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200230
Willy Tarreau6471afb2011-09-23 10:54:59 +0200231 if ((fd = si->fd = socket(si->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200232 qfprintf(stderr, "Cannot get a server socket.\n");
233
234 if (errno == ENFILE)
235 send_log(be, LOG_EMERG,
236 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
237 be->id, maxfd);
238 else if (errno == EMFILE)
239 send_log(be, LOG_EMERG,
240 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
241 be->id, maxfd);
242 else if (errno == ENOBUFS || errno == ENOMEM)
243 send_log(be, LOG_EMERG,
244 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
245 be->id, maxfd);
246 /* this is a resource error */
247 return SN_ERR_RESOURCE;
248 }
249
250 if (fd >= global.maxsock) {
251 /* do not log anything there, it's a normal condition when this option
252 * is used to serialize connections to a server !
253 */
254 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
255 close(fd);
256 return SN_ERR_PRXCOND; /* it is a configuration limit */
257 }
258
259 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900260 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200261 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
262 close(fd);
263 return SN_ERR_INTERNAL;
264 }
265
266 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900267 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200268
269 if (be->options & PR_O_TCP_NOLING)
Willy Tarreauf6f82252011-11-30 18:02:24 +0100270 si->flags |= SI_FL_NOLINGER;
Willy Tarreau9650f372009-08-16 14:02:45 +0200271
272 /* allow specific binding :
273 * - server-specific at first
274 * - proxy-specific next
275 */
276 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200277 int ret, flags = 0;
278
Willy Tarreau9650f372009-08-16 14:02:45 +0200279 switch (srv->state & SRV_TPROXY_MASK) {
280 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200281 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200282 flags = 3;
283 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200284 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200285 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200286 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200287 break;
288 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200289
Willy Tarreau9650f372009-08-16 14:02:45 +0200290#ifdef SO_BINDTODEVICE
291 /* Note: this might fail if not CAP_NET_RAW */
292 if (srv->iface_name)
293 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
294#endif
295
296 if (srv->sport_range) {
297 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100298 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200299
300 ret = 1;
301 src = srv->source_addr;
302
303 do {
304 /* note: in case of retry, we may have to release a previously
305 * allocated port, hence this loop's construct.
306 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200307 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
308 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200309
310 if (!attempts)
311 break;
312 attempts--;
313
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200314 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
315 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200316 break;
317
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200318 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200319 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200320
Willy Tarreau6471afb2011-09-23 10:54:59 +0200321 ret = tcp_bind_socket(fd, flags, &src, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200322 } while (ret != 0); /* binding NOK */
323 }
324 else {
Willy Tarreau6471afb2011-09-23 10:54:59 +0200325 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200326 }
327
328 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200329 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
330 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200331 close(fd);
332
333 if (ret == 1) {
334 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
335 be->id, srv->id);
336 send_log(be, LOG_EMERG,
337 "Cannot bind to source address before connect() for server %s/%s.\n",
338 be->id, srv->id);
339 } else {
340 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
341 be->id, srv->id);
342 send_log(be, LOG_EMERG,
343 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
344 be->id, srv->id);
345 }
346 return SN_ERR_RESOURCE;
347 }
348 }
349 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200350 int ret, flags = 0;
351
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 switch (be->options & PR_O_TPXY_MASK) {
353 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200354 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200355 flags = 3;
356 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200357 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200358 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200359 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200360 break;
361 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200362
Willy Tarreau9650f372009-08-16 14:02:45 +0200363#ifdef SO_BINDTODEVICE
364 /* Note: this might fail if not CAP_NET_RAW */
365 if (be->iface_name)
366 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
367#endif
Willy Tarreau6471afb2011-09-23 10:54:59 +0200368 ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200369 if (ret) {
370 close(fd);
371 if (ret == 1) {
372 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
373 be->id);
374 send_log(be, LOG_EMERG,
375 "Cannot bind to source address before connect() for proxy %s.\n",
376 be->id);
377 } else {
378 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
379 be->id);
380 send_log(be, LOG_EMERG,
381 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
382 be->id);
383 }
384 return SN_ERR_RESOURCE;
385 }
386 }
387
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400388#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200389 /* disabling tcp quick ack now allows the first request to leave the
390 * machine with the first ACK. We only do this if there are pending
391 * data in the buffer.
392 */
Willy Tarreau2e046c62012-03-01 16:08:30 +0100393 if ((be->options2 & PR_O2_SMARTCON) && si->ob->o)
Simon Hormande072bd2011-06-24 15:11:37 +0900394 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200395#endif
396
Willy Tarreaue803de22010-01-21 17:43:04 +0100397 if (global.tune.server_sndbuf)
398 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
399
400 if (global.tune.server_rcvbuf)
401 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
402
Willy Tarreau9b061e32012-04-07 18:03:52 +0200403 si->flags &= ~SI_FL_FROM_SET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200404 if ((connect(fd, (struct sockaddr *)&si->addr.to, get_addr_len(&si->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200405 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
406
407 if (errno == EAGAIN || errno == EADDRINUSE) {
408 char *msg;
409 if (errno == EAGAIN) /* no free ports left, try again later */
410 msg = "no free ports";
411 else
412 msg = "local address already in use";
413
414 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200415 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
416 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200417 close(fd);
418 send_log(be, LOG_EMERG,
419 "Connect() failed for server %s/%s: %s.\n",
420 be->id, srv->id, msg);
421 return SN_ERR_RESOURCE;
422 } else if (errno == ETIMEDOUT) {
423 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200424 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
425 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200426 close(fd);
427 return SN_ERR_SRVTO;
428 } else {
429 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
430 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200431 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
432 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200433 close(fd);
434 return SN_ERR_SRVCL;
435 }
436 }
437
William Lallemandb7ff6a32012-03-02 14:35:21 +0100438 /* needs src ip/port for logging */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200439 if (si->flags & SI_FL_SRC_ADDR)
440 stream_sock_get_from_addr(si);
William Lallemandb7ff6a32012-03-02 14:35:21 +0100441
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 fdtab[fd].owner = si;
443 fdtab[fd].state = FD_STCONN; /* connection in progress */
444 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau1b79bde2012-05-07 17:01:39 +0200445 fdtab[fd].cb[DIR_RD].f = si->sock.read;
Willy Tarreau9650f372009-08-16 14:02:45 +0200446 fdtab[fd].cb[DIR_RD].b = si->ib;
Willy Tarreau1b79bde2012-05-07 17:01:39 +0200447 fdtab[fd].cb[DIR_WR].f = si->sock.write;
Willy Tarreau9650f372009-08-16 14:02:45 +0200448 fdtab[fd].cb[DIR_WR].b = si->ob;
449
Willy Tarreau6471afb2011-09-23 10:54:59 +0200450 fdinfo[fd].peeraddr = (struct sockaddr *)&si->addr.to;
451 fdinfo[fd].peerlen = get_addr_len(&si->addr.to);
Willy Tarreau9650f372009-08-16 14:02:45 +0200452
453 fd_insert(fd);
454 EV_FD_SET(fd, DIR_WR); /* for connect status */
455
456 si->state = SI_ST_CON;
457 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
458 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
459
460 return SN_ERR_NONE; /* connection is OK */
461}
462
463
Willy Tarreaue6b98942007-10-29 01:09:36 +0100464/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
465 * an error message in <err> if the message is at most <errlen> bytes long
466 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
467 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
468 * was alright and that no message was returned. ERR_RETRYABLE means that an
469 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700470 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100471 * the meaning of the error, but just indicate that a message is present which
472 * should be displayed with the respective level. Last, ERR_ABORT indicates
473 * that it's pointless to try to start other listeners. No error message is
474 * returned if errlen is NULL.
475 */
476int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
477{
478 __label__ tcp_return, tcp_close_return;
479 int fd, err;
480 const char *msg = NULL;
481
482 /* ensure we never return garbage */
483 if (errmsg && errlen)
484 *errmsg = 0;
485
486 if (listener->state != LI_ASSIGNED)
487 return ERR_NONE; /* already bound */
488
489 err = ERR_NONE;
490
491 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
492 err |= ERR_RETRYABLE | ERR_ALERT;
493 msg = "cannot create listening socket";
494 goto tcp_return;
495 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100496
Willy Tarreaue6b98942007-10-29 01:09:36 +0100497 if (fd >= global.maxsock) {
498 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
499 msg = "not enough free sockets (raise '-n' parameter)";
500 goto tcp_close_return;
501 }
502
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200503 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100504 err |= ERR_FATAL | ERR_ALERT;
505 msg = "cannot make socket non-blocking";
506 goto tcp_close_return;
507 }
508
Simon Hormande072bd2011-06-24 15:11:37 +0900509 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100510 /* not fatal but should be reported */
511 msg = "cannot do so_reuseaddr";
512 err |= ERR_ALERT;
513 }
514
515 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900516 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100517
Willy Tarreaue6b98942007-10-29 01:09:36 +0100518#ifdef SO_REUSEPORT
519 /* OpenBSD supports this. As it's present in old libc versions of Linux,
520 * it might return an error that we will silently ignore.
521 */
Simon Hormande072bd2011-06-24 15:11:37 +0900522 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100523#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100524#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100525 if ((listener->options & LI_O_FOREIGN)
Simon Hormande072bd2011-06-24 15:11:37 +0900526 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
527 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100528 msg = "cannot make listening socket transparent";
529 err |= ERR_ALERT;
530 }
531#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100532#ifdef SO_BINDTODEVICE
533 /* Note: this might fail if not CAP_NET_RAW */
534 if (listener->interface) {
535 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100536 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100537 msg = "cannot bind listener to device";
538 err |= ERR_WARN;
539 }
540 }
541#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400542#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100543 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400544 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200545 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
546 msg = "cannot set MSS";
547 err |= ERR_WARN;
548 }
549 }
550#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200551#if defined(TCP_DEFER_ACCEPT)
552 if (listener->options & LI_O_DEF_ACCEPT) {
553 /* defer accept by up to one second */
554 int accept_delay = 1;
555 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
556 msg = "cannot enable DEFER_ACCEPT";
557 err |= ERR_WARN;
558 }
559 }
560#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100561 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
562 err |= ERR_RETRYABLE | ERR_ALERT;
563 msg = "cannot bind socket";
564 goto tcp_close_return;
565 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100566
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100567 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100568 err |= ERR_RETRYABLE | ERR_ALERT;
569 msg = "cannot listen to socket";
570 goto tcp_close_return;
571 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100572
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400573#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200574 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900575 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200576#endif
577
Willy Tarreaue6b98942007-10-29 01:09:36 +0100578 /* the socket is ready */
579 listener->fd = fd;
580 listener->state = LI_LISTEN;
581
Willy Tarreaueabf3132008-08-29 23:36:51 +0200582 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100583 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaueb472682010-05-28 18:46:57 +0200584 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
585 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
586 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
587 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200588
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200589 fdinfo[fd].peeraddr = NULL;
590 fdinfo[fd].peerlen = 0;
Willy Tarreaueb472682010-05-28 18:46:57 +0200591 fd_insert(fd);
592
Willy Tarreaue6b98942007-10-29 01:09:36 +0100593 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100594 if (msg && errlen) {
595 char pn[INET6_ADDRSTRLEN];
596
Willy Tarreau631f01c2011-09-05 00:36:48 +0200597 addr_to_str(&listener->addr, pn, sizeof(pn));
598 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100599 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100600 return err;
601
602 tcp_close_return:
603 close(fd);
604 goto tcp_return;
605}
606
607/* This function creates all TCP sockets bound to the protocol entry <proto>.
608 * It is intended to be used as the protocol's bind_all() function.
609 * The sockets will be registered but not added to any fd_set, in order not to
610 * loose them across the fork(). A call to enable_all_listeners() is needed
611 * to complete initialization. The return value is composed from ERR_*.
612 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200613static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100614{
615 struct listener *listener;
616 int err = ERR_NONE;
617
618 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200619 err |= tcp_bind_listener(listener, errmsg, errlen);
620 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100621 break;
622 }
623
624 return err;
625}
626
627/* Add listener to the list of tcpv4 listeners. The listener's state
628 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
629 * listeners is updated. This is the function to use to add a new listener.
630 */
631void tcpv4_add_listener(struct listener *listener)
632{
633 if (listener->state != LI_INIT)
634 return;
635 listener->state = LI_ASSIGNED;
636 listener->proto = &proto_tcpv4;
637 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
638 proto_tcpv4.nb_listeners++;
639}
640
641/* Add listener to the list of tcpv4 listeners. The listener's state
642 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
643 * listeners is updated. This is the function to use to add a new listener.
644 */
645void tcpv6_add_listener(struct listener *listener)
646{
647 if (listener->state != LI_INIT)
648 return;
649 listener->state = LI_ASSIGNED;
650 listener->proto = &proto_tcpv6;
651 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
652 proto_tcpv6.nb_listeners++;
653}
654
Willy Tarreauedcf6682008-11-30 23:15:34 +0100655/* This function performs the TCP request analysis on the current request. It
656 * returns 1 if the processing can continue on next analysers, or zero if it
657 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200658 * request. It relies on buffers flags, and updates s->req->analysers. The
659 * function may be called for frontend rules and backend rules. It only relies
660 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100661 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200662int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100663{
664 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200665 struct stksess *ts;
666 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100667 int partial;
668
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100669 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreauedcf6682008-11-30 23:15:34 +0100670 now_ms, __FUNCTION__,
671 s,
672 req,
673 req->rex, req->wex,
674 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100675 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100676 req->analysers);
677
Willy Tarreauedcf6682008-11-30 23:15:34 +0100678 /* We don't know whether we have enough data, so must proceed
679 * this way :
680 * - iterate through all rules in their declaration order
681 * - if one rule returns MISS, it means the inspect delay is
682 * not over yet, then return immediately, otherwise consider
683 * it as a non-match.
684 * - if one rule returns OK, then return OK
685 * - if one rule returns KO, then return KO
686 */
687
Willy Tarreaub824b002010-09-29 16:36:16 +0200688 if (req->flags & (BF_SHUTR|BF_FULL) || !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200689 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100690 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200691 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100692
Willy Tarreaufb356202010-08-03 14:02:05 +0200693 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100694 int ret = ACL_PAT_PASS;
695
696 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200697 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100698 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200699 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100700 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200701 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
702 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100703 return 0;
704 }
705
706 ret = acl_pass(ret);
707 if (rule->cond->pol == ACL_COND_UNLESS)
708 ret = !ret;
709 }
710
711 if (ret) {
712 /* we have a matching rule. */
713 if (rule->action == TCP_ACT_REJECT) {
714 buffer_abort(req);
715 buffer_abort(s->rep);
716 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200717
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100718 s->be->be_counters.denied_req++;
719 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200720 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200721 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200722
Willy Tarreauedcf6682008-11-30 23:15:34 +0100723 if (!(s->flags & SN_ERR_MASK))
724 s->flags |= SN_ERR_PRXCOND;
725 if (!(s->flags & SN_FINST_MASK))
726 s->flags |= SN_FINST_R;
727 return 0;
728 }
Willy Tarreau56123282010-08-06 19:06:56 +0200729 else if (rule->action == TCP_ACT_TRK_SC1) {
730 if (!s->stkctr1_entry) {
731 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200732 * Also, note that right now we can only track SRC so we
733 * don't check how to get the key, but later we may need
734 * to consider rule->act_prm->trk_ctr.type.
735 */
736 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100737 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200738 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200739 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200740 if (s->fe != s->be)
741 s->flags |= SN_BE_TRACK_SC1;
742 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200743 }
744 }
Willy Tarreau56123282010-08-06 19:06:56 +0200745 else if (rule->action == TCP_ACT_TRK_SC2) {
746 if (!s->stkctr2_entry) {
747 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200748 * Also, note that right now we can only track SRC so we
749 * don't check how to get the key, but later we may need
750 * to consider rule->act_prm->trk_ctr.type.
751 */
752 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100753 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200754 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200755 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200756 if (s->fe != s->be)
757 s->flags |= SN_BE_TRACK_SC2;
758 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200759 }
760 }
761 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100762 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200763 break;
764 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100765 }
766 }
767
768 /* if we get there, it means we have no rule which matches, or
769 * we have an explicit accept, so we apply the default accept.
770 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200771 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100772 req->analyse_exp = TICK_ETERNITY;
773 return 1;
774}
775
Emeric Brun97679e72010-09-23 17:56:44 +0200776/* This function performs the TCP response analysis on the current response. It
777 * returns 1 if the processing can continue on next analysers, or zero if it
778 * needs more data, encounters an error, or wants to immediately abort the
779 * response. It relies on buffers flags, and updates s->rep->analysers. The
780 * function may be called for backend rules.
781 */
782int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit)
783{
784 struct tcp_rule *rule;
785 int partial;
786
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100787 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun97679e72010-09-23 17:56:44 +0200788 now_ms, __FUNCTION__,
789 s,
790 rep,
791 rep->rex, rep->wex,
792 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100793 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200794 rep->analysers);
795
796 /* We don't know whether we have enough data, so must proceed
797 * this way :
798 * - iterate through all rules in their declaration order
799 * - if one rule returns MISS, it means the inspect delay is
800 * not over yet, then return immediately, otherwise consider
801 * it as a non-match.
802 * - if one rule returns OK, then return OK
803 * - if one rule returns KO, then return KO
804 */
805
806 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200807 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200808 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200809 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200810
811 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
812 int ret = ACL_PAT_PASS;
813
814 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200815 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200816 if (ret == ACL_PAT_MISS) {
817 /* just set the analyser timeout once at the beginning of the response */
818 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
819 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
820 return 0;
821 }
822
823 ret = acl_pass(ret);
824 if (rule->cond->pol == ACL_COND_UNLESS)
825 ret = !ret;
826 }
827
828 if (ret) {
829 /* we have a matching rule. */
830 if (rule->action == TCP_ACT_REJECT) {
831 buffer_abort(rep);
832 buffer_abort(s->req);
833 rep->analysers = 0;
834
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100835 s->be->be_counters.denied_resp++;
836 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200837 if (s->listener->counters)
838 s->listener->counters->denied_resp++;
839
840 if (!(s->flags & SN_ERR_MASK))
841 s->flags |= SN_ERR_PRXCOND;
842 if (!(s->flags & SN_FINST_MASK))
843 s->flags |= SN_FINST_D;
844 return 0;
845 }
846 else {
847 /* otherwise accept */
848 break;
849 }
850 }
851 }
852
853 /* if we get there, it means we have no rule which matches, or
854 * we have an explicit accept, so we apply the default accept.
855 */
856 rep->analysers &= ~an_bit;
857 rep->analyse_exp = TICK_ETERNITY;
858 return 1;
859}
860
861
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200862/* This function performs the TCP layer4 analysis on the current request. It
863 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
864 * matches or if no more rule matches. It can only use rules which don't need
865 * any data.
866 */
867int tcp_exec_req_rules(struct session *s)
868{
869 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200870 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200871 struct stktable *t = NULL;
872 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200873 int ret;
874
875 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
876 ret = ACL_PAT_PASS;
877
878 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200879 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200880 ret = acl_pass(ret);
881 if (rule->cond->pol == ACL_COND_UNLESS)
882 ret = !ret;
883 }
884
885 if (ret) {
886 /* we have a matching rule. */
887 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100888 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200889 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200890 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200891
892 if (!(s->flags & SN_ERR_MASK))
893 s->flags |= SN_ERR_PRXCOND;
894 if (!(s->flags & SN_FINST_MASK))
895 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200896 result = 0;
897 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200898 }
Willy Tarreau56123282010-08-06 19:06:56 +0200899 else if (rule->action == TCP_ACT_TRK_SC1) {
900 if (!s->stkctr1_entry) {
901 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200902 * Also, note that right now we can only track SRC so we
903 * don't check how to get the key, but later we may need
904 * to consider rule->act_prm->trk_ctr.type.
905 */
906 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100907 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200908 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200909 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200910 }
911 }
Willy Tarreau56123282010-08-06 19:06:56 +0200912 else if (rule->action == TCP_ACT_TRK_SC2) {
913 if (!s->stkctr2_entry) {
914 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200915 * Also, note that right now we can only track SRC so we
916 * don't check how to get the key, but later we may need
917 * to consider rule->act_prm->trk_ctr.type.
918 */
919 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100920 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200921 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200922 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200923 }
924 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200925 else {
926 /* otherwise it's an accept */
927 break;
928 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200929 }
930 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200931 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200932}
933
Emeric Brun97679e72010-09-23 17:56:44 +0200934/* Parse a tcp-response rule. Return a negative value in case of failure */
935static int tcp_parse_response_rule(char **args, int arg, int section_type,
936 struct proxy *curpx, struct proxy *defpx,
937 struct tcp_rule *rule, char *err, int errlen)
938{
939 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
940 snprintf(err, errlen, "%s %s is only allowed in 'backend' sections",
941 args[0], args[1]);
942 return -1;
943 }
944
945 if (strcmp(args[arg], "accept") == 0) {
946 arg++;
947 rule->action = TCP_ACT_ACCEPT;
948 }
949 else if (strcmp(args[arg], "reject") == 0) {
950 arg++;
951 rule->action = TCP_ACT_REJECT;
952 }
953 else {
954 snprintf(err, errlen,
955 "'%s %s' expects 'accept' or 'reject' in %s '%s' (was '%s')",
956 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
957 return -1;
958 }
959
960 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200961 char *errmsg = NULL;
962
963 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, &errmsg)) == NULL) {
Emeric Brun97679e72010-09-23 17:56:44 +0200964 snprintf(err, errlen,
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200965 "error detected in %s '%s' while parsing '%s' condition : %s",
966 proxy_type_str(curpx), curpx->id, args[arg], errmsg);
967 free(errmsg);
Emeric Brun97679e72010-09-23 17:56:44 +0200968 return -1;
969 }
970 }
971 else if (*args[arg]) {
972 snprintf(err, errlen,
973 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
974 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
975 return -1;
976 }
977 return 0;
978}
979
980
981
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200982/* Parse a tcp-request rule. Return a negative value in case of failure */
983static int tcp_parse_request_rule(char **args, int arg, int section_type,
984 struct proxy *curpx, struct proxy *defpx,
985 struct tcp_rule *rule, char *err, int errlen)
986{
987 if (curpx == defpx) {
988 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
989 args[0], args[1]);
990 return -1;
991 }
992
993 if (!strcmp(args[arg], "accept")) {
994 arg++;
995 rule->action = TCP_ACT_ACCEPT;
996 }
997 else if (!strcmp(args[arg], "reject")) {
998 arg++;
999 rule->action = TCP_ACT_REJECT;
1000 }
Willy Tarreau56123282010-08-06 19:06:56 +02001001 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001002 int ret;
1003
1004 arg++;
1005 ret = parse_track_counters(args, &arg, section_type, curpx,
1006 &rule->act_prm.trk_ctr, defpx, err, errlen);
1007
1008 if (ret < 0) /* nb: warnings are not handled yet */
1009 return -1;
1010
Willy Tarreau56123282010-08-06 19:06:56 +02001011 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001012 }
Willy Tarreau56123282010-08-06 19:06:56 +02001013 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001014 int ret;
1015
1016 arg++;
1017 ret = parse_track_counters(args, &arg, section_type, curpx,
1018 &rule->act_prm.trk_ctr, defpx, err, errlen);
1019
1020 if (ret < 0) /* nb: warnings are not handled yet */
1021 return -1;
1022
Willy Tarreau56123282010-08-06 19:06:56 +02001023 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001024 }
1025 else {
1026 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02001027 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1028 "or 'track-sc2' in %s '%s' (was '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001029 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
1030 return -1;
1031 }
1032
1033 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001034 char *errmsg = NULL;
1035
1036 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, &errmsg)) == NULL) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001037 snprintf(err, errlen,
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001038 "error detected in %s '%s' while parsing '%s' condition : %s",
1039 proxy_type_str(curpx), curpx->id, args[arg], errmsg);
1040 free(errmsg);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001041 return -1;
1042 }
1043 }
1044 else if (*args[arg]) {
1045 snprintf(err, errlen,
1046 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
1047 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1048 return -1;
1049 }
1050 return 0;
1051}
1052
Emeric Brun97679e72010-09-23 17:56:44 +02001053/* This function should be called to parse a line starting with the "tcp-response"
1054 * keyword.
1055 */
1056static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
1057 struct proxy *defpx, char *err, int errlen)
1058{
1059 const char *ptr = NULL;
1060 unsigned int val;
1061 int retlen;
1062 int warn = 0;
1063 int arg;
1064 struct tcp_rule *rule;
1065
1066 if (!*args[1]) {
1067 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
1068 args[0], proxy_type_str(curpx), curpx->id);
1069 return -1;
1070 }
1071
1072 if (strcmp(args[1], "inspect-delay") == 0) {
1073 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
1074 snprintf(err, errlen, "%s %s is only allowed in 'backend' sections",
1075 args[0], args[1]);
1076 return -1;
1077 }
1078
1079 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1080 retlen = snprintf(err, errlen,
1081 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1082 args[0], args[1], proxy_type_str(curpx), curpx->id);
1083 if (ptr && retlen < errlen)
1084 retlen += snprintf(err + retlen, errlen - retlen,
1085 " (unexpected character '%c')", *ptr);
1086 return -1;
1087 }
1088
1089 if (curpx->tcp_rep.inspect_delay) {
1090 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
1091 args[0], args[1], proxy_type_str(curpx), curpx->id);
1092 return 1;
1093 }
1094 curpx->tcp_rep.inspect_delay = val;
1095 return 0;
1096 }
1097
Simon Hormande072bd2011-06-24 15:11:37 +09001098 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001099 LIST_INIT(&rule->list);
1100 arg = 1;
1101
1102 if (strcmp(args[1], "content") == 0) {
1103 arg++;
1104 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
1105 goto error;
1106
1107 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1108 struct acl *acl;
1109 const char *name;
1110
1111 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1112 name = acl ? acl->name : "(unknown)";
1113
1114 retlen = snprintf(err, errlen,
1115 "acl '%s' involves some request-only criteria which will be ignored.",
1116 name);
1117 warn++;
1118 }
1119
1120 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1121 }
1122 else {
1123 retlen = snprintf(err, errlen,
1124 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (was '%s')",
1125 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1126 goto error;
1127 }
1128
1129 return warn;
1130 error:
1131 free(rule);
1132 return -1;
1133}
1134
1135
Willy Tarreaub6866442008-07-14 23:54:42 +02001136/* This function should be called to parse a line starting with the "tcp-request"
1137 * keyword.
1138 */
1139static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
1140 struct proxy *defpx, char *err, int errlen)
1141{
1142 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001143 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +02001144 int retlen;
Willy Tarreau1a687942010-05-23 22:40:30 +02001145 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001146 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001147 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001148
1149 if (!*args[1]) {
1150 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +02001151 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001152 return -1;
1153 }
1154
1155 if (!strcmp(args[1], "inspect-delay")) {
1156 if (curpx == defpx) {
1157 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
1158 args[0], args[1]);
1159 return -1;
1160 }
1161
Willy Tarreaub6866442008-07-14 23:54:42 +02001162 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1163 retlen = snprintf(err, errlen,
1164 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +02001165 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001166 if (ptr && retlen < errlen)
1167 retlen += snprintf(err+retlen, errlen - retlen,
1168 " (unexpected character '%c')", *ptr);
1169 return -1;
1170 }
1171
1172 if (curpx->tcp_req.inspect_delay) {
1173 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +02001174 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001175 return 1;
1176 }
1177 curpx->tcp_req.inspect_delay = val;
1178 return 0;
1179 }
1180
Simon Hormande072bd2011-06-24 15:11:37 +09001181 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001182 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001183 arg = 1;
1184
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001185 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001186 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001187 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001188 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001189
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001190 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001191 struct acl *acl;
1192 const char *name;
1193
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001194 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001195 name = acl ? acl->name : "(unknown)";
1196
1197 retlen = snprintf(err, errlen,
Willy Tarreau1a211942009-07-14 13:53:17 +02001198 "acl '%s' involves some response-only criteria which will be ignored.",
1199 name);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001200 warn++;
1201 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001202 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001203 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001204 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001205 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001206
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001207 if (!(curpx->cap & PR_CAP_FE)) {
1208 snprintf(err, errlen, "%s %s is not allowed because %s %s is not a frontend",
1209 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001210 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001211 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001212
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001213 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001214 goto error;
1215
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001216 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1217 struct acl *acl;
1218 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001219
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001220 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1221 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001222
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001223 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
1224 retlen = snprintf(err, errlen,
1225 "'%s %s' may not reference acl '%s' which makes use of "
1226 "payload in %s '%s'. Please use '%s content' for this.",
1227 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
1228 goto error;
1229 }
1230 if (acl->requires & ACL_USE_RTR_ANY)
1231 retlen = snprintf(err, errlen,
1232 "acl '%s' involves some response-only criteria which will be ignored.",
1233 name);
1234 warn++;
1235 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001236 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001237 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001238 else {
1239 retlen = snprintf(err, errlen,
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001240 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (was '%s')",
Willy Tarreau1a687942010-05-23 22:40:30 +02001241 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001242 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001243 }
1244
Willy Tarreau1a687942010-05-23 22:40:30 +02001245 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001246 error:
1247 free(rule);
1248 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001249}
1250
Willy Tarreau645513a2010-05-24 20:55:15 +02001251
1252/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001253/* All supported sample fetch functios must be declared here */
1254/************************************************************************/
1255
1256/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001257 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001258 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1259 * is a string (cookie name), other types will lead to undefined behaviour.
1260 */
1261int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001262smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001263 const struct arg *args, struct sample *smp)
1264{
1265 int bleft;
1266 const unsigned char *data;
1267
1268 if (!l4 || !l4->req)
1269 return 0;
1270
1271 smp->flags = 0;
1272 smp->type = SMP_T_CSTR;
1273
1274 bleft = l4->req->i;
1275 if (bleft <= 11)
1276 goto too_short;
1277
1278 data = (const unsigned char *)l4->req->p + 11;
1279 bleft -= 11;
1280
1281 if (bleft <= 7)
1282 goto too_short;
1283
1284 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1285 goto not_cookie;
1286
1287 data += 7;
1288 bleft -= 7;
1289
1290 while (bleft > 0 && *data == ' ') {
1291 data++;
1292 bleft--;
1293 }
1294
1295 if (args) {
1296
1297 if (bleft <= args->data.str.len)
1298 goto too_short;
1299
1300 if ((data[args->data.str.len] != '=') ||
1301 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1302 goto not_cookie;
1303
1304 data += args->data.str.len + 1;
1305 bleft -= args->data.str.len + 1;
1306 } else {
1307 while (bleft > 0 && *data != '=') {
1308 if (*data == '\r' || *data == '\n')
1309 goto not_cookie;
1310 data++;
1311 bleft--;
1312 }
1313
1314 if (bleft < 1)
1315 goto too_short;
1316
1317 if (*data != '=')
1318 goto not_cookie;
1319
1320 data++;
1321 bleft--;
1322 }
1323
1324 /* data points to cookie value */
1325 smp->data.str.str = (char *)data;
1326 smp->data.str.len = 0;
1327
1328 while (bleft > 0 && *data != '\r') {
1329 data++;
1330 bleft--;
1331 }
1332
1333 if (bleft < 2)
1334 goto too_short;
1335
1336 if (data[0] != '\r' || data[1] != '\n')
1337 goto not_cookie;
1338
1339 smp->data.str.len = (char *)data - smp->data.str.str;
1340 smp->flags = SMP_F_VOLATILE;
1341 return 1;
1342
1343 too_short:
1344 smp->flags = SMP_F_MAY_CHANGE;
1345 not_cookie:
1346 return 0;
1347}
1348
Willy Tarreau32389b72012-04-23 23:13:20 +02001349/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001350/* All supported ACL keywords must be declared here. */
1351/************************************************************************/
1352
Willy Tarreau32389b72012-04-23 23:13:20 +02001353/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1354static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001355acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001356 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001357{
1358 int ret;
1359
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001360 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001361
1362 if (smp->flags & SMP_F_MAY_CHANGE)
1363 return 0;
1364
1365 smp->flags = SMP_F_VOLATILE;
1366 smp->type = SMP_T_UINT;
1367 smp->data.uint = ret;
1368 return 1;
1369}
1370
1371
Willy Tarreau4a129812012-04-25 17:31:42 +02001372/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001373static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001374smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001375 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001376{
Willy Tarreauf4362b32011-12-16 17:49:52 +01001377 switch (l4->si[0].addr.from.ss_family) {
1378 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001379 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.from)->sin_addr;
1380 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001381 break;
1382 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001383 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.from))->sin6_addr;
1384 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001385 break;
1386 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001387 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001388 }
Emeric Brunf769f512010-10-22 17:14:01 +02001389
Willy Tarreau37406352012-04-23 16:16:37 +02001390 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001391 return 1;
1392}
1393
David du Colombier4f92d322011-03-24 11:09:31 +01001394/* extract the connection's source ipv6 address */
1395static int
Willy Tarreau12785782012-04-27 21:37:17 +02001396smp_fetch_src6(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1397 const struct arg *arg_p, struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +01001398{
Willy Tarreau6471afb2011-09-23 10:54:59 +02001399 if (l4->si[0].addr.from.ss_family != AF_INET6)
David du Colombier4f92d322011-03-24 11:09:31 +01001400 return 0;
1401
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001402 smp->type = SMP_T_IPV6;
Willy Tarreau342acb42012-04-23 22:03:39 +02001403 memcpy(smp->data.ipv6.s6_addr, ((struct sockaddr_in6 *)&l4->si[0].addr.from)->sin6_addr.s6_addr, sizeof(smp->data.ipv6.s6_addr));
David du Colombier4f92d322011-03-24 11:09:31 +01001404 return 1;
1405}
Willy Tarreau645513a2010-05-24 20:55:15 +02001406
Willy Tarreaua5e37562011-12-16 17:06:15 +01001407/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001408static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001409smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001410 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001411{
Willy Tarreauf853c462012-04-23 18:53:56 +02001412 smp->type = SMP_T_UINT;
1413 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001414 return 0;
1415
Willy Tarreau37406352012-04-23 16:16:37 +02001416 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001417 return 1;
1418}
1419
Willy Tarreau4a129812012-04-25 17:31:42 +02001420/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001421static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001422smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001423 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001424{
Willy Tarreau9b061e32012-04-07 18:03:52 +02001425 stream_sock_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001426
Willy Tarreauf4362b32011-12-16 17:49:52 +01001427 switch (l4->si[0].addr.to.ss_family) {
1428 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001429 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.to)->sin_addr;
1430 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001431 break;
1432 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001433 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.to))->sin6_addr;
1434 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001435 break;
1436 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001437 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001438 }
Emeric Brunf769f512010-10-22 17:14:01 +02001439
Willy Tarreau37406352012-04-23 16:16:37 +02001440 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001441 return 1;
1442}
1443
David du Colombier4f92d322011-03-24 11:09:31 +01001444/* extract the connection's destination ipv6 address */
1445static int
Willy Tarreau12785782012-04-27 21:37:17 +02001446smp_fetch_dst6(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1447 const struct arg *arg_p, struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +01001448{
Willy Tarreau9b061e32012-04-07 18:03:52 +02001449 stream_sock_get_to_addr(&l4->si[0]);
David du Colombier4f92d322011-03-24 11:09:31 +01001450
Willy Tarreau6471afb2011-09-23 10:54:59 +02001451 if (l4->si[0].addr.to.ss_family != AF_INET6)
David du Colombier4f92d322011-03-24 11:09:31 +01001452 return 0;
1453
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001454 smp->type = SMP_T_IPV6;
Willy Tarreau342acb42012-04-23 22:03:39 +02001455 memcpy(smp->data.ipv6.s6_addr, ((struct sockaddr_in6 *)&l4->si[0].addr.to)->sin6_addr.s6_addr, sizeof(smp->data.ipv6.s6_addr));
David du Colombier4f92d322011-03-24 11:09:31 +01001456 return 1;
1457}
1458
Willy Tarreaua5e37562011-12-16 17:06:15 +01001459/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001460static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001461smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001462 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001463{
Willy Tarreau9b061e32012-04-07 18:03:52 +02001464 stream_sock_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001465
Willy Tarreauf853c462012-04-23 18:53:56 +02001466 smp->type = SMP_T_UINT;
1467 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001468 return 0;
1469
Willy Tarreau37406352012-04-23 16:16:37 +02001470 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001471 return 1;
1472}
1473
1474static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001475smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1476 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001477{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001478 unsigned int len_offset = arg_p[0].data.uint;
1479 unsigned int len_size = arg_p[1].data.uint;
1480 unsigned int buf_offset;
1481 unsigned int buf_size = 0;
Emericf2d7cae2010-11-05 18:13:50 +01001482 struct buffer *b;
1483 int i;
1484
1485 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1486 /* by default buf offset == len offset + len size */
1487 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1488
1489 if (!l4)
1490 return 0;
1491
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001492 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001493
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001494 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001495 return 0;
1496
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001497 if (len_offset + len_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001498 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001499
1500 for (i = 0; i < len_size; i++) {
Willy Tarreau89fa7062012-03-02 16:13:16 +01001501 buf_size = (buf_size << 8) + ((unsigned char *)b->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001502 }
1503
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001504 /* buf offset may be implicit, absolute or relative */
1505 buf_offset = len_offset + len_size;
1506 if (arg_p[2].type == ARGT_UINT)
1507 buf_offset = arg_p[2].data.uint;
1508 else if (arg_p[2].type == ARGT_SINT)
1509 buf_offset += arg_p[2].data.sint;
1510
Willy Tarreau82ea8002012-04-25 19:19:43 +02001511 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1512 /* will never match */
1513 smp->flags = 0;
1514 return 0;
1515 }
1516
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001517 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001518 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001519
1520 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001521 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001522 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001523 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001524 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001525
1526 too_short:
1527 smp->flags = SMP_F_MAY_CHANGE;
1528 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001529}
1530
1531static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001532smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1533 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001534{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001535 unsigned int buf_offset = arg_p[0].data.uint;
1536 unsigned int buf_size = arg_p[1].data.uint;
Emericf2d7cae2010-11-05 18:13:50 +01001537 struct buffer *b;
1538
1539 if (!l4)
1540 return 0;
1541
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001542 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001543
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001544 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001545 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001546
1547 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1548 /* will never match */
1549 smp->flags = 0;
1550 return 0;
1551 }
Emericf2d7cae2010-11-05 18:13:50 +01001552
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001553 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001554 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001555
1556 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001557 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001558 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001559 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001560 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001561
1562 too_short:
1563 smp->flags = SMP_F_MAY_CHANGE;
1564 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001565}
1566
Willy Tarreau21d68a62012-04-20 15:52:36 +02001567/* This function is used to validate the arguments passed to a "payload" fetch
1568 * keyword. This keyword expects two positive integers, with the second one
1569 * being strictly positive. It is assumed that the types are already the correct
1570 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1571 * filled with a pointer to an error message in case of error, that the caller
1572 * is responsible for freeing. The initial location must either be freeable or
1573 * NULL.
1574 */
1575static int val_payload(struct arg *arg, char **err_msg)
1576{
1577 if (!arg[1].data.uint) {
1578 if (err_msg)
1579 memprintf(err_msg, "payload length must be > 0");
1580 return 0;
1581 }
1582 return 1;
1583}
1584
1585/* This function is used to validate the arguments passed to a "payload_lv" fetch
1586 * keyword. This keyword allows two positive integers and an optional signed one,
1587 * with the second one being strictly positive and the third one being greater than
1588 * the opposite of the two others if negative. It is assumed that the types are
1589 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1590 * not NULL, it will be filled with a pointer to an error message in case of
1591 * error, that the caller is responsible for freeing. The initial location must
1592 * either be freeable or NULL.
1593 */
1594static int val_payload_lv(struct arg *arg, char **err_msg)
1595{
1596 if (!arg[1].data.uint) {
1597 if (err_msg)
1598 memprintf(err_msg, "payload length must be > 0");
1599 return 0;
1600 }
1601
1602 if (arg[2].type == ARGT_SINT &&
1603 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1604 if (err_msg)
1605 memprintf(err_msg, "payload offset too negative");
1606 return 0;
1607 }
1608 return 1;
1609}
1610
Willy Tarreaub6866442008-07-14 23:54:42 +02001611static struct cfg_kw_list cfg_kws = {{ },{
1612 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001613 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001614 { 0, NULL, NULL },
1615}};
1616
Willy Tarreau61612d42012-04-19 18:42:05 +02001617/* Note: must not be declared <const> as its list will be overwritten.
1618 * Please take care of keeping this list alphabetically sorted.
1619 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001620static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001621 { "dst", acl_parse_ip, smp_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001622 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001623 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1624 { "payload_lv", acl_parse_str, smp_fetch_payload_lv, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG3(2,UINT,UINT,SINT), val_payload_lv },
Willy Tarreau9fb4bc72012-04-24 00:09:26 +02001625 { "req_rdp_cookie", acl_parse_str, smp_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
Willy Tarreau32389b72012-04-23 23:13:20 +02001626 { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau4a129812012-04-25 17:31:42 +02001627 { "src", acl_parse_ip, smp_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001628 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001629 { NULL, NULL, NULL, NULL },
1630}};
1631
Willy Tarreau4a129812012-04-25 17:31:42 +02001632/* Note: must not be declared <const> as its list will be overwritten.
1633 * Note: fetches that may return multiple types must be declared as the lowest
1634 * common denominator, the type that can be casted into all other ones. For
1635 * instance v4/v6 must be declared v4.
1636 */
Willy Tarreau12785782012-04-27 21:37:17 +02001637static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001638 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau12785782012-04-27 21:37:17 +02001639 { "src6", smp_fetch_src6, 0, NULL, SMP_T_IPV6, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001640 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau12785782012-04-27 21:37:17 +02001641 { "dst6", smp_fetch_dst6, 0, NULL, SMP_T_IPV6, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001642 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001643 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1644 { "payload_lv", smp_fetch_payload_lv, ARG3(2,UINT,UINT,SINT), val_payload_lv, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreaud6281ae2012-04-26 11:23:39 +02001645 { "rdp_cookie", smp_fetch_rdp_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001646 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001647 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001648}};
1649
Willy Tarreaue6b98942007-10-29 01:09:36 +01001650__attribute__((constructor))
1651static void __tcp_protocol_init(void)
1652{
1653 protocol_register(&proto_tcpv4);
1654 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001655 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001656 cfg_register_keywords(&cfg_kws);
1657 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001658}
1659
1660
1661/*
1662 * Local variables:
1663 * c-indent-level: 8
1664 * c-basic-offset: 8
1665 * End:
1666 */