blob: c1a3e6ff0c67db7f5d2aab5b270e3e1ebb725b19 [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 Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020044//#include <proto/frontend.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020045#include <proto/log.h>
46#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010047#include <proto/protocols.h>
48#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020049#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020050#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020051#include <proto/session.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020052#include <proto/sock_raw.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/stick_table.h>
Willy Tarreau59b94792012-05-11 16:16:40 +020054#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020055#include <proto/task.h>
Emeric Brun97679e72010-09-23 17:56:44 +020056#include <proto/buffers.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010057
Willy Tarreaue8c66af2008-01-13 18:40:14 +010058#ifdef CONFIG_HAP_CTTPROXY
59#include <import/ip_tproxy.h>
60#endif
61
Emeric Bruncf20bf12010-10-22 16:06:11 +020062static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
63static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010064
65/* Note: must not be declared <const> as its list will be overwritten */
66static struct protocol proto_tcpv4 = {
67 .name = "tcpv4",
68 .sock_domain = AF_INET,
69 .sock_type = SOCK_STREAM,
70 .sock_prot = IPPROTO_TCP,
71 .sock_family = AF_INET,
72 .sock_addrlen = sizeof(struct sockaddr_in),
73 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020074 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020075 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020076 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010077 .bind_all = tcp_bind_listeners,
78 .unbind_all = unbind_all_listeners,
79 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020080 .get_src = tcp_get_src,
81 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010082 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
83 .nb_listeners = 0,
84};
85
86/* Note: must not be declared <const> as its list will be overwritten */
87static struct protocol proto_tcpv6 = {
88 .name = "tcpv6",
89 .sock_domain = AF_INET6,
90 .sock_type = SOCK_STREAM,
91 .sock_prot = IPPROTO_TCP,
92 .sock_family = AF_INET6,
93 .sock_addrlen = sizeof(struct sockaddr_in6),
94 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020095 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020096 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020097 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010098 .bind_all = tcp_bind_listeners,
99 .unbind_all = unbind_all_listeners,
100 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200101 .get_src = tcp_get_src,
102 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100103 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
104 .nb_listeners = 0,
105};
106
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100107
David du Colombier6f5ccb12011-03-10 22:26:24 +0100108/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100109 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
110 * - 0 : ignore remote address (may even be a NULL pointer)
111 * - 1 : use provided address
112 * - 2 : use provided port
113 * - 3 : use both
114 *
115 * The function supports multiple foreign binding methods :
116 * - linux_tproxy: we directly bind to the foreign address
117 * - cttproxy: we bind to a local address then nat.
118 * The second one can be used as a fallback for the first one.
119 * This function returns 0 when everything's OK, 1 if it could not bind, to the
120 * local address, 2 if it could not bind to the foreign address.
121 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100122int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100123{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100124 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100125 int foreign_ok = 0;
126 int ret;
127
128#ifdef CONFIG_HAP_LINUX_TPROXY
129 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200130 static int ip6_transp_working = 1;
131 switch (local->ss_family) {
132 case AF_INET:
133 if (flags && ip_transp_working) {
134 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
135 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
136 foreign_ok = 1;
137 else
138 ip_transp_working = 0;
139 }
140 break;
141 case AF_INET6:
142 if (flags && ip6_transp_working) {
143 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
144 foreign_ok = 1;
145 else
146 ip6_transp_working = 0;
147 }
148 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100149 }
150#endif
151 if (flags) {
152 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200153 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100154 switch (remote->ss_family) {
155 case AF_INET:
156 if (flags & 1)
157 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
158 if (flags & 2)
159 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
160 break;
161 case AF_INET6:
162 if (flags & 1)
163 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
164 if (flags & 2)
165 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
166 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100167 default:
168 /* we don't want to try to bind to an unknown address family */
169 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100170 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100171 }
172
Simon Hormande072bd2011-06-24 15:11:37 +0900173 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100174 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200175 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100176 if (ret < 0)
177 return 2;
178 }
179 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200180 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100181 if (ret < 0)
182 return 1;
183 }
184
185 if (!flags)
186 return 0;
187
188#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100189 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100190 struct in_tproxy itp1, itp2;
191 memset(&itp1, 0, sizeof(itp1));
192
193 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100194 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
195 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100196
197 /* set connect flag on socket */
198 itp2.op = TPROXY_FLAGS;
199 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
200
201 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
202 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
203 foreign_ok = 1;
204 }
205 }
206#endif
207 if (!foreign_ok)
208 /* we could not bind to a foreign address */
209 return 2;
210
211 return 0;
212}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100213
Willy Tarreau9650f372009-08-16 14:02:45 +0200214
215/*
Willy Tarreauac825402011-03-04 22:04:29 +0100216 * This function initiates a connection to the target assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200217 * (si->{target,addr.to}). A source address may be pointed to by si->addr.from
Willy Tarreauac825402011-03-04 22:04:29 +0100218 * in case of transparent proxying. Normal source bind addresses are still
219 * determined locally (due to the possible need of a source port).
220 * si->target may point either to a valid server or to a backend, depending
221 * on si->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are supported.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200222 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200223 * It can return one of :
224 * - SN_ERR_NONE if everything's OK
225 * - SN_ERR_SRVTO if there are no more servers
226 * - SN_ERR_SRVCL if the connection was refused by the server
227 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
228 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
229 * - SN_ERR_INTERNAL for any other purely internal errors
230 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
231 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100232
David du Colombier6f5ccb12011-03-10 22:26:24 +0100233int tcp_connect_server(struct stream_interface *si)
Willy Tarreau9650f372009-08-16 14:02:45 +0200234{
235 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100236 struct server *srv;
237 struct proxy *be;
238
239 switch (si->target.type) {
240 case TARG_TYPE_PROXY:
241 be = si->target.ptr.p;
242 srv = NULL;
243 break;
244 case TARG_TYPE_SERVER:
245 srv = si->target.ptr.s;
246 be = srv->proxy;
247 break;
248 default:
249 return SN_ERR_INTERNAL;
250 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200251
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200252 if ((fd = si->conn.t.sock.fd = socket(si->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200253 qfprintf(stderr, "Cannot get a server socket.\n");
254
255 if (errno == ENFILE)
256 send_log(be, LOG_EMERG,
257 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
258 be->id, maxfd);
259 else if (errno == EMFILE)
260 send_log(be, LOG_EMERG,
261 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
262 be->id, maxfd);
263 else if (errno == ENOBUFS || errno == ENOMEM)
264 send_log(be, LOG_EMERG,
265 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
266 be->id, maxfd);
267 /* this is a resource error */
268 return SN_ERR_RESOURCE;
269 }
270
271 if (fd >= global.maxsock) {
272 /* do not log anything there, it's a normal condition when this option
273 * is used to serialize connections to a server !
274 */
275 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
276 close(fd);
277 return SN_ERR_PRXCOND; /* it is a configuration limit */
278 }
279
280 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900281 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200282 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
283 close(fd);
284 return SN_ERR_INTERNAL;
285 }
286
287 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900288 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200289
290 if (be->options & PR_O_TCP_NOLING)
Willy Tarreauf6f82252011-11-30 18:02:24 +0100291 si->flags |= SI_FL_NOLINGER;
Willy Tarreau9650f372009-08-16 14:02:45 +0200292
293 /* allow specific binding :
294 * - server-specific at first
295 * - proxy-specific next
296 */
297 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200298 int ret, flags = 0;
299
Willy Tarreau9650f372009-08-16 14:02:45 +0200300 switch (srv->state & SRV_TPROXY_MASK) {
301 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200302 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200303 flags = 3;
304 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200305 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200306 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200307 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200308 break;
309 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200310
Willy Tarreau9650f372009-08-16 14:02:45 +0200311#ifdef SO_BINDTODEVICE
312 /* Note: this might fail if not CAP_NET_RAW */
313 if (srv->iface_name)
314 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
315#endif
316
317 if (srv->sport_range) {
318 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100319 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200320
321 ret = 1;
322 src = srv->source_addr;
323
324 do {
325 /* note: in case of retry, we may have to release a previously
326 * allocated port, hence this loop's construct.
327 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200328 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
329 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200330
331 if (!attempts)
332 break;
333 attempts--;
334
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200335 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
336 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200337 break;
338
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200339 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200340 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200341
Willy Tarreau6471afb2011-09-23 10:54:59 +0200342 ret = tcp_bind_socket(fd, flags, &src, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200343 } while (ret != 0); /* binding NOK */
344 }
345 else {
Willy Tarreau6471afb2011-09-23 10:54:59 +0200346 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200347 }
348
349 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200350 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
351 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 close(fd);
353
354 if (ret == 1) {
355 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
356 be->id, srv->id);
357 send_log(be, LOG_EMERG,
358 "Cannot bind to source address before connect() for server %s/%s.\n",
359 be->id, srv->id);
360 } else {
361 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
362 be->id, srv->id);
363 send_log(be, LOG_EMERG,
364 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
365 be->id, srv->id);
366 }
367 return SN_ERR_RESOURCE;
368 }
369 }
370 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200371 int ret, flags = 0;
372
Willy Tarreau9650f372009-08-16 14:02:45 +0200373 switch (be->options & PR_O_TPXY_MASK) {
374 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200376 flags = 3;
377 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200378 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200379 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200380 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200381 break;
382 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200383
Willy Tarreau9650f372009-08-16 14:02:45 +0200384#ifdef SO_BINDTODEVICE
385 /* Note: this might fail if not CAP_NET_RAW */
386 if (be->iface_name)
387 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
388#endif
Willy Tarreau6471afb2011-09-23 10:54:59 +0200389 ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200390 if (ret) {
391 close(fd);
392 if (ret == 1) {
393 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
394 be->id);
395 send_log(be, LOG_EMERG,
396 "Cannot bind to source address before connect() for proxy %s.\n",
397 be->id);
398 } else {
399 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
400 be->id);
401 send_log(be, LOG_EMERG,
402 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
403 be->id);
404 }
405 return SN_ERR_RESOURCE;
406 }
407 }
408
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400409#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200410 /* disabling tcp quick ack now allows the first request to leave the
411 * machine with the first ACK. We only do this if there are pending
412 * data in the buffer.
413 */
Willy Tarreau2e046c62012-03-01 16:08:30 +0100414 if ((be->options2 & PR_O2_SMARTCON) && si->ob->o)
Simon Hormande072bd2011-06-24 15:11:37 +0900415 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200416#endif
417
Willy Tarreaue803de22010-01-21 17:43:04 +0100418 if (global.tune.server_sndbuf)
419 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
420
421 if (global.tune.server_rcvbuf)
422 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
423
Willy Tarreau9b061e32012-04-07 18:03:52 +0200424 si->flags &= ~SI_FL_FROM_SET;
Willy Tarreau96596ae2012-06-08 22:57:36 +0200425
426 si->conn.peeraddr = (struct sockaddr *)&si->addr.to;
427 si->conn.peerlen = get_addr_len(&si->addr.to);
428
429 if ((connect(fd, si->conn.peeraddr, si->conn.peerlen) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200430 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
431
432 if (errno == EAGAIN || errno == EADDRINUSE) {
433 char *msg;
434 if (errno == EAGAIN) /* no free ports left, try again later */
435 msg = "no free ports";
436 else
437 msg = "local address already in use";
438
439 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200440 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
441 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 close(fd);
443 send_log(be, LOG_EMERG,
444 "Connect() failed for server %s/%s: %s.\n",
445 be->id, srv->id, msg);
446 return SN_ERR_RESOURCE;
447 } else if (errno == ETIMEDOUT) {
448 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200449 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
450 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200451 close(fd);
452 return SN_ERR_SRVTO;
453 } else {
454 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
455 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200456 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
457 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 close(fd);
459 return SN_ERR_SRVCL;
460 }
461 }
462
William Lallemandb7ff6a32012-03-02 14:35:21 +0100463 /* needs src ip/port for logging */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200464 if (si->flags & SI_FL_SRC_ADDR)
Willy Tarreau59b94792012-05-11 16:16:40 +0200465 si_get_from_addr(si);
William Lallemandb7ff6a32012-03-02 14:35:21 +0100466
Willy Tarreau80184712012-07-06 14:54:49 +0200467 fdtab[fd].owner = &si->conn;
Willy Tarreau9650f372009-08-16 14:02:45 +0200468 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau505e34a2012-07-06 10:17:53 +0200469 si->conn.flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9650f372009-08-16 14:02:45 +0200470
Willy Tarreau076be252012-07-06 16:02:29 +0200471 /* Prepare to send a few handshakes related to the on-wire protocol. */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200472 if (si->send_proxy_ofs)
473 si->conn.flags |= CO_FL_SI_SEND_PROXY;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200474
Willy Tarreaud2274c62012-07-06 14:29:45 +0200475 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200476 fd_insert(fd);
477 EV_FD_SET(fd, DIR_WR); /* for connect status */
478
479 si->state = SI_ST_CON;
480 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
481 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
482
483 return SN_ERR_NONE; /* connection is OK */
484}
485
486
Willy Tarreau59b94792012-05-11 16:16:40 +0200487/*
488 * Retrieves the source address for the socket <fd>, with <dir> indicating
489 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
490 * success, -1 in case of error. The socket's source address is stored in
491 * <sa> for <salen> bytes.
492 */
493int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
494{
495 if (dir)
496 return getsockname(fd, sa, &salen);
497 else
498 return getpeername(fd, sa, &salen);
499}
500
501
502/*
503 * Retrieves the original destination address for the socket <fd>, with <dir>
504 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
505 * listener, if the original destination address was translated, the original
506 * address is retrieved. It returns 0 in case of success, -1 in case of error.
507 * The socket's source address is stored in <sa> for <salen> bytes.
508 */
509int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
510{
511 if (dir)
512 return getpeername(fd, sa, &salen);
513#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
514 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
515 return 0;
516#endif
517 else
518 return getsockname(fd, sa, &salen);
519}
520
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200521/* This is the callback which is set when a connection establishment is pending
522 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreaua190d592012-05-20 18:35:19 +0200523 * once the connection is established. It returns zero if it needs some polling
524 * before being called again.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200525 */
Willy Tarreau2da156f2012-07-23 15:07:23 +0200526int tcp_connect_probe(int fd)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200527{
Willy Tarreau80184712012-07-06 14:54:49 +0200528 struct connection *conn = fdtab[fd].owner;
529 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200530 struct buffer *b = si->ob;
Willy Tarreaua190d592012-05-20 18:35:19 +0200531 int retval = 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200532
Willy Tarreau80184712012-07-06 14:54:49 +0200533 if (conn->flags & CO_FL_ERROR)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200534 goto out_error;
535
Willy Tarreau80184712012-07-06 14:54:49 +0200536 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200537 goto out_ignore; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200538
Willy Tarreau2da156f2012-07-23 15:07:23 +0200539 /* stop here if we reached the end of data */
540 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
541 goto out_error;
542
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200543 /* we might have been called just after an asynchronous shutw */
544 if (b->flags & BF_SHUTW)
545 goto out_wakeup;
546
Willy Tarreau2c6be842012-07-06 17:12:34 +0200547 /* We have no data to send to check the connection, and
548 * getsockopt() will not inform us whether the connection
549 * is still pending. So we'll reuse connect() to check the
550 * state of the socket. This has the advantage of giving us
551 * the following info :
552 * - error
553 * - connecting (EALREADY, EINPROGRESS)
554 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200555 */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200556 if ((connect(fd, conn->peeraddr, conn->peerlen) < 0)) {
557 if (errno == EALREADY || errno == EINPROGRESS)
Willy Tarreaua190d592012-05-20 18:35:19 +0200558 goto out_ignore;
559
Willy Tarreau2c6be842012-07-06 17:12:34 +0200560 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200561 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200562
Willy Tarreau2c6be842012-07-06 17:12:34 +0200563 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200564 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200565
566 /* OK we just need to indicate that we got a connection
567 * and that we wrote nothing.
568 */
569 b->flags |= BF_WRITE_NULL;
570
Willy Tarreau076be252012-07-06 16:02:29 +0200571 /* The FD is ready now, we'll mark the connection as complete and
572 * forward the event to the data layer which will update the stream
573 * interface flags.
Willy Tarreaua190d592012-05-20 18:35:19 +0200574 */
Willy Tarreau80184712012-07-06 14:54:49 +0200575 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200576 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200577 return si_data(si)->write(fd);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200578
579 out_wakeup:
580 task_wakeup(si->owner, TASK_WOKEN_IO);
581
582 out_ignore:
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200583 return retval;
584
585 out_error:
586 /* Write error on the file descriptor. We mark the FD as STERROR so
587 * that we don't use it anymore. The error is reported to the stream
588 * interface which will take proper action. We must not perturbate the
589 * buffer because the stream interface wants to ensure transparent
590 * connection retries.
591 */
592
Willy Tarreau80184712012-07-06 14:54:49 +0200593 conn->flags |= CO_FL_ERROR;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200594 fdtab[fd].ev &= ~FD_POLL_STICKY;
595 EV_FD_REM(fd);
596 si->flags |= SI_FL_ERR;
Willy Tarreaua190d592012-05-20 18:35:19 +0200597 retval = 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200598 goto out_wakeup;
599}
600
Willy Tarreau59b94792012-05-11 16:16:40 +0200601
Willy Tarreaue6b98942007-10-29 01:09:36 +0100602/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
603 * an error message in <err> if the message is at most <errlen> bytes long
604 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
605 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
606 * was alright and that no message was returned. ERR_RETRYABLE means that an
607 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700608 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100609 * the meaning of the error, but just indicate that a message is present which
610 * should be displayed with the respective level. Last, ERR_ABORT indicates
611 * that it's pointless to try to start other listeners. No error message is
612 * returned if errlen is NULL.
613 */
614int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
615{
616 __label__ tcp_return, tcp_close_return;
617 int fd, err;
618 const char *msg = NULL;
619
620 /* ensure we never return garbage */
621 if (errmsg && errlen)
622 *errmsg = 0;
623
624 if (listener->state != LI_ASSIGNED)
625 return ERR_NONE; /* already bound */
626
627 err = ERR_NONE;
628
629 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
630 err |= ERR_RETRYABLE | ERR_ALERT;
631 msg = "cannot create listening socket";
632 goto tcp_return;
633 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100634
Willy Tarreaue6b98942007-10-29 01:09:36 +0100635 if (fd >= global.maxsock) {
636 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
637 msg = "not enough free sockets (raise '-n' parameter)";
638 goto tcp_close_return;
639 }
640
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200641 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100642 err |= ERR_FATAL | ERR_ALERT;
643 msg = "cannot make socket non-blocking";
644 goto tcp_close_return;
645 }
646
Simon Hormande072bd2011-06-24 15:11:37 +0900647 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100648 /* not fatal but should be reported */
649 msg = "cannot do so_reuseaddr";
650 err |= ERR_ALERT;
651 }
652
653 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900654 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100655
Willy Tarreaue6b98942007-10-29 01:09:36 +0100656#ifdef SO_REUSEPORT
657 /* OpenBSD supports this. As it's present in old libc versions of Linux,
658 * it might return an error that we will silently ignore.
659 */
Simon Hormande072bd2011-06-24 15:11:37 +0900660 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100661#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100662#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200663 if (listener->options & LI_O_FOREIGN) {
664 switch (listener->addr.ss_family) {
665 case AF_INET:
666 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
667 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
668 msg = "cannot make listening socket transparent";
669 err |= ERR_ALERT;
670 }
671 break;
672 case AF_INET6:
673 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
674 msg = "cannot make listening socket transparent";
675 err |= ERR_ALERT;
676 }
677 break;
678 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100679 }
680#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100681#ifdef SO_BINDTODEVICE
682 /* Note: this might fail if not CAP_NET_RAW */
683 if (listener->interface) {
684 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100685 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100686 msg = "cannot bind listener to device";
687 err |= ERR_WARN;
688 }
689 }
690#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400691#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100692 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400693 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200694 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
695 msg = "cannot set MSS";
696 err |= ERR_WARN;
697 }
698 }
699#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200700#if defined(TCP_DEFER_ACCEPT)
701 if (listener->options & LI_O_DEF_ACCEPT) {
702 /* defer accept by up to one second */
703 int accept_delay = 1;
704 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
705 msg = "cannot enable DEFER_ACCEPT";
706 err |= ERR_WARN;
707 }
708 }
709#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100710 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
711 err |= ERR_RETRYABLE | ERR_ALERT;
712 msg = "cannot bind socket";
713 goto tcp_close_return;
714 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100715
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100716 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100717 err |= ERR_RETRYABLE | ERR_ALERT;
718 msg = "cannot listen to socket";
719 goto tcp_close_return;
720 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100721
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400722#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200723 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900724 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200725#endif
726
Willy Tarreaue6b98942007-10-29 01:09:36 +0100727 /* the socket is ready */
728 listener->fd = fd;
729 listener->state = LI_LISTEN;
730
Willy Tarreaueabf3132008-08-29 23:36:51 +0200731 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaueb472682010-05-28 18:46:57 +0200732 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200733 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200734 fd_insert(fd);
735
Willy Tarreaue6b98942007-10-29 01:09:36 +0100736 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100737 if (msg && errlen) {
738 char pn[INET6_ADDRSTRLEN];
739
Willy Tarreau631f01c2011-09-05 00:36:48 +0200740 addr_to_str(&listener->addr, pn, sizeof(pn));
741 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100742 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100743 return err;
744
745 tcp_close_return:
746 close(fd);
747 goto tcp_return;
748}
749
750/* This function creates all TCP sockets bound to the protocol entry <proto>.
751 * It is intended to be used as the protocol's bind_all() function.
752 * The sockets will be registered but not added to any fd_set, in order not to
753 * loose them across the fork(). A call to enable_all_listeners() is needed
754 * to complete initialization. The return value is composed from ERR_*.
755 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200756static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100757{
758 struct listener *listener;
759 int err = ERR_NONE;
760
761 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200762 err |= tcp_bind_listener(listener, errmsg, errlen);
763 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100764 break;
765 }
766
767 return err;
768}
769
770/* Add listener to the list of tcpv4 listeners. The listener's state
771 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
772 * listeners is updated. This is the function to use to add a new listener.
773 */
774void tcpv4_add_listener(struct listener *listener)
775{
776 if (listener->state != LI_INIT)
777 return;
778 listener->state = LI_ASSIGNED;
779 listener->proto = &proto_tcpv4;
780 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
781 proto_tcpv4.nb_listeners++;
782}
783
784/* Add listener to the list of tcpv4 listeners. The listener's state
785 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
786 * listeners is updated. This is the function to use to add a new listener.
787 */
788void tcpv6_add_listener(struct listener *listener)
789{
790 if (listener->state != LI_INIT)
791 return;
792 listener->state = LI_ASSIGNED;
793 listener->proto = &proto_tcpv6;
794 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
795 proto_tcpv6.nb_listeners++;
796}
797
Willy Tarreauedcf6682008-11-30 23:15:34 +0100798/* This function performs the TCP request analysis on the current request. It
799 * returns 1 if the processing can continue on next analysers, or zero if it
800 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200801 * request. It relies on buffers flags, and updates s->req->analysers. The
802 * function may be called for frontend rules and backend rules. It only relies
803 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100804 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200805int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100806{
807 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200808 struct stksess *ts;
809 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100810 int partial;
811
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100812 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 +0100813 now_ms, __FUNCTION__,
814 s,
815 req,
816 req->rex, req->wex,
817 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100818 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100819 req->analysers);
820
Willy Tarreauedcf6682008-11-30 23:15:34 +0100821 /* We don't know whether we have enough data, so must proceed
822 * this way :
823 * - iterate through all rules in their declaration order
824 * - if one rule returns MISS, it means the inspect delay is
825 * not over yet, then return immediately, otherwise consider
826 * it as a non-match.
827 * - if one rule returns OK, then return OK
828 * - if one rule returns KO, then return KO
829 */
830
Willy Tarreaub824b002010-09-29 16:36:16 +0200831 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 +0200832 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100833 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200834 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100835
Willy Tarreaufb356202010-08-03 14:02:05 +0200836 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100837 int ret = ACL_PAT_PASS;
838
839 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200840 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100841 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200842 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100843 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200844 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
845 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100846 return 0;
847 }
848
849 ret = acl_pass(ret);
850 if (rule->cond->pol == ACL_COND_UNLESS)
851 ret = !ret;
852 }
853
854 if (ret) {
855 /* we have a matching rule. */
856 if (rule->action == TCP_ACT_REJECT) {
857 buffer_abort(req);
858 buffer_abort(s->rep);
859 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200860
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100861 s->be->be_counters.denied_req++;
862 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200863 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200864 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200865
Willy Tarreauedcf6682008-11-30 23:15:34 +0100866 if (!(s->flags & SN_ERR_MASK))
867 s->flags |= SN_ERR_PRXCOND;
868 if (!(s->flags & SN_FINST_MASK))
869 s->flags |= SN_FINST_R;
870 return 0;
871 }
Willy Tarreau56123282010-08-06 19:06:56 +0200872 else if (rule->action == TCP_ACT_TRK_SC1) {
873 if (!s->stkctr1_entry) {
874 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200875 * Also, note that right now we can only track SRC so we
876 * don't check how to get the key, but later we may need
877 * to consider rule->act_prm->trk_ctr.type.
878 */
879 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100880 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200881 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200882 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200883 if (s->fe != s->be)
884 s->flags |= SN_BE_TRACK_SC1;
885 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200886 }
887 }
Willy Tarreau56123282010-08-06 19:06:56 +0200888 else if (rule->action == TCP_ACT_TRK_SC2) {
889 if (!s->stkctr2_entry) {
890 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200891 * Also, note that right now we can only track SRC so we
892 * don't check how to get the key, but later we may need
893 * to consider rule->act_prm->trk_ctr.type.
894 */
895 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100896 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200897 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200898 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200899 if (s->fe != s->be)
900 s->flags |= SN_BE_TRACK_SC2;
901 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200902 }
903 }
904 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100905 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200906 break;
907 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100908 }
909 }
910
911 /* if we get there, it means we have no rule which matches, or
912 * we have an explicit accept, so we apply the default accept.
913 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200914 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100915 req->analyse_exp = TICK_ETERNITY;
916 return 1;
917}
918
Emeric Brun97679e72010-09-23 17:56:44 +0200919/* This function performs the TCP response analysis on the current response. It
920 * returns 1 if the processing can continue on next analysers, or zero if it
921 * needs more data, encounters an error, or wants to immediately abort the
922 * response. It relies on buffers flags, and updates s->rep->analysers. The
923 * function may be called for backend rules.
924 */
925int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit)
926{
927 struct tcp_rule *rule;
928 int partial;
929
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100930 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 +0200931 now_ms, __FUNCTION__,
932 s,
933 rep,
934 rep->rex, rep->wex,
935 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100936 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200937 rep->analysers);
938
939 /* We don't know whether we have enough data, so must proceed
940 * this way :
941 * - iterate through all rules in their declaration order
942 * - if one rule returns MISS, it means the inspect delay is
943 * not over yet, then return immediately, otherwise consider
944 * it as a non-match.
945 * - if one rule returns OK, then return OK
946 * - if one rule returns KO, then return KO
947 */
948
949 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200950 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200951 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200952 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200953
954 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
955 int ret = ACL_PAT_PASS;
956
957 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200958 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200959 if (ret == ACL_PAT_MISS) {
960 /* just set the analyser timeout once at the beginning of the response */
961 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
962 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
963 return 0;
964 }
965
966 ret = acl_pass(ret);
967 if (rule->cond->pol == ACL_COND_UNLESS)
968 ret = !ret;
969 }
970
971 if (ret) {
972 /* we have a matching rule. */
973 if (rule->action == TCP_ACT_REJECT) {
974 buffer_abort(rep);
975 buffer_abort(s->req);
976 rep->analysers = 0;
977
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100978 s->be->be_counters.denied_resp++;
979 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200980 if (s->listener->counters)
981 s->listener->counters->denied_resp++;
982
983 if (!(s->flags & SN_ERR_MASK))
984 s->flags |= SN_ERR_PRXCOND;
985 if (!(s->flags & SN_FINST_MASK))
986 s->flags |= SN_FINST_D;
987 return 0;
988 }
989 else {
990 /* otherwise accept */
991 break;
992 }
993 }
994 }
995
996 /* if we get there, it means we have no rule which matches, or
997 * we have an explicit accept, so we apply the default accept.
998 */
999 rep->analysers &= ~an_bit;
1000 rep->analyse_exp = TICK_ETERNITY;
1001 return 1;
1002}
1003
1004
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001005/* This function performs the TCP layer4 analysis on the current request. It
1006 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1007 * matches or if no more rule matches. It can only use rules which don't need
1008 * any data.
1009 */
1010int tcp_exec_req_rules(struct session *s)
1011{
1012 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001013 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001014 struct stktable *t = NULL;
1015 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001016 int ret;
1017
1018 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1019 ret = ACL_PAT_PASS;
1020
1021 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001022 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001023 ret = acl_pass(ret);
1024 if (rule->cond->pol == ACL_COND_UNLESS)
1025 ret = !ret;
1026 }
1027
1028 if (ret) {
1029 /* we have a matching rule. */
1030 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001031 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001032 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001033 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001034
1035 if (!(s->flags & SN_ERR_MASK))
1036 s->flags |= SN_ERR_PRXCOND;
1037 if (!(s->flags & SN_FINST_MASK))
1038 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001039 result = 0;
1040 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001041 }
Willy Tarreau56123282010-08-06 19:06:56 +02001042 else if (rule->action == TCP_ACT_TRK_SC1) {
1043 if (!s->stkctr1_entry) {
1044 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001045 * Also, note that right now we can only track SRC so we
1046 * don't check how to get the key, but later we may need
1047 * to consider rule->act_prm->trk_ctr.type.
1048 */
1049 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001050 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001051 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001052 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001053 }
1054 }
Willy Tarreau56123282010-08-06 19:06:56 +02001055 else if (rule->action == TCP_ACT_TRK_SC2) {
1056 if (!s->stkctr2_entry) {
1057 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001058 * Also, note that right now we can only track SRC so we
1059 * don't check how to get the key, but later we may need
1060 * to consider rule->act_prm->trk_ctr.type.
1061 */
1062 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001063 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001064 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001065 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001066 }
1067 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001068 else {
1069 /* otherwise it's an accept */
1070 break;
1071 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001072 }
1073 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001074 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001075}
1076
Emeric Brun97679e72010-09-23 17:56:44 +02001077/* Parse a tcp-response rule. Return a negative value in case of failure */
1078static int tcp_parse_response_rule(char **args, int arg, int section_type,
1079 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001080 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001081{
1082 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001083 memprintf(err, "%s %s is only allowed in 'backend' sections",
1084 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001085 return -1;
1086 }
1087
1088 if (strcmp(args[arg], "accept") == 0) {
1089 arg++;
1090 rule->action = TCP_ACT_ACCEPT;
1091 }
1092 else if (strcmp(args[arg], "reject") == 0) {
1093 arg++;
1094 rule->action = TCP_ACT_REJECT;
1095 }
1096 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001097 memprintf(err,
1098 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1099 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001100 return -1;
1101 }
1102
1103 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001104 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1105 memprintf(err,
1106 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1107 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001108 return -1;
1109 }
1110 }
1111 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001112 memprintf(err,
1113 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001114 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1115 return -1;
1116 }
1117 return 0;
1118}
1119
1120
1121
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001122/* Parse a tcp-request rule. Return a negative value in case of failure */
1123static int tcp_parse_request_rule(char **args, int arg, int section_type,
1124 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001125 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001126{
1127 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001128 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1129 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001130 return -1;
1131 }
1132
1133 if (!strcmp(args[arg], "accept")) {
1134 arg++;
1135 rule->action = TCP_ACT_ACCEPT;
1136 }
1137 else if (!strcmp(args[arg], "reject")) {
1138 arg++;
1139 rule->action = TCP_ACT_REJECT;
1140 }
Willy Tarreau56123282010-08-06 19:06:56 +02001141 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001142 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001143 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001144
1145 arg++;
1146 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001147 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001148
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001149 if (ret < 0) { /* nb: warnings are not handled yet */
1150 memprintf(err,
1151 "'%s %s %s' : %s in %s '%s'",
1152 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1153 return ret;
1154 }
Willy Tarreau56123282010-08-06 19:06:56 +02001155 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001156 }
Willy Tarreau56123282010-08-06 19:06:56 +02001157 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001158 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001159 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001160
1161 arg++;
1162 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001163 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001164
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001165 if (ret < 0) { /* nb: warnings are not handled yet */
1166 memprintf(err,
1167 "'%s %s %s' : %s in %s '%s'",
1168 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1169 return ret;
1170 }
Willy Tarreau56123282010-08-06 19:06:56 +02001171 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001172 }
1173 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001174 memprintf(err,
1175 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1176 "or 'track-sc2' in %s '%s' (got '%s')",
1177 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001178 return -1;
1179 }
1180
1181 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001182 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1183 memprintf(err,
1184 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1185 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001186 return -1;
1187 }
1188 }
1189 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001190 memprintf(err,
1191 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001192 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1193 return -1;
1194 }
1195 return 0;
1196}
1197
Emeric Brun97679e72010-09-23 17:56:44 +02001198/* This function should be called to parse a line starting with the "tcp-response"
1199 * keyword.
1200 */
1201static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001202 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001203{
1204 const char *ptr = NULL;
1205 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001206 int warn = 0;
1207 int arg;
1208 struct tcp_rule *rule;
1209
1210 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001211 memprintf(err, "missing argument for '%s' in %s '%s'",
1212 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001213 return -1;
1214 }
1215
1216 if (strcmp(args[1], "inspect-delay") == 0) {
1217 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001218 memprintf(err, "%s %s is only allowed in 'backend' sections",
1219 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001220 return -1;
1221 }
1222
1223 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001224 memprintf(err,
1225 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1226 args[0], args[1], proxy_type_str(curpx), curpx->id);
1227 if (ptr)
1228 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001229 return -1;
1230 }
1231
1232 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001233 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1234 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001235 return 1;
1236 }
1237 curpx->tcp_rep.inspect_delay = val;
1238 return 0;
1239 }
1240
Simon Hormande072bd2011-06-24 15:11:37 +09001241 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001242 LIST_INIT(&rule->list);
1243 arg = 1;
1244
1245 if (strcmp(args[1], "content") == 0) {
1246 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001247 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001248 goto error;
1249
1250 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1251 struct acl *acl;
1252 const char *name;
1253
1254 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1255 name = acl ? acl->name : "(unknown)";
1256
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001257 memprintf(err,
1258 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1259 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001260 warn++;
1261 }
1262
1263 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1264 }
1265 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001266 memprintf(err,
1267 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1268 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001269 goto error;
1270 }
1271
1272 return warn;
1273 error:
1274 free(rule);
1275 return -1;
1276}
1277
1278
Willy Tarreaub6866442008-07-14 23:54:42 +02001279/* This function should be called to parse a line starting with the "tcp-request"
1280 * keyword.
1281 */
1282static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001283 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001284{
1285 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001286 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001287 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001288 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001289 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001290
1291 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001292 if (curpx == defpx)
1293 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1294 else
1295 memprintf(err, "missing argument for '%s' in %s '%s'",
1296 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001297 return -1;
1298 }
1299
1300 if (!strcmp(args[1], "inspect-delay")) {
1301 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001302 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1303 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001304 return -1;
1305 }
1306
Willy Tarreaub6866442008-07-14 23:54:42 +02001307 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001308 memprintf(err,
1309 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1310 args[0], args[1], proxy_type_str(curpx), curpx->id);
1311 if (ptr)
1312 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001313 return -1;
1314 }
1315
1316 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001317 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1318 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001319 return 1;
1320 }
1321 curpx->tcp_req.inspect_delay = val;
1322 return 0;
1323 }
1324
Simon Hormande072bd2011-06-24 15:11:37 +09001325 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001326 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001327 arg = 1;
1328
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001329 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001330 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001331 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001332 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001333
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001334 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001335 struct acl *acl;
1336 const char *name;
1337
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001338 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001339 name = acl ? acl->name : "(unknown)";
1340
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001341 memprintf(err,
1342 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1343 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001344 warn++;
1345 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001346 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001347 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001348 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001349 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001350
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001351 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001352 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1353 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001354 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001355 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001356
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001357 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001358 goto error;
1359
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001360 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1361 struct acl *acl;
1362 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001363
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001364 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1365 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001366
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001367 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001368 memprintf(err,
1369 "'%s %s' may not reference acl '%s' which makes use of "
1370 "payload in %s '%s'. Please use '%s content' for this.",
1371 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001372 goto error;
1373 }
1374 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001375 memprintf(err,
1376 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1377 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001378 warn++;
1379 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001380 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001381 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001382 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001383 if (curpx == defpx)
1384 memprintf(err,
1385 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1386 args[0], args[1]);
1387 else
1388 memprintf(err,
1389 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1390 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001391 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001392 }
1393
Willy Tarreau1a687942010-05-23 22:40:30 +02001394 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001395 error:
1396 free(rule);
1397 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001398}
1399
Willy Tarreau645513a2010-05-24 20:55:15 +02001400
1401/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001402/* All supported sample fetch functios must be declared here */
1403/************************************************************************/
1404
1405/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001406 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001407 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1408 * is a string (cookie name), other types will lead to undefined behaviour.
1409 */
1410int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001411smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001412 const struct arg *args, struct sample *smp)
1413{
1414 int bleft;
1415 const unsigned char *data;
1416
1417 if (!l4 || !l4->req)
1418 return 0;
1419
1420 smp->flags = 0;
1421 smp->type = SMP_T_CSTR;
1422
1423 bleft = l4->req->i;
1424 if (bleft <= 11)
1425 goto too_short;
1426
1427 data = (const unsigned char *)l4->req->p + 11;
1428 bleft -= 11;
1429
1430 if (bleft <= 7)
1431 goto too_short;
1432
1433 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1434 goto not_cookie;
1435
1436 data += 7;
1437 bleft -= 7;
1438
1439 while (bleft > 0 && *data == ' ') {
1440 data++;
1441 bleft--;
1442 }
1443
1444 if (args) {
1445
1446 if (bleft <= args->data.str.len)
1447 goto too_short;
1448
1449 if ((data[args->data.str.len] != '=') ||
1450 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1451 goto not_cookie;
1452
1453 data += args->data.str.len + 1;
1454 bleft -= args->data.str.len + 1;
1455 } else {
1456 while (bleft > 0 && *data != '=') {
1457 if (*data == '\r' || *data == '\n')
1458 goto not_cookie;
1459 data++;
1460 bleft--;
1461 }
1462
1463 if (bleft < 1)
1464 goto too_short;
1465
1466 if (*data != '=')
1467 goto not_cookie;
1468
1469 data++;
1470 bleft--;
1471 }
1472
1473 /* data points to cookie value */
1474 smp->data.str.str = (char *)data;
1475 smp->data.str.len = 0;
1476
1477 while (bleft > 0 && *data != '\r') {
1478 data++;
1479 bleft--;
1480 }
1481
1482 if (bleft < 2)
1483 goto too_short;
1484
1485 if (data[0] != '\r' || data[1] != '\n')
1486 goto not_cookie;
1487
1488 smp->data.str.len = (char *)data - smp->data.str.str;
1489 smp->flags = SMP_F_VOLATILE;
1490 return 1;
1491
1492 too_short:
1493 smp->flags = SMP_F_MAY_CHANGE;
1494 not_cookie:
1495 return 0;
1496}
1497
Willy Tarreau32389b72012-04-23 23:13:20 +02001498/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001499/* All supported ACL keywords must be declared here. */
1500/************************************************************************/
1501
Willy Tarreau32389b72012-04-23 23:13:20 +02001502/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1503static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001504acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001505 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001506{
1507 int ret;
1508
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001509 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001510
1511 if (smp->flags & SMP_F_MAY_CHANGE)
1512 return 0;
1513
1514 smp->flags = SMP_F_VOLATILE;
1515 smp->type = SMP_T_UINT;
1516 smp->data.uint = ret;
1517 return 1;
1518}
1519
1520
Willy Tarreau4a129812012-04-25 17:31:42 +02001521/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001522static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001523smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001524 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001525{
Willy Tarreauf4362b32011-12-16 17:49:52 +01001526 switch (l4->si[0].addr.from.ss_family) {
1527 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001528 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.from)->sin_addr;
1529 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001530 break;
1531 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001532 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.from))->sin6_addr;
1533 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001534 break;
1535 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001536 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001537 }
Emeric Brunf769f512010-10-22 17:14:01 +02001538
Willy Tarreau37406352012-04-23 16:16:37 +02001539 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001540 return 1;
1541}
1542
Willy Tarreaua5e37562011-12-16 17:06:15 +01001543/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001544static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001545smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001546 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001547{
Willy Tarreauf853c462012-04-23 18:53:56 +02001548 smp->type = SMP_T_UINT;
1549 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001550 return 0;
1551
Willy Tarreau37406352012-04-23 16:16:37 +02001552 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001553 return 1;
1554}
1555
Willy Tarreau4a129812012-04-25 17:31:42 +02001556/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001557static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001558smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001559 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001560{
Willy Tarreau59b94792012-05-11 16:16:40 +02001561 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001562
Willy Tarreauf4362b32011-12-16 17:49:52 +01001563 switch (l4->si[0].addr.to.ss_family) {
1564 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001565 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.to)->sin_addr;
1566 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001567 break;
1568 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001569 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.to))->sin6_addr;
1570 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001571 break;
1572 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001573 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001574 }
Emeric Brunf769f512010-10-22 17:14:01 +02001575
Willy Tarreau37406352012-04-23 16:16:37 +02001576 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001577 return 1;
1578}
1579
Willy Tarreaua5e37562011-12-16 17:06:15 +01001580/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001581static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001582smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001583 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001584{
Willy Tarreau59b94792012-05-11 16:16:40 +02001585 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001586
Willy Tarreauf853c462012-04-23 18:53:56 +02001587 smp->type = SMP_T_UINT;
1588 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001589 return 0;
1590
Willy Tarreau37406352012-04-23 16:16:37 +02001591 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001592 return 1;
1593}
1594
1595static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001596smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1597 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001598{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001599 unsigned int len_offset = arg_p[0].data.uint;
1600 unsigned int len_size = arg_p[1].data.uint;
1601 unsigned int buf_offset;
1602 unsigned int buf_size = 0;
Emericf2d7cae2010-11-05 18:13:50 +01001603 struct buffer *b;
1604 int i;
1605
1606 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1607 /* by default buf offset == len offset + len size */
1608 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1609
1610 if (!l4)
1611 return 0;
1612
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001613 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001614
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001615 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001616 return 0;
1617
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001618 if (len_offset + len_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001619 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001620
1621 for (i = 0; i < len_size; i++) {
Willy Tarreau89fa7062012-03-02 16:13:16 +01001622 buf_size = (buf_size << 8) + ((unsigned char *)b->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001623 }
1624
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001625 /* buf offset may be implicit, absolute or relative */
1626 buf_offset = len_offset + len_size;
1627 if (arg_p[2].type == ARGT_UINT)
1628 buf_offset = arg_p[2].data.uint;
1629 else if (arg_p[2].type == ARGT_SINT)
1630 buf_offset += arg_p[2].data.sint;
1631
Willy Tarreau82ea8002012-04-25 19:19:43 +02001632 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1633 /* will never match */
1634 smp->flags = 0;
1635 return 0;
1636 }
1637
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001638 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001639 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001640
1641 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001642 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001643 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001644 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001645 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001646
1647 too_short:
1648 smp->flags = SMP_F_MAY_CHANGE;
1649 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001650}
1651
1652static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001653smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1654 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001655{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001656 unsigned int buf_offset = arg_p[0].data.uint;
1657 unsigned int buf_size = arg_p[1].data.uint;
Emericf2d7cae2010-11-05 18:13:50 +01001658 struct buffer *b;
1659
1660 if (!l4)
1661 return 0;
1662
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001663 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001664
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001665 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001666 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001667
1668 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1669 /* will never match */
1670 smp->flags = 0;
1671 return 0;
1672 }
Emericf2d7cae2010-11-05 18:13:50 +01001673
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001674 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001675 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001676
1677 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001678 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001679 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001680 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001681 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001682
1683 too_short:
1684 smp->flags = SMP_F_MAY_CHANGE;
1685 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001686}
1687
Willy Tarreau21d68a62012-04-20 15:52:36 +02001688/* This function is used to validate the arguments passed to a "payload" fetch
1689 * keyword. This keyword expects two positive integers, with the second one
1690 * being strictly positive. It is assumed that the types are already the correct
1691 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1692 * filled with a pointer to an error message in case of error, that the caller
1693 * is responsible for freeing. The initial location must either be freeable or
1694 * NULL.
1695 */
1696static int val_payload(struct arg *arg, char **err_msg)
1697{
1698 if (!arg[1].data.uint) {
1699 if (err_msg)
1700 memprintf(err_msg, "payload length must be > 0");
1701 return 0;
1702 }
1703 return 1;
1704}
1705
1706/* This function is used to validate the arguments passed to a "payload_lv" fetch
1707 * keyword. This keyword allows two positive integers and an optional signed one,
1708 * with the second one being strictly positive and the third one being greater than
1709 * the opposite of the two others if negative. It is assumed that the types are
1710 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1711 * not NULL, it will be filled with a pointer to an error message in case of
1712 * error, that the caller is responsible for freeing. The initial location must
1713 * either be freeable or NULL.
1714 */
1715static int val_payload_lv(struct arg *arg, char **err_msg)
1716{
1717 if (!arg[1].data.uint) {
1718 if (err_msg)
1719 memprintf(err_msg, "payload length must be > 0");
1720 return 0;
1721 }
1722
1723 if (arg[2].type == ARGT_SINT &&
1724 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1725 if (err_msg)
1726 memprintf(err_msg, "payload offset too negative");
1727 return 0;
1728 }
1729 return 1;
1730}
1731
Willy Tarreaub6866442008-07-14 23:54:42 +02001732static struct cfg_kw_list cfg_kws = {{ },{
1733 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001734 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001735 { 0, NULL, NULL },
1736}};
1737
Willy Tarreau61612d42012-04-19 18:42:05 +02001738/* Note: must not be declared <const> as its list will be overwritten.
1739 * Please take care of keeping this list alphabetically sorted.
1740 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001741static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001742 { "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 +02001743 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001744 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1745 { "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 +02001746 { "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 +02001747 { "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 +02001748 { "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 +02001749 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001750 { NULL, NULL, NULL, NULL },
1751}};
1752
Willy Tarreau4a129812012-04-25 17:31:42 +02001753/* Note: must not be declared <const> as its list will be overwritten.
1754 * Note: fetches that may return multiple types must be declared as the lowest
1755 * common denominator, the type that can be casted into all other ones. For
1756 * instance v4/v6 must be declared v4.
1757 */
Willy Tarreau12785782012-04-27 21:37:17 +02001758static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001759 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001760 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001761 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001762 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1763 { "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 +02001764 { "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 +02001765 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001766 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001767}};
1768
Willy Tarreaue6b98942007-10-29 01:09:36 +01001769__attribute__((constructor))
1770static void __tcp_protocol_init(void)
1771{
1772 protocol_register(&proto_tcpv4);
1773 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001774 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001775 cfg_register_keywords(&cfg_kws);
1776 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001777}
1778
1779
1780/*
1781 * Local variables:
1782 * c-indent-level: 8
1783 * c-basic-offset: 8
1784 * End:
1785 */