blob: 8b0792baa3e96107690f190b69fd9233eb35e456 [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 Tarreau1b4b7ce2011-04-05 16:56:50 +0200173 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100174 if (ret < 0)
175 return 2;
176 }
177 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200178 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100179 if (ret < 0)
180 return 1;
181 }
182
183 if (!flags)
184 return 0;
185
186#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100187 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100188 struct in_tproxy itp1, itp2;
189 memset(&itp1, 0, sizeof(itp1));
190
191 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100192 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
193 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100194
195 /* set connect flag on socket */
196 itp2.op = TPROXY_FLAGS;
197 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
198
199 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
200 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
201 foreign_ok = 1;
202 }
203 }
204#endif
205 if (!foreign_ok)
206 /* we could not bind to a foreign address */
207 return 2;
208
209 return 0;
210}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100211
Willy Tarreau9650f372009-08-16 14:02:45 +0200212
213/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200214 * This function initiates a TCP connection establishment to the target assigned
215 * to connection <conn> using (si->{target,addr.to}). A source address may be
216 * pointed to by conn->addr.from in case of transparent proxying. Normal source
217 * bind addresses are still determined locally (due to the possible need of a
218 * source port). conn->target may point either to a valid server or to a backend,
219 * depending on conn->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are
220 * supported. The <data> parameter is a boolean indicating whether there are data
221 * waiting for being sent or not, in order to adjust data write polling and on
222 * some platforms, the ability to avoid an empty initial ACK.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200223 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200224 * It can return one of :
225 * - SN_ERR_NONE if everything's OK
226 * - SN_ERR_SRVTO if there are no more servers
227 * - SN_ERR_SRVCL if the connection was refused by the server
228 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
229 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
230 * - SN_ERR_INTERNAL for any other purely internal errors
231 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
232 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100233
Willy Tarreau14f8e862012-08-30 22:23:13 +0200234int tcp_connect_server(struct connection *conn, int data)
Willy Tarreau9650f372009-08-16 14:02:45 +0200235{
236 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100237 struct server *srv;
238 struct proxy *be;
239
Willy Tarreau986a9d22012-08-30 21:11:38 +0200240 switch (conn->target.type) {
Willy Tarreauac825402011-03-04 22:04:29 +0100241 case TARG_TYPE_PROXY:
Willy Tarreau986a9d22012-08-30 21:11:38 +0200242 be = conn->target.ptr.p;
Willy Tarreauac825402011-03-04 22:04:29 +0100243 srv = NULL;
244 break;
245 case TARG_TYPE_SERVER:
Willy Tarreau986a9d22012-08-30 21:11:38 +0200246 srv = conn->target.ptr.s;
Willy Tarreauac825402011-03-04 22:04:29 +0100247 be = srv->proxy;
248 break;
249 default:
250 return SN_ERR_INTERNAL;
251 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200252
Willy Tarreau986a9d22012-08-30 21:11:38 +0200253 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200254 qfprintf(stderr, "Cannot get a server socket.\n");
255
256 if (errno == ENFILE)
257 send_log(be, LOG_EMERG,
258 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
259 be->id, maxfd);
260 else if (errno == EMFILE)
261 send_log(be, LOG_EMERG,
262 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
263 be->id, maxfd);
264 else if (errno == ENOBUFS || errno == ENOMEM)
265 send_log(be, LOG_EMERG,
266 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
267 be->id, maxfd);
268 /* this is a resource error */
269 return SN_ERR_RESOURCE;
270 }
271
272 if (fd >= global.maxsock) {
273 /* do not log anything there, it's a normal condition when this option
274 * is used to serialize connections to a server !
275 */
276 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
277 close(fd);
278 return SN_ERR_PRXCOND; /* it is a configuration limit */
279 }
280
281 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900282 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200283 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
284 close(fd);
285 return SN_ERR_INTERNAL;
286 }
287
288 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900289 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200290
Willy Tarreau9650f372009-08-16 14:02:45 +0200291 /* allow specific binding :
292 * - server-specific at first
293 * - proxy-specific next
294 */
295 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200296 int ret, flags = 0;
297
Willy Tarreau9650f372009-08-16 14:02:45 +0200298 switch (srv->state & SRV_TPROXY_MASK) {
299 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200300 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200301 flags = 3;
302 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200303 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200304 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200305 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200306 break;
307 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200308
Willy Tarreau9650f372009-08-16 14:02:45 +0200309#ifdef SO_BINDTODEVICE
310 /* Note: this might fail if not CAP_NET_RAW */
311 if (srv->iface_name)
312 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
313#endif
314
315 if (srv->sport_range) {
316 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100317 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200318
319 ret = 1;
320 src = srv->source_addr;
321
322 do {
323 /* note: in case of retry, we may have to release a previously
324 * allocated port, hence this loop's construct.
325 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200326 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
327 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200328
329 if (!attempts)
330 break;
331 attempts--;
332
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200333 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
334 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200335 break;
336
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200337 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200338 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200339
Willy Tarreau986a9d22012-08-30 21:11:38 +0200340 ret = tcp_bind_socket(fd, flags, &src, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200341 } while (ret != 0); /* binding NOK */
342 }
343 else {
Willy Tarreau986a9d22012-08-30 21:11:38 +0200344 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200345 }
346
347 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200348 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
349 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200350 close(fd);
351
352 if (ret == 1) {
353 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
354 be->id, srv->id);
355 send_log(be, LOG_EMERG,
356 "Cannot bind to source address before connect() for server %s/%s.\n",
357 be->id, srv->id);
358 } else {
359 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
360 be->id, srv->id);
361 send_log(be, LOG_EMERG,
362 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
363 be->id, srv->id);
364 }
365 return SN_ERR_RESOURCE;
366 }
367 }
368 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200369 int ret, flags = 0;
370
Willy Tarreau9650f372009-08-16 14:02:45 +0200371 switch (be->options & PR_O_TPXY_MASK) {
372 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200373 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200374 flags = 3;
375 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200376 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200377 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200378 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200379 break;
380 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200381
Willy Tarreau9650f372009-08-16 14:02:45 +0200382#ifdef SO_BINDTODEVICE
383 /* Note: this might fail if not CAP_NET_RAW */
384 if (be->iface_name)
385 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
386#endif
Willy Tarreau986a9d22012-08-30 21:11:38 +0200387 ret = tcp_bind_socket(fd, flags, &be->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200388 if (ret) {
389 close(fd);
390 if (ret == 1) {
391 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
392 be->id);
393 send_log(be, LOG_EMERG,
394 "Cannot bind to source address before connect() for proxy %s.\n",
395 be->id);
396 } else {
397 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
398 be->id);
399 send_log(be, LOG_EMERG,
400 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
401 be->id);
402 }
403 return SN_ERR_RESOURCE;
404 }
405 }
406
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400407#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200408 /* disabling tcp quick ack now allows the first request to leave the
409 * machine with the first ACK. We only do this if there are pending
410 * data in the buffer.
411 */
Willy Tarreau14f8e862012-08-30 22:23:13 +0200412 if ((be->options2 & PR_O2_SMARTCON) && data)
Simon Hormande072bd2011-06-24 15:11:37 +0900413 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200414#endif
415
Willy Tarreaue803de22010-01-21 17:43:04 +0100416 if (global.tune.server_sndbuf)
417 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
418
419 if (global.tune.server_rcvbuf)
420 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
421
Willy Tarreau986a9d22012-08-30 21:11:38 +0200422 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200423 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
424
425 if (errno == EAGAIN || errno == EADDRINUSE) {
426 char *msg;
427 if (errno == EAGAIN) /* no free ports left, try again later */
428 msg = "no free ports";
429 else
430 msg = "local address already in use";
431
432 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200433 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
434 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200435 close(fd);
436 send_log(be, LOG_EMERG,
437 "Connect() failed for server %s/%s: %s.\n",
438 be->id, srv->id, msg);
439 return SN_ERR_RESOURCE;
440 } else if (errno == ETIMEDOUT) {
441 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200442 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
443 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200444 close(fd);
445 return SN_ERR_SRVTO;
446 } else {
447 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
448 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200449 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
450 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200451 close(fd);
452 return SN_ERR_SRVCL;
453 }
454 }
455
Willy Tarreau986a9d22012-08-30 21:11:38 +0200456 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200457 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200458
Willy Tarreaud2274c62012-07-06 14:29:45 +0200459 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200460 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200461 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200462
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200463 if (conn_xprt_init(conn) < 0) {
Willy Tarreau184636e2012-09-06 14:04:41 +0200464 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200465 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200466 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200467
Willy Tarreau14f8e862012-08-30 22:23:13 +0200468 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200469 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200470
Willy Tarreau9650f372009-08-16 14:02:45 +0200471 return SN_ERR_NONE; /* connection is OK */
472}
473
474
Willy Tarreau59b94792012-05-11 16:16:40 +0200475/*
476 * Retrieves the source address for the socket <fd>, with <dir> indicating
477 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
478 * success, -1 in case of error. The socket's source address is stored in
479 * <sa> for <salen> bytes.
480 */
481int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
482{
483 if (dir)
484 return getsockname(fd, sa, &salen);
485 else
486 return getpeername(fd, sa, &salen);
487}
488
489
490/*
491 * Retrieves the original destination address for the socket <fd>, with <dir>
492 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
493 * listener, if the original destination address was translated, the original
494 * address is retrieved. It returns 0 in case of success, -1 in case of error.
495 * The socket's source address is stored in <sa> for <salen> bytes.
496 */
497int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
498{
499 if (dir)
500 return getpeername(fd, sa, &salen);
501#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
502 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
503 return 0;
504#endif
505 else
506 return getsockname(fd, sa, &salen);
507}
508
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200509/* This is the callback which is set when a connection establishment is pending
510 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200511 * once the connection is established. It updates the FD polling status. It
512 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
513 * it returns non-zero and removes itself from the connection's flags (the bit is
514 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200515 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200516int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200517{
Willy Tarreau239d7182012-07-23 18:53:03 +0200518 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200519
Willy Tarreau80184712012-07-06 14:54:49 +0200520 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200521 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200522
Willy Tarreau80184712012-07-06 14:54:49 +0200523 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200524 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200525
Willy Tarreau2da156f2012-07-23 15:07:23 +0200526 /* stop here if we reached the end of data */
527 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
528 goto out_error;
529
Willy Tarreau2c6be842012-07-06 17:12:34 +0200530 /* We have no data to send to check the connection, and
531 * getsockopt() will not inform us whether the connection
532 * is still pending. So we'll reuse connect() to check the
533 * state of the socket. This has the advantage of giving us
534 * the following info :
535 * - error
536 * - connecting (EALREADY, EINPROGRESS)
537 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200538 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200539 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200540 if (errno == EALREADY || errno == EINPROGRESS) {
541 conn_sock_stop_recv(conn);
542 conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200543 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200544 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200545
Willy Tarreau2c6be842012-07-06 17:12:34 +0200546 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200547 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200548
Willy Tarreau2c6be842012-07-06 17:12:34 +0200549 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200550 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200551
Willy Tarreau076be252012-07-06 16:02:29 +0200552 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200553 * forward the event to the transport layer which will notify the
554 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200555 */
Willy Tarreau80184712012-07-06 14:54:49 +0200556 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200557 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200558
559 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200560 /* Write error on the file descriptor. Report it to the connection
561 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200562 */
563
Willy Tarreau80184712012-07-06 14:54:49 +0200564 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200565 conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200566 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200567}
568
Willy Tarreau59b94792012-05-11 16:16:40 +0200569
Willy Tarreaue6b98942007-10-29 01:09:36 +0100570/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
571 * an error message in <err> if the message is at most <errlen> bytes long
572 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
573 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
574 * was alright and that no message was returned. ERR_RETRYABLE means that an
575 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700576 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100577 * the meaning of the error, but just indicate that a message is present which
578 * should be displayed with the respective level. Last, ERR_ABORT indicates
579 * that it's pointless to try to start other listeners. No error message is
580 * returned if errlen is NULL.
581 */
582int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
583{
584 __label__ tcp_return, tcp_close_return;
585 int fd, err;
586 const char *msg = NULL;
587
588 /* ensure we never return garbage */
589 if (errmsg && errlen)
590 *errmsg = 0;
591
592 if (listener->state != LI_ASSIGNED)
593 return ERR_NONE; /* already bound */
594
595 err = ERR_NONE;
596
597 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
598 err |= ERR_RETRYABLE | ERR_ALERT;
599 msg = "cannot create listening socket";
600 goto tcp_return;
601 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100602
Willy Tarreaue6b98942007-10-29 01:09:36 +0100603 if (fd >= global.maxsock) {
604 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
605 msg = "not enough free sockets (raise '-n' parameter)";
606 goto tcp_close_return;
607 }
608
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200609 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100610 err |= ERR_FATAL | ERR_ALERT;
611 msg = "cannot make socket non-blocking";
612 goto tcp_close_return;
613 }
614
Simon Hormande072bd2011-06-24 15:11:37 +0900615 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100616 /* not fatal but should be reported */
617 msg = "cannot do so_reuseaddr";
618 err |= ERR_ALERT;
619 }
620
621 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900622 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100623
Willy Tarreaue6b98942007-10-29 01:09:36 +0100624#ifdef SO_REUSEPORT
625 /* OpenBSD supports this. As it's present in old libc versions of Linux,
626 * it might return an error that we will silently ignore.
627 */
Simon Hormande072bd2011-06-24 15:11:37 +0900628 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100629#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100630#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200631 if (listener->options & LI_O_FOREIGN) {
632 switch (listener->addr.ss_family) {
633 case AF_INET:
634 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
635 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
636 msg = "cannot make listening socket transparent";
637 err |= ERR_ALERT;
638 }
639 break;
640 case AF_INET6:
641 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
642 msg = "cannot make listening socket transparent";
643 err |= ERR_ALERT;
644 }
645 break;
646 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100647 }
648#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100649#ifdef SO_BINDTODEVICE
650 /* Note: this might fail if not CAP_NET_RAW */
651 if (listener->interface) {
652 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100653 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100654 msg = "cannot bind listener to device";
655 err |= ERR_WARN;
656 }
657 }
658#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400659#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100660 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400661 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200662 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
663 msg = "cannot set MSS";
664 err |= ERR_WARN;
665 }
666 }
667#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200668#if defined(TCP_DEFER_ACCEPT)
669 if (listener->options & LI_O_DEF_ACCEPT) {
670 /* defer accept by up to one second */
671 int accept_delay = 1;
672 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
673 msg = "cannot enable DEFER_ACCEPT";
674 err |= ERR_WARN;
675 }
676 }
677#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200678#if defined(TCP_FASTOPEN)
679 if (listener->options & LI_O_TCP_FO) {
680 /* TFO needs a queue length, let's use the configured backlog */
681 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
682 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
683 msg = "cannot enable TCP_FASTOPEN";
684 err |= ERR_WARN;
685 }
686 }
687#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100688 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
689 err |= ERR_RETRYABLE | ERR_ALERT;
690 msg = "cannot bind socket";
691 goto tcp_close_return;
692 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100693
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100694 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100695 err |= ERR_RETRYABLE | ERR_ALERT;
696 msg = "cannot listen to socket";
697 goto tcp_close_return;
698 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100699
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400700#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200701 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900702 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200703#endif
704
Willy Tarreaue6b98942007-10-29 01:09:36 +0100705 /* the socket is ready */
706 listener->fd = fd;
707 listener->state = LI_LISTEN;
708
Willy Tarreaueabf3132008-08-29 23:36:51 +0200709 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200710 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200711 fd_insert(fd);
712
Willy Tarreaue6b98942007-10-29 01:09:36 +0100713 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100714 if (msg && errlen) {
715 char pn[INET6_ADDRSTRLEN];
716
Willy Tarreau631f01c2011-09-05 00:36:48 +0200717 addr_to_str(&listener->addr, pn, sizeof(pn));
718 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100719 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100720 return err;
721
722 tcp_close_return:
723 close(fd);
724 goto tcp_return;
725}
726
727/* This function creates all TCP sockets bound to the protocol entry <proto>.
728 * It is intended to be used as the protocol's bind_all() function.
729 * The sockets will be registered but not added to any fd_set, in order not to
730 * loose them across the fork(). A call to enable_all_listeners() is needed
731 * to complete initialization. The return value is composed from ERR_*.
732 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200733static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100734{
735 struct listener *listener;
736 int err = ERR_NONE;
737
738 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200739 err |= tcp_bind_listener(listener, errmsg, errlen);
740 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100741 break;
742 }
743
744 return err;
745}
746
747/* Add listener to the list of tcpv4 listeners. The listener's state
748 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
749 * listeners is updated. This is the function to use to add a new listener.
750 */
751void tcpv4_add_listener(struct listener *listener)
752{
753 if (listener->state != LI_INIT)
754 return;
755 listener->state = LI_ASSIGNED;
756 listener->proto = &proto_tcpv4;
757 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
758 proto_tcpv4.nb_listeners++;
759}
760
761/* Add listener to the list of tcpv4 listeners. The listener's state
762 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
763 * listeners is updated. This is the function to use to add a new listener.
764 */
765void tcpv6_add_listener(struct listener *listener)
766{
767 if (listener->state != LI_INIT)
768 return;
769 listener->state = LI_ASSIGNED;
770 listener->proto = &proto_tcpv6;
771 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
772 proto_tcpv6.nb_listeners++;
773}
774
Willy Tarreauedcf6682008-11-30 23:15:34 +0100775/* This function performs the TCP request analysis on the current request. It
776 * returns 1 if the processing can continue on next analysers, or zero if it
777 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200778 * request. It relies on buffers flags, and updates s->req->analysers. The
779 * function may be called for frontend rules and backend rules. It only relies
780 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100781 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200782int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100783{
784 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200785 struct stksess *ts;
786 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100787 int partial;
788
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100789 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 +0100790 now_ms, __FUNCTION__,
791 s,
792 req,
793 req->rex, req->wex,
794 req->flags,
Cyril Bonté3aaba442012-09-23 14:19:12 +0200795 req->buf.i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100796 req->analysers);
797
Willy Tarreauedcf6682008-11-30 23:15:34 +0100798 /* We don't know whether we have enough data, so must proceed
799 * this way :
800 * - iterate through all rules in their declaration order
801 * - if one rule returns MISS, it means the inspect delay is
802 * not over yet, then return immediately, otherwise consider
803 * it as a non-match.
804 * - if one rule returns OK, then return OK
805 * - if one rule returns KO, then return KO
806 */
807
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200808 if ((req->flags & CF_SHUTR) || buffer_full(&req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200809 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200810 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100811 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200812 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100813
Willy Tarreaufb356202010-08-03 14:02:05 +0200814 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100815 int ret = ACL_PAT_PASS;
816
817 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200818 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100819 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200820 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100821 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200822 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
823 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100824 return 0;
825 }
826
827 ret = acl_pass(ret);
828 if (rule->cond->pol == ACL_COND_UNLESS)
829 ret = !ret;
830 }
831
832 if (ret) {
833 /* we have a matching rule. */
834 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200835 channel_abort(req);
836 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100837 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200838
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100839 s->be->be_counters.denied_req++;
840 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200841 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200842 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200843
Willy Tarreauedcf6682008-11-30 23:15:34 +0100844 if (!(s->flags & SN_ERR_MASK))
845 s->flags |= SN_ERR_PRXCOND;
846 if (!(s->flags & SN_FINST_MASK))
847 s->flags |= SN_FINST_R;
848 return 0;
849 }
Willy Tarreau56123282010-08-06 19:06:56 +0200850 else if (rule->action == TCP_ACT_TRK_SC1) {
851 if (!s->stkctr1_entry) {
852 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200853 * Also, note that right now we can only track SRC so we
854 * don't check how to get the key, but later we may need
855 * to consider rule->act_prm->trk_ctr.type.
856 */
857 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +0200858 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200859 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200860 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200861 if (s->fe != s->be)
862 s->flags |= SN_BE_TRACK_SC1;
863 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200864 }
865 }
Willy Tarreau56123282010-08-06 19:06:56 +0200866 else if (rule->action == TCP_ACT_TRK_SC2) {
867 if (!s->stkctr2_entry) {
868 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200869 * Also, note that right now we can only track SRC so we
870 * don't check how to get the key, but later we may need
871 * to consider rule->act_prm->trk_ctr.type.
872 */
873 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +0200874 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200875 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200876 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200877 if (s->fe != s->be)
878 s->flags |= SN_BE_TRACK_SC2;
879 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200880 }
881 }
882 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100883 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200884 break;
885 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100886 }
887 }
888
889 /* if we get there, it means we have no rule which matches, or
890 * we have an explicit accept, so we apply the default accept.
891 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200892 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100893 req->analyse_exp = TICK_ETERNITY;
894 return 1;
895}
896
Emeric Brun97679e72010-09-23 17:56:44 +0200897/* This function performs the TCP response analysis on the current response. It
898 * returns 1 if the processing can continue on next analysers, or zero if it
899 * needs more data, encounters an error, or wants to immediately abort the
900 * response. It relies on buffers flags, and updates s->rep->analysers. The
901 * function may be called for backend rules.
902 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200903int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200904{
905 struct tcp_rule *rule;
906 int partial;
907
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100908 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 +0200909 now_ms, __FUNCTION__,
910 s,
911 rep,
912 rep->rex, rep->wex,
913 rep->flags,
Cyril Bonté3aaba442012-09-23 14:19:12 +0200914 rep->buf.i,
Emeric Brun97679e72010-09-23 17:56:44 +0200915 rep->analysers);
916
917 /* We don't know whether we have enough data, so must proceed
918 * this way :
919 * - iterate through all rules in their declaration order
920 * - if one rule returns MISS, it means the inspect delay is
921 * not over yet, then return immediately, otherwise consider
922 * it as a non-match.
923 * - if one rule returns OK, then return OK
924 * - if one rule returns KO, then return KO
925 */
926
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200927 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200928 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200929 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200930 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200931
932 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
933 int ret = ACL_PAT_PASS;
934
935 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200936 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200937 if (ret == ACL_PAT_MISS) {
938 /* just set the analyser timeout once at the beginning of the response */
939 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
940 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
941 return 0;
942 }
943
944 ret = acl_pass(ret);
945 if (rule->cond->pol == ACL_COND_UNLESS)
946 ret = !ret;
947 }
948
949 if (ret) {
950 /* we have a matching rule. */
951 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200952 channel_abort(rep);
953 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200954 rep->analysers = 0;
955
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100956 s->be->be_counters.denied_resp++;
957 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200958 if (s->listener->counters)
959 s->listener->counters->denied_resp++;
960
961 if (!(s->flags & SN_ERR_MASK))
962 s->flags |= SN_ERR_PRXCOND;
963 if (!(s->flags & SN_FINST_MASK))
964 s->flags |= SN_FINST_D;
965 return 0;
966 }
967 else {
968 /* otherwise accept */
969 break;
970 }
971 }
972 }
973
974 /* if we get there, it means we have no rule which matches, or
975 * we have an explicit accept, so we apply the default accept.
976 */
977 rep->analysers &= ~an_bit;
978 rep->analyse_exp = TICK_ETERNITY;
979 return 1;
980}
981
982
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200983/* This function performs the TCP layer4 analysis on the current request. It
984 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
985 * matches or if no more rule matches. It can only use rules which don't need
986 * any data.
987 */
988int tcp_exec_req_rules(struct session *s)
989{
990 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200991 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200992 struct stktable *t = NULL;
993 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200994 int ret;
995
996 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
997 ret = ACL_PAT_PASS;
998
999 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001000 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001001 ret = acl_pass(ret);
1002 if (rule->cond->pol == ACL_COND_UNLESS)
1003 ret = !ret;
1004 }
1005
1006 if (ret) {
1007 /* we have a matching rule. */
1008 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001009 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001010 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001011 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001012
1013 if (!(s->flags & SN_ERR_MASK))
1014 s->flags |= SN_ERR_PRXCOND;
1015 if (!(s->flags & SN_FINST_MASK))
1016 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001017 result = 0;
1018 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001019 }
Willy Tarreau56123282010-08-06 19:06:56 +02001020 else if (rule->action == TCP_ACT_TRK_SC1) {
1021 if (!s->stkctr1_entry) {
1022 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001023 * Also, note that right now we can only track SRC so we
1024 * don't check how to get the key, but later we may need
1025 * to consider rule->act_prm->trk_ctr.type.
1026 */
1027 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +02001028 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001029 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001030 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001031 }
1032 }
Willy Tarreau56123282010-08-06 19:06:56 +02001033 else if (rule->action == TCP_ACT_TRK_SC2) {
1034 if (!s->stkctr2_entry) {
1035 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001036 * Also, note that right now we can only track SRC so we
1037 * don't check how to get the key, but later we may need
1038 * to consider rule->act_prm->trk_ctr.type.
1039 */
1040 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau64ee4912012-08-30 22:59:48 +02001041 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn.addr.from));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001042 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001043 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001044 }
1045 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001046 else {
1047 /* otherwise it's an accept */
1048 break;
1049 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001050 }
1051 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001052 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001053}
1054
Emeric Brun97679e72010-09-23 17:56:44 +02001055/* Parse a tcp-response rule. Return a negative value in case of failure */
1056static int tcp_parse_response_rule(char **args, int arg, int section_type,
1057 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001058 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001059{
1060 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001061 memprintf(err, "%s %s is only allowed in 'backend' sections",
1062 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001063 return -1;
1064 }
1065
1066 if (strcmp(args[arg], "accept") == 0) {
1067 arg++;
1068 rule->action = TCP_ACT_ACCEPT;
1069 }
1070 else if (strcmp(args[arg], "reject") == 0) {
1071 arg++;
1072 rule->action = TCP_ACT_REJECT;
1073 }
1074 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001075 memprintf(err,
1076 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1077 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001078 return -1;
1079 }
1080
1081 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001082 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1083 memprintf(err,
1084 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1085 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001086 return -1;
1087 }
1088 }
1089 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001090 memprintf(err,
1091 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001092 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1093 return -1;
1094 }
1095 return 0;
1096}
1097
1098
1099
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001100/* Parse a tcp-request rule. Return a negative value in case of failure */
1101static int tcp_parse_request_rule(char **args, int arg, int section_type,
1102 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001103 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001104{
1105 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001106 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1107 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001108 return -1;
1109 }
1110
1111 if (!strcmp(args[arg], "accept")) {
1112 arg++;
1113 rule->action = TCP_ACT_ACCEPT;
1114 }
1115 else if (!strcmp(args[arg], "reject")) {
1116 arg++;
1117 rule->action = TCP_ACT_REJECT;
1118 }
Willy Tarreau56123282010-08-06 19:06:56 +02001119 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001120 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001121 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001122
1123 arg++;
1124 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001125 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001126
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001127 if (ret < 0) { /* nb: warnings are not handled yet */
1128 memprintf(err,
1129 "'%s %s %s' : %s in %s '%s'",
1130 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1131 return ret;
1132 }
Willy Tarreau56123282010-08-06 19:06:56 +02001133 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001134 }
Willy Tarreau56123282010-08-06 19:06:56 +02001135 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001136 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001137 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001138
1139 arg++;
1140 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001141 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001142
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001143 if (ret < 0) { /* nb: warnings are not handled yet */
1144 memprintf(err,
1145 "'%s %s %s' : %s in %s '%s'",
1146 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1147 return ret;
1148 }
Willy Tarreau56123282010-08-06 19:06:56 +02001149 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001150 }
1151 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001152 memprintf(err,
1153 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1154 "or 'track-sc2' in %s '%s' (got '%s')",
1155 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001156 return -1;
1157 }
1158
1159 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001160 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1161 memprintf(err,
1162 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1163 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001164 return -1;
1165 }
1166 }
1167 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001168 memprintf(err,
1169 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001170 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1171 return -1;
1172 }
1173 return 0;
1174}
1175
Emeric Brun97679e72010-09-23 17:56:44 +02001176/* This function should be called to parse a line starting with the "tcp-response"
1177 * keyword.
1178 */
1179static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001180 struct proxy *defpx, const char *file, int line,
1181 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001182{
1183 const char *ptr = NULL;
1184 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001185 int warn = 0;
1186 int arg;
1187 struct tcp_rule *rule;
1188
1189 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001190 memprintf(err, "missing argument for '%s' in %s '%s'",
1191 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001192 return -1;
1193 }
1194
1195 if (strcmp(args[1], "inspect-delay") == 0) {
1196 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001197 memprintf(err, "%s %s is only allowed in 'backend' sections",
1198 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001199 return -1;
1200 }
1201
1202 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001203 memprintf(err,
1204 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1205 args[0], args[1], proxy_type_str(curpx), curpx->id);
1206 if (ptr)
1207 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001208 return -1;
1209 }
1210
1211 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001212 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1213 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001214 return 1;
1215 }
1216 curpx->tcp_rep.inspect_delay = val;
1217 return 0;
1218 }
1219
Simon Hormande072bd2011-06-24 15:11:37 +09001220 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001221 LIST_INIT(&rule->list);
1222 arg = 1;
1223
1224 if (strcmp(args[1], "content") == 0) {
1225 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001226 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001227 goto error;
1228
1229 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1230 struct acl *acl;
1231 const char *name;
1232
1233 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1234 name = acl ? acl->name : "(unknown)";
1235
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001236 memprintf(err,
1237 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1238 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001239 warn++;
1240 }
1241
1242 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1243 }
1244 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001245 memprintf(err,
1246 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1247 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001248 goto error;
1249 }
1250
1251 return warn;
1252 error:
1253 free(rule);
1254 return -1;
1255}
1256
1257
Willy Tarreaub6866442008-07-14 23:54:42 +02001258/* This function should be called to parse a line starting with the "tcp-request"
1259 * keyword.
1260 */
1261static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001262 struct proxy *defpx, const char *file, int line,
1263 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001264{
1265 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001266 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001267 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001268 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001269 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001270
1271 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001272 if (curpx == defpx)
1273 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1274 else
1275 memprintf(err, "missing argument for '%s' in %s '%s'",
1276 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001277 return -1;
1278 }
1279
1280 if (!strcmp(args[1], "inspect-delay")) {
1281 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001282 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1283 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001284 return -1;
1285 }
1286
Willy Tarreaub6866442008-07-14 23:54:42 +02001287 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001288 memprintf(err,
1289 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1290 args[0], args[1], proxy_type_str(curpx), curpx->id);
1291 if (ptr)
1292 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001293 return -1;
1294 }
1295
1296 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001297 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1298 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001299 return 1;
1300 }
1301 curpx->tcp_req.inspect_delay = val;
1302 return 0;
1303 }
1304
Simon Hormande072bd2011-06-24 15:11:37 +09001305 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001306 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001307 arg = 1;
1308
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001309 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001310 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001311 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001312 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001313
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001314 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001315 struct acl *acl;
1316 const char *name;
1317
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001318 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001319 name = acl ? acl->name : "(unknown)";
1320
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001321 memprintf(err,
1322 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1323 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001324 warn++;
1325 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001326 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001327 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001328 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001329 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001330
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001331 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001332 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1333 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001334 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001335 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001336
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001337 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001338 goto error;
1339
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001340 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1341 struct acl *acl;
1342 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001343
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001344 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1345 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001346
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001347 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001348 memprintf(err,
1349 "'%s %s' may not reference acl '%s' which makes use of "
1350 "payload in %s '%s'. Please use '%s content' for this.",
1351 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001352 goto error;
1353 }
1354 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001355 memprintf(err,
1356 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1357 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001358 warn++;
1359 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001360 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001361 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001362 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001363 if (curpx == defpx)
1364 memprintf(err,
1365 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1366 args[0], args[1]);
1367 else
1368 memprintf(err,
1369 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1370 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001371 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001372 }
1373
Willy Tarreau1a687942010-05-23 22:40:30 +02001374 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001375 error:
1376 free(rule);
1377 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001378}
1379
Willy Tarreau645513a2010-05-24 20:55:15 +02001380
1381/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001382/* All supported sample fetch functios must be declared here */
1383/************************************************************************/
1384
1385/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001386 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001387 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1388 * is a string (cookie name), other types will lead to undefined behaviour.
1389 */
1390int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001391smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001392 const struct arg *args, struct sample *smp)
1393{
1394 int bleft;
1395 const unsigned char *data;
1396
1397 if (!l4 || !l4->req)
1398 return 0;
1399
1400 smp->flags = 0;
1401 smp->type = SMP_T_CSTR;
1402
Willy Tarreau572bf902012-07-02 17:01:20 +02001403 bleft = l4->req->buf.i;
Willy Tarreau32389b72012-04-23 23:13:20 +02001404 if (bleft <= 11)
1405 goto too_short;
1406
Willy Tarreau572bf902012-07-02 17:01:20 +02001407 data = (const unsigned char *)l4->req->buf.p + 11;
Willy Tarreau32389b72012-04-23 23:13:20 +02001408 bleft -= 11;
1409
1410 if (bleft <= 7)
1411 goto too_short;
1412
1413 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1414 goto not_cookie;
1415
1416 data += 7;
1417 bleft -= 7;
1418
1419 while (bleft > 0 && *data == ' ') {
1420 data++;
1421 bleft--;
1422 }
1423
1424 if (args) {
1425
1426 if (bleft <= args->data.str.len)
1427 goto too_short;
1428
1429 if ((data[args->data.str.len] != '=') ||
1430 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1431 goto not_cookie;
1432
1433 data += args->data.str.len + 1;
1434 bleft -= args->data.str.len + 1;
1435 } else {
1436 while (bleft > 0 && *data != '=') {
1437 if (*data == '\r' || *data == '\n')
1438 goto not_cookie;
1439 data++;
1440 bleft--;
1441 }
1442
1443 if (bleft < 1)
1444 goto too_short;
1445
1446 if (*data != '=')
1447 goto not_cookie;
1448
1449 data++;
1450 bleft--;
1451 }
1452
1453 /* data points to cookie value */
1454 smp->data.str.str = (char *)data;
1455 smp->data.str.len = 0;
1456
1457 while (bleft > 0 && *data != '\r') {
1458 data++;
1459 bleft--;
1460 }
1461
1462 if (bleft < 2)
1463 goto too_short;
1464
1465 if (data[0] != '\r' || data[1] != '\n')
1466 goto not_cookie;
1467
1468 smp->data.str.len = (char *)data - smp->data.str.str;
1469 smp->flags = SMP_F_VOLATILE;
1470 return 1;
1471
1472 too_short:
1473 smp->flags = SMP_F_MAY_CHANGE;
1474 not_cookie:
1475 return 0;
1476}
1477
Willy Tarreau32389b72012-04-23 23:13:20 +02001478/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001479/* All supported ACL keywords must be declared here. */
1480/************************************************************************/
1481
Willy Tarreau32389b72012-04-23 23:13:20 +02001482/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1483static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001484acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001485 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001486{
1487 int ret;
1488
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001489 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001490
1491 if (smp->flags & SMP_F_MAY_CHANGE)
1492 return 0;
1493
1494 smp->flags = SMP_F_VOLATILE;
1495 smp->type = SMP_T_UINT;
1496 smp->data.uint = ret;
1497 return 1;
1498}
1499
1500
Willy Tarreau4a129812012-04-25 17:31:42 +02001501/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001502static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001503smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001504 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001505{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001506 switch (l4->si[0].conn.addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001507 case AF_INET:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001508 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn.addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001509 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001510 break;
1511 case AF_INET6:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001512 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn.addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001513 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001514 break;
1515 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001516 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001517 }
Emeric Brunf769f512010-10-22 17:14:01 +02001518
Willy Tarreau37406352012-04-23 16:16:37 +02001519 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001520 return 1;
1521}
1522
Willy Tarreaua5e37562011-12-16 17:06:15 +01001523/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001524static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001525smp_fetch_sport(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 Tarreauf853c462012-04-23 18:53:56 +02001528 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02001529 if (!(smp->data.uint = get_host_port(&l4->si[0].conn.addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001530 return 0;
1531
Willy Tarreau37406352012-04-23 16:16:37 +02001532 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001533 return 1;
1534}
1535
Willy Tarreau4a129812012-04-25 17:31:42 +02001536/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001537static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001538smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001539 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001540{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001541 conn_get_to_addr(&l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001542
Willy Tarreau986a9d22012-08-30 21:11:38 +02001543 switch (l4->si[0].conn.addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001544 case AF_INET:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001545 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn.addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001546 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001547 break;
1548 case AF_INET6:
Willy Tarreau986a9d22012-08-30 21:11:38 +02001549 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn.addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001550 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001551 break;
1552 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001553 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001554 }
Emeric Brunf769f512010-10-22 17:14:01 +02001555
Willy Tarreau37406352012-04-23 16:16:37 +02001556 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001557 return 1;
1558}
1559
Willy Tarreaua5e37562011-12-16 17:06:15 +01001560/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001561static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001562smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001563 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001564{
Willy Tarreau986a9d22012-08-30 21:11:38 +02001565 conn_get_to_addr(&l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001566
Willy Tarreauf853c462012-04-23 18:53:56 +02001567 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02001568 if (!(smp->data.uint = get_host_port(&l4->si[0].conn.addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001569 return 0;
1570
Willy Tarreau37406352012-04-23 16:16:37 +02001571 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001572 return 1;
1573}
1574
1575static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001576smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1577 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001578{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001579 unsigned int len_offset = arg_p[0].data.uint;
1580 unsigned int len_size = arg_p[1].data.uint;
1581 unsigned int buf_offset;
1582 unsigned int buf_size = 0;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001583 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001584 int i;
1585
1586 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1587 /* by default buf offset == len offset + len size */
1588 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1589
1590 if (!l4)
1591 return 0;
1592
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001593 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001594
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001595 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001596 return 0;
1597
Willy Tarreau572bf902012-07-02 17:01:20 +02001598 if (len_offset + len_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001599 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001600
1601 for (i = 0; i < len_size; i++) {
Willy Tarreau572bf902012-07-02 17:01:20 +02001602 buf_size = (buf_size << 8) + ((unsigned char *)b->buf.p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001603 }
1604
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001605 /* buf offset may be implicit, absolute or relative */
1606 buf_offset = len_offset + len_size;
1607 if (arg_p[2].type == ARGT_UINT)
1608 buf_offset = arg_p[2].data.uint;
1609 else if (arg_p[2].type == ARGT_SINT)
1610 buf_offset += arg_p[2].data.sint;
1611
Willy Tarreau572bf902012-07-02 17:01:20 +02001612 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001613 /* will never match */
1614 smp->flags = 0;
1615 return 0;
1616 }
1617
Willy Tarreau572bf902012-07-02 17:01:20 +02001618 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001619 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001620
1621 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001622 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001623 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001624 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001625 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001626
1627 too_short:
1628 smp->flags = SMP_F_MAY_CHANGE;
1629 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001630}
1631
1632static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001633smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1634 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001635{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001636 unsigned int buf_offset = arg_p[0].data.uint;
1637 unsigned int buf_size = arg_p[1].data.uint;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001638 struct channel *b;
Emericf2d7cae2010-11-05 18:13:50 +01001639
1640 if (!l4)
1641 return 0;
1642
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001643 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001644
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001645 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001646 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001647
Willy Tarreau572bf902012-07-02 17:01:20 +02001648 if (!buf_size || buf_size > b->buf.size || buf_offset + buf_size > b->buf.size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001649 /* will never match */
1650 smp->flags = 0;
1651 return 0;
1652 }
Emericf2d7cae2010-11-05 18:13:50 +01001653
Willy Tarreau572bf902012-07-02 17:01:20 +02001654 if (buf_offset + buf_size > b->buf.i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001655 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001656
1657 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001658 smp->type = SMP_T_CBIN;
Willy Tarreau572bf902012-07-02 17:01:20 +02001659 chunk_initlen(&smp->data.str, b->buf.p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001660 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001661 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001662
1663 too_short:
1664 smp->flags = SMP_F_MAY_CHANGE;
1665 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001666}
1667
Willy Tarreau21d68a62012-04-20 15:52:36 +02001668/* This function is used to validate the arguments passed to a "payload" fetch
1669 * keyword. This keyword expects two positive integers, with the second one
1670 * being strictly positive. It is assumed that the types are already the correct
1671 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1672 * filled with a pointer to an error message in case of error, that the caller
1673 * is responsible for freeing. The initial location must either be freeable or
1674 * NULL.
1675 */
1676static int val_payload(struct arg *arg, char **err_msg)
1677{
1678 if (!arg[1].data.uint) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001679 memprintf(err_msg, "payload length must be > 0");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001680 return 0;
1681 }
1682 return 1;
1683}
1684
1685/* This function is used to validate the arguments passed to a "payload_lv" fetch
1686 * keyword. This keyword allows two positive integers and an optional signed one,
1687 * with the second one being strictly positive and the third one being greater than
1688 * the opposite of the two others if negative. It is assumed that the types are
1689 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1690 * not NULL, it will be filled with a pointer to an error message in case of
1691 * error, that the caller is responsible for freeing. The initial location must
1692 * either be freeable or NULL.
1693 */
1694static int val_payload_lv(struct arg *arg, char **err_msg)
1695{
1696 if (!arg[1].data.uint) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001697 memprintf(err_msg, "payload length must be > 0");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001698 return 0;
1699 }
1700
1701 if (arg[2].type == ARGT_SINT &&
1702 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001703 memprintf(err_msg, "payload offset too negative");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001704 return 0;
1705 }
1706 return 1;
1707}
1708
Willy Tarreau44791242012-09-12 23:27:21 +02001709#ifdef CONFIG_HAP_LINUX_TPROXY
1710/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001711static 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 +02001712{
1713 struct listener *l;
1714
Willy Tarreau4348fad2012-09-20 16:48:07 +02001715 list_for_each_entry(l, &conf->listeners, by_bind) {
1716 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1717 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001718 }
1719
Willy Tarreau44791242012-09-12 23:27:21 +02001720 return 0;
1721}
1722#endif
1723
1724#ifdef TCP_DEFER_ACCEPT
1725/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001726static 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 +02001727{
1728 struct listener *l;
1729
Willy Tarreau4348fad2012-09-20 16:48:07 +02001730 list_for_each_entry(l, &conf->listeners, by_bind) {
1731 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1732 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001733 }
1734
Willy Tarreau44791242012-09-12 23:27:21 +02001735 return 0;
1736}
1737#endif
1738
Willy Tarreau1c862c52012-10-05 16:21:00 +02001739#ifdef TCP_FASTOPEN
1740/* parse the "defer-accept" bind keyword */
1741static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1742{
1743 struct listener *l;
1744
1745 list_for_each_entry(l, &conf->listeners, by_bind) {
1746 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1747 l->options |= LI_O_TCP_FO;
1748 }
1749
1750 return 0;
1751}
1752#endif
1753
Willy Tarreau44791242012-09-12 23:27:21 +02001754#ifdef TCP_MAXSEG
1755/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001756static 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 +02001757{
1758 struct listener *l;
1759 int mss;
1760
Willy Tarreau44791242012-09-12 23:27:21 +02001761 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001762 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001763 return ERR_ALERT | ERR_FATAL;
1764 }
1765
1766 mss = atoi(args[cur_arg + 1]);
1767 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001768 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001769 return ERR_ALERT | ERR_FATAL;
1770 }
1771
Willy Tarreau4348fad2012-09-20 16:48:07 +02001772 list_for_each_entry(l, &conf->listeners, by_bind) {
1773 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1774 l->maxseg = mss;
1775 }
Willy Tarreau44791242012-09-12 23:27:21 +02001776
1777 return 0;
1778}
1779#endif
1780
1781#ifdef SO_BINDTODEVICE
1782/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001783static 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 +02001784{
1785 struct listener *l;
1786
Willy Tarreau44791242012-09-12 23:27:21 +02001787 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001788 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001789 return ERR_ALERT | ERR_FATAL;
1790 }
1791
Willy Tarreau4348fad2012-09-20 16:48:07 +02001792 list_for_each_entry(l, &conf->listeners, by_bind) {
1793 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1794 l->interface = strdup(args[cur_arg + 1]);
1795 }
Willy Tarreau44791242012-09-12 23:27:21 +02001796
1797 global.last_checks |= LSTCHK_NETADM;
1798 return 0;
1799}
1800#endif
1801
Willy Tarreaub6866442008-07-14 23:54:42 +02001802static struct cfg_kw_list cfg_kws = {{ },{
1803 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001804 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001805 { 0, NULL, NULL },
1806}};
1807
Willy Tarreau61612d42012-04-19 18:42:05 +02001808/* Note: must not be declared <const> as its list will be overwritten.
1809 * Please take care of keeping this list alphabetically sorted.
1810 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001811static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001812 { "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 +02001813 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001814 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1815 { "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 +02001816 { "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 +02001817 { "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 +02001818 { "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 +02001819 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001820 { NULL, NULL, NULL, NULL },
1821}};
1822
Willy Tarreau4a129812012-04-25 17:31:42 +02001823/* Note: must not be declared <const> as its list will be overwritten.
1824 * Note: fetches that may return multiple types must be declared as the lowest
1825 * common denominator, the type that can be casted into all other ones. For
1826 * instance v4/v6 must be declared v4.
1827 */
Willy Tarreau12785782012-04-27 21:37:17 +02001828static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001829 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001830 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001831 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001832 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1833 { "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 +02001834 { "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 +02001835 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001836 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001837}};
1838
Willy Tarreau44791242012-09-12 23:27:21 +02001839/************************************************************************/
1840/* All supported bind keywords must be declared here. */
1841/************************************************************************/
1842
1843/* Note: must not be declared <const> as its list will be overwritten.
1844 * Please take care of keeping this list alphabetically sorted, doing so helps
1845 * all code contributors.
1846 * Optional keywords are also declared with a NULL ->parse() function so that
1847 * the config parser can report an appropriate error when a known keyword was
1848 * not enabled.
1849 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001850static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001851#ifdef TCP_DEFER_ACCEPT
1852 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1853#endif
1854#ifdef SO_BINDTODEVICE
1855 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1856#endif
1857#ifdef TCP_MAXSEG
1858 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1859#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001860#ifdef TCP_FASTOPEN
1861 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1862#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001863#ifdef CONFIG_HAP_LINUX_TPROXY
1864 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1865#endif
1866 /* the versions with the NULL parse function*/
1867 { "defer-accept", NULL, 0 },
1868 { "interface", NULL, 1 },
1869 { "mss", NULL, 1 },
1870 { "transparent", NULL, 0 },
1871 { NULL, NULL, 0 },
1872}};
1873
Willy Tarreaue6b98942007-10-29 01:09:36 +01001874__attribute__((constructor))
1875static void __tcp_protocol_init(void)
1876{
1877 protocol_register(&proto_tcpv4);
1878 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001879 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001880 cfg_register_keywords(&cfg_kws);
1881 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001882 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001883}
1884
1885
1886/*
1887 * Local variables:
1888 * c-indent-level: 8
1889 * c-basic-offset: 8
1890 * End:
1891 */