blob: be246e19214ac6816a68bcc4cf778c0ac830e235 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreau1a687942010-05-23 22:40:30 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040027#include <netinet/tcp.h>
28
Willy Tarreaub6866442008-07-14 23:54:42 +020029#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010036
Willy Tarreaue6b98942007-10-29 01:09:36 +010037#include <types/global.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020038#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039
40#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020041#include <proto/arg.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010042#include <proto/buffers.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020044//#include <proto/frontend.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020045#include <proto/log.h>
46#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010047#include <proto/protocols.h>
48#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020049#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020050#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020051#include <proto/session.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020052#include <proto/sock_raw.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/stick_table.h>
Willy Tarreau59b94792012-05-11 16:16:40 +020054#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020055#include <proto/task.h>
Emeric Brun97679e72010-09-23 17:56:44 +020056#include <proto/buffers.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010057
Willy Tarreaue8c66af2008-01-13 18:40:14 +010058#ifdef CONFIG_HAP_CTTPROXY
59#include <import/ip_tproxy.h>
60#endif
61
Emeric Bruncf20bf12010-10-22 16:06:11 +020062static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
63static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010064
65/* Note: must not be declared <const> as its list will be overwritten */
66static struct protocol proto_tcpv4 = {
67 .name = "tcpv4",
68 .sock_domain = AF_INET,
69 .sock_type = SOCK_STREAM,
70 .sock_prot = IPPROTO_TCP,
71 .sock_family = AF_INET,
72 .sock_addrlen = sizeof(struct sockaddr_in),
73 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020074 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020075 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020076 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010077 .bind_all = tcp_bind_listeners,
78 .unbind_all = unbind_all_listeners,
79 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020080 .get_src = tcp_get_src,
81 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010082 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
83 .nb_listeners = 0,
84};
85
86/* Note: must not be declared <const> as its list will be overwritten */
87static struct protocol proto_tcpv6 = {
88 .name = "tcpv6",
89 .sock_domain = AF_INET6,
90 .sock_type = SOCK_STREAM,
91 .sock_prot = IPPROTO_TCP,
92 .sock_family = AF_INET6,
93 .sock_addrlen = sizeof(struct sockaddr_in6),
94 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020095 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020096 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020097 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010098 .bind_all = tcp_bind_listeners,
99 .unbind_all = unbind_all_listeners,
100 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200101 .get_src = tcp_get_src,
102 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100103 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
104 .nb_listeners = 0,
105};
106
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100107
David du Colombier6f5ccb12011-03-10 22:26:24 +0100108/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100109 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
110 * - 0 : ignore remote address (may even be a NULL pointer)
111 * - 1 : use provided address
112 * - 2 : use provided port
113 * - 3 : use both
114 *
115 * The function supports multiple foreign binding methods :
116 * - linux_tproxy: we directly bind to the foreign address
117 * - cttproxy: we bind to a local address then nat.
118 * The second one can be used as a fallback for the first one.
119 * This function returns 0 when everything's OK, 1 if it could not bind, to the
120 * local address, 2 if it could not bind to the foreign address.
121 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100122int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100123{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100124 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100125 int foreign_ok = 0;
126 int ret;
127
128#ifdef CONFIG_HAP_LINUX_TPROXY
129 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200130 static int ip6_transp_working = 1;
131 switch (local->ss_family) {
132 case AF_INET:
133 if (flags && ip_transp_working) {
134 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
135 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
136 foreign_ok = 1;
137 else
138 ip_transp_working = 0;
139 }
140 break;
141 case AF_INET6:
142 if (flags && ip6_transp_working) {
143 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
144 foreign_ok = 1;
145 else
146 ip6_transp_working = 0;
147 }
148 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100149 }
150#endif
151 if (flags) {
152 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200153 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100154 switch (remote->ss_family) {
155 case AF_INET:
156 if (flags & 1)
157 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
158 if (flags & 2)
159 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
160 break;
161 case AF_INET6:
162 if (flags & 1)
163 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
164 if (flags & 2)
165 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
166 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100167 default:
168 /* we don't want to try to bind to an unknown address family */
169 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100170 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100171 }
172
Simon Hormande072bd2011-06-24 15:11:37 +0900173 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100174 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200175 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100176 if (ret < 0)
177 return 2;
178 }
179 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200180 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100181 if (ret < 0)
182 return 1;
183 }
184
185 if (!flags)
186 return 0;
187
188#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100189 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100190 struct in_tproxy itp1, itp2;
191 memset(&itp1, 0, sizeof(itp1));
192
193 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100194 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
195 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100196
197 /* set connect flag on socket */
198 itp2.op = TPROXY_FLAGS;
199 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
200
201 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
202 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
203 foreign_ok = 1;
204 }
205 }
206#endif
207 if (!foreign_ok)
208 /* we could not bind to a foreign address */
209 return 2;
210
211 return 0;
212}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100213
Willy Tarreau9650f372009-08-16 14:02:45 +0200214
215/*
Willy Tarreauac825402011-03-04 22:04:29 +0100216 * This function initiates a connection to the target assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200217 * (si->{target,addr.to}). A source address may be pointed to by si->addr.from
Willy Tarreauac825402011-03-04 22:04:29 +0100218 * in case of transparent proxying. Normal source bind addresses are still
219 * determined locally (due to the possible need of a source port).
220 * si->target may point either to a valid server or to a backend, depending
221 * on si->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are supported.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200222 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200223 * It can return one of :
224 * - SN_ERR_NONE if everything's OK
225 * - SN_ERR_SRVTO if there are no more servers
226 * - SN_ERR_SRVCL if the connection was refused by the server
227 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
228 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
229 * - SN_ERR_INTERNAL for any other purely internal errors
230 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
231 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100232
David du Colombier6f5ccb12011-03-10 22:26:24 +0100233int tcp_connect_server(struct stream_interface *si)
Willy Tarreau9650f372009-08-16 14:02:45 +0200234{
235 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100236 struct server *srv;
237 struct proxy *be;
238
239 switch (si->target.type) {
240 case TARG_TYPE_PROXY:
241 be = si->target.ptr.p;
242 srv = NULL;
243 break;
244 case TARG_TYPE_SERVER:
245 srv = si->target.ptr.s;
246 be = srv->proxy;
247 break;
248 default:
249 return SN_ERR_INTERNAL;
250 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200251
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200252 if ((fd = si->conn.t.sock.fd = socket(si->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200253 qfprintf(stderr, "Cannot get a server socket.\n");
254
255 if (errno == ENFILE)
256 send_log(be, LOG_EMERG,
257 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
258 be->id, maxfd);
259 else if (errno == EMFILE)
260 send_log(be, LOG_EMERG,
261 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
262 be->id, maxfd);
263 else if (errno == ENOBUFS || errno == ENOMEM)
264 send_log(be, LOG_EMERG,
265 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
266 be->id, maxfd);
267 /* this is a resource error */
268 return SN_ERR_RESOURCE;
269 }
270
271 if (fd >= global.maxsock) {
272 /* do not log anything there, it's a normal condition when this option
273 * is used to serialize connections to a server !
274 */
275 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
276 close(fd);
277 return SN_ERR_PRXCOND; /* it is a configuration limit */
278 }
279
280 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900281 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200282 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
283 close(fd);
284 return SN_ERR_INTERNAL;
285 }
286
287 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900288 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200289
290 if (be->options & PR_O_TCP_NOLING)
Willy Tarreauf6f82252011-11-30 18:02:24 +0100291 si->flags |= SI_FL_NOLINGER;
Willy Tarreau9650f372009-08-16 14:02:45 +0200292
293 /* allow specific binding :
294 * - server-specific at first
295 * - proxy-specific next
296 */
297 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200298 int ret, flags = 0;
299
Willy Tarreau9650f372009-08-16 14:02:45 +0200300 switch (srv->state & SRV_TPROXY_MASK) {
301 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200302 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200303 flags = 3;
304 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200305 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200306 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200307 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200308 break;
309 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200310
Willy Tarreau9650f372009-08-16 14:02:45 +0200311#ifdef SO_BINDTODEVICE
312 /* Note: this might fail if not CAP_NET_RAW */
313 if (srv->iface_name)
314 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
315#endif
316
317 if (srv->sport_range) {
318 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100319 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200320
321 ret = 1;
322 src = srv->source_addr;
323
324 do {
325 /* note: in case of retry, we may have to release a previously
326 * allocated port, hence this loop's construct.
327 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200328 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
329 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200330
331 if (!attempts)
332 break;
333 attempts--;
334
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200335 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
336 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200337 break;
338
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200339 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200340 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200341
Willy Tarreau6471afb2011-09-23 10:54:59 +0200342 ret = tcp_bind_socket(fd, flags, &src, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200343 } while (ret != 0); /* binding NOK */
344 }
345 else {
Willy Tarreau6471afb2011-09-23 10:54:59 +0200346 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200347 }
348
349 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200350 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
351 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 close(fd);
353
354 if (ret == 1) {
355 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
356 be->id, srv->id);
357 send_log(be, LOG_EMERG,
358 "Cannot bind to source address before connect() for server %s/%s.\n",
359 be->id, srv->id);
360 } else {
361 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
362 be->id, srv->id);
363 send_log(be, LOG_EMERG,
364 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
365 be->id, srv->id);
366 }
367 return SN_ERR_RESOURCE;
368 }
369 }
370 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200371 int ret, flags = 0;
372
Willy Tarreau9650f372009-08-16 14:02:45 +0200373 switch (be->options & PR_O_TPXY_MASK) {
374 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200376 flags = 3;
377 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200378 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200379 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200380 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200381 break;
382 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200383
Willy Tarreau9650f372009-08-16 14:02:45 +0200384#ifdef SO_BINDTODEVICE
385 /* Note: this might fail if not CAP_NET_RAW */
386 if (be->iface_name)
387 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
388#endif
Willy Tarreau6471afb2011-09-23 10:54:59 +0200389 ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200390 if (ret) {
391 close(fd);
392 if (ret == 1) {
393 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
394 be->id);
395 send_log(be, LOG_EMERG,
396 "Cannot bind to source address before connect() for proxy %s.\n",
397 be->id);
398 } else {
399 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
400 be->id);
401 send_log(be, LOG_EMERG,
402 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
403 be->id);
404 }
405 return SN_ERR_RESOURCE;
406 }
407 }
408
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400409#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200410 /* disabling tcp quick ack now allows the first request to leave the
411 * machine with the first ACK. We only do this if there are pending
412 * data in the buffer.
413 */
Willy Tarreau572bf902012-07-02 17:01:20 +0200414 if ((be->options2 & PR_O2_SMARTCON) && si->ob->buf.o)
Simon Hormande072bd2011-06-24 15:11:37 +0900415 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200416#endif
417
Willy Tarreaue803de22010-01-21 17:43:04 +0100418 if (global.tune.server_sndbuf)
419 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
420
421 if (global.tune.server_rcvbuf)
422 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
423
Willy Tarreau9b061e32012-04-07 18:03:52 +0200424 si->flags &= ~SI_FL_FROM_SET;
Willy Tarreau96596ae2012-06-08 22:57:36 +0200425
426 si->conn.peeraddr = (struct sockaddr *)&si->addr.to;
427 si->conn.peerlen = get_addr_len(&si->addr.to);
428
429 if ((connect(fd, si->conn.peeraddr, si->conn.peerlen) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200430 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
431
432 if (errno == EAGAIN || errno == EADDRINUSE) {
433 char *msg;
434 if (errno == EAGAIN) /* no free ports left, try again later */
435 msg = "no free ports";
436 else
437 msg = "local address already in use";
438
439 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200440 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
441 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 close(fd);
443 send_log(be, LOG_EMERG,
444 "Connect() failed for server %s/%s: %s.\n",
445 be->id, srv->id, msg);
446 return SN_ERR_RESOURCE;
447 } else if (errno == ETIMEDOUT) {
448 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200449 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
450 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200451 close(fd);
452 return SN_ERR_SRVTO;
453 } else {
454 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
455 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200456 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
457 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 close(fd);
459 return SN_ERR_SRVCL;
460 }
461 }
462
William Lallemandb7ff6a32012-03-02 14:35:21 +0100463 /* needs src ip/port for logging */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200464 if (si->flags & SI_FL_SRC_ADDR)
Willy Tarreau59b94792012-05-11 16:16:40 +0200465 si_get_from_addr(si);
William Lallemandb7ff6a32012-03-02 14:35:21 +0100466
Willy Tarreau80184712012-07-06 14:54:49 +0200467 fdtab[fd].owner = &si->conn;
Willy Tarreau9650f372009-08-16 14:02:45 +0200468 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau505e34a2012-07-06 10:17:53 +0200469 si->conn.flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufd31e532012-07-23 18:24:25 +0200470 si->conn.flags |= CO_FL_NOTIFY_SI; /* we're on a stream_interface */
Willy Tarreau9650f372009-08-16 14:02:45 +0200471
Willy Tarreau076be252012-07-06 16:02:29 +0200472 /* Prepare to send a few handshakes related to the on-wire protocol. */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200473 if (si->send_proxy_ofs)
474 si->conn.flags |= CO_FL_SI_SEND_PROXY;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200475
Willy Tarreaud2274c62012-07-06 14:29:45 +0200476 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200477 fd_insert(fd);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200478 conn_sock_want_send(&si->conn); /* for connect status */
479 if (!(si->ob->flags & BF_OUT_EMPTY))
480 conn_data_want_send(&si->conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200481
482 si->state = SI_ST_CON;
483 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
484 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
485
486 return SN_ERR_NONE; /* connection is OK */
487}
488
489
Willy Tarreau59b94792012-05-11 16:16:40 +0200490/*
491 * Retrieves the source address for the socket <fd>, with <dir> indicating
492 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
493 * success, -1 in case of error. The socket's source address is stored in
494 * <sa> for <salen> bytes.
495 */
496int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
497{
498 if (dir)
499 return getsockname(fd, sa, &salen);
500 else
501 return getpeername(fd, sa, &salen);
502}
503
504
505/*
506 * Retrieves the original destination address for the socket <fd>, with <dir>
507 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
508 * listener, if the original destination address was translated, the original
509 * address is retrieved. It returns 0 in case of success, -1 in case of error.
510 * The socket's source address is stored in <sa> for <salen> bytes.
511 */
512int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
513{
514 if (dir)
515 return getpeername(fd, sa, &salen);
516#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
517 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
518 return 0;
519#endif
520 else
521 return getsockname(fd, sa, &salen);
522}
523
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200524/* This is the callback which is set when a connection establishment is pending
525 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200526 * once the connection is established. It updates the FD polling status. It
527 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
528 * it returns non-zero and removes itself from the connection's flags (the bit is
529 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200530 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200531int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200532{
Willy Tarreau239d7182012-07-23 18:53:03 +0200533 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200534
Willy Tarreau80184712012-07-06 14:54:49 +0200535 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200536 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200537
Willy Tarreau80184712012-07-06 14:54:49 +0200538 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200539 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200540
Willy Tarreau2da156f2012-07-23 15:07:23 +0200541 /* stop here if we reached the end of data */
542 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
543 goto out_error;
544
Willy Tarreau2c6be842012-07-06 17:12:34 +0200545 /* We have no data to send to check the connection, and
546 * getsockopt() will not inform us whether the connection
547 * is still pending. So we'll reuse connect() to check the
548 * state of the socket. This has the advantage of giving us
549 * the following info :
550 * - error
551 * - connecting (EALREADY, EINPROGRESS)
552 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200553 */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200554 if ((connect(fd, conn->peeraddr, conn->peerlen) < 0)) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200555 if (errno == EALREADY || errno == EINPROGRESS) {
556 conn_sock_stop_recv(conn);
557 conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200558 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200559 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200560
Willy Tarreau2c6be842012-07-06 17:12:34 +0200561 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200562 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200563
Willy Tarreau2c6be842012-07-06 17:12:34 +0200564 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200565 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200566
Willy Tarreau076be252012-07-06 16:02:29 +0200567 /* The FD is ready now, we'll mark the connection as complete and
568 * forward the event to the data layer which will update the stream
569 * interface flags.
Willy Tarreaua190d592012-05-20 18:35:19 +0200570 */
Willy Tarreau80184712012-07-06 14:54:49 +0200571 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200572 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200573
574 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200575 /* Write error on the file descriptor. Report it to the connection
576 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200577 */
578
Willy Tarreau80184712012-07-06 14:54:49 +0200579 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200580 conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200581 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200582}
583
Willy Tarreau59b94792012-05-11 16:16:40 +0200584
Willy Tarreaue6b98942007-10-29 01:09:36 +0100585/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
586 * an error message in <err> if the message is at most <errlen> bytes long
587 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
588 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
589 * was alright and that no message was returned. ERR_RETRYABLE means that an
590 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700591 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100592 * the meaning of the error, but just indicate that a message is present which
593 * should be displayed with the respective level. Last, ERR_ABORT indicates
594 * that it's pointless to try to start other listeners. No error message is
595 * returned if errlen is NULL.
596 */
597int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
598{
599 __label__ tcp_return, tcp_close_return;
600 int fd, err;
601 const char *msg = NULL;
602
603 /* ensure we never return garbage */
604 if (errmsg && errlen)
605 *errmsg = 0;
606
607 if (listener->state != LI_ASSIGNED)
608 return ERR_NONE; /* already bound */
609
610 err = ERR_NONE;
611
612 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
613 err |= ERR_RETRYABLE | ERR_ALERT;
614 msg = "cannot create listening socket";
615 goto tcp_return;
616 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100617
Willy Tarreaue6b98942007-10-29 01:09:36 +0100618 if (fd >= global.maxsock) {
619 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
620 msg = "not enough free sockets (raise '-n' parameter)";
621 goto tcp_close_return;
622 }
623
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200624 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100625 err |= ERR_FATAL | ERR_ALERT;
626 msg = "cannot make socket non-blocking";
627 goto tcp_close_return;
628 }
629
Simon Hormande072bd2011-06-24 15:11:37 +0900630 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100631 /* not fatal but should be reported */
632 msg = "cannot do so_reuseaddr";
633 err |= ERR_ALERT;
634 }
635
636 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900637 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100638
Willy Tarreaue6b98942007-10-29 01:09:36 +0100639#ifdef SO_REUSEPORT
640 /* OpenBSD supports this. As it's present in old libc versions of Linux,
641 * it might return an error that we will silently ignore.
642 */
Simon Hormande072bd2011-06-24 15:11:37 +0900643 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100644#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100645#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200646 if (listener->options & LI_O_FOREIGN) {
647 switch (listener->addr.ss_family) {
648 case AF_INET:
649 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
650 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
651 msg = "cannot make listening socket transparent";
652 err |= ERR_ALERT;
653 }
654 break;
655 case AF_INET6:
656 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
657 msg = "cannot make listening socket transparent";
658 err |= ERR_ALERT;
659 }
660 break;
661 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100662 }
663#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100664#ifdef SO_BINDTODEVICE
665 /* Note: this might fail if not CAP_NET_RAW */
666 if (listener->interface) {
667 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100668 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100669 msg = "cannot bind listener to device";
670 err |= ERR_WARN;
671 }
672 }
673#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400674#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100675 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400676 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200677 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
678 msg = "cannot set MSS";
679 err |= ERR_WARN;
680 }
681 }
682#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200683#if defined(TCP_DEFER_ACCEPT)
684 if (listener->options & LI_O_DEF_ACCEPT) {
685 /* defer accept by up to one second */
686 int accept_delay = 1;
687 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
688 msg = "cannot enable DEFER_ACCEPT";
689 err |= ERR_WARN;
690 }
691 }
692#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100693 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
694 err |= ERR_RETRYABLE | ERR_ALERT;
695 msg = "cannot bind socket";
696 goto tcp_close_return;
697 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100698
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100699 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100700 err |= ERR_RETRYABLE | ERR_ALERT;
701 msg = "cannot listen to socket";
702 goto tcp_close_return;
703 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100704
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400705#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200706 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900707 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200708#endif
709
Willy Tarreaue6b98942007-10-29 01:09:36 +0100710 /* the socket is ready */
711 listener->fd = fd;
712 listener->state = LI_LISTEN;
713
Willy Tarreaueabf3132008-08-29 23:36:51 +0200714 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaueb472682010-05-28 18:46:57 +0200715 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200716 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200717 fd_insert(fd);
718
Willy Tarreaue6b98942007-10-29 01:09:36 +0100719 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100720 if (msg && errlen) {
721 char pn[INET6_ADDRSTRLEN];
722
Willy Tarreau631f01c2011-09-05 00:36:48 +0200723 addr_to_str(&listener->addr, pn, sizeof(pn));
724 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100725 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100726 return err;
727
728 tcp_close_return:
729 close(fd);
730 goto tcp_return;
731}
732
733/* This function creates all TCP sockets bound to the protocol entry <proto>.
734 * It is intended to be used as the protocol's bind_all() function.
735 * The sockets will be registered but not added to any fd_set, in order not to
736 * loose them across the fork(). A call to enable_all_listeners() is needed
737 * to complete initialization. The return value is composed from ERR_*.
738 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200739static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100740{
741 struct listener *listener;
742 int err = ERR_NONE;
743
744 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200745 err |= tcp_bind_listener(listener, errmsg, errlen);
746 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100747 break;
748 }
749
750 return err;
751}
752
753/* Add listener to the list of tcpv4 listeners. The listener's state
754 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
755 * listeners is updated. This is the function to use to add a new listener.
756 */
757void tcpv4_add_listener(struct listener *listener)
758{
759 if (listener->state != LI_INIT)
760 return;
761 listener->state = LI_ASSIGNED;
762 listener->proto = &proto_tcpv4;
763 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
764 proto_tcpv4.nb_listeners++;
765}
766
767/* Add listener to the list of tcpv4 listeners. The listener's state
768 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
769 * listeners is updated. This is the function to use to add a new listener.
770 */
771void tcpv6_add_listener(struct listener *listener)
772{
773 if (listener->state != LI_INIT)
774 return;
775 listener->state = LI_ASSIGNED;
776 listener->proto = &proto_tcpv6;
777 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
778 proto_tcpv6.nb_listeners++;
779}
780
Willy Tarreauedcf6682008-11-30 23:15:34 +0100781/* This function performs the TCP request analysis on the current request. It
782 * returns 1 if the processing can continue on next analysers, or zero if it
783 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200784 * request. It relies on buffers flags, and updates s->req->analysers. The
785 * function may be called for frontend rules and backend rules. It only relies
786 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100787 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200788int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100789{
790 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200791 struct stksess *ts;
792 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100793 int partial;
794
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100795 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 +0100796 now_ms, __FUNCTION__,
797 s,
798 req,
799 req->rex, req->wex,
800 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100801 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100802 req->analysers);
803
Willy Tarreauedcf6682008-11-30 23:15:34 +0100804 /* We don't know whether we have enough data, so must proceed
805 * this way :
806 * - iterate through all rules in their declaration order
807 * - if one rule returns MISS, it means the inspect delay is
808 * not over yet, then return immediately, otherwise consider
809 * it as a non-match.
810 * - if one rule returns OK, then return OK
811 * - if one rule returns KO, then return KO
812 */
813
Willy Tarreaub824b002010-09-29 16:36:16 +0200814 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 +0200815 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100816 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200817 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100818
Willy Tarreaufb356202010-08-03 14:02:05 +0200819 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100820 int ret = ACL_PAT_PASS;
821
822 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200823 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100824 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200825 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100826 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200827 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
828 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100829 return 0;
830 }
831
832 ret = acl_pass(ret);
833 if (rule->cond->pol == ACL_COND_UNLESS)
834 ret = !ret;
835 }
836
837 if (ret) {
838 /* we have a matching rule. */
839 if (rule->action == TCP_ACT_REJECT) {
840 buffer_abort(req);
841 buffer_abort(s->rep);
842 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200843
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100844 s->be->be_counters.denied_req++;
845 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200846 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200847 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200848
Willy Tarreauedcf6682008-11-30 23:15:34 +0100849 if (!(s->flags & SN_ERR_MASK))
850 s->flags |= SN_ERR_PRXCOND;
851 if (!(s->flags & SN_FINST_MASK))
852 s->flags |= SN_FINST_R;
853 return 0;
854 }
Willy Tarreau56123282010-08-06 19:06:56 +0200855 else if (rule->action == TCP_ACT_TRK_SC1) {
856 if (!s->stkctr1_entry) {
857 /* only the first valid track-sc1 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;
David du Colombier4f92d322011-03-24 11:09:31 +0100863 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200864 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200865 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200866 if (s->fe != s->be)
867 s->flags |= SN_BE_TRACK_SC1;
868 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200869 }
870 }
Willy Tarreau56123282010-08-06 19:06:56 +0200871 else if (rule->action == TCP_ACT_TRK_SC2) {
872 if (!s->stkctr2_entry) {
873 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200874 * Also, note that right now we can only track SRC so we
875 * don't check how to get the key, but later we may need
876 * to consider rule->act_prm->trk_ctr.type.
877 */
878 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100879 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200880 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200881 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200882 if (s->fe != s->be)
883 s->flags |= SN_BE_TRACK_SC2;
884 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200885 }
886 }
887 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100888 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200889 break;
890 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100891 }
892 }
893
894 /* if we get there, it means we have no rule which matches, or
895 * we have an explicit accept, so we apply the default accept.
896 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200897 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100898 req->analyse_exp = TICK_ETERNITY;
899 return 1;
900}
901
Emeric Brun97679e72010-09-23 17:56:44 +0200902/* This function performs the TCP response analysis on the current response. It
903 * returns 1 if the processing can continue on next analysers, or zero if it
904 * needs more data, encounters an error, or wants to immediately abort the
905 * response. It relies on buffers flags, and updates s->rep->analysers. The
906 * function may be called for backend rules.
907 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200908int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200909{
910 struct tcp_rule *rule;
911 int partial;
912
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100913 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 +0200914 now_ms, __FUNCTION__,
915 s,
916 rep,
917 rep->rex, rep->wex,
918 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100919 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200920 rep->analysers);
921
922 /* We don't know whether we have enough data, so must proceed
923 * this way :
924 * - iterate through all rules in their declaration order
925 * - if one rule returns MISS, it means the inspect delay is
926 * not over yet, then return immediately, otherwise consider
927 * it as a non-match.
928 * - if one rule returns OK, then return OK
929 * - if one rule returns KO, then return KO
930 */
931
932 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200933 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200934 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200935 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200936
937 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
938 int ret = ACL_PAT_PASS;
939
940 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200941 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200942 if (ret == ACL_PAT_MISS) {
943 /* just set the analyser timeout once at the beginning of the response */
944 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
945 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
946 return 0;
947 }
948
949 ret = acl_pass(ret);
950 if (rule->cond->pol == ACL_COND_UNLESS)
951 ret = !ret;
952 }
953
954 if (ret) {
955 /* we have a matching rule. */
956 if (rule->action == TCP_ACT_REJECT) {
957 buffer_abort(rep);
958 buffer_abort(s->req);
959 rep->analysers = 0;
960
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100961 s->be->be_counters.denied_resp++;
962 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200963 if (s->listener->counters)
964 s->listener->counters->denied_resp++;
965
966 if (!(s->flags & SN_ERR_MASK))
967 s->flags |= SN_ERR_PRXCOND;
968 if (!(s->flags & SN_FINST_MASK))
969 s->flags |= SN_FINST_D;
970 return 0;
971 }
972 else {
973 /* otherwise accept */
974 break;
975 }
976 }
977 }
978
979 /* if we get there, it means we have no rule which matches, or
980 * we have an explicit accept, so we apply the default accept.
981 */
982 rep->analysers &= ~an_bit;
983 rep->analyse_exp = TICK_ETERNITY;
984 return 1;
985}
986
987
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200988/* This function performs the TCP layer4 analysis on the current request. It
989 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
990 * matches or if no more rule matches. It can only use rules which don't need
991 * any data.
992 */
993int tcp_exec_req_rules(struct session *s)
994{
995 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200996 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200997 struct stktable *t = NULL;
998 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200999 int ret;
1000
1001 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1002 ret = ACL_PAT_PASS;
1003
1004 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001005 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001006 ret = acl_pass(ret);
1007 if (rule->cond->pol == ACL_COND_UNLESS)
1008 ret = !ret;
1009 }
1010
1011 if (ret) {
1012 /* we have a matching rule. */
1013 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001014 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001015 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001016 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001017
1018 if (!(s->flags & SN_ERR_MASK))
1019 s->flags |= SN_ERR_PRXCOND;
1020 if (!(s->flags & SN_FINST_MASK))
1021 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001022 result = 0;
1023 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001024 }
Willy Tarreau56123282010-08-06 19:06:56 +02001025 else if (rule->action == TCP_ACT_TRK_SC1) {
1026 if (!s->stkctr1_entry) {
1027 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001028 * Also, note that right now we can only track SRC so we
1029 * don't check how to get the key, but later we may need
1030 * to consider rule->act_prm->trk_ctr.type.
1031 */
1032 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001033 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001034 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001035 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001036 }
1037 }
Willy Tarreau56123282010-08-06 19:06:56 +02001038 else if (rule->action == TCP_ACT_TRK_SC2) {
1039 if (!s->stkctr2_entry) {
1040 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001041 * Also, note that right now we can only track SRC so we
1042 * don't check how to get the key, but later we may need
1043 * to consider rule->act_prm->trk_ctr.type.
1044 */
1045 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001046 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001047 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001048 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001049 }
1050 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001051 else {
1052 /* otherwise it's an accept */
1053 break;
1054 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001055 }
1056 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001057 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001058}
1059
Emeric Brun97679e72010-09-23 17:56:44 +02001060/* Parse a tcp-response rule. Return a negative value in case of failure */
1061static int tcp_parse_response_rule(char **args, int arg, int section_type,
1062 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001063 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001064{
1065 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001066 memprintf(err, "%s %s is only allowed in 'backend' sections",
1067 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001068 return -1;
1069 }
1070
1071 if (strcmp(args[arg], "accept") == 0) {
1072 arg++;
1073 rule->action = TCP_ACT_ACCEPT;
1074 }
1075 else if (strcmp(args[arg], "reject") == 0) {
1076 arg++;
1077 rule->action = TCP_ACT_REJECT;
1078 }
1079 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001080 memprintf(err,
1081 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1082 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001083 return -1;
1084 }
1085
1086 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001087 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1088 memprintf(err,
1089 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1090 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001091 return -1;
1092 }
1093 }
1094 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001095 memprintf(err,
1096 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001097 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1098 return -1;
1099 }
1100 return 0;
1101}
1102
1103
1104
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001105/* Parse a tcp-request rule. Return a negative value in case of failure */
1106static int tcp_parse_request_rule(char **args, int arg, int section_type,
1107 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001108 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001109{
1110 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001111 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1112 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001113 return -1;
1114 }
1115
1116 if (!strcmp(args[arg], "accept")) {
1117 arg++;
1118 rule->action = TCP_ACT_ACCEPT;
1119 }
1120 else if (!strcmp(args[arg], "reject")) {
1121 arg++;
1122 rule->action = TCP_ACT_REJECT;
1123 }
Willy Tarreau56123282010-08-06 19:06:56 +02001124 else if (strcmp(args[arg], "track-sc1") == 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_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001139 }
Willy Tarreau56123282010-08-06 19:06:56 +02001140 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001141 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001142 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001143
1144 arg++;
1145 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001146 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001147
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001148 if (ret < 0) { /* nb: warnings are not handled yet */
1149 memprintf(err,
1150 "'%s %s %s' : %s in %s '%s'",
1151 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1152 return ret;
1153 }
Willy Tarreau56123282010-08-06 19:06:56 +02001154 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001155 }
1156 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001157 memprintf(err,
1158 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1159 "or 'track-sc2' in %s '%s' (got '%s')",
1160 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001161 return -1;
1162 }
1163
1164 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001165 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1166 memprintf(err,
1167 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1168 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001169 return -1;
1170 }
1171 }
1172 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001173 memprintf(err,
1174 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001175 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1176 return -1;
1177 }
1178 return 0;
1179}
1180
Emeric Brun97679e72010-09-23 17:56:44 +02001181/* This function should be called to parse a line starting with the "tcp-response"
1182 * keyword.
1183 */
1184static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001185 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001186{
1187 const char *ptr = NULL;
1188 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001189 int warn = 0;
1190 int arg;
1191 struct tcp_rule *rule;
1192
1193 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001194 memprintf(err, "missing argument for '%s' in %s '%s'",
1195 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001196 return -1;
1197 }
1198
1199 if (strcmp(args[1], "inspect-delay") == 0) {
1200 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001201 memprintf(err, "%s %s is only allowed in 'backend' sections",
1202 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001203 return -1;
1204 }
1205
1206 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001207 memprintf(err,
1208 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1209 args[0], args[1], proxy_type_str(curpx), curpx->id);
1210 if (ptr)
1211 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001212 return -1;
1213 }
1214
1215 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001216 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1217 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001218 return 1;
1219 }
1220 curpx->tcp_rep.inspect_delay = val;
1221 return 0;
1222 }
1223
Simon Hormande072bd2011-06-24 15:11:37 +09001224 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001225 LIST_INIT(&rule->list);
1226 arg = 1;
1227
1228 if (strcmp(args[1], "content") == 0) {
1229 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001230 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001231 goto error;
1232
1233 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1234 struct acl *acl;
1235 const char *name;
1236
1237 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1238 name = acl ? acl->name : "(unknown)";
1239
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001240 memprintf(err,
1241 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1242 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001243 warn++;
1244 }
1245
1246 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1247 }
1248 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001249 memprintf(err,
1250 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1251 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001252 goto error;
1253 }
1254
1255 return warn;
1256 error:
1257 free(rule);
1258 return -1;
1259}
1260
1261
Willy Tarreaub6866442008-07-14 23:54:42 +02001262/* This function should be called to parse a line starting with the "tcp-request"
1263 * keyword.
1264 */
1265static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001266 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001267{
1268 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001269 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001270 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001271 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001272 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001273
1274 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001275 if (curpx == defpx)
1276 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1277 else
1278 memprintf(err, "missing argument for '%s' in %s '%s'",
1279 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001280 return -1;
1281 }
1282
1283 if (!strcmp(args[1], "inspect-delay")) {
1284 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001285 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1286 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001287 return -1;
1288 }
1289
Willy Tarreaub6866442008-07-14 23:54:42 +02001290 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001291 memprintf(err,
1292 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1293 args[0], args[1], proxy_type_str(curpx), curpx->id);
1294 if (ptr)
1295 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001296 return -1;
1297 }
1298
1299 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001300 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1301 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001302 return 1;
1303 }
1304 curpx->tcp_req.inspect_delay = val;
1305 return 0;
1306 }
1307
Simon Hormande072bd2011-06-24 15:11:37 +09001308 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001309 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001310 arg = 1;
1311
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001312 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001313 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001314 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001315 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001316
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001317 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001318 struct acl *acl;
1319 const char *name;
1320
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001321 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001322 name = acl ? acl->name : "(unknown)";
1323
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001324 memprintf(err,
1325 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1326 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001327 warn++;
1328 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001329 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001330 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001331 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001332 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001333
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001334 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001335 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1336 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001337 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001338 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001339
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001340 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001341 goto error;
1342
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001343 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1344 struct acl *acl;
1345 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001346
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001347 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1348 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001349
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001350 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001351 memprintf(err,
1352 "'%s %s' may not reference acl '%s' which makes use of "
1353 "payload in %s '%s'. Please use '%s content' for this.",
1354 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001355 goto error;
1356 }
1357 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001358 memprintf(err,
1359 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1360 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001361 warn++;
1362 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001363 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001364 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001365 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001366 if (curpx == defpx)
1367 memprintf(err,
1368 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1369 args[0], args[1]);
1370 else
1371 memprintf(err,
1372 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1373 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001374 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001375 }
1376
Willy Tarreau1a687942010-05-23 22:40:30 +02001377 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001378 error:
1379 free(rule);
1380 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001381}
1382
Willy Tarreau645513a2010-05-24 20:55:15 +02001383
1384/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001385/* All supported sample fetch functios must be declared here */
1386/************************************************************************/
1387
1388/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001389 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001390 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1391 * is a string (cookie name), other types will lead to undefined behaviour.
1392 */
1393int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001394smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001395 const struct arg *args, struct sample *smp)
1396{
1397 int bleft;
1398 const unsigned char *data;
1399
1400 if (!l4 || !l4->req)
1401 return 0;
1402
1403 smp->flags = 0;
1404 smp->type = SMP_T_CSTR;
1405
Willy Tarreau572bf902012-07-02 17:01:20 +02001406 bleft = l4->req->buf.i;
Willy Tarreau32389b72012-04-23 23:13:20 +02001407 if (bleft <= 11)
1408 goto too_short;
1409
Willy Tarreau572bf902012-07-02 17:01:20 +02001410 data = (const unsigned char *)l4->req->buf.p + 11;
Willy Tarreau32389b72012-04-23 23:13:20 +02001411 bleft -= 11;
1412
1413 if (bleft <= 7)
1414 goto too_short;
1415
1416 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1417 goto not_cookie;
1418
1419 data += 7;
1420 bleft -= 7;
1421
1422 while (bleft > 0 && *data == ' ') {
1423 data++;
1424 bleft--;
1425 }
1426
1427 if (args) {
1428
1429 if (bleft <= args->data.str.len)
1430 goto too_short;
1431
1432 if ((data[args->data.str.len] != '=') ||
1433 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1434 goto not_cookie;
1435
1436 data += args->data.str.len + 1;
1437 bleft -= args->data.str.len + 1;
1438 } else {
1439 while (bleft > 0 && *data != '=') {
1440 if (*data == '\r' || *data == '\n')
1441 goto not_cookie;
1442 data++;
1443 bleft--;
1444 }
1445
1446 if (bleft < 1)
1447 goto too_short;
1448
1449 if (*data != '=')
1450 goto not_cookie;
1451
1452 data++;
1453 bleft--;
1454 }
1455
1456 /* data points to cookie value */
1457 smp->data.str.str = (char *)data;
1458 smp->data.str.len = 0;
1459
1460 while (bleft > 0 && *data != '\r') {
1461 data++;
1462 bleft--;
1463 }
1464
1465 if (bleft < 2)
1466 goto too_short;
1467
1468 if (data[0] != '\r' || data[1] != '\n')
1469 goto not_cookie;
1470
1471 smp->data.str.len = (char *)data - smp->data.str.str;
1472 smp->flags = SMP_F_VOLATILE;
1473 return 1;
1474
1475 too_short:
1476 smp->flags = SMP_F_MAY_CHANGE;
1477 not_cookie:
1478 return 0;
1479}
1480
Willy Tarreau32389b72012-04-23 23:13:20 +02001481/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001482/* All supported ACL keywords must be declared here. */
1483/************************************************************************/
1484
Willy Tarreau32389b72012-04-23 23:13:20 +02001485/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1486static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001487acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001488 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001489{
1490 int ret;
1491
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001492 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001493
1494 if (smp->flags & SMP_F_MAY_CHANGE)
1495 return 0;
1496
1497 smp->flags = SMP_F_VOLATILE;
1498 smp->type = SMP_T_UINT;
1499 smp->data.uint = ret;
1500 return 1;
1501}
1502
1503
Willy Tarreau4a129812012-04-25 17:31:42 +02001504/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001505static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001506smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001507 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001508{
Willy Tarreauf4362b32011-12-16 17:49:52 +01001509 switch (l4->si[0].addr.from.ss_family) {
1510 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001511 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.from)->sin_addr;
1512 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001513 break;
1514 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001515 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.from))->sin6_addr;
1516 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001517 break;
1518 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001519 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001520 }
Emeric Brunf769f512010-10-22 17:14:01 +02001521
Willy Tarreau37406352012-04-23 16:16:37 +02001522 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001523 return 1;
1524}
1525
Willy Tarreaua5e37562011-12-16 17:06:15 +01001526/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001527static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001528smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001529 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001530{
Willy Tarreauf853c462012-04-23 18:53:56 +02001531 smp->type = SMP_T_UINT;
1532 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001533 return 0;
1534
Willy Tarreau37406352012-04-23 16:16:37 +02001535 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001536 return 1;
1537}
1538
Willy Tarreau4a129812012-04-25 17:31:42 +02001539/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001540static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001541smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001542 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001543{
Willy Tarreau59b94792012-05-11 16:16:40 +02001544 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001545
Willy Tarreauf4362b32011-12-16 17:49:52 +01001546 switch (l4->si[0].addr.to.ss_family) {
1547 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001548 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.to)->sin_addr;
1549 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001550 break;
1551 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001552 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.to))->sin6_addr;
1553 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001554 break;
1555 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001556 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001557 }
Emeric Brunf769f512010-10-22 17:14:01 +02001558
Willy Tarreau37406352012-04-23 16:16:37 +02001559 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001560 return 1;
1561}
1562
Willy Tarreaua5e37562011-12-16 17:06:15 +01001563/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001564static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001565smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001566 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001567{
Willy Tarreau59b94792012-05-11 16:16:40 +02001568 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001569
Willy Tarreauf853c462012-04-23 18:53:56 +02001570 smp->type = SMP_T_UINT;
1571 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001572 return 0;
1573
Willy Tarreau37406352012-04-23 16:16:37 +02001574 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001575 return 1;
1576}
1577
1578static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001579smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1580 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001581{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001582 unsigned int len_offset = arg_p[0].data.uint;
1583 unsigned int len_size = arg_p[1].data.uint;
1584 unsigned int buf_offset;
1585 unsigned int buf_size = 0;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001586 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001587 int i;
1588
1589 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1590 /* by default buf offset == len offset + len size */
1591 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1592
1593 if (!l4)
1594 return 0;
1595
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001596 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001597
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001598 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001599 return 0;
1600
Willy Tarreau572bf902012-07-02 17:01:20 +02001601 if (len_offset + len_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001602 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001603
1604 for (i = 0; i < len_size; i++) {
Willy Tarreau572bf902012-07-02 17:01:20 +02001605 buf_size = (buf_size << 8) + ((unsigned char *)b->buf.p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001606 }
1607
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001608 /* buf offset may be implicit, absolute or relative */
1609 buf_offset = len_offset + len_size;
1610 if (arg_p[2].type == ARGT_UINT)
1611 buf_offset = arg_p[2].data.uint;
1612 else if (arg_p[2].type == ARGT_SINT)
1613 buf_offset += arg_p[2].data.sint;
1614
Willy Tarreau572bf902012-07-02 17:01:20 +02001615 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001616 /* will never match */
1617 smp->flags = 0;
1618 return 0;
1619 }
1620
Willy Tarreau572bf902012-07-02 17:01:20 +02001621 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001622 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001623
1624 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001625 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001626 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001627 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001628 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001629
1630 too_short:
1631 smp->flags = SMP_F_MAY_CHANGE;
1632 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001633}
1634
1635static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001636smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1637 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001638{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001639 unsigned int buf_offset = arg_p[0].data.uint;
1640 unsigned int buf_size = arg_p[1].data.uint;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001641 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001642
1643 if (!l4)
1644 return 0;
1645
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001646 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001647
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001648 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001649 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001650
Willy Tarreau572bf902012-07-02 17:01:20 +02001651 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001652 /* will never match */
1653 smp->flags = 0;
1654 return 0;
1655 }
Emericf2d7cae2010-11-05 18:13:50 +01001656
Willy Tarreau572bf902012-07-02 17:01:20 +02001657 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001658 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001659
1660 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001661 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001662 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001663 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001664 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001665
1666 too_short:
1667 smp->flags = SMP_F_MAY_CHANGE;
1668 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001669}
1670
Willy Tarreau21d68a62012-04-20 15:52:36 +02001671/* This function is used to validate the arguments passed to a "payload" fetch
1672 * keyword. This keyword expects two positive integers, with the second one
1673 * being strictly positive. It is assumed that the types are already the correct
1674 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1675 * filled with a pointer to an error message in case of error, that the caller
1676 * is responsible for freeing. The initial location must either be freeable or
1677 * NULL.
1678 */
1679static int val_payload(struct arg *arg, char **err_msg)
1680{
1681 if (!arg[1].data.uint) {
1682 if (err_msg)
1683 memprintf(err_msg, "payload length must be > 0");
1684 return 0;
1685 }
1686 return 1;
1687}
1688
1689/* This function is used to validate the arguments passed to a "payload_lv" fetch
1690 * keyword. This keyword allows two positive integers and an optional signed one,
1691 * with the second one being strictly positive and the third one being greater than
1692 * the opposite of the two others if negative. It is assumed that the types are
1693 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1694 * not NULL, it will be filled with a pointer to an error message in case of
1695 * error, that the caller is responsible for freeing. The initial location must
1696 * either be freeable or NULL.
1697 */
1698static int val_payload_lv(struct arg *arg, char **err_msg)
1699{
1700 if (!arg[1].data.uint) {
1701 if (err_msg)
1702 memprintf(err_msg, "payload length must be > 0");
1703 return 0;
1704 }
1705
1706 if (arg[2].type == ARGT_SINT &&
1707 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1708 if (err_msg)
1709 memprintf(err_msg, "payload offset too negative");
1710 return 0;
1711 }
1712 return 1;
1713}
1714
Willy Tarreaub6866442008-07-14 23:54:42 +02001715static struct cfg_kw_list cfg_kws = {{ },{
1716 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001717 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001718 { 0, NULL, NULL },
1719}};
1720
Willy Tarreau61612d42012-04-19 18:42:05 +02001721/* Note: must not be declared <const> as its list will be overwritten.
1722 * Please take care of keeping this list alphabetically sorted.
1723 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001724static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001725 { "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 +02001726 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001727 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1728 { "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 +02001729 { "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 +02001730 { "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 +02001731 { "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 +02001732 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001733 { NULL, NULL, NULL, NULL },
1734}};
1735
Willy Tarreau4a129812012-04-25 17:31:42 +02001736/* Note: must not be declared <const> as its list will be overwritten.
1737 * Note: fetches that may return multiple types must be declared as the lowest
1738 * common denominator, the type that can be casted into all other ones. For
1739 * instance v4/v6 must be declared v4.
1740 */
Willy Tarreau12785782012-04-27 21:37:17 +02001741static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001742 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001743 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001744 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001745 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1746 { "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 +02001747 { "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 +02001748 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001749 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001750}};
1751
Willy Tarreaue6b98942007-10-29 01:09:36 +01001752__attribute__((constructor))
1753static void __tcp_protocol_init(void)
1754{
1755 protocol_register(&proto_tcpv4);
1756 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001757 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001758 cfg_register_keywords(&cfg_kws);
1759 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001760}
1761
1762
1763/*
1764 * Local variables:
1765 * c-indent-level: 8
1766 * c-basic-offset: 8
1767 * End:
1768 */