blob: 3757d5631f1ae0a107039fe2674917b37e97b6d4 [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
Emeric Bruncf20bf12010-10-22 16:06:11 +020058static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
59static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010060
61/* Note: must not be declared <const> as its list will be overwritten */
62static struct protocol proto_tcpv4 = {
63 .name = "tcpv4",
64 .sock_domain = AF_INET,
65 .sock_type = SOCK_STREAM,
66 .sock_prot = IPPROTO_TCP,
67 .sock_family = AF_INET,
68 .sock_addrlen = sizeof(struct sockaddr_in),
69 .l3_addrlen = 32/8,
Willy Tarreaueb472682010-05-28 18:46:57 +020070 .accept = &stream_sock_accept,
Willy Tarreaue6b98942007-10-29 01:09:36 +010071 .read = &stream_sock_read,
72 .write = &stream_sock_write,
Emeric Bruncf20bf12010-10-22 16:06:11 +020073 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010074 .bind_all = tcp_bind_listeners,
75 .unbind_all = unbind_all_listeners,
76 .enable_all = enable_all_listeners,
77 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
78 .nb_listeners = 0,
79};
80
81/* Note: must not be declared <const> as its list will be overwritten */
82static struct protocol proto_tcpv6 = {
83 .name = "tcpv6",
84 .sock_domain = AF_INET6,
85 .sock_type = SOCK_STREAM,
86 .sock_prot = IPPROTO_TCP,
87 .sock_family = AF_INET6,
88 .sock_addrlen = sizeof(struct sockaddr_in6),
89 .l3_addrlen = 128/8,
Willy Tarreaueb472682010-05-28 18:46:57 +020090 .accept = &stream_sock_accept,
Willy Tarreaue6b98942007-10-29 01:09:36 +010091 .read = &stream_sock_read,
92 .write = &stream_sock_write,
Emeric Bruncf20bf12010-10-22 16:06:11 +020093 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010094 .bind_all = tcp_bind_listeners,
95 .unbind_all = unbind_all_listeners,
96 .enable_all = enable_all_listeners,
97 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
98 .nb_listeners = 0,
99};
100
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100101
102/* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which
103 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
104 * - 0 : ignore remote address (may even be a NULL pointer)
105 * - 1 : use provided address
106 * - 2 : use provided port
107 * - 3 : use both
108 *
109 * The function supports multiple foreign binding methods :
110 * - linux_tproxy: we directly bind to the foreign address
111 * - cttproxy: we bind to a local address then nat.
112 * The second one can be used as a fallback for the first one.
113 * This function returns 0 when everything's OK, 1 if it could not bind, to the
114 * local address, 2 if it could not bind to the foreign address.
115 */
116int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote)
117{
118 struct sockaddr_in bind_addr;
119 int foreign_ok = 0;
120 int ret;
121
122#ifdef CONFIG_HAP_LINUX_TPROXY
123 static int ip_transp_working = 1;
124 if (flags && ip_transp_working) {
125 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0
126 || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0)
127 foreign_ok = 1;
128 else
129 ip_transp_working = 0;
130 }
131#endif
132 if (flags) {
133 memset(&bind_addr, 0, sizeof(bind_addr));
134 if (flags & 1)
135 bind_addr.sin_addr = remote->sin_addr;
136 if (flags & 2)
137 bind_addr.sin_port = remote->sin_port;
138 }
139
140 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
141 if (foreign_ok) {
142 ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
143 if (ret < 0)
144 return 2;
145 }
146 else {
147 ret = bind(fd, (struct sockaddr *)local, sizeof(*local));
148 if (ret < 0)
149 return 1;
150 }
151
152 if (!flags)
153 return 0;
154
155#ifdef CONFIG_HAP_CTTPROXY
156 if (!foreign_ok) {
157 struct in_tproxy itp1, itp2;
158 memset(&itp1, 0, sizeof(itp1));
159
160 itp1.op = TPROXY_ASSIGN;
161 itp1.v.addr.faddr = bind_addr.sin_addr;
162 itp1.v.addr.fport = bind_addr.sin_port;
163
164 /* set connect flag on socket */
165 itp2.op = TPROXY_FLAGS;
166 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
167
168 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
169 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
170 foreign_ok = 1;
171 }
172 }
173#endif
174 if (!foreign_ok)
175 /* we could not bind to a foreign address */
176 return 2;
177
178 return 0;
179}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100180
Willy Tarreau9650f372009-08-16 14:02:45 +0200181
182/*
183 * This function initiates a connection to the server assigned to this session
Willy Tarreaub1d67742010-03-29 19:36:59 +0200184 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. A
185 * source address may be pointed to by <from_addr>. Note that this is only used
186 * in case of transparent proxying. Normal source bind addresses are still
187 * determined locally (due to the possible need of a source port).
188 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200189 * It can return one of :
190 * - SN_ERR_NONE if everything's OK
191 * - SN_ERR_SRVTO if there are no more servers
192 * - SN_ERR_SRVCL if the connection was refused by the server
193 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
194 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
195 * - SN_ERR_INTERNAL for any other purely internal errors
196 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
197 */
198int tcpv4_connect_server(struct stream_interface *si,
199 struct proxy *be, struct server *srv,
Willy Tarreaub1d67742010-03-29 19:36:59 +0200200 struct sockaddr *srv_addr, struct sockaddr *from_addr)
Willy Tarreau9650f372009-08-16 14:02:45 +0200201{
202 int fd;
203
204 if ((fd = si->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
205 qfprintf(stderr, "Cannot get a server socket.\n");
206
207 if (errno == ENFILE)
208 send_log(be, LOG_EMERG,
209 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
210 be->id, maxfd);
211 else if (errno == EMFILE)
212 send_log(be, LOG_EMERG,
213 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
214 be->id, maxfd);
215 else if (errno == ENOBUFS || errno == ENOMEM)
216 send_log(be, LOG_EMERG,
217 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
218 be->id, maxfd);
219 /* this is a resource error */
220 return SN_ERR_RESOURCE;
221 }
222
223 if (fd >= global.maxsock) {
224 /* do not log anything there, it's a normal condition when this option
225 * is used to serialize connections to a server !
226 */
227 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
228 close(fd);
229 return SN_ERR_PRXCOND; /* it is a configuration limit */
230 }
231
232 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
233 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
234 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
235 close(fd);
236 return SN_ERR_INTERNAL;
237 }
238
239 if (be->options & PR_O_TCP_SRV_KA)
240 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
241
242 if (be->options & PR_O_TCP_NOLING)
243 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
244
245 /* allow specific binding :
246 * - server-specific at first
247 * - proxy-specific next
248 */
249 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200250 int ret, flags = 0;
251
Willy Tarreau9650f372009-08-16 14:02:45 +0200252 switch (srv->state & SRV_TPROXY_MASK) {
253 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200254 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200255 flags = 3;
256 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200257 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200258 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200259 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200260 break;
261 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200262
Willy Tarreau9650f372009-08-16 14:02:45 +0200263#ifdef SO_BINDTODEVICE
264 /* Note: this might fail if not CAP_NET_RAW */
265 if (srv->iface_name)
266 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
267#endif
268
269 if (srv->sport_range) {
270 int attempts = 10; /* should be more than enough to find a spare port */
271 struct sockaddr_in src;
272
273 ret = 1;
274 src = srv->source_addr;
275
276 do {
277 /* note: in case of retry, we may have to release a previously
278 * allocated port, hence this loop's construct.
279 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200280 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
281 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200282
283 if (!attempts)
284 break;
285 attempts--;
286
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200287 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
288 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200289 break;
290
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200291 fdinfo[fd].port_range = srv->sport_range;
292 src.sin_port = htons(fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200293
Willy Tarreaub1d67742010-03-29 19:36:59 +0200294 ret = tcpv4_bind_socket(fd, flags, &src, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200295 } while (ret != 0); /* binding NOK */
296 }
297 else {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200298 ret = tcpv4_bind_socket(fd, flags, &srv->source_addr, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200299 }
300
301 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200302 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
303 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200304 close(fd);
305
306 if (ret == 1) {
307 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
308 be->id, srv->id);
309 send_log(be, LOG_EMERG,
310 "Cannot bind to source address before connect() for server %s/%s.\n",
311 be->id, srv->id);
312 } else {
313 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
314 be->id, srv->id);
315 send_log(be, LOG_EMERG,
316 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
317 be->id, srv->id);
318 }
319 return SN_ERR_RESOURCE;
320 }
321 }
322 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200323 int ret, flags = 0;
324
Willy Tarreau9650f372009-08-16 14:02:45 +0200325 switch (be->options & PR_O_TPXY_MASK) {
326 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200327 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200328 flags = 3;
329 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200330 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200331 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200332 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200333 break;
334 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200335
Willy Tarreau9650f372009-08-16 14:02:45 +0200336#ifdef SO_BINDTODEVICE
337 /* Note: this might fail if not CAP_NET_RAW */
338 if (be->iface_name)
339 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
340#endif
Willy Tarreaub1d67742010-03-29 19:36:59 +0200341 ret = tcpv4_bind_socket(fd, flags, &be->source_addr, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200342 if (ret) {
343 close(fd);
344 if (ret == 1) {
345 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
346 be->id);
347 send_log(be, LOG_EMERG,
348 "Cannot bind to source address before connect() for proxy %s.\n",
349 be->id);
350 } else {
351 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
352 be->id);
353 send_log(be, LOG_EMERG,
354 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
355 be->id);
356 }
357 return SN_ERR_RESOURCE;
358 }
359 }
360
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400361#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200362 /* disabling tcp quick ack now allows the first request to leave the
363 * machine with the first ACK. We only do this if there are pending
364 * data in the buffer.
365 */
366 if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400367 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200368#endif
369
Willy Tarreaue803de22010-01-21 17:43:04 +0100370 if (global.tune.server_sndbuf)
371 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
372
373 if (global.tune.server_rcvbuf)
374 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
375
Willy Tarreau9650f372009-08-16 14:02:45 +0200376 if ((connect(fd, (struct sockaddr *)srv_addr, sizeof(struct sockaddr_in)) == -1) &&
377 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
378
379 if (errno == EAGAIN || errno == EADDRINUSE) {
380 char *msg;
381 if (errno == EAGAIN) /* no free ports left, try again later */
382 msg = "no free ports";
383 else
384 msg = "local address already in use";
385
386 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200387 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
388 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200389 close(fd);
390 send_log(be, LOG_EMERG,
391 "Connect() failed for server %s/%s: %s.\n",
392 be->id, srv->id, msg);
393 return SN_ERR_RESOURCE;
394 } else if (errno == ETIMEDOUT) {
395 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200396 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
397 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200398 close(fd);
399 return SN_ERR_SRVTO;
400 } else {
401 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
402 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200403 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
404 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200405 close(fd);
406 return SN_ERR_SRVCL;
407 }
408 }
409
410 fdtab[fd].owner = si;
411 fdtab[fd].state = FD_STCONN; /* connection in progress */
412 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
413 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
414 fdtab[fd].cb[DIR_RD].b = si->ib;
415 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
416 fdtab[fd].cb[DIR_WR].b = si->ob;
417
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200418 fdinfo[fd].peeraddr = (struct sockaddr *)srv_addr;
419 fdinfo[fd].peerlen = sizeof(struct sockaddr_in);
Willy Tarreau9650f372009-08-16 14:02:45 +0200420
421 fd_insert(fd);
422 EV_FD_SET(fd, DIR_WR); /* for connect status */
423
424 si->state = SI_ST_CON;
425 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
426 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
427
428 return SN_ERR_NONE; /* connection is OK */
429}
430
431
Willy Tarreaue6b98942007-10-29 01:09:36 +0100432/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
433 * an error message in <err> if the message is at most <errlen> bytes long
434 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
435 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
436 * was alright and that no message was returned. ERR_RETRYABLE means that an
437 * error occurred but that it may vanish after a retry (eg: port in use), and
438 * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter
439 * the meaning of the error, but just indicate that a message is present which
440 * should be displayed with the respective level. Last, ERR_ABORT indicates
441 * that it's pointless to try to start other listeners. No error message is
442 * returned if errlen is NULL.
443 */
444int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
445{
446 __label__ tcp_return, tcp_close_return;
447 int fd, err;
448 const char *msg = NULL;
449
450 /* ensure we never return garbage */
451 if (errmsg && errlen)
452 *errmsg = 0;
453
454 if (listener->state != LI_ASSIGNED)
455 return ERR_NONE; /* already bound */
456
457 err = ERR_NONE;
458
459 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
460 err |= ERR_RETRYABLE | ERR_ALERT;
461 msg = "cannot create listening socket";
462 goto tcp_return;
463 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100464
Willy Tarreaue6b98942007-10-29 01:09:36 +0100465 if (fd >= global.maxsock) {
466 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
467 msg = "not enough free sockets (raise '-n' parameter)";
468 goto tcp_close_return;
469 }
470
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200471 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100472 err |= ERR_FATAL | ERR_ALERT;
473 msg = "cannot make socket non-blocking";
474 goto tcp_close_return;
475 }
476
477 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
478 /* not fatal but should be reported */
479 msg = "cannot do so_reuseaddr";
480 err |= ERR_ALERT;
481 }
482
483 if (listener->options & LI_O_NOLINGER)
484 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100485
Willy Tarreaue6b98942007-10-29 01:09:36 +0100486#ifdef SO_REUSEPORT
487 /* OpenBSD supports this. As it's present in old libc versions of Linux,
488 * it might return an error that we will silently ignore.
489 */
490 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
491#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100492#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100493 if ((listener->options & LI_O_FOREIGN)
Willy Tarreau0a459892008-01-13 17:37:16 +0100494 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1)
495 && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100496 msg = "cannot make listening socket transparent";
497 err |= ERR_ALERT;
498 }
499#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100500#ifdef SO_BINDTODEVICE
501 /* Note: this might fail if not CAP_NET_RAW */
502 if (listener->interface) {
503 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100504 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100505 msg = "cannot bind listener to device";
506 err |= ERR_WARN;
507 }
508 }
509#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400510#if defined(TCP_MAXSEG)
Willy Tarreaube1b9182009-06-14 18:48:19 +0200511 if (listener->maxseg) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400512 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200513 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
514 msg = "cannot set MSS";
515 err |= ERR_WARN;
516 }
517 }
518#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200519#if defined(TCP_DEFER_ACCEPT)
520 if (listener->options & LI_O_DEF_ACCEPT) {
521 /* defer accept by up to one second */
522 int accept_delay = 1;
523 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
524 msg = "cannot enable DEFER_ACCEPT";
525 err |= ERR_WARN;
526 }
527 }
528#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100529 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
530 err |= ERR_RETRYABLE | ERR_ALERT;
531 msg = "cannot bind socket";
532 goto tcp_close_return;
533 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100534
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100535 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100536 err |= ERR_RETRYABLE | ERR_ALERT;
537 msg = "cannot listen to socket";
538 goto tcp_close_return;
539 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100540
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400541#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200542 if (listener->options & LI_O_NOQUICKACK)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400543 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200544#endif
545
Willy Tarreaue6b98942007-10-29 01:09:36 +0100546 /* the socket is ready */
547 listener->fd = fd;
548 listener->state = LI_LISTEN;
549
Willy Tarreaueabf3132008-08-29 23:36:51 +0200550 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100551 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaueb472682010-05-28 18:46:57 +0200552 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
553 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
554 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
555 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200556
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200557 fdinfo[fd].peeraddr = NULL;
558 fdinfo[fd].peerlen = 0;
Willy Tarreaueb472682010-05-28 18:46:57 +0200559 fd_insert(fd);
560
Willy Tarreaue6b98942007-10-29 01:09:36 +0100561 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100562 if (msg && errlen) {
563 char pn[INET6_ADDRSTRLEN];
564
565 if (listener->addr.ss_family == AF_INET) {
566 inet_ntop(AF_INET,
567 (const void *)&((struct sockaddr_in *)&listener->addr)->sin_addr,
568 pn, sizeof(pn));
569 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, ntohs(((struct sockaddr_in *)&listener->addr)->sin_port));
570 }
571 else {
572 inet_ntop(AF_INET6,
573 (const void *)&((struct sockaddr_in6 *)(&listener->addr))->sin6_addr,
574 pn, sizeof(pn));
575 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, ntohs(((struct sockaddr_in6 *)&listener->addr)->sin6_port));
576 }
577 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100578 return err;
579
580 tcp_close_return:
581 close(fd);
582 goto tcp_return;
583}
584
585/* This function creates all TCP sockets bound to the protocol entry <proto>.
586 * It is intended to be used as the protocol's bind_all() function.
587 * The sockets will be registered but not added to any fd_set, in order not to
588 * loose them across the fork(). A call to enable_all_listeners() is needed
589 * to complete initialization. The return value is composed from ERR_*.
590 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200591static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100592{
593 struct listener *listener;
594 int err = ERR_NONE;
595
596 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200597 err |= tcp_bind_listener(listener, errmsg, errlen);
598 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100599 break;
600 }
601
602 return err;
603}
604
605/* Add listener to the list of tcpv4 listeners. The listener's state
606 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
607 * listeners is updated. This is the function to use to add a new listener.
608 */
609void tcpv4_add_listener(struct listener *listener)
610{
611 if (listener->state != LI_INIT)
612 return;
613 listener->state = LI_ASSIGNED;
614 listener->proto = &proto_tcpv4;
615 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
616 proto_tcpv4.nb_listeners++;
617}
618
619/* Add listener to the list of tcpv4 listeners. The listener's state
620 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
621 * listeners is updated. This is the function to use to add a new listener.
622 */
623void tcpv6_add_listener(struct listener *listener)
624{
625 if (listener->state != LI_INIT)
626 return;
627 listener->state = LI_ASSIGNED;
628 listener->proto = &proto_tcpv6;
629 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
630 proto_tcpv6.nb_listeners++;
631}
632
Willy Tarreauedcf6682008-11-30 23:15:34 +0100633/* This function performs the TCP request analysis on the current request. It
634 * returns 1 if the processing can continue on next analysers, or zero if it
635 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200636 * request. It relies on buffers flags, and updates s->req->analysers. The
637 * function may be called for frontend rules and backend rules. It only relies
638 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100639 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200640int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100641{
642 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200643 struct stksess *ts;
644 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100645 int partial;
646
647 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
648 now_ms, __FUNCTION__,
649 s,
650 req,
651 req->rex, req->wex,
652 req->flags,
653 req->l,
654 req->analysers);
655
Willy Tarreauedcf6682008-11-30 23:15:34 +0100656 /* We don't know whether we have enough data, so must proceed
657 * this way :
658 * - iterate through all rules in their declaration order
659 * - if one rule returns MISS, it means the inspect delay is
660 * not over yet, then return immediately, otherwise consider
661 * it as a non-match.
662 * - if one rule returns OK, then return OK
663 * - if one rule returns KO, then return KO
664 */
665
Willy Tarreaub824b002010-09-29 16:36:16 +0200666 if (req->flags & (BF_SHUTR|BF_FULL) || !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100667 partial = 0;
668 else
669 partial = ACL_PARTIAL;
670
Willy Tarreaufb356202010-08-03 14:02:05 +0200671 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100672 int ret = ACL_PAT_PASS;
673
674 if (rule->cond) {
Willy Tarreaufb356202010-08-03 14:02:05 +0200675 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100676 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200677 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100678 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200679 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
680 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100681 return 0;
682 }
683
684 ret = acl_pass(ret);
685 if (rule->cond->pol == ACL_COND_UNLESS)
686 ret = !ret;
687 }
688
689 if (ret) {
690 /* we have a matching rule. */
691 if (rule->action == TCP_ACT_REJECT) {
692 buffer_abort(req);
693 buffer_abort(s->rep);
694 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200695
Willy Tarreaufb356202010-08-03 14:02:05 +0200696 s->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200697 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200698 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200699
Willy Tarreauedcf6682008-11-30 23:15:34 +0100700 if (!(s->flags & SN_ERR_MASK))
701 s->flags |= SN_ERR_PRXCOND;
702 if (!(s->flags & SN_FINST_MASK))
703 s->flags |= SN_FINST_R;
704 return 0;
705 }
Willy Tarreau56123282010-08-06 19:06:56 +0200706 else if (rule->action == TCP_ACT_TRK_SC1) {
707 if (!s->stkctr1_entry) {
708 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200709 * Also, note that right now we can only track SRC so we
710 * don't check how to get the key, but later we may need
711 * to consider rule->act_prm->trk_ctr.type.
712 */
713 t = rule->act_prm.trk_ctr.table.t;
714 ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200715 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200716 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200717 if (s->fe != s->be)
718 s->flags |= SN_BE_TRACK_SC1;
719 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200720 }
721 }
Willy Tarreau56123282010-08-06 19:06:56 +0200722 else if (rule->action == TCP_ACT_TRK_SC2) {
723 if (!s->stkctr2_entry) {
724 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200725 * Also, note that right now we can only track SRC so we
726 * don't check how to get the key, but later we may need
727 * to consider rule->act_prm->trk_ctr.type.
728 */
729 t = rule->act_prm.trk_ctr.table.t;
730 ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200731 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200732 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200733 if (s->fe != s->be)
734 s->flags |= SN_BE_TRACK_SC2;
735 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200736 }
737 }
738 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100739 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200740 break;
741 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100742 }
743 }
744
745 /* if we get there, it means we have no rule which matches, or
746 * we have an explicit accept, so we apply the default accept.
747 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200748 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100749 req->analyse_exp = TICK_ETERNITY;
750 return 1;
751}
752
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200753/* This function performs the TCP layer4 analysis on the current request. It
754 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
755 * matches or if no more rule matches. It can only use rules which don't need
756 * any data.
757 */
758int tcp_exec_req_rules(struct session *s)
759{
760 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200761 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200762 struct stktable *t = NULL;
763 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200764 int ret;
765
766 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
767 ret = ACL_PAT_PASS;
768
769 if (rule->cond) {
770 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ);
771 ret = acl_pass(ret);
772 if (rule->cond->pol == ACL_COND_UNLESS)
773 ret = !ret;
774 }
775
776 if (ret) {
777 /* we have a matching rule. */
778 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau2799e982010-06-05 15:43:21 +0200779 s->fe->counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200780 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200781 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200782
783 if (!(s->flags & SN_ERR_MASK))
784 s->flags |= SN_ERR_PRXCOND;
785 if (!(s->flags & SN_FINST_MASK))
786 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200787 result = 0;
788 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200789 }
Willy Tarreau56123282010-08-06 19:06:56 +0200790 else if (rule->action == TCP_ACT_TRK_SC1) {
791 if (!s->stkctr1_entry) {
792 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200793 * Also, note that right now we can only track SRC so we
794 * don't check how to get the key, but later we may need
795 * to consider rule->act_prm->trk_ctr.type.
796 */
797 t = rule->act_prm.trk_ctr.table.t;
798 ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s));
799 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200800 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200801 }
802 }
Willy Tarreau56123282010-08-06 19:06:56 +0200803 else if (rule->action == TCP_ACT_TRK_SC2) {
804 if (!s->stkctr2_entry) {
805 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200806 * Also, note that right now we can only track SRC so we
807 * don't check how to get the key, but later we may need
808 * to consider rule->act_prm->trk_ctr.type.
809 */
810 t = rule->act_prm.trk_ctr.table.t;
811 ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s));
812 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +0200813 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200814 }
815 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200816 else {
817 /* otherwise it's an accept */
818 break;
819 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200820 }
821 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200822 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200823}
824
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200825/* Parse a tcp-request rule. Return a negative value in case of failure */
826static int tcp_parse_request_rule(char **args, int arg, int section_type,
827 struct proxy *curpx, struct proxy *defpx,
828 struct tcp_rule *rule, char *err, int errlen)
829{
830 if (curpx == defpx) {
831 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
832 args[0], args[1]);
833 return -1;
834 }
835
836 if (!strcmp(args[arg], "accept")) {
837 arg++;
838 rule->action = TCP_ACT_ACCEPT;
839 }
840 else if (!strcmp(args[arg], "reject")) {
841 arg++;
842 rule->action = TCP_ACT_REJECT;
843 }
Willy Tarreau56123282010-08-06 19:06:56 +0200844 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200845 int ret;
846
847 arg++;
848 ret = parse_track_counters(args, &arg, section_type, curpx,
849 &rule->act_prm.trk_ctr, defpx, err, errlen);
850
851 if (ret < 0) /* nb: warnings are not handled yet */
852 return -1;
853
Willy Tarreau56123282010-08-06 19:06:56 +0200854 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200855 }
Willy Tarreau56123282010-08-06 19:06:56 +0200856 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200857 int ret;
858
859 arg++;
860 ret = parse_track_counters(args, &arg, section_type, curpx,
861 &rule->act_prm.trk_ctr, defpx, err, errlen);
862
863 if (ret < 0) /* nb: warnings are not handled yet */
864 return -1;
865
Willy Tarreau56123282010-08-06 19:06:56 +0200866 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200867 }
868 else {
869 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +0200870 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
871 "or 'track-sc2' in %s '%s' (was '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200872 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
873 return -1;
874 }
875
876 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
877 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) {
878 snprintf(err, errlen,
879 "error detected in %s '%s' while parsing '%s' condition",
880 proxy_type_str(curpx), curpx->id, args[arg]);
881 return -1;
882 }
883 }
884 else if (*args[arg]) {
885 snprintf(err, errlen,
886 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
887 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
888 return -1;
889 }
890 return 0;
891}
892
Willy Tarreaub6866442008-07-14 23:54:42 +0200893/* This function should be called to parse a line starting with the "tcp-request"
894 * keyword.
895 */
896static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
897 struct proxy *defpx, char *err, int errlen)
898{
899 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200900 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +0200901 int retlen;
Willy Tarreau1a687942010-05-23 22:40:30 +0200902 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200903 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +0200904 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +0200905
906 if (!*args[1]) {
907 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +0200908 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +0200909 return -1;
910 }
911
912 if (!strcmp(args[1], "inspect-delay")) {
913 if (curpx == defpx) {
914 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
915 args[0], args[1]);
916 return -1;
917 }
918
Willy Tarreaub6866442008-07-14 23:54:42 +0200919 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
920 retlen = snprintf(err, errlen,
921 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +0200922 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +0200923 if (ptr && retlen < errlen)
924 retlen += snprintf(err+retlen, errlen - retlen,
925 " (unexpected character '%c')", *ptr);
926 return -1;
927 }
928
929 if (curpx->tcp_req.inspect_delay) {
930 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
Willy Tarreauf5356832010-06-14 18:40:26 +0200931 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +0200932 return 1;
933 }
934 curpx->tcp_req.inspect_delay = val;
935 return 0;
936 }
937
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200938 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +0200939 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200940 arg = 1;
941
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200942 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +0200943 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200944 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200945 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +0200946
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200947 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200948 struct acl *acl;
949 const char *name;
950
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200951 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200952 name = acl ? acl->name : "(unknown)";
953
954 retlen = snprintf(err, errlen,
Willy Tarreau1a211942009-07-14 13:53:17 +0200955 "acl '%s' involves some response-only criteria which will be ignored.",
956 name);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200957 warn++;
958 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +0200959 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200960 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200961 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200962 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200963
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200964 if (!(curpx->cap & PR_CAP_FE)) {
965 snprintf(err, errlen, "%s %s is not allowed because %s %s is not a frontend",
966 args[0], args[1], proxy_type_str(curpx), curpx->id);
967 return -1;
968 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200969
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200970 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200971 goto error;
972
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200973 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
974 struct acl *acl;
975 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200976
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200977 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
978 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200979
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200980 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
981 retlen = snprintf(err, errlen,
982 "'%s %s' may not reference acl '%s' which makes use of "
983 "payload in %s '%s'. Please use '%s content' for this.",
984 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
985 goto error;
986 }
987 if (acl->requires & ACL_USE_RTR_ANY)
988 retlen = snprintf(err, errlen,
989 "acl '%s' involves some response-only criteria which will be ignored.",
990 name);
991 warn++;
992 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +0200993 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200994 }
Willy Tarreau1a687942010-05-23 22:40:30 +0200995 else {
996 retlen = snprintf(err, errlen,
Willy Tarreau68c03ab2010-08-06 15:08:45 +0200997 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (was '%s')",
Willy Tarreau1a687942010-05-23 22:40:30 +0200998 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +0200999 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001000 }
1001
Willy Tarreau1a687942010-05-23 22:40:30 +02001002 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001003 error:
1004 free(rule);
1005 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001006}
1007
Willy Tarreau645513a2010-05-24 20:55:15 +02001008
1009/************************************************************************/
1010/* All supported ACL keywords must be declared here. */
1011/************************************************************************/
1012
1013/* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */
1014static int
1015acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
1016 struct acl_expr *expr, struct acl_test *test)
1017{
1018 test->i = l4->cli_addr.ss_family;
1019 if (test->i == AF_INET)
1020 test->ptr = (void *)&((struct sockaddr_in *)&l4->cli_addr)->sin_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001021 else if (test->i == AF_INET6)
Willy Tarreau645513a2010-05-24 20:55:15 +02001022 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001023 else
1024 return 0;
1025
Willy Tarreau645513a2010-05-24 20:55:15 +02001026 test->flags = ACL_TEST_F_READ_ONLY;
1027 return 1;
1028}
1029
1030/* extract the connection's source address */
1031static int
1032pattern_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
1033 const char *arg, int arg_len, union pattern_data *data)
1034{
Emeric Brunf769f512010-10-22 17:14:01 +02001035 if (l4->cli_addr.ss_family != AF_INET )
1036 return 0;
1037
Willy Tarreau645513a2010-05-24 20:55:15 +02001038 data->ip.s_addr = ((struct sockaddr_in *)&l4->cli_addr)->sin_addr.s_addr;
1039 return 1;
1040}
1041
1042
1043/* set test->i to the connection's source port */
1044static int
1045acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir,
1046 struct acl_expr *expr, struct acl_test *test)
1047{
1048 if (l4->cli_addr.ss_family == AF_INET)
1049 test->i = ntohs(((struct sockaddr_in *)&l4->cli_addr)->sin_port);
Emeric Brunf769f512010-10-22 17:14:01 +02001050 else if (l4->cli_addr.ss_family == AF_INET6)
Willy Tarreau645513a2010-05-24 20:55:15 +02001051 test->i = ntohs(((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_port);
Emeric Brunf769f512010-10-22 17:14:01 +02001052 else
1053 return 0;
1054
Willy Tarreau645513a2010-05-24 20:55:15 +02001055 test->flags = 0;
1056 return 1;
1057}
1058
1059
1060/* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */
1061static int
1062acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
1063 struct acl_expr *expr, struct acl_test *test)
1064{
1065 if (!(l4->flags & SN_FRT_ADDR_SET))
1066 get_frt_addr(l4);
1067
1068 test->i = l4->frt_addr.ss_family;
1069 if (test->i == AF_INET)
1070 test->ptr = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001071 else if (test->i == AF_INET6)
Willy Tarreau645513a2010-05-24 20:55:15 +02001072 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_addr;
Emeric Brunf769f512010-10-22 17:14:01 +02001073 else
1074 return 0;
1075
Willy Tarreau645513a2010-05-24 20:55:15 +02001076 test->flags = ACL_TEST_F_READ_ONLY;
1077 return 1;
1078}
1079
1080
1081/* extract the connection's destination address */
1082static int
1083pattern_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
1084 const char *arg, int arg_len, union pattern_data *data)
1085{
emeric8aa6b372010-10-22 17:06:26 +02001086 if (!(l4->flags & SN_FRT_ADDR_SET))
1087 get_frt_addr(l4);
1088
Emeric Brunf769f512010-10-22 17:14:01 +02001089 if (l4->frt_addr.ss_family != AF_INET)
1090 return 0;
1091
Willy Tarreau645513a2010-05-24 20:55:15 +02001092 data->ip.s_addr = ((struct sockaddr_in *)&l4->frt_addr)->sin_addr.s_addr;
1093 return 1;
1094}
1095
1096/* set test->i to the frontend connexion's destination port */
1097static int
1098acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
1099 struct acl_expr *expr, struct acl_test *test)
1100{
1101 if (!(l4->flags & SN_FRT_ADDR_SET))
1102 get_frt_addr(l4);
1103
1104 if (l4->frt_addr.ss_family == AF_INET)
1105 test->i = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port);
Emeric Brunf769f512010-10-22 17:14:01 +02001106 else if (l4->frt_addr.ss_family == AF_INET6)
Willy Tarreau645513a2010-05-24 20:55:15 +02001107 test->i = ntohs(((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_port);
Emeric Brunf769f512010-10-22 17:14:01 +02001108 else
1109 return 0;
1110
Willy Tarreau645513a2010-05-24 20:55:15 +02001111 test->flags = 0;
1112 return 1;
1113}
1114
1115static int
1116pattern_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
1117 const char *arg, int arg_len, union pattern_data *data)
1118
1119{
emeric8aa6b372010-10-22 17:06:26 +02001120 if (!(l4->flags & SN_FRT_ADDR_SET))
1121 get_frt_addr(l4);
1122
Emeric Brunf769f512010-10-22 17:14:01 +02001123 if (l4->frt_addr.ss_family != AF_INET)
1124 return 0;
1125
Willy Tarreau645513a2010-05-24 20:55:15 +02001126 data->integer = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port);
1127 return 1;
1128}
1129
Willy Tarreaub6866442008-07-14 23:54:42 +02001130static struct cfg_kw_list cfg_kws = {{ },{
1131 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1132 { 0, NULL, NULL },
1133}};
1134
Willy Tarreau645513a2010-05-24 20:55:15 +02001135/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub6866442008-07-14 23:54:42 +02001136static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +02001137 { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT },
1138 { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1139 { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1140 { "dst_port", acl_parse_int, acl_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT },
Willy Tarreaub6866442008-07-14 23:54:42 +02001141 { NULL, NULL, NULL, NULL },
1142}};
1143
Willy Tarreau645513a2010-05-24 20:55:15 +02001144/* Note: must not be declared <const> as its list will be overwritten */
1145static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
1146 { "src", pattern_fetch_src, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
1147 { "dst", pattern_fetch_dst, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
1148 { "dst_port", pattern_fetch_dport, PATTERN_TYPE_INTEGER, PATTERN_FETCH_REQ },
1149 { NULL, NULL, 0, 0 },
1150}};
1151
Willy Tarreaue6b98942007-10-29 01:09:36 +01001152__attribute__((constructor))
1153static void __tcp_protocol_init(void)
1154{
1155 protocol_register(&proto_tcpv4);
1156 protocol_register(&proto_tcpv6);
Willy Tarreau645513a2010-05-24 20:55:15 +02001157 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001158 cfg_register_keywords(&cfg_kws);
1159 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001160}
1161
1162
1163/*
1164 * Local variables:
1165 * c-indent-level: 8
1166 * c-basic-offset: 8
1167 * End:
1168 */