blob: e6d6c58a0a699004151518753437d0e9aa1d4662 [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) {
126 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0
127 || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0)
128 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
152 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
153 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) ||
260 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
261 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)
267 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
268
269 if (be->options & PR_O_TCP_NOLING)
270 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
271
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;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100319 switch (src.ss_family) {
320 case AF_INET:
321 ((struct sockaddr_in *)&src)->sin_port = htons(fdinfo[fd].local_port);
322 break;
323 case AF_INET6:
324 ((struct sockaddr_in6 *)&src)->sin6_port = htons(fdinfo[fd].local_port);
325 break;
326 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200327
David du Colombier6f5ccb12011-03-10 22:26:24 +0100328 ret = tcp_bind_socket(fd, flags, &src, &si->addr.s.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200329 } while (ret != 0); /* binding NOK */
330 }
331 else {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100332 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.s.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200333 }
334
335 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200336 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
337 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200338 close(fd);
339
340 if (ret == 1) {
341 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
342 be->id, srv->id);
343 send_log(be, LOG_EMERG,
344 "Cannot bind to source address before connect() for server %s/%s.\n",
345 be->id, srv->id);
346 } else {
347 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
348 be->id, srv->id);
349 send_log(be, LOG_EMERG,
350 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
351 be->id, srv->id);
352 }
353 return SN_ERR_RESOURCE;
354 }
355 }
356 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200357 int ret, flags = 0;
358
Willy Tarreau9650f372009-08-16 14:02:45 +0200359 switch (be->options & PR_O_TPXY_MASK) {
360 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200361 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200362 flags = 3;
363 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200364 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200365 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200366 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200367 break;
368 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200369
Willy Tarreau9650f372009-08-16 14:02:45 +0200370#ifdef SO_BINDTODEVICE
371 /* Note: this might fail if not CAP_NET_RAW */
372 if (be->iface_name)
373 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
374#endif
David du Colombier6f5ccb12011-03-10 22:26:24 +0100375 ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.s.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200376 if (ret) {
377 close(fd);
378 if (ret == 1) {
379 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
380 be->id);
381 send_log(be, LOG_EMERG,
382 "Cannot bind to source address before connect() for proxy %s.\n",
383 be->id);
384 } else {
385 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
386 be->id);
387 send_log(be, LOG_EMERG,
388 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
389 be->id);
390 }
391 return SN_ERR_RESOURCE;
392 }
393 }
394
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400395#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200396 /* disabling tcp quick ack now allows the first request to leave the
397 * machine with the first ACK. We only do this if there are pending
398 * data in the buffer.
399 */
400 if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400401 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200402#endif
403
Willy Tarreaue803de22010-01-21 17:43:04 +0100404 if (global.tune.server_sndbuf)
405 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
406
407 if (global.tune.server_rcvbuf)
408 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
409
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200410 if ((connect(fd, (struct sockaddr *)&si->addr.s.to, get_addr_len(&si->addr.s.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200411 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
412
413 if (errno == EAGAIN || errno == EADDRINUSE) {
414 char *msg;
415 if (errno == EAGAIN) /* no free ports left, try again later */
416 msg = "no free ports";
417 else
418 msg = "local address already in use";
419
420 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200421 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
422 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200423 close(fd);
424 send_log(be, LOG_EMERG,
425 "Connect() failed for server %s/%s: %s.\n",
426 be->id, srv->id, msg);
427 return SN_ERR_RESOURCE;
428 } else if (errno == ETIMEDOUT) {
429 //qfprintf(stderr,"Connect(): ETIMEDOUT");
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_SRVTO;
434 } else {
435 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
436 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200437 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
438 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200439 close(fd);
440 return SN_ERR_SRVCL;
441 }
442 }
443
444 fdtab[fd].owner = si;
445 fdtab[fd].state = FD_STCONN; /* connection in progress */
446 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
447 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
448 fdtab[fd].cb[DIR_RD].b = si->ib;
449 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
450 fdtab[fd].cb[DIR_WR].b = si->ob;
451
Willy Tarreauf1536862011-03-03 18:27:32 +0100452 fdinfo[fd].peeraddr = (struct sockaddr *)&si->addr.s.to;
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200453 fdinfo[fd].peerlen = get_addr_len(&si->addr.s.to);
Willy Tarreau9650f372009-08-16 14:02:45 +0200454
455 fd_insert(fd);
456 EV_FD_SET(fd, DIR_WR); /* for connect status */
457
458 si->state = SI_ST_CON;
459 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
460 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
461
462 return SN_ERR_NONE; /* connection is OK */
463}
464
465
Willy Tarreaue6b98942007-10-29 01:09:36 +0100466/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
467 * an error message in <err> if the message is at most <errlen> bytes long
468 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
469 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
470 * was alright and that no message was returned. ERR_RETRYABLE means that an
471 * error occurred but that it may vanish after a retry (eg: port in use), and
472 * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter
473 * the meaning of the error, but just indicate that a message is present which
474 * should be displayed with the respective level. Last, ERR_ABORT indicates
475 * that it's pointless to try to start other listeners. No error message is
476 * returned if errlen is NULL.
477 */
478int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
479{
480 __label__ tcp_return, tcp_close_return;
481 int fd, err;
482 const char *msg = NULL;
483
484 /* ensure we never return garbage */
485 if (errmsg && errlen)
486 *errmsg = 0;
487
488 if (listener->state != LI_ASSIGNED)
489 return ERR_NONE; /* already bound */
490
491 err = ERR_NONE;
492
493 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
494 err |= ERR_RETRYABLE | ERR_ALERT;
495 msg = "cannot create listening socket";
496 goto tcp_return;
497 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100498
Willy Tarreaue6b98942007-10-29 01:09:36 +0100499 if (fd >= global.maxsock) {
500 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
501 msg = "not enough free sockets (raise '-n' parameter)";
502 goto tcp_close_return;
503 }
504
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200505 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100506 err |= ERR_FATAL | ERR_ALERT;
507 msg = "cannot make socket non-blocking";
508 goto tcp_close_return;
509 }
510
511 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
512 /* not fatal but should be reported */
513 msg = "cannot do so_reuseaddr";
514 err |= ERR_ALERT;
515 }
516
517 if (listener->options & LI_O_NOLINGER)
518 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100519
Willy Tarreaue6b98942007-10-29 01:09:36 +0100520#ifdef SO_REUSEPORT
521 /* OpenBSD supports this. As it's present in old libc versions of Linux,
522 * it might return an error that we will silently ignore.
523 */
524 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
525#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100526#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100527 if ((listener->options & LI_O_FOREIGN)
Willy Tarreau0a459892008-01-13 17:37:16 +0100528 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1)
529 && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100530 msg = "cannot make listening socket transparent";
531 err |= ERR_ALERT;
532 }
533#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100534#ifdef SO_BINDTODEVICE
535 /* Note: this might fail if not CAP_NET_RAW */
536 if (listener->interface) {
537 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100538 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100539 msg = "cannot bind listener to device";
540 err |= ERR_WARN;
541 }
542 }
543#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400544#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100545 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400546 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200547 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
548 msg = "cannot set MSS";
549 err |= ERR_WARN;
550 }
551 }
552#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200553#if defined(TCP_DEFER_ACCEPT)
554 if (listener->options & LI_O_DEF_ACCEPT) {
555 /* defer accept by up to one second */
556 int accept_delay = 1;
557 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
558 msg = "cannot enable DEFER_ACCEPT";
559 err |= ERR_WARN;
560 }
561 }
562#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100563 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
564 err |= ERR_RETRYABLE | ERR_ALERT;
565 msg = "cannot bind socket";
566 goto tcp_close_return;
567 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100568
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100569 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100570 err |= ERR_RETRYABLE | ERR_ALERT;
571 msg = "cannot listen to socket";
572 goto tcp_close_return;
573 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100574
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400575#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200576 if (listener->options & LI_O_NOQUICKACK)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400577 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200578#endif
579
Willy Tarreaue6b98942007-10-29 01:09:36 +0100580 /* the socket is ready */
581 listener->fd = fd;
582 listener->state = LI_LISTEN;
583
Willy Tarreaueabf3132008-08-29 23:36:51 +0200584 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100585 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaueb472682010-05-28 18:46:57 +0200586 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
587 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
588 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
589 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200590
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200591 fdinfo[fd].peeraddr = NULL;
592 fdinfo[fd].peerlen = 0;
Willy Tarreaueb472682010-05-28 18:46:57 +0200593 fd_insert(fd);
594
Willy Tarreaue6b98942007-10-29 01:09:36 +0100595 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100596 if (msg && errlen) {
597 char pn[INET6_ADDRSTRLEN];
598
599 if (listener->addr.ss_family == AF_INET) {
600 inet_ntop(AF_INET,
601 (const void *)&((struct sockaddr_in *)&listener->addr)->sin_addr,
602 pn, sizeof(pn));
603 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, ntohs(((struct sockaddr_in *)&listener->addr)->sin_port));
604 }
605 else {
606 inet_ntop(AF_INET6,
607 (const void *)&((struct sockaddr_in6 *)(&listener->addr))->sin6_addr,
608 pn, sizeof(pn));
609 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, ntohs(((struct sockaddr_in6 *)&listener->addr)->sin6_port));
610 }
611 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100612 return err;
613
614 tcp_close_return:
615 close(fd);
616 goto tcp_return;
617}
618
619/* This function creates all TCP sockets bound to the protocol entry <proto>.
620 * It is intended to be used as the protocol's bind_all() function.
621 * The sockets will be registered but not added to any fd_set, in order not to
622 * loose them across the fork(). A call to enable_all_listeners() is needed
623 * to complete initialization. The return value is composed from ERR_*.
624 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200625static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100626{
627 struct listener *listener;
628 int err = ERR_NONE;
629
630 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200631 err |= tcp_bind_listener(listener, errmsg, errlen);
632 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100633 break;
634 }
635
636 return err;
637}
638
639/* Add listener to the list of tcpv4 listeners. The listener's state
640 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
641 * listeners is updated. This is the function to use to add a new listener.
642 */
643void tcpv4_add_listener(struct listener *listener)
644{
645 if (listener->state != LI_INIT)
646 return;
647 listener->state = LI_ASSIGNED;
648 listener->proto = &proto_tcpv4;
649 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
650 proto_tcpv4.nb_listeners++;
651}
652
653/* Add listener to the list of tcpv4 listeners. The listener's state
654 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
655 * listeners is updated. This is the function to use to add a new listener.
656 */
657void tcpv6_add_listener(struct listener *listener)
658{
659 if (listener->state != LI_INIT)
660 return;
661 listener->state = LI_ASSIGNED;
662 listener->proto = &proto_tcpv6;
663 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
664 proto_tcpv6.nb_listeners++;
665}
666
Willy Tarreauedcf6682008-11-30 23:15:34 +0100667/* This function performs the TCP request analysis on the current request. It
668 * returns 1 if the processing can continue on next analysers, or zero if it
669 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200670 * request. It relies on buffers flags, and updates s->req->analysers. The
671 * function may be called for frontend rules and backend rules. It only relies
672 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100673 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200674int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100675{
676 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200677 struct stksess *ts;
678 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100679 int partial;
680
681 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
682 now_ms, __FUNCTION__,
683 s,
684 req,
685 req->rex, req->wex,
686 req->flags,
687 req->l,
688 req->analysers);
689
Willy Tarreauedcf6682008-11-30 23:15:34 +0100690 /* We don't know whether we have enough data, so must proceed
691 * this way :
692 * - iterate through all rules in their declaration order
693 * - if one rule returns MISS, it means the inspect delay is
694 * not over yet, then return immediately, otherwise consider
695 * it as a non-match.
696 * - if one rule returns OK, then return OK
697 * - if one rule returns KO, then return KO
698 */
699
Willy Tarreaub824b002010-09-29 16:36:16 +0200700 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 +0100701 partial = 0;
702 else
703 partial = ACL_PARTIAL;
704
Willy Tarreaufb356202010-08-03 14:02:05 +0200705 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100706 int ret = ACL_PAT_PASS;
707
708 if (rule->cond) {
Willy Tarreaufb356202010-08-03 14:02:05 +0200709 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100710 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200711 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100712 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200713 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
714 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100715 return 0;
716 }
717
718 ret = acl_pass(ret);
719 if (rule->cond->pol == ACL_COND_UNLESS)
720 ret = !ret;
721 }
722
723 if (ret) {
724 /* we have a matching rule. */
725 if (rule->action == TCP_ACT_REJECT) {
726 buffer_abort(req);
727 buffer_abort(s->rep);
728 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200729
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100730 s->be->be_counters.denied_req++;
731 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200732 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200733 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200734
Willy Tarreauedcf6682008-11-30 23:15:34 +0100735 if (!(s->flags & SN_ERR_MASK))
736 s->flags |= SN_ERR_PRXCOND;
737 if (!(s->flags & SN_FINST_MASK))
738 s->flags |= SN_FINST_R;
739 return 0;
740 }
Willy Tarreau56123282010-08-06 19:06:56 +0200741 else if (rule->action == TCP_ACT_TRK_SC1) {
742 if (!s->stkctr1_entry) {
743 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200744 * Also, note that right now we can only track SRC so we
745 * don't check how to get the key, but later we may need
746 * to consider rule->act_prm->trk_ctr.type.
747 */
748 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100749 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200750 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200751 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200752 if (s->fe != s->be)
753 s->flags |= SN_BE_TRACK_SC1;
754 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200755 }
756 }
Willy Tarreau56123282010-08-06 19:06:56 +0200757 else if (rule->action == TCP_ACT_TRK_SC2) {
758 if (!s->stkctr2_entry) {
759 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200760 * Also, note that right now we can only track SRC so we
761 * don't check how to get the key, but later we may need
762 * to consider rule->act_prm->trk_ctr.type.
763 */
764 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100765 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200766 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200767 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200768 if (s->fe != s->be)
769 s->flags |= SN_BE_TRACK_SC2;
770 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200771 }
772 }
773 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100774 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200775 break;
776 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100777 }
778 }
779
780 /* if we get there, it means we have no rule which matches, or
781 * we have an explicit accept, so we apply the default accept.
782 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200783 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100784 req->analyse_exp = TICK_ETERNITY;
785 return 1;
786}
787
Emeric Brun97679e72010-09-23 17:56:44 +0200788/* This function performs the TCP response analysis on the current response. It
789 * returns 1 if the processing can continue on next analysers, or zero if it
790 * needs more data, encounters an error, or wants to immediately abort the
791 * response. It relies on buffers flags, and updates s->rep->analysers. The
792 * function may be called for backend rules.
793 */
794int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit)
795{
796 struct tcp_rule *rule;
797 int partial;
798
799 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
800 now_ms, __FUNCTION__,
801 s,
802 rep,
803 rep->rex, rep->wex,
804 rep->flags,
805 rep->l,
806 rep->analysers);
807
808 /* We don't know whether we have enough data, so must proceed
809 * this way :
810 * - iterate through all rules in their declaration order
811 * - if one rule returns MISS, it means the inspect delay is
812 * not over yet, then return immediately, otherwise consider
813 * it as a non-match.
814 * - if one rule returns OK, then return OK
815 * - if one rule returns KO, then return KO
816 */
817
818 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
819 partial = 0;
820 else
821 partial = ACL_PARTIAL;
822
823 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
824 int ret = ACL_PAT_PASS;
825
826 if (rule->cond) {
827 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_RTR | partial);
828 if (ret == ACL_PAT_MISS) {
829 /* just set the analyser timeout once at the beginning of the response */
830 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
831 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
832 return 0;
833 }
834
835 ret = acl_pass(ret);
836 if (rule->cond->pol == ACL_COND_UNLESS)
837 ret = !ret;
838 }
839
840 if (ret) {
841 /* we have a matching rule. */
842 if (rule->action == TCP_ACT_REJECT) {
843 buffer_abort(rep);
844 buffer_abort(s->req);
845 rep->analysers = 0;
846
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100847 s->be->be_counters.denied_resp++;
848 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200849 if (s->listener->counters)
850 s->listener->counters->denied_resp++;
851
852 if (!(s->flags & SN_ERR_MASK))
853 s->flags |= SN_ERR_PRXCOND;
854 if (!(s->flags & SN_FINST_MASK))
855 s->flags |= SN_FINST_D;
856 return 0;
857 }
858 else {
859 /* otherwise accept */
860 break;
861 }
862 }
863 }
864
865 /* if we get there, it means we have no rule which matches, or
866 * we have an explicit accept, so we apply the default accept.
867 */
868 rep->analysers &= ~an_bit;
869 rep->analyse_exp = TICK_ETERNITY;
870 return 1;
871}
872
873
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200874/* This function performs the TCP layer4 analysis on the current request. It
875 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
876 * matches or if no more rule matches. It can only use rules which don't need
877 * any data.
878 */
879int tcp_exec_req_rules(struct session *s)
880{
881 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200882 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200883 struct stktable *t = NULL;
884 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200885 int ret;
886
887 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
888 ret = ACL_PAT_PASS;
889
890 if (rule->cond) {
891 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ);
892 ret = acl_pass(ret);
893 if (rule->cond->pol == ACL_COND_UNLESS)
894 ret = !ret;
895 }
896
897 if (ret) {
898 /* we have a matching rule. */
899 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100900 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200901 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200902 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200903
904 if (!(s->flags & SN_ERR_MASK))
905 s->flags |= SN_ERR_PRXCOND;
906 if (!(s->flags & SN_FINST_MASK))
907 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200908 result = 0;
909 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200910 }
Willy Tarreau56123282010-08-06 19:06:56 +0200911 else if (rule->action == TCP_ACT_TRK_SC1) {
912 if (!s->stkctr1_entry) {
913 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200914 * Also, note that right now we can only track SRC so we
915 * don't check how to get the key, but later we may need
916 * to consider rule->act_prm->trk_ctr.type.
917 */
918 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100919 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200920 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200921 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200922 }
923 }
Willy Tarreau56123282010-08-06 19:06:56 +0200924 else if (rule->action == TCP_ACT_TRK_SC2) {
925 if (!s->stkctr2_entry) {
926 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200927 * Also, note that right now we can only track SRC so we
928 * don't check how to get the key, but later we may need
929 * to consider rule->act_prm->trk_ctr.type.
930 */
931 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100932 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200933 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200934 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200935 }
936 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200937 else {
938 /* otherwise it's an accept */
939 break;
940 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200941 }
942 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200943 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200944}
945
Emeric Brun97679e72010-09-23 17:56:44 +0200946/* Parse a tcp-response rule. Return a negative value in case of failure */
947static int tcp_parse_response_rule(char **args, int arg, int section_type,
948 struct proxy *curpx, struct proxy *defpx,
949 struct tcp_rule *rule, char *err, int errlen)
950{
951 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
952 snprintf(err, errlen, "%s %s is only allowed in 'backend' sections",
953 args[0], args[1]);
954 return -1;
955 }
956
957 if (strcmp(args[arg], "accept") == 0) {
958 arg++;
959 rule->action = TCP_ACT_ACCEPT;
960 }
961 else if (strcmp(args[arg], "reject") == 0) {
962 arg++;
963 rule->action = TCP_ACT_REJECT;
964 }
965 else {
966 snprintf(err, errlen,
967 "'%s %s' expects 'accept' or 'reject' in %s '%s' (was '%s')",
968 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
969 return -1;
970 }
971
972 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
973 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) {
974 snprintf(err, errlen,
975 "error detected in %s '%s' while parsing '%s' condition",
976 proxy_type_str(curpx), curpx->id, args[arg]);
977 return -1;
978 }
979 }
980 else if (*args[arg]) {
981 snprintf(err, errlen,
982 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
983 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
984 return -1;
985 }
986 return 0;
987}
988
989
990
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200991/* Parse a tcp-request rule. Return a negative value in case of failure */
992static int tcp_parse_request_rule(char **args, int arg, int section_type,
993 struct proxy *curpx, struct proxy *defpx,
994 struct tcp_rule *rule, char *err, int errlen)
995{
996 if (curpx == defpx) {
997 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
998 args[0], args[1]);
999 return -1;
1000 }
1001
1002 if (!strcmp(args[arg], "accept")) {
1003 arg++;
1004 rule->action = TCP_ACT_ACCEPT;
1005 }
1006 else if (!strcmp(args[arg], "reject")) {
1007 arg++;
1008 rule->action = TCP_ACT_REJECT;
1009 }
Willy Tarreau56123282010-08-06 19:06:56 +02001010 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001011 int ret;
1012
1013 arg++;
1014 ret = parse_track_counters(args, &arg, section_type, curpx,
1015 &rule->act_prm.trk_ctr, defpx, err, errlen);
1016
1017 if (ret < 0) /* nb: warnings are not handled yet */
1018 return -1;
1019
Willy Tarreau56123282010-08-06 19:06:56 +02001020 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001021 }
Willy Tarreau56123282010-08-06 19:06:56 +02001022 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001023 int ret;
1024
1025 arg++;
1026 ret = parse_track_counters(args, &arg, section_type, curpx,
1027 &rule->act_prm.trk_ctr, defpx, err, errlen);
1028
1029 if (ret < 0) /* nb: warnings are not handled yet */
1030 return -1;
1031
Willy Tarreau56123282010-08-06 19:06:56 +02001032 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001033 }
1034 else {
1035 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02001036 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1037 "or 'track-sc2' in %s '%s' (was '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001038 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
1039 return -1;
1040 }
1041
1042 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
1043 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) {
1044 snprintf(err, errlen,
1045 "error detected in %s '%s' while parsing '%s' condition",
1046 proxy_type_str(curpx), curpx->id, args[arg]);
1047 return -1;
1048 }
1049 }
1050 else if (*args[arg]) {
1051 snprintf(err, errlen,
1052 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
1053 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1054 return -1;
1055 }
1056 return 0;
1057}
1058
Emeric Brun97679e72010-09-23 17:56:44 +02001059/* This function should be called to parse a line starting with the "tcp-response"
1060 * keyword.
1061 */
1062static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
1063 struct proxy *defpx, char *err, int errlen)
1064{
1065 const char *ptr = NULL;
1066 unsigned int val;
1067 int retlen;
1068 int warn = 0;
1069 int arg;
1070 struct tcp_rule *rule;
1071
1072 if (!*args[1]) {
1073 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
1074 args[0], proxy_type_str(curpx), curpx->id);
1075 return -1;
1076 }
1077
1078 if (strcmp(args[1], "inspect-delay") == 0) {
1079 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
1080 snprintf(err, errlen, "%s %s is only allowed in 'backend' sections",
1081 args[0], args[1]);
1082 return -1;
1083 }
1084
1085 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1086 retlen = snprintf(err, errlen,
1087 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1088 args[0], args[1], proxy_type_str(curpx), curpx->id);
1089 if (ptr && retlen < errlen)
1090 retlen += snprintf(err + retlen, errlen - retlen,
1091 " (unexpected character '%c')", *ptr);
1092 return -1;
1093 }
1094
1095 if (curpx->tcp_rep.inspect_delay) {
1096 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
1097 args[0], args[1], proxy_type_str(curpx), curpx->id);
1098 return 1;
1099 }
1100 curpx->tcp_rep.inspect_delay = val;
1101 return 0;
1102 }
1103
1104 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
1105 LIST_INIT(&rule->list);
1106 arg = 1;
1107
1108 if (strcmp(args[1], "content") == 0) {
1109 arg++;
1110 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
1111 goto error;
1112
1113 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1114 struct acl *acl;
1115 const char *name;
1116
1117 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1118 name = acl ? acl->name : "(unknown)";
1119
1120 retlen = snprintf(err, errlen,
1121 "acl '%s' involves some request-only criteria which will be ignored.",
1122 name);
1123 warn++;
1124 }
1125
1126 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1127 }
1128 else {
1129 retlen = snprintf(err, errlen,
1130 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (was '%s')",
1131 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1132 goto error;
1133 }
1134
1135 return warn;
1136 error:
1137 free(rule);
1138 return -1;
1139}
1140
1141
Willy Tarreaub6866442008-07-14 23:54:42 +02001142/* This function should be called to parse a line starting with the "tcp-request"
1143 * keyword.
1144 */
1145static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
1146 struct proxy *defpx, char *err, int errlen)
1147{
1148 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001149 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +02001150 int retlen;
Willy Tarreau1a687942010-05-23 22:40:30 +02001151 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001152 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001153 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001154
1155 if (!*args[1]) {
1156 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +02001157 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001158 return -1;
1159 }
1160
1161 if (!strcmp(args[1], "inspect-delay")) {
1162 if (curpx == defpx) {
1163 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
1164 args[0], args[1]);
1165 return -1;
1166 }
1167
Willy Tarreaub6866442008-07-14 23:54:42 +02001168 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1169 retlen = snprintf(err, errlen,
1170 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +02001171 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001172 if (ptr && retlen < errlen)
1173 retlen += snprintf(err+retlen, errlen - retlen,
1174 " (unexpected character '%c')", *ptr);
1175 return -1;
1176 }
1177
1178 if (curpx->tcp_req.inspect_delay) {
1179 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +02001180 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001181 return 1;
1182 }
1183 curpx->tcp_req.inspect_delay = val;
1184 return 0;
1185 }
1186
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001187 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001188 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001189 arg = 1;
1190
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001191 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001192 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001193 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001194 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001195
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001196 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001197 struct acl *acl;
1198 const char *name;
1199
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001200 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001201 name = acl ? acl->name : "(unknown)";
1202
1203 retlen = snprintf(err, errlen,
Willy Tarreau1a211942009-07-14 13:53:17 +02001204 "acl '%s' involves some response-only criteria which will be ignored.",
1205 name);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001206 warn++;
1207 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001208 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001209 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001210 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001211 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001212
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001213 if (!(curpx->cap & PR_CAP_FE)) {
1214 snprintf(err, errlen, "%s %s is not allowed because %s %s is not a frontend",
1215 args[0], args[1], proxy_type_str(curpx), curpx->id);
1216 return -1;
1217 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001218
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001219 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001220 goto error;
1221
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001222 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1223 struct acl *acl;
1224 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001225
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001226 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1227 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001228
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001229 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
1230 retlen = snprintf(err, errlen,
1231 "'%s %s' may not reference acl '%s' which makes use of "
1232 "payload in %s '%s'. Please use '%s content' for this.",
1233 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
1234 goto error;
1235 }
1236 if (acl->requires & ACL_USE_RTR_ANY)
1237 retlen = snprintf(err, errlen,
1238 "acl '%s' involves some response-only criteria which will be ignored.",
1239 name);
1240 warn++;
1241 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001242 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001243 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001244 else {
1245 retlen = snprintf(err, errlen,
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001246 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (was '%s')",
Willy Tarreau1a687942010-05-23 22:40:30 +02001247 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001248 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001249 }
1250
Willy Tarreau1a687942010-05-23 22:40:30 +02001251 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001252 error:
1253 free(rule);
1254 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001255}
1256
Willy Tarreau645513a2010-05-24 20:55:15 +02001257
1258/************************************************************************/
1259/* All supported ACL keywords must be declared here. */
1260/************************************************************************/
1261
1262/* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */
1263static int
1264acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
1265 struct acl_expr *expr, struct acl_test *test)
1266{
Willy Tarreau957c0a52011-03-03 17:42:23 +01001267 test->i = l4->si[0].addr.c.from.ss_family;
Willy Tarreau645513a2010-05-24 20:55:15 +02001268 if (test->i == AF_INET)
Willy Tarreau957c0a52011-03-03 17:42:23 +01001269 test->ptr = (void *)&((struct sockaddr_in *)&l4->si[0].addr.c.from)->sin_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001270 else if (test->i == AF_INET6)
Willy Tarreau957c0a52011-03-03 17:42:23 +01001271 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->si[0].addr.c.from))->sin6_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001272 else
1273 return 0;
1274
Willy Tarreau645513a2010-05-24 20:55:15 +02001275 test->flags = ACL_TEST_F_READ_ONLY;
1276 return 1;
1277}
1278
David du Colombier4f92d322011-03-24 11:09:31 +01001279/* extract the connection's source ipv4 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001280static int
1281pattern_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02001282 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau645513a2010-05-24 20:55:15 +02001283{
Willy Tarreau957c0a52011-03-03 17:42:23 +01001284 if (l4->si[0].addr.c.from.ss_family != AF_INET )
Emeric Brunf769f512010-10-22 17:14:01 +02001285 return 0;
1286
Willy Tarreau957c0a52011-03-03 17:42:23 +01001287 data->ip.s_addr = ((struct sockaddr_in *)&l4->si[0].addr.c.from)->sin_addr.s_addr;
Willy Tarreau645513a2010-05-24 20:55:15 +02001288 return 1;
1289}
1290
David du Colombier4f92d322011-03-24 11:09:31 +01001291/* extract the connection's source ipv6 address */
1292static int
1293pattern_fetch_src6(struct proxy *px, struct session *l4, void *l7, int dir,
1294 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
1295{
1296 if (l4->si[0].addr.c.from.ss_family != AF_INET6)
1297 return 0;
1298
1299 memcpy(data->ipv6.s6_addr, ((struct sockaddr_in6 *)&l4->si[0].addr.c.from)->sin6_addr.s6_addr, sizeof(data->ipv6.s6_addr));
1300 return 1;
1301}
Willy Tarreau645513a2010-05-24 20:55:15 +02001302
1303/* set test->i to the connection's source port */
1304static int
1305acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir,
1306 struct acl_expr *expr, struct acl_test *test)
1307{
Willy Tarreau957c0a52011-03-03 17:42:23 +01001308 if (l4->si[0].addr.c.from.ss_family == AF_INET)
1309 test->i = ntohs(((struct sockaddr_in *)&l4->si[0].addr.c.from)->sin_port);
1310 else if (l4->si[0].addr.c.from.ss_family == AF_INET6)
1311 test->i = ntohs(((struct sockaddr_in6 *)(&l4->si[0].addr.c.from))->sin6_port);
Emeric Brunf769f512010-10-22 17:14:01 +02001312 else
1313 return 0;
1314
Willy Tarreau645513a2010-05-24 20:55:15 +02001315 test->flags = 0;
1316 return 1;
1317}
1318
1319
1320/* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */
1321static int
1322acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
1323 struct acl_expr *expr, struct acl_test *test)
1324{
1325 if (!(l4->flags & SN_FRT_ADDR_SET))
1326 get_frt_addr(l4);
1327
Willy Tarreau957c0a52011-03-03 17:42:23 +01001328 test->i = l4->si[0].addr.c.to.ss_family;
Willy Tarreau645513a2010-05-24 20:55:15 +02001329 if (test->i == AF_INET)
Willy Tarreau957c0a52011-03-03 17:42:23 +01001330 test->ptr = (void *)&((struct sockaddr_in *)&l4->si[0].addr.c.to)->sin_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001331 else if (test->i == AF_INET6)
Willy Tarreau957c0a52011-03-03 17:42:23 +01001332 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->si[0].addr.c.to))->sin6_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001333 else
1334 return 0;
1335
Willy Tarreau645513a2010-05-24 20:55:15 +02001336 test->flags = ACL_TEST_F_READ_ONLY;
1337 return 1;
1338}
1339
1340
David du Colombier4f92d322011-03-24 11:09:31 +01001341/* extract the connection's destination ipv4 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001342static int
1343pattern_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02001344 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau645513a2010-05-24 20:55:15 +02001345{
emeric8aa6b372010-10-22 17:06:26 +02001346 if (!(l4->flags & SN_FRT_ADDR_SET))
1347 get_frt_addr(l4);
1348
Willy Tarreau957c0a52011-03-03 17:42:23 +01001349 if (l4->si[0].addr.c.to.ss_family != AF_INET)
Emeric Brunf769f512010-10-22 17:14:01 +02001350 return 0;
1351
Willy Tarreau957c0a52011-03-03 17:42:23 +01001352 data->ip.s_addr = ((struct sockaddr_in *)&l4->si[0].addr.c.to)->sin_addr.s_addr;
Willy Tarreau645513a2010-05-24 20:55:15 +02001353 return 1;
1354}
1355
David du Colombier4f92d322011-03-24 11:09:31 +01001356/* extract the connection's destination ipv6 address */
1357static int
1358pattern_fetch_dst6(struct proxy *px, struct session *l4, void *l7, int dir,
1359 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
1360{
1361 if (!(l4->flags & SN_FRT_ADDR_SET))
1362 get_frt_addr(l4);
1363
1364 if (l4->si[0].addr.c.to.ss_family != AF_INET6)
1365 return 0;
1366
1367 memcpy(data->ipv6.s6_addr, ((struct sockaddr_in6 *)&l4->si[0].addr.c.to)->sin6_addr.s6_addr, sizeof(data->ipv6.s6_addr));
1368 return 1;
1369}
1370
Willy Tarreau645513a2010-05-24 20:55:15 +02001371/* set test->i to the frontend connexion's destination port */
1372static int
1373acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
1374 struct acl_expr *expr, struct acl_test *test)
1375{
1376 if (!(l4->flags & SN_FRT_ADDR_SET))
1377 get_frt_addr(l4);
1378
Willy Tarreau957c0a52011-03-03 17:42:23 +01001379 if (l4->si[0].addr.c.to.ss_family == AF_INET)
1380 test->i = ntohs(((struct sockaddr_in *)&l4->si[0].addr.c.to)->sin_port);
1381 else if (l4->si[0].addr.c.to.ss_family == AF_INET6)
1382 test->i = ntohs(((struct sockaddr_in6 *)(&l4->si[0].addr.c.to))->sin6_port);
Emeric Brunf769f512010-10-22 17:14:01 +02001383 else
1384 return 0;
1385
Willy Tarreau645513a2010-05-24 20:55:15 +02001386 test->flags = 0;
1387 return 1;
1388}
1389
1390static int
1391pattern_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02001392 const struct pattern_arg *arg, int i, union pattern_data *data)
Willy Tarreau645513a2010-05-24 20:55:15 +02001393{
emeric8aa6b372010-10-22 17:06:26 +02001394 if (!(l4->flags & SN_FRT_ADDR_SET))
1395 get_frt_addr(l4);
1396
David du Colombier4f92d322011-03-24 11:09:31 +01001397 if (l4->si[0].addr.c.to.ss_family == AF_INET)
1398 data->integer = ntohs(((struct sockaddr_in *)&l4->si[0].addr.c.to)->sin_port);
1399 else if (l4->si[0].addr.c.to.ss_family == AF_INET6)
1400 data->integer = ntohs(((struct sockaddr_in6 *)&l4->si[0].addr.c.to)->sin6_port);
1401 else
Emeric Brunf769f512010-10-22 17:14:01 +02001402 return 0;
1403
Willy Tarreau645513a2010-05-24 20:55:15 +02001404 return 1;
1405}
1406
Emericf2d7cae2010-11-05 18:13:50 +01001407static int
1408pattern_arg_fetch_payloadlv(const char *arg, struct pattern_arg **arg_p, int *arg_i)
1409{
1410 int member = 0;
1411 int len_offset = 0;
1412 int len_size = 0;
1413 int buf_offset = 0;
1414 int relative = 0;
1415 int arg_len = strlen(arg);
1416 int i;
1417
1418 for (i = 0; i < arg_len; i++) {
1419 if (arg[i] == ',') {
1420 member++;
1421 } else if (member == 0) {
1422 if (arg[i] < '0' || arg[i] > '9')
1423 return 0;
1424
1425 len_offset = 10 * len_offset + arg[i] - '0';
1426 } else if (member == 1) {
1427 if (arg[i] < '0' || arg[i] > '9')
1428 return 0;
1429
1430 len_size = 10 * len_size + arg[i] - '0';
1431 } else if (member == 2) {
1432 if (!relative && !buf_offset && arg[i] == '+') {
1433 relative = 1;
1434 continue;
1435 } else if (!relative && !buf_offset && arg[i] == '-') {
1436 relative = 2;
1437 continue;
1438 } else if (arg[i] < '0' || arg[i] > '9')
1439 return 0;
1440
1441 buf_offset = 10 * buf_offset + arg[i] - '0';
1442 }
1443 }
1444
1445 if (member < 1)
1446 return 0;
1447
1448 if (!len_size)
1449 return 0;
1450
1451 if (member == 1) {
1452 buf_offset = len_offset + len_size;
1453 }
1454 else if (relative == 1) {
1455 buf_offset = len_offset + len_size + buf_offset;
1456 }
1457 else if (relative == 2) {
1458 if (len_offset + len_size < buf_offset)
1459 return 0;
1460
1461 buf_offset = len_offset + len_size - buf_offset;
1462 }
1463
1464 *arg_i = 3;
1465 *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg));
1466 (*arg_p)[0].type = PATTERN_ARG_TYPE_INTEGER;
1467 (*arg_p)[0].data.integer = len_offset;
1468 (*arg_p)[1].type = PATTERN_ARG_TYPE_INTEGER;
1469 (*arg_p)[1].data.integer = len_size;
1470 (*arg_p)[2].type = PATTERN_ARG_TYPE_INTEGER;
1471 (*arg_p)[2].data.integer = buf_offset;
1472
1473 return 1;
1474}
1475
1476static int
1477pattern_fetch_payloadlv(struct proxy *px, struct session *l4, void *l7, int dir,
1478 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
1479{
1480 int len_offset = arg_p[0].data.integer;
1481 int len_size = arg_p[1].data.integer;
1482 int buf_offset = arg_p[2].data.integer;
1483 int buf_size = 0;
1484 struct buffer *b;
1485 int i;
1486
1487 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1488 /* by default buf offset == len offset + len size */
1489 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1490
1491 if (!l4)
1492 return 0;
1493
1494 b = (dir & PATTERN_FETCH_RTR) ? l4->rep : l4->req;
1495
1496 if (!b || !b->l)
1497 return 0;
1498
1499 if (len_offset + len_size > b->l)
1500 return 0;
1501
1502 for (i = 0; i < len_size; i++) {
1503 buf_size = (buf_size << 8) + ((unsigned char *)b->w)[i + len_offset];
1504 }
1505
1506 if (!buf_size)
1507 return 0;
1508
1509 if (buf_offset + buf_size > b->l)
1510 return 0;
1511
1512 /* init chunk as read only */
1513 chunk_initlen(&data->str, (char *)(b->w + buf_offset), 0, buf_size);
1514
1515 return 1;
1516}
1517
1518static int
1519pattern_arg_fetch_payload (const char *arg, struct pattern_arg **arg_p, int *arg_i)
1520{
1521 int member = 0;
1522 int buf_offset = 0;
1523 int buf_size = 0;
1524 int arg_len = strlen(arg);
1525 int i;
1526
1527 for (i = 0 ; i < arg_len ; i++) {
1528 if (arg[i] == ',') {
1529 member++;
1530 } else if (member == 0) {
1531 if (arg[i] < '0' || arg[i] > '9')
1532 return 0;
1533
1534 buf_offset = 10 * buf_offset + arg[i] - '0';
1535 } else if (member == 1) {
1536 if (arg[i] < '0' || arg[i] > '9')
1537 return 0;
1538
1539 buf_size = 10 * buf_size + arg[i] - '0';
1540 }
1541 }
1542
1543 if (!buf_size)
1544 return 0;
1545
1546 *arg_i = 2;
1547 *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg));
1548 (*arg_p)[0].type = PATTERN_ARG_TYPE_INTEGER;
1549 (*arg_p)[0].data.integer = buf_offset;
1550 (*arg_p)[1].type = PATTERN_ARG_TYPE_INTEGER;
1551 (*arg_p)[1].data.integer = buf_size;
1552
1553 return 1;
1554}
1555
1556static int
1557pattern_fetch_payload(struct proxy *px, struct session *l4, void *l7, int dir,
1558 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
1559{
1560 int buf_offset = arg_p[0].data.integer;
1561 int buf_size = arg_p[1].data.integer;
1562 struct buffer *b;
1563
1564 if (!l4)
1565 return 0;
1566
1567 b = (dir & PATTERN_FETCH_RTR) ? l4->rep : l4->req;
1568
1569 if (!b || !b->l)
1570 return 0;
1571
1572 if (buf_offset + buf_size > b->l)
1573 return 0;
1574
1575 /* init chunk as read only */
1576 chunk_initlen(&data->str, (char *)(b->w + buf_offset), 0, buf_size);
1577
1578 return 1;
1579}
1580
Willy Tarreaub6866442008-07-14 23:54:42 +02001581static struct cfg_kw_list cfg_kws = {{ },{
1582 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001583 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001584 { 0, NULL, NULL },
1585}};
1586
Willy Tarreau645513a2010-05-24 20:55:15 +02001587/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub6866442008-07-14 23:54:42 +02001588static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +02001589 { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT },
1590 { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1591 { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1592 { "dst_port", acl_parse_int, acl_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT },
Willy Tarreaub6866442008-07-14 23:54:42 +02001593 { NULL, NULL, NULL, NULL },
1594}};
1595
Willy Tarreau645513a2010-05-24 20:55:15 +02001596/* Note: must not be declared <const> as its list will be overwritten */
1597static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emericf2d7cae2010-11-05 18:13:50 +01001598 { "src", pattern_fetch_src, NULL, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David du Colombier4f92d322011-03-24 11:09:31 +01001599 { "src6", pattern_fetch_src6, NULL, PATTERN_TYPE_IPV6, PATTERN_FETCH_REQ },
Emericf2d7cae2010-11-05 18:13:50 +01001600 { "dst", pattern_fetch_dst, NULL, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David du Colombier4f92d322011-03-24 11:09:31 +01001601 { "dst6", pattern_fetch_dst6, NULL, PATTERN_TYPE_IPV6, PATTERN_FETCH_REQ },
Emericf2d7cae2010-11-05 18:13:50 +01001602 { "dst_port", pattern_fetch_dport, NULL, PATTERN_TYPE_INTEGER, PATTERN_FETCH_REQ },
1603 { "payload", pattern_fetch_payload, pattern_arg_fetch_payload, PATTERN_TYPE_CONSTDATA, PATTERN_FETCH_REQ|PATTERN_FETCH_RTR },
1604 { "payload_lv", pattern_fetch_payloadlv, pattern_arg_fetch_payloadlv, PATTERN_TYPE_CONSTDATA, PATTERN_FETCH_REQ|PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02001605 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001606}};
1607
Willy Tarreaue6b98942007-10-29 01:09:36 +01001608__attribute__((constructor))
1609static void __tcp_protocol_init(void)
1610{
1611 protocol_register(&proto_tcpv4);
1612 protocol_register(&proto_tcpv6);
Willy Tarreau645513a2010-05-24 20:55:15 +02001613 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001614 cfg_register_keywords(&cfg_kws);
1615 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001616}
1617
1618
1619/*
1620 * Local variables:
1621 * c-indent-level: 8
1622 * c-basic-offset: 8
1623 * End:
1624 */