blob: b43731cb91369fe2b2bdd902a4a459f6edcb0639 [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 Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020044#include <proto/fd.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 Tarreaua975b8f2010-06-05 19:13:27 +020052#include <proto/stick_table.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010054
Willy Tarreaue8c66af2008-01-13 18:40:14 +010055#ifdef CONFIG_HAP_CTTPROXY
56#include <import/ip_tproxy.h>
57#endif
58
Emeric Bruncf20bf12010-10-22 16:06:11 +020059static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
60static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010061
62/* Note: must not be declared <const> as its list will be overwritten */
63static struct protocol proto_tcpv4 = {
64 .name = "tcpv4",
65 .sock_domain = AF_INET,
66 .sock_type = SOCK_STREAM,
67 .sock_prot = IPPROTO_TCP,
68 .sock_family = AF_INET,
69 .sock_addrlen = sizeof(struct sockaddr_in),
70 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020071 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020072 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020073 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010074 .bind_all = tcp_bind_listeners,
75 .unbind_all = unbind_all_listeners,
76 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020077 .get_src = tcp_get_src,
78 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010079 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
80 .nb_listeners = 0,
81};
82
83/* Note: must not be declared <const> as its list will be overwritten */
84static struct protocol proto_tcpv6 = {
85 .name = "tcpv6",
86 .sock_domain = AF_INET6,
87 .sock_type = SOCK_STREAM,
88 .sock_prot = IPPROTO_TCP,
89 .sock_family = AF_INET6,
90 .sock_addrlen = sizeof(struct sockaddr_in6),
91 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020092 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020093 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020094 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010095 .bind_all = tcp_bind_listeners,
96 .unbind_all = unbind_all_listeners,
97 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020098 .get_src = tcp_get_src,
99 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100100 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
101 .nb_listeners = 0,
102};
103
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100104
David du Colombier6f5ccb12011-03-10 22:26:24 +0100105/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100106 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
107 * - 0 : ignore remote address (may even be a NULL pointer)
108 * - 1 : use provided address
109 * - 2 : use provided port
110 * - 3 : use both
111 *
112 * The function supports multiple foreign binding methods :
113 * - linux_tproxy: we directly bind to the foreign address
114 * - cttproxy: we bind to a local address then nat.
115 * The second one can be used as a fallback for the first one.
116 * This function returns 0 when everything's OK, 1 if it could not bind, to the
117 * local address, 2 if it could not bind to the foreign address.
118 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100119int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100120{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100121 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100122 int foreign_ok = 0;
123 int ret;
124
125#ifdef CONFIG_HAP_LINUX_TPROXY
126 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200127 static int ip6_transp_working = 1;
128 switch (local->ss_family) {
129 case AF_INET:
130 if (flags && ip_transp_working) {
131 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
132 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
133 foreign_ok = 1;
134 else
135 ip_transp_working = 0;
136 }
137 break;
138 case AF_INET6:
139 if (flags && ip6_transp_working) {
140 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
141 foreign_ok = 1;
142 else
143 ip6_transp_working = 0;
144 }
145 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100146 }
147#endif
148 if (flags) {
149 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200150 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100151 switch (remote->ss_family) {
152 case AF_INET:
153 if (flags & 1)
154 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
155 if (flags & 2)
156 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
157 break;
158 case AF_INET6:
159 if (flags & 1)
160 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
161 if (flags & 2)
162 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
163 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100164 default:
165 /* we don't want to try to bind to an unknown address family */
166 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100167 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100168 }
169
Simon Hormande072bd2011-06-24 15:11:37 +0900170 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100171 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200172 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100173 if (ret < 0)
174 return 2;
175 }
176 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200177 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100178 if (ret < 0)
179 return 1;
180 }
181
182 if (!flags)
183 return 0;
184
185#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100186 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100187 struct in_tproxy itp1, itp2;
188 memset(&itp1, 0, sizeof(itp1));
189
190 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100191 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
192 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100193
194 /* set connect flag on socket */
195 itp2.op = TPROXY_FLAGS;
196 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
197
198 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
199 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
200 foreign_ok = 1;
201 }
202 }
203#endif
204 if (!foreign_ok)
205 /* we could not bind to a foreign address */
206 return 2;
207
208 return 0;
209}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100210
Willy Tarreau9650f372009-08-16 14:02:45 +0200211
212/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200213 * This function initiates a TCP connection establishment to the target assigned
214 * to connection <conn> using (si->{target,addr.to}). A source address may be
215 * pointed to by conn->addr.from in case of transparent proxying. Normal source
216 * bind addresses are still determined locally (due to the possible need of a
217 * source port). conn->target may point either to a valid server or to a backend,
218 * depending on conn->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are
219 * supported. The <data> parameter is a boolean indicating whether there are data
220 * waiting for being sent or not, in order to adjust data write polling and on
221 * some platforms, the ability to avoid an empty initial ACK.
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
Willy Tarreau14f8e862012-08-30 22:23:13 +0200233int tcp_connect_server(struct connection *conn, int data)
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
Willy Tarreau986a9d22012-08-30 21:11:38 +0200239 switch (conn->target.type) {
Willy Tarreauac825402011-03-04 22:04:29 +0100240 case TARG_TYPE_PROXY:
Willy Tarreau986a9d22012-08-30 21:11:38 +0200241 be = conn->target.ptr.p;
Willy Tarreauac825402011-03-04 22:04:29 +0100242 srv = NULL;
243 break;
244 case TARG_TYPE_SERVER:
Willy Tarreau986a9d22012-08-30 21:11:38 +0200245 srv = conn->target.ptr.s;
Willy Tarreauac825402011-03-04 22:04:29 +0100246 be = srv->proxy;
247 break;
248 default:
249 return SN_ERR_INTERNAL;
250 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200251
Willy Tarreau986a9d22012-08-30 21:11:38 +0200252 if ((fd = conn->t.sock.fd = socket(conn->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
Willy Tarreau9650f372009-08-16 14:02:45 +0200290 /* allow specific binding :
291 * - server-specific at first
292 * - proxy-specific next
293 */
294 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200295 int ret, flags = 0;
296
Willy Tarreau9650f372009-08-16 14:02:45 +0200297 switch (srv->state & SRV_TPROXY_MASK) {
298 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200299 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200300 flags = 3;
301 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200302 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200303 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200304 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200305 break;
306 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200307
Willy Tarreau9650f372009-08-16 14:02:45 +0200308#ifdef SO_BINDTODEVICE
309 /* Note: this might fail if not CAP_NET_RAW */
310 if (srv->iface_name)
311 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
312#endif
313
314 if (srv->sport_range) {
315 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100316 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200317
318 ret = 1;
319 src = srv->source_addr;
320
321 do {
322 /* note: in case of retry, we may have to release a previously
323 * allocated port, hence this loop's construct.
324 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200325 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
326 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200327
328 if (!attempts)
329 break;
330 attempts--;
331
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200332 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
333 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200334 break;
335
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200336 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200337 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200338
Willy Tarreau986a9d22012-08-30 21:11:38 +0200339 ret = tcp_bind_socket(fd, flags, &src, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200340 } while (ret != 0); /* binding NOK */
341 }
342 else {
Willy Tarreau986a9d22012-08-30 21:11:38 +0200343 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200344 }
345
346 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200347 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
348 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200349 close(fd);
350
351 if (ret == 1) {
352 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
353 be->id, srv->id);
354 send_log(be, LOG_EMERG,
355 "Cannot bind to source address before connect() for server %s/%s.\n",
356 be->id, srv->id);
357 } else {
358 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
359 be->id, srv->id);
360 send_log(be, LOG_EMERG,
361 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
362 be->id, srv->id);
363 }
364 return SN_ERR_RESOURCE;
365 }
366 }
367 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200368 int ret, flags = 0;
369
Willy Tarreau9650f372009-08-16 14:02:45 +0200370 switch (be->options & PR_O_TPXY_MASK) {
371 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200372 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200373 flags = 3;
374 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200376 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200377 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200378 break;
379 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200380
Willy Tarreau9650f372009-08-16 14:02:45 +0200381#ifdef SO_BINDTODEVICE
382 /* Note: this might fail if not CAP_NET_RAW */
383 if (be->iface_name)
384 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
385#endif
Willy Tarreau986a9d22012-08-30 21:11:38 +0200386 ret = tcp_bind_socket(fd, flags, &be->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200387 if (ret) {
388 close(fd);
389 if (ret == 1) {
390 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
391 be->id);
392 send_log(be, LOG_EMERG,
393 "Cannot bind to source address before connect() for proxy %s.\n",
394 be->id);
395 } else {
396 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
397 be->id);
398 send_log(be, LOG_EMERG,
399 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
400 be->id);
401 }
402 return SN_ERR_RESOURCE;
403 }
404 }
405
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400406#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200407 /* disabling tcp quick ack now allows the first request to leave the
408 * machine with the first ACK. We only do this if there are pending
409 * data in the buffer.
410 */
Willy Tarreau14f8e862012-08-30 22:23:13 +0200411 if ((be->options2 & PR_O2_SMARTCON) && data)
Simon Hormande072bd2011-06-24 15:11:37 +0900412 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200413#endif
414
Willy Tarreaue803de22010-01-21 17:43:04 +0100415 if (global.tune.server_sndbuf)
416 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
417
418 if (global.tune.server_rcvbuf)
419 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
420
Willy Tarreau986a9d22012-08-30 21:11:38 +0200421 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200422 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
423
424 if (errno == EAGAIN || errno == EADDRINUSE) {
425 char *msg;
426 if (errno == EAGAIN) /* no free ports left, try again later */
427 msg = "no free ports";
428 else
429 msg = "local address already in use";
430
431 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200432 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
433 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200434 close(fd);
435 send_log(be, LOG_EMERG,
436 "Connect() failed for server %s/%s: %s.\n",
437 be->id, srv->id, msg);
438 return SN_ERR_RESOURCE;
439 } else if (errno == ETIMEDOUT) {
440 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200441 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
442 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200443 close(fd);
444 return SN_ERR_SRVTO;
445 } else {
446 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
447 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200448 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
449 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200450 close(fd);
451 return SN_ERR_SRVCL;
452 }
453 }
454
Willy Tarreau986a9d22012-08-30 21:11:38 +0200455 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200456 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200457
Willy Tarreaud2274c62012-07-06 14:29:45 +0200458 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200459 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200460 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200461
462 if (conn_data_init(conn) < 0)
463 return SN_ERR_RESOURCE;
464
Willy Tarreau14f8e862012-08-30 22:23:13 +0200465 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200466 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200467
Willy Tarreau9650f372009-08-16 14:02:45 +0200468 return SN_ERR_NONE; /* connection is OK */
469}
470
471
Willy Tarreau59b94792012-05-11 16:16:40 +0200472/*
473 * Retrieves the source address for the socket <fd>, with <dir> indicating
474 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
475 * success, -1 in case of error. The socket's source address is stored in
476 * <sa> for <salen> bytes.
477 */
478int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
479{
480 if (dir)
481 return getsockname(fd, sa, &salen);
482 else
483 return getpeername(fd, sa, &salen);
484}
485
486
487/*
488 * Retrieves the original destination address for the socket <fd>, with <dir>
489 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
490 * listener, if the original destination address was translated, the original
491 * address is retrieved. It returns 0 in case of success, -1 in case of error.
492 * The socket's source address is stored in <sa> for <salen> bytes.
493 */
494int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
495{
496 if (dir)
497 return getpeername(fd, sa, &salen);
498#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
499 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
500 return 0;
501#endif
502 else
503 return getsockname(fd, sa, &salen);
504}
505
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200506/* This is the callback which is set when a connection establishment is pending
507 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200508 * once the connection is established. It updates the FD polling status. It
509 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
510 * it returns non-zero and removes itself from the connection's flags (the bit is
511 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200512 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200513int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200514{
Willy Tarreau239d7182012-07-23 18:53:03 +0200515 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200516
Willy Tarreau80184712012-07-06 14:54:49 +0200517 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200518 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200519
Willy Tarreau80184712012-07-06 14:54:49 +0200520 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200521 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200522
Willy Tarreau2da156f2012-07-23 15:07:23 +0200523 /* stop here if we reached the end of data */
524 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
525 goto out_error;
526
Willy Tarreau2c6be842012-07-06 17:12:34 +0200527 /* We have no data to send to check the connection, and
528 * getsockopt() will not inform us whether the connection
529 * is still pending. So we'll reuse connect() to check the
530 * state of the socket. This has the advantage of giving us
531 * the following info :
532 * - error
533 * - connecting (EALREADY, EINPROGRESS)
534 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200535 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200536 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200537 if (errno == EALREADY || errno == EINPROGRESS) {
538 conn_sock_stop_recv(conn);
539 conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200540 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200541 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200542
Willy Tarreau2c6be842012-07-06 17:12:34 +0200543 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200544 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200545
Willy Tarreau2c6be842012-07-06 17:12:34 +0200546 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200547 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200548
Willy Tarreau076be252012-07-06 16:02:29 +0200549 /* The FD is ready now, we'll mark the connection as complete and
550 * forward the event to the data layer which will update the stream
551 * interface flags.
Willy Tarreaua190d592012-05-20 18:35:19 +0200552 */
Willy Tarreau80184712012-07-06 14:54:49 +0200553 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200554 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200555
556 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200557 /* Write error on the file descriptor. Report it to the connection
558 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200559 */
560
Willy Tarreau80184712012-07-06 14:54:49 +0200561 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200562 conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200563 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200564}
565
Willy Tarreau59b94792012-05-11 16:16:40 +0200566
Willy Tarreaue6b98942007-10-29 01:09:36 +0100567/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
568 * an error message in <err> if the message is at most <errlen> bytes long
569 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
570 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
571 * was alright and that no message was returned. ERR_RETRYABLE means that an
572 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700573 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100574 * the meaning of the error, but just indicate that a message is present which
575 * should be displayed with the respective level. Last, ERR_ABORT indicates
576 * that it's pointless to try to start other listeners. No error message is
577 * returned if errlen is NULL.
578 */
579int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
580{
581 __label__ tcp_return, tcp_close_return;
582 int fd, err;
583 const char *msg = NULL;
584
585 /* ensure we never return garbage */
586 if (errmsg && errlen)
587 *errmsg = 0;
588
589 if (listener->state != LI_ASSIGNED)
590 return ERR_NONE; /* already bound */
591
592 err = ERR_NONE;
593
594 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
595 err |= ERR_RETRYABLE | ERR_ALERT;
596 msg = "cannot create listening socket";
597 goto tcp_return;
598 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100599
Willy Tarreaue6b98942007-10-29 01:09:36 +0100600 if (fd >= global.maxsock) {
601 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
602 msg = "not enough free sockets (raise '-n' parameter)";
603 goto tcp_close_return;
604 }
605
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200606 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100607 err |= ERR_FATAL | ERR_ALERT;
608 msg = "cannot make socket non-blocking";
609 goto tcp_close_return;
610 }
611
Simon Hormande072bd2011-06-24 15:11:37 +0900612 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100613 /* not fatal but should be reported */
614 msg = "cannot do so_reuseaddr";
615 err |= ERR_ALERT;
616 }
617
618 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900619 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100620
Willy Tarreaue6b98942007-10-29 01:09:36 +0100621#ifdef SO_REUSEPORT
622 /* OpenBSD supports this. As it's present in old libc versions of Linux,
623 * it might return an error that we will silently ignore.
624 */
Simon Hormande072bd2011-06-24 15:11:37 +0900625 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100626#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100627#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200628 if (listener->options & LI_O_FOREIGN) {
629 switch (listener->addr.ss_family) {
630 case AF_INET:
631 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
632 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
633 msg = "cannot make listening socket transparent";
634 err |= ERR_ALERT;
635 }
636 break;
637 case AF_INET6:
638 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
639 msg = "cannot make listening socket transparent";
640 err |= ERR_ALERT;
641 }
642 break;
643 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100644 }
645#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100646#ifdef SO_BINDTODEVICE
647 /* Note: this might fail if not CAP_NET_RAW */
648 if (listener->interface) {
649 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100650 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100651 msg = "cannot bind listener to device";
652 err |= ERR_WARN;
653 }
654 }
655#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400656#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100657 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400658 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200659 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
660 msg = "cannot set MSS";
661 err |= ERR_WARN;
662 }
663 }
664#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200665#if defined(TCP_DEFER_ACCEPT)
666 if (listener->options & LI_O_DEF_ACCEPT) {
667 /* defer accept by up to one second */
668 int accept_delay = 1;
669 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
670 msg = "cannot enable DEFER_ACCEPT";
671 err |= ERR_WARN;
672 }
673 }
674#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100675 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
676 err |= ERR_RETRYABLE | ERR_ALERT;
677 msg = "cannot bind socket";
678 goto tcp_close_return;
679 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100680
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100681 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100682 err |= ERR_RETRYABLE | ERR_ALERT;
683 msg = "cannot listen to socket";
684 goto tcp_close_return;
685 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100686
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400687#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200688 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900689 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200690#endif
691
Willy Tarreaue6b98942007-10-29 01:09:36 +0100692 /* the socket is ready */
693 listener->fd = fd;
694 listener->state = LI_LISTEN;
695
Willy Tarreaueabf3132008-08-29 23:36:51 +0200696 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200697 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200698 fd_insert(fd);
699
Willy Tarreaue6b98942007-10-29 01:09:36 +0100700 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100701 if (msg && errlen) {
702 char pn[INET6_ADDRSTRLEN];
703
Willy Tarreau631f01c2011-09-05 00:36:48 +0200704 addr_to_str(&listener->addr, pn, sizeof(pn));
705 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100706 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100707 return err;
708
709 tcp_close_return:
710 close(fd);
711 goto tcp_return;
712}
713
714/* This function creates all TCP sockets bound to the protocol entry <proto>.
715 * It is intended to be used as the protocol's bind_all() function.
716 * The sockets will be registered but not added to any fd_set, in order not to
717 * loose them across the fork(). A call to enable_all_listeners() is needed
718 * to complete initialization. The return value is composed from ERR_*.
719 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200720static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100721{
722 struct listener *listener;
723 int err = ERR_NONE;
724
725 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200726 err |= tcp_bind_listener(listener, errmsg, errlen);
727 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100728 break;
729 }
730
731 return err;
732}
733
734/* Add listener to the list of tcpv4 listeners. The listener's state
735 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
736 * listeners is updated. This is the function to use to add a new listener.
737 */
738void tcpv4_add_listener(struct listener *listener)
739{
740 if (listener->state != LI_INIT)
741 return;
742 listener->state = LI_ASSIGNED;
743 listener->proto = &proto_tcpv4;
744 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
745 proto_tcpv4.nb_listeners++;
746}
747
748/* Add listener to the list of tcpv4 listeners. The listener's state
749 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
750 * listeners is updated. This is the function to use to add a new listener.
751 */
752void tcpv6_add_listener(struct listener *listener)
753{
754 if (listener->state != LI_INIT)
755 return;
756 listener->state = LI_ASSIGNED;
757 listener->proto = &proto_tcpv6;
758 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
759 proto_tcpv6.nb_listeners++;
760}
761
Willy Tarreauedcf6682008-11-30 23:15:34 +0100762/* This function performs the TCP request analysis on the current request. It
763 * returns 1 if the processing can continue on next analysers, or zero if it
764 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200765 * request. It relies on buffers flags, and updates s->req->analysers. The
766 * function may be called for frontend rules and backend rules. It only relies
767 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100768 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200769int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100770{
771 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200772 struct stksess *ts;
773 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100774 int partial;
775
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100776 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 +0100777 now_ms, __FUNCTION__,
778 s,
779 req,
780 req->rex, req->wex,
781 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100782 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100783 req->analysers);
784
Willy Tarreauedcf6682008-11-30 23:15:34 +0100785 /* We don't know whether we have enough data, so must proceed
786 * this way :
787 * - iterate through all rules in their declaration order
788 * - if one rule returns MISS, it means the inspect delay is
789 * not over yet, then return immediately, otherwise consider
790 * it as a non-match.
791 * - if one rule returns OK, then return OK
792 * - if one rule returns KO, then return KO
793 */
794
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200795 if ((req->flags & CF_SHUTR) || buffer_full(&req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200796 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200797 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100798 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200799 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100800
Willy Tarreaufb356202010-08-03 14:02:05 +0200801 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100802 int ret = ACL_PAT_PASS;
803
804 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200805 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100806 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200807 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100808 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200809 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
810 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100811 return 0;
812 }
813
814 ret = acl_pass(ret);
815 if (rule->cond->pol == ACL_COND_UNLESS)
816 ret = !ret;
817 }
818
819 if (ret) {
820 /* we have a matching rule. */
821 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200822 channel_abort(req);
823 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100824 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200825
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100826 s->be->be_counters.denied_req++;
827 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200828 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200829 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200830
Willy Tarreauedcf6682008-11-30 23:15:34 +0100831 if (!(s->flags & SN_ERR_MASK))
832 s->flags |= SN_ERR_PRXCOND;
833 if (!(s->flags & SN_FINST_MASK))
834 s->flags |= SN_FINST_R;
835 return 0;
836 }
Willy Tarreau56123282010-08-06 19:06:56 +0200837 else if (rule->action == TCP_ACT_TRK_SC1) {
838 if (!s->stkctr1_entry) {
839 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200840 * Also, note that right now we can only track SRC so we
841 * don't check how to get the key, but later we may need
842 * to consider rule->act_prm->trk_ctr.type.
843 */
844 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +0200845 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200846 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200847 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200848 if (s->fe != s->be)
849 s->flags |= SN_BE_TRACK_SC1;
850 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200851 }
852 }
Willy Tarreau56123282010-08-06 19:06:56 +0200853 else if (rule->action == TCP_ACT_TRK_SC2) {
854 if (!s->stkctr2_entry) {
855 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200856 * Also, note that right now we can only track SRC so we
857 * don't check how to get the key, but later we may need
858 * to consider rule->act_prm->trk_ctr.type.
859 */
860 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +0200861 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200862 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200863 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200864 if (s->fe != s->be)
865 s->flags |= SN_BE_TRACK_SC2;
866 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200867 }
868 }
869 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100870 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200871 break;
872 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100873 }
874 }
875
876 /* if we get there, it means we have no rule which matches, or
877 * we have an explicit accept, so we apply the default accept.
878 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200879 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100880 req->analyse_exp = TICK_ETERNITY;
881 return 1;
882}
883
Emeric Brun97679e72010-09-23 17:56:44 +0200884/* This function performs the TCP response analysis on the current response. It
885 * returns 1 if the processing can continue on next analysers, or zero if it
886 * needs more data, encounters an error, or wants to immediately abort the
887 * response. It relies on buffers flags, and updates s->rep->analysers. The
888 * function may be called for backend rules.
889 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200890int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200891{
892 struct tcp_rule *rule;
893 int partial;
894
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100895 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 +0200896 now_ms, __FUNCTION__,
897 s,
898 rep,
899 rep->rex, rep->wex,
900 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100901 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200902 rep->analysers);
903
904 /* We don't know whether we have enough data, so must proceed
905 * this way :
906 * - iterate through all rules in their declaration order
907 * - if one rule returns MISS, it means the inspect delay is
908 * not over yet, then return immediately, otherwise consider
909 * it as a non-match.
910 * - if one rule returns OK, then return OK
911 * - if one rule returns KO, then return KO
912 */
913
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200914 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200915 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200916 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200917 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200918
919 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
920 int ret = ACL_PAT_PASS;
921
922 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200923 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200924 if (ret == ACL_PAT_MISS) {
925 /* just set the analyser timeout once at the beginning of the response */
926 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
927 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
928 return 0;
929 }
930
931 ret = acl_pass(ret);
932 if (rule->cond->pol == ACL_COND_UNLESS)
933 ret = !ret;
934 }
935
936 if (ret) {
937 /* we have a matching rule. */
938 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200939 channel_abort(rep);
940 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200941 rep->analysers = 0;
942
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100943 s->be->be_counters.denied_resp++;
944 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200945 if (s->listener->counters)
946 s->listener->counters->denied_resp++;
947
948 if (!(s->flags & SN_ERR_MASK))
949 s->flags |= SN_ERR_PRXCOND;
950 if (!(s->flags & SN_FINST_MASK))
951 s->flags |= SN_FINST_D;
952 return 0;
953 }
954 else {
955 /* otherwise accept */
956 break;
957 }
958 }
959 }
960
961 /* if we get there, it means we have no rule which matches, or
962 * we have an explicit accept, so we apply the default accept.
963 */
964 rep->analysers &= ~an_bit;
965 rep->analyse_exp = TICK_ETERNITY;
966 return 1;
967}
968
969
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200970/* This function performs the TCP layer4 analysis on the current request. It
971 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
972 * matches or if no more rule matches. It can only use rules which don't need
973 * any data.
974 */
975int tcp_exec_req_rules(struct session *s)
976{
977 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200978 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200979 struct stktable *t = NULL;
980 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200981 int ret;
982
983 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
984 ret = ACL_PAT_PASS;
985
986 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200987 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200988 ret = acl_pass(ret);
989 if (rule->cond->pol == ACL_COND_UNLESS)
990 ret = !ret;
991 }
992
993 if (ret) {
994 /* we have a matching rule. */
995 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100996 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200997 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200998 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200999
1000 if (!(s->flags & SN_ERR_MASK))
1001 s->flags |= SN_ERR_PRXCOND;
1002 if (!(s->flags & SN_FINST_MASK))
1003 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001004 result = 0;
1005 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001006 }
Willy Tarreau56123282010-08-06 19:06:56 +02001007 else if (rule->action == TCP_ACT_TRK_SC1) {
1008 if (!s->stkctr1_entry) {
1009 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001010 * Also, note that right now we can only track SRC so we
1011 * don't check how to get the key, but later we may need
1012 * to consider rule->act_prm->trk_ctr.type.
1013 */
1014 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +02001015 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001016 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001017 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001018 }
1019 }
Willy Tarreau56123282010-08-06 19:06:56 +02001020 else if (rule->action == TCP_ACT_TRK_SC2) {
1021 if (!s->stkctr2_entry) {
1022 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001023 * Also, note that right now we can only track SRC so we
1024 * don't check how to get the key, but later we may need
1025 * to consider rule->act_prm->trk_ctr.type.
1026 */
1027 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +02001028 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001029 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001030 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001031 }
1032 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001033 else {
1034 /* otherwise it's an accept */
1035 break;
1036 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001037 }
1038 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001039 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001040}
1041
Emeric Brun97679e72010-09-23 17:56:44 +02001042/* Parse a tcp-response rule. Return a negative value in case of failure */
1043static int tcp_parse_response_rule(char **args, int arg, int section_type,
1044 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001045 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001046{
1047 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001048 memprintf(err, "%s %s is only allowed in 'backend' sections",
1049 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001050 return -1;
1051 }
1052
1053 if (strcmp(args[arg], "accept") == 0) {
1054 arg++;
1055 rule->action = TCP_ACT_ACCEPT;
1056 }
1057 else if (strcmp(args[arg], "reject") == 0) {
1058 arg++;
1059 rule->action = TCP_ACT_REJECT;
1060 }
1061 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001062 memprintf(err,
1063 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1064 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001065 return -1;
1066 }
1067
1068 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001069 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1070 memprintf(err,
1071 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1072 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001073 return -1;
1074 }
1075 }
1076 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001077 memprintf(err,
1078 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001079 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1080 return -1;
1081 }
1082 return 0;
1083}
1084
1085
1086
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001087/* Parse a tcp-request rule. Return a negative value in case of failure */
1088static int tcp_parse_request_rule(char **args, int arg, int section_type,
1089 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001090 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001091{
1092 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001093 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1094 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001095 return -1;
1096 }
1097
1098 if (!strcmp(args[arg], "accept")) {
1099 arg++;
1100 rule->action = TCP_ACT_ACCEPT;
1101 }
1102 else if (!strcmp(args[arg], "reject")) {
1103 arg++;
1104 rule->action = TCP_ACT_REJECT;
1105 }
Willy Tarreau56123282010-08-06 19:06:56 +02001106 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001107 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001108 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001109
1110 arg++;
1111 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001112 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001113
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001114 if (ret < 0) { /* nb: warnings are not handled yet */
1115 memprintf(err,
1116 "'%s %s %s' : %s in %s '%s'",
1117 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1118 return ret;
1119 }
Willy Tarreau56123282010-08-06 19:06:56 +02001120 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001121 }
Willy Tarreau56123282010-08-06 19:06:56 +02001122 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001123 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001124 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001125
1126 arg++;
1127 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001128 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001129
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001130 if (ret < 0) { /* nb: warnings are not handled yet */
1131 memprintf(err,
1132 "'%s %s %s' : %s in %s '%s'",
1133 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1134 return ret;
1135 }
Willy Tarreau56123282010-08-06 19:06:56 +02001136 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001137 }
1138 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001139 memprintf(err,
1140 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1141 "or 'track-sc2' in %s '%s' (got '%s')",
1142 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001143 return -1;
1144 }
1145
1146 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001147 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1148 memprintf(err,
1149 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1150 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001151 return -1;
1152 }
1153 }
1154 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001155 memprintf(err,
1156 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001157 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1158 return -1;
1159 }
1160 return 0;
1161}
1162
Emeric Brun97679e72010-09-23 17:56:44 +02001163/* This function should be called to parse a line starting with the "tcp-response"
1164 * keyword.
1165 */
1166static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001167 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001168{
1169 const char *ptr = NULL;
1170 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001171 int warn = 0;
1172 int arg;
1173 struct tcp_rule *rule;
1174
1175 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001176 memprintf(err, "missing argument for '%s' in %s '%s'",
1177 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001178 return -1;
1179 }
1180
1181 if (strcmp(args[1], "inspect-delay") == 0) {
1182 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001183 memprintf(err, "%s %s is only allowed in 'backend' sections",
1184 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001185 return -1;
1186 }
1187
1188 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001189 memprintf(err,
1190 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1191 args[0], args[1], proxy_type_str(curpx), curpx->id);
1192 if (ptr)
1193 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001194 return -1;
1195 }
1196
1197 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001198 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1199 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001200 return 1;
1201 }
1202 curpx->tcp_rep.inspect_delay = val;
1203 return 0;
1204 }
1205
Simon Hormande072bd2011-06-24 15:11:37 +09001206 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001207 LIST_INIT(&rule->list);
1208 arg = 1;
1209
1210 if (strcmp(args[1], "content") == 0) {
1211 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001212 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001213 goto error;
1214
1215 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1216 struct acl *acl;
1217 const char *name;
1218
1219 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1220 name = acl ? acl->name : "(unknown)";
1221
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001222 memprintf(err,
1223 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1224 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001225 warn++;
1226 }
1227
1228 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1229 }
1230 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001231 memprintf(err,
1232 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1233 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001234 goto error;
1235 }
1236
1237 return warn;
1238 error:
1239 free(rule);
1240 return -1;
1241}
1242
1243
Willy Tarreaub6866442008-07-14 23:54:42 +02001244/* This function should be called to parse a line starting with the "tcp-request"
1245 * keyword.
1246 */
1247static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001248 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001249{
1250 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001251 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001252 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001253 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001254 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001255
1256 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001257 if (curpx == defpx)
1258 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1259 else
1260 memprintf(err, "missing argument for '%s' in %s '%s'",
1261 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001262 return -1;
1263 }
1264
1265 if (!strcmp(args[1], "inspect-delay")) {
1266 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001267 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1268 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001269 return -1;
1270 }
1271
Willy Tarreaub6866442008-07-14 23:54:42 +02001272 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001273 memprintf(err,
1274 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1275 args[0], args[1], proxy_type_str(curpx), curpx->id);
1276 if (ptr)
1277 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001278 return -1;
1279 }
1280
1281 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001282 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1283 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001284 return 1;
1285 }
1286 curpx->tcp_req.inspect_delay = val;
1287 return 0;
1288 }
1289
Simon Hormande072bd2011-06-24 15:11:37 +09001290 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001291 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001292 arg = 1;
1293
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001294 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001295 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001296 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001297 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001298
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001299 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001300 struct acl *acl;
1301 const char *name;
1302
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001303 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001304 name = acl ? acl->name : "(unknown)";
1305
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001306 memprintf(err,
1307 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1308 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001309 warn++;
1310 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001311 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001312 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001313 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001314 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001315
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001316 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001317 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1318 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001319 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001320 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001321
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001322 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001323 goto error;
1324
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001325 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1326 struct acl *acl;
1327 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001328
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001329 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1330 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001331
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001332 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001333 memprintf(err,
1334 "'%s %s' may not reference acl '%s' which makes use of "
1335 "payload in %s '%s'. Please use '%s content' for this.",
1336 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001337 goto error;
1338 }
1339 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001340 memprintf(err,
1341 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1342 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001343 warn++;
1344 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001345 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001346 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001347 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001348 if (curpx == defpx)
1349 memprintf(err,
1350 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1351 args[0], args[1]);
1352 else
1353 memprintf(err,
1354 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1355 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001356 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001357 }
1358
Willy Tarreau1a687942010-05-23 22:40:30 +02001359 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001360 error:
1361 free(rule);
1362 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001363}
1364
Willy Tarreau645513a2010-05-24 20:55:15 +02001365
1366/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001367/* All supported sample fetch functios must be declared here */
1368/************************************************************************/
1369
1370/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001371 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001372 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1373 * is a string (cookie name), other types will lead to undefined behaviour.
1374 */
1375int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001376smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001377 const struct arg *args, struct sample *smp)
1378{
1379 int bleft;
1380 const unsigned char *data;
1381
1382 if (!l4 || !l4->req)
1383 return 0;
1384
1385 smp->flags = 0;
1386 smp->type = SMP_T_CSTR;
1387
Willy Tarreau572bf902012-07-02 17:01:20 +02001388 bleft = l4->req->buf.i;
Willy Tarreau32389b72012-04-23 23:13:20 +02001389 if (bleft <= 11)
1390 goto too_short;
1391
Willy Tarreau572bf902012-07-02 17:01:20 +02001392 data = (const unsigned char *)l4->req->buf.p + 11;
Willy Tarreau32389b72012-04-23 23:13:20 +02001393 bleft -= 11;
1394
1395 if (bleft <= 7)
1396 goto too_short;
1397
1398 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1399 goto not_cookie;
1400
1401 data += 7;
1402 bleft -= 7;
1403
1404 while (bleft > 0 && *data == ' ') {
1405 data++;
1406 bleft--;
1407 }
1408
1409 if (args) {
1410
1411 if (bleft <= args->data.str.len)
1412 goto too_short;
1413
1414 if ((data[args->data.str.len] != '=') ||
1415 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1416 goto not_cookie;
1417
1418 data += args->data.str.len + 1;
1419 bleft -= args->data.str.len + 1;
1420 } else {
1421 while (bleft > 0 && *data != '=') {
1422 if (*data == '\r' || *data == '\n')
1423 goto not_cookie;
1424 data++;
1425 bleft--;
1426 }
1427
1428 if (bleft < 1)
1429 goto too_short;
1430
1431 if (*data != '=')
1432 goto not_cookie;
1433
1434 data++;
1435 bleft--;
1436 }
1437
1438 /* data points to cookie value */
1439 smp->data.str.str = (char *)data;
1440 smp->data.str.len = 0;
1441
1442 while (bleft > 0 && *data != '\r') {
1443 data++;
1444 bleft--;
1445 }
1446
1447 if (bleft < 2)
1448 goto too_short;
1449
1450 if (data[0] != '\r' || data[1] != '\n')
1451 goto not_cookie;
1452
1453 smp->data.str.len = (char *)data - smp->data.str.str;
1454 smp->flags = SMP_F_VOLATILE;
1455 return 1;
1456
1457 too_short:
1458 smp->flags = SMP_F_MAY_CHANGE;
1459 not_cookie:
1460 return 0;
1461}
1462
Willy Tarreau32389b72012-04-23 23:13:20 +02001463/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001464/* All supported ACL keywords must be declared here. */
1465/************************************************************************/
1466
Willy Tarreau32389b72012-04-23 23:13:20 +02001467/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1468static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001469acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001470 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001471{
1472 int ret;
1473
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001474 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001475
1476 if (smp->flags & SMP_F_MAY_CHANGE)
1477 return 0;
1478
1479 smp->flags = SMP_F_VOLATILE;
1480 smp->type = SMP_T_UINT;
1481 smp->data.uint = ret;
1482 return 1;
1483}
1484
1485
Willy Tarreau4a129812012-04-25 17:31:42 +02001486/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001487static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001488smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001489 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001490{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001491 switch (l4->si[0].conn.addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001492 case AF_INET:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001493 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn.addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001494 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001495 break;
1496 case AF_INET6:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001497 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn.addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001498 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001499 break;
1500 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001501 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001502 }
Emeric Brunf769f512010-10-22 17:14:01 +02001503
Willy Tarreau37406352012-04-23 16:16:37 +02001504 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001505 return 1;
1506}
1507
Willy Tarreaua5e37562011-12-16 17:06:15 +01001508/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001509static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001510smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001511 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001512{
Willy Tarreauf853c462012-04-23 18:53:56 +02001513 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02001514 if (!(smp->data.uint = get_host_port(&l4->si[0].conn.addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001515 return 0;
1516
Willy Tarreau37406352012-04-23 16:16:37 +02001517 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001518 return 1;
1519}
1520
Willy Tarreau4a129812012-04-25 17:31:42 +02001521/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001522static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001523smp_fetch_dst(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 Tarreau986a9d22012-08-30 21:11:38 +02001526 conn_get_to_addr(&l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001527
Willy Tarreau986a9d22012-08-30 21:11:38 +02001528 switch (l4->si[0].conn.addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001529 case AF_INET:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001530 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn.addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001531 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001532 break;
1533 case AF_INET6:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001534 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn.addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001535 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001536 break;
1537 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001538 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001539 }
Emeric Brunf769f512010-10-22 17:14:01 +02001540
Willy Tarreau37406352012-04-23 16:16:37 +02001541 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001542 return 1;
1543}
1544
Willy Tarreaua5e37562011-12-16 17:06:15 +01001545/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001546static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001547smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001548 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001549{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001550 conn_get_to_addr(&l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001551
Willy Tarreauf853c462012-04-23 18:53:56 +02001552 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02001553 if (!(smp->data.uint = get_host_port(&l4->si[0].conn.addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001554 return 0;
1555
Willy Tarreau37406352012-04-23 16:16:37 +02001556 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001557 return 1;
1558}
1559
1560static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001561smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1562 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001563{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001564 unsigned int len_offset = arg_p[0].data.uint;
1565 unsigned int len_size = arg_p[1].data.uint;
1566 unsigned int buf_offset;
1567 unsigned int buf_size = 0;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001568 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001569 int i;
1570
1571 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1572 /* by default buf offset == len offset + len size */
1573 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1574
1575 if (!l4)
1576 return 0;
1577
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001578 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001579
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001580 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001581 return 0;
1582
Willy Tarreau572bf902012-07-02 17:01:20 +02001583 if (len_offset + len_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001584 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001585
1586 for (i = 0; i < len_size; i++) {
Willy Tarreau572bf902012-07-02 17:01:20 +02001587 buf_size = (buf_size << 8) + ((unsigned char *)b->buf.p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001588 }
1589
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001590 /* buf offset may be implicit, absolute or relative */
1591 buf_offset = len_offset + len_size;
1592 if (arg_p[2].type == ARGT_UINT)
1593 buf_offset = arg_p[2].data.uint;
1594 else if (arg_p[2].type == ARGT_SINT)
1595 buf_offset += arg_p[2].data.sint;
1596
Willy Tarreau572bf902012-07-02 17:01:20 +02001597 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001598 /* will never match */
1599 smp->flags = 0;
1600 return 0;
1601 }
1602
Willy Tarreau572bf902012-07-02 17:01:20 +02001603 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001604 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001605
1606 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001607 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001608 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001609 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001610 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001611
1612 too_short:
1613 smp->flags = SMP_F_MAY_CHANGE;
1614 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001615}
1616
1617static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001618smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1619 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001620{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001621 unsigned int buf_offset = arg_p[0].data.uint;
1622 unsigned int buf_size = arg_p[1].data.uint;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001623 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001624
1625 if (!l4)
1626 return 0;
1627
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001628 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001629
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001630 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001631 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001632
Willy Tarreau572bf902012-07-02 17:01:20 +02001633 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001634 /* will never match */
1635 smp->flags = 0;
1636 return 0;
1637 }
Emericf2d7cae2010-11-05 18:13:50 +01001638
Willy Tarreau572bf902012-07-02 17:01:20 +02001639 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001640 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001641
1642 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001643 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001644 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001645 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001646 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001647
1648 too_short:
1649 smp->flags = SMP_F_MAY_CHANGE;
1650 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001651}
1652
Willy Tarreau21d68a62012-04-20 15:52:36 +02001653/* This function is used to validate the arguments passed to a "payload" fetch
1654 * keyword. This keyword expects two positive integers, with the second one
1655 * being strictly positive. It is assumed that the types are already the correct
1656 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1657 * filled with a pointer to an error message in case of error, that the caller
1658 * is responsible for freeing. The initial location must either be freeable or
1659 * NULL.
1660 */
1661static int val_payload(struct arg *arg, char **err_msg)
1662{
1663 if (!arg[1].data.uint) {
1664 if (err_msg)
1665 memprintf(err_msg, "payload length must be > 0");
1666 return 0;
1667 }
1668 return 1;
1669}
1670
1671/* This function is used to validate the arguments passed to a "payload_lv" fetch
1672 * keyword. This keyword allows two positive integers and an optional signed one,
1673 * with the second one being strictly positive and the third one being greater than
1674 * the opposite of the two others if negative. It is assumed that the types are
1675 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1676 * not NULL, it will be filled with a pointer to an error message in case of
1677 * error, that the caller is responsible for freeing. The initial location must
1678 * either be freeable or NULL.
1679 */
1680static int val_payload_lv(struct arg *arg, char **err_msg)
1681{
1682 if (!arg[1].data.uint) {
1683 if (err_msg)
1684 memprintf(err_msg, "payload length must be > 0");
1685 return 0;
1686 }
1687
1688 if (arg[2].type == ARGT_SINT &&
1689 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1690 if (err_msg)
1691 memprintf(err_msg, "payload offset too negative");
1692 return 0;
1693 }
1694 return 1;
1695}
1696
Willy Tarreaub6866442008-07-14 23:54:42 +02001697static struct cfg_kw_list cfg_kws = {{ },{
1698 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001699 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001700 { 0, NULL, NULL },
1701}};
1702
Willy Tarreau61612d42012-04-19 18:42:05 +02001703/* Note: must not be declared <const> as its list will be overwritten.
1704 * Please take care of keeping this list alphabetically sorted.
1705 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001706static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001707 { "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 +02001708 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001709 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1710 { "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 +02001711 { "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 +02001712 { "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 +02001713 { "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 +02001714 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001715 { NULL, NULL, NULL, NULL },
1716}};
1717
Willy Tarreau4a129812012-04-25 17:31:42 +02001718/* Note: must not be declared <const> as its list will be overwritten.
1719 * Note: fetches that may return multiple types must be declared as the lowest
1720 * common denominator, the type that can be casted into all other ones. For
1721 * instance v4/v6 must be declared v4.
1722 */
Willy Tarreau12785782012-04-27 21:37:17 +02001723static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001724 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001725 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001726 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001727 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1728 { "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 +02001729 { "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 +02001730 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001731 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001732}};
1733
Willy Tarreaue6b98942007-10-29 01:09:36 +01001734__attribute__((constructor))
1735static void __tcp_protocol_init(void)
1736{
1737 protocol_register(&proto_tcpv4);
1738 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001739 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001740 cfg_register_keywords(&cfg_kws);
1741 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001742}
1743
1744
1745/*
1746 * Local variables:
1747 * c-indent-level: 8
1748 * c-basic-offset: 8
1749 * End:
1750 */