blob: 551603677af887431b14467038b09db2061f69f9 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreau1a687942010-05-23 22:40:30 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040027#include <netinet/tcp.h>
28
Willy Tarreaub6866442008-07-14 23:54:42 +020029#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010036
Willy Tarreaue6b98942007-10-29 01:09:36 +010037#include <types/global.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020038#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039
40#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020041#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020044#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020046#include <proto/log.h>
47#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020048#include <proto/protocol.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010049#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020051#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020052#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/stick_table.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020054#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010055
Willy Tarreaue8c66af2008-01-13 18:40:14 +010056#ifdef CONFIG_HAP_CTTPROXY
57#include <import/ip_tproxy.h>
58#endif
59
Emeric Bruncf20bf12010-10-22 16:06:11 +020060static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
61static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010062
63/* Note: must not be declared <const> as its list will be overwritten */
64static struct protocol proto_tcpv4 = {
65 .name = "tcpv4",
66 .sock_domain = AF_INET,
67 .sock_type = SOCK_STREAM,
68 .sock_prot = IPPROTO_TCP,
69 .sock_family = AF_INET,
70 .sock_addrlen = sizeof(struct sockaddr_in),
71 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020072 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020073 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020074 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010075 .bind_all = tcp_bind_listeners,
76 .unbind_all = unbind_all_listeners,
77 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020078 .get_src = tcp_get_src,
79 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010080 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
81 .nb_listeners = 0,
82};
83
84/* Note: must not be declared <const> as its list will be overwritten */
85static struct protocol proto_tcpv6 = {
86 .name = "tcpv6",
87 .sock_domain = AF_INET6,
88 .sock_type = SOCK_STREAM,
89 .sock_prot = IPPROTO_TCP,
90 .sock_family = AF_INET6,
91 .sock_addrlen = sizeof(struct sockaddr_in6),
92 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020093 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020094 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020095 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010096 .bind_all = tcp_bind_listeners,
97 .unbind_all = unbind_all_listeners,
98 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020099 .get_src = tcp_get_src,
100 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100101 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
102 .nb_listeners = 0,
103};
104
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100105
David du Colombier6f5ccb12011-03-10 22:26:24 +0100106/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100107 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
108 * - 0 : ignore remote address (may even be a NULL pointer)
109 * - 1 : use provided address
110 * - 2 : use provided port
111 * - 3 : use both
112 *
113 * The function supports multiple foreign binding methods :
114 * - linux_tproxy: we directly bind to the foreign address
115 * - cttproxy: we bind to a local address then nat.
116 * The second one can be used as a fallback for the first one.
117 * This function returns 0 when everything's OK, 1 if it could not bind, to the
118 * local address, 2 if it could not bind to the foreign address.
119 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100120int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100121{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100122 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100123 int foreign_ok = 0;
124 int ret;
125
126#ifdef CONFIG_HAP_LINUX_TPROXY
127 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200128 static int ip6_transp_working = 1;
129 switch (local->ss_family) {
130 case AF_INET:
131 if (flags && ip_transp_working) {
132 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
133 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
134 foreign_ok = 1;
135 else
136 ip_transp_working = 0;
137 }
138 break;
139 case AF_INET6:
140 if (flags && ip6_transp_working) {
141 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
142 foreign_ok = 1;
143 else
144 ip6_transp_working = 0;
145 }
146 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100147 }
148#endif
149 if (flags) {
150 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200151 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100152 switch (remote->ss_family) {
153 case AF_INET:
154 if (flags & 1)
155 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
156 if (flags & 2)
157 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
158 break;
159 case AF_INET6:
160 if (flags & 1)
161 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
162 if (flags & 2)
163 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
164 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100165 default:
166 /* we don't want to try to bind to an unknown address family */
167 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100168 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100169 }
170
Simon Hormande072bd2011-06-24 15:11:37 +0900171 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100172 if (foreign_ok) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200173 if (is_addr(&bind_addr)) {
174 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
175 if (ret < 0)
176 return 2;
177 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100178 }
179 else {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200180 if (is_addr(local)) {
181 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
182 if (ret < 0)
183 return 1;
184 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100185 }
186
187 if (!flags)
188 return 0;
189
190#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100191 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100192 struct in_tproxy itp1, itp2;
193 memset(&itp1, 0, sizeof(itp1));
194
195 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100196 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
197 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100198
199 /* set connect flag on socket */
200 itp2.op = TPROXY_FLAGS;
201 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
202
203 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
204 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
205 foreign_ok = 1;
206 }
207 }
208#endif
209 if (!foreign_ok)
210 /* we could not bind to a foreign address */
211 return 2;
212
213 return 0;
214}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100215
Willy Tarreau9650f372009-08-16 14:02:45 +0200216
217/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200218 * This function initiates a TCP connection establishment to the target assigned
219 * to connection <conn> using (si->{target,addr.to}). A source address may be
220 * pointed to by conn->addr.from in case of transparent proxying. Normal source
221 * bind addresses are still determined locally (due to the possible need of a
222 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100223 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100224 * supported. The <data> parameter is a boolean indicating whether there are data
225 * waiting for being sent or not, in order to adjust data write polling and on
226 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
227 * allows the caller to force using a delayed ACK when establishing the connection :
228 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
229 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
230 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200231 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200232 * It can return one of :
233 * - SN_ERR_NONE if everything's OK
234 * - SN_ERR_SRVTO if there are no more servers
235 * - SN_ERR_SRVCL if the connection was refused by the server
236 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
237 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
238 * - SN_ERR_INTERNAL for any other purely internal errors
239 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100240 *
241 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
242 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200243 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100244
Willy Tarreauf0837b22012-11-24 10:24:27 +0100245int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200246{
247 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100248 struct server *srv;
249 struct proxy *be;
250
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100251 switch (obj_type(conn->target)) {
252 case OBJ_TYPE_PROXY:
253 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100254 srv = NULL;
255 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100256 case OBJ_TYPE_SERVER:
257 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100258 be = srv->proxy;
259 break;
260 default:
261 return SN_ERR_INTERNAL;
262 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200263
Willy Tarreau986a9d22012-08-30 21:11:38 +0200264 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200265 qfprintf(stderr, "Cannot get a server socket.\n");
266
267 if (errno == ENFILE)
268 send_log(be, LOG_EMERG,
269 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
270 be->id, maxfd);
271 else if (errno == EMFILE)
272 send_log(be, LOG_EMERG,
273 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
274 be->id, maxfd);
275 else if (errno == ENOBUFS || errno == ENOMEM)
276 send_log(be, LOG_EMERG,
277 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
278 be->id, maxfd);
279 /* this is a resource error */
280 return SN_ERR_RESOURCE;
281 }
282
283 if (fd >= global.maxsock) {
284 /* do not log anything there, it's a normal condition when this option
285 * is used to serialize connections to a server !
286 */
287 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
288 close(fd);
289 return SN_ERR_PRXCOND; /* it is a configuration limit */
290 }
291
292 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900293 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200294 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
295 close(fd);
296 return SN_ERR_INTERNAL;
297 }
298
299 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900300 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200301
Willy Tarreau9650f372009-08-16 14:02:45 +0200302 /* allow specific binding :
303 * - server-specific at first
304 * - proxy-specific next
305 */
306 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200307 int ret, flags = 0;
308
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200309 if (is_addr(&conn->addr.from)) {
310 switch (srv->state & SRV_TPROXY_MASK) {
311 case SRV_TPROXY_ADDR:
312 case SRV_TPROXY_CLI:
313 flags = 3;
314 break;
315 case SRV_TPROXY_CIP:
316 case SRV_TPROXY_DYN:
317 flags = 1;
318 break;
319 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200320 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200321
Willy Tarreau9650f372009-08-16 14:02:45 +0200322#ifdef SO_BINDTODEVICE
323 /* Note: this might fail if not CAP_NET_RAW */
324 if (srv->iface_name)
325 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
326#endif
327
328 if (srv->sport_range) {
329 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100330 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200331
332 ret = 1;
333 src = srv->source_addr;
334
335 do {
336 /* note: in case of retry, we may have to release a previously
337 * allocated port, hence this loop's construct.
338 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200339 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
340 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200341
342 if (!attempts)
343 break;
344 attempts--;
345
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200346 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
347 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200348 break;
349
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200350 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200351 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200352
Willy Tarreau986a9d22012-08-30 21:11:38 +0200353 ret = tcp_bind_socket(fd, flags, &src, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200354 } while (ret != 0); /* binding NOK */
355 }
356 else {
Willy Tarreau986a9d22012-08-30 21:11:38 +0200357 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200358 }
359
360 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200361 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
362 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200363 close(fd);
364
365 if (ret == 1) {
366 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
367 be->id, srv->id);
368 send_log(be, LOG_EMERG,
369 "Cannot bind to source address before connect() for server %s/%s.\n",
370 be->id, srv->id);
371 } else {
372 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
373 be->id, srv->id);
374 send_log(be, LOG_EMERG,
375 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
376 be->id, srv->id);
377 }
378 return SN_ERR_RESOURCE;
379 }
380 }
381 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200382 int ret, flags = 0;
383
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200384 if (is_addr(&conn->addr.from)) {
385 switch (be->options & PR_O_TPXY_MASK) {
386 case PR_O_TPXY_ADDR:
387 case PR_O_TPXY_CLI:
388 flags = 3;
389 break;
390 case PR_O_TPXY_CIP:
391 case PR_O_TPXY_DYN:
392 flags = 1;
393 break;
394 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200395 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200396
Willy Tarreau9650f372009-08-16 14:02:45 +0200397#ifdef SO_BINDTODEVICE
398 /* Note: this might fail if not CAP_NET_RAW */
399 if (be->iface_name)
400 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
401#endif
Willy Tarreau986a9d22012-08-30 21:11:38 +0200402 ret = tcp_bind_socket(fd, flags, &be->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200403 if (ret) {
404 close(fd);
405 if (ret == 1) {
406 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
407 be->id);
408 send_log(be, LOG_EMERG,
409 "Cannot bind to source address before connect() for proxy %s.\n",
410 be->id);
411 } else {
412 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
413 be->id);
414 send_log(be, LOG_EMERG,
415 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
416 be->id);
417 }
418 return SN_ERR_RESOURCE;
419 }
420 }
421
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400422#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200423 /* disabling tcp quick ack now allows the first request to leave the
424 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100425 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200426 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100427 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900428 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200429#endif
430
Willy Tarreaue803de22010-01-21 17:43:04 +0100431 if (global.tune.server_sndbuf)
432 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
433
434 if (global.tune.server_rcvbuf)
435 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
436
Willy Tarreau986a9d22012-08-30 21:11:38 +0200437 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
439
440 if (errno == EAGAIN || errno == EADDRINUSE) {
441 char *msg;
442 if (errno == EAGAIN) /* no free ports left, try again later */
443 msg = "no free ports";
444 else
445 msg = "local address already in use";
446
447 qfprintf(stderr,"Cannot connect: %s.\n",msg);
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 send_log(be, LOG_EMERG,
452 "Connect() failed for server %s/%s: %s.\n",
453 be->id, srv->id, msg);
454 return SN_ERR_RESOURCE;
455 } else if (errno == ETIMEDOUT) {
456 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200457 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
458 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200459 close(fd);
460 return SN_ERR_SRVTO;
461 } else {
462 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
463 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200464 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
465 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200466 close(fd);
467 return SN_ERR_SRVCL;
468 }
469 }
470
Willy Tarreau986a9d22012-08-30 21:11:38 +0200471 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200472 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200473
Willy Tarreaud2274c62012-07-06 14:29:45 +0200474 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200475 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200476 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200477
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200478 if (conn_xprt_init(conn) < 0) {
Willy Tarreau184636e2012-09-06 14:04:41 +0200479 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200480 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200481 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200482
Willy Tarreau14f8e862012-08-30 22:23:13 +0200483 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200484 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200485
Willy Tarreau9650f372009-08-16 14:02:45 +0200486 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 Tarreau986a9d22012-08-30 21:11:38 +0200554 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 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
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200568 * forward the event to the transport layer which will notify the
569 * data layer.
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 Tarreau1c862c52012-10-05 16:21:00 +0200693#if defined(TCP_FASTOPEN)
694 if (listener->options & LI_O_TCP_FO) {
695 /* TFO needs a queue length, let's use the configured backlog */
696 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
697 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
698 msg = "cannot enable TCP_FASTOPEN";
699 err |= ERR_WARN;
700 }
701 }
702#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100703#if defined(IPV6_V6ONLY)
704 if (listener->options & LI_O_V6ONLY)
705 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100706 else if (listener->options & LI_O_V4V6)
707 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100708#endif
709
Willy Tarreaue6b98942007-10-29 01:09:36 +0100710 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
711 err |= ERR_RETRYABLE | ERR_ALERT;
712 msg = "cannot bind socket";
713 goto tcp_close_return;
714 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100715
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100716 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100717 err |= ERR_RETRYABLE | ERR_ALERT;
718 msg = "cannot listen to socket";
719 goto tcp_close_return;
720 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100721
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400722#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200723 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900724 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200725#endif
726
Willy Tarreaue6b98942007-10-29 01:09:36 +0100727 /* the socket is ready */
728 listener->fd = fd;
729 listener->state = LI_LISTEN;
730
Willy Tarreaueabf3132008-08-29 23:36:51 +0200731 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200732 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200733 fd_insert(fd);
734
Willy Tarreaue6b98942007-10-29 01:09:36 +0100735 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100736 if (msg && errlen) {
737 char pn[INET6_ADDRSTRLEN];
738
Willy Tarreau631f01c2011-09-05 00:36:48 +0200739 addr_to_str(&listener->addr, pn, sizeof(pn));
740 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100741 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100742 return err;
743
744 tcp_close_return:
745 close(fd);
746 goto tcp_return;
747}
748
749/* This function creates all TCP sockets bound to the protocol entry <proto>.
750 * It is intended to be used as the protocol's bind_all() function.
751 * The sockets will be registered but not added to any fd_set, in order not to
752 * loose them across the fork(). A call to enable_all_listeners() is needed
753 * to complete initialization. The return value is composed from ERR_*.
754 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200755static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100756{
757 struct listener *listener;
758 int err = ERR_NONE;
759
760 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200761 err |= tcp_bind_listener(listener, errmsg, errlen);
762 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100763 break;
764 }
765
766 return err;
767}
768
769/* Add listener to the list of tcpv4 listeners. The listener's state
770 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
771 * listeners is updated. This is the function to use to add a new listener.
772 */
773void tcpv4_add_listener(struct listener *listener)
774{
775 if (listener->state != LI_INIT)
776 return;
777 listener->state = LI_ASSIGNED;
778 listener->proto = &proto_tcpv4;
779 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
780 proto_tcpv4.nb_listeners++;
781}
782
783/* Add listener to the list of tcpv4 listeners. The listener's state
784 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
785 * listeners is updated. This is the function to use to add a new listener.
786 */
787void tcpv6_add_listener(struct listener *listener)
788{
789 if (listener->state != LI_INIT)
790 return;
791 listener->state = LI_ASSIGNED;
792 listener->proto = &proto_tcpv6;
793 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
794 proto_tcpv6.nb_listeners++;
795}
796
Willy Tarreauedcf6682008-11-30 23:15:34 +0100797/* This function performs the TCP request analysis on the current request. It
798 * returns 1 if the processing can continue on next analysers, or zero if it
799 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200800 * request. It relies on buffers flags, and updates s->req->analysers. The
801 * function may be called for frontend rules and backend rules. It only relies
802 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100803 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200804int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100805{
806 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200807 struct stksess *ts;
808 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100809 int partial;
810
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100811 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 +0100812 now_ms, __FUNCTION__,
813 s,
814 req,
815 req->rex, req->wex,
816 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200817 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100818 req->analysers);
819
Willy Tarreauedcf6682008-11-30 23:15:34 +0100820 /* We don't know whether we have enough data, so must proceed
821 * this way :
822 * - iterate through all rules in their declaration order
823 * - if one rule returns MISS, it means the inspect delay is
824 * not over yet, then return immediately, otherwise consider
825 * it as a non-match.
826 * - if one rule returns OK, then return OK
827 * - if one rule returns KO, then return KO
828 */
829
Willy Tarreau9b28e032012-10-12 23:49:43 +0200830 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200831 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200832 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100833 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200834 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100835
Willy Tarreaufb356202010-08-03 14:02:05 +0200836 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100837 int ret = ACL_PAT_PASS;
838
839 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200840 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100841 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200842 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100843 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200844 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
845 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100846 return 0;
847 }
848
849 ret = acl_pass(ret);
850 if (rule->cond->pol == ACL_COND_UNLESS)
851 ret = !ret;
852 }
853
854 if (ret) {
855 /* we have a matching rule. */
856 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200857 channel_abort(req);
858 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100859 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200860
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100861 s->be->be_counters.denied_req++;
862 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200863 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200864 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200865
Willy Tarreauedcf6682008-11-30 23:15:34 +0100866 if (!(s->flags & SN_ERR_MASK))
867 s->flags |= SN_ERR_PRXCOND;
868 if (!(s->flags & SN_FINST_MASK))
869 s->flags |= SN_FINST_R;
870 return 0;
871 }
Willy Tarreau56123282010-08-06 19:06:56 +0200872 else if (rule->action == TCP_ACT_TRK_SC1) {
873 if (!s->stkctr1_entry) {
874 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200875 * Also, note that right now we can only track SRC so we
876 * don't check how to get the key, but later we may need
877 * to consider rule->act_prm->trk_ctr.type.
878 */
879 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200880 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn->addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200881 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200882 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200883 if (s->fe != s->be)
884 s->flags |= SN_BE_TRACK_SC1;
885 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200886 }
887 }
Willy Tarreau56123282010-08-06 19:06:56 +0200888 else if (rule->action == TCP_ACT_TRK_SC2) {
889 if (!s->stkctr2_entry) {
890 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200891 * Also, note that right now we can only track SRC so we
892 * don't check how to get the key, but later we may need
893 * to consider rule->act_prm->trk_ctr.type.
894 */
895 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200896 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn->addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200897 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200898 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200899 if (s->fe != s->be)
900 s->flags |= SN_BE_TRACK_SC2;
901 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200902 }
903 }
904 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100905 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200906 break;
907 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100908 }
909 }
910
911 /* if we get there, it means we have no rule which matches, or
912 * we have an explicit accept, so we apply the default accept.
913 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200914 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100915 req->analyse_exp = TICK_ETERNITY;
916 return 1;
917}
918
Emeric Brun97679e72010-09-23 17:56:44 +0200919/* This function performs the TCP response analysis on the current response. It
920 * returns 1 if the processing can continue on next analysers, or zero if it
921 * needs more data, encounters an error, or wants to immediately abort the
922 * response. It relies on buffers flags, and updates s->rep->analysers. The
923 * function may be called for backend rules.
924 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200925int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200926{
927 struct tcp_rule *rule;
928 int partial;
929
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100930 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 +0200931 now_ms, __FUNCTION__,
932 s,
933 rep,
934 rep->rex, rep->wex,
935 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200936 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200937 rep->analysers);
938
939 /* We don't know whether we have enough data, so must proceed
940 * this way :
941 * - iterate through all rules in their declaration order
942 * - if one rule returns MISS, it means the inspect delay is
943 * not over yet, then return immediately, otherwise consider
944 * it as a non-match.
945 * - if one rule returns OK, then return OK
946 * - if one rule returns KO, then return KO
947 */
948
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200949 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200950 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200951 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200952 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200953
954 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
955 int ret = ACL_PAT_PASS;
956
957 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200958 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200959 if (ret == ACL_PAT_MISS) {
960 /* just set the analyser timeout once at the beginning of the response */
961 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
962 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
963 return 0;
964 }
965
966 ret = acl_pass(ret);
967 if (rule->cond->pol == ACL_COND_UNLESS)
968 ret = !ret;
969 }
970
971 if (ret) {
972 /* we have a matching rule. */
973 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200974 channel_abort(rep);
975 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200976 rep->analysers = 0;
977
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100978 s->be->be_counters.denied_resp++;
979 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200980 if (s->listener->counters)
981 s->listener->counters->denied_resp++;
982
983 if (!(s->flags & SN_ERR_MASK))
984 s->flags |= SN_ERR_PRXCOND;
985 if (!(s->flags & SN_FINST_MASK))
986 s->flags |= SN_FINST_D;
987 return 0;
988 }
989 else {
990 /* otherwise accept */
991 break;
992 }
993 }
994 }
995
996 /* if we get there, it means we have no rule which matches, or
997 * we have an explicit accept, so we apply the default accept.
998 */
999 rep->analysers &= ~an_bit;
1000 rep->analyse_exp = TICK_ETERNITY;
1001 return 1;
1002}
1003
1004
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001005/* This function performs the TCP layer4 analysis on the current request. It
1006 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1007 * matches or if no more rule matches. It can only use rules which don't need
1008 * any data.
1009 */
1010int tcp_exec_req_rules(struct session *s)
1011{
1012 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001013 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001014 struct stktable *t = NULL;
1015 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001016 int ret;
1017
1018 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1019 ret = ACL_PAT_PASS;
1020
1021 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001022 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001023 ret = acl_pass(ret);
1024 if (rule->cond->pol == ACL_COND_UNLESS)
1025 ret = !ret;
1026 }
1027
1028 if (ret) {
1029 /* we have a matching rule. */
1030 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001031 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001032 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001033 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001034
1035 if (!(s->flags & SN_ERR_MASK))
1036 s->flags |= SN_ERR_PRXCOND;
1037 if (!(s->flags & SN_FINST_MASK))
1038 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001039 result = 0;
1040 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001041 }
Willy Tarreau56123282010-08-06 19:06:56 +02001042 else if (rule->action == TCP_ACT_TRK_SC1) {
1043 if (!s->stkctr1_entry) {
1044 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001045 * Also, note that right now we can only track SRC so we
1046 * don't check how to get the key, but later we may need
1047 * to consider rule->act_prm->trk_ctr.type.
1048 */
1049 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001050 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn->addr.from));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001051 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001052 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001053 }
1054 }
Willy Tarreau56123282010-08-06 19:06:56 +02001055 else if (rule->action == TCP_ACT_TRK_SC2) {
1056 if (!s->stkctr2_entry) {
1057 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001058 * Also, note that right now we can only track SRC so we
1059 * don't check how to get the key, but later we may need
1060 * to consider rule->act_prm->trk_ctr.type.
1061 */
1062 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001063 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn->addr.from));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001064 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001065 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001066 }
1067 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001068 else {
1069 /* otherwise it's an accept */
1070 break;
1071 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001072 }
1073 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001074 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001075}
1076
Emeric Brun97679e72010-09-23 17:56:44 +02001077/* Parse a tcp-response rule. Return a negative value in case of failure */
1078static int tcp_parse_response_rule(char **args, int arg, int section_type,
1079 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001080 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001081{
1082 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001083 memprintf(err, "%s %s is only allowed in 'backend' sections",
1084 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001085 return -1;
1086 }
1087
1088 if (strcmp(args[arg], "accept") == 0) {
1089 arg++;
1090 rule->action = TCP_ACT_ACCEPT;
1091 }
1092 else if (strcmp(args[arg], "reject") == 0) {
1093 arg++;
1094 rule->action = TCP_ACT_REJECT;
1095 }
1096 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001097 memprintf(err,
1098 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1099 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001100 return -1;
1101 }
1102
1103 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001104 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1105 memprintf(err,
1106 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1107 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001108 return -1;
1109 }
1110 }
1111 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001112 memprintf(err,
1113 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001114 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1115 return -1;
1116 }
1117 return 0;
1118}
1119
1120
1121
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001122/* Parse a tcp-request rule. Return a negative value in case of failure */
1123static int tcp_parse_request_rule(char **args, int arg, int section_type,
1124 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001125 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001126{
1127 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001128 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1129 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001130 return -1;
1131 }
1132
1133 if (!strcmp(args[arg], "accept")) {
1134 arg++;
1135 rule->action = TCP_ACT_ACCEPT;
1136 }
1137 else if (!strcmp(args[arg], "reject")) {
1138 arg++;
1139 rule->action = TCP_ACT_REJECT;
1140 }
Willy Tarreau56123282010-08-06 19:06:56 +02001141 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001142 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001143 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001144
1145 arg++;
1146 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001147 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001148
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001149 if (ret < 0) { /* nb: warnings are not handled yet */
1150 memprintf(err,
1151 "'%s %s %s' : %s in %s '%s'",
1152 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1153 return ret;
1154 }
Willy Tarreau56123282010-08-06 19:06:56 +02001155 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001156 }
Willy Tarreau56123282010-08-06 19:06:56 +02001157 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001158 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001159 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001160
1161 arg++;
1162 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001163 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001164
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001165 if (ret < 0) { /* nb: warnings are not handled yet */
1166 memprintf(err,
1167 "'%s %s %s' : %s in %s '%s'",
1168 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1169 return ret;
1170 }
Willy Tarreau56123282010-08-06 19:06:56 +02001171 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001172 }
1173 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001174 memprintf(err,
1175 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1176 "or 'track-sc2' in %s '%s' (got '%s')",
1177 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001178 return -1;
1179 }
1180
1181 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001182 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1183 memprintf(err,
1184 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1185 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001186 return -1;
1187 }
1188 }
1189 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001190 memprintf(err,
1191 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001192 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1193 return -1;
1194 }
1195 return 0;
1196}
1197
Emeric Brun97679e72010-09-23 17:56:44 +02001198/* This function should be called to parse a line starting with the "tcp-response"
1199 * keyword.
1200 */
1201static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001202 struct proxy *defpx, const char *file, int line,
1203 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001204{
1205 const char *ptr = NULL;
1206 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001207 int warn = 0;
1208 int arg;
1209 struct tcp_rule *rule;
1210
1211 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001212 memprintf(err, "missing argument for '%s' in %s '%s'",
1213 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001214 return -1;
1215 }
1216
1217 if (strcmp(args[1], "inspect-delay") == 0) {
1218 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001219 memprintf(err, "%s %s is only allowed in 'backend' sections",
1220 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001221 return -1;
1222 }
1223
1224 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001225 memprintf(err,
1226 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1227 args[0], args[1], proxy_type_str(curpx), curpx->id);
1228 if (ptr)
1229 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001230 return -1;
1231 }
1232
1233 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001234 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1235 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001236 return 1;
1237 }
1238 curpx->tcp_rep.inspect_delay = val;
1239 return 0;
1240 }
1241
Simon Hormande072bd2011-06-24 15:11:37 +09001242 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001243 LIST_INIT(&rule->list);
1244 arg = 1;
1245
1246 if (strcmp(args[1], "content") == 0) {
1247 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001248 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001249 goto error;
1250
1251 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1252 struct acl *acl;
1253 const char *name;
1254
1255 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1256 name = acl ? acl->name : "(unknown)";
1257
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001258 memprintf(err,
1259 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1260 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001261 warn++;
1262 }
1263
1264 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1265 }
1266 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001267 memprintf(err,
1268 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1269 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001270 goto error;
1271 }
1272
1273 return warn;
1274 error:
1275 free(rule);
1276 return -1;
1277}
1278
1279
Willy Tarreaub6866442008-07-14 23:54:42 +02001280/* This function should be called to parse a line starting with the "tcp-request"
1281 * keyword.
1282 */
1283static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001284 struct proxy *defpx, const char *file, int line,
1285 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001286{
1287 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001288 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001289 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001290 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001291 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001292
1293 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001294 if (curpx == defpx)
1295 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1296 else
1297 memprintf(err, "missing argument for '%s' in %s '%s'",
1298 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001299 return -1;
1300 }
1301
1302 if (!strcmp(args[1], "inspect-delay")) {
1303 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001304 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1305 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001306 return -1;
1307 }
1308
Willy Tarreaub6866442008-07-14 23:54:42 +02001309 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001310 memprintf(err,
1311 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1312 args[0], args[1], proxy_type_str(curpx), curpx->id);
1313 if (ptr)
1314 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001315 return -1;
1316 }
1317
1318 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001319 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1320 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001321 return 1;
1322 }
1323 curpx->tcp_req.inspect_delay = val;
1324 return 0;
1325 }
1326
Simon Hormande072bd2011-06-24 15:11:37 +09001327 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001328 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001329 arg = 1;
1330
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001331 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001332 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001333 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001334 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001335
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001336 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001337 struct acl *acl;
1338 const char *name;
1339
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001340 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001341 name = acl ? acl->name : "(unknown)";
1342
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001343 memprintf(err,
1344 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1345 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001346 warn++;
1347 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001348 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001349 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001350 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001351 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001352
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001353 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001354 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1355 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001356 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001357 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001358
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001359 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001360 goto error;
1361
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001362 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1363 struct acl *acl;
1364 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001365
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001366 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1367 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001368
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001369 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001370 memprintf(err,
1371 "'%s %s' may not reference acl '%s' which makes use of "
1372 "payload in %s '%s'. Please use '%s content' for this.",
1373 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001374 goto error;
1375 }
1376 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001377 memprintf(err,
1378 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1379 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001380 warn++;
1381 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001382 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001383 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001384 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001385 if (curpx == defpx)
1386 memprintf(err,
1387 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1388 args[0], args[1]);
1389 else
1390 memprintf(err,
1391 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1392 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001393 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001394 }
1395
Willy Tarreau1a687942010-05-23 22:40:30 +02001396 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001397 error:
1398 free(rule);
1399 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001400}
1401
Willy Tarreau645513a2010-05-24 20:55:15 +02001402
1403/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001404/* All supported sample fetch functios must be declared here */
1405/************************************************************************/
1406
1407/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001408 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001409 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1410 * is a string (cookie name), other types will lead to undefined behaviour.
1411 */
1412int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001413smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001414 const struct arg *args, struct sample *smp)
1415{
1416 int bleft;
1417 const unsigned char *data;
1418
1419 if (!l4 || !l4->req)
1420 return 0;
1421
1422 smp->flags = 0;
1423 smp->type = SMP_T_CSTR;
1424
Willy Tarreau9b28e032012-10-12 23:49:43 +02001425 bleft = l4->req->buf->i;
Willy Tarreau32389b72012-04-23 23:13:20 +02001426 if (bleft <= 11)
1427 goto too_short;
1428
Willy Tarreau9b28e032012-10-12 23:49:43 +02001429 data = (const unsigned char *)l4->req->buf->p + 11;
Willy Tarreau32389b72012-04-23 23:13:20 +02001430 bleft -= 11;
1431
1432 if (bleft <= 7)
1433 goto too_short;
1434
1435 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1436 goto not_cookie;
1437
1438 data += 7;
1439 bleft -= 7;
1440
1441 while (bleft > 0 && *data == ' ') {
1442 data++;
1443 bleft--;
1444 }
1445
1446 if (args) {
1447
1448 if (bleft <= args->data.str.len)
1449 goto too_short;
1450
1451 if ((data[args->data.str.len] != '=') ||
1452 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1453 goto not_cookie;
1454
1455 data += args->data.str.len + 1;
1456 bleft -= args->data.str.len + 1;
1457 } else {
1458 while (bleft > 0 && *data != '=') {
1459 if (*data == '\r' || *data == '\n')
1460 goto not_cookie;
1461 data++;
1462 bleft--;
1463 }
1464
1465 if (bleft < 1)
1466 goto too_short;
1467
1468 if (*data != '=')
1469 goto not_cookie;
1470
1471 data++;
1472 bleft--;
1473 }
1474
1475 /* data points to cookie value */
1476 smp->data.str.str = (char *)data;
1477 smp->data.str.len = 0;
1478
1479 while (bleft > 0 && *data != '\r') {
1480 data++;
1481 bleft--;
1482 }
1483
1484 if (bleft < 2)
1485 goto too_short;
1486
1487 if (data[0] != '\r' || data[1] != '\n')
1488 goto not_cookie;
1489
1490 smp->data.str.len = (char *)data - smp->data.str.str;
1491 smp->flags = SMP_F_VOLATILE;
1492 return 1;
1493
1494 too_short:
1495 smp->flags = SMP_F_MAY_CHANGE;
1496 not_cookie:
1497 return 0;
1498}
1499
Willy Tarreau32389b72012-04-23 23:13:20 +02001500/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001501/* All supported ACL keywords must be declared here. */
1502/************************************************************************/
1503
Willy Tarreau32389b72012-04-23 23:13:20 +02001504/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1505static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001506acl_fetch_rdp_cookie_cnt(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 Tarreau32389b72012-04-23 23:13:20 +02001508{
1509 int ret;
1510
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001511 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001512
1513 if (smp->flags & SMP_F_MAY_CHANGE)
1514 return 0;
1515
1516 smp->flags = SMP_F_VOLATILE;
1517 smp->type = SMP_T_UINT;
1518 smp->data.uint = ret;
1519 return 1;
1520}
1521
1522
Willy Tarreau4a129812012-04-25 17:31:42 +02001523/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001524static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001525smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001526 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001527{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001528 switch (l4->si[0].conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001529 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001530 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001531 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001532 break;
1533 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001534 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001535 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001536 break;
1537 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001538 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001539 }
Emeric Brunf769f512010-10-22 17:14:01 +02001540
Willy Tarreau37406352012-04-23 16:16:37 +02001541 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001542 return 1;
1543}
1544
Willy Tarreaua5e37562011-12-16 17:06:15 +01001545/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001546static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001547smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001548 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001549{
Willy Tarreauf853c462012-04-23 18:53:56 +02001550 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001551 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001552 return 0;
1553
Willy Tarreau37406352012-04-23 16:16:37 +02001554 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001555 return 1;
1556}
1557
Willy Tarreau4a129812012-04-25 17:31:42 +02001558/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001559static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001560smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001561 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001562{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001563 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001564
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001565 switch (l4->si[0].conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001566 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001567 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001568 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001569 break;
1570 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001571 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001572 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001573 break;
1574 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001575 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001576 }
Emeric Brunf769f512010-10-22 17:14:01 +02001577
Willy Tarreau37406352012-04-23 16:16:37 +02001578 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001579 return 1;
1580}
1581
Willy Tarreaua5e37562011-12-16 17:06:15 +01001582/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001583static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001584smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001585 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001586{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001587 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001588
Willy Tarreauf853c462012-04-23 18:53:56 +02001589 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001590 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001591 return 0;
1592
Willy Tarreau37406352012-04-23 16:16:37 +02001593 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001594 return 1;
1595}
1596
1597static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001598smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1599 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001600{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001601 unsigned int len_offset = arg_p[0].data.uint;
1602 unsigned int len_size = arg_p[1].data.uint;
1603 unsigned int buf_offset;
1604 unsigned int buf_size = 0;
Willy Tarreau697d8502012-10-12 23:53:39 +02001605 struct channel *chn;
Emericf2d7cae2010-11-05 18:13:50 +01001606 int i;
1607
1608 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1609 /* by default buf offset == len offset + len size */
1610 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1611
1612 if (!l4)
1613 return 0;
1614
Willy Tarreau697d8502012-10-12 23:53:39 +02001615 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001616
Willy Tarreau697d8502012-10-12 23:53:39 +02001617 if (!chn)
Emericf2d7cae2010-11-05 18:13:50 +01001618 return 0;
1619
Willy Tarreau9b28e032012-10-12 23:49:43 +02001620 if (len_offset + len_size > chn->buf->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001621 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001622
1623 for (i = 0; i < len_size; i++) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02001624 buf_size = (buf_size << 8) + ((unsigned char *)chn->buf->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001625 }
1626
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001627 /* buf offset may be implicit, absolute or relative */
1628 buf_offset = len_offset + len_size;
1629 if (arg_p[2].type == ARGT_UINT)
1630 buf_offset = arg_p[2].data.uint;
1631 else if (arg_p[2].type == ARGT_SINT)
1632 buf_offset += arg_p[2].data.sint;
1633
Willy Tarreau9b28e032012-10-12 23:49:43 +02001634 if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001635 /* will never match */
1636 smp->flags = 0;
1637 return 0;
1638 }
1639
Willy Tarreau9b28e032012-10-12 23:49:43 +02001640 if (buf_offset + buf_size > chn->buf->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001641 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001642
1643 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001644 smp->type = SMP_T_CBIN;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001645 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001646 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001647 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001648
1649 too_short:
1650 smp->flags = SMP_F_MAY_CHANGE;
1651 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001652}
1653
1654static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001655smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1656 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001657{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001658 unsigned int buf_offset = arg_p[0].data.uint;
1659 unsigned int buf_size = arg_p[1].data.uint;
Willy Tarreau697d8502012-10-12 23:53:39 +02001660 struct channel *chn;
Emericf2d7cae2010-11-05 18:13:50 +01001661
1662 if (!l4)
1663 return 0;
1664
Willy Tarreau697d8502012-10-12 23:53:39 +02001665 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001666
Willy Tarreau697d8502012-10-12 23:53:39 +02001667 if (!chn)
Emericf2d7cae2010-11-05 18:13:50 +01001668 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001669
Willy Tarreau9b28e032012-10-12 23:49:43 +02001670 if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001671 /* will never match */
1672 smp->flags = 0;
1673 return 0;
1674 }
Emericf2d7cae2010-11-05 18:13:50 +01001675
Willy Tarreau9b28e032012-10-12 23:49:43 +02001676 if (buf_offset + buf_size > chn->buf->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001677 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001678
1679 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001680 smp->type = SMP_T_CBIN;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001681 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001682 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001683 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001684
1685 too_short:
1686 smp->flags = SMP_F_MAY_CHANGE;
1687 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001688}
1689
Willy Tarreau21d68a62012-04-20 15:52:36 +02001690/* This function is used to validate the arguments passed to a "payload" fetch
1691 * keyword. This keyword expects two positive integers, with the second one
1692 * being strictly positive. It is assumed that the types are already the correct
1693 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1694 * filled with a pointer to an error message in case of error, that the caller
1695 * is responsible for freeing. The initial location must either be freeable or
1696 * NULL.
1697 */
1698static int val_payload(struct arg *arg, char **err_msg)
1699{
1700 if (!arg[1].data.uint) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001701 memprintf(err_msg, "payload length must be > 0");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001702 return 0;
1703 }
1704 return 1;
1705}
1706
1707/* This function is used to validate the arguments passed to a "payload_lv" fetch
1708 * keyword. This keyword allows two positive integers and an optional signed one,
1709 * with the second one being strictly positive and the third one being greater than
1710 * the opposite of the two others if negative. It is assumed that the types are
1711 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1712 * not NULL, it will be filled with a pointer to an error message in case of
1713 * error, that the caller is responsible for freeing. The initial location must
1714 * either be freeable or NULL.
1715 */
1716static int val_payload_lv(struct arg *arg, char **err_msg)
1717{
1718 if (!arg[1].data.uint) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001719 memprintf(err_msg, "payload length must be > 0");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001720 return 0;
1721 }
1722
1723 if (arg[2].type == ARGT_SINT &&
1724 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001725 memprintf(err_msg, "payload offset too negative");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001726 return 0;
1727 }
1728 return 1;
1729}
1730
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001731#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001732/* parse the "v4v6" bind keyword */
1733static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1734{
1735 struct listener *l;
1736
1737 list_for_each_entry(l, &conf->listeners, by_bind) {
1738 if (l->addr.ss_family == AF_INET6)
1739 l->options |= LI_O_V4V6;
1740 }
1741
1742 return 0;
1743}
1744
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001745/* parse the "v6only" bind keyword */
1746static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1747{
1748 struct listener *l;
1749
1750 list_for_each_entry(l, &conf->listeners, by_bind) {
1751 if (l->addr.ss_family == AF_INET6)
1752 l->options |= LI_O_V6ONLY;
1753 }
1754
1755 return 0;
1756}
1757#endif
1758
Willy Tarreau44791242012-09-12 23:27:21 +02001759#ifdef CONFIG_HAP_LINUX_TPROXY
1760/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001761static int bind_parse_transparent(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001762{
1763 struct listener *l;
1764
Willy Tarreau4348fad2012-09-20 16:48:07 +02001765 list_for_each_entry(l, &conf->listeners, by_bind) {
1766 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1767 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001768 }
1769
Willy Tarreau44791242012-09-12 23:27:21 +02001770 return 0;
1771}
1772#endif
1773
1774#ifdef TCP_DEFER_ACCEPT
1775/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001776static int bind_parse_defer_accept(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001777{
1778 struct listener *l;
1779
Willy Tarreau4348fad2012-09-20 16:48:07 +02001780 list_for_each_entry(l, &conf->listeners, by_bind) {
1781 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1782 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001783 }
1784
Willy Tarreau44791242012-09-12 23:27:21 +02001785 return 0;
1786}
1787#endif
1788
Willy Tarreau1c862c52012-10-05 16:21:00 +02001789#ifdef TCP_FASTOPEN
1790/* parse the "defer-accept" bind keyword */
1791static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1792{
1793 struct listener *l;
1794
1795 list_for_each_entry(l, &conf->listeners, by_bind) {
1796 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1797 l->options |= LI_O_TCP_FO;
1798 }
1799
1800 return 0;
1801}
1802#endif
1803
Willy Tarreau44791242012-09-12 23:27:21 +02001804#ifdef TCP_MAXSEG
1805/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001806static int bind_parse_mss(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001807{
1808 struct listener *l;
1809 int mss;
1810
Willy Tarreau44791242012-09-12 23:27:21 +02001811 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001812 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001813 return ERR_ALERT | ERR_FATAL;
1814 }
1815
1816 mss = atoi(args[cur_arg + 1]);
1817 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001818 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001819 return ERR_ALERT | ERR_FATAL;
1820 }
1821
Willy Tarreau4348fad2012-09-20 16:48:07 +02001822 list_for_each_entry(l, &conf->listeners, by_bind) {
1823 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1824 l->maxseg = mss;
1825 }
Willy Tarreau44791242012-09-12 23:27:21 +02001826
1827 return 0;
1828}
1829#endif
1830
1831#ifdef SO_BINDTODEVICE
1832/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001833static int bind_parse_interface(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001834{
1835 struct listener *l;
1836
Willy Tarreau44791242012-09-12 23:27:21 +02001837 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001838 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001839 return ERR_ALERT | ERR_FATAL;
1840 }
1841
Willy Tarreau4348fad2012-09-20 16:48:07 +02001842 list_for_each_entry(l, &conf->listeners, by_bind) {
1843 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1844 l->interface = strdup(args[cur_arg + 1]);
1845 }
Willy Tarreau44791242012-09-12 23:27:21 +02001846
1847 global.last_checks |= LSTCHK_NETADM;
1848 return 0;
1849}
1850#endif
1851
Willy Tarreaub6866442008-07-14 23:54:42 +02001852static struct cfg_kw_list cfg_kws = {{ },{
1853 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001854 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001855 { 0, NULL, NULL },
1856}};
1857
Willy Tarreau61612d42012-04-19 18:42:05 +02001858/* Note: must not be declared <const> as its list will be overwritten.
1859 * Please take care of keeping this list alphabetically sorted.
1860 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001861static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001862 { "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 +02001863 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001864 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1865 { "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 +02001866 { "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 +02001867 { "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 +02001868 { "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 +02001869 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001870 { NULL, NULL, NULL, NULL },
1871}};
1872
Willy Tarreau4a129812012-04-25 17:31:42 +02001873/* Note: must not be declared <const> as its list will be overwritten.
1874 * Note: fetches that may return multiple types must be declared as the lowest
1875 * common denominator, the type that can be casted into all other ones. For
1876 * instance v4/v6 must be declared v4.
1877 */
Willy Tarreau12785782012-04-27 21:37:17 +02001878static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001879 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001880 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001881 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001882 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1883 { "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 +02001884 { "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 +02001885 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001886 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001887}};
1888
Willy Tarreau44791242012-09-12 23:27:21 +02001889/************************************************************************/
1890/* All supported bind keywords must be declared here. */
1891/************************************************************************/
1892
1893/* Note: must not be declared <const> as its list will be overwritten.
1894 * Please take care of keeping this list alphabetically sorted, doing so helps
1895 * all code contributors.
1896 * Optional keywords are also declared with a NULL ->parse() function so that
1897 * the config parser can report an appropriate error when a known keyword was
1898 * not enabled.
1899 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001900static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001901#ifdef TCP_DEFER_ACCEPT
1902 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1903#endif
1904#ifdef SO_BINDTODEVICE
1905 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1906#endif
1907#ifdef TCP_MAXSEG
1908 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1909#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001910#ifdef TCP_FASTOPEN
1911 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1912#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001913#ifdef CONFIG_HAP_LINUX_TPROXY
1914 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1915#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001916#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001917 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001918 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1919#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001920 /* the versions with the NULL parse function*/
1921 { "defer-accept", NULL, 0 },
1922 { "interface", NULL, 1 },
1923 { "mss", NULL, 1 },
1924 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001925 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001926 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001927 { NULL, NULL, 0 },
1928}};
1929
Willy Tarreaue6b98942007-10-29 01:09:36 +01001930__attribute__((constructor))
1931static void __tcp_protocol_init(void)
1932{
1933 protocol_register(&proto_tcpv4);
1934 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001935 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001936 cfg_register_keywords(&cfg_kws);
1937 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001938 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001939}
1940
1941
1942/*
1943 * Local variables:
1944 * c-indent-level: 8
1945 * c-basic-offset: 8
1946 * End:
1947 */