blob: f819147820e637fdd770f3c4a4acbd967b3ac46e [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
Willy Tarreau184636e2012-09-06 14:04:41 +0200462 if (conn_data_init(conn) < 0) {
463 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200464 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200465 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200466
Willy Tarreau14f8e862012-08-30 22:23:13 +0200467 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200468 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200469
Willy Tarreau9650f372009-08-16 14:02:45 +0200470 return SN_ERR_NONE; /* connection is OK */
471}
472
473
Willy Tarreau59b94792012-05-11 16:16:40 +0200474/*
475 * Retrieves the source address for the socket <fd>, with <dir> indicating
476 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
477 * success, -1 in case of error. The socket's source address is stored in
478 * <sa> for <salen> bytes.
479 */
480int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
481{
482 if (dir)
483 return getsockname(fd, sa, &salen);
484 else
485 return getpeername(fd, sa, &salen);
486}
487
488
489/*
490 * Retrieves the original destination address for the socket <fd>, with <dir>
491 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
492 * listener, if the original destination address was translated, the original
493 * address is retrieved. It returns 0 in case of success, -1 in case of error.
494 * The socket's source address is stored in <sa> for <salen> bytes.
495 */
496int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
497{
498 if (dir)
499 return getpeername(fd, sa, &salen);
500#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
501 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
502 return 0;
503#endif
504 else
505 return getsockname(fd, sa, &salen);
506}
507
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200508/* This is the callback which is set when a connection establishment is pending
509 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200510 * once the connection is established. It updates the FD polling status. It
511 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
512 * it returns non-zero and removes itself from the connection's flags (the bit is
513 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200514 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200515int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200516{
Willy Tarreau239d7182012-07-23 18:53:03 +0200517 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200518
Willy Tarreau80184712012-07-06 14:54:49 +0200519 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200520 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200521
Willy Tarreau80184712012-07-06 14:54:49 +0200522 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200523 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200524
Willy Tarreau2da156f2012-07-23 15:07:23 +0200525 /* stop here if we reached the end of data */
526 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
527 goto out_error;
528
Willy Tarreau2c6be842012-07-06 17:12:34 +0200529 /* We have no data to send to check the connection, and
530 * getsockopt() will not inform us whether the connection
531 * is still pending. So we'll reuse connect() to check the
532 * state of the socket. This has the advantage of giving us
533 * the following info :
534 * - error
535 * - connecting (EALREADY, EINPROGRESS)
536 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200537 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200538 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200539 if (errno == EALREADY || errno == EINPROGRESS) {
540 conn_sock_stop_recv(conn);
541 conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200542 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200543 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200544
Willy Tarreau2c6be842012-07-06 17:12:34 +0200545 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200546 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200547
Willy Tarreau2c6be842012-07-06 17:12:34 +0200548 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200549 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200550
Willy Tarreau076be252012-07-06 16:02:29 +0200551 /* The FD is ready now, we'll mark the connection as complete and
552 * forward the event to the data layer which will update the stream
553 * interface flags.
Willy Tarreaua190d592012-05-20 18:35:19 +0200554 */
Willy Tarreau80184712012-07-06 14:54:49 +0200555 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200556 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200557
558 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200559 /* Write error on the file descriptor. Report it to the connection
560 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200561 */
562
Willy Tarreau80184712012-07-06 14:54:49 +0200563 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200564 conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200565 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200566}
567
Willy Tarreau59b94792012-05-11 16:16:40 +0200568
Willy Tarreaue6b98942007-10-29 01:09:36 +0100569/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
570 * an error message in <err> if the message is at most <errlen> bytes long
571 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
572 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
573 * was alright and that no message was returned. ERR_RETRYABLE means that an
574 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700575 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100576 * the meaning of the error, but just indicate that a message is present which
577 * should be displayed with the respective level. Last, ERR_ABORT indicates
578 * that it's pointless to try to start other listeners. No error message is
579 * returned if errlen is NULL.
580 */
581int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
582{
583 __label__ tcp_return, tcp_close_return;
584 int fd, err;
585 const char *msg = NULL;
586
587 /* ensure we never return garbage */
588 if (errmsg && errlen)
589 *errmsg = 0;
590
591 if (listener->state != LI_ASSIGNED)
592 return ERR_NONE; /* already bound */
593
594 err = ERR_NONE;
595
596 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
597 err |= ERR_RETRYABLE | ERR_ALERT;
598 msg = "cannot create listening socket";
599 goto tcp_return;
600 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100601
Willy Tarreaue6b98942007-10-29 01:09:36 +0100602 if (fd >= global.maxsock) {
603 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
604 msg = "not enough free sockets (raise '-n' parameter)";
605 goto tcp_close_return;
606 }
607
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200608 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100609 err |= ERR_FATAL | ERR_ALERT;
610 msg = "cannot make socket non-blocking";
611 goto tcp_close_return;
612 }
613
Simon Hormande072bd2011-06-24 15:11:37 +0900614 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100615 /* not fatal but should be reported */
616 msg = "cannot do so_reuseaddr";
617 err |= ERR_ALERT;
618 }
619
620 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900621 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100622
Willy Tarreaue6b98942007-10-29 01:09:36 +0100623#ifdef SO_REUSEPORT
624 /* OpenBSD supports this. As it's present in old libc versions of Linux,
625 * it might return an error that we will silently ignore.
626 */
Simon Hormande072bd2011-06-24 15:11:37 +0900627 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100628#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100629#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200630 if (listener->options & LI_O_FOREIGN) {
631 switch (listener->addr.ss_family) {
632 case AF_INET:
633 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
634 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
635 msg = "cannot make listening socket transparent";
636 err |= ERR_ALERT;
637 }
638 break;
639 case AF_INET6:
640 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
641 msg = "cannot make listening socket transparent";
642 err |= ERR_ALERT;
643 }
644 break;
645 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100646 }
647#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100648#ifdef SO_BINDTODEVICE
649 /* Note: this might fail if not CAP_NET_RAW */
650 if (listener->interface) {
651 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100652 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100653 msg = "cannot bind listener to device";
654 err |= ERR_WARN;
655 }
656 }
657#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400658#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100659 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400660 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200661 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
662 msg = "cannot set MSS";
663 err |= ERR_WARN;
664 }
665 }
666#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200667#if defined(TCP_DEFER_ACCEPT)
668 if (listener->options & LI_O_DEF_ACCEPT) {
669 /* defer accept by up to one second */
670 int accept_delay = 1;
671 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
672 msg = "cannot enable DEFER_ACCEPT";
673 err |= ERR_WARN;
674 }
675 }
676#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100677 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
678 err |= ERR_RETRYABLE | ERR_ALERT;
679 msg = "cannot bind socket";
680 goto tcp_close_return;
681 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100682
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100683 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100684 err |= ERR_RETRYABLE | ERR_ALERT;
685 msg = "cannot listen to socket";
686 goto tcp_close_return;
687 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100688
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400689#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200690 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900691 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200692#endif
693
Willy Tarreaue6b98942007-10-29 01:09:36 +0100694 /* the socket is ready */
695 listener->fd = fd;
696 listener->state = LI_LISTEN;
697
Willy Tarreaueabf3132008-08-29 23:36:51 +0200698 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200699 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200700 fd_insert(fd);
701
Willy Tarreaue6b98942007-10-29 01:09:36 +0100702 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100703 if (msg && errlen) {
704 char pn[INET6_ADDRSTRLEN];
705
Willy Tarreau631f01c2011-09-05 00:36:48 +0200706 addr_to_str(&listener->addr, pn, sizeof(pn));
707 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100708 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100709 return err;
710
711 tcp_close_return:
712 close(fd);
713 goto tcp_return;
714}
715
716/* This function creates all TCP sockets bound to the protocol entry <proto>.
717 * It is intended to be used as the protocol's bind_all() function.
718 * The sockets will be registered but not added to any fd_set, in order not to
719 * loose them across the fork(). A call to enable_all_listeners() is needed
720 * to complete initialization. The return value is composed from ERR_*.
721 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200722static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100723{
724 struct listener *listener;
725 int err = ERR_NONE;
726
727 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200728 err |= tcp_bind_listener(listener, errmsg, errlen);
729 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100730 break;
731 }
732
733 return err;
734}
735
736/* Add listener to the list of tcpv4 listeners. The listener's state
737 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
738 * listeners is updated. This is the function to use to add a new listener.
739 */
740void tcpv4_add_listener(struct listener *listener)
741{
742 if (listener->state != LI_INIT)
743 return;
744 listener->state = LI_ASSIGNED;
745 listener->proto = &proto_tcpv4;
746 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
747 proto_tcpv4.nb_listeners++;
748}
749
750/* Add listener to the list of tcpv4 listeners. The listener's state
751 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
752 * listeners is updated. This is the function to use to add a new listener.
753 */
754void tcpv6_add_listener(struct listener *listener)
755{
756 if (listener->state != LI_INIT)
757 return;
758 listener->state = LI_ASSIGNED;
759 listener->proto = &proto_tcpv6;
760 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
761 proto_tcpv6.nb_listeners++;
762}
763
Willy Tarreauedcf6682008-11-30 23:15:34 +0100764/* This function performs the TCP request analysis on the current request. It
765 * returns 1 if the processing can continue on next analysers, or zero if it
766 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200767 * request. It relies on buffers flags, and updates s->req->analysers. The
768 * function may be called for frontend rules and backend rules. It only relies
769 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100770 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200771int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100772{
773 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200774 struct stksess *ts;
775 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100776 int partial;
777
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100778 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 +0100779 now_ms, __FUNCTION__,
780 s,
781 req,
782 req->rex, req->wex,
783 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100784 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100785 req->analysers);
786
Willy Tarreauedcf6682008-11-30 23:15:34 +0100787 /* We don't know whether we have enough data, so must proceed
788 * this way :
789 * - iterate through all rules in their declaration order
790 * - if one rule returns MISS, it means the inspect delay is
791 * not over yet, then return immediately, otherwise consider
792 * it as a non-match.
793 * - if one rule returns OK, then return OK
794 * - if one rule returns KO, then return KO
795 */
796
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200797 if ((req->flags & CF_SHUTR) || buffer_full(&req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200798 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200799 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100800 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200801 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100802
Willy Tarreaufb356202010-08-03 14:02:05 +0200803 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100804 int ret = ACL_PAT_PASS;
805
806 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200807 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100808 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200809 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100810 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200811 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
812 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100813 return 0;
814 }
815
816 ret = acl_pass(ret);
817 if (rule->cond->pol == ACL_COND_UNLESS)
818 ret = !ret;
819 }
820
821 if (ret) {
822 /* we have a matching rule. */
823 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200824 channel_abort(req);
825 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100826 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200827
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100828 s->be->be_counters.denied_req++;
829 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200830 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200831 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200832
Willy Tarreauedcf6682008-11-30 23:15:34 +0100833 if (!(s->flags & SN_ERR_MASK))
834 s->flags |= SN_ERR_PRXCOND;
835 if (!(s->flags & SN_FINST_MASK))
836 s->flags |= SN_FINST_R;
837 return 0;
838 }
Willy Tarreau56123282010-08-06 19:06:56 +0200839 else if (rule->action == TCP_ACT_TRK_SC1) {
840 if (!s->stkctr1_entry) {
841 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200842 * Also, note that right now we can only track SRC so we
843 * don't check how to get the key, but later we may need
844 * to consider rule->act_prm->trk_ctr.type.
845 */
846 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +0200847 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200848 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200849 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200850 if (s->fe != s->be)
851 s->flags |= SN_BE_TRACK_SC1;
852 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200853 }
854 }
Willy Tarreau56123282010-08-06 19:06:56 +0200855 else if (rule->action == TCP_ACT_TRK_SC2) {
856 if (!s->stkctr2_entry) {
857 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200858 * Also, note that right now we can only track SRC so we
859 * don't check how to get the key, but later we may need
860 * to consider rule->act_prm->trk_ctr.type.
861 */
862 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +0200863 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200864 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200865 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200866 if (s->fe != s->be)
867 s->flags |= SN_BE_TRACK_SC2;
868 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200869 }
870 }
871 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100872 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200873 break;
874 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100875 }
876 }
877
878 /* if we get there, it means we have no rule which matches, or
879 * we have an explicit accept, so we apply the default accept.
880 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200881 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100882 req->analyse_exp = TICK_ETERNITY;
883 return 1;
884}
885
Emeric Brun97679e72010-09-23 17:56:44 +0200886/* This function performs the TCP response analysis on the current response. It
887 * returns 1 if the processing can continue on next analysers, or zero if it
888 * needs more data, encounters an error, or wants to immediately abort the
889 * response. It relies on buffers flags, and updates s->rep->analysers. The
890 * function may be called for backend rules.
891 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200892int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200893{
894 struct tcp_rule *rule;
895 int partial;
896
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100897 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 +0200898 now_ms, __FUNCTION__,
899 s,
900 rep,
901 rep->rex, rep->wex,
902 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100903 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200904 rep->analysers);
905
906 /* We don't know whether we have enough data, so must proceed
907 * this way :
908 * - iterate through all rules in their declaration order
909 * - if one rule returns MISS, it means the inspect delay is
910 * not over yet, then return immediately, otherwise consider
911 * it as a non-match.
912 * - if one rule returns OK, then return OK
913 * - if one rule returns KO, then return KO
914 */
915
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200916 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200917 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200918 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200919 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200920
921 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
922 int ret = ACL_PAT_PASS;
923
924 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200925 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200926 if (ret == ACL_PAT_MISS) {
927 /* just set the analyser timeout once at the beginning of the response */
928 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
929 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
930 return 0;
931 }
932
933 ret = acl_pass(ret);
934 if (rule->cond->pol == ACL_COND_UNLESS)
935 ret = !ret;
936 }
937
938 if (ret) {
939 /* we have a matching rule. */
940 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200941 channel_abort(rep);
942 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200943 rep->analysers = 0;
944
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100945 s->be->be_counters.denied_resp++;
946 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200947 if (s->listener->counters)
948 s->listener->counters->denied_resp++;
949
950 if (!(s->flags & SN_ERR_MASK))
951 s->flags |= SN_ERR_PRXCOND;
952 if (!(s->flags & SN_FINST_MASK))
953 s->flags |= SN_FINST_D;
954 return 0;
955 }
956 else {
957 /* otherwise accept */
958 break;
959 }
960 }
961 }
962
963 /* if we get there, it means we have no rule which matches, or
964 * we have an explicit accept, so we apply the default accept.
965 */
966 rep->analysers &= ~an_bit;
967 rep->analyse_exp = TICK_ETERNITY;
968 return 1;
969}
970
971
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200972/* This function performs the TCP layer4 analysis on the current request. It
973 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
974 * matches or if no more rule matches. It can only use rules which don't need
975 * any data.
976 */
977int tcp_exec_req_rules(struct session *s)
978{
979 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200980 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200981 struct stktable *t = NULL;
982 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200983 int ret;
984
985 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
986 ret = ACL_PAT_PASS;
987
988 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200989 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200990 ret = acl_pass(ret);
991 if (rule->cond->pol == ACL_COND_UNLESS)
992 ret = !ret;
993 }
994
995 if (ret) {
996 /* we have a matching rule. */
997 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100998 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200999 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001000 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001001
1002 if (!(s->flags & SN_ERR_MASK))
1003 s->flags |= SN_ERR_PRXCOND;
1004 if (!(s->flags & SN_FINST_MASK))
1005 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001006 result = 0;
1007 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001008 }
Willy Tarreau56123282010-08-06 19:06:56 +02001009 else if (rule->action == TCP_ACT_TRK_SC1) {
1010 if (!s->stkctr1_entry) {
1011 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001012 * Also, note that right now we can only track SRC so we
1013 * don't check how to get the key, but later we may need
1014 * to consider rule->act_prm->trk_ctr.type.
1015 */
1016 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +02001017 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001018 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001019 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001020 }
1021 }
Willy Tarreau56123282010-08-06 19:06:56 +02001022 else if (rule->action == TCP_ACT_TRK_SC2) {
1023 if (!s->stkctr2_entry) {
1024 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001025 * Also, note that right now we can only track SRC so we
1026 * don't check how to get the key, but later we may need
1027 * to consider rule->act_prm->trk_ctr.type.
1028 */
1029 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +02001030 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001031 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001032 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001033 }
1034 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001035 else {
1036 /* otherwise it's an accept */
1037 break;
1038 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001039 }
1040 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001041 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001042}
1043
Emeric Brun97679e72010-09-23 17:56:44 +02001044/* Parse a tcp-response rule. Return a negative value in case of failure */
1045static int tcp_parse_response_rule(char **args, int arg, int section_type,
1046 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001047 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001048{
1049 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001050 memprintf(err, "%s %s is only allowed in 'backend' sections",
1051 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001052 return -1;
1053 }
1054
1055 if (strcmp(args[arg], "accept") == 0) {
1056 arg++;
1057 rule->action = TCP_ACT_ACCEPT;
1058 }
1059 else if (strcmp(args[arg], "reject") == 0) {
1060 arg++;
1061 rule->action = TCP_ACT_REJECT;
1062 }
1063 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001064 memprintf(err,
1065 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1066 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001067 return -1;
1068 }
1069
1070 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001071 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1072 memprintf(err,
1073 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1074 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001075 return -1;
1076 }
1077 }
1078 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001079 memprintf(err,
1080 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001081 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1082 return -1;
1083 }
1084 return 0;
1085}
1086
1087
1088
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001089/* Parse a tcp-request rule. Return a negative value in case of failure */
1090static int tcp_parse_request_rule(char **args, int arg, int section_type,
1091 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001092 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001093{
1094 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001095 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1096 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001097 return -1;
1098 }
1099
1100 if (!strcmp(args[arg], "accept")) {
1101 arg++;
1102 rule->action = TCP_ACT_ACCEPT;
1103 }
1104 else if (!strcmp(args[arg], "reject")) {
1105 arg++;
1106 rule->action = TCP_ACT_REJECT;
1107 }
Willy Tarreau56123282010-08-06 19:06:56 +02001108 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001109 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001110 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001111
1112 arg++;
1113 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001114 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001115
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001116 if (ret < 0) { /* nb: warnings are not handled yet */
1117 memprintf(err,
1118 "'%s %s %s' : %s in %s '%s'",
1119 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1120 return ret;
1121 }
Willy Tarreau56123282010-08-06 19:06:56 +02001122 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001123 }
Willy Tarreau56123282010-08-06 19:06:56 +02001124 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001125 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001126 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001127
1128 arg++;
1129 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001130 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001131
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001132 if (ret < 0) { /* nb: warnings are not handled yet */
1133 memprintf(err,
1134 "'%s %s %s' : %s in %s '%s'",
1135 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1136 return ret;
1137 }
Willy Tarreau56123282010-08-06 19:06:56 +02001138 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001139 }
1140 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001141 memprintf(err,
1142 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1143 "or 'track-sc2' in %s '%s' (got '%s')",
1144 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001145 return -1;
1146 }
1147
1148 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001149 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1150 memprintf(err,
1151 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1152 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001153 return -1;
1154 }
1155 }
1156 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001157 memprintf(err,
1158 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001159 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1160 return -1;
1161 }
1162 return 0;
1163}
1164
Emeric Brun97679e72010-09-23 17:56:44 +02001165/* This function should be called to parse a line starting with the "tcp-response"
1166 * keyword.
1167 */
1168static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001169 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001170{
1171 const char *ptr = NULL;
1172 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001173 int warn = 0;
1174 int arg;
1175 struct tcp_rule *rule;
1176
1177 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001178 memprintf(err, "missing argument for '%s' in %s '%s'",
1179 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001180 return -1;
1181 }
1182
1183 if (strcmp(args[1], "inspect-delay") == 0) {
1184 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001185 memprintf(err, "%s %s is only allowed in 'backend' sections",
1186 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001187 return -1;
1188 }
1189
1190 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001191 memprintf(err,
1192 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1193 args[0], args[1], proxy_type_str(curpx), curpx->id);
1194 if (ptr)
1195 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001196 return -1;
1197 }
1198
1199 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001200 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1201 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001202 return 1;
1203 }
1204 curpx->tcp_rep.inspect_delay = val;
1205 return 0;
1206 }
1207
Simon Hormande072bd2011-06-24 15:11:37 +09001208 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001209 LIST_INIT(&rule->list);
1210 arg = 1;
1211
1212 if (strcmp(args[1], "content") == 0) {
1213 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001214 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001215 goto error;
1216
1217 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1218 struct acl *acl;
1219 const char *name;
1220
1221 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1222 name = acl ? acl->name : "(unknown)";
1223
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001224 memprintf(err,
1225 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1226 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001227 warn++;
1228 }
1229
1230 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1231 }
1232 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001233 memprintf(err,
1234 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1235 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001236 goto error;
1237 }
1238
1239 return warn;
1240 error:
1241 free(rule);
1242 return -1;
1243}
1244
1245
Willy Tarreaub6866442008-07-14 23:54:42 +02001246/* This function should be called to parse a line starting with the "tcp-request"
1247 * keyword.
1248 */
1249static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001250 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001251{
1252 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001253 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001254 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001255 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001256 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001257
1258 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001259 if (curpx == defpx)
1260 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1261 else
1262 memprintf(err, "missing argument for '%s' in %s '%s'",
1263 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001264 return -1;
1265 }
1266
1267 if (!strcmp(args[1], "inspect-delay")) {
1268 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001269 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1270 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001271 return -1;
1272 }
1273
Willy Tarreaub6866442008-07-14 23:54:42 +02001274 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001275 memprintf(err,
1276 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1277 args[0], args[1], proxy_type_str(curpx), curpx->id);
1278 if (ptr)
1279 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001280 return -1;
1281 }
1282
1283 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001284 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1285 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001286 return 1;
1287 }
1288 curpx->tcp_req.inspect_delay = val;
1289 return 0;
1290 }
1291
Simon Hormande072bd2011-06-24 15:11:37 +09001292 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001293 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001294 arg = 1;
1295
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001296 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001297 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001298 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001299 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001300
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001301 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001302 struct acl *acl;
1303 const char *name;
1304
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001305 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001306 name = acl ? acl->name : "(unknown)";
1307
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001308 memprintf(err,
1309 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1310 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001311 warn++;
1312 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001313 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001314 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001315 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001316 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001317
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001318 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001319 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1320 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001321 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001322 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001323
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001324 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001325 goto error;
1326
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001327 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1328 struct acl *acl;
1329 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001330
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001331 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1332 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001333
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001334 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001335 memprintf(err,
1336 "'%s %s' may not reference acl '%s' which makes use of "
1337 "payload in %s '%s'. Please use '%s content' for this.",
1338 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001339 goto error;
1340 }
1341 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001342 memprintf(err,
1343 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1344 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001345 warn++;
1346 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001347 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001348 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001349 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001350 if (curpx == defpx)
1351 memprintf(err,
1352 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1353 args[0], args[1]);
1354 else
1355 memprintf(err,
1356 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1357 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001358 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001359 }
1360
Willy Tarreau1a687942010-05-23 22:40:30 +02001361 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001362 error:
1363 free(rule);
1364 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001365}
1366
Willy Tarreau645513a2010-05-24 20:55:15 +02001367
1368/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001369/* All supported sample fetch functios must be declared here */
1370/************************************************************************/
1371
1372/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001373 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001374 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1375 * is a string (cookie name), other types will lead to undefined behaviour.
1376 */
1377int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001378smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001379 const struct arg *args, struct sample *smp)
1380{
1381 int bleft;
1382 const unsigned char *data;
1383
1384 if (!l4 || !l4->req)
1385 return 0;
1386
1387 smp->flags = 0;
1388 smp->type = SMP_T_CSTR;
1389
Willy Tarreau572bf902012-07-02 17:01:20 +02001390 bleft = l4->req->buf.i;
Willy Tarreau32389b72012-04-23 23:13:20 +02001391 if (bleft <= 11)
1392 goto too_short;
1393
Willy Tarreau572bf902012-07-02 17:01:20 +02001394 data = (const unsigned char *)l4->req->buf.p + 11;
Willy Tarreau32389b72012-04-23 23:13:20 +02001395 bleft -= 11;
1396
1397 if (bleft <= 7)
1398 goto too_short;
1399
1400 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1401 goto not_cookie;
1402
1403 data += 7;
1404 bleft -= 7;
1405
1406 while (bleft > 0 && *data == ' ') {
1407 data++;
1408 bleft--;
1409 }
1410
1411 if (args) {
1412
1413 if (bleft <= args->data.str.len)
1414 goto too_short;
1415
1416 if ((data[args->data.str.len] != '=') ||
1417 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1418 goto not_cookie;
1419
1420 data += args->data.str.len + 1;
1421 bleft -= args->data.str.len + 1;
1422 } else {
1423 while (bleft > 0 && *data != '=') {
1424 if (*data == '\r' || *data == '\n')
1425 goto not_cookie;
1426 data++;
1427 bleft--;
1428 }
1429
1430 if (bleft < 1)
1431 goto too_short;
1432
1433 if (*data != '=')
1434 goto not_cookie;
1435
1436 data++;
1437 bleft--;
1438 }
1439
1440 /* data points to cookie value */
1441 smp->data.str.str = (char *)data;
1442 smp->data.str.len = 0;
1443
1444 while (bleft > 0 && *data != '\r') {
1445 data++;
1446 bleft--;
1447 }
1448
1449 if (bleft < 2)
1450 goto too_short;
1451
1452 if (data[0] != '\r' || data[1] != '\n')
1453 goto not_cookie;
1454
1455 smp->data.str.len = (char *)data - smp->data.str.str;
1456 smp->flags = SMP_F_VOLATILE;
1457 return 1;
1458
1459 too_short:
1460 smp->flags = SMP_F_MAY_CHANGE;
1461 not_cookie:
1462 return 0;
1463}
1464
Willy Tarreau32389b72012-04-23 23:13:20 +02001465/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001466/* All supported ACL keywords must be declared here. */
1467/************************************************************************/
1468
Willy Tarreau32389b72012-04-23 23:13:20 +02001469/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1470static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001471acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001472 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001473{
1474 int ret;
1475
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001476 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001477
1478 if (smp->flags & SMP_F_MAY_CHANGE)
1479 return 0;
1480
1481 smp->flags = SMP_F_VOLATILE;
1482 smp->type = SMP_T_UINT;
1483 smp->data.uint = ret;
1484 return 1;
1485}
1486
1487
Willy Tarreau4a129812012-04-25 17:31:42 +02001488/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001489static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001490smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001491 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001492{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001493 switch (l4->si[0].conn.addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001494 case AF_INET:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001495 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn.addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001496 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001497 break;
1498 case AF_INET6:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001499 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn.addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001500 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001501 break;
1502 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001503 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001504 }
Emeric Brunf769f512010-10-22 17:14:01 +02001505
Willy Tarreau37406352012-04-23 16:16:37 +02001506 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001507 return 1;
1508}
1509
Willy Tarreaua5e37562011-12-16 17:06:15 +01001510/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001511static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001512smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001513 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001514{
Willy Tarreauf853c462012-04-23 18:53:56 +02001515 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02001516 if (!(smp->data.uint = get_host_port(&l4->si[0].conn.addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001517 return 0;
1518
Willy Tarreau37406352012-04-23 16:16:37 +02001519 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001520 return 1;
1521}
1522
Willy Tarreau4a129812012-04-25 17:31:42 +02001523/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001524static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001525smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001526 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001527{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001528 conn_get_to_addr(&l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001529
Willy Tarreau986a9d22012-08-30 21:11:38 +02001530 switch (l4->si[0].conn.addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001531 case AF_INET:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001532 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn.addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001533 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001534 break;
1535 case AF_INET6:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001536 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn.addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001537 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001538 break;
1539 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001540 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001541 }
Emeric Brunf769f512010-10-22 17:14:01 +02001542
Willy Tarreau37406352012-04-23 16:16:37 +02001543 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001544 return 1;
1545}
1546
Willy Tarreaua5e37562011-12-16 17:06:15 +01001547/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001548static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001549smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001550 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001551{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001552 conn_get_to_addr(&l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001553
Willy Tarreauf853c462012-04-23 18:53:56 +02001554 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02001555 if (!(smp->data.uint = get_host_port(&l4->si[0].conn.addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001556 return 0;
1557
Willy Tarreau37406352012-04-23 16:16:37 +02001558 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001559 return 1;
1560}
1561
1562static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001563smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1564 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001565{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001566 unsigned int len_offset = arg_p[0].data.uint;
1567 unsigned int len_size = arg_p[1].data.uint;
1568 unsigned int buf_offset;
1569 unsigned int buf_size = 0;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001570 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001571 int i;
1572
1573 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1574 /* by default buf offset == len offset + len size */
1575 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1576
1577 if (!l4)
1578 return 0;
1579
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001580 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001581
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001582 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001583 return 0;
1584
Willy Tarreau572bf902012-07-02 17:01:20 +02001585 if (len_offset + len_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001586 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001587
1588 for (i = 0; i < len_size; i++) {
Willy Tarreau572bf902012-07-02 17:01:20 +02001589 buf_size = (buf_size << 8) + ((unsigned char *)b->buf.p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001590 }
1591
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001592 /* buf offset may be implicit, absolute or relative */
1593 buf_offset = len_offset + len_size;
1594 if (arg_p[2].type == ARGT_UINT)
1595 buf_offset = arg_p[2].data.uint;
1596 else if (arg_p[2].type == ARGT_SINT)
1597 buf_offset += arg_p[2].data.sint;
1598
Willy Tarreau572bf902012-07-02 17:01:20 +02001599 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001600 /* will never match */
1601 smp->flags = 0;
1602 return 0;
1603 }
1604
Willy Tarreau572bf902012-07-02 17:01:20 +02001605 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001606 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001607
1608 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001609 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001610 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001611 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001612 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001613
1614 too_short:
1615 smp->flags = SMP_F_MAY_CHANGE;
1616 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001617}
1618
1619static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001620smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1621 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001622{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001623 unsigned int buf_offset = arg_p[0].data.uint;
1624 unsigned int buf_size = arg_p[1].data.uint;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001625 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001626
1627 if (!l4)
1628 return 0;
1629
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001630 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001631
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001632 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001633 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001634
Willy Tarreau572bf902012-07-02 17:01:20 +02001635 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001636 /* will never match */
1637 smp->flags = 0;
1638 return 0;
1639 }
Emericf2d7cae2010-11-05 18:13:50 +01001640
Willy Tarreau572bf902012-07-02 17:01:20 +02001641 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001642 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001643
1644 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001645 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001646 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001647 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001648 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001649
1650 too_short:
1651 smp->flags = SMP_F_MAY_CHANGE;
1652 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001653}
1654
Willy Tarreau21d68a62012-04-20 15:52:36 +02001655/* This function is used to validate the arguments passed to a "payload" fetch
1656 * keyword. This keyword expects two positive integers, with the second one
1657 * being strictly positive. It is assumed that the types are already the correct
1658 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1659 * filled with a pointer to an error message in case of error, that the caller
1660 * is responsible for freeing. The initial location must either be freeable or
1661 * NULL.
1662 */
1663static int val_payload(struct arg *arg, char **err_msg)
1664{
1665 if (!arg[1].data.uint) {
1666 if (err_msg)
1667 memprintf(err_msg, "payload length must be > 0");
1668 return 0;
1669 }
1670 return 1;
1671}
1672
1673/* This function is used to validate the arguments passed to a "payload_lv" fetch
1674 * keyword. This keyword allows two positive integers and an optional signed one,
1675 * with the second one being strictly positive and the third one being greater than
1676 * the opposite of the two others if negative. It is assumed that the types are
1677 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1678 * not NULL, it will be filled with a pointer to an error message in case of
1679 * error, that the caller is responsible for freeing. The initial location must
1680 * either be freeable or NULL.
1681 */
1682static int val_payload_lv(struct arg *arg, char **err_msg)
1683{
1684 if (!arg[1].data.uint) {
1685 if (err_msg)
1686 memprintf(err_msg, "payload length must be > 0");
1687 return 0;
1688 }
1689
1690 if (arg[2].type == ARGT_SINT &&
1691 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1692 if (err_msg)
1693 memprintf(err_msg, "payload offset too negative");
1694 return 0;
1695 }
1696 return 1;
1697}
1698
Willy Tarreaub6866442008-07-14 23:54:42 +02001699static struct cfg_kw_list cfg_kws = {{ },{
1700 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001701 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001702 { 0, NULL, NULL },
1703}};
1704
Willy Tarreau61612d42012-04-19 18:42:05 +02001705/* Note: must not be declared <const> as its list will be overwritten.
1706 * Please take care of keeping this list alphabetically sorted.
1707 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001708static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001709 { "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 +02001710 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001711 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1712 { "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 +02001713 { "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 +02001714 { "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 +02001715 { "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 +02001716 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001717 { NULL, NULL, NULL, NULL },
1718}};
1719
Willy Tarreau4a129812012-04-25 17:31:42 +02001720/* Note: must not be declared <const> as its list will be overwritten.
1721 * Note: fetches that may return multiple types must be declared as the lowest
1722 * common denominator, the type that can be casted into all other ones. For
1723 * instance v4/v6 must be declared v4.
1724 */
Willy Tarreau12785782012-04-27 21:37:17 +02001725static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001726 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001727 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001728 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001729 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1730 { "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 +02001731 { "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 +02001732 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001733 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001734}};
1735
Willy Tarreaue6b98942007-10-29 01:09:36 +01001736__attribute__((constructor))
1737static void __tcp_protocol_init(void)
1738{
1739 protocol_register(&proto_tcpv4);
1740 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001741 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001742 cfg_register_keywords(&cfg_kws);
1743 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001744}
1745
1746
1747/*
1748 * Local variables:
1749 * c-indent-level: 8
1750 * c-basic-offset: 8
1751 * End:
1752 */