blob: 5d5ede6820db545ee82d339a89718fa65889b81e [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 Tarreauc63190d2012-05-11 14:23:52 +020051#include <proto/sock_raw.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020052#include <proto/stick_table.h>
Willy Tarreau59b94792012-05-11 16:16:40 +020053#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020054#include <proto/task.h>
Emeric Brun97679e72010-09-23 17:56:44 +020055#include <proto/buffers.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010056
Willy Tarreaue8c66af2008-01-13 18:40:14 +010057#ifdef CONFIG_HAP_CTTPROXY
58#include <import/ip_tproxy.h>
59#endif
60
Emeric Bruncf20bf12010-10-22 16:06:11 +020061static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
62static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010063
64/* Note: must not be declared <const> as its list will be overwritten */
65static struct protocol proto_tcpv4 = {
66 .name = "tcpv4",
67 .sock_domain = AF_INET,
68 .sock_type = SOCK_STREAM,
69 .sock_prot = IPPROTO_TCP,
70 .sock_family = AF_INET,
71 .sock_addrlen = sizeof(struct sockaddr_in),
72 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020073 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020074 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020075 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010076 .bind_all = tcp_bind_listeners,
77 .unbind_all = unbind_all_listeners,
78 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020079 .get_src = tcp_get_src,
80 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010081 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
82 .nb_listeners = 0,
83};
84
85/* Note: must not be declared <const> as its list will be overwritten */
86static struct protocol proto_tcpv6 = {
87 .name = "tcpv6",
88 .sock_domain = AF_INET6,
89 .sock_type = SOCK_STREAM,
90 .sock_prot = IPPROTO_TCP,
91 .sock_family = AF_INET6,
92 .sock_addrlen = sizeof(struct sockaddr_in6),
93 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020094 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020095 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020096 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010097 .bind_all = tcp_bind_listeners,
98 .unbind_all = unbind_all_listeners,
99 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200100 .get_src = tcp_get_src,
101 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100102 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
103 .nb_listeners = 0,
104};
105
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100106
David du Colombier6f5ccb12011-03-10 22:26:24 +0100107/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100108 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
109 * - 0 : ignore remote address (may even be a NULL pointer)
110 * - 1 : use provided address
111 * - 2 : use provided port
112 * - 3 : use both
113 *
114 * The function supports multiple foreign binding methods :
115 * - linux_tproxy: we directly bind to the foreign address
116 * - cttproxy: we bind to a local address then nat.
117 * The second one can be used as a fallback for the first one.
118 * This function returns 0 when everything's OK, 1 if it could not bind, to the
119 * local address, 2 if it could not bind to the foreign address.
120 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100121int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100122{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100123 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100124 int foreign_ok = 0;
125 int ret;
126
127#ifdef CONFIG_HAP_LINUX_TPROXY
128 static int ip_transp_working = 1;
129 if (flags && ip_transp_working) {
Simon Hormande072bd2011-06-24 15:11:37 +0900130 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
131 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100132 foreign_ok = 1;
133 else
134 ip_transp_working = 0;
135 }
136#endif
137 if (flags) {
138 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200139 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100140 switch (remote->ss_family) {
141 case AF_INET:
142 if (flags & 1)
143 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
144 if (flags & 2)
145 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
146 break;
147 case AF_INET6:
148 if (flags & 1)
149 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
150 if (flags & 2)
151 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
152 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100153 default:
154 /* we don't want to try to bind to an unknown address family */
155 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100156 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100157 }
158
Simon Hormande072bd2011-06-24 15:11:37 +0900159 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100160 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200161 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100162 if (ret < 0)
163 return 2;
164 }
165 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200166 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100167 if (ret < 0)
168 return 1;
169 }
170
171 if (!flags)
172 return 0;
173
174#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100175 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100176 struct in_tproxy itp1, itp2;
177 memset(&itp1, 0, sizeof(itp1));
178
179 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100180 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
181 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100182
183 /* set connect flag on socket */
184 itp2.op = TPROXY_FLAGS;
185 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
186
187 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
188 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
189 foreign_ok = 1;
190 }
191 }
192#endif
193 if (!foreign_ok)
194 /* we could not bind to a foreign address */
195 return 2;
196
197 return 0;
198}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100199
Willy Tarreau9650f372009-08-16 14:02:45 +0200200
201/*
Willy Tarreauac825402011-03-04 22:04:29 +0100202 * This function initiates a connection to the target assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200203 * (si->{target,addr.to}). A source address may be pointed to by si->addr.from
Willy Tarreauac825402011-03-04 22:04:29 +0100204 * in case of transparent proxying. Normal source bind addresses are still
205 * determined locally (due to the possible need of a source port).
206 * si->target may point either to a valid server or to a backend, depending
207 * on si->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are supported.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200208 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200209 * It can return one of :
210 * - SN_ERR_NONE if everything's OK
211 * - SN_ERR_SRVTO if there are no more servers
212 * - SN_ERR_SRVCL if the connection was refused by the server
213 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
214 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
215 * - SN_ERR_INTERNAL for any other purely internal errors
216 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
217 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100218
David du Colombier6f5ccb12011-03-10 22:26:24 +0100219int tcp_connect_server(struct stream_interface *si)
Willy Tarreau9650f372009-08-16 14:02:45 +0200220{
221 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100222 struct server *srv;
223 struct proxy *be;
224
225 switch (si->target.type) {
226 case TARG_TYPE_PROXY:
227 be = si->target.ptr.p;
228 srv = NULL;
229 break;
230 case TARG_TYPE_SERVER:
231 srv = si->target.ptr.s;
232 be = srv->proxy;
233 break;
234 default:
235 return SN_ERR_INTERNAL;
236 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200237
Willy Tarreau6471afb2011-09-23 10:54:59 +0200238 if ((fd = si->fd = socket(si->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200239 qfprintf(stderr, "Cannot get a server socket.\n");
240
241 if (errno == ENFILE)
242 send_log(be, LOG_EMERG,
243 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
244 be->id, maxfd);
245 else if (errno == EMFILE)
246 send_log(be, LOG_EMERG,
247 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
248 be->id, maxfd);
249 else if (errno == ENOBUFS || errno == ENOMEM)
250 send_log(be, LOG_EMERG,
251 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
252 be->id, maxfd);
253 /* this is a resource error */
254 return SN_ERR_RESOURCE;
255 }
256
257 if (fd >= global.maxsock) {
258 /* do not log anything there, it's a normal condition when this option
259 * is used to serialize connections to a server !
260 */
261 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
262 close(fd);
263 return SN_ERR_PRXCOND; /* it is a configuration limit */
264 }
265
266 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900267 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200268 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
269 close(fd);
270 return SN_ERR_INTERNAL;
271 }
272
273 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900274 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200275
276 if (be->options & PR_O_TCP_NOLING)
Willy Tarreauf6f82252011-11-30 18:02:24 +0100277 si->flags |= SI_FL_NOLINGER;
Willy Tarreau9650f372009-08-16 14:02:45 +0200278
279 /* allow specific binding :
280 * - server-specific at first
281 * - proxy-specific next
282 */
283 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200284 int ret, flags = 0;
285
Willy Tarreau9650f372009-08-16 14:02:45 +0200286 switch (srv->state & SRV_TPROXY_MASK) {
287 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200288 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200289 flags = 3;
290 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200291 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200292 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200293 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200294 break;
295 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200296
Willy Tarreau9650f372009-08-16 14:02:45 +0200297#ifdef SO_BINDTODEVICE
298 /* Note: this might fail if not CAP_NET_RAW */
299 if (srv->iface_name)
300 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
301#endif
302
303 if (srv->sport_range) {
304 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100305 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200306
307 ret = 1;
308 src = srv->source_addr;
309
310 do {
311 /* note: in case of retry, we may have to release a previously
312 * allocated port, hence this loop's construct.
313 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200314 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
315 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200316
317 if (!attempts)
318 break;
319 attempts--;
320
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200321 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
322 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200323 break;
324
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200325 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200326 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200327
Willy Tarreau6471afb2011-09-23 10:54:59 +0200328 ret = tcp_bind_socket(fd, flags, &src, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200329 } while (ret != 0); /* binding NOK */
330 }
331 else {
Willy Tarreau6471afb2011-09-23 10:54:59 +0200332 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200333 }
334
335 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200336 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
337 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200338 close(fd);
339
340 if (ret == 1) {
341 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
342 be->id, srv->id);
343 send_log(be, LOG_EMERG,
344 "Cannot bind to source address before connect() for server %s/%s.\n",
345 be->id, srv->id);
346 } else {
347 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
348 be->id, srv->id);
349 send_log(be, LOG_EMERG,
350 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
351 be->id, srv->id);
352 }
353 return SN_ERR_RESOURCE;
354 }
355 }
356 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200357 int ret, flags = 0;
358
Willy Tarreau9650f372009-08-16 14:02:45 +0200359 switch (be->options & PR_O_TPXY_MASK) {
360 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200361 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200362 flags = 3;
363 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200364 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200365 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200366 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200367 break;
368 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200369
Willy Tarreau9650f372009-08-16 14:02:45 +0200370#ifdef SO_BINDTODEVICE
371 /* Note: this might fail if not CAP_NET_RAW */
372 if (be->iface_name)
373 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
374#endif
Willy Tarreau6471afb2011-09-23 10:54:59 +0200375 ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200376 if (ret) {
377 close(fd);
378 if (ret == 1) {
379 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
380 be->id);
381 send_log(be, LOG_EMERG,
382 "Cannot bind to source address before connect() for proxy %s.\n",
383 be->id);
384 } else {
385 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
386 be->id);
387 send_log(be, LOG_EMERG,
388 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
389 be->id);
390 }
391 return SN_ERR_RESOURCE;
392 }
393 }
394
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400395#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200396 /* disabling tcp quick ack now allows the first request to leave the
397 * machine with the first ACK. We only do this if there are pending
398 * data in the buffer.
399 */
Willy Tarreau2e046c62012-03-01 16:08:30 +0100400 if ((be->options2 & PR_O2_SMARTCON) && si->ob->o)
Simon Hormande072bd2011-06-24 15:11:37 +0900401 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200402#endif
403
Willy Tarreaue803de22010-01-21 17:43:04 +0100404 if (global.tune.server_sndbuf)
405 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
406
407 if (global.tune.server_rcvbuf)
408 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
409
Willy Tarreau9b061e32012-04-07 18:03:52 +0200410 si->flags &= ~SI_FL_FROM_SET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200411 if ((connect(fd, (struct sockaddr *)&si->addr.to, get_addr_len(&si->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200412 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
413
414 if (errno == EAGAIN || errno == EADDRINUSE) {
415 char *msg;
416 if (errno == EAGAIN) /* no free ports left, try again later */
417 msg = "no free ports";
418 else
419 msg = "local address already in use";
420
421 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200422 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
423 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200424 close(fd);
425 send_log(be, LOG_EMERG,
426 "Connect() failed for server %s/%s: %s.\n",
427 be->id, srv->id, msg);
428 return SN_ERR_RESOURCE;
429 } else if (errno == ETIMEDOUT) {
430 //qfprintf(stderr,"Connect(): ETIMEDOUT");
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_SRVTO;
435 } else {
436 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
437 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200438 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
439 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200440 close(fd);
441 return SN_ERR_SRVCL;
442 }
443 }
444
William Lallemandb7ff6a32012-03-02 14:35:21 +0100445 /* needs src ip/port for logging */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200446 if (si->flags & SI_FL_SRC_ADDR)
Willy Tarreau59b94792012-05-11 16:16:40 +0200447 si_get_from_addr(si);
William Lallemandb7ff6a32012-03-02 14:35:21 +0100448
Willy Tarreau9650f372009-08-16 14:02:45 +0200449 fdtab[fd].owner = si;
450 fdtab[fd].state = FD_STCONN; /* connection in progress */
451 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau1b79bde2012-05-07 17:01:39 +0200452 fdtab[fd].cb[DIR_RD].f = si->sock.read;
Willy Tarreau9650f372009-08-16 14:02:45 +0200453 fdtab[fd].cb[DIR_RD].b = si->ib;
Willy Tarreau1b79bde2012-05-07 17:01:39 +0200454 fdtab[fd].cb[DIR_WR].f = si->sock.write;
Willy Tarreau9650f372009-08-16 14:02:45 +0200455 fdtab[fd].cb[DIR_WR].b = si->ob;
456
Willy Tarreau6471afb2011-09-23 10:54:59 +0200457 fdinfo[fd].peeraddr = (struct sockaddr *)&si->addr.to;
458 fdinfo[fd].peerlen = get_addr_len(&si->addr.to);
Willy Tarreau9650f372009-08-16 14:02:45 +0200459
460 fd_insert(fd);
461 EV_FD_SET(fd, DIR_WR); /* for connect status */
462
463 si->state = SI_ST_CON;
464 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
465 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
466
467 return SN_ERR_NONE; /* connection is OK */
468}
469
470
Willy Tarreau59b94792012-05-11 16:16:40 +0200471/*
472 * Retrieves the source address for the socket <fd>, with <dir> indicating
473 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
474 * success, -1 in case of error. The socket's source address is stored in
475 * <sa> for <salen> bytes.
476 */
477int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
478{
479 if (dir)
480 return getsockname(fd, sa, &salen);
481 else
482 return getpeername(fd, sa, &salen);
483}
484
485
486/*
487 * Retrieves the original destination address for the socket <fd>, with <dir>
488 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
489 * listener, if the original destination address was translated, the original
490 * address is retrieved. It returns 0 in case of success, -1 in case of error.
491 * The socket's source address is stored in <sa> for <salen> bytes.
492 */
493int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
494{
495 if (dir)
496 return getpeername(fd, sa, &salen);
497#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
498 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
499 return 0;
500#endif
501 else
502 return getsockname(fd, sa, &salen);
503}
504
505
Willy Tarreaue6b98942007-10-29 01:09:36 +0100506/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
507 * an error message in <err> if the message is at most <errlen> bytes long
508 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
509 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
510 * was alright and that no message was returned. ERR_RETRYABLE means that an
511 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700512 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100513 * the meaning of the error, but just indicate that a message is present which
514 * should be displayed with the respective level. Last, ERR_ABORT indicates
515 * that it's pointless to try to start other listeners. No error message is
516 * returned if errlen is NULL.
517 */
518int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
519{
520 __label__ tcp_return, tcp_close_return;
521 int fd, err;
522 const char *msg = NULL;
523
524 /* ensure we never return garbage */
525 if (errmsg && errlen)
526 *errmsg = 0;
527
528 if (listener->state != LI_ASSIGNED)
529 return ERR_NONE; /* already bound */
530
531 err = ERR_NONE;
532
533 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
534 err |= ERR_RETRYABLE | ERR_ALERT;
535 msg = "cannot create listening socket";
536 goto tcp_return;
537 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100538
Willy Tarreaue6b98942007-10-29 01:09:36 +0100539 if (fd >= global.maxsock) {
540 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
541 msg = "not enough free sockets (raise '-n' parameter)";
542 goto tcp_close_return;
543 }
544
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200545 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100546 err |= ERR_FATAL | ERR_ALERT;
547 msg = "cannot make socket non-blocking";
548 goto tcp_close_return;
549 }
550
Simon Hormande072bd2011-06-24 15:11:37 +0900551 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100552 /* not fatal but should be reported */
553 msg = "cannot do so_reuseaddr";
554 err |= ERR_ALERT;
555 }
556
557 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900558 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100559
Willy Tarreaue6b98942007-10-29 01:09:36 +0100560#ifdef SO_REUSEPORT
561 /* OpenBSD supports this. As it's present in old libc versions of Linux,
562 * it might return an error that we will silently ignore.
563 */
Simon Hormande072bd2011-06-24 15:11:37 +0900564 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100565#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100566#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100567 if ((listener->options & LI_O_FOREIGN)
Simon Hormande072bd2011-06-24 15:11:37 +0900568 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
569 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100570 msg = "cannot make listening socket transparent";
571 err |= ERR_ALERT;
572 }
573#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100574#ifdef SO_BINDTODEVICE
575 /* Note: this might fail if not CAP_NET_RAW */
576 if (listener->interface) {
577 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100578 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100579 msg = "cannot bind listener to device";
580 err |= ERR_WARN;
581 }
582 }
583#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400584#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100585 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400586 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200587 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
588 msg = "cannot set MSS";
589 err |= ERR_WARN;
590 }
591 }
592#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200593#if defined(TCP_DEFER_ACCEPT)
594 if (listener->options & LI_O_DEF_ACCEPT) {
595 /* defer accept by up to one second */
596 int accept_delay = 1;
597 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
598 msg = "cannot enable DEFER_ACCEPT";
599 err |= ERR_WARN;
600 }
601 }
602#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100603 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
604 err |= ERR_RETRYABLE | ERR_ALERT;
605 msg = "cannot bind socket";
606 goto tcp_close_return;
607 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100608
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100609 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100610 err |= ERR_RETRYABLE | ERR_ALERT;
611 msg = "cannot listen to socket";
612 goto tcp_close_return;
613 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100614
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400615#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200616 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900617 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200618#endif
619
Willy Tarreaue6b98942007-10-29 01:09:36 +0100620 /* the socket is ready */
621 listener->fd = fd;
622 listener->state = LI_LISTEN;
623
Willy Tarreaueabf3132008-08-29 23:36:51 +0200624 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100625 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaueb472682010-05-28 18:46:57 +0200626 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
627 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
628 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
629 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200630
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200631 fdinfo[fd].peeraddr = NULL;
632 fdinfo[fd].peerlen = 0;
Willy Tarreaueb472682010-05-28 18:46:57 +0200633 fd_insert(fd);
634
Willy Tarreaue6b98942007-10-29 01:09:36 +0100635 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100636 if (msg && errlen) {
637 char pn[INET6_ADDRSTRLEN];
638
Willy Tarreau631f01c2011-09-05 00:36:48 +0200639 addr_to_str(&listener->addr, pn, sizeof(pn));
640 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100641 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100642 return err;
643
644 tcp_close_return:
645 close(fd);
646 goto tcp_return;
647}
648
649/* This function creates all TCP sockets bound to the protocol entry <proto>.
650 * It is intended to be used as the protocol's bind_all() function.
651 * The sockets will be registered but not added to any fd_set, in order not to
652 * loose them across the fork(). A call to enable_all_listeners() is needed
653 * to complete initialization. The return value is composed from ERR_*.
654 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200655static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100656{
657 struct listener *listener;
658 int err = ERR_NONE;
659
660 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200661 err |= tcp_bind_listener(listener, errmsg, errlen);
662 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100663 break;
664 }
665
666 return err;
667}
668
669/* Add listener to the list of tcpv4 listeners. The listener's state
670 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
671 * listeners is updated. This is the function to use to add a new listener.
672 */
673void tcpv4_add_listener(struct listener *listener)
674{
675 if (listener->state != LI_INIT)
676 return;
677 listener->state = LI_ASSIGNED;
678 listener->proto = &proto_tcpv4;
679 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
680 proto_tcpv4.nb_listeners++;
681}
682
683/* Add listener to the list of tcpv4 listeners. The listener's state
684 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
685 * listeners is updated. This is the function to use to add a new listener.
686 */
687void tcpv6_add_listener(struct listener *listener)
688{
689 if (listener->state != LI_INIT)
690 return;
691 listener->state = LI_ASSIGNED;
692 listener->proto = &proto_tcpv6;
693 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
694 proto_tcpv6.nb_listeners++;
695}
696
Willy Tarreauedcf6682008-11-30 23:15:34 +0100697/* This function performs the TCP request analysis on the current request. It
698 * returns 1 if the processing can continue on next analysers, or zero if it
699 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200700 * request. It relies on buffers flags, and updates s->req->analysers. The
701 * function may be called for frontend rules and backend rules. It only relies
702 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100703 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200704int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100705{
706 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200707 struct stksess *ts;
708 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100709 int partial;
710
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100711 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 +0100712 now_ms, __FUNCTION__,
713 s,
714 req,
715 req->rex, req->wex,
716 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100717 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100718 req->analysers);
719
Willy Tarreauedcf6682008-11-30 23:15:34 +0100720 /* We don't know whether we have enough data, so must proceed
721 * this way :
722 * - iterate through all rules in their declaration order
723 * - if one rule returns MISS, it means the inspect delay is
724 * not over yet, then return immediately, otherwise consider
725 * it as a non-match.
726 * - if one rule returns OK, then return OK
727 * - if one rule returns KO, then return KO
728 */
729
Willy Tarreaub824b002010-09-29 16:36:16 +0200730 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 +0200731 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100732 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200733 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100734
Willy Tarreaufb356202010-08-03 14:02:05 +0200735 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100736 int ret = ACL_PAT_PASS;
737
738 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200739 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100740 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200741 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100742 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200743 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
744 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100745 return 0;
746 }
747
748 ret = acl_pass(ret);
749 if (rule->cond->pol == ACL_COND_UNLESS)
750 ret = !ret;
751 }
752
753 if (ret) {
754 /* we have a matching rule. */
755 if (rule->action == TCP_ACT_REJECT) {
756 buffer_abort(req);
757 buffer_abort(s->rep);
758 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200759
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100760 s->be->be_counters.denied_req++;
761 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200762 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200763 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200764
Willy Tarreauedcf6682008-11-30 23:15:34 +0100765 if (!(s->flags & SN_ERR_MASK))
766 s->flags |= SN_ERR_PRXCOND;
767 if (!(s->flags & SN_FINST_MASK))
768 s->flags |= SN_FINST_R;
769 return 0;
770 }
Willy Tarreau56123282010-08-06 19:06:56 +0200771 else if (rule->action == TCP_ACT_TRK_SC1) {
772 if (!s->stkctr1_entry) {
773 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200774 * Also, note that right now we can only track SRC so we
775 * don't check how to get the key, but later we may need
776 * to consider rule->act_prm->trk_ctr.type.
777 */
778 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100779 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200780 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200781 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200782 if (s->fe != s->be)
783 s->flags |= SN_BE_TRACK_SC1;
784 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200785 }
786 }
Willy Tarreau56123282010-08-06 19:06:56 +0200787 else if (rule->action == TCP_ACT_TRK_SC2) {
788 if (!s->stkctr2_entry) {
789 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200790 * Also, note that right now we can only track SRC so we
791 * don't check how to get the key, but later we may need
792 * to consider rule->act_prm->trk_ctr.type.
793 */
794 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100795 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200796 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200797 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200798 if (s->fe != s->be)
799 s->flags |= SN_BE_TRACK_SC2;
800 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200801 }
802 }
803 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100804 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200805 break;
806 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100807 }
808 }
809
810 /* if we get there, it means we have no rule which matches, or
811 * we have an explicit accept, so we apply the default accept.
812 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200813 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100814 req->analyse_exp = TICK_ETERNITY;
815 return 1;
816}
817
Emeric Brun97679e72010-09-23 17:56:44 +0200818/* This function performs the TCP response analysis on the current response. It
819 * returns 1 if the processing can continue on next analysers, or zero if it
820 * needs more data, encounters an error, or wants to immediately abort the
821 * response. It relies on buffers flags, and updates s->rep->analysers. The
822 * function may be called for backend rules.
823 */
824int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit)
825{
826 struct tcp_rule *rule;
827 int partial;
828
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100829 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 +0200830 now_ms, __FUNCTION__,
831 s,
832 rep,
833 rep->rex, rep->wex,
834 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100835 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200836 rep->analysers);
837
838 /* We don't know whether we have enough data, so must proceed
839 * this way :
840 * - iterate through all rules in their declaration order
841 * - if one rule returns MISS, it means the inspect delay is
842 * not over yet, then return immediately, otherwise consider
843 * it as a non-match.
844 * - if one rule returns OK, then return OK
845 * - if one rule returns KO, then return KO
846 */
847
848 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200849 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200850 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200851 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200852
853 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
854 int ret = ACL_PAT_PASS;
855
856 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200857 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200858 if (ret == ACL_PAT_MISS) {
859 /* just set the analyser timeout once at the beginning of the response */
860 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
861 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
862 return 0;
863 }
864
865 ret = acl_pass(ret);
866 if (rule->cond->pol == ACL_COND_UNLESS)
867 ret = !ret;
868 }
869
870 if (ret) {
871 /* we have a matching rule. */
872 if (rule->action == TCP_ACT_REJECT) {
873 buffer_abort(rep);
874 buffer_abort(s->req);
875 rep->analysers = 0;
876
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100877 s->be->be_counters.denied_resp++;
878 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200879 if (s->listener->counters)
880 s->listener->counters->denied_resp++;
881
882 if (!(s->flags & SN_ERR_MASK))
883 s->flags |= SN_ERR_PRXCOND;
884 if (!(s->flags & SN_FINST_MASK))
885 s->flags |= SN_FINST_D;
886 return 0;
887 }
888 else {
889 /* otherwise accept */
890 break;
891 }
892 }
893 }
894
895 /* if we get there, it means we have no rule which matches, or
896 * we have an explicit accept, so we apply the default accept.
897 */
898 rep->analysers &= ~an_bit;
899 rep->analyse_exp = TICK_ETERNITY;
900 return 1;
901}
902
903
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200904/* This function performs the TCP layer4 analysis on the current request. It
905 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
906 * matches or if no more rule matches. It can only use rules which don't need
907 * any data.
908 */
909int tcp_exec_req_rules(struct session *s)
910{
911 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200912 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200913 struct stktable *t = NULL;
914 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200915 int ret;
916
917 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
918 ret = ACL_PAT_PASS;
919
920 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200921 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200922 ret = acl_pass(ret);
923 if (rule->cond->pol == ACL_COND_UNLESS)
924 ret = !ret;
925 }
926
927 if (ret) {
928 /* we have a matching rule. */
929 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100930 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200931 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200932 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200933
934 if (!(s->flags & SN_ERR_MASK))
935 s->flags |= SN_ERR_PRXCOND;
936 if (!(s->flags & SN_FINST_MASK))
937 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200938 result = 0;
939 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200940 }
Willy Tarreau56123282010-08-06 19:06:56 +0200941 else if (rule->action == TCP_ACT_TRK_SC1) {
942 if (!s->stkctr1_entry) {
943 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200944 * Also, note that right now we can only track SRC so we
945 * don't check how to get the key, but later we may need
946 * to consider rule->act_prm->trk_ctr.type.
947 */
948 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100949 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200950 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200951 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200952 }
953 }
Willy Tarreau56123282010-08-06 19:06:56 +0200954 else if (rule->action == TCP_ACT_TRK_SC2) {
955 if (!s->stkctr2_entry) {
956 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200957 * Also, note that right now we can only track SRC so we
958 * don't check how to get the key, but later we may need
959 * to consider rule->act_prm->trk_ctr.type.
960 */
961 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100962 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200963 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200964 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200965 }
966 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200967 else {
968 /* otherwise it's an accept */
969 break;
970 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200971 }
972 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200973 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200974}
975
Emeric Brun97679e72010-09-23 17:56:44 +0200976/* Parse a tcp-response rule. Return a negative value in case of failure */
977static int tcp_parse_response_rule(char **args, int arg, int section_type,
978 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200979 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +0200980{
981 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200982 memprintf(err, "%s %s is only allowed in 'backend' sections",
983 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +0200984 return -1;
985 }
986
987 if (strcmp(args[arg], "accept") == 0) {
988 arg++;
989 rule->action = TCP_ACT_ACCEPT;
990 }
991 else if (strcmp(args[arg], "reject") == 0) {
992 arg++;
993 rule->action = TCP_ACT_REJECT;
994 }
995 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200996 memprintf(err,
997 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
998 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +0200999 return -1;
1000 }
1001
1002 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001003 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1004 memprintf(err,
1005 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1006 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001007 return -1;
1008 }
1009 }
1010 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001011 memprintf(err,
1012 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001013 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1014 return -1;
1015 }
1016 return 0;
1017}
1018
1019
1020
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001021/* Parse a tcp-request rule. Return a negative value in case of failure */
1022static int tcp_parse_request_rule(char **args, int arg, int section_type,
1023 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001024 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001025{
1026 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001027 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1028 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001029 return -1;
1030 }
1031
1032 if (!strcmp(args[arg], "accept")) {
1033 arg++;
1034 rule->action = TCP_ACT_ACCEPT;
1035 }
1036 else if (!strcmp(args[arg], "reject")) {
1037 arg++;
1038 rule->action = TCP_ACT_REJECT;
1039 }
Willy Tarreau56123282010-08-06 19:06:56 +02001040 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001041 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001042 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001043
1044 arg++;
1045 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001046 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001047
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001048 if (ret < 0) { /* nb: warnings are not handled yet */
1049 memprintf(err,
1050 "'%s %s %s' : %s in %s '%s'",
1051 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1052 return ret;
1053 }
Willy Tarreau56123282010-08-06 19:06:56 +02001054 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001055 }
Willy Tarreau56123282010-08-06 19:06:56 +02001056 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001057 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001058 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001059
1060 arg++;
1061 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001062 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001063
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001064 if (ret < 0) { /* nb: warnings are not handled yet */
1065 memprintf(err,
1066 "'%s %s %s' : %s in %s '%s'",
1067 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1068 return ret;
1069 }
Willy Tarreau56123282010-08-06 19:06:56 +02001070 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001071 }
1072 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001073 memprintf(err,
1074 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1075 "or 'track-sc2' in %s '%s' (got '%s')",
1076 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001077 return -1;
1078 }
1079
1080 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001081 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1082 memprintf(err,
1083 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1084 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001085 return -1;
1086 }
1087 }
1088 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001089 memprintf(err,
1090 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001091 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1092 return -1;
1093 }
1094 return 0;
1095}
1096
Emeric Brun97679e72010-09-23 17:56:44 +02001097/* This function should be called to parse a line starting with the "tcp-response"
1098 * keyword.
1099 */
1100static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001101 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001102{
1103 const char *ptr = NULL;
1104 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001105 int warn = 0;
1106 int arg;
1107 struct tcp_rule *rule;
1108
1109 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001110 memprintf(err, "missing argument for '%s' in %s '%s'",
1111 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001112 return -1;
1113 }
1114
1115 if (strcmp(args[1], "inspect-delay") == 0) {
1116 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001117 memprintf(err, "%s %s is only allowed in 'backend' sections",
1118 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001119 return -1;
1120 }
1121
1122 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001123 memprintf(err,
1124 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1125 args[0], args[1], proxy_type_str(curpx), curpx->id);
1126 if (ptr)
1127 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001128 return -1;
1129 }
1130
1131 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001132 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1133 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001134 return 1;
1135 }
1136 curpx->tcp_rep.inspect_delay = val;
1137 return 0;
1138 }
1139
Simon Hormande072bd2011-06-24 15:11:37 +09001140 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001141 LIST_INIT(&rule->list);
1142 arg = 1;
1143
1144 if (strcmp(args[1], "content") == 0) {
1145 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001146 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001147 goto error;
1148
1149 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1150 struct acl *acl;
1151 const char *name;
1152
1153 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1154 name = acl ? acl->name : "(unknown)";
1155
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001156 memprintf(err,
1157 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1158 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001159 warn++;
1160 }
1161
1162 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1163 }
1164 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001165 memprintf(err,
1166 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1167 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001168 goto error;
1169 }
1170
1171 return warn;
1172 error:
1173 free(rule);
1174 return -1;
1175}
1176
1177
Willy Tarreaub6866442008-07-14 23:54:42 +02001178/* This function should be called to parse a line starting with the "tcp-request"
1179 * keyword.
1180 */
1181static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001182 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001183{
1184 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001185 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001186 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001187 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001188 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001189
1190 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001191 if (curpx == defpx)
1192 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1193 else
1194 memprintf(err, "missing argument for '%s' in %s '%s'",
1195 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001196 return -1;
1197 }
1198
1199 if (!strcmp(args[1], "inspect-delay")) {
1200 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001201 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1202 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001203 return -1;
1204 }
1205
Willy Tarreaub6866442008-07-14 23:54:42 +02001206 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001207 memprintf(err,
1208 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1209 args[0], args[1], proxy_type_str(curpx), curpx->id);
1210 if (ptr)
1211 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001212 return -1;
1213 }
1214
1215 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001216 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1217 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001218 return 1;
1219 }
1220 curpx->tcp_req.inspect_delay = val;
1221 return 0;
1222 }
1223
Simon Hormande072bd2011-06-24 15:11:37 +09001224 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001225 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001226 arg = 1;
1227
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001228 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001229 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001230 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001231 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001232
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001233 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001234 struct acl *acl;
1235 const char *name;
1236
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001237 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001238 name = acl ? acl->name : "(unknown)";
1239
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001240 memprintf(err,
1241 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1242 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001243 warn++;
1244 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001245 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001246 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001247 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001248 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001249
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001250 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001251 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1252 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001253 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001254 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001255
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001256 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001257 goto error;
1258
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001259 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1260 struct acl *acl;
1261 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001262
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001263 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1264 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001265
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001266 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001267 memprintf(err,
1268 "'%s %s' may not reference acl '%s' which makes use of "
1269 "payload in %s '%s'. Please use '%s content' for this.",
1270 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001271 goto error;
1272 }
1273 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001274 memprintf(err,
1275 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1276 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001277 warn++;
1278 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001279 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001280 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001281 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001282 if (curpx == defpx)
1283 memprintf(err,
1284 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1285 args[0], args[1]);
1286 else
1287 memprintf(err,
1288 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1289 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001290 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001291 }
1292
Willy Tarreau1a687942010-05-23 22:40:30 +02001293 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001294 error:
1295 free(rule);
1296 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001297}
1298
Willy Tarreau645513a2010-05-24 20:55:15 +02001299
1300/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001301/* All supported sample fetch functios must be declared here */
1302/************************************************************************/
1303
1304/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001305 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001306 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1307 * is a string (cookie name), other types will lead to undefined behaviour.
1308 */
1309int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001310smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001311 const struct arg *args, struct sample *smp)
1312{
1313 int bleft;
1314 const unsigned char *data;
1315
1316 if (!l4 || !l4->req)
1317 return 0;
1318
1319 smp->flags = 0;
1320 smp->type = SMP_T_CSTR;
1321
1322 bleft = l4->req->i;
1323 if (bleft <= 11)
1324 goto too_short;
1325
1326 data = (const unsigned char *)l4->req->p + 11;
1327 bleft -= 11;
1328
1329 if (bleft <= 7)
1330 goto too_short;
1331
1332 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1333 goto not_cookie;
1334
1335 data += 7;
1336 bleft -= 7;
1337
1338 while (bleft > 0 && *data == ' ') {
1339 data++;
1340 bleft--;
1341 }
1342
1343 if (args) {
1344
1345 if (bleft <= args->data.str.len)
1346 goto too_short;
1347
1348 if ((data[args->data.str.len] != '=') ||
1349 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1350 goto not_cookie;
1351
1352 data += args->data.str.len + 1;
1353 bleft -= args->data.str.len + 1;
1354 } else {
1355 while (bleft > 0 && *data != '=') {
1356 if (*data == '\r' || *data == '\n')
1357 goto not_cookie;
1358 data++;
1359 bleft--;
1360 }
1361
1362 if (bleft < 1)
1363 goto too_short;
1364
1365 if (*data != '=')
1366 goto not_cookie;
1367
1368 data++;
1369 bleft--;
1370 }
1371
1372 /* data points to cookie value */
1373 smp->data.str.str = (char *)data;
1374 smp->data.str.len = 0;
1375
1376 while (bleft > 0 && *data != '\r') {
1377 data++;
1378 bleft--;
1379 }
1380
1381 if (bleft < 2)
1382 goto too_short;
1383
1384 if (data[0] != '\r' || data[1] != '\n')
1385 goto not_cookie;
1386
1387 smp->data.str.len = (char *)data - smp->data.str.str;
1388 smp->flags = SMP_F_VOLATILE;
1389 return 1;
1390
1391 too_short:
1392 smp->flags = SMP_F_MAY_CHANGE;
1393 not_cookie:
1394 return 0;
1395}
1396
Willy Tarreau32389b72012-04-23 23:13:20 +02001397/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001398/* All supported ACL keywords must be declared here. */
1399/************************************************************************/
1400
Willy Tarreau32389b72012-04-23 23:13:20 +02001401/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1402static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001403acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001404 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001405{
1406 int ret;
1407
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001408 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001409
1410 if (smp->flags & SMP_F_MAY_CHANGE)
1411 return 0;
1412
1413 smp->flags = SMP_F_VOLATILE;
1414 smp->type = SMP_T_UINT;
1415 smp->data.uint = ret;
1416 return 1;
1417}
1418
1419
Willy Tarreau4a129812012-04-25 17:31:42 +02001420/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001421static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001422smp_fetch_src(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 Tarreauf4362b32011-12-16 17:49:52 +01001425 switch (l4->si[0].addr.from.ss_family) {
1426 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001427 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.from)->sin_addr;
1428 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001429 break;
1430 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001431 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.from))->sin6_addr;
1432 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001433 break;
1434 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001435 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001436 }
Emeric Brunf769f512010-10-22 17:14:01 +02001437
Willy Tarreau37406352012-04-23 16:16:37 +02001438 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001439 return 1;
1440}
1441
Willy Tarreaua5e37562011-12-16 17:06:15 +01001442/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001443static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001444smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001445 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001446{
Willy Tarreauf853c462012-04-23 18:53:56 +02001447 smp->type = SMP_T_UINT;
1448 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001449 return 0;
1450
Willy Tarreau37406352012-04-23 16:16:37 +02001451 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001452 return 1;
1453}
1454
Willy Tarreau4a129812012-04-25 17:31:42 +02001455/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001456static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001457smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001458 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001459{
Willy Tarreau59b94792012-05-11 16:16:40 +02001460 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001461
Willy Tarreauf4362b32011-12-16 17:49:52 +01001462 switch (l4->si[0].addr.to.ss_family) {
1463 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001464 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.to)->sin_addr;
1465 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001466 break;
1467 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001468 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.to))->sin6_addr;
1469 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001470 break;
1471 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001472 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001473 }
Emeric Brunf769f512010-10-22 17:14:01 +02001474
Willy Tarreau37406352012-04-23 16:16:37 +02001475 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001476 return 1;
1477}
1478
Willy Tarreaua5e37562011-12-16 17:06:15 +01001479/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001480static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001481smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001482 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001483{
Willy Tarreau59b94792012-05-11 16:16:40 +02001484 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001485
Willy Tarreauf853c462012-04-23 18:53:56 +02001486 smp->type = SMP_T_UINT;
1487 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001488 return 0;
1489
Willy Tarreau37406352012-04-23 16:16:37 +02001490 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001491 return 1;
1492}
1493
1494static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001495smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1496 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001497{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001498 unsigned int len_offset = arg_p[0].data.uint;
1499 unsigned int len_size = arg_p[1].data.uint;
1500 unsigned int buf_offset;
1501 unsigned int buf_size = 0;
Emericf2d7cae2010-11-05 18:13:50 +01001502 struct buffer *b;
1503 int i;
1504
1505 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1506 /* by default buf offset == len offset + len size */
1507 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1508
1509 if (!l4)
1510 return 0;
1511
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001512 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001513
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001514 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001515 return 0;
1516
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001517 if (len_offset + len_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001518 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001519
1520 for (i = 0; i < len_size; i++) {
Willy Tarreau89fa7062012-03-02 16:13:16 +01001521 buf_size = (buf_size << 8) + ((unsigned char *)b->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001522 }
1523
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001524 /* buf offset may be implicit, absolute or relative */
1525 buf_offset = len_offset + len_size;
1526 if (arg_p[2].type == ARGT_UINT)
1527 buf_offset = arg_p[2].data.uint;
1528 else if (arg_p[2].type == ARGT_SINT)
1529 buf_offset += arg_p[2].data.sint;
1530
Willy Tarreau82ea8002012-04-25 19:19:43 +02001531 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1532 /* will never match */
1533 smp->flags = 0;
1534 return 0;
1535 }
1536
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001537 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001538 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001539
1540 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001541 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001542 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001543 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001544 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001545
1546 too_short:
1547 smp->flags = SMP_F_MAY_CHANGE;
1548 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001549}
1550
1551static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001552smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1553 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001554{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001555 unsigned int buf_offset = arg_p[0].data.uint;
1556 unsigned int buf_size = arg_p[1].data.uint;
Emericf2d7cae2010-11-05 18:13:50 +01001557 struct buffer *b;
1558
1559 if (!l4)
1560 return 0;
1561
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001562 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001563
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001564 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001565 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001566
1567 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1568 /* will never match */
1569 smp->flags = 0;
1570 return 0;
1571 }
Emericf2d7cae2010-11-05 18:13:50 +01001572
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001573 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001574 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001575
1576 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001577 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001578 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001579 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001580 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001581
1582 too_short:
1583 smp->flags = SMP_F_MAY_CHANGE;
1584 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001585}
1586
Willy Tarreau21d68a62012-04-20 15:52:36 +02001587/* This function is used to validate the arguments passed to a "payload" fetch
1588 * keyword. This keyword expects two positive integers, with the second one
1589 * being strictly positive. It is assumed that the types are already the correct
1590 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1591 * filled with a pointer to an error message in case of error, that the caller
1592 * is responsible for freeing. The initial location must either be freeable or
1593 * NULL.
1594 */
1595static int val_payload(struct arg *arg, char **err_msg)
1596{
1597 if (!arg[1].data.uint) {
1598 if (err_msg)
1599 memprintf(err_msg, "payload length must be > 0");
1600 return 0;
1601 }
1602 return 1;
1603}
1604
1605/* This function is used to validate the arguments passed to a "payload_lv" fetch
1606 * keyword. This keyword allows two positive integers and an optional signed one,
1607 * with the second one being strictly positive and the third one being greater than
1608 * the opposite of the two others if negative. It is assumed that the types are
1609 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1610 * not NULL, it will be filled with a pointer to an error message in case of
1611 * error, that the caller is responsible for freeing. The initial location must
1612 * either be freeable or NULL.
1613 */
1614static int val_payload_lv(struct arg *arg, char **err_msg)
1615{
1616 if (!arg[1].data.uint) {
1617 if (err_msg)
1618 memprintf(err_msg, "payload length must be > 0");
1619 return 0;
1620 }
1621
1622 if (arg[2].type == ARGT_SINT &&
1623 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1624 if (err_msg)
1625 memprintf(err_msg, "payload offset too negative");
1626 return 0;
1627 }
1628 return 1;
1629}
1630
Willy Tarreaub6866442008-07-14 23:54:42 +02001631static struct cfg_kw_list cfg_kws = {{ },{
1632 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001633 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001634 { 0, NULL, NULL },
1635}};
1636
Willy Tarreau61612d42012-04-19 18:42:05 +02001637/* Note: must not be declared <const> as its list will be overwritten.
1638 * Please take care of keeping this list alphabetically sorted.
1639 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001640static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001641 { "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 +02001642 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001643 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1644 { "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 +02001645 { "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 +02001646 { "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 +02001647 { "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 +02001648 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001649 { NULL, NULL, NULL, NULL },
1650}};
1651
Willy Tarreau4a129812012-04-25 17:31:42 +02001652/* Note: must not be declared <const> as its list will be overwritten.
1653 * Note: fetches that may return multiple types must be declared as the lowest
1654 * common denominator, the type that can be casted into all other ones. For
1655 * instance v4/v6 must be declared v4.
1656 */
Willy Tarreau12785782012-04-27 21:37:17 +02001657static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001658 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001659 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001660 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001661 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1662 { "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 +02001663 { "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 +02001664 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001665 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001666}};
1667
Willy Tarreaue6b98942007-10-29 01:09:36 +01001668__attribute__((constructor))
1669static void __tcp_protocol_init(void)
1670{
1671 protocol_register(&proto_tcpv4);
1672 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001673 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001674 cfg_register_keywords(&cfg_kws);
1675 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001676}
1677
1678
1679/*
1680 * Local variables:
1681 * c-indent-level: 8
1682 * c-basic-offset: 8
1683 * End:
1684 */