blob: af9dbf95b0e7ce590b7b8ac8090933d7278fdda2 [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 Tarreau9650f372009-08-16 14:02:45 +020044#include <proto/log.h>
45#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010046#include <proto/protocols.h>
47#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020049#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020050#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020051#include <proto/stick_table.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020052#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010053
Willy Tarreaue8c66af2008-01-13 18:40:14 +010054#ifdef CONFIG_HAP_CTTPROXY
55#include <import/ip_tproxy.h>
56#endif
57
Emeric Bruncf20bf12010-10-22 16:06:11 +020058static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
59static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010060
61/* Note: must not be declared <const> as its list will be overwritten */
62static struct protocol proto_tcpv4 = {
63 .name = "tcpv4",
64 .sock_domain = AF_INET,
65 .sock_type = SOCK_STREAM,
66 .sock_prot = IPPROTO_TCP,
67 .sock_family = AF_INET,
68 .sock_addrlen = sizeof(struct sockaddr_in),
69 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020070 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020071 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020072 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010073 .bind_all = tcp_bind_listeners,
74 .unbind_all = unbind_all_listeners,
75 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020076 .get_src = tcp_get_src,
77 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010078 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
79 .nb_listeners = 0,
80};
81
82/* Note: must not be declared <const> as its list will be overwritten */
83static struct protocol proto_tcpv6 = {
84 .name = "tcpv6",
85 .sock_domain = AF_INET6,
86 .sock_type = SOCK_STREAM,
87 .sock_prot = IPPROTO_TCP,
88 .sock_family = AF_INET6,
89 .sock_addrlen = sizeof(struct sockaddr_in6),
90 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020091 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020092 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020093 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010094 .bind_all = tcp_bind_listeners,
95 .unbind_all = unbind_all_listeners,
96 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020097 .get_src = tcp_get_src,
98 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010099 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
100 .nb_listeners = 0,
101};
102
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100103
David du Colombier6f5ccb12011-03-10 22:26:24 +0100104/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100105 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
106 * - 0 : ignore remote address (may even be a NULL pointer)
107 * - 1 : use provided address
108 * - 2 : use provided port
109 * - 3 : use both
110 *
111 * The function supports multiple foreign binding methods :
112 * - linux_tproxy: we directly bind to the foreign address
113 * - cttproxy: we bind to a local address then nat.
114 * The second one can be used as a fallback for the first one.
115 * This function returns 0 when everything's OK, 1 if it could not bind, to the
116 * local address, 2 if it could not bind to the foreign address.
117 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100118int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100119{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100120 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100121 int foreign_ok = 0;
122 int ret;
123
124#ifdef CONFIG_HAP_LINUX_TPROXY
125 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200126 static int ip6_transp_working = 1;
127 switch (local->ss_family) {
128 case AF_INET:
129 if (flags && ip_transp_working) {
130 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
131 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
132 foreign_ok = 1;
133 else
134 ip_transp_working = 0;
135 }
136 break;
137 case AF_INET6:
138 if (flags && ip6_transp_working) {
139 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
140 foreign_ok = 1;
141 else
142 ip6_transp_working = 0;
143 }
144 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100145 }
146#endif
147 if (flags) {
148 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200149 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100150 switch (remote->ss_family) {
151 case AF_INET:
152 if (flags & 1)
153 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
154 if (flags & 2)
155 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
156 break;
157 case AF_INET6:
158 if (flags & 1)
159 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
160 if (flags & 2)
161 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
162 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100163 default:
164 /* we don't want to try to bind to an unknown address family */
165 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100166 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100167 }
168
Simon Hormande072bd2011-06-24 15:11:37 +0900169 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100170 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200171 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100172 if (ret < 0)
173 return 2;
174 }
175 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200176 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100177 if (ret < 0)
178 return 1;
179 }
180
181 if (!flags)
182 return 0;
183
184#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100185 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100186 struct in_tproxy itp1, itp2;
187 memset(&itp1, 0, sizeof(itp1));
188
189 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100190 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
191 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100192
193 /* set connect flag on socket */
194 itp2.op = TPROXY_FLAGS;
195 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
196
197 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
198 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
199 foreign_ok = 1;
200 }
201 }
202#endif
203 if (!foreign_ok)
204 /* we could not bind to a foreign address */
205 return 2;
206
207 return 0;
208}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100209
Willy Tarreau9650f372009-08-16 14:02:45 +0200210
211/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200212 * This function initiates a TCP connection establishment to the target assigned
213 * to connection <conn> using (si->{target,addr.to}). A source address may be
214 * pointed to by conn->addr.from in case of transparent proxying. Normal source
215 * bind addresses are still determined locally (due to the possible need of a
216 * source port). conn->target may point either to a valid server or to a backend,
217 * depending on conn->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are
218 * supported. The <data> parameter is a boolean indicating whether there are data
219 * waiting for being sent or not, in order to adjust data write polling and on
220 * some platforms, the ability to avoid an empty initial ACK.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200221 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200222 * It can return one of :
223 * - SN_ERR_NONE if everything's OK
224 * - SN_ERR_SRVTO if there are no more servers
225 * - SN_ERR_SRVCL if the connection was refused by the server
226 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
227 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
228 * - SN_ERR_INTERNAL for any other purely internal errors
229 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
230 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100231
Willy Tarreau14f8e862012-08-30 22:23:13 +0200232int tcp_connect_server(struct connection *conn, int data)
Willy Tarreau9650f372009-08-16 14:02:45 +0200233{
234 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100235 struct server *srv;
236 struct proxy *be;
237
Willy Tarreau986a9d22012-08-30 21:11:38 +0200238 switch (conn->target.type) {
Willy Tarreauac825402011-03-04 22:04:29 +0100239 case TARG_TYPE_PROXY:
Willy Tarreau986a9d22012-08-30 21:11:38 +0200240 be = conn->target.ptr.p;
Willy Tarreauac825402011-03-04 22:04:29 +0100241 srv = NULL;
242 break;
243 case TARG_TYPE_SERVER:
Willy Tarreau986a9d22012-08-30 21:11:38 +0200244 srv = conn->target.ptr.s;
Willy Tarreauac825402011-03-04 22:04:29 +0100245 be = srv->proxy;
246 break;
247 default:
248 return SN_ERR_INTERNAL;
249 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200250
Willy Tarreau986a9d22012-08-30 21:11:38 +0200251 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200252 qfprintf(stderr, "Cannot get a server socket.\n");
253
254 if (errno == ENFILE)
255 send_log(be, LOG_EMERG,
256 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
257 be->id, maxfd);
258 else if (errno == EMFILE)
259 send_log(be, LOG_EMERG,
260 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
261 be->id, maxfd);
262 else if (errno == ENOBUFS || errno == ENOMEM)
263 send_log(be, LOG_EMERG,
264 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
265 be->id, maxfd);
266 /* this is a resource error */
267 return SN_ERR_RESOURCE;
268 }
269
270 if (fd >= global.maxsock) {
271 /* do not log anything there, it's a normal condition when this option
272 * is used to serialize connections to a server !
273 */
274 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
275 close(fd);
276 return SN_ERR_PRXCOND; /* it is a configuration limit */
277 }
278
279 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900280 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200281 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
282 close(fd);
283 return SN_ERR_INTERNAL;
284 }
285
286 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900287 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200288
Willy Tarreau9650f372009-08-16 14:02:45 +0200289 /* allow specific binding :
290 * - server-specific at first
291 * - proxy-specific next
292 */
293 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200294 int ret, flags = 0;
295
Willy Tarreau9650f372009-08-16 14:02:45 +0200296 switch (srv->state & SRV_TPROXY_MASK) {
297 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200298 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200299 flags = 3;
300 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200301 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200302 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200303 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200304 break;
305 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200306
Willy Tarreau9650f372009-08-16 14:02:45 +0200307#ifdef SO_BINDTODEVICE
308 /* Note: this might fail if not CAP_NET_RAW */
309 if (srv->iface_name)
310 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
311#endif
312
313 if (srv->sport_range) {
314 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100315 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200316
317 ret = 1;
318 src = srv->source_addr;
319
320 do {
321 /* note: in case of retry, we may have to release a previously
322 * allocated port, hence this loop's construct.
323 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200324 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
325 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200326
327 if (!attempts)
328 break;
329 attempts--;
330
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200331 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
332 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200333 break;
334
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200335 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200336 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200337
Willy Tarreau986a9d22012-08-30 21:11:38 +0200338 ret = tcp_bind_socket(fd, flags, &src, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200339 } while (ret != 0); /* binding NOK */
340 }
341 else {
Willy Tarreau986a9d22012-08-30 21:11:38 +0200342 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200343 }
344
345 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200346 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
347 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200348 close(fd);
349
350 if (ret == 1) {
351 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
352 be->id, srv->id);
353 send_log(be, LOG_EMERG,
354 "Cannot bind to source address before connect() for server %s/%s.\n",
355 be->id, srv->id);
356 } else {
357 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
358 be->id, srv->id);
359 send_log(be, LOG_EMERG,
360 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
361 be->id, srv->id);
362 }
363 return SN_ERR_RESOURCE;
364 }
365 }
366 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200367 int ret, flags = 0;
368
Willy Tarreau9650f372009-08-16 14:02:45 +0200369 switch (be->options & PR_O_TPXY_MASK) {
370 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200371 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200372 flags = 3;
373 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200374 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200375 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200376 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200377 break;
378 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200379
Willy Tarreau9650f372009-08-16 14:02:45 +0200380#ifdef SO_BINDTODEVICE
381 /* Note: this might fail if not CAP_NET_RAW */
382 if (be->iface_name)
383 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
384#endif
Willy Tarreau986a9d22012-08-30 21:11:38 +0200385 ret = tcp_bind_socket(fd, flags, &be->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200386 if (ret) {
387 close(fd);
388 if (ret == 1) {
389 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
390 be->id);
391 send_log(be, LOG_EMERG,
392 "Cannot bind to source address before connect() for proxy %s.\n",
393 be->id);
394 } else {
395 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
396 be->id);
397 send_log(be, LOG_EMERG,
398 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
399 be->id);
400 }
401 return SN_ERR_RESOURCE;
402 }
403 }
404
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400405#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200406 /* disabling tcp quick ack now allows the first request to leave the
407 * machine with the first ACK. We only do this if there are pending
408 * data in the buffer.
409 */
Willy Tarreau14f8e862012-08-30 22:23:13 +0200410 if ((be->options2 & PR_O2_SMARTCON) && data)
Simon Hormande072bd2011-06-24 15:11:37 +0900411 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200412#endif
413
Willy Tarreaue803de22010-01-21 17:43:04 +0100414 if (global.tune.server_sndbuf)
415 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
416
417 if (global.tune.server_rcvbuf)
418 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
419
Willy Tarreau986a9d22012-08-30 21:11:38 +0200420 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200421 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
422
423 if (errno == EAGAIN || errno == EADDRINUSE) {
424 char *msg;
425 if (errno == EAGAIN) /* no free ports left, try again later */
426 msg = "no free ports";
427 else
428 msg = "local address already in use";
429
430 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200431 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
432 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200433 close(fd);
434 send_log(be, LOG_EMERG,
435 "Connect() failed for server %s/%s: %s.\n",
436 be->id, srv->id, msg);
437 return SN_ERR_RESOURCE;
438 } else if (errno == ETIMEDOUT) {
439 //qfprintf(stderr,"Connect(): ETIMEDOUT");
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 return SN_ERR_SRVTO;
444 } else {
445 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
446 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200447 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
448 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200449 close(fd);
450 return SN_ERR_SRVCL;
451 }
452 }
453
Willy Tarreau986a9d22012-08-30 21:11:38 +0200454 fdtab[fd].owner = conn;
Willy Tarreau9650f372009-08-16 14:02:45 +0200455 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
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 Tarreau14f8e862012-08-30 22:23:13 +0200461 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200462 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200463
Willy Tarreau9650f372009-08-16 14:02:45 +0200464 return SN_ERR_NONE; /* connection is OK */
465}
466
467
Willy Tarreau59b94792012-05-11 16:16:40 +0200468/*
469 * Retrieves the source address for the socket <fd>, with <dir> indicating
470 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
471 * success, -1 in case of error. The socket's source address is stored in
472 * <sa> for <salen> bytes.
473 */
474int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
475{
476 if (dir)
477 return getsockname(fd, sa, &salen);
478 else
479 return getpeername(fd, sa, &salen);
480}
481
482
483/*
484 * Retrieves the original destination address for the socket <fd>, with <dir>
485 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
486 * listener, if the original destination address was translated, the original
487 * address is retrieved. It returns 0 in case of success, -1 in case of error.
488 * The socket's source address is stored in <sa> for <salen> bytes.
489 */
490int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
491{
492 if (dir)
493 return getpeername(fd, sa, &salen);
494#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
495 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
496 return 0;
497#endif
498 else
499 return getsockname(fd, sa, &salen);
500}
501
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200502/* This is the callback which is set when a connection establishment is pending
503 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200504 * once the connection is established. It updates the FD polling status. It
505 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
506 * it returns non-zero and removes itself from the connection's flags (the bit is
507 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200508 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200509int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200510{
Willy Tarreau239d7182012-07-23 18:53:03 +0200511 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200512
Willy Tarreau80184712012-07-06 14:54:49 +0200513 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200514 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200515
Willy Tarreau80184712012-07-06 14:54:49 +0200516 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200517 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200518
Willy Tarreau2da156f2012-07-23 15:07:23 +0200519 /* stop here if we reached the end of data */
520 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
521 goto out_error;
522
Willy Tarreau2c6be842012-07-06 17:12:34 +0200523 /* We have no data to send to check the connection, and
524 * getsockopt() will not inform us whether the connection
525 * is still pending. So we'll reuse connect() to check the
526 * state of the socket. This has the advantage of giving us
527 * the following info :
528 * - error
529 * - connecting (EALREADY, EINPROGRESS)
530 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200531 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200532 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200533 if (errno == EALREADY || errno == EINPROGRESS) {
534 conn_sock_stop_recv(conn);
535 conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200536 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200537 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200538
Willy Tarreau2c6be842012-07-06 17:12:34 +0200539 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200540 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200541
Willy Tarreau2c6be842012-07-06 17:12:34 +0200542 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200543 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200544
Willy Tarreau076be252012-07-06 16:02:29 +0200545 /* The FD is ready now, we'll mark the connection as complete and
546 * forward the event to the data layer which will update the stream
547 * interface flags.
Willy Tarreaua190d592012-05-20 18:35:19 +0200548 */
Willy Tarreau80184712012-07-06 14:54:49 +0200549 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200550 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200551
552 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200553 /* Write error on the file descriptor. Report it to the connection
554 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200555 */
556
Willy Tarreau80184712012-07-06 14:54:49 +0200557 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200558 conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200559 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200560}
561
Willy Tarreau59b94792012-05-11 16:16:40 +0200562
Willy Tarreaue6b98942007-10-29 01:09:36 +0100563/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
564 * an error message in <err> if the message is at most <errlen> bytes long
565 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
566 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
567 * was alright and that no message was returned. ERR_RETRYABLE means that an
568 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700569 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100570 * the meaning of the error, but just indicate that a message is present which
571 * should be displayed with the respective level. Last, ERR_ABORT indicates
572 * that it's pointless to try to start other listeners. No error message is
573 * returned if errlen is NULL.
574 */
575int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
576{
577 __label__ tcp_return, tcp_close_return;
578 int fd, err;
579 const char *msg = NULL;
580
581 /* ensure we never return garbage */
582 if (errmsg && errlen)
583 *errmsg = 0;
584
585 if (listener->state != LI_ASSIGNED)
586 return ERR_NONE; /* already bound */
587
588 err = ERR_NONE;
589
590 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
591 err |= ERR_RETRYABLE | ERR_ALERT;
592 msg = "cannot create listening socket";
593 goto tcp_return;
594 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100595
Willy Tarreaue6b98942007-10-29 01:09:36 +0100596 if (fd >= global.maxsock) {
597 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
598 msg = "not enough free sockets (raise '-n' parameter)";
599 goto tcp_close_return;
600 }
601
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200602 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100603 err |= ERR_FATAL | ERR_ALERT;
604 msg = "cannot make socket non-blocking";
605 goto tcp_close_return;
606 }
607
Simon Hormande072bd2011-06-24 15:11:37 +0900608 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100609 /* not fatal but should be reported */
610 msg = "cannot do so_reuseaddr";
611 err |= ERR_ALERT;
612 }
613
614 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900615 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100616
Willy Tarreaue6b98942007-10-29 01:09:36 +0100617#ifdef SO_REUSEPORT
618 /* OpenBSD supports this. As it's present in old libc versions of Linux,
619 * it might return an error that we will silently ignore.
620 */
Simon Hormande072bd2011-06-24 15:11:37 +0900621 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100622#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100623#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200624 if (listener->options & LI_O_FOREIGN) {
625 switch (listener->addr.ss_family) {
626 case AF_INET:
627 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
628 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
629 msg = "cannot make listening socket transparent";
630 err |= ERR_ALERT;
631 }
632 break;
633 case AF_INET6:
634 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
635 msg = "cannot make listening socket transparent";
636 err |= ERR_ALERT;
637 }
638 break;
639 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100640 }
641#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100642#ifdef SO_BINDTODEVICE
643 /* Note: this might fail if not CAP_NET_RAW */
644 if (listener->interface) {
645 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100646 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100647 msg = "cannot bind listener to device";
648 err |= ERR_WARN;
649 }
650 }
651#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400652#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100653 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400654 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200655 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
656 msg = "cannot set MSS";
657 err |= ERR_WARN;
658 }
659 }
660#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200661#if defined(TCP_DEFER_ACCEPT)
662 if (listener->options & LI_O_DEF_ACCEPT) {
663 /* defer accept by up to one second */
664 int accept_delay = 1;
665 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
666 msg = "cannot enable DEFER_ACCEPT";
667 err |= ERR_WARN;
668 }
669 }
670#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100671 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
672 err |= ERR_RETRYABLE | ERR_ALERT;
673 msg = "cannot bind socket";
674 goto tcp_close_return;
675 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100676
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100677 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100678 err |= ERR_RETRYABLE | ERR_ALERT;
679 msg = "cannot listen to socket";
680 goto tcp_close_return;
681 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100682
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400683#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200684 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900685 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200686#endif
687
Willy Tarreaue6b98942007-10-29 01:09:36 +0100688 /* the socket is ready */
689 listener->fd = fd;
690 listener->state = LI_LISTEN;
691
Willy Tarreaueabf3132008-08-29 23:36:51 +0200692 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaueb472682010-05-28 18:46:57 +0200693 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200694 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200695 fd_insert(fd);
696
Willy Tarreaue6b98942007-10-29 01:09:36 +0100697 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100698 if (msg && errlen) {
699 char pn[INET6_ADDRSTRLEN];
700
Willy Tarreau631f01c2011-09-05 00:36:48 +0200701 addr_to_str(&listener->addr, pn, sizeof(pn));
702 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100703 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100704 return err;
705
706 tcp_close_return:
707 close(fd);
708 goto tcp_return;
709}
710
711/* This function creates all TCP sockets bound to the protocol entry <proto>.
712 * It is intended to be used as the protocol's bind_all() function.
713 * The sockets will be registered but not added to any fd_set, in order not to
714 * loose them across the fork(). A call to enable_all_listeners() is needed
715 * to complete initialization. The return value is composed from ERR_*.
716 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200717static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100718{
719 struct listener *listener;
720 int err = ERR_NONE;
721
722 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200723 err |= tcp_bind_listener(listener, errmsg, errlen);
724 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100725 break;
726 }
727
728 return err;
729}
730
731/* Add listener to the list of tcpv4 listeners. The listener's state
732 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
733 * listeners is updated. This is the function to use to add a new listener.
734 */
735void tcpv4_add_listener(struct listener *listener)
736{
737 if (listener->state != LI_INIT)
738 return;
739 listener->state = LI_ASSIGNED;
740 listener->proto = &proto_tcpv4;
741 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
742 proto_tcpv4.nb_listeners++;
743}
744
745/* Add listener to the list of tcpv4 listeners. The listener's state
746 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
747 * listeners is updated. This is the function to use to add a new listener.
748 */
749void tcpv6_add_listener(struct listener *listener)
750{
751 if (listener->state != LI_INIT)
752 return;
753 listener->state = LI_ASSIGNED;
754 listener->proto = &proto_tcpv6;
755 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
756 proto_tcpv6.nb_listeners++;
757}
758
Willy Tarreauedcf6682008-11-30 23:15:34 +0100759/* This function performs the TCP request analysis on the current request. It
760 * returns 1 if the processing can continue on next analysers, or zero if it
761 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200762 * request. It relies on buffers flags, and updates s->req->analysers. The
763 * function may be called for frontend rules and backend rules. It only relies
764 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100765 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200766int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100767{
768 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200769 struct stksess *ts;
770 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100771 int partial;
772
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100773 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 +0100774 now_ms, __FUNCTION__,
775 s,
776 req,
777 req->rex, req->wex,
778 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100779 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100780 req->analysers);
781
Willy Tarreauedcf6682008-11-30 23:15:34 +0100782 /* We don't know whether we have enough data, so must proceed
783 * this way :
784 * - iterate through all rules in their declaration order
785 * - if one rule returns MISS, it means the inspect delay is
786 * not over yet, then return immediately, otherwise consider
787 * it as a non-match.
788 * - if one rule returns OK, then return OK
789 * - if one rule returns KO, then return KO
790 */
791
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200792 if ((req->flags & CF_SHUTR) || buffer_full(&req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200793 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200794 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100795 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200796 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100797
Willy Tarreaufb356202010-08-03 14:02:05 +0200798 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100799 int ret = ACL_PAT_PASS;
800
801 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200802 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100803 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200804 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100805 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200806 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
807 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100808 return 0;
809 }
810
811 ret = acl_pass(ret);
812 if (rule->cond->pol == ACL_COND_UNLESS)
813 ret = !ret;
814 }
815
816 if (ret) {
817 /* we have a matching rule. */
818 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200819 channel_abort(req);
820 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100821 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200822
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100823 s->be->be_counters.denied_req++;
824 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200825 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200826 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200827
Willy Tarreauedcf6682008-11-30 23:15:34 +0100828 if (!(s->flags & SN_ERR_MASK))
829 s->flags |= SN_ERR_PRXCOND;
830 if (!(s->flags & SN_FINST_MASK))
831 s->flags |= SN_FINST_R;
832 return 0;
833 }
Willy Tarreau56123282010-08-06 19:06:56 +0200834 else if (rule->action == TCP_ACT_TRK_SC1) {
835 if (!s->stkctr1_entry) {
836 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200837 * Also, note that right now we can only track SRC so we
838 * don't check how to get the key, but later we may need
839 * to consider rule->act_prm->trk_ctr.type.
840 */
841 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +0200842 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200843 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200844 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200845 if (s->fe != s->be)
846 s->flags |= SN_BE_TRACK_SC1;
847 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200848 }
849 }
Willy Tarreau56123282010-08-06 19:06:56 +0200850 else if (rule->action == TCP_ACT_TRK_SC2) {
851 if (!s->stkctr2_entry) {
852 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200853 * Also, note that right now we can only track SRC so we
854 * don't check how to get the key, but later we may need
855 * to consider rule->act_prm->trk_ctr.type.
856 */
857 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +0200858 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200859 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200860 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200861 if (s->fe != s->be)
862 s->flags |= SN_BE_TRACK_SC2;
863 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200864 }
865 }
866 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100867 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200868 break;
869 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100870 }
871 }
872
873 /* if we get there, it means we have no rule which matches, or
874 * we have an explicit accept, so we apply the default accept.
875 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200876 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100877 req->analyse_exp = TICK_ETERNITY;
878 return 1;
879}
880
Emeric Brun97679e72010-09-23 17:56:44 +0200881/* This function performs the TCP response analysis on the current response. It
882 * returns 1 if the processing can continue on next analysers, or zero if it
883 * needs more data, encounters an error, or wants to immediately abort the
884 * response. It relies on buffers flags, and updates s->rep->analysers. The
885 * function may be called for backend rules.
886 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200887int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200888{
889 struct tcp_rule *rule;
890 int partial;
891
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100892 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 +0200893 now_ms, __FUNCTION__,
894 s,
895 rep,
896 rep->rex, rep->wex,
897 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100898 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200899 rep->analysers);
900
901 /* We don't know whether we have enough data, so must proceed
902 * this way :
903 * - iterate through all rules in their declaration order
904 * - if one rule returns MISS, it means the inspect delay is
905 * not over yet, then return immediately, otherwise consider
906 * it as a non-match.
907 * - if one rule returns OK, then return OK
908 * - if one rule returns KO, then return KO
909 */
910
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200911 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200912 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200913 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200914 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200915
916 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
917 int ret = ACL_PAT_PASS;
918
919 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200920 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200921 if (ret == ACL_PAT_MISS) {
922 /* just set the analyser timeout once at the beginning of the response */
923 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
924 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
925 return 0;
926 }
927
928 ret = acl_pass(ret);
929 if (rule->cond->pol == ACL_COND_UNLESS)
930 ret = !ret;
931 }
932
933 if (ret) {
934 /* we have a matching rule. */
935 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200936 channel_abort(rep);
937 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200938 rep->analysers = 0;
939
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100940 s->be->be_counters.denied_resp++;
941 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200942 if (s->listener->counters)
943 s->listener->counters->denied_resp++;
944
945 if (!(s->flags & SN_ERR_MASK))
946 s->flags |= SN_ERR_PRXCOND;
947 if (!(s->flags & SN_FINST_MASK))
948 s->flags |= SN_FINST_D;
949 return 0;
950 }
951 else {
952 /* otherwise accept */
953 break;
954 }
955 }
956 }
957
958 /* if we get there, it means we have no rule which matches, or
959 * we have an explicit accept, so we apply the default accept.
960 */
961 rep->analysers &= ~an_bit;
962 rep->analyse_exp = TICK_ETERNITY;
963 return 1;
964}
965
966
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200967/* This function performs the TCP layer4 analysis on the current request. It
968 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
969 * matches or if no more rule matches. It can only use rules which don't need
970 * any data.
971 */
972int tcp_exec_req_rules(struct session *s)
973{
974 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200975 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200976 struct stktable *t = NULL;
977 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200978 int ret;
979
980 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
981 ret = ACL_PAT_PASS;
982
983 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200984 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200985 ret = acl_pass(ret);
986 if (rule->cond->pol == ACL_COND_UNLESS)
987 ret = !ret;
988 }
989
990 if (ret) {
991 /* we have a matching rule. */
992 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100993 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200994 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200995 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200996
997 if (!(s->flags & SN_ERR_MASK))
998 s->flags |= SN_ERR_PRXCOND;
999 if (!(s->flags & SN_FINST_MASK))
1000 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001001 result = 0;
1002 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001003 }
Willy Tarreau56123282010-08-06 19:06:56 +02001004 else if (rule->action == TCP_ACT_TRK_SC1) {
1005 if (!s->stkctr1_entry) {
1006 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001007 * Also, note that right now we can only track SRC so we
1008 * don't check how to get the key, but later we may need
1009 * to consider rule->act_prm->trk_ctr.type.
1010 */
1011 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +02001012 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001013 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001014 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001015 }
1016 }
Willy Tarreau56123282010-08-06 19:06:56 +02001017 else if (rule->action == TCP_ACT_TRK_SC2) {
1018 if (!s->stkctr2_entry) {
1019 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001020 * Also, note that right now we can only track SRC so we
1021 * don't check how to get the key, but later we may need
1022 * to consider rule->act_prm->trk_ctr.type.
1023 */
1024 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +02001025 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001026 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001027 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001028 }
1029 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001030 else {
1031 /* otherwise it's an accept */
1032 break;
1033 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001034 }
1035 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001036 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001037}
1038
Emeric Brun97679e72010-09-23 17:56:44 +02001039/* Parse a tcp-response rule. Return a negative value in case of failure */
1040static int tcp_parse_response_rule(char **args, int arg, int section_type,
1041 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001042 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001043{
1044 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001045 memprintf(err, "%s %s is only allowed in 'backend' sections",
1046 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001047 return -1;
1048 }
1049
1050 if (strcmp(args[arg], "accept") == 0) {
1051 arg++;
1052 rule->action = TCP_ACT_ACCEPT;
1053 }
1054 else if (strcmp(args[arg], "reject") == 0) {
1055 arg++;
1056 rule->action = TCP_ACT_REJECT;
1057 }
1058 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001059 memprintf(err,
1060 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1061 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001062 return -1;
1063 }
1064
1065 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001066 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1067 memprintf(err,
1068 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1069 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001070 return -1;
1071 }
1072 }
1073 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001074 memprintf(err,
1075 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001076 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1077 return -1;
1078 }
1079 return 0;
1080}
1081
1082
1083
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001084/* Parse a tcp-request rule. Return a negative value in case of failure */
1085static int tcp_parse_request_rule(char **args, int arg, int section_type,
1086 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001087 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001088{
1089 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001090 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1091 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001092 return -1;
1093 }
1094
1095 if (!strcmp(args[arg], "accept")) {
1096 arg++;
1097 rule->action = TCP_ACT_ACCEPT;
1098 }
1099 else if (!strcmp(args[arg], "reject")) {
1100 arg++;
1101 rule->action = TCP_ACT_REJECT;
1102 }
Willy Tarreau56123282010-08-06 19:06:56 +02001103 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001104 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001105 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001106
1107 arg++;
1108 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001109 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001110
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001111 if (ret < 0) { /* nb: warnings are not handled yet */
1112 memprintf(err,
1113 "'%s %s %s' : %s in %s '%s'",
1114 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1115 return ret;
1116 }
Willy Tarreau56123282010-08-06 19:06:56 +02001117 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001118 }
Willy Tarreau56123282010-08-06 19:06:56 +02001119 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001120 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001121 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001122
1123 arg++;
1124 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001125 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001126
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001127 if (ret < 0) { /* nb: warnings are not handled yet */
1128 memprintf(err,
1129 "'%s %s %s' : %s in %s '%s'",
1130 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1131 return ret;
1132 }
Willy Tarreau56123282010-08-06 19:06:56 +02001133 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001134 }
1135 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001136 memprintf(err,
1137 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1138 "or 'track-sc2' in %s '%s' (got '%s')",
1139 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001140 return -1;
1141 }
1142
1143 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001144 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1145 memprintf(err,
1146 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1147 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001148 return -1;
1149 }
1150 }
1151 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001152 memprintf(err,
1153 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001154 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1155 return -1;
1156 }
1157 return 0;
1158}
1159
Emeric Brun97679e72010-09-23 17:56:44 +02001160/* This function should be called to parse a line starting with the "tcp-response"
1161 * keyword.
1162 */
1163static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001164 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001165{
1166 const char *ptr = NULL;
1167 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001168 int warn = 0;
1169 int arg;
1170 struct tcp_rule *rule;
1171
1172 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001173 memprintf(err, "missing argument for '%s' in %s '%s'",
1174 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001175 return -1;
1176 }
1177
1178 if (strcmp(args[1], "inspect-delay") == 0) {
1179 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001180 memprintf(err, "%s %s is only allowed in 'backend' sections",
1181 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001182 return -1;
1183 }
1184
1185 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001186 memprintf(err,
1187 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1188 args[0], args[1], proxy_type_str(curpx), curpx->id);
1189 if (ptr)
1190 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001191 return -1;
1192 }
1193
1194 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001195 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1196 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001197 return 1;
1198 }
1199 curpx->tcp_rep.inspect_delay = val;
1200 return 0;
1201 }
1202
Simon Hormande072bd2011-06-24 15:11:37 +09001203 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001204 LIST_INIT(&rule->list);
1205 arg = 1;
1206
1207 if (strcmp(args[1], "content") == 0) {
1208 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001209 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001210 goto error;
1211
1212 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1213 struct acl *acl;
1214 const char *name;
1215
1216 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1217 name = acl ? acl->name : "(unknown)";
1218
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001219 memprintf(err,
1220 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1221 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001222 warn++;
1223 }
1224
1225 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1226 }
1227 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001228 memprintf(err,
1229 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1230 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001231 goto error;
1232 }
1233
1234 return warn;
1235 error:
1236 free(rule);
1237 return -1;
1238}
1239
1240
Willy Tarreaub6866442008-07-14 23:54:42 +02001241/* This function should be called to parse a line starting with the "tcp-request"
1242 * keyword.
1243 */
1244static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001245 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001246{
1247 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001248 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001249 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001250 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001251 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001252
1253 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001254 if (curpx == defpx)
1255 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1256 else
1257 memprintf(err, "missing argument for '%s' in %s '%s'",
1258 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001259 return -1;
1260 }
1261
1262 if (!strcmp(args[1], "inspect-delay")) {
1263 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001264 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1265 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001266 return -1;
1267 }
1268
Willy Tarreaub6866442008-07-14 23:54:42 +02001269 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001270 memprintf(err,
1271 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1272 args[0], args[1], proxy_type_str(curpx), curpx->id);
1273 if (ptr)
1274 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001275 return -1;
1276 }
1277
1278 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001279 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1280 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001281 return 1;
1282 }
1283 curpx->tcp_req.inspect_delay = val;
1284 return 0;
1285 }
1286
Simon Hormande072bd2011-06-24 15:11:37 +09001287 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001288 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001289 arg = 1;
1290
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001291 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001292 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001293 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001294 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001295
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001296 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001297 struct acl *acl;
1298 const char *name;
1299
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001300 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001301 name = acl ? acl->name : "(unknown)";
1302
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001303 memprintf(err,
1304 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1305 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001306 warn++;
1307 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001308 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001309 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001310 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001311 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001312
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001313 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001314 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1315 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001316 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001317 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001318
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001319 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001320 goto error;
1321
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001322 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1323 struct acl *acl;
1324 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001325
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001326 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1327 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001328
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001329 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001330 memprintf(err,
1331 "'%s %s' may not reference acl '%s' which makes use of "
1332 "payload in %s '%s'. Please use '%s content' for this.",
1333 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001334 goto error;
1335 }
1336 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001337 memprintf(err,
1338 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1339 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001340 warn++;
1341 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001342 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001343 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001344 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001345 if (curpx == defpx)
1346 memprintf(err,
1347 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1348 args[0], args[1]);
1349 else
1350 memprintf(err,
1351 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1352 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001353 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001354 }
1355
Willy Tarreau1a687942010-05-23 22:40:30 +02001356 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001357 error:
1358 free(rule);
1359 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001360}
1361
Willy Tarreau645513a2010-05-24 20:55:15 +02001362
1363/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001364/* All supported sample fetch functios must be declared here */
1365/************************************************************************/
1366
1367/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001368 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001369 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1370 * is a string (cookie name), other types will lead to undefined behaviour.
1371 */
1372int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001373smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001374 const struct arg *args, struct sample *smp)
1375{
1376 int bleft;
1377 const unsigned char *data;
1378
1379 if (!l4 || !l4->req)
1380 return 0;
1381
1382 smp->flags = 0;
1383 smp->type = SMP_T_CSTR;
1384
Willy Tarreau572bf902012-07-02 17:01:20 +02001385 bleft = l4->req->buf.i;
Willy Tarreau32389b72012-04-23 23:13:20 +02001386 if (bleft <= 11)
1387 goto too_short;
1388
Willy Tarreau572bf902012-07-02 17:01:20 +02001389 data = (const unsigned char *)l4->req->buf.p + 11;
Willy Tarreau32389b72012-04-23 23:13:20 +02001390 bleft -= 11;
1391
1392 if (bleft <= 7)
1393 goto too_short;
1394
1395 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1396 goto not_cookie;
1397
1398 data += 7;
1399 bleft -= 7;
1400
1401 while (bleft > 0 && *data == ' ') {
1402 data++;
1403 bleft--;
1404 }
1405
1406 if (args) {
1407
1408 if (bleft <= args->data.str.len)
1409 goto too_short;
1410
1411 if ((data[args->data.str.len] != '=') ||
1412 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1413 goto not_cookie;
1414
1415 data += args->data.str.len + 1;
1416 bleft -= args->data.str.len + 1;
1417 } else {
1418 while (bleft > 0 && *data != '=') {
1419 if (*data == '\r' || *data == '\n')
1420 goto not_cookie;
1421 data++;
1422 bleft--;
1423 }
1424
1425 if (bleft < 1)
1426 goto too_short;
1427
1428 if (*data != '=')
1429 goto not_cookie;
1430
1431 data++;
1432 bleft--;
1433 }
1434
1435 /* data points to cookie value */
1436 smp->data.str.str = (char *)data;
1437 smp->data.str.len = 0;
1438
1439 while (bleft > 0 && *data != '\r') {
1440 data++;
1441 bleft--;
1442 }
1443
1444 if (bleft < 2)
1445 goto too_short;
1446
1447 if (data[0] != '\r' || data[1] != '\n')
1448 goto not_cookie;
1449
1450 smp->data.str.len = (char *)data - smp->data.str.str;
1451 smp->flags = SMP_F_VOLATILE;
1452 return 1;
1453
1454 too_short:
1455 smp->flags = SMP_F_MAY_CHANGE;
1456 not_cookie:
1457 return 0;
1458}
1459
Willy Tarreau32389b72012-04-23 23:13:20 +02001460/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001461/* All supported ACL keywords must be declared here. */
1462/************************************************************************/
1463
Willy Tarreau32389b72012-04-23 23:13:20 +02001464/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1465static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001466acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001467 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001468{
1469 int ret;
1470
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001471 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001472
1473 if (smp->flags & SMP_F_MAY_CHANGE)
1474 return 0;
1475
1476 smp->flags = SMP_F_VOLATILE;
1477 smp->type = SMP_T_UINT;
1478 smp->data.uint = ret;
1479 return 1;
1480}
1481
1482
Willy Tarreau4a129812012-04-25 17:31:42 +02001483/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001484static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001485smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001486 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001487{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001488 switch (l4->si[0].conn.addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001489 case AF_INET:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001490 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn.addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001491 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001492 break;
1493 case AF_INET6:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001494 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn.addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001495 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001496 break;
1497 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001498 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001499 }
Emeric Brunf769f512010-10-22 17:14:01 +02001500
Willy Tarreau37406352012-04-23 16:16:37 +02001501 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001502 return 1;
1503}
1504
Willy Tarreaua5e37562011-12-16 17:06:15 +01001505/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001506static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001507smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001508 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001509{
Willy Tarreauf853c462012-04-23 18:53:56 +02001510 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02001511 if (!(smp->data.uint = get_host_port(&l4->si[0].conn.addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001512 return 0;
1513
Willy Tarreau37406352012-04-23 16:16:37 +02001514 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001515 return 1;
1516}
1517
Willy Tarreau4a129812012-04-25 17:31:42 +02001518/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001519static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001520smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001521 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001522{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001523 conn_get_to_addr(&l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001524
Willy Tarreau986a9d22012-08-30 21:11:38 +02001525 switch (l4->si[0].conn.addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001526 case AF_INET:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001527 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn.addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001528 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001529 break;
1530 case AF_INET6:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001531 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn.addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001532 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001533 break;
1534 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001535 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001536 }
Emeric Brunf769f512010-10-22 17:14:01 +02001537
Willy Tarreau37406352012-04-23 16:16:37 +02001538 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001539 return 1;
1540}
1541
Willy Tarreaua5e37562011-12-16 17:06:15 +01001542/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001543static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001544smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001545 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001546{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001547 conn_get_to_addr(&l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001548
Willy Tarreauf853c462012-04-23 18:53:56 +02001549 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02001550 if (!(smp->data.uint = get_host_port(&l4->si[0].conn.addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001551 return 0;
1552
Willy Tarreau37406352012-04-23 16:16:37 +02001553 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001554 return 1;
1555}
1556
1557static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001558smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1559 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001560{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001561 unsigned int len_offset = arg_p[0].data.uint;
1562 unsigned int len_size = arg_p[1].data.uint;
1563 unsigned int buf_offset;
1564 unsigned int buf_size = 0;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001565 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001566 int i;
1567
1568 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1569 /* by default buf offset == len offset + len size */
1570 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1571
1572 if (!l4)
1573 return 0;
1574
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001575 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001576
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001577 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001578 return 0;
1579
Willy Tarreau572bf902012-07-02 17:01:20 +02001580 if (len_offset + len_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001581 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001582
1583 for (i = 0; i < len_size; i++) {
Willy Tarreau572bf902012-07-02 17:01:20 +02001584 buf_size = (buf_size << 8) + ((unsigned char *)b->buf.p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001585 }
1586
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001587 /* buf offset may be implicit, absolute or relative */
1588 buf_offset = len_offset + len_size;
1589 if (arg_p[2].type == ARGT_UINT)
1590 buf_offset = arg_p[2].data.uint;
1591 else if (arg_p[2].type == ARGT_SINT)
1592 buf_offset += arg_p[2].data.sint;
1593
Willy Tarreau572bf902012-07-02 17:01:20 +02001594 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001595 /* will never match */
1596 smp->flags = 0;
1597 return 0;
1598 }
1599
Willy Tarreau572bf902012-07-02 17:01:20 +02001600 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001601 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001602
1603 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001604 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001605 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001606 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001607 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001608
1609 too_short:
1610 smp->flags = SMP_F_MAY_CHANGE;
1611 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001612}
1613
1614static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001615smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1616 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001617{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001618 unsigned int buf_offset = arg_p[0].data.uint;
1619 unsigned int buf_size = arg_p[1].data.uint;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001620 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001621
1622 if (!l4)
1623 return 0;
1624
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001625 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001626
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001627 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001628 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001629
Willy Tarreau572bf902012-07-02 17:01:20 +02001630 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001631 /* will never match */
1632 smp->flags = 0;
1633 return 0;
1634 }
Emericf2d7cae2010-11-05 18:13:50 +01001635
Willy Tarreau572bf902012-07-02 17:01:20 +02001636 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001637 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001638
1639 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001640 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001641 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001642 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001643 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001644
1645 too_short:
1646 smp->flags = SMP_F_MAY_CHANGE;
1647 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001648}
1649
Willy Tarreau21d68a62012-04-20 15:52:36 +02001650/* This function is used to validate the arguments passed to a "payload" fetch
1651 * keyword. This keyword expects two positive integers, with the second one
1652 * being strictly positive. It is assumed that the types are already the correct
1653 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1654 * filled with a pointer to an error message in case of error, that the caller
1655 * is responsible for freeing. The initial location must either be freeable or
1656 * NULL.
1657 */
1658static int val_payload(struct arg *arg, char **err_msg)
1659{
1660 if (!arg[1].data.uint) {
1661 if (err_msg)
1662 memprintf(err_msg, "payload length must be > 0");
1663 return 0;
1664 }
1665 return 1;
1666}
1667
1668/* This function is used to validate the arguments passed to a "payload_lv" fetch
1669 * keyword. This keyword allows two positive integers and an optional signed one,
1670 * with the second one being strictly positive and the third one being greater than
1671 * the opposite of the two others if negative. It is assumed that the types are
1672 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1673 * not NULL, it will be filled with a pointer to an error message in case of
1674 * error, that the caller is responsible for freeing. The initial location must
1675 * either be freeable or NULL.
1676 */
1677static int val_payload_lv(struct arg *arg, char **err_msg)
1678{
1679 if (!arg[1].data.uint) {
1680 if (err_msg)
1681 memprintf(err_msg, "payload length must be > 0");
1682 return 0;
1683 }
1684
1685 if (arg[2].type == ARGT_SINT &&
1686 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1687 if (err_msg)
1688 memprintf(err_msg, "payload offset too negative");
1689 return 0;
1690 }
1691 return 1;
1692}
1693
Willy Tarreaub6866442008-07-14 23:54:42 +02001694static struct cfg_kw_list cfg_kws = {{ },{
1695 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001696 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001697 { 0, NULL, NULL },
1698}};
1699
Willy Tarreau61612d42012-04-19 18:42:05 +02001700/* Note: must not be declared <const> as its list will be overwritten.
1701 * Please take care of keeping this list alphabetically sorted.
1702 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001703static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001704 { "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 +02001705 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001706 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1707 { "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 +02001708 { "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 +02001709 { "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 +02001710 { "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 +02001711 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001712 { NULL, NULL, NULL, NULL },
1713}};
1714
Willy Tarreau4a129812012-04-25 17:31:42 +02001715/* Note: must not be declared <const> as its list will be overwritten.
1716 * Note: fetches that may return multiple types must be declared as the lowest
1717 * common denominator, the type that can be casted into all other ones. For
1718 * instance v4/v6 must be declared v4.
1719 */
Willy Tarreau12785782012-04-27 21:37:17 +02001720static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001721 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001722 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001723 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001724 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1725 { "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 +02001726 { "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 +02001727 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001728 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001729}};
1730
Willy Tarreaue6b98942007-10-29 01:09:36 +01001731__attribute__((constructor))
1732static void __tcp_protocol_init(void)
1733{
1734 protocol_register(&proto_tcpv4);
1735 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001736 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001737 cfg_register_keywords(&cfg_kws);
1738 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001739}
1740
1741
1742/*
1743 * Local variables:
1744 * c-indent-level: 8
1745 * c-basic-offset: 8
1746 * End:
1747 */