blob: 913e04efb27b1ee11f1d2962c16e81b4f1de0310 [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 Tarreaue6b98942007-10-29 01:09:36 +010041#include <proto/buffers.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020042#include <proto/frontend.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020043#include <proto/log.h>
Willy Tarreau645513a2010-05-24 20:55:15 +020044#include <proto/pattern.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020045#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010046#include <proto/protocols.h>
47#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proxy.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020049#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020050#include <proto/stick_table.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010051#include <proto/stream_sock.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020052#include <proto/task.h>
Emeric Brun97679e72010-09-23 17:56:44 +020053#include <proto/buffers.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010054
Willy Tarreaue8c66af2008-01-13 18:40:14 +010055#ifdef CONFIG_HAP_CTTPROXY
56#include <import/ip_tproxy.h>
57#endif
58
Emeric Bruncf20bf12010-10-22 16:06:11 +020059static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
60static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010061
62/* Note: must not be declared <const> as its list will be overwritten */
63static struct protocol proto_tcpv4 = {
64 .name = "tcpv4",
65 .sock_domain = AF_INET,
66 .sock_type = SOCK_STREAM,
67 .sock_prot = IPPROTO_TCP,
68 .sock_family = AF_INET,
69 .sock_addrlen = sizeof(struct sockaddr_in),
70 .l3_addrlen = 32/8,
Willy Tarreaueb472682010-05-28 18:46:57 +020071 .accept = &stream_sock_accept,
Willy Tarreaue6b98942007-10-29 01:09:36 +010072 .read = &stream_sock_read,
73 .write = &stream_sock_write,
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,
78 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
79 .nb_listeners = 0,
80};
81
82/* Note: must not be declared <const> as its list will be overwritten */
83static struct protocol proto_tcpv6 = {
84 .name = "tcpv6",
85 .sock_domain = AF_INET6,
86 .sock_type = SOCK_STREAM,
87 .sock_prot = IPPROTO_TCP,
88 .sock_family = AF_INET6,
89 .sock_addrlen = sizeof(struct sockaddr_in6),
90 .l3_addrlen = 128/8,
Willy Tarreaueb472682010-05-28 18:46:57 +020091 .accept = &stream_sock_accept,
Willy Tarreaue6b98942007-10-29 01:09:36 +010092 .read = &stream_sock_read,
93 .write = &stream_sock_write,
Emeric Bruncf20bf12010-10-22 16:06:11 +020094 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010095 .bind_all = tcp_bind_listeners,
96 .unbind_all = unbind_all_listeners,
97 .enable_all = enable_all_listeners,
98 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
99 .nb_listeners = 0,
100};
101
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100102
David du Colombier6f5ccb12011-03-10 22:26:24 +0100103/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100104 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
105 * - 0 : ignore remote address (may even be a NULL pointer)
106 * - 1 : use provided address
107 * - 2 : use provided port
108 * - 3 : use both
109 *
110 * The function supports multiple foreign binding methods :
111 * - linux_tproxy: we directly bind to the foreign address
112 * - cttproxy: we bind to a local address then nat.
113 * The second one can be used as a fallback for the first one.
114 * This function returns 0 when everything's OK, 1 if it could not bind, to the
115 * local address, 2 if it could not bind to the foreign address.
116 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100117int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100118{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100119 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100120 int foreign_ok = 0;
121 int ret;
122
123#ifdef CONFIG_HAP_LINUX_TPROXY
124 static int ip_transp_working = 1;
125 if (flags && ip_transp_working) {
Simon Hormande072bd2011-06-24 15:11:37 +0900126 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
127 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100128 foreign_ok = 1;
129 else
130 ip_transp_working = 0;
131 }
132#endif
133 if (flags) {
134 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200135 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100136 switch (remote->ss_family) {
137 case AF_INET:
138 if (flags & 1)
139 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
140 if (flags & 2)
141 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
142 break;
143 case AF_INET6:
144 if (flags & 1)
145 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
146 if (flags & 2)
147 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
148 break;
149 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100150 }
151
Simon Hormande072bd2011-06-24 15:11:37 +0900152 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100153 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200154 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100155 if (ret < 0)
156 return 2;
157 }
158 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200159 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100160 if (ret < 0)
161 return 1;
162 }
163
164 if (!flags)
165 return 0;
166
167#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100168 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100169 struct in_tproxy itp1, itp2;
170 memset(&itp1, 0, sizeof(itp1));
171
172 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100173 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
174 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100175
176 /* set connect flag on socket */
177 itp2.op = TPROXY_FLAGS;
178 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
179
180 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
181 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
182 foreign_ok = 1;
183 }
184 }
185#endif
186 if (!foreign_ok)
187 /* we could not bind to a foreign address */
188 return 2;
189
190 return 0;
191}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100192
Willy Tarreau9650f372009-08-16 14:02:45 +0200193
194/*
Willy Tarreauac825402011-03-04 22:04:29 +0100195 * This function initiates a connection to the target assigned to this session
196 * (si->{target,addr.s.to}). A source address may be pointed to by si->addr.s.from
197 * in case of transparent proxying. Normal source bind addresses are still
198 * determined locally (due to the possible need of a source port).
199 * si->target may point either to a valid server or to a backend, depending
200 * on si->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are supported.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200201 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200202 * It can return one of :
203 * - SN_ERR_NONE if everything's OK
204 * - SN_ERR_SRVTO if there are no more servers
205 * - SN_ERR_SRVCL if the connection was refused by the server
206 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
207 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
208 * - SN_ERR_INTERNAL for any other purely internal errors
209 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
210 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100211
David du Colombier6f5ccb12011-03-10 22:26:24 +0100212int tcp_connect_server(struct stream_interface *si)
Willy Tarreau9650f372009-08-16 14:02:45 +0200213{
214 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100215 struct server *srv;
216 struct proxy *be;
217
218 switch (si->target.type) {
219 case TARG_TYPE_PROXY:
220 be = si->target.ptr.p;
221 srv = NULL;
222 break;
223 case TARG_TYPE_SERVER:
224 srv = si->target.ptr.s;
225 be = srv->proxy;
226 break;
227 default:
228 return SN_ERR_INTERNAL;
229 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200230
David du Colombier6f5ccb12011-03-10 22:26:24 +0100231 if ((fd = si->fd = socket(si->addr.s.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200232 qfprintf(stderr, "Cannot get a server socket.\n");
233
234 if (errno == ENFILE)
235 send_log(be, LOG_EMERG,
236 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
237 be->id, maxfd);
238 else if (errno == EMFILE)
239 send_log(be, LOG_EMERG,
240 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
241 be->id, maxfd);
242 else if (errno == ENOBUFS || errno == ENOMEM)
243 send_log(be, LOG_EMERG,
244 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
245 be->id, maxfd);
246 /* this is a resource error */
247 return SN_ERR_RESOURCE;
248 }
249
250 if (fd >= global.maxsock) {
251 /* do not log anything there, it's a normal condition when this option
252 * is used to serialize connections to a server !
253 */
254 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
255 close(fd);
256 return SN_ERR_PRXCOND; /* it is a configuration limit */
257 }
258
259 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900260 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200261 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
262 close(fd);
263 return SN_ERR_INTERNAL;
264 }
265
266 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900267 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200268
269 if (be->options & PR_O_TCP_NOLING)
Simon Hormande072bd2011-06-24 15:11:37 +0900270 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreau9650f372009-08-16 14:02:45 +0200271
272 /* allow specific binding :
273 * - server-specific at first
274 * - proxy-specific next
275 */
276 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200277 int ret, flags = 0;
278
Willy Tarreau9650f372009-08-16 14:02:45 +0200279 switch (srv->state & SRV_TPROXY_MASK) {
280 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200281 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200282 flags = 3;
283 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200284 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200285 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200286 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200287 break;
288 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200289
Willy Tarreau9650f372009-08-16 14:02:45 +0200290#ifdef SO_BINDTODEVICE
291 /* Note: this might fail if not CAP_NET_RAW */
292 if (srv->iface_name)
293 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
294#endif
295
296 if (srv->sport_range) {
297 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100298 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200299
300 ret = 1;
301 src = srv->source_addr;
302
303 do {
304 /* note: in case of retry, we may have to release a previously
305 * allocated port, hence this loop's construct.
306 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200307 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
308 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200309
310 if (!attempts)
311 break;
312 attempts--;
313
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200314 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
315 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200316 break;
317
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200318 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200319 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200320
David du Colombier6f5ccb12011-03-10 22:26:24 +0100321 ret = tcp_bind_socket(fd, flags, &src, &si->addr.s.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200322 } while (ret != 0); /* binding NOK */
323 }
324 else {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100325 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.s.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200326 }
327
328 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200329 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
330 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200331 close(fd);
332
333 if (ret == 1) {
334 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
335 be->id, srv->id);
336 send_log(be, LOG_EMERG,
337 "Cannot bind to source address before connect() for server %s/%s.\n",
338 be->id, srv->id);
339 } else {
340 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
341 be->id, srv->id);
342 send_log(be, LOG_EMERG,
343 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
344 be->id, srv->id);
345 }
346 return SN_ERR_RESOURCE;
347 }
348 }
349 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200350 int ret, flags = 0;
351
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 switch (be->options & PR_O_TPXY_MASK) {
353 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200354 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200355 flags = 3;
356 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200357 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200358 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200359 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200360 break;
361 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200362
Willy Tarreau9650f372009-08-16 14:02:45 +0200363#ifdef SO_BINDTODEVICE
364 /* Note: this might fail if not CAP_NET_RAW */
365 if (be->iface_name)
366 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
367#endif
David du Colombier6f5ccb12011-03-10 22:26:24 +0100368 ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.s.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200369 if (ret) {
370 close(fd);
371 if (ret == 1) {
372 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
373 be->id);
374 send_log(be, LOG_EMERG,
375 "Cannot bind to source address before connect() for proxy %s.\n",
376 be->id);
377 } else {
378 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
379 be->id);
380 send_log(be, LOG_EMERG,
381 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
382 be->id);
383 }
384 return SN_ERR_RESOURCE;
385 }
386 }
387
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400388#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200389 /* disabling tcp quick ack now allows the first request to leave the
390 * machine with the first ACK. We only do this if there are pending
391 * data in the buffer.
392 */
393 if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max)
Simon Hormande072bd2011-06-24 15:11:37 +0900394 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200395#endif
396
Willy Tarreaue803de22010-01-21 17:43:04 +0100397 if (global.tune.server_sndbuf)
398 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
399
400 if (global.tune.server_rcvbuf)
401 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
402
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200403 if ((connect(fd, (struct sockaddr *)&si->addr.s.to, get_addr_len(&si->addr.s.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200404 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
405
406 if (errno == EAGAIN || errno == EADDRINUSE) {
407 char *msg;
408 if (errno == EAGAIN) /* no free ports left, try again later */
409 msg = "no free ports";
410 else
411 msg = "local address already in use";
412
413 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200414 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
415 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200416 close(fd);
417 send_log(be, LOG_EMERG,
418 "Connect() failed for server %s/%s: %s.\n",
419 be->id, srv->id, msg);
420 return SN_ERR_RESOURCE;
421 } else if (errno == ETIMEDOUT) {
422 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200423 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
424 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200425 close(fd);
426 return SN_ERR_SRVTO;
427 } else {
428 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
429 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200430 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
431 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200432 close(fd);
433 return SN_ERR_SRVCL;
434 }
435 }
436
437 fdtab[fd].owner = si;
438 fdtab[fd].state = FD_STCONN; /* connection in progress */
439 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
440 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
441 fdtab[fd].cb[DIR_RD].b = si->ib;
442 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
443 fdtab[fd].cb[DIR_WR].b = si->ob;
444
Willy Tarreauf1536862011-03-03 18:27:32 +0100445 fdinfo[fd].peeraddr = (struct sockaddr *)&si->addr.s.to;
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200446 fdinfo[fd].peerlen = get_addr_len(&si->addr.s.to);
Willy Tarreau9650f372009-08-16 14:02:45 +0200447
448 fd_insert(fd);
449 EV_FD_SET(fd, DIR_WR); /* for connect status */
450
451 si->state = SI_ST_CON;
452 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
453 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
454
455 return SN_ERR_NONE; /* connection is OK */
456}
457
458
Willy Tarreaue6b98942007-10-29 01:09:36 +0100459/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
460 * an error message in <err> if the message is at most <errlen> bytes long
461 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
462 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
463 * was alright and that no message was returned. ERR_RETRYABLE means that an
464 * error occurred but that it may vanish after a retry (eg: port in use), and
465 * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter
466 * the meaning of the error, but just indicate that a message is present which
467 * should be displayed with the respective level. Last, ERR_ABORT indicates
468 * that it's pointless to try to start other listeners. No error message is
469 * returned if errlen is NULL.
470 */
471int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
472{
473 __label__ tcp_return, tcp_close_return;
474 int fd, err;
475 const char *msg = NULL;
476
477 /* ensure we never return garbage */
478 if (errmsg && errlen)
479 *errmsg = 0;
480
481 if (listener->state != LI_ASSIGNED)
482 return ERR_NONE; /* already bound */
483
484 err = ERR_NONE;
485
486 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
487 err |= ERR_RETRYABLE | ERR_ALERT;
488 msg = "cannot create listening socket";
489 goto tcp_return;
490 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100491
Willy Tarreaue6b98942007-10-29 01:09:36 +0100492 if (fd >= global.maxsock) {
493 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
494 msg = "not enough free sockets (raise '-n' parameter)";
495 goto tcp_close_return;
496 }
497
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200498 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100499 err |= ERR_FATAL | ERR_ALERT;
500 msg = "cannot make socket non-blocking";
501 goto tcp_close_return;
502 }
503
Simon Hormande072bd2011-06-24 15:11:37 +0900504 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100505 /* not fatal but should be reported */
506 msg = "cannot do so_reuseaddr";
507 err |= ERR_ALERT;
508 }
509
510 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900511 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100512
Willy Tarreaue6b98942007-10-29 01:09:36 +0100513#ifdef SO_REUSEPORT
514 /* OpenBSD supports this. As it's present in old libc versions of Linux,
515 * it might return an error that we will silently ignore.
516 */
Simon Hormande072bd2011-06-24 15:11:37 +0900517 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100518#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100519#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100520 if ((listener->options & LI_O_FOREIGN)
Simon Hormande072bd2011-06-24 15:11:37 +0900521 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
522 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100523 msg = "cannot make listening socket transparent";
524 err |= ERR_ALERT;
525 }
526#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100527#ifdef SO_BINDTODEVICE
528 /* Note: this might fail if not CAP_NET_RAW */
529 if (listener->interface) {
530 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100531 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100532 msg = "cannot bind listener to device";
533 err |= ERR_WARN;
534 }
535 }
536#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400537#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100538 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400539 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200540 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
541 msg = "cannot set MSS";
542 err |= ERR_WARN;
543 }
544 }
545#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200546#if defined(TCP_DEFER_ACCEPT)
547 if (listener->options & LI_O_DEF_ACCEPT) {
548 /* defer accept by up to one second */
549 int accept_delay = 1;
550 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
551 msg = "cannot enable DEFER_ACCEPT";
552 err |= ERR_WARN;
553 }
554 }
555#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100556 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
557 err |= ERR_RETRYABLE | ERR_ALERT;
558 msg = "cannot bind socket";
559 goto tcp_close_return;
560 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100561
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100562 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100563 err |= ERR_RETRYABLE | ERR_ALERT;
564 msg = "cannot listen to socket";
565 goto tcp_close_return;
566 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100567
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400568#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200569 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900570 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200571#endif
572
Willy Tarreaue6b98942007-10-29 01:09:36 +0100573 /* the socket is ready */
574 listener->fd = fd;
575 listener->state = LI_LISTEN;
576
Willy Tarreaueabf3132008-08-29 23:36:51 +0200577 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100578 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaueb472682010-05-28 18:46:57 +0200579 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
580 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
581 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
582 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200583
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200584 fdinfo[fd].peeraddr = NULL;
585 fdinfo[fd].peerlen = 0;
Willy Tarreaueb472682010-05-28 18:46:57 +0200586 fd_insert(fd);
587
Willy Tarreaue6b98942007-10-29 01:09:36 +0100588 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100589 if (msg && errlen) {
590 char pn[INET6_ADDRSTRLEN];
591
Willy Tarreau631f01c2011-09-05 00:36:48 +0200592 addr_to_str(&listener->addr, pn, sizeof(pn));
593 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100594 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100595 return err;
596
597 tcp_close_return:
598 close(fd);
599 goto tcp_return;
600}
601
602/* This function creates all TCP sockets bound to the protocol entry <proto>.
603 * It is intended to be used as the protocol's bind_all() function.
604 * The sockets will be registered but not added to any fd_set, in order not to
605 * loose them across the fork(). A call to enable_all_listeners() is needed
606 * to complete initialization. The return value is composed from ERR_*.
607 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200608static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100609{
610 struct listener *listener;
611 int err = ERR_NONE;
612
613 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200614 err |= tcp_bind_listener(listener, errmsg, errlen);
615 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100616 break;
617 }
618
619 return err;
620}
621
622/* Add listener to the list of tcpv4 listeners. The listener's state
623 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
624 * listeners is updated. This is the function to use to add a new listener.
625 */
626void tcpv4_add_listener(struct listener *listener)
627{
628 if (listener->state != LI_INIT)
629 return;
630 listener->state = LI_ASSIGNED;
631 listener->proto = &proto_tcpv4;
632 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
633 proto_tcpv4.nb_listeners++;
634}
635
636/* Add listener to the list of tcpv4 listeners. The listener's state
637 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
638 * listeners is updated. This is the function to use to add a new listener.
639 */
640void tcpv6_add_listener(struct listener *listener)
641{
642 if (listener->state != LI_INIT)
643 return;
644 listener->state = LI_ASSIGNED;
645 listener->proto = &proto_tcpv6;
646 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
647 proto_tcpv6.nb_listeners++;
648}
649
Willy Tarreauedcf6682008-11-30 23:15:34 +0100650/* This function performs the TCP request analysis on the current request. It
651 * returns 1 if the processing can continue on next analysers, or zero if it
652 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200653 * request. It relies on buffers flags, and updates s->req->analysers. The
654 * function may be called for frontend rules and backend rules. It only relies
655 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100656 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200657int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100658{
659 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200660 struct stksess *ts;
661 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100662 int partial;
663
664 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
665 now_ms, __FUNCTION__,
666 s,
667 req,
668 req->rex, req->wex,
669 req->flags,
670 req->l,
671 req->analysers);
672
Willy Tarreauedcf6682008-11-30 23:15:34 +0100673 /* We don't know whether we have enough data, so must proceed
674 * this way :
675 * - iterate through all rules in their declaration order
676 * - if one rule returns MISS, it means the inspect delay is
677 * not over yet, then return immediately, otherwise consider
678 * it as a non-match.
679 * - if one rule returns OK, then return OK
680 * - if one rule returns KO, then return KO
681 */
682
Willy Tarreaub824b002010-09-29 16:36:16 +0200683 if (req->flags & (BF_SHUTR|BF_FULL) || !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100684 partial = 0;
685 else
686 partial = ACL_PARTIAL;
687
Willy Tarreaufb356202010-08-03 14:02:05 +0200688 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100689 int ret = ACL_PAT_PASS;
690
691 if (rule->cond) {
Willy Tarreaufb356202010-08-03 14:02:05 +0200692 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100693 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200694 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100695 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200696 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
697 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100698 return 0;
699 }
700
701 ret = acl_pass(ret);
702 if (rule->cond->pol == ACL_COND_UNLESS)
703 ret = !ret;
704 }
705
706 if (ret) {
707 /* we have a matching rule. */
708 if (rule->action == TCP_ACT_REJECT) {
709 buffer_abort(req);
710 buffer_abort(s->rep);
711 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200712
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100713 s->be->be_counters.denied_req++;
714 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200715 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200716 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200717
Willy Tarreauedcf6682008-11-30 23:15:34 +0100718 if (!(s->flags & SN_ERR_MASK))
719 s->flags |= SN_ERR_PRXCOND;
720 if (!(s->flags & SN_FINST_MASK))
721 s->flags |= SN_FINST_R;
722 return 0;
723 }
Willy Tarreau56123282010-08-06 19:06:56 +0200724 else if (rule->action == TCP_ACT_TRK_SC1) {
725 if (!s->stkctr1_entry) {
726 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200727 * Also, note that right now we can only track SRC so we
728 * don't check how to get the key, but later we may need
729 * to consider rule->act_prm->trk_ctr.type.
730 */
731 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100732 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200733 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200734 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200735 if (s->fe != s->be)
736 s->flags |= SN_BE_TRACK_SC1;
737 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200738 }
739 }
Willy Tarreau56123282010-08-06 19:06:56 +0200740 else if (rule->action == TCP_ACT_TRK_SC2) {
741 if (!s->stkctr2_entry) {
742 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200743 * Also, note that right now we can only track SRC so we
744 * don't check how to get the key, but later we may need
745 * to consider rule->act_prm->trk_ctr.type.
746 */
747 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100748 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200749 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200750 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200751 if (s->fe != s->be)
752 s->flags |= SN_BE_TRACK_SC2;
753 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200754 }
755 }
756 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100757 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200758 break;
759 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100760 }
761 }
762
763 /* if we get there, it means we have no rule which matches, or
764 * we have an explicit accept, so we apply the default accept.
765 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200766 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100767 req->analyse_exp = TICK_ETERNITY;
768 return 1;
769}
770
Emeric Brun97679e72010-09-23 17:56:44 +0200771/* This function performs the TCP response analysis on the current response. It
772 * returns 1 if the processing can continue on next analysers, or zero if it
773 * needs more data, encounters an error, or wants to immediately abort the
774 * response. It relies on buffers flags, and updates s->rep->analysers. The
775 * function may be called for backend rules.
776 */
777int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit)
778{
779 struct tcp_rule *rule;
780 int partial;
781
782 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
783 now_ms, __FUNCTION__,
784 s,
785 rep,
786 rep->rex, rep->wex,
787 rep->flags,
788 rep->l,
789 rep->analysers);
790
791 /* We don't know whether we have enough data, so must proceed
792 * this way :
793 * - iterate through all rules in their declaration order
794 * - if one rule returns MISS, it means the inspect delay is
795 * not over yet, then return immediately, otherwise consider
796 * it as a non-match.
797 * - if one rule returns OK, then return OK
798 * - if one rule returns KO, then return KO
799 */
800
801 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
802 partial = 0;
803 else
804 partial = ACL_PARTIAL;
805
806 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
807 int ret = ACL_PAT_PASS;
808
809 if (rule->cond) {
810 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_RTR | partial);
811 if (ret == ACL_PAT_MISS) {
812 /* just set the analyser timeout once at the beginning of the response */
813 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
814 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
815 return 0;
816 }
817
818 ret = acl_pass(ret);
819 if (rule->cond->pol == ACL_COND_UNLESS)
820 ret = !ret;
821 }
822
823 if (ret) {
824 /* we have a matching rule. */
825 if (rule->action == TCP_ACT_REJECT) {
826 buffer_abort(rep);
827 buffer_abort(s->req);
828 rep->analysers = 0;
829
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100830 s->be->be_counters.denied_resp++;
831 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200832 if (s->listener->counters)
833 s->listener->counters->denied_resp++;
834
835 if (!(s->flags & SN_ERR_MASK))
836 s->flags |= SN_ERR_PRXCOND;
837 if (!(s->flags & SN_FINST_MASK))
838 s->flags |= SN_FINST_D;
839 return 0;
840 }
841 else {
842 /* otherwise accept */
843 break;
844 }
845 }
846 }
847
848 /* if we get there, it means we have no rule which matches, or
849 * we have an explicit accept, so we apply the default accept.
850 */
851 rep->analysers &= ~an_bit;
852 rep->analyse_exp = TICK_ETERNITY;
853 return 1;
854}
855
856
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200857/* This function performs the TCP layer4 analysis on the current request. It
858 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
859 * matches or if no more rule matches. It can only use rules which don't need
860 * any data.
861 */
862int tcp_exec_req_rules(struct session *s)
863{
864 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200865 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200866 struct stktable *t = NULL;
867 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200868 int ret;
869
870 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
871 ret = ACL_PAT_PASS;
872
873 if (rule->cond) {
874 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ);
875 ret = acl_pass(ret);
876 if (rule->cond->pol == ACL_COND_UNLESS)
877 ret = !ret;
878 }
879
880 if (ret) {
881 /* we have a matching rule. */
882 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100883 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200884 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200885 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200886
887 if (!(s->flags & SN_ERR_MASK))
888 s->flags |= SN_ERR_PRXCOND;
889 if (!(s->flags & SN_FINST_MASK))
890 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200891 result = 0;
892 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200893 }
Willy Tarreau56123282010-08-06 19:06:56 +0200894 else if (rule->action == TCP_ACT_TRK_SC1) {
895 if (!s->stkctr1_entry) {
896 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200897 * Also, note that right now we can only track SRC so we
898 * don't check how to get the key, but later we may need
899 * to consider rule->act_prm->trk_ctr.type.
900 */
901 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100902 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200903 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200904 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200905 }
906 }
Willy Tarreau56123282010-08-06 19:06:56 +0200907 else if (rule->action == TCP_ACT_TRK_SC2) {
908 if (!s->stkctr2_entry) {
909 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200910 * Also, note that right now we can only track SRC so we
911 * don't check how to get the key, but later we may need
912 * to consider rule->act_prm->trk_ctr.type.
913 */
914 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100915 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200916 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200917 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200918 }
919 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200920 else {
921 /* otherwise it's an accept */
922 break;
923 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200924 }
925 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200926 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200927}
928
Emeric Brun97679e72010-09-23 17:56:44 +0200929/* Parse a tcp-response rule. Return a negative value in case of failure */
930static int tcp_parse_response_rule(char **args, int arg, int section_type,
931 struct proxy *curpx, struct proxy *defpx,
932 struct tcp_rule *rule, char *err, int errlen)
933{
934 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
935 snprintf(err, errlen, "%s %s is only allowed in 'backend' sections",
936 args[0], args[1]);
937 return -1;
938 }
939
940 if (strcmp(args[arg], "accept") == 0) {
941 arg++;
942 rule->action = TCP_ACT_ACCEPT;
943 }
944 else if (strcmp(args[arg], "reject") == 0) {
945 arg++;
946 rule->action = TCP_ACT_REJECT;
947 }
948 else {
949 snprintf(err, errlen,
950 "'%s %s' expects 'accept' or 'reject' in %s '%s' (was '%s')",
951 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
952 return -1;
953 }
954
955 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
956 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) {
957 snprintf(err, errlen,
958 "error detected in %s '%s' while parsing '%s' condition",
959 proxy_type_str(curpx), curpx->id, args[arg]);
960 return -1;
961 }
962 }
963 else if (*args[arg]) {
964 snprintf(err, errlen,
965 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
966 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
967 return -1;
968 }
969 return 0;
970}
971
972
973
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200974/* Parse a tcp-request rule. Return a negative value in case of failure */
975static int tcp_parse_request_rule(char **args, int arg, int section_type,
976 struct proxy *curpx, struct proxy *defpx,
977 struct tcp_rule *rule, char *err, int errlen)
978{
979 if (curpx == defpx) {
980 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
981 args[0], args[1]);
982 return -1;
983 }
984
985 if (!strcmp(args[arg], "accept")) {
986 arg++;
987 rule->action = TCP_ACT_ACCEPT;
988 }
989 else if (!strcmp(args[arg], "reject")) {
990 arg++;
991 rule->action = TCP_ACT_REJECT;
992 }
Willy Tarreau56123282010-08-06 19:06:56 +0200993 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200994 int ret;
995
996 arg++;
997 ret = parse_track_counters(args, &arg, section_type, curpx,
998 &rule->act_prm.trk_ctr, defpx, err, errlen);
999
1000 if (ret < 0) /* nb: warnings are not handled yet */
1001 return -1;
1002
Willy Tarreau56123282010-08-06 19:06:56 +02001003 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001004 }
Willy Tarreau56123282010-08-06 19:06:56 +02001005 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001006 int ret;
1007
1008 arg++;
1009 ret = parse_track_counters(args, &arg, section_type, curpx,
1010 &rule->act_prm.trk_ctr, defpx, err, errlen);
1011
1012 if (ret < 0) /* nb: warnings are not handled yet */
1013 return -1;
1014
Willy Tarreau56123282010-08-06 19:06:56 +02001015 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001016 }
1017 else {
1018 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02001019 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1020 "or 'track-sc2' in %s '%s' (was '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001021 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
1022 return -1;
1023 }
1024
1025 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
1026 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) {
1027 snprintf(err, errlen,
1028 "error detected in %s '%s' while parsing '%s' condition",
1029 proxy_type_str(curpx), curpx->id, args[arg]);
1030 return -1;
1031 }
1032 }
1033 else if (*args[arg]) {
1034 snprintf(err, errlen,
1035 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
1036 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1037 return -1;
1038 }
1039 return 0;
1040}
1041
Emeric Brun97679e72010-09-23 17:56:44 +02001042/* This function should be called to parse a line starting with the "tcp-response"
1043 * keyword.
1044 */
1045static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
1046 struct proxy *defpx, char *err, int errlen)
1047{
1048 const char *ptr = NULL;
1049 unsigned int val;
1050 int retlen;
1051 int warn = 0;
1052 int arg;
1053 struct tcp_rule *rule;
1054
1055 if (!*args[1]) {
1056 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
1057 args[0], proxy_type_str(curpx), curpx->id);
1058 return -1;
1059 }
1060
1061 if (strcmp(args[1], "inspect-delay") == 0) {
1062 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
1063 snprintf(err, errlen, "%s %s is only allowed in 'backend' sections",
1064 args[0], args[1]);
1065 return -1;
1066 }
1067
1068 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1069 retlen = snprintf(err, errlen,
1070 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1071 args[0], args[1], proxy_type_str(curpx), curpx->id);
1072 if (ptr && retlen < errlen)
1073 retlen += snprintf(err + retlen, errlen - retlen,
1074 " (unexpected character '%c')", *ptr);
1075 return -1;
1076 }
1077
1078 if (curpx->tcp_rep.inspect_delay) {
1079 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
1080 args[0], args[1], proxy_type_str(curpx), curpx->id);
1081 return 1;
1082 }
1083 curpx->tcp_rep.inspect_delay = val;
1084 return 0;
1085 }
1086
Simon Hormande072bd2011-06-24 15:11:37 +09001087 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001088 LIST_INIT(&rule->list);
1089 arg = 1;
1090
1091 if (strcmp(args[1], "content") == 0) {
1092 arg++;
1093 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
1094 goto error;
1095
1096 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1097 struct acl *acl;
1098 const char *name;
1099
1100 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1101 name = acl ? acl->name : "(unknown)";
1102
1103 retlen = snprintf(err, errlen,
1104 "acl '%s' involves some request-only criteria which will be ignored.",
1105 name);
1106 warn++;
1107 }
1108
1109 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1110 }
1111 else {
1112 retlen = snprintf(err, errlen,
1113 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (was '%s')",
1114 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1115 goto error;
1116 }
1117
1118 return warn;
1119 error:
1120 free(rule);
1121 return -1;
1122}
1123
1124
Willy Tarreaub6866442008-07-14 23:54:42 +02001125/* This function should be called to parse a line starting with the "tcp-request"
1126 * keyword.
1127 */
1128static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
1129 struct proxy *defpx, char *err, int errlen)
1130{
1131 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001132 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +02001133 int retlen;
Willy Tarreau1a687942010-05-23 22:40:30 +02001134 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001135 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001136 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001137
1138 if (!*args[1]) {
1139 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +02001140 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001141 return -1;
1142 }
1143
1144 if (!strcmp(args[1], "inspect-delay")) {
1145 if (curpx == defpx) {
1146 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
1147 args[0], args[1]);
1148 return -1;
1149 }
1150
Willy Tarreaub6866442008-07-14 23:54:42 +02001151 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1152 retlen = snprintf(err, errlen,
1153 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +02001154 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001155 if (ptr && retlen < errlen)
1156 retlen += snprintf(err+retlen, errlen - retlen,
1157 " (unexpected character '%c')", *ptr);
1158 return -1;
1159 }
1160
1161 if (curpx->tcp_req.inspect_delay) {
1162 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +02001163 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001164 return 1;
1165 }
1166 curpx->tcp_req.inspect_delay = val;
1167 return 0;
1168 }
1169
Simon Hormande072bd2011-06-24 15:11:37 +09001170 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001171 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001172 arg = 1;
1173
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001174 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001175 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001176 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001177 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001178
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001179 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001180 struct acl *acl;
1181 const char *name;
1182
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001183 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001184 name = acl ? acl->name : "(unknown)";
1185
1186 retlen = snprintf(err, errlen,
Willy Tarreau1a211942009-07-14 13:53:17 +02001187 "acl '%s' involves some response-only criteria which will be ignored.",
1188 name);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001189 warn++;
1190 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001191 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001192 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001193 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001194 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001195
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001196 if (!(curpx->cap & PR_CAP_FE)) {
1197 snprintf(err, errlen, "%s %s is not allowed because %s %s is not a frontend",
1198 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001199 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001200 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001201
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001202 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001203 goto error;
1204
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001205 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1206 struct acl *acl;
1207 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001208
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001209 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1210 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001211
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001212 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
1213 retlen = snprintf(err, errlen,
1214 "'%s %s' may not reference acl '%s' which makes use of "
1215 "payload in %s '%s'. Please use '%s content' for this.",
1216 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
1217 goto error;
1218 }
1219 if (acl->requires & ACL_USE_RTR_ANY)
1220 retlen = snprintf(err, errlen,
1221 "acl '%s' involves some response-only criteria which will be ignored.",
1222 name);
1223 warn++;
1224 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001225 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001226 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001227 else {
1228 retlen = snprintf(err, errlen,
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001229 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (was '%s')",
Willy Tarreau1a687942010-05-23 22:40:30 +02001230 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001231 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001232 }
1233
Willy Tarreau1a687942010-05-23 22:40:30 +02001234 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001235 error:
1236 free(rule);
1237 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001238}
1239
Willy Tarreau645513a2010-05-24 20:55:15 +02001240
1241/************************************************************************/
1242/* All supported ACL keywords must be declared here. */
1243/************************************************************************/
1244
1245/* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */
1246static int
1247acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
1248 struct acl_expr *expr, struct acl_test *test)
1249{
Willy Tarreau957c0a52011-03-03 17:42:23 +01001250 test->i = l4->si[0].addr.c.from.ss_family;
Willy Tarreau645513a2010-05-24 20:55:15 +02001251 if (test->i == AF_INET)
Simon Hormande072bd2011-06-24 15:11:37 +09001252 test->ptr = (char *)&((struct sockaddr_in *)&l4->si[0].addr.c.from)->sin_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001253 else if (test->i == AF_INET6)
Simon Hormande072bd2011-06-24 15:11:37 +09001254 test->ptr = (char *)&((struct sockaddr_in6 *)(&l4->si[0].addr.c.from))->sin6_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001255 else
1256 return 0;
1257
Willy Tarreau645513a2010-05-24 20:55:15 +02001258 test->flags = ACL_TEST_F_READ_ONLY;
1259 return 1;
1260}
1261
David du Colombier4f92d322011-03-24 11:09:31 +01001262/* extract the connection's source ipv4 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001263static int
1264pattern_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02001265 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau645513a2010-05-24 20:55:15 +02001266{
Willy Tarreau957c0a52011-03-03 17:42:23 +01001267 if (l4->si[0].addr.c.from.ss_family != AF_INET )
Emeric Brunf769f512010-10-22 17:14:01 +02001268 return 0;
1269
Willy Tarreau957c0a52011-03-03 17:42:23 +01001270 data->ip.s_addr = ((struct sockaddr_in *)&l4->si[0].addr.c.from)->sin_addr.s_addr;
Willy Tarreau645513a2010-05-24 20:55:15 +02001271 return 1;
1272}
1273
David du Colombier4f92d322011-03-24 11:09:31 +01001274/* extract the connection's source ipv6 address */
1275static int
1276pattern_fetch_src6(struct proxy *px, struct session *l4, void *l7, int dir,
1277 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
1278{
1279 if (l4->si[0].addr.c.from.ss_family != AF_INET6)
1280 return 0;
1281
1282 memcpy(data->ipv6.s6_addr, ((struct sockaddr_in6 *)&l4->si[0].addr.c.from)->sin6_addr.s6_addr, sizeof(data->ipv6.s6_addr));
1283 return 1;
1284}
Willy Tarreau645513a2010-05-24 20:55:15 +02001285
1286/* set test->i to the connection's source port */
1287static int
1288acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir,
1289 struct acl_expr *expr, struct acl_test *test)
1290{
Willy Tarreau86ad42c2011-08-27 12:29:07 +02001291 if (!(test->i = get_host_port(&l4->si[0].addr.c.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001292 return 0;
1293
Willy Tarreau645513a2010-05-24 20:55:15 +02001294 test->flags = 0;
1295 return 1;
1296}
1297
1298
1299/* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */
1300static int
1301acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
1302 struct acl_expr *expr, struct acl_test *test)
1303{
1304 if (!(l4->flags & SN_FRT_ADDR_SET))
1305 get_frt_addr(l4);
1306
Willy Tarreau957c0a52011-03-03 17:42:23 +01001307 test->i = l4->si[0].addr.c.to.ss_family;
Willy Tarreau645513a2010-05-24 20:55:15 +02001308 if (test->i == AF_INET)
Simon Hormande072bd2011-06-24 15:11:37 +09001309 test->ptr = (char *)&((struct sockaddr_in *)&l4->si[0].addr.c.to)->sin_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001310 else if (test->i == AF_INET6)
Simon Hormande072bd2011-06-24 15:11:37 +09001311 test->ptr = (char *)&((struct sockaddr_in6 *)(&l4->si[0].addr.c.to))->sin6_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001312 else
1313 return 0;
1314
Willy Tarreau645513a2010-05-24 20:55:15 +02001315 test->flags = ACL_TEST_F_READ_ONLY;
1316 return 1;
1317}
1318
1319
David du Colombier4f92d322011-03-24 11:09:31 +01001320/* extract the connection's destination ipv4 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001321static int
1322pattern_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02001323 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau645513a2010-05-24 20:55:15 +02001324{
emeric8aa6b372010-10-22 17:06:26 +02001325 if (!(l4->flags & SN_FRT_ADDR_SET))
1326 get_frt_addr(l4);
1327
Willy Tarreau957c0a52011-03-03 17:42:23 +01001328 if (l4->si[0].addr.c.to.ss_family != AF_INET)
Emeric Brunf769f512010-10-22 17:14:01 +02001329 return 0;
1330
Willy Tarreau957c0a52011-03-03 17:42:23 +01001331 data->ip.s_addr = ((struct sockaddr_in *)&l4->si[0].addr.c.to)->sin_addr.s_addr;
Willy Tarreau645513a2010-05-24 20:55:15 +02001332 return 1;
1333}
1334
David du Colombier4f92d322011-03-24 11:09:31 +01001335/* extract the connection's destination ipv6 address */
1336static int
1337pattern_fetch_dst6(struct proxy *px, struct session *l4, void *l7, int dir,
1338 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
1339{
1340 if (!(l4->flags & SN_FRT_ADDR_SET))
1341 get_frt_addr(l4);
1342
1343 if (l4->si[0].addr.c.to.ss_family != AF_INET6)
1344 return 0;
1345
1346 memcpy(data->ipv6.s6_addr, ((struct sockaddr_in6 *)&l4->si[0].addr.c.to)->sin6_addr.s6_addr, sizeof(data->ipv6.s6_addr));
1347 return 1;
1348}
1349
Willy Tarreau645513a2010-05-24 20:55:15 +02001350/* set test->i to the frontend connexion's destination port */
1351static int
1352acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
1353 struct acl_expr *expr, struct acl_test *test)
1354{
1355 if (!(l4->flags & SN_FRT_ADDR_SET))
1356 get_frt_addr(l4);
1357
Willy Tarreau86ad42c2011-08-27 12:29:07 +02001358 if (!(test->i = get_host_port(&l4->si[0].addr.c.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001359 return 0;
1360
Willy Tarreau645513a2010-05-24 20:55:15 +02001361 test->flags = 0;
1362 return 1;
1363}
1364
1365static int
1366pattern_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02001367 const struct pattern_arg *arg, int i, union pattern_data *data)
Willy Tarreau645513a2010-05-24 20:55:15 +02001368{
emeric8aa6b372010-10-22 17:06:26 +02001369 if (!(l4->flags & SN_FRT_ADDR_SET))
1370 get_frt_addr(l4);
1371
Willy Tarreau86ad42c2011-08-27 12:29:07 +02001372 if (!(data->integer = get_host_port(&l4->si[0].addr.c.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001373 return 0;
1374
Willy Tarreau645513a2010-05-24 20:55:15 +02001375 return 1;
1376}
1377
Emericf2d7cae2010-11-05 18:13:50 +01001378static int
1379pattern_arg_fetch_payloadlv(const char *arg, struct pattern_arg **arg_p, int *arg_i)
1380{
1381 int member = 0;
1382 int len_offset = 0;
1383 int len_size = 0;
1384 int buf_offset = 0;
1385 int relative = 0;
1386 int arg_len = strlen(arg);
1387 int i;
1388
1389 for (i = 0; i < arg_len; i++) {
1390 if (arg[i] == ',') {
1391 member++;
1392 } else if (member == 0) {
1393 if (arg[i] < '0' || arg[i] > '9')
1394 return 0;
1395
1396 len_offset = 10 * len_offset + arg[i] - '0';
1397 } else if (member == 1) {
1398 if (arg[i] < '0' || arg[i] > '9')
1399 return 0;
1400
1401 len_size = 10 * len_size + arg[i] - '0';
1402 } else if (member == 2) {
1403 if (!relative && !buf_offset && arg[i] == '+') {
1404 relative = 1;
1405 continue;
1406 } else if (!relative && !buf_offset && arg[i] == '-') {
1407 relative = 2;
1408 continue;
1409 } else if (arg[i] < '0' || arg[i] > '9')
1410 return 0;
1411
1412 buf_offset = 10 * buf_offset + arg[i] - '0';
1413 }
1414 }
1415
1416 if (member < 1)
1417 return 0;
1418
1419 if (!len_size)
1420 return 0;
1421
1422 if (member == 1) {
1423 buf_offset = len_offset + len_size;
1424 }
1425 else if (relative == 1) {
1426 buf_offset = len_offset + len_size + buf_offset;
1427 }
1428 else if (relative == 2) {
1429 if (len_offset + len_size < buf_offset)
1430 return 0;
1431
1432 buf_offset = len_offset + len_size - buf_offset;
1433 }
1434
1435 *arg_i = 3;
1436 *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg));
1437 (*arg_p)[0].type = PATTERN_ARG_TYPE_INTEGER;
1438 (*arg_p)[0].data.integer = len_offset;
1439 (*arg_p)[1].type = PATTERN_ARG_TYPE_INTEGER;
1440 (*arg_p)[1].data.integer = len_size;
1441 (*arg_p)[2].type = PATTERN_ARG_TYPE_INTEGER;
1442 (*arg_p)[2].data.integer = buf_offset;
1443
1444 return 1;
1445}
1446
1447static int
1448pattern_fetch_payloadlv(struct proxy *px, struct session *l4, void *l7, int dir,
1449 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
1450{
1451 int len_offset = arg_p[0].data.integer;
1452 int len_size = arg_p[1].data.integer;
1453 int buf_offset = arg_p[2].data.integer;
1454 int buf_size = 0;
1455 struct buffer *b;
1456 int i;
1457
1458 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1459 /* by default buf offset == len offset + len size */
1460 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1461
1462 if (!l4)
1463 return 0;
1464
1465 b = (dir & PATTERN_FETCH_RTR) ? l4->rep : l4->req;
1466
1467 if (!b || !b->l)
1468 return 0;
1469
1470 if (len_offset + len_size > b->l)
1471 return 0;
1472
1473 for (i = 0; i < len_size; i++) {
1474 buf_size = (buf_size << 8) + ((unsigned char *)b->w)[i + len_offset];
1475 }
1476
1477 if (!buf_size)
1478 return 0;
1479
1480 if (buf_offset + buf_size > b->l)
1481 return 0;
1482
1483 /* init chunk as read only */
Simon Hormande072bd2011-06-24 15:11:37 +09001484 chunk_initlen(&data->str, b->w + buf_offset, 0, buf_size);
Emericf2d7cae2010-11-05 18:13:50 +01001485
1486 return 1;
1487}
1488
1489static int
1490pattern_arg_fetch_payload (const char *arg, struct pattern_arg **arg_p, int *arg_i)
1491{
1492 int member = 0;
1493 int buf_offset = 0;
1494 int buf_size = 0;
1495 int arg_len = strlen(arg);
1496 int i;
1497
1498 for (i = 0 ; i < arg_len ; i++) {
1499 if (arg[i] == ',') {
1500 member++;
1501 } else if (member == 0) {
1502 if (arg[i] < '0' || arg[i] > '9')
1503 return 0;
1504
1505 buf_offset = 10 * buf_offset + arg[i] - '0';
1506 } else if (member == 1) {
1507 if (arg[i] < '0' || arg[i] > '9')
1508 return 0;
1509
1510 buf_size = 10 * buf_size + arg[i] - '0';
1511 }
1512 }
1513
1514 if (!buf_size)
1515 return 0;
1516
1517 *arg_i = 2;
1518 *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg));
1519 (*arg_p)[0].type = PATTERN_ARG_TYPE_INTEGER;
1520 (*arg_p)[0].data.integer = buf_offset;
1521 (*arg_p)[1].type = PATTERN_ARG_TYPE_INTEGER;
1522 (*arg_p)[1].data.integer = buf_size;
1523
1524 return 1;
1525}
1526
1527static int
1528pattern_fetch_payload(struct proxy *px, struct session *l4, void *l7, int dir,
1529 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
1530{
1531 int buf_offset = arg_p[0].data.integer;
1532 int buf_size = arg_p[1].data.integer;
1533 struct buffer *b;
1534
1535 if (!l4)
1536 return 0;
1537
1538 b = (dir & PATTERN_FETCH_RTR) ? l4->rep : l4->req;
1539
1540 if (!b || !b->l)
1541 return 0;
1542
1543 if (buf_offset + buf_size > b->l)
1544 return 0;
1545
1546 /* init chunk as read only */
Simon Hormande072bd2011-06-24 15:11:37 +09001547 chunk_initlen(&data->str, b->w + buf_offset, 0, buf_size);
Emericf2d7cae2010-11-05 18:13:50 +01001548
1549 return 1;
1550}
1551
Simon Hormanab814e02011-06-24 14:50:20 +09001552static int
1553pattern_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
1554 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
1555{
1556 int ret;
1557 struct acl_expr expr;
1558 struct acl_test test;
1559
1560 if (!l4)
1561 return 0;
1562
1563 memset(&expr, 0, sizeof(expr));
1564 memset(&test, 0, sizeof(test));
1565
1566 expr.arg.str = arg_p[0].data.str.str;
1567 expr.arg_len = arg_p[0].data.str.len;
1568
1569 ret = acl_fetch_rdp_cookie(px, l4, NULL, ACL_DIR_REQ, &expr, &test);
1570 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
1571 return 0;
1572
1573 /* init chunk as read only */
1574 chunk_initlen(&data->str, test.ptr, 0, test.len);
1575 return 1;
1576}
1577
Willy Tarreaub6866442008-07-14 23:54:42 +02001578static struct cfg_kw_list cfg_kws = {{ },{
1579 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001580 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001581 { 0, NULL, NULL },
1582}};
1583
Willy Tarreau645513a2010-05-24 20:55:15 +02001584/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub6866442008-07-14 23:54:42 +02001585static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +02001586 { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT },
1587 { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1588 { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1589 { "dst_port", acl_parse_int, acl_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT },
Willy Tarreaub6866442008-07-14 23:54:42 +02001590 { NULL, NULL, NULL, NULL },
1591}};
1592
Willy Tarreau645513a2010-05-24 20:55:15 +02001593/* Note: must not be declared <const> as its list will be overwritten */
1594static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emericf2d7cae2010-11-05 18:13:50 +01001595 { "src", pattern_fetch_src, NULL, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David du Colombier4f92d322011-03-24 11:09:31 +01001596 { "src6", pattern_fetch_src6, NULL, PATTERN_TYPE_IPV6, PATTERN_FETCH_REQ },
Emericf2d7cae2010-11-05 18:13:50 +01001597 { "dst", pattern_fetch_dst, NULL, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David du Colombier4f92d322011-03-24 11:09:31 +01001598 { "dst6", pattern_fetch_dst6, NULL, PATTERN_TYPE_IPV6, PATTERN_FETCH_REQ },
Emericf2d7cae2010-11-05 18:13:50 +01001599 { "dst_port", pattern_fetch_dport, NULL, PATTERN_TYPE_INTEGER, PATTERN_FETCH_REQ },
1600 { "payload", pattern_fetch_payload, pattern_arg_fetch_payload, PATTERN_TYPE_CONSTDATA, PATTERN_FETCH_REQ|PATTERN_FETCH_RTR },
1601 { "payload_lv", pattern_fetch_payloadlv, pattern_arg_fetch_payloadlv, PATTERN_TYPE_CONSTDATA, PATTERN_FETCH_REQ|PATTERN_FETCH_RTR },
Simon Hormanab814e02011-06-24 14:50:20 +09001602 { "rdp_cookie", pattern_fetch_rdp_cookie, pattern_arg_str, PATTERN_TYPE_CONSTSTRING, PATTERN_FETCH_REQ },
Emeric Brun485479d2010-09-23 18:02:19 +02001603 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001604}};
1605
Willy Tarreaue6b98942007-10-29 01:09:36 +01001606__attribute__((constructor))
1607static void __tcp_protocol_init(void)
1608{
1609 protocol_register(&proto_tcpv4);
1610 protocol_register(&proto_tcpv6);
Willy Tarreau645513a2010-05-24 20:55:15 +02001611 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001612 cfg_register_keywords(&cfg_kws);
1613 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001614}
1615
1616
1617/*
1618 * Local variables:
1619 * c-indent-level: 8
1620 * c-basic-offset: 8
1621 * End:
1622 */