blob: 7adb0f99905b88c217c8352a91a7bf94f534b895 [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>
Willy Tarreaue6b98942007-10-29 01:09:36 +010053
Willy Tarreaue8c66af2008-01-13 18:40:14 +010054#ifdef CONFIG_HAP_CTTPROXY
55#include <import/ip_tproxy.h>
56#endif
57
Willy Tarreaue6b98942007-10-29 01:09:36 +010058static int tcp_bind_listeners(struct protocol *proto);
59
60/* Note: must not be declared <const> as its list will be overwritten */
61static struct protocol proto_tcpv4 = {
62 .name = "tcpv4",
63 .sock_domain = AF_INET,
64 .sock_type = SOCK_STREAM,
65 .sock_prot = IPPROTO_TCP,
66 .sock_family = AF_INET,
67 .sock_addrlen = sizeof(struct sockaddr_in),
68 .l3_addrlen = 32/8,
Willy Tarreaueb472682010-05-28 18:46:57 +020069 .accept = &stream_sock_accept,
Willy Tarreaue6b98942007-10-29 01:09:36 +010070 .read = &stream_sock_read,
71 .write = &stream_sock_write,
72 .bind_all = tcp_bind_listeners,
73 .unbind_all = unbind_all_listeners,
74 .enable_all = enable_all_listeners,
75 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
76 .nb_listeners = 0,
77};
78
79/* Note: must not be declared <const> as its list will be overwritten */
80static struct protocol proto_tcpv6 = {
81 .name = "tcpv6",
82 .sock_domain = AF_INET6,
83 .sock_type = SOCK_STREAM,
84 .sock_prot = IPPROTO_TCP,
85 .sock_family = AF_INET6,
86 .sock_addrlen = sizeof(struct sockaddr_in6),
87 .l3_addrlen = 128/8,
Willy Tarreaueb472682010-05-28 18:46:57 +020088 .accept = &stream_sock_accept,
Willy Tarreaue6b98942007-10-29 01:09:36 +010089 .read = &stream_sock_read,
90 .write = &stream_sock_write,
91 .bind_all = tcp_bind_listeners,
92 .unbind_all = unbind_all_listeners,
93 .enable_all = enable_all_listeners,
94 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
95 .nb_listeners = 0,
96};
97
Willy Tarreaue8c66af2008-01-13 18:40:14 +010098
99/* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which
100 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
101 * - 0 : ignore remote address (may even be a NULL pointer)
102 * - 1 : use provided address
103 * - 2 : use provided port
104 * - 3 : use both
105 *
106 * The function supports multiple foreign binding methods :
107 * - linux_tproxy: we directly bind to the foreign address
108 * - cttproxy: we bind to a local address then nat.
109 * The second one can be used as a fallback for the first one.
110 * This function returns 0 when everything's OK, 1 if it could not bind, to the
111 * local address, 2 if it could not bind to the foreign address.
112 */
113int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote)
114{
115 struct sockaddr_in bind_addr;
116 int foreign_ok = 0;
117 int ret;
118
119#ifdef CONFIG_HAP_LINUX_TPROXY
120 static int ip_transp_working = 1;
121 if (flags && ip_transp_working) {
122 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0
123 || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0)
124 foreign_ok = 1;
125 else
126 ip_transp_working = 0;
127 }
128#endif
129 if (flags) {
130 memset(&bind_addr, 0, sizeof(bind_addr));
131 if (flags & 1)
132 bind_addr.sin_addr = remote->sin_addr;
133 if (flags & 2)
134 bind_addr.sin_port = remote->sin_port;
135 }
136
137 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
138 if (foreign_ok) {
139 ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
140 if (ret < 0)
141 return 2;
142 }
143 else {
144 ret = bind(fd, (struct sockaddr *)local, sizeof(*local));
145 if (ret < 0)
146 return 1;
147 }
148
149 if (!flags)
150 return 0;
151
152#ifdef CONFIG_HAP_CTTPROXY
153 if (!foreign_ok) {
154 struct in_tproxy itp1, itp2;
155 memset(&itp1, 0, sizeof(itp1));
156
157 itp1.op = TPROXY_ASSIGN;
158 itp1.v.addr.faddr = bind_addr.sin_addr;
159 itp1.v.addr.fport = bind_addr.sin_port;
160
161 /* set connect flag on socket */
162 itp2.op = TPROXY_FLAGS;
163 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
164
165 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
166 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
167 foreign_ok = 1;
168 }
169 }
170#endif
171 if (!foreign_ok)
172 /* we could not bind to a foreign address */
173 return 2;
174
175 return 0;
176}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100177
Willy Tarreau9650f372009-08-16 14:02:45 +0200178
179/*
180 * This function initiates a connection to the server assigned to this session
Willy Tarreaub1d67742010-03-29 19:36:59 +0200181 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. A
182 * source address may be pointed to by <from_addr>. Note that this is only used
183 * in case of transparent proxying. Normal source bind addresses are still
184 * determined locally (due to the possible need of a source port).
185 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200186 * It can return one of :
187 * - SN_ERR_NONE if everything's OK
188 * - SN_ERR_SRVTO if there are no more servers
189 * - SN_ERR_SRVCL if the connection was refused by the server
190 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
191 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
192 * - SN_ERR_INTERNAL for any other purely internal errors
193 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
194 */
195int tcpv4_connect_server(struct stream_interface *si,
196 struct proxy *be, struct server *srv,
Willy Tarreaub1d67742010-03-29 19:36:59 +0200197 struct sockaddr *srv_addr, struct sockaddr *from_addr)
Willy Tarreau9650f372009-08-16 14:02:45 +0200198{
199 int fd;
200
201 if ((fd = si->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
202 qfprintf(stderr, "Cannot get a server socket.\n");
203
204 if (errno == ENFILE)
205 send_log(be, LOG_EMERG,
206 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
207 be->id, maxfd);
208 else if (errno == EMFILE)
209 send_log(be, LOG_EMERG,
210 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
211 be->id, maxfd);
212 else if (errno == ENOBUFS || errno == ENOMEM)
213 send_log(be, LOG_EMERG,
214 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
215 be->id, maxfd);
216 /* this is a resource error */
217 return SN_ERR_RESOURCE;
218 }
219
220 if (fd >= global.maxsock) {
221 /* do not log anything there, it's a normal condition when this option
222 * is used to serialize connections to a server !
223 */
224 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
225 close(fd);
226 return SN_ERR_PRXCOND; /* it is a configuration limit */
227 }
228
229 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
230 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
231 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
232 close(fd);
233 return SN_ERR_INTERNAL;
234 }
235
236 if (be->options & PR_O_TCP_SRV_KA)
237 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
238
239 if (be->options & PR_O_TCP_NOLING)
240 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
241
242 /* allow specific binding :
243 * - server-specific at first
244 * - proxy-specific next
245 */
246 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200247 int ret, flags = 0;
248
Willy Tarreau9650f372009-08-16 14:02:45 +0200249 switch (srv->state & SRV_TPROXY_MASK) {
250 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200251 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200252 flags = 3;
253 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200254 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200255 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200256 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200257 break;
258 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200259
Willy Tarreau9650f372009-08-16 14:02:45 +0200260#ifdef SO_BINDTODEVICE
261 /* Note: this might fail if not CAP_NET_RAW */
262 if (srv->iface_name)
263 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
264#endif
265
266 if (srv->sport_range) {
267 int attempts = 10; /* should be more than enough to find a spare port */
268 struct sockaddr_in src;
269
270 ret = 1;
271 src = srv->source_addr;
272
273 do {
274 /* note: in case of retry, we may have to release a previously
275 * allocated port, hence this loop's construct.
276 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200277 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
278 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200279
280 if (!attempts)
281 break;
282 attempts--;
283
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200284 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
285 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200286 break;
287
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200288 fdinfo[fd].port_range = srv->sport_range;
289 src.sin_port = htons(fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200290
Willy Tarreaub1d67742010-03-29 19:36:59 +0200291 ret = tcpv4_bind_socket(fd, flags, &src, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200292 } while (ret != 0); /* binding NOK */
293 }
294 else {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200295 ret = tcpv4_bind_socket(fd, flags, &srv->source_addr, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200296 }
297
298 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200299 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
300 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200301 close(fd);
302
303 if (ret == 1) {
304 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
305 be->id, srv->id);
306 send_log(be, LOG_EMERG,
307 "Cannot bind to source address before connect() for server %s/%s.\n",
308 be->id, srv->id);
309 } else {
310 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
311 be->id, srv->id);
312 send_log(be, LOG_EMERG,
313 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
314 be->id, srv->id);
315 }
316 return SN_ERR_RESOURCE;
317 }
318 }
319 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200320 int ret, flags = 0;
321
Willy Tarreau9650f372009-08-16 14:02:45 +0200322 switch (be->options & PR_O_TPXY_MASK) {
323 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200324 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200325 flags = 3;
326 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200327 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200328 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200329 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200330 break;
331 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200332
Willy Tarreau9650f372009-08-16 14:02:45 +0200333#ifdef SO_BINDTODEVICE
334 /* Note: this might fail if not CAP_NET_RAW */
335 if (be->iface_name)
336 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
337#endif
Willy Tarreaub1d67742010-03-29 19:36:59 +0200338 ret = tcpv4_bind_socket(fd, flags, &be->source_addr, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200339 if (ret) {
340 close(fd);
341 if (ret == 1) {
342 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
343 be->id);
344 send_log(be, LOG_EMERG,
345 "Cannot bind to source address before connect() for proxy %s.\n",
346 be->id);
347 } else {
348 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
349 be->id);
350 send_log(be, LOG_EMERG,
351 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
352 be->id);
353 }
354 return SN_ERR_RESOURCE;
355 }
356 }
357
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400358#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200359 /* disabling tcp quick ack now allows the first request to leave the
360 * machine with the first ACK. We only do this if there are pending
361 * data in the buffer.
362 */
363 if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400364 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200365#endif
366
Willy Tarreaue803de22010-01-21 17:43:04 +0100367 if (global.tune.server_sndbuf)
368 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
369
370 if (global.tune.server_rcvbuf)
371 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
372
Willy Tarreau9650f372009-08-16 14:02:45 +0200373 if ((connect(fd, (struct sockaddr *)srv_addr, sizeof(struct sockaddr_in)) == -1) &&
374 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
375
376 if (errno == EAGAIN || errno == EADDRINUSE) {
377 char *msg;
378 if (errno == EAGAIN) /* no free ports left, try again later */
379 msg = "no free ports";
380 else
381 msg = "local address already in use";
382
383 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200384 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
385 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200386 close(fd);
387 send_log(be, LOG_EMERG,
388 "Connect() failed for server %s/%s: %s.\n",
389 be->id, srv->id, msg);
390 return SN_ERR_RESOURCE;
391 } else if (errno == ETIMEDOUT) {
392 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200393 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
394 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200395 close(fd);
396 return SN_ERR_SRVTO;
397 } else {
398 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
399 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200400 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
401 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200402 close(fd);
403 return SN_ERR_SRVCL;
404 }
405 }
406
407 fdtab[fd].owner = si;
408 fdtab[fd].state = FD_STCONN; /* connection in progress */
409 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
410 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
411 fdtab[fd].cb[DIR_RD].b = si->ib;
412 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
413 fdtab[fd].cb[DIR_WR].b = si->ob;
414
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200415 fdinfo[fd].peeraddr = (struct sockaddr *)srv_addr;
416 fdinfo[fd].peerlen = sizeof(struct sockaddr_in);
Willy Tarreau9650f372009-08-16 14:02:45 +0200417
418 fd_insert(fd);
419 EV_FD_SET(fd, DIR_WR); /* for connect status */
420
421 si->state = SI_ST_CON;
422 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
423 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
424
425 return SN_ERR_NONE; /* connection is OK */
426}
427
428
Willy Tarreaue6b98942007-10-29 01:09:36 +0100429/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
430 * an error message in <err> if the message is at most <errlen> bytes long
431 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
432 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
433 * was alright and that no message was returned. ERR_RETRYABLE means that an
434 * error occurred but that it may vanish after a retry (eg: port in use), and
435 * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter
436 * the meaning of the error, but just indicate that a message is present which
437 * should be displayed with the respective level. Last, ERR_ABORT indicates
438 * that it's pointless to try to start other listeners. No error message is
439 * returned if errlen is NULL.
440 */
441int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
442{
443 __label__ tcp_return, tcp_close_return;
444 int fd, err;
445 const char *msg = NULL;
446
447 /* ensure we never return garbage */
448 if (errmsg && errlen)
449 *errmsg = 0;
450
451 if (listener->state != LI_ASSIGNED)
452 return ERR_NONE; /* already bound */
453
454 err = ERR_NONE;
455
456 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
457 err |= ERR_RETRYABLE | ERR_ALERT;
458 msg = "cannot create listening socket";
459 goto tcp_return;
460 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100461
Willy Tarreaue6b98942007-10-29 01:09:36 +0100462 if (fd >= global.maxsock) {
463 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
464 msg = "not enough free sockets (raise '-n' parameter)";
465 goto tcp_close_return;
466 }
467
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200468 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100469 err |= ERR_FATAL | ERR_ALERT;
470 msg = "cannot make socket non-blocking";
471 goto tcp_close_return;
472 }
473
474 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
475 /* not fatal but should be reported */
476 msg = "cannot do so_reuseaddr";
477 err |= ERR_ALERT;
478 }
479
480 if (listener->options & LI_O_NOLINGER)
481 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100482
Willy Tarreaue6b98942007-10-29 01:09:36 +0100483#ifdef SO_REUSEPORT
484 /* OpenBSD supports this. As it's present in old libc versions of Linux,
485 * it might return an error that we will silently ignore.
486 */
487 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
488#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100489#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100490 if ((listener->options & LI_O_FOREIGN)
Willy Tarreau0a459892008-01-13 17:37:16 +0100491 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1)
492 && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100493 msg = "cannot make listening socket transparent";
494 err |= ERR_ALERT;
495 }
496#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100497#ifdef SO_BINDTODEVICE
498 /* Note: this might fail if not CAP_NET_RAW */
499 if (listener->interface) {
500 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100501 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100502 msg = "cannot bind listener to device";
503 err |= ERR_WARN;
504 }
505 }
506#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400507#if defined(TCP_MAXSEG)
Willy Tarreaube1b9182009-06-14 18:48:19 +0200508 if (listener->maxseg) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400509 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200510 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
511 msg = "cannot set MSS";
512 err |= ERR_WARN;
513 }
514 }
515#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200516#if defined(TCP_DEFER_ACCEPT)
517 if (listener->options & LI_O_DEF_ACCEPT) {
518 /* defer accept by up to one second */
519 int accept_delay = 1;
520 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
521 msg = "cannot enable DEFER_ACCEPT";
522 err |= ERR_WARN;
523 }
524 }
525#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100526 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
527 err |= ERR_RETRYABLE | ERR_ALERT;
528 msg = "cannot bind socket";
529 goto tcp_close_return;
530 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100531
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100532 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100533 err |= ERR_RETRYABLE | ERR_ALERT;
534 msg = "cannot listen to socket";
535 goto tcp_close_return;
536 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100537
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400538#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200539 if (listener->options & LI_O_NOQUICKACK)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400540 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200541#endif
542
Willy Tarreaue6b98942007-10-29 01:09:36 +0100543 /* the socket is ready */
544 listener->fd = fd;
545 listener->state = LI_LISTEN;
546
Willy Tarreaueabf3132008-08-29 23:36:51 +0200547 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100548 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaueb472682010-05-28 18:46:57 +0200549 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
550 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
551 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
552 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200553
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200554 fdinfo[fd].peeraddr = NULL;
555 fdinfo[fd].peerlen = 0;
Willy Tarreaueb472682010-05-28 18:46:57 +0200556 fd_insert(fd);
557
Willy Tarreaue6b98942007-10-29 01:09:36 +0100558 tcp_return:
559 if (msg && errlen)
560 strlcpy2(errmsg, msg, errlen);
561 return err;
562
563 tcp_close_return:
564 close(fd);
565 goto tcp_return;
566}
567
568/* This function creates all TCP sockets bound to the protocol entry <proto>.
569 * It is intended to be used as the protocol's bind_all() function.
570 * The sockets will be registered but not added to any fd_set, in order not to
571 * loose them across the fork(). A call to enable_all_listeners() is needed
572 * to complete initialization. The return value is composed from ERR_*.
573 */
574static int tcp_bind_listeners(struct protocol *proto)
575{
576 struct listener *listener;
577 int err = ERR_NONE;
578
579 list_for_each_entry(listener, &proto->listeners, proto_list) {
580 err |= tcp_bind_listener(listener, NULL, 0);
581 if ((err & ERR_CODE) == ERR_ABORT)
582 break;
583 }
584
585 return err;
586}
587
588/* Add listener to the list of tcpv4 listeners. The listener's state
589 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
590 * listeners is updated. This is the function to use to add a new listener.
591 */
592void tcpv4_add_listener(struct listener *listener)
593{
594 if (listener->state != LI_INIT)
595 return;
596 listener->state = LI_ASSIGNED;
597 listener->proto = &proto_tcpv4;
598 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
599 proto_tcpv4.nb_listeners++;
600}
601
602/* Add listener to the list of tcpv4 listeners. The listener's state
603 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
604 * listeners is updated. This is the function to use to add a new listener.
605 */
606void tcpv6_add_listener(struct listener *listener)
607{
608 if (listener->state != LI_INIT)
609 return;
610 listener->state = LI_ASSIGNED;
611 listener->proto = &proto_tcpv6;
612 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
613 proto_tcpv6.nb_listeners++;
614}
615
Willy Tarreauedcf6682008-11-30 23:15:34 +0100616/* This function performs the TCP request analysis on the current request. It
617 * returns 1 if the processing can continue on next analysers, or zero if it
618 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200619 * request. It relies on buffers flags, and updates s->req->analysers. The
620 * function may be called for frontend rules and backend rules. It only relies
621 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100622 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200623int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100624{
625 struct tcp_rule *rule;
626 int partial;
627
628 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
629 now_ms, __FUNCTION__,
630 s,
631 req,
632 req->rex, req->wex,
633 req->flags,
634 req->l,
635 req->analysers);
636
Willy Tarreauedcf6682008-11-30 23:15:34 +0100637 /* We don't know whether we have enough data, so must proceed
638 * this way :
639 * - iterate through all rules in their declaration order
640 * - if one rule returns MISS, it means the inspect delay is
641 * not over yet, then return immediately, otherwise consider
642 * it as a non-match.
643 * - if one rule returns OK, then return OK
644 * - if one rule returns KO, then return KO
645 */
646
Willy Tarreaufb356202010-08-03 14:02:05 +0200647 if (req->flags & BF_SHUTR || !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100648 partial = 0;
649 else
650 partial = ACL_PARTIAL;
651
Willy Tarreaufb356202010-08-03 14:02:05 +0200652 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100653 int ret = ACL_PAT_PASS;
654
655 if (rule->cond) {
Willy Tarreaufb356202010-08-03 14:02:05 +0200656 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100657 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200658 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100659 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200660 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
661 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100662 return 0;
663 }
664
665 ret = acl_pass(ret);
666 if (rule->cond->pol == ACL_COND_UNLESS)
667 ret = !ret;
668 }
669
670 if (ret) {
671 /* we have a matching rule. */
672 if (rule->action == TCP_ACT_REJECT) {
673 buffer_abort(req);
674 buffer_abort(s->rep);
675 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200676
Willy Tarreaufb356202010-08-03 14:02:05 +0200677 s->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200678 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200679 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200680
Willy Tarreauedcf6682008-11-30 23:15:34 +0100681 if (!(s->flags & SN_ERR_MASK))
682 s->flags |= SN_ERR_PRXCOND;
683 if (!(s->flags & SN_FINST_MASK))
684 s->flags |= SN_FINST_R;
685 return 0;
686 }
687 /* otherwise accept */
688 break;
689 }
690 }
691
692 /* if we get there, it means we have no rule which matches, or
693 * we have an explicit accept, so we apply the default accept.
694 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200695 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100696 req->analyse_exp = TICK_ETERNITY;
697 return 1;
698}
699
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200700/* This function performs the TCP layer4 analysis on the current request. It
701 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
702 * matches or if no more rule matches. It can only use rules which don't need
703 * any data.
704 */
705int tcp_exec_req_rules(struct session *s)
706{
707 struct tcp_rule *rule;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200708 struct stksess *ts = s->tracked_counters;
709 struct stktable *t = NULL;
710 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200711 int ret;
712
713 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
714 ret = ACL_PAT_PASS;
715
716 if (rule->cond) {
717 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ);
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) {
Willy Tarreau2799e982010-06-05 15:43:21 +0200726 s->fe->counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200727 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200728 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200729
730 if (!(s->flags & SN_ERR_MASK))
731 s->flags |= SN_ERR_PRXCOND;
732 if (!(s->flags & SN_FINST_MASK))
733 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200734 result = 0;
735 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200736 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200737 else if (rule->action == TCP_ACT_TRK_CTR) {
738 if (!s->tracked_counters) {
739 /* only the first valid track-counters directive applies.
740 * Also, note that right now we can only track SRC so we
741 * don't check how to get the key, but later we may need
742 * to consider rule->act_prm->trk_ctr.type.
743 */
744 t = rule->act_prm.trk_ctr.table.t;
745 ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s));
746 if (ts)
747 session_track_counters(s, t, ts);
748 }
749 }
750 else {
751 /* otherwise it's an accept */
752 break;
753 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200754 }
755 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200756 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200757}
758
Willy Tarreaub6866442008-07-14 23:54:42 +0200759/* This function should be called to parse a line starting with the "tcp-request"
760 * keyword.
761 */
762static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
763 struct proxy *defpx, char *err, int errlen)
764{
765 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200766 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +0200767 int retlen;
Willy Tarreau1a687942010-05-23 22:40:30 +0200768 int warn = 0;
769 int pol = ACL_COND_NONE;
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200770 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +0200771 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +0200772
773 if (!*args[1]) {
774 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +0200775 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +0200776 return -1;
777 }
778
779 if (!strcmp(args[1], "inspect-delay")) {
780 if (curpx == defpx) {
781 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
782 args[0], args[1]);
783 return -1;
784 }
785
Willy Tarreaub6866442008-07-14 23:54:42 +0200786 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
787 retlen = snprintf(err, errlen,
788 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +0200789 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +0200790 if (ptr && retlen < errlen)
791 retlen += snprintf(err+retlen, errlen - retlen,
792 " (unexpected character '%c')", *ptr);
793 return -1;
794 }
795
796 if (curpx->tcp_req.inspect_delay) {
797 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +0200798 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +0200799 return 1;
800 }
801 curpx->tcp_req.inspect_delay = val;
802 return 0;
803 }
804
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200805 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
806 arg = 1;
807
Willy Tarreaub6866442008-07-14 23:54:42 +0200808 if (!strcmp(args[1], "content")) {
Willy Tarreaub6866442008-07-14 23:54:42 +0200809 if (curpx == defpx) {
810 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
811 args[0], args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200812 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +0200813 }
814
815 if (!strcmp(args[2], "accept"))
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200816 rule->action = TCP_ACT_ACCEPT;
Willy Tarreaub6866442008-07-14 23:54:42 +0200817 else if (!strcmp(args[2], "reject"))
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200818 rule->action = TCP_ACT_REJECT;
Willy Tarreaub6866442008-07-14 23:54:42 +0200819 else {
820 retlen = snprintf(err, errlen,
821 "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')",
822 args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200823 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +0200824 }
825
826 pol = ACL_COND_NONE;
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200827 rule->cond = NULL;
Willy Tarreaub6866442008-07-14 23:54:42 +0200828
Willy Tarreauef6494c2010-01-28 17:12:36 +0100829 if (strcmp(args[3], "if") == 0 || strcmp(args[3], "unless") == 0) {
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200830 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+3)) == NULL) {
Willy Tarreauef6494c2010-01-28 17:12:36 +0100831 retlen = snprintf(err, errlen,
832 "error detected in %s '%s' while parsing '%s' condition",
833 proxy_type_str(curpx), curpx->id, args[3]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200834 goto error;
Willy Tarreauef6494c2010-01-28 17:12:36 +0100835 }
836 }
837 else if (*args[3]) {
Willy Tarreau606ad732009-07-14 21:17:05 +0200838 retlen = snprintf(err, errlen,
839 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
840 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[3]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200841 goto error;
Willy Tarreau606ad732009-07-14 21:17:05 +0200842 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200843
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200844 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200845 struct acl *acl;
846 const char *name;
847
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200848 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200849 name = acl ? acl->name : "(unknown)";
850
851 retlen = snprintf(err, errlen,
Willy Tarreau1a211942009-07-14 13:53:17 +0200852 "acl '%s' involves some response-only criteria which will be ignored.",
853 name);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200854 warn++;
855 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200856 LIST_INIT(&rule->list);
857 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200858 return warn;
Willy Tarreaub6866442008-07-14 23:54:42 +0200859 }
860
Willy Tarreau1a687942010-05-23 22:40:30 +0200861 /* OK so we're in front of plain L4 rules */
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200862
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200863 if (strcmp(args[1], "accept") == 0) {
864 arg++;
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200865 rule->action = TCP_ACT_ACCEPT;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200866 }
867 else if (strcmp(args[1], "reject") == 0) {
868 arg++;
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200869 rule->action = TCP_ACT_REJECT;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200870 }
871 else if (strcmp(args[1], "track-counters") == 0) {
872 int ret;
873
874 arg++;
875 ret = parse_track_counters(args, &arg, section_type, curpx,
876 &rule->act_prm.trk_ctr, defpx, err, errlen);
877
878 if (ret < 0) /* nb: warnings are not handled yet */
879 goto error;
880
881 rule->action = TCP_ACT_TRK_CTR;
882 }
Willy Tarreau1a687942010-05-23 22:40:30 +0200883 else {
884 retlen = snprintf(err, errlen,
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200885 "'%s' expects 'inspect-delay', 'content', 'accept', 'reject', or 'track-counters' in %s '%s' (was '%s')",
Willy Tarreau1a687942010-05-23 22:40:30 +0200886 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200887 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +0200888 }
889
890 if (curpx == defpx) {
891 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
892 args[0], args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200893 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +0200894 }
895
896 pol = ACL_COND_NONE;
Willy Tarreau1a687942010-05-23 22:40:30 +0200897
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200898 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
899 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) {
Willy Tarreau1a687942010-05-23 22:40:30 +0200900 retlen = snprintf(err, errlen,
901 "error detected in %s '%s' while parsing '%s' condition",
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200902 proxy_type_str(curpx), curpx->id, args[arg]);
903 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +0200904 }
905 }
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200906 else if (*args[arg]) {
Willy Tarreau1a687942010-05-23 22:40:30 +0200907 retlen = snprintf(err, errlen,
908 "'%s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200909 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
910 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +0200911 }
912
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200913 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
Willy Tarreau1a687942010-05-23 22:40:30 +0200914 struct acl *acl;
915 const char *name;
916
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200917 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
Willy Tarreau1a687942010-05-23 22:40:30 +0200918 name = acl ? acl->name : "(unknown)";
919
920 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
921 retlen = snprintf(err, errlen,
922 "'%s %s' may not reference acl '%s' which makes use of payload in %s '%s'. Please use '%s content' for this.",
923 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200924 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +0200925 }
926 if (acl->requires & ACL_USE_RTR_ANY)
927 retlen = snprintf(err, errlen,
928 "acl '%s' involves some response-only criteria which will be ignored.",
929 name);
930
931 warn++;
932 }
933
Willy Tarreau1a687942010-05-23 22:40:30 +0200934 LIST_INIT(&rule->list);
935 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
936 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200937 error:
938 free(rule);
939 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +0200940}
941
Willy Tarreau645513a2010-05-24 20:55:15 +0200942
943/************************************************************************/
944/* All supported ACL keywords must be declared here. */
945/************************************************************************/
946
947/* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */
948static int
949acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
950 struct acl_expr *expr, struct acl_test *test)
951{
952 test->i = l4->cli_addr.ss_family;
953 if (test->i == AF_INET)
954 test->ptr = (void *)&((struct sockaddr_in *)&l4->cli_addr)->sin_addr;
955 else
956 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_addr;
957 test->flags = ACL_TEST_F_READ_ONLY;
958 return 1;
959}
960
961/* extract the connection's source address */
962static int
963pattern_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
964 const char *arg, int arg_len, union pattern_data *data)
965{
966 data->ip.s_addr = ((struct sockaddr_in *)&l4->cli_addr)->sin_addr.s_addr;
967 return 1;
968}
969
970
971/* set test->i to the connection's source port */
972static int
973acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir,
974 struct acl_expr *expr, struct acl_test *test)
975{
976 if (l4->cli_addr.ss_family == AF_INET)
977 test->i = ntohs(((struct sockaddr_in *)&l4->cli_addr)->sin_port);
978 else
979 test->i = ntohs(((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_port);
980 test->flags = 0;
981 return 1;
982}
983
984
985/* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */
986static int
987acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
988 struct acl_expr *expr, struct acl_test *test)
989{
990 if (!(l4->flags & SN_FRT_ADDR_SET))
991 get_frt_addr(l4);
992
993 test->i = l4->frt_addr.ss_family;
994 if (test->i == AF_INET)
995 test->ptr = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr;
996 else
997 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_addr;
998 test->flags = ACL_TEST_F_READ_ONLY;
999 return 1;
1000}
1001
1002
1003/* extract the connection's destination address */
1004static int
1005pattern_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
1006 const char *arg, int arg_len, union pattern_data *data)
1007{
1008 data->ip.s_addr = ((struct sockaddr_in *)&l4->frt_addr)->sin_addr.s_addr;
1009 return 1;
1010}
1011
1012/* set test->i to the frontend connexion's destination port */
1013static int
1014acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
1015 struct acl_expr *expr, struct acl_test *test)
1016{
1017 if (!(l4->flags & SN_FRT_ADDR_SET))
1018 get_frt_addr(l4);
1019
1020 if (l4->frt_addr.ss_family == AF_INET)
1021 test->i = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port);
1022 else
1023 test->i = ntohs(((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_port);
1024 test->flags = 0;
1025 return 1;
1026}
1027
1028static int
1029pattern_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
1030 const char *arg, int arg_len, union pattern_data *data)
1031
1032{
1033 data->integer = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port);
1034 return 1;
1035}
1036
Willy Tarreaua975b8f2010-06-05 19:13:27 +02001037/* set test->i to the number of connections from the session's source address
1038 * in the table pointed to by expr.
1039 */
1040static int
1041acl_fetch_src_count(struct proxy *px, struct session *l4, void *l7, int dir,
1042 struct acl_expr *expr, struct acl_test *test)
1043{
1044 struct stksess *ts;
1045
1046 /* right now we only support IPv4 */
1047 if (l4->cli_addr.ss_family != AF_INET)
1048 return 0;
1049
1050 if (expr->arg_len) {
1051 /* another table was designated, we must look for it */
1052 for (px = proxy; px; px = px->next)
1053 if (strcmp(px->id, expr->arg.str) == 0)
1054 break;
1055 }
1056 if (!px)
1057 return 0; /* table not found */
1058
1059 static_table_key.key = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr;
1060 test->flags = ACL_TEST_F_VOL_TEST;
1061 test->i = 0;
1062 if ((ts = stktable_lookup_key(&px->table, &static_table_key)) != NULL) {
1063 void *ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CUM);
1064 if (!ptr)
1065 return 0; /* parameter not stored */
1066 test->i = stktable_data_cast(ptr, conn_cum);
1067 }
1068
1069 return 1;
1070}
1071
1072/* set test->i to the number of connections from the session's source address
1073 * in the table pointed to by expr, after updating it.
1074 */
1075static int
1076acl_fetch_src_update_count(struct proxy *px, struct session *l4, void *l7, int dir,
1077 struct acl_expr *expr, struct acl_test *test)
1078{
1079 struct stksess *ts;
1080 void *ptr;
1081
1082 /* right now we only support IPv4 */
1083 if (l4->cli_addr.ss_family != AF_INET)
1084 return 0;
1085
1086 if (expr->arg_len) {
1087 /* another table was designated, we must look for it */
1088 for (px = proxy; px; px = px->next)
1089 if (strcmp(px->id, expr->arg.str) == 0)
1090 break;
1091 }
1092 if (!px)
1093 return 0;
1094
1095 static_table_key.key = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr;
1096 if ((ts = stktable_lookup_key(&px->table, &static_table_key)) == NULL) {
1097 /* entry does not exist, initialize a new one */
1098 ts = stksess_new(&px->table, &static_table_key);
1099 if (!ts)
1100 return 0;
1101 stktable_store(&px->table, ts);
1102 }
Willy Tarreaucb183642010-06-06 17:58:34 +02001103 else
1104 stktable_touch(&px->table, ts);
Willy Tarreaua975b8f2010-06-05 19:13:27 +02001105
1106 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CUM);
1107 if (!ptr)
1108 return 0; /* parameter not stored in this table */
1109
1110 test->i = ++stktable_data_cast(ptr, conn_cum);
1111 test->flags = ACL_TEST_F_VOL_TEST;
1112 return 1;
1113}
Willy Tarreau645513a2010-05-24 20:55:15 +02001114
Willy Tarreaub6866442008-07-14 23:54:42 +02001115static struct cfg_kw_list cfg_kws = {{ },{
1116 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1117 { 0, NULL, NULL },
1118}};
1119
Willy Tarreau645513a2010-05-24 20:55:15 +02001120/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub6866442008-07-14 23:54:42 +02001121static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +02001122 { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT },
1123 { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1124 { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1125 { "dst_port", acl_parse_int, acl_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT },
Willy Tarreau171819b2010-06-15 17:57:36 +02001126 { "src_count", acl_parse_int, acl_fetch_src_count,acl_match_int, ACL_USE_TCP4_VOLATILE },
1127 { "src_update_count", acl_parse_int, acl_fetch_src_update_count, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreaub6866442008-07-14 23:54:42 +02001128 { NULL, NULL, NULL, NULL },
1129}};
1130
Willy Tarreau645513a2010-05-24 20:55:15 +02001131/* Note: must not be declared <const> as its list will be overwritten */
1132static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
1133 { "src", pattern_fetch_src, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
1134 { "dst", pattern_fetch_dst, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
1135 { "dst_port", pattern_fetch_dport, PATTERN_TYPE_INTEGER, PATTERN_FETCH_REQ },
1136 { NULL, NULL, 0, 0 },
1137}};
1138
Willy Tarreaue6b98942007-10-29 01:09:36 +01001139__attribute__((constructor))
1140static void __tcp_protocol_init(void)
1141{
1142 protocol_register(&proto_tcpv4);
1143 protocol_register(&proto_tcpv6);
Willy Tarreau645513a2010-05-24 20:55:15 +02001144 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001145 cfg_register_keywords(&cfg_kws);
1146 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001147}
1148
1149
1150/*
1151 * Local variables:
1152 * c-indent-level: 8
1153 * c-basic-offset: 8
1154 * End:
1155 */