blob: 588279181704899ed2c5798a9e3f2bd356825a85 [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;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200626 struct stksess *ts;
627 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100628 int partial;
629
630 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
631 now_ms, __FUNCTION__,
632 s,
633 req,
634 req->rex, req->wex,
635 req->flags,
636 req->l,
637 req->analysers);
638
Willy Tarreauedcf6682008-11-30 23:15:34 +0100639 /* We don't know whether we have enough data, so must proceed
640 * this way :
641 * - iterate through all rules in their declaration order
642 * - if one rule returns MISS, it means the inspect delay is
643 * not over yet, then return immediately, otherwise consider
644 * it as a non-match.
645 * - if one rule returns OK, then return OK
646 * - if one rule returns KO, then return KO
647 */
648
Willy Tarreaufb356202010-08-03 14:02:05 +0200649 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 +0100650 partial = 0;
651 else
652 partial = ACL_PARTIAL;
653
Willy Tarreaufb356202010-08-03 14:02:05 +0200654 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100655 int ret = ACL_PAT_PASS;
656
657 if (rule->cond) {
Willy Tarreaufb356202010-08-03 14:02:05 +0200658 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100659 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200660 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100661 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200662 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
663 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100664 return 0;
665 }
666
667 ret = acl_pass(ret);
668 if (rule->cond->pol == ACL_COND_UNLESS)
669 ret = !ret;
670 }
671
672 if (ret) {
673 /* we have a matching rule. */
674 if (rule->action == TCP_ACT_REJECT) {
675 buffer_abort(req);
676 buffer_abort(s->rep);
677 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200678
Willy Tarreaufb356202010-08-03 14:02:05 +0200679 s->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200680 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200681 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200682
Willy Tarreauedcf6682008-11-30 23:15:34 +0100683 if (!(s->flags & SN_ERR_MASK))
684 s->flags |= SN_ERR_PRXCOND;
685 if (!(s->flags & SN_FINST_MASK))
686 s->flags |= SN_FINST_R;
687 return 0;
688 }
Willy Tarreau56123282010-08-06 19:06:56 +0200689 else if (rule->action == TCP_ACT_TRK_SC1) {
690 if (!s->stkctr1_entry) {
691 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200692 * Also, note that right now we can only track SRC so we
693 * don't check how to get the key, but later we may need
694 * to consider rule->act_prm->trk_ctr.type.
695 */
696 t = rule->act_prm.trk_ctr.table.t;
697 ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s));
698 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200699 session_track_stkctr1(s, t, ts);
Willy Tarreaud1f96522010-08-03 19:34:32 +0200700 }
701 }
Willy Tarreau56123282010-08-06 19:06:56 +0200702 else if (rule->action == TCP_ACT_TRK_SC2) {
703 if (!s->stkctr2_entry) {
704 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200705 * Also, note that right now we can only track SRC so we
706 * don't check how to get the key, but later we may need
707 * to consider rule->act_prm->trk_ctr.type.
708 */
709 t = rule->act_prm.trk_ctr.table.t;
710 ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s));
711 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200712 session_track_stkctr2(s, t, ts);
Willy Tarreaud1f96522010-08-03 19:34:32 +0200713 }
714 }
715 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100716 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200717 break;
718 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100719 }
720 }
721
722 /* if we get there, it means we have no rule which matches, or
723 * we have an explicit accept, so we apply the default accept.
724 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200725 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100726 req->analyse_exp = TICK_ETERNITY;
727 return 1;
728}
729
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200730/* This function performs the TCP layer4 analysis on the current request. It
731 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
732 * matches or if no more rule matches. It can only use rules which don't need
733 * any data.
734 */
735int tcp_exec_req_rules(struct session *s)
736{
737 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200738 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200739 struct stktable *t = NULL;
740 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200741 int ret;
742
743 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
744 ret = ACL_PAT_PASS;
745
746 if (rule->cond) {
747 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ);
748 ret = acl_pass(ret);
749 if (rule->cond->pol == ACL_COND_UNLESS)
750 ret = !ret;
751 }
752
753 if (ret) {
754 /* we have a matching rule. */
755 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau2799e982010-06-05 15:43:21 +0200756 s->fe->counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200757 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200758 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200759
760 if (!(s->flags & SN_ERR_MASK))
761 s->flags |= SN_ERR_PRXCOND;
762 if (!(s->flags & SN_FINST_MASK))
763 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200764 result = 0;
765 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200766 }
Willy Tarreau56123282010-08-06 19:06:56 +0200767 else if (rule->action == TCP_ACT_TRK_SC1) {
768 if (!s->stkctr1_entry) {
769 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200770 * Also, note that right now we can only track SRC so we
771 * don't check how to get the key, but later we may need
772 * to consider rule->act_prm->trk_ctr.type.
773 */
774 t = rule->act_prm.trk_ctr.table.t;
775 ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s));
776 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200777 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200778 }
779 }
Willy Tarreau56123282010-08-06 19:06:56 +0200780 else if (rule->action == TCP_ACT_TRK_SC2) {
781 if (!s->stkctr2_entry) {
782 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200783 * Also, note that right now we can only track SRC so we
784 * don't check how to get the key, but later we may need
785 * to consider rule->act_prm->trk_ctr.type.
786 */
787 t = rule->act_prm.trk_ctr.table.t;
788 ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s));
789 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200790 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200791 }
792 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200793 else {
794 /* otherwise it's an accept */
795 break;
796 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200797 }
798 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200799 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200800}
801
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200802/* Parse a tcp-request rule. Return a negative value in case of failure */
803static int tcp_parse_request_rule(char **args, int arg, int section_type,
804 struct proxy *curpx, struct proxy *defpx,
805 struct tcp_rule *rule, char *err, int errlen)
806{
807 if (curpx == defpx) {
808 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
809 args[0], args[1]);
810 return -1;
811 }
812
813 if (!strcmp(args[arg], "accept")) {
814 arg++;
815 rule->action = TCP_ACT_ACCEPT;
816 }
817 else if (!strcmp(args[arg], "reject")) {
818 arg++;
819 rule->action = TCP_ACT_REJECT;
820 }
Willy Tarreau56123282010-08-06 19:06:56 +0200821 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200822 int ret;
823
824 arg++;
825 ret = parse_track_counters(args, &arg, section_type, curpx,
826 &rule->act_prm.trk_ctr, defpx, err, errlen);
827
828 if (ret < 0) /* nb: warnings are not handled yet */
829 return -1;
830
Willy Tarreau56123282010-08-06 19:06:56 +0200831 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200832 }
Willy Tarreau56123282010-08-06 19:06:56 +0200833 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200834 int ret;
835
836 arg++;
837 ret = parse_track_counters(args, &arg, section_type, curpx,
838 &rule->act_prm.trk_ctr, defpx, err, errlen);
839
840 if (ret < 0) /* nb: warnings are not handled yet */
841 return -1;
842
Willy Tarreau56123282010-08-06 19:06:56 +0200843 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200844 }
845 else {
846 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +0200847 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
848 "or 'track-sc2' in %s '%s' (was '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200849 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
850 return -1;
851 }
852
853 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
854 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) {
855 snprintf(err, errlen,
856 "error detected in %s '%s' while parsing '%s' condition",
857 proxy_type_str(curpx), curpx->id, args[arg]);
858 return -1;
859 }
860 }
861 else if (*args[arg]) {
862 snprintf(err, errlen,
863 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
864 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
865 return -1;
866 }
867 return 0;
868}
869
Willy Tarreaub6866442008-07-14 23:54:42 +0200870/* This function should be called to parse a line starting with the "tcp-request"
871 * keyword.
872 */
873static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
874 struct proxy *defpx, char *err, int errlen)
875{
876 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200877 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +0200878 int retlen;
Willy Tarreau1a687942010-05-23 22:40:30 +0200879 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200880 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +0200881 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +0200882
883 if (!*args[1]) {
884 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +0200885 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +0200886 return -1;
887 }
888
889 if (!strcmp(args[1], "inspect-delay")) {
890 if (curpx == defpx) {
891 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
892 args[0], args[1]);
893 return -1;
894 }
895
Willy Tarreaub6866442008-07-14 23:54:42 +0200896 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
897 retlen = snprintf(err, errlen,
898 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +0200899 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +0200900 if (ptr && retlen < errlen)
901 retlen += snprintf(err+retlen, errlen - retlen,
902 " (unexpected character '%c')", *ptr);
903 return -1;
904 }
905
906 if (curpx->tcp_req.inspect_delay) {
907 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +0200908 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +0200909 return 1;
910 }
911 curpx->tcp_req.inspect_delay = val;
912 return 0;
913 }
914
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200915 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
916 arg = 1;
917
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200918 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +0200919 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200920 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200921 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +0200922
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200923 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200924 struct acl *acl;
925 const char *name;
926
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200927 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200928 name = acl ? acl->name : "(unknown)";
929
930 retlen = snprintf(err, errlen,
Willy Tarreau1a211942009-07-14 13:53:17 +0200931 "acl '%s' involves some response-only criteria which will be ignored.",
932 name);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200933 warn++;
934 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200935 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200936 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200937 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200938
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200939 if (!(curpx->cap & PR_CAP_FE)) {
940 snprintf(err, errlen, "%s %s is not allowed because %s %s is not a frontend",
941 args[0], args[1], proxy_type_str(curpx), curpx->id);
942 return -1;
943 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200944
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200945 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200946 goto error;
947
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200948 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
949 struct acl *acl;
950 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200951
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200952 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
953 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200954
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200955 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
956 retlen = snprintf(err, errlen,
957 "'%s %s' may not reference acl '%s' which makes use of "
958 "payload in %s '%s'. Please use '%s content' for this.",
959 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
960 goto error;
961 }
962 if (acl->requires & ACL_USE_RTR_ANY)
963 retlen = snprintf(err, errlen,
964 "acl '%s' involves some response-only criteria which will be ignored.",
965 name);
966 warn++;
967 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200968 }
Willy Tarreau1a687942010-05-23 22:40:30 +0200969 else {
970 retlen = snprintf(err, errlen,
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200971 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (was '%s')",
Willy Tarreau1a687942010-05-23 22:40:30 +0200972 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200973 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +0200974 }
975
Willy Tarreau1a687942010-05-23 22:40:30 +0200976 LIST_INIT(&rule->list);
977 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
978 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200979 error:
980 free(rule);
981 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +0200982}
983
Willy Tarreau645513a2010-05-24 20:55:15 +0200984
985/************************************************************************/
986/* All supported ACL keywords must be declared here. */
987/************************************************************************/
988
989/* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */
990static int
991acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
992 struct acl_expr *expr, struct acl_test *test)
993{
994 test->i = l4->cli_addr.ss_family;
995 if (test->i == AF_INET)
996 test->ptr = (void *)&((struct sockaddr_in *)&l4->cli_addr)->sin_addr;
997 else
998 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_addr;
999 test->flags = ACL_TEST_F_READ_ONLY;
1000 return 1;
1001}
1002
1003/* extract the connection's source address */
1004static int
1005pattern_fetch_src(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->cli_addr)->sin_addr.s_addr;
1009 return 1;
1010}
1011
1012
1013/* set test->i to the connection's source port */
1014static int
1015acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir,
1016 struct acl_expr *expr, struct acl_test *test)
1017{
1018 if (l4->cli_addr.ss_family == AF_INET)
1019 test->i = ntohs(((struct sockaddr_in *)&l4->cli_addr)->sin_port);
1020 else
1021 test->i = ntohs(((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_port);
1022 test->flags = 0;
1023 return 1;
1024}
1025
1026
1027/* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */
1028static int
1029acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
1030 struct acl_expr *expr, struct acl_test *test)
1031{
1032 if (!(l4->flags & SN_FRT_ADDR_SET))
1033 get_frt_addr(l4);
1034
1035 test->i = l4->frt_addr.ss_family;
1036 if (test->i == AF_INET)
1037 test->ptr = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr;
1038 else
1039 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_addr;
1040 test->flags = ACL_TEST_F_READ_ONLY;
1041 return 1;
1042}
1043
1044
1045/* extract the connection's destination address */
1046static int
1047pattern_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
1048 const char *arg, int arg_len, union pattern_data *data)
1049{
1050 data->ip.s_addr = ((struct sockaddr_in *)&l4->frt_addr)->sin_addr.s_addr;
1051 return 1;
1052}
1053
1054/* set test->i to the frontend connexion's destination port */
1055static int
1056acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
1057 struct acl_expr *expr, struct acl_test *test)
1058{
1059 if (!(l4->flags & SN_FRT_ADDR_SET))
1060 get_frt_addr(l4);
1061
1062 if (l4->frt_addr.ss_family == AF_INET)
1063 test->i = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port);
1064 else
1065 test->i = ntohs(((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_port);
1066 test->flags = 0;
1067 return 1;
1068}
1069
1070static int
1071pattern_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
1072 const char *arg, int arg_len, union pattern_data *data)
1073
1074{
1075 data->integer = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port);
1076 return 1;
1077}
1078
Willy Tarreaub6866442008-07-14 23:54:42 +02001079static struct cfg_kw_list cfg_kws = {{ },{
1080 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1081 { 0, NULL, NULL },
1082}};
1083
Willy Tarreau645513a2010-05-24 20:55:15 +02001084/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub6866442008-07-14 23:54:42 +02001085static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +02001086 { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT },
1087 { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1088 { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1089 { "dst_port", acl_parse_int, acl_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT },
Willy Tarreaub6866442008-07-14 23:54:42 +02001090 { NULL, NULL, NULL, NULL },
1091}};
1092
Willy Tarreau645513a2010-05-24 20:55:15 +02001093/* Note: must not be declared <const> as its list will be overwritten */
1094static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
1095 { "src", pattern_fetch_src, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
1096 { "dst", pattern_fetch_dst, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
1097 { "dst_port", pattern_fetch_dport, PATTERN_TYPE_INTEGER, PATTERN_FETCH_REQ },
1098 { NULL, NULL, 0, 0 },
1099}};
1100
Willy Tarreaue6b98942007-10-29 01:09:36 +01001101__attribute__((constructor))
1102static void __tcp_protocol_init(void)
1103{
1104 protocol_register(&proto_tcpv4);
1105 protocol_register(&proto_tcpv6);
Willy Tarreau645513a2010-05-24 20:55:15 +02001106 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001107 cfg_register_keywords(&cfg_kws);
1108 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001109}
1110
1111
1112/*
1113 * Local variables:
1114 * c-indent-level: 8
1115 * c-basic-offset: 8
1116 * End:
1117 */