blob: 86fd6bd512da5a8a5fd53cb362f270a9d94c9752 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreau1a687942010-05-23 22:40:30 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040027#include <netinet/tcp.h>
28
Willy Tarreaub6866442008-07-14 23:54:42 +020029#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010036
Willy Tarreaue6b98942007-10-29 01:09:36 +010037#include <types/global.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020038#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039
40#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020041#include <proto/arg.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010042#include <proto/buffers.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020044//#include <proto/frontend.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020045#include <proto/log.h>
46#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010047#include <proto/protocols.h>
48#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020049#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020050#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020051#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020052#include <proto/stick_table.h>
Willy Tarreau59b94792012-05-11 16:16:40 +020053#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020054#include <proto/task.h>
Emeric Brun97679e72010-09-23 17:56:44 +020055#include <proto/buffers.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010056
Willy Tarreaue8c66af2008-01-13 18:40:14 +010057#ifdef CONFIG_HAP_CTTPROXY
58#include <import/ip_tproxy.h>
59#endif
60
Emeric Bruncf20bf12010-10-22 16:06:11 +020061static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
62static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010063
64/* Note: must not be declared <const> as its list will be overwritten */
65static struct protocol proto_tcpv4 = {
66 .name = "tcpv4",
67 .sock_domain = AF_INET,
68 .sock_type = SOCK_STREAM,
69 .sock_prot = IPPROTO_TCP,
70 .sock_family = AF_INET,
71 .sock_addrlen = sizeof(struct sockaddr_in),
72 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020073 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020074 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020075 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010076 .bind_all = tcp_bind_listeners,
77 .unbind_all = unbind_all_listeners,
78 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020079 .get_src = tcp_get_src,
80 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010081 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
82 .nb_listeners = 0,
83};
84
85/* Note: must not be declared <const> as its list will be overwritten */
86static struct protocol proto_tcpv6 = {
87 .name = "tcpv6",
88 .sock_domain = AF_INET6,
89 .sock_type = SOCK_STREAM,
90 .sock_prot = IPPROTO_TCP,
91 .sock_family = AF_INET6,
92 .sock_addrlen = sizeof(struct sockaddr_in6),
93 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020094 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020095 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020096 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010097 .bind_all = tcp_bind_listeners,
98 .unbind_all = unbind_all_listeners,
99 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200100 .get_src = tcp_get_src,
101 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100102 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
103 .nb_listeners = 0,
104};
105
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100106
David du Colombier6f5ccb12011-03-10 22:26:24 +0100107/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100108 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
109 * - 0 : ignore remote address (may even be a NULL pointer)
110 * - 1 : use provided address
111 * - 2 : use provided port
112 * - 3 : use both
113 *
114 * The function supports multiple foreign binding methods :
115 * - linux_tproxy: we directly bind to the foreign address
116 * - cttproxy: we bind to a local address then nat.
117 * The second one can be used as a fallback for the first one.
118 * This function returns 0 when everything's OK, 1 if it could not bind, to the
119 * local address, 2 if it could not bind to the foreign address.
120 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100121int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100122{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100123 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100124 int foreign_ok = 0;
125 int ret;
126
127#ifdef CONFIG_HAP_LINUX_TPROXY
128 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200129 static int ip6_transp_working = 1;
130 switch (local->ss_family) {
131 case AF_INET:
132 if (flags && ip_transp_working) {
133 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
134 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
135 foreign_ok = 1;
136 else
137 ip_transp_working = 0;
138 }
139 break;
140 case AF_INET6:
141 if (flags && ip6_transp_working) {
142 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
143 foreign_ok = 1;
144 else
145 ip6_transp_working = 0;
146 }
147 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100148 }
149#endif
150 if (flags) {
151 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200152 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100153 switch (remote->ss_family) {
154 case AF_INET:
155 if (flags & 1)
156 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
157 if (flags & 2)
158 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
159 break;
160 case AF_INET6:
161 if (flags & 1)
162 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
163 if (flags & 2)
164 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
165 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100166 default:
167 /* we don't want to try to bind to an unknown address family */
168 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100169 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100170 }
171
Simon Hormande072bd2011-06-24 15:11:37 +0900172 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100173 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200174 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100175 if (ret < 0)
176 return 2;
177 }
178 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200179 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100180 if (ret < 0)
181 return 1;
182 }
183
184 if (!flags)
185 return 0;
186
187#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100188 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100189 struct in_tproxy itp1, itp2;
190 memset(&itp1, 0, sizeof(itp1));
191
192 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100193 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
194 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100195
196 /* set connect flag on socket */
197 itp2.op = TPROXY_FLAGS;
198 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
199
200 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
201 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
202 foreign_ok = 1;
203 }
204 }
205#endif
206 if (!foreign_ok)
207 /* we could not bind to a foreign address */
208 return 2;
209
210 return 0;
211}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100212
Willy Tarreau9650f372009-08-16 14:02:45 +0200213
214/*
Willy Tarreauac825402011-03-04 22:04:29 +0100215 * This function initiates a connection to the target assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200216 * (si->{target,addr.to}). A source address may be pointed to by si->addr.from
Willy Tarreauac825402011-03-04 22:04:29 +0100217 * in case of transparent proxying. Normal source bind addresses are still
218 * determined locally (due to the possible need of a source port).
219 * si->target may point either to a valid server or to a backend, depending
220 * on si->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are supported.
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
David du Colombier6f5ccb12011-03-10 22:26:24 +0100232int tcp_connect_server(struct stream_interface *si)
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
238 switch (si->target.type) {
239 case TARG_TYPE_PROXY:
240 be = si->target.ptr.p;
241 srv = NULL;
242 break;
243 case TARG_TYPE_SERVER:
244 srv = si->target.ptr.s;
245 be = srv->proxy;
246 break;
247 default:
248 return SN_ERR_INTERNAL;
249 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200250
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200251 if ((fd = si->conn.t.sock.fd = socket(si->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +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
289 if (be->options & PR_O_TCP_NOLING)
Willy Tarreauf6f82252011-11-30 18:02:24 +0100290 si->flags |= SI_FL_NOLINGER;
Willy Tarreau9650f372009-08-16 14:02:45 +0200291
292 /* allow specific binding :
293 * - server-specific at first
294 * - proxy-specific next
295 */
296 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200297 int ret, flags = 0;
298
Willy Tarreau9650f372009-08-16 14:02:45 +0200299 switch (srv->state & SRV_TPROXY_MASK) {
300 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200301 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200302 flags = 3;
303 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200304 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200305 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200306 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200307 break;
308 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200309
Willy Tarreau9650f372009-08-16 14:02:45 +0200310#ifdef SO_BINDTODEVICE
311 /* Note: this might fail if not CAP_NET_RAW */
312 if (srv->iface_name)
313 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
314#endif
315
316 if (srv->sport_range) {
317 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100318 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200319
320 ret = 1;
321 src = srv->source_addr;
322
323 do {
324 /* note: in case of retry, we may have to release a previously
325 * allocated port, hence this loop's construct.
326 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200327 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
328 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200329
330 if (!attempts)
331 break;
332 attempts--;
333
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200334 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
335 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200336 break;
337
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200338 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200339 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200340
Willy Tarreau6471afb2011-09-23 10:54:59 +0200341 ret = tcp_bind_socket(fd, flags, &src, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200342 } while (ret != 0); /* binding NOK */
343 }
344 else {
Willy Tarreau6471afb2011-09-23 10:54:59 +0200345 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200346 }
347
348 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200349 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
350 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200351 close(fd);
352
353 if (ret == 1) {
354 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
355 be->id, srv->id);
356 send_log(be, LOG_EMERG,
357 "Cannot bind to source address before connect() for server %s/%s.\n",
358 be->id, srv->id);
359 } else {
360 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
361 be->id, srv->id);
362 send_log(be, LOG_EMERG,
363 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
364 be->id, srv->id);
365 }
366 return SN_ERR_RESOURCE;
367 }
368 }
369 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200370 int ret, flags = 0;
371
Willy Tarreau9650f372009-08-16 14:02:45 +0200372 switch (be->options & PR_O_TPXY_MASK) {
373 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200374 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200375 flags = 3;
376 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200377 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200378 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200379 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200380 break;
381 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200382
Willy Tarreau9650f372009-08-16 14:02:45 +0200383#ifdef SO_BINDTODEVICE
384 /* Note: this might fail if not CAP_NET_RAW */
385 if (be->iface_name)
386 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
387#endif
Willy Tarreau6471afb2011-09-23 10:54:59 +0200388 ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200389 if (ret) {
390 close(fd);
391 if (ret == 1) {
392 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
393 be->id);
394 send_log(be, LOG_EMERG,
395 "Cannot bind to source address before connect() for proxy %s.\n",
396 be->id);
397 } else {
398 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
399 be->id);
400 send_log(be, LOG_EMERG,
401 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
402 be->id);
403 }
404 return SN_ERR_RESOURCE;
405 }
406 }
407
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400408#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200409 /* disabling tcp quick ack now allows the first request to leave the
410 * machine with the first ACK. We only do this if there are pending
411 * data in the buffer.
412 */
Willy Tarreau572bf902012-07-02 17:01:20 +0200413 if ((be->options2 & PR_O2_SMARTCON) && si->ob->buf.o)
Simon Hormande072bd2011-06-24 15:11:37 +0900414 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200415#endif
416
Willy Tarreaue803de22010-01-21 17:43:04 +0100417 if (global.tune.server_sndbuf)
418 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
419
420 if (global.tune.server_rcvbuf)
421 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
422
Willy Tarreau9b061e32012-04-07 18:03:52 +0200423 si->flags &= ~SI_FL_FROM_SET;
Willy Tarreau96596ae2012-06-08 22:57:36 +0200424
425 si->conn.peeraddr = (struct sockaddr *)&si->addr.to;
426 si->conn.peerlen = get_addr_len(&si->addr.to);
427
428 if ((connect(fd, si->conn.peeraddr, si->conn.peerlen) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200429 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
430
431 if (errno == EAGAIN || errno == EADDRINUSE) {
432 char *msg;
433 if (errno == EAGAIN) /* no free ports left, try again later */
434 msg = "no free ports";
435 else
436 msg = "local address already in use";
437
438 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200439 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
440 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200441 close(fd);
442 send_log(be, LOG_EMERG,
443 "Connect() failed for server %s/%s: %s.\n",
444 be->id, srv->id, msg);
445 return SN_ERR_RESOURCE;
446 } else if (errno == ETIMEDOUT) {
447 //qfprintf(stderr,"Connect(): ETIMEDOUT");
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_SRVTO;
452 } else {
453 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
454 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200455 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
456 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200457 close(fd);
458 return SN_ERR_SRVCL;
459 }
460 }
461
William Lallemandb7ff6a32012-03-02 14:35:21 +0100462 /* needs src ip/port for logging */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200463 if (si->flags & SI_FL_SRC_ADDR)
Willy Tarreau59b94792012-05-11 16:16:40 +0200464 si_get_from_addr(si);
William Lallemandb7ff6a32012-03-02 14:35:21 +0100465
Willy Tarreau80184712012-07-06 14:54:49 +0200466 fdtab[fd].owner = &si->conn;
Willy Tarreau9650f372009-08-16 14:02:45 +0200467 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau505e34a2012-07-06 10:17:53 +0200468 si->conn.flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufd31e532012-07-23 18:24:25 +0200469 si->conn.flags |= CO_FL_NOTIFY_SI; /* we're on a stream_interface */
Willy Tarreau9650f372009-08-16 14:02:45 +0200470
Willy Tarreau076be252012-07-06 16:02:29 +0200471 /* Prepare to send a few handshakes related to the on-wire protocol. */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200472 if (si->send_proxy_ofs)
473 si->conn.flags |= CO_FL_SI_SEND_PROXY;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200474
Willy Tarreaud2274c62012-07-06 14:29:45 +0200475 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200476 fd_insert(fd);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200477 conn_sock_want_send(&si->conn); /* for connect status */
478 if (!(si->ob->flags & BF_OUT_EMPTY))
479 conn_data_want_send(&si->conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200480
481 si->state = SI_ST_CON;
482 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
483 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
484
485 return SN_ERR_NONE; /* connection is OK */
486}
487
488
Willy Tarreau59b94792012-05-11 16:16:40 +0200489/*
490 * Retrieves the source address for the socket <fd>, with <dir> indicating
491 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
492 * success, -1 in case of error. The socket's source address is stored in
493 * <sa> for <salen> bytes.
494 */
495int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
496{
497 if (dir)
498 return getsockname(fd, sa, &salen);
499 else
500 return getpeername(fd, sa, &salen);
501}
502
503
504/*
505 * Retrieves the original destination address for the socket <fd>, with <dir>
506 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
507 * listener, if the original destination address was translated, the original
508 * address is retrieved. It returns 0 in case of success, -1 in case of error.
509 * The socket's source address is stored in <sa> for <salen> bytes.
510 */
511int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
512{
513 if (dir)
514 return getpeername(fd, sa, &salen);
515#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
516 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
517 return 0;
518#endif
519 else
520 return getsockname(fd, sa, &salen);
521}
522
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200523/* This is the callback which is set when a connection establishment is pending
524 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200525 * once the connection is established. It updates the FD polling status. It
526 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
527 * it returns non-zero and removes itself from the connection's flags (the bit is
528 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200529 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200530int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200531{
Willy Tarreau239d7182012-07-23 18:53:03 +0200532 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200533
Willy Tarreau80184712012-07-06 14:54:49 +0200534 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200535 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200536
Willy Tarreau80184712012-07-06 14:54:49 +0200537 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200538 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200539
Willy Tarreau2da156f2012-07-23 15:07:23 +0200540 /* stop here if we reached the end of data */
541 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
542 goto out_error;
543
Willy Tarreau2c6be842012-07-06 17:12:34 +0200544 /* We have no data to send to check the connection, and
545 * getsockopt() will not inform us whether the connection
546 * is still pending. So we'll reuse connect() to check the
547 * state of the socket. This has the advantage of giving us
548 * the following info :
549 * - error
550 * - connecting (EALREADY, EINPROGRESS)
551 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200552 */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200553 if ((connect(fd, conn->peeraddr, conn->peerlen) < 0)) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200554 if (errno == EALREADY || errno == EINPROGRESS) {
555 conn_sock_stop_recv(conn);
556 conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200557 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200558 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200559
Willy Tarreau2c6be842012-07-06 17:12:34 +0200560 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200561 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200562
Willy Tarreau2c6be842012-07-06 17:12:34 +0200563 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200564 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200565
Willy Tarreau076be252012-07-06 16:02:29 +0200566 /* The FD is ready now, we'll mark the connection as complete and
567 * forward the event to the data layer which will update the stream
568 * interface flags.
Willy Tarreaua190d592012-05-20 18:35:19 +0200569 */
Willy Tarreau80184712012-07-06 14:54:49 +0200570 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200571 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200572
573 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200574 /* Write error on the file descriptor. Report it to the connection
575 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200576 */
577
Willy Tarreau80184712012-07-06 14:54:49 +0200578 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200579 conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200580 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200581}
582
Willy Tarreau59b94792012-05-11 16:16:40 +0200583
Willy Tarreaue6b98942007-10-29 01:09:36 +0100584/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
585 * an error message in <err> if the message is at most <errlen> bytes long
586 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
587 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
588 * was alright and that no message was returned. ERR_RETRYABLE means that an
589 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700590 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100591 * the meaning of the error, but just indicate that a message is present which
592 * should be displayed with the respective level. Last, ERR_ABORT indicates
593 * that it's pointless to try to start other listeners. No error message is
594 * returned if errlen is NULL.
595 */
596int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
597{
598 __label__ tcp_return, tcp_close_return;
599 int fd, err;
600 const char *msg = NULL;
601
602 /* ensure we never return garbage */
603 if (errmsg && errlen)
604 *errmsg = 0;
605
606 if (listener->state != LI_ASSIGNED)
607 return ERR_NONE; /* already bound */
608
609 err = ERR_NONE;
610
611 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
612 err |= ERR_RETRYABLE | ERR_ALERT;
613 msg = "cannot create listening socket";
614 goto tcp_return;
615 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100616
Willy Tarreaue6b98942007-10-29 01:09:36 +0100617 if (fd >= global.maxsock) {
618 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
619 msg = "not enough free sockets (raise '-n' parameter)";
620 goto tcp_close_return;
621 }
622
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200623 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100624 err |= ERR_FATAL | ERR_ALERT;
625 msg = "cannot make socket non-blocking";
626 goto tcp_close_return;
627 }
628
Simon Hormande072bd2011-06-24 15:11:37 +0900629 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100630 /* not fatal but should be reported */
631 msg = "cannot do so_reuseaddr";
632 err |= ERR_ALERT;
633 }
634
635 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900636 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100637
Willy Tarreaue6b98942007-10-29 01:09:36 +0100638#ifdef SO_REUSEPORT
639 /* OpenBSD supports this. As it's present in old libc versions of Linux,
640 * it might return an error that we will silently ignore.
641 */
Simon Hormande072bd2011-06-24 15:11:37 +0900642 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100643#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100644#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200645 if (listener->options & LI_O_FOREIGN) {
646 switch (listener->addr.ss_family) {
647 case AF_INET:
648 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
649 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
650 msg = "cannot make listening socket transparent";
651 err |= ERR_ALERT;
652 }
653 break;
654 case AF_INET6:
655 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
656 msg = "cannot make listening socket transparent";
657 err |= ERR_ALERT;
658 }
659 break;
660 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100661 }
662#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100663#ifdef SO_BINDTODEVICE
664 /* Note: this might fail if not CAP_NET_RAW */
665 if (listener->interface) {
666 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100667 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100668 msg = "cannot bind listener to device";
669 err |= ERR_WARN;
670 }
671 }
672#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400673#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100674 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400675 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200676 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
677 msg = "cannot set MSS";
678 err |= ERR_WARN;
679 }
680 }
681#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200682#if defined(TCP_DEFER_ACCEPT)
683 if (listener->options & LI_O_DEF_ACCEPT) {
684 /* defer accept by up to one second */
685 int accept_delay = 1;
686 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
687 msg = "cannot enable DEFER_ACCEPT";
688 err |= ERR_WARN;
689 }
690 }
691#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100692 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
693 err |= ERR_RETRYABLE | ERR_ALERT;
694 msg = "cannot bind socket";
695 goto tcp_close_return;
696 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100697
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100698 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100699 err |= ERR_RETRYABLE | ERR_ALERT;
700 msg = "cannot listen to socket";
701 goto tcp_close_return;
702 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100703
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400704#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200705 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900706 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200707#endif
708
Willy Tarreaue6b98942007-10-29 01:09:36 +0100709 /* the socket is ready */
710 listener->fd = fd;
711 listener->state = LI_LISTEN;
712
Willy Tarreaueabf3132008-08-29 23:36:51 +0200713 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaueb472682010-05-28 18:46:57 +0200714 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200715 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200716 fd_insert(fd);
717
Willy Tarreaue6b98942007-10-29 01:09:36 +0100718 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100719 if (msg && errlen) {
720 char pn[INET6_ADDRSTRLEN];
721
Willy Tarreau631f01c2011-09-05 00:36:48 +0200722 addr_to_str(&listener->addr, pn, sizeof(pn));
723 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100724 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100725 return err;
726
727 tcp_close_return:
728 close(fd);
729 goto tcp_return;
730}
731
732/* This function creates all TCP sockets bound to the protocol entry <proto>.
733 * It is intended to be used as the protocol's bind_all() function.
734 * The sockets will be registered but not added to any fd_set, in order not to
735 * loose them across the fork(). A call to enable_all_listeners() is needed
736 * to complete initialization. The return value is composed from ERR_*.
737 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200738static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100739{
740 struct listener *listener;
741 int err = ERR_NONE;
742
743 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200744 err |= tcp_bind_listener(listener, errmsg, errlen);
745 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100746 break;
747 }
748
749 return err;
750}
751
752/* Add listener to the list of tcpv4 listeners. The listener's state
753 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
754 * listeners is updated. This is the function to use to add a new listener.
755 */
756void tcpv4_add_listener(struct listener *listener)
757{
758 if (listener->state != LI_INIT)
759 return;
760 listener->state = LI_ASSIGNED;
761 listener->proto = &proto_tcpv4;
762 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
763 proto_tcpv4.nb_listeners++;
764}
765
766/* Add listener to the list of tcpv4 listeners. The listener's state
767 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
768 * listeners is updated. This is the function to use to add a new listener.
769 */
770void tcpv6_add_listener(struct listener *listener)
771{
772 if (listener->state != LI_INIT)
773 return;
774 listener->state = LI_ASSIGNED;
775 listener->proto = &proto_tcpv6;
776 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
777 proto_tcpv6.nb_listeners++;
778}
779
Willy Tarreauedcf6682008-11-30 23:15:34 +0100780/* This function performs the TCP request analysis on the current request. It
781 * returns 1 if the processing can continue on next analysers, or zero if it
782 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200783 * request. It relies on buffers flags, and updates s->req->analysers. The
784 * function may be called for frontend rules and backend rules. It only relies
785 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100786 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200787int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100788{
789 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200790 struct stksess *ts;
791 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100792 int partial;
793
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100794 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 +0100795 now_ms, __FUNCTION__,
796 s,
797 req,
798 req->rex, req->wex,
799 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100800 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100801 req->analysers);
802
Willy Tarreauedcf6682008-11-30 23:15:34 +0100803 /* We don't know whether we have enough data, so must proceed
804 * this way :
805 * - iterate through all rules in their declaration order
806 * - if one rule returns MISS, it means the inspect delay is
807 * not over yet, then return immediately, otherwise consider
808 * it as a non-match.
809 * - if one rule returns OK, then return OK
810 * - if one rule returns KO, then return KO
811 */
812
Willy Tarreaub824b002010-09-29 16:36:16 +0200813 if (req->flags & (BF_SHUTR|BF_FULL) || !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200814 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100815 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200816 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100817
Willy Tarreaufb356202010-08-03 14:02:05 +0200818 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100819 int ret = ACL_PAT_PASS;
820
821 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200822 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100823 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200824 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100825 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200826 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
827 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100828 return 0;
829 }
830
831 ret = acl_pass(ret);
832 if (rule->cond->pol == ACL_COND_UNLESS)
833 ret = !ret;
834 }
835
836 if (ret) {
837 /* we have a matching rule. */
838 if (rule->action == TCP_ACT_REJECT) {
839 buffer_abort(req);
840 buffer_abort(s->rep);
841 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200842
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100843 s->be->be_counters.denied_req++;
844 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200845 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200846 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200847
Willy Tarreauedcf6682008-11-30 23:15:34 +0100848 if (!(s->flags & SN_ERR_MASK))
849 s->flags |= SN_ERR_PRXCOND;
850 if (!(s->flags & SN_FINST_MASK))
851 s->flags |= SN_FINST_R;
852 return 0;
853 }
Willy Tarreau56123282010-08-06 19:06:56 +0200854 else if (rule->action == TCP_ACT_TRK_SC1) {
855 if (!s->stkctr1_entry) {
856 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200857 * Also, note that right now we can only track SRC so we
858 * don't check how to get the key, but later we may need
859 * to consider rule->act_prm->trk_ctr.type.
860 */
861 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100862 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200863 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200864 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200865 if (s->fe != s->be)
866 s->flags |= SN_BE_TRACK_SC1;
867 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200868 }
869 }
Willy Tarreau56123282010-08-06 19:06:56 +0200870 else if (rule->action == TCP_ACT_TRK_SC2) {
871 if (!s->stkctr2_entry) {
872 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200873 * Also, note that right now we can only track SRC so we
874 * don't check how to get the key, but later we may need
875 * to consider rule->act_prm->trk_ctr.type.
876 */
877 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100878 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200879 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200880 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200881 if (s->fe != s->be)
882 s->flags |= SN_BE_TRACK_SC2;
883 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200884 }
885 }
886 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100887 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200888 break;
889 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100890 }
891 }
892
893 /* if we get there, it means we have no rule which matches, or
894 * we have an explicit accept, so we apply the default accept.
895 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200896 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100897 req->analyse_exp = TICK_ETERNITY;
898 return 1;
899}
900
Emeric Brun97679e72010-09-23 17:56:44 +0200901/* This function performs the TCP response analysis on the current response. It
902 * returns 1 if the processing can continue on next analysers, or zero if it
903 * needs more data, encounters an error, or wants to immediately abort the
904 * response. It relies on buffers flags, and updates s->rep->analysers. The
905 * function may be called for backend rules.
906 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200907int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200908{
909 struct tcp_rule *rule;
910 int partial;
911
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100912 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 +0200913 now_ms, __FUNCTION__,
914 s,
915 rep,
916 rep->rex, rep->wex,
917 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100918 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200919 rep->analysers);
920
921 /* We don't know whether we have enough data, so must proceed
922 * this way :
923 * - iterate through all rules in their declaration order
924 * - if one rule returns MISS, it means the inspect delay is
925 * not over yet, then return immediately, otherwise consider
926 * it as a non-match.
927 * - if one rule returns OK, then return OK
928 * - if one rule returns KO, then return KO
929 */
930
931 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200932 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200933 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200934 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200935
936 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
937 int ret = ACL_PAT_PASS;
938
939 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200940 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200941 if (ret == ACL_PAT_MISS) {
942 /* just set the analyser timeout once at the beginning of the response */
943 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
944 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
945 return 0;
946 }
947
948 ret = acl_pass(ret);
949 if (rule->cond->pol == ACL_COND_UNLESS)
950 ret = !ret;
951 }
952
953 if (ret) {
954 /* we have a matching rule. */
955 if (rule->action == TCP_ACT_REJECT) {
956 buffer_abort(rep);
957 buffer_abort(s->req);
958 rep->analysers = 0;
959
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100960 s->be->be_counters.denied_resp++;
961 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200962 if (s->listener->counters)
963 s->listener->counters->denied_resp++;
964
965 if (!(s->flags & SN_ERR_MASK))
966 s->flags |= SN_ERR_PRXCOND;
967 if (!(s->flags & SN_FINST_MASK))
968 s->flags |= SN_FINST_D;
969 return 0;
970 }
971 else {
972 /* otherwise accept */
973 break;
974 }
975 }
976 }
977
978 /* if we get there, it means we have no rule which matches, or
979 * we have an explicit accept, so we apply the default accept.
980 */
981 rep->analysers &= ~an_bit;
982 rep->analyse_exp = TICK_ETERNITY;
983 return 1;
984}
985
986
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200987/* This function performs the TCP layer4 analysis on the current request. It
988 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
989 * matches or if no more rule matches. It can only use rules which don't need
990 * any data.
991 */
992int tcp_exec_req_rules(struct session *s)
993{
994 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200995 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200996 struct stktable *t = NULL;
997 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200998 int ret;
999
1000 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1001 ret = ACL_PAT_PASS;
1002
1003 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001004 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001005 ret = acl_pass(ret);
1006 if (rule->cond->pol == ACL_COND_UNLESS)
1007 ret = !ret;
1008 }
1009
1010 if (ret) {
1011 /* we have a matching rule. */
1012 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001013 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001014 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001015 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001016
1017 if (!(s->flags & SN_ERR_MASK))
1018 s->flags |= SN_ERR_PRXCOND;
1019 if (!(s->flags & SN_FINST_MASK))
1020 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001021 result = 0;
1022 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001023 }
Willy Tarreau56123282010-08-06 19:06:56 +02001024 else if (rule->action == TCP_ACT_TRK_SC1) {
1025 if (!s->stkctr1_entry) {
1026 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001027 * Also, note that right now we can only track SRC so we
1028 * don't check how to get the key, but later we may need
1029 * to consider rule->act_prm->trk_ctr.type.
1030 */
1031 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001032 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001033 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001034 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001035 }
1036 }
Willy Tarreau56123282010-08-06 19:06:56 +02001037 else if (rule->action == TCP_ACT_TRK_SC2) {
1038 if (!s->stkctr2_entry) {
1039 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001040 * Also, note that right now we can only track SRC so we
1041 * don't check how to get the key, but later we may need
1042 * to consider rule->act_prm->trk_ctr.type.
1043 */
1044 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001045 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001046 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001047 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001048 }
1049 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001050 else {
1051 /* otherwise it's an accept */
1052 break;
1053 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001054 }
1055 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001056 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001057}
1058
Emeric Brun97679e72010-09-23 17:56:44 +02001059/* Parse a tcp-response rule. Return a negative value in case of failure */
1060static int tcp_parse_response_rule(char **args, int arg, int section_type,
1061 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001062 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001063{
1064 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001065 memprintf(err, "%s %s is only allowed in 'backend' sections",
1066 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001067 return -1;
1068 }
1069
1070 if (strcmp(args[arg], "accept") == 0) {
1071 arg++;
1072 rule->action = TCP_ACT_ACCEPT;
1073 }
1074 else if (strcmp(args[arg], "reject") == 0) {
1075 arg++;
1076 rule->action = TCP_ACT_REJECT;
1077 }
1078 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001079 memprintf(err,
1080 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1081 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001082 return -1;
1083 }
1084
1085 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001086 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1087 memprintf(err,
1088 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1089 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001090 return -1;
1091 }
1092 }
1093 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001094 memprintf(err,
1095 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001096 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1097 return -1;
1098 }
1099 return 0;
1100}
1101
1102
1103
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001104/* Parse a tcp-request rule. Return a negative value in case of failure */
1105static int tcp_parse_request_rule(char **args, int arg, int section_type,
1106 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001107 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001108{
1109 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001110 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1111 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001112 return -1;
1113 }
1114
1115 if (!strcmp(args[arg], "accept")) {
1116 arg++;
1117 rule->action = TCP_ACT_ACCEPT;
1118 }
1119 else if (!strcmp(args[arg], "reject")) {
1120 arg++;
1121 rule->action = TCP_ACT_REJECT;
1122 }
Willy Tarreau56123282010-08-06 19:06:56 +02001123 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001124 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001125 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001126
1127 arg++;
1128 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001129 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001130
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001131 if (ret < 0) { /* nb: warnings are not handled yet */
1132 memprintf(err,
1133 "'%s %s %s' : %s in %s '%s'",
1134 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1135 return ret;
1136 }
Willy Tarreau56123282010-08-06 19:06:56 +02001137 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001138 }
Willy Tarreau56123282010-08-06 19:06:56 +02001139 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001140 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001141 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001142
1143 arg++;
1144 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001145 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001146
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001147 if (ret < 0) { /* nb: warnings are not handled yet */
1148 memprintf(err,
1149 "'%s %s %s' : %s in %s '%s'",
1150 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1151 return ret;
1152 }
Willy Tarreau56123282010-08-06 19:06:56 +02001153 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001154 }
1155 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001156 memprintf(err,
1157 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1158 "or 'track-sc2' in %s '%s' (got '%s')",
1159 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001160 return -1;
1161 }
1162
1163 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001164 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1165 memprintf(err,
1166 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1167 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001168 return -1;
1169 }
1170 }
1171 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001172 memprintf(err,
1173 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001174 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1175 return -1;
1176 }
1177 return 0;
1178}
1179
Emeric Brun97679e72010-09-23 17:56:44 +02001180/* This function should be called to parse a line starting with the "tcp-response"
1181 * keyword.
1182 */
1183static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001184 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001185{
1186 const char *ptr = NULL;
1187 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001188 int warn = 0;
1189 int arg;
1190 struct tcp_rule *rule;
1191
1192 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001193 memprintf(err, "missing argument for '%s' in %s '%s'",
1194 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001195 return -1;
1196 }
1197
1198 if (strcmp(args[1], "inspect-delay") == 0) {
1199 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001200 memprintf(err, "%s %s is only allowed in 'backend' sections",
1201 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001202 return -1;
1203 }
1204
1205 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001206 memprintf(err,
1207 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1208 args[0], args[1], proxy_type_str(curpx), curpx->id);
1209 if (ptr)
1210 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001211 return -1;
1212 }
1213
1214 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001215 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1216 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001217 return 1;
1218 }
1219 curpx->tcp_rep.inspect_delay = val;
1220 return 0;
1221 }
1222
Simon Hormande072bd2011-06-24 15:11:37 +09001223 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001224 LIST_INIT(&rule->list);
1225 arg = 1;
1226
1227 if (strcmp(args[1], "content") == 0) {
1228 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001229 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001230 goto error;
1231
1232 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1233 struct acl *acl;
1234 const char *name;
1235
1236 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1237 name = acl ? acl->name : "(unknown)";
1238
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001239 memprintf(err,
1240 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1241 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001242 warn++;
1243 }
1244
1245 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1246 }
1247 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001248 memprintf(err,
1249 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1250 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001251 goto error;
1252 }
1253
1254 return warn;
1255 error:
1256 free(rule);
1257 return -1;
1258}
1259
1260
Willy Tarreaub6866442008-07-14 23:54:42 +02001261/* This function should be called to parse a line starting with the "tcp-request"
1262 * keyword.
1263 */
1264static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001265 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001266{
1267 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001268 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001269 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001270 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001271 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001272
1273 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001274 if (curpx == defpx)
1275 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1276 else
1277 memprintf(err, "missing argument for '%s' in %s '%s'",
1278 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001279 return -1;
1280 }
1281
1282 if (!strcmp(args[1], "inspect-delay")) {
1283 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001284 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1285 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001286 return -1;
1287 }
1288
Willy Tarreaub6866442008-07-14 23:54:42 +02001289 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001290 memprintf(err,
1291 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1292 args[0], args[1], proxy_type_str(curpx), curpx->id);
1293 if (ptr)
1294 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001295 return -1;
1296 }
1297
1298 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001299 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1300 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001301 return 1;
1302 }
1303 curpx->tcp_req.inspect_delay = val;
1304 return 0;
1305 }
1306
Simon Hormande072bd2011-06-24 15:11:37 +09001307 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001308 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001309 arg = 1;
1310
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001311 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001312 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001313 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001314 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001315
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001316 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001317 struct acl *acl;
1318 const char *name;
1319
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001320 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001321 name = acl ? acl->name : "(unknown)";
1322
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001323 memprintf(err,
1324 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1325 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001326 warn++;
1327 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001328 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001329 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001330 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001331 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001332
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001333 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001334 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1335 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001336 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001337 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001338
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001339 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001340 goto error;
1341
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001342 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1343 struct acl *acl;
1344 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001345
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001346 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1347 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001348
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001349 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001350 memprintf(err,
1351 "'%s %s' may not reference acl '%s' which makes use of "
1352 "payload in %s '%s'. Please use '%s content' for this.",
1353 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001354 goto error;
1355 }
1356 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001357 memprintf(err,
1358 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1359 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001360 warn++;
1361 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001362 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001363 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001364 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001365 if (curpx == defpx)
1366 memprintf(err,
1367 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1368 args[0], args[1]);
1369 else
1370 memprintf(err,
1371 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1372 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001373 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001374 }
1375
Willy Tarreau1a687942010-05-23 22:40:30 +02001376 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001377 error:
1378 free(rule);
1379 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001380}
1381
Willy Tarreau645513a2010-05-24 20:55:15 +02001382
1383/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001384/* All supported sample fetch functios must be declared here */
1385/************************************************************************/
1386
1387/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001388 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001389 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1390 * is a string (cookie name), other types will lead to undefined behaviour.
1391 */
1392int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001393smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001394 const struct arg *args, struct sample *smp)
1395{
1396 int bleft;
1397 const unsigned char *data;
1398
1399 if (!l4 || !l4->req)
1400 return 0;
1401
1402 smp->flags = 0;
1403 smp->type = SMP_T_CSTR;
1404
Willy Tarreau572bf902012-07-02 17:01:20 +02001405 bleft = l4->req->buf.i;
Willy Tarreau32389b72012-04-23 23:13:20 +02001406 if (bleft <= 11)
1407 goto too_short;
1408
Willy Tarreau572bf902012-07-02 17:01:20 +02001409 data = (const unsigned char *)l4->req->buf.p + 11;
Willy Tarreau32389b72012-04-23 23:13:20 +02001410 bleft -= 11;
1411
1412 if (bleft <= 7)
1413 goto too_short;
1414
1415 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1416 goto not_cookie;
1417
1418 data += 7;
1419 bleft -= 7;
1420
1421 while (bleft > 0 && *data == ' ') {
1422 data++;
1423 bleft--;
1424 }
1425
1426 if (args) {
1427
1428 if (bleft <= args->data.str.len)
1429 goto too_short;
1430
1431 if ((data[args->data.str.len] != '=') ||
1432 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1433 goto not_cookie;
1434
1435 data += args->data.str.len + 1;
1436 bleft -= args->data.str.len + 1;
1437 } else {
1438 while (bleft > 0 && *data != '=') {
1439 if (*data == '\r' || *data == '\n')
1440 goto not_cookie;
1441 data++;
1442 bleft--;
1443 }
1444
1445 if (bleft < 1)
1446 goto too_short;
1447
1448 if (*data != '=')
1449 goto not_cookie;
1450
1451 data++;
1452 bleft--;
1453 }
1454
1455 /* data points to cookie value */
1456 smp->data.str.str = (char *)data;
1457 smp->data.str.len = 0;
1458
1459 while (bleft > 0 && *data != '\r') {
1460 data++;
1461 bleft--;
1462 }
1463
1464 if (bleft < 2)
1465 goto too_short;
1466
1467 if (data[0] != '\r' || data[1] != '\n')
1468 goto not_cookie;
1469
1470 smp->data.str.len = (char *)data - smp->data.str.str;
1471 smp->flags = SMP_F_VOLATILE;
1472 return 1;
1473
1474 too_short:
1475 smp->flags = SMP_F_MAY_CHANGE;
1476 not_cookie:
1477 return 0;
1478}
1479
Willy Tarreau32389b72012-04-23 23:13:20 +02001480/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001481/* All supported ACL keywords must be declared here. */
1482/************************************************************************/
1483
Willy Tarreau32389b72012-04-23 23:13:20 +02001484/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1485static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001486acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001487 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001488{
1489 int ret;
1490
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001491 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001492
1493 if (smp->flags & SMP_F_MAY_CHANGE)
1494 return 0;
1495
1496 smp->flags = SMP_F_VOLATILE;
1497 smp->type = SMP_T_UINT;
1498 smp->data.uint = ret;
1499 return 1;
1500}
1501
1502
Willy Tarreau4a129812012-04-25 17:31:42 +02001503/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001504static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001505smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001506 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001507{
Willy Tarreauf4362b32011-12-16 17:49:52 +01001508 switch (l4->si[0].addr.from.ss_family) {
1509 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001510 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.from)->sin_addr;
1511 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001512 break;
1513 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001514 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.from))->sin6_addr;
1515 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001516 break;
1517 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001518 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001519 }
Emeric Brunf769f512010-10-22 17:14:01 +02001520
Willy Tarreau37406352012-04-23 16:16:37 +02001521 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001522 return 1;
1523}
1524
Willy Tarreaua5e37562011-12-16 17:06:15 +01001525/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001526static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001527smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001528 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001529{
Willy Tarreauf853c462012-04-23 18:53:56 +02001530 smp->type = SMP_T_UINT;
1531 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001532 return 0;
1533
Willy Tarreau37406352012-04-23 16:16:37 +02001534 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001535 return 1;
1536}
1537
Willy Tarreau4a129812012-04-25 17:31:42 +02001538/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001539static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001540smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001541 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001542{
Willy Tarreau59b94792012-05-11 16:16:40 +02001543 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001544
Willy Tarreauf4362b32011-12-16 17:49:52 +01001545 switch (l4->si[0].addr.to.ss_family) {
1546 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001547 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.to)->sin_addr;
1548 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001549 break;
1550 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001551 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.to))->sin6_addr;
1552 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001553 break;
1554 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001555 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001556 }
Emeric Brunf769f512010-10-22 17:14:01 +02001557
Willy Tarreau37406352012-04-23 16:16:37 +02001558 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001559 return 1;
1560}
1561
Willy Tarreaua5e37562011-12-16 17:06:15 +01001562/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001563static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001564smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001565 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001566{
Willy Tarreau59b94792012-05-11 16:16:40 +02001567 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001568
Willy Tarreauf853c462012-04-23 18:53:56 +02001569 smp->type = SMP_T_UINT;
1570 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001571 return 0;
1572
Willy Tarreau37406352012-04-23 16:16:37 +02001573 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001574 return 1;
1575}
1576
1577static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001578smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1579 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001580{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001581 unsigned int len_offset = arg_p[0].data.uint;
1582 unsigned int len_size = arg_p[1].data.uint;
1583 unsigned int buf_offset;
1584 unsigned int buf_size = 0;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001585 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001586 int i;
1587
1588 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1589 /* by default buf offset == len offset + len size */
1590 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1591
1592 if (!l4)
1593 return 0;
1594
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001595 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001596
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001597 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001598 return 0;
1599
Willy Tarreau572bf902012-07-02 17:01:20 +02001600 if (len_offset + len_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001601 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001602
1603 for (i = 0; i < len_size; i++) {
Willy Tarreau572bf902012-07-02 17:01:20 +02001604 buf_size = (buf_size << 8) + ((unsigned char *)b->buf.p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001605 }
1606
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001607 /* buf offset may be implicit, absolute or relative */
1608 buf_offset = len_offset + len_size;
1609 if (arg_p[2].type == ARGT_UINT)
1610 buf_offset = arg_p[2].data.uint;
1611 else if (arg_p[2].type == ARGT_SINT)
1612 buf_offset += arg_p[2].data.sint;
1613
Willy Tarreau572bf902012-07-02 17:01:20 +02001614 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001615 /* will never match */
1616 smp->flags = 0;
1617 return 0;
1618 }
1619
Willy Tarreau572bf902012-07-02 17:01:20 +02001620 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001621 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001622
1623 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001624 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001625 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001626 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001627 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001628
1629 too_short:
1630 smp->flags = SMP_F_MAY_CHANGE;
1631 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001632}
1633
1634static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001635smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1636 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001637{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001638 unsigned int buf_offset = arg_p[0].data.uint;
1639 unsigned int buf_size = arg_p[1].data.uint;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001640 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001641
1642 if (!l4)
1643 return 0;
1644
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001645 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001646
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001647 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001648 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001649
Willy Tarreau572bf902012-07-02 17:01:20 +02001650 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001651 /* will never match */
1652 smp->flags = 0;
1653 return 0;
1654 }
Emericf2d7cae2010-11-05 18:13:50 +01001655
Willy Tarreau572bf902012-07-02 17:01:20 +02001656 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001657 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001658
1659 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001660 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001661 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001662 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001663 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001664
1665 too_short:
1666 smp->flags = SMP_F_MAY_CHANGE;
1667 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001668}
1669
Willy Tarreau21d68a62012-04-20 15:52:36 +02001670/* This function is used to validate the arguments passed to a "payload" fetch
1671 * keyword. This keyword expects two positive integers, with the second one
1672 * being strictly positive. It is assumed that the types are already the correct
1673 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1674 * filled with a pointer to an error message in case of error, that the caller
1675 * is responsible for freeing. The initial location must either be freeable or
1676 * NULL.
1677 */
1678static int val_payload(struct arg *arg, char **err_msg)
1679{
1680 if (!arg[1].data.uint) {
1681 if (err_msg)
1682 memprintf(err_msg, "payload length must be > 0");
1683 return 0;
1684 }
1685 return 1;
1686}
1687
1688/* This function is used to validate the arguments passed to a "payload_lv" fetch
1689 * keyword. This keyword allows two positive integers and an optional signed one,
1690 * with the second one being strictly positive and the third one being greater than
1691 * the opposite of the two others if negative. It is assumed that the types are
1692 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1693 * not NULL, it will be filled with a pointer to an error message in case of
1694 * error, that the caller is responsible for freeing. The initial location must
1695 * either be freeable or NULL.
1696 */
1697static int val_payload_lv(struct arg *arg, char **err_msg)
1698{
1699 if (!arg[1].data.uint) {
1700 if (err_msg)
1701 memprintf(err_msg, "payload length must be > 0");
1702 return 0;
1703 }
1704
1705 if (arg[2].type == ARGT_SINT &&
1706 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1707 if (err_msg)
1708 memprintf(err_msg, "payload offset too negative");
1709 return 0;
1710 }
1711 return 1;
1712}
1713
Willy Tarreaub6866442008-07-14 23:54:42 +02001714static struct cfg_kw_list cfg_kws = {{ },{
1715 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001716 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001717 { 0, NULL, NULL },
1718}};
1719
Willy Tarreau61612d42012-04-19 18:42:05 +02001720/* Note: must not be declared <const> as its list will be overwritten.
1721 * Please take care of keeping this list alphabetically sorted.
1722 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001723static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001724 { "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 +02001725 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001726 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1727 { "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 +02001728 { "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 +02001729 { "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 +02001730 { "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 +02001731 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001732 { NULL, NULL, NULL, NULL },
1733}};
1734
Willy Tarreau4a129812012-04-25 17:31:42 +02001735/* Note: must not be declared <const> as its list will be overwritten.
1736 * Note: fetches that may return multiple types must be declared as the lowest
1737 * common denominator, the type that can be casted into all other ones. For
1738 * instance v4/v6 must be declared v4.
1739 */
Willy Tarreau12785782012-04-27 21:37:17 +02001740static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001741 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001742 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001743 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001744 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1745 { "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 +02001746 { "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 +02001747 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001748 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001749}};
1750
Willy Tarreaue6b98942007-10-29 01:09:36 +01001751__attribute__((constructor))
1752static void __tcp_protocol_init(void)
1753{
1754 protocol_register(&proto_tcpv4);
1755 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001756 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001757 cfg_register_keywords(&cfg_kws);
1758 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001759}
1760
1761
1762/*
1763 * Local variables:
1764 * c-indent-level: 8
1765 * c-basic-offset: 8
1766 * End:
1767 */