blob: 1c93396089f2fc0081b1ee0052b1b48b09ee8cd8 [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 Tarreaue6b98942007-10-29 01:09:36 +010049#include <proto/stream_sock.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010050
Willy Tarreaue8c66af2008-01-13 18:40:14 +010051#ifdef CONFIG_HAP_CTTPROXY
52#include <import/ip_tproxy.h>
53#endif
54
Willy Tarreaue6b98942007-10-29 01:09:36 +010055static int tcp_bind_listeners(struct protocol *proto);
56
57/* Note: must not be declared <const> as its list will be overwritten */
58static struct protocol proto_tcpv4 = {
59 .name = "tcpv4",
60 .sock_domain = AF_INET,
61 .sock_type = SOCK_STREAM,
62 .sock_prot = IPPROTO_TCP,
63 .sock_family = AF_INET,
64 .sock_addrlen = sizeof(struct sockaddr_in),
65 .l3_addrlen = 32/8,
Willy Tarreaueb472682010-05-28 18:46:57 +020066 .accept = &stream_sock_accept,
Willy Tarreaue6b98942007-10-29 01:09:36 +010067 .read = &stream_sock_read,
68 .write = &stream_sock_write,
69 .bind_all = tcp_bind_listeners,
70 .unbind_all = unbind_all_listeners,
71 .enable_all = enable_all_listeners,
72 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
73 .nb_listeners = 0,
74};
75
76/* Note: must not be declared <const> as its list will be overwritten */
77static struct protocol proto_tcpv6 = {
78 .name = "tcpv6",
79 .sock_domain = AF_INET6,
80 .sock_type = SOCK_STREAM,
81 .sock_prot = IPPROTO_TCP,
82 .sock_family = AF_INET6,
83 .sock_addrlen = sizeof(struct sockaddr_in6),
84 .l3_addrlen = 128/8,
Willy Tarreaueb472682010-05-28 18:46:57 +020085 .accept = &stream_sock_accept,
Willy Tarreaue6b98942007-10-29 01:09:36 +010086 .read = &stream_sock_read,
87 .write = &stream_sock_write,
88 .bind_all = tcp_bind_listeners,
89 .unbind_all = unbind_all_listeners,
90 .enable_all = enable_all_listeners,
91 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
92 .nb_listeners = 0,
93};
94
Willy Tarreaue8c66af2008-01-13 18:40:14 +010095
96/* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which
97 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
98 * - 0 : ignore remote address (may even be a NULL pointer)
99 * - 1 : use provided address
100 * - 2 : use provided port
101 * - 3 : use both
102 *
103 * The function supports multiple foreign binding methods :
104 * - linux_tproxy: we directly bind to the foreign address
105 * - cttproxy: we bind to a local address then nat.
106 * The second one can be used as a fallback for the first one.
107 * This function returns 0 when everything's OK, 1 if it could not bind, to the
108 * local address, 2 if it could not bind to the foreign address.
109 */
110int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote)
111{
112 struct sockaddr_in bind_addr;
113 int foreign_ok = 0;
114 int ret;
115
116#ifdef CONFIG_HAP_LINUX_TPROXY
117 static int ip_transp_working = 1;
118 if (flags && ip_transp_working) {
119 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0
120 || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0)
121 foreign_ok = 1;
122 else
123 ip_transp_working = 0;
124 }
125#endif
126 if (flags) {
127 memset(&bind_addr, 0, sizeof(bind_addr));
128 if (flags & 1)
129 bind_addr.sin_addr = remote->sin_addr;
130 if (flags & 2)
131 bind_addr.sin_port = remote->sin_port;
132 }
133
134 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
135 if (foreign_ok) {
136 ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
137 if (ret < 0)
138 return 2;
139 }
140 else {
141 ret = bind(fd, (struct sockaddr *)local, sizeof(*local));
142 if (ret < 0)
143 return 1;
144 }
145
146 if (!flags)
147 return 0;
148
149#ifdef CONFIG_HAP_CTTPROXY
150 if (!foreign_ok) {
151 struct in_tproxy itp1, itp2;
152 memset(&itp1, 0, sizeof(itp1));
153
154 itp1.op = TPROXY_ASSIGN;
155 itp1.v.addr.faddr = bind_addr.sin_addr;
156 itp1.v.addr.fport = bind_addr.sin_port;
157
158 /* set connect flag on socket */
159 itp2.op = TPROXY_FLAGS;
160 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
161
162 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
163 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
164 foreign_ok = 1;
165 }
166 }
167#endif
168 if (!foreign_ok)
169 /* we could not bind to a foreign address */
170 return 2;
171
172 return 0;
173}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100174
Willy Tarreau9650f372009-08-16 14:02:45 +0200175
176/*
177 * This function initiates a connection to the server assigned to this session
Willy Tarreaub1d67742010-03-29 19:36:59 +0200178 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. A
179 * source address may be pointed to by <from_addr>. Note that this is only used
180 * in case of transparent proxying. Normal source bind addresses are still
181 * determined locally (due to the possible need of a source port).
182 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200183 * It can return one of :
184 * - SN_ERR_NONE if everything's OK
185 * - SN_ERR_SRVTO if there are no more servers
186 * - SN_ERR_SRVCL if the connection was refused by the server
187 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
188 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
189 * - SN_ERR_INTERNAL for any other purely internal errors
190 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
191 */
192int tcpv4_connect_server(struct stream_interface *si,
193 struct proxy *be, struct server *srv,
Willy Tarreaub1d67742010-03-29 19:36:59 +0200194 struct sockaddr *srv_addr, struct sockaddr *from_addr)
Willy Tarreau9650f372009-08-16 14:02:45 +0200195{
196 int fd;
197
198 if ((fd = si->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
199 qfprintf(stderr, "Cannot get a server socket.\n");
200
201 if (errno == ENFILE)
202 send_log(be, LOG_EMERG,
203 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
204 be->id, maxfd);
205 else if (errno == EMFILE)
206 send_log(be, LOG_EMERG,
207 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
208 be->id, maxfd);
209 else if (errno == ENOBUFS || errno == ENOMEM)
210 send_log(be, LOG_EMERG,
211 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
212 be->id, maxfd);
213 /* this is a resource error */
214 return SN_ERR_RESOURCE;
215 }
216
217 if (fd >= global.maxsock) {
218 /* do not log anything there, it's a normal condition when this option
219 * is used to serialize connections to a server !
220 */
221 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
222 close(fd);
223 return SN_ERR_PRXCOND; /* it is a configuration limit */
224 }
225
226 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
227 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
228 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
229 close(fd);
230 return SN_ERR_INTERNAL;
231 }
232
233 if (be->options & PR_O_TCP_SRV_KA)
234 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
235
236 if (be->options & PR_O_TCP_NOLING)
237 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
238
239 /* allow specific binding :
240 * - server-specific at first
241 * - proxy-specific next
242 */
243 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200244 int ret, flags = 0;
245
Willy Tarreau9650f372009-08-16 14:02:45 +0200246 switch (srv->state & SRV_TPROXY_MASK) {
247 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200248 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200249 flags = 3;
250 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200251 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200252 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200253 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200254 break;
255 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200256
Willy Tarreau9650f372009-08-16 14:02:45 +0200257#ifdef SO_BINDTODEVICE
258 /* Note: this might fail if not CAP_NET_RAW */
259 if (srv->iface_name)
260 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
261#endif
262
263 if (srv->sport_range) {
264 int attempts = 10; /* should be more than enough to find a spare port */
265 struct sockaddr_in src;
266
267 ret = 1;
268 src = srv->source_addr;
269
270 do {
271 /* note: in case of retry, we may have to release a previously
272 * allocated port, hence this loop's construct.
273 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200274 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
275 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200276
277 if (!attempts)
278 break;
279 attempts--;
280
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200281 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
282 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200283 break;
284
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200285 fdinfo[fd].port_range = srv->sport_range;
286 src.sin_port = htons(fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200287
Willy Tarreaub1d67742010-03-29 19:36:59 +0200288 ret = tcpv4_bind_socket(fd, flags, &src, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200289 } while (ret != 0); /* binding NOK */
290 }
291 else {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200292 ret = tcpv4_bind_socket(fd, flags, &srv->source_addr, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200293 }
294
295 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200296 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
297 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200298 close(fd);
299
300 if (ret == 1) {
301 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
302 be->id, srv->id);
303 send_log(be, LOG_EMERG,
304 "Cannot bind to source address before connect() for server %s/%s.\n",
305 be->id, srv->id);
306 } else {
307 Alert("Cannot bind to tproxy 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 tproxy source address before connect() for server %s/%s.\n",
311 be->id, srv->id);
312 }
313 return SN_ERR_RESOURCE;
314 }
315 }
316 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200317 int ret, flags = 0;
318
Willy Tarreau9650f372009-08-16 14:02:45 +0200319 switch (be->options & PR_O_TPXY_MASK) {
320 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200321 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200322 flags = 3;
323 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200324 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200325 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200326 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200327 break;
328 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200329
Willy Tarreau9650f372009-08-16 14:02:45 +0200330#ifdef SO_BINDTODEVICE
331 /* Note: this might fail if not CAP_NET_RAW */
332 if (be->iface_name)
333 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
334#endif
Willy Tarreaub1d67742010-03-29 19:36:59 +0200335 ret = tcpv4_bind_socket(fd, flags, &be->source_addr, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200336 if (ret) {
337 close(fd);
338 if (ret == 1) {
339 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
340 be->id);
341 send_log(be, LOG_EMERG,
342 "Cannot bind to source address before connect() for proxy %s.\n",
343 be->id);
344 } else {
345 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
346 be->id);
347 send_log(be, LOG_EMERG,
348 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
349 be->id);
350 }
351 return SN_ERR_RESOURCE;
352 }
353 }
354
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400355#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 /* disabling tcp quick ack now allows the first request to leave the
357 * machine with the first ACK. We only do this if there are pending
358 * data in the buffer.
359 */
360 if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400361 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200362#endif
363
Willy Tarreaue803de22010-01-21 17:43:04 +0100364 if (global.tune.server_sndbuf)
365 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
366
367 if (global.tune.server_rcvbuf)
368 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
369
Willy Tarreau9650f372009-08-16 14:02:45 +0200370 if ((connect(fd, (struct sockaddr *)srv_addr, sizeof(struct sockaddr_in)) == -1) &&
371 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
372
373 if (errno == EAGAIN || errno == EADDRINUSE) {
374 char *msg;
375 if (errno == EAGAIN) /* no free ports left, try again later */
376 msg = "no free ports";
377 else
378 msg = "local address already in use";
379
380 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200381 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
382 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200383 close(fd);
384 send_log(be, LOG_EMERG,
385 "Connect() failed for server %s/%s: %s.\n",
386 be->id, srv->id, msg);
387 return SN_ERR_RESOURCE;
388 } else if (errno == ETIMEDOUT) {
389 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200390 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
391 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200392 close(fd);
393 return SN_ERR_SRVTO;
394 } else {
395 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
396 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200397 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
398 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200399 close(fd);
400 return SN_ERR_SRVCL;
401 }
402 }
403
404 fdtab[fd].owner = si;
405 fdtab[fd].state = FD_STCONN; /* connection in progress */
406 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
407 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
408 fdtab[fd].cb[DIR_RD].b = si->ib;
409 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
410 fdtab[fd].cb[DIR_WR].b = si->ob;
411
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200412 fdinfo[fd].peeraddr = (struct sockaddr *)srv_addr;
413 fdinfo[fd].peerlen = sizeof(struct sockaddr_in);
Willy Tarreau9650f372009-08-16 14:02:45 +0200414
415 fd_insert(fd);
416 EV_FD_SET(fd, DIR_WR); /* for connect status */
417
418 si->state = SI_ST_CON;
419 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
420 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
421
422 return SN_ERR_NONE; /* connection is OK */
423}
424
425
Willy Tarreaue6b98942007-10-29 01:09:36 +0100426/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
427 * an error message in <err> if the message is at most <errlen> bytes long
428 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
429 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
430 * was alright and that no message was returned. ERR_RETRYABLE means that an
431 * error occurred but that it may vanish after a retry (eg: port in use), and
432 * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter
433 * the meaning of the error, but just indicate that a message is present which
434 * should be displayed with the respective level. Last, ERR_ABORT indicates
435 * that it's pointless to try to start other listeners. No error message is
436 * returned if errlen is NULL.
437 */
438int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
439{
440 __label__ tcp_return, tcp_close_return;
441 int fd, err;
442 const char *msg = NULL;
443
444 /* ensure we never return garbage */
445 if (errmsg && errlen)
446 *errmsg = 0;
447
448 if (listener->state != LI_ASSIGNED)
449 return ERR_NONE; /* already bound */
450
451 err = ERR_NONE;
452
453 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
454 err |= ERR_RETRYABLE | ERR_ALERT;
455 msg = "cannot create listening socket";
456 goto tcp_return;
457 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100458
Willy Tarreaue6b98942007-10-29 01:09:36 +0100459 if (fd >= global.maxsock) {
460 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
461 msg = "not enough free sockets (raise '-n' parameter)";
462 goto tcp_close_return;
463 }
464
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200465 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100466 err |= ERR_FATAL | ERR_ALERT;
467 msg = "cannot make socket non-blocking";
468 goto tcp_close_return;
469 }
470
471 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
472 /* not fatal but should be reported */
473 msg = "cannot do so_reuseaddr";
474 err |= ERR_ALERT;
475 }
476
477 if (listener->options & LI_O_NOLINGER)
478 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100479
Willy Tarreaue6b98942007-10-29 01:09:36 +0100480#ifdef SO_REUSEPORT
481 /* OpenBSD supports this. As it's present in old libc versions of Linux,
482 * it might return an error that we will silently ignore.
483 */
484 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
485#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100486#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100487 if ((listener->options & LI_O_FOREIGN)
Willy Tarreau0a459892008-01-13 17:37:16 +0100488 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1)
489 && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100490 msg = "cannot make listening socket transparent";
491 err |= ERR_ALERT;
492 }
493#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100494#ifdef SO_BINDTODEVICE
495 /* Note: this might fail if not CAP_NET_RAW */
496 if (listener->interface) {
497 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100498 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100499 msg = "cannot bind listener to device";
500 err |= ERR_WARN;
501 }
502 }
503#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400504#if defined(TCP_MAXSEG)
Willy Tarreaube1b9182009-06-14 18:48:19 +0200505 if (listener->maxseg) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400506 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200507 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
508 msg = "cannot set MSS";
509 err |= ERR_WARN;
510 }
511 }
512#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200513#if defined(TCP_DEFER_ACCEPT)
514 if (listener->options & LI_O_DEF_ACCEPT) {
515 /* defer accept by up to one second */
516 int accept_delay = 1;
517 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
518 msg = "cannot enable DEFER_ACCEPT";
519 err |= ERR_WARN;
520 }
521 }
522#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100523 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
524 err |= ERR_RETRYABLE | ERR_ALERT;
525 msg = "cannot bind socket";
526 goto tcp_close_return;
527 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100528
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100529 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100530 err |= ERR_RETRYABLE | ERR_ALERT;
531 msg = "cannot listen to socket";
532 goto tcp_close_return;
533 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100534
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400535#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200536 if (listener->options & LI_O_NOQUICKACK)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400537 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200538#endif
539
Willy Tarreaue6b98942007-10-29 01:09:36 +0100540 /* the socket is ready */
541 listener->fd = fd;
542 listener->state = LI_LISTEN;
543
Willy Tarreaueabf3132008-08-29 23:36:51 +0200544 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100545 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaueb472682010-05-28 18:46:57 +0200546 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
547 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
548 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
549 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200550
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200551 fdinfo[fd].peeraddr = NULL;
552 fdinfo[fd].peerlen = 0;
Willy Tarreaueb472682010-05-28 18:46:57 +0200553 fd_insert(fd);
554
Willy Tarreaue6b98942007-10-29 01:09:36 +0100555 tcp_return:
556 if (msg && errlen)
557 strlcpy2(errmsg, msg, errlen);
558 return err;
559
560 tcp_close_return:
561 close(fd);
562 goto tcp_return;
563}
564
565/* This function creates all TCP sockets bound to the protocol entry <proto>.
566 * It is intended to be used as the protocol's bind_all() function.
567 * The sockets will be registered but not added to any fd_set, in order not to
568 * loose them across the fork(). A call to enable_all_listeners() is needed
569 * to complete initialization. The return value is composed from ERR_*.
570 */
571static int tcp_bind_listeners(struct protocol *proto)
572{
573 struct listener *listener;
574 int err = ERR_NONE;
575
576 list_for_each_entry(listener, &proto->listeners, proto_list) {
577 err |= tcp_bind_listener(listener, NULL, 0);
578 if ((err & ERR_CODE) == ERR_ABORT)
579 break;
580 }
581
582 return err;
583}
584
585/* Add listener to the list of tcpv4 listeners. The listener's state
586 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
587 * listeners is updated. This is the function to use to add a new listener.
588 */
589void tcpv4_add_listener(struct listener *listener)
590{
591 if (listener->state != LI_INIT)
592 return;
593 listener->state = LI_ASSIGNED;
594 listener->proto = &proto_tcpv4;
595 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
596 proto_tcpv4.nb_listeners++;
597}
598
599/* Add listener to the list of tcpv4 listeners. The listener's state
600 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
601 * listeners is updated. This is the function to use to add a new listener.
602 */
603void tcpv6_add_listener(struct listener *listener)
604{
605 if (listener->state != LI_INIT)
606 return;
607 listener->state = LI_ASSIGNED;
608 listener->proto = &proto_tcpv6;
609 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
610 proto_tcpv6.nb_listeners++;
611}
612
Willy Tarreauedcf6682008-11-30 23:15:34 +0100613/* This function performs the TCP request analysis on the current request. It
614 * returns 1 if the processing can continue on next analysers, or zero if it
615 * needs more data, encounters an error, or wants to immediately abort the
616 * request. It relies on buffers flags, and updates s->req->analysers. Its
617 * behaviour is rather simple:
Willy Tarreau86ef7dc2009-03-15 22:55:47 +0100618 * - the analyser should check for errors and timeouts, and react as expected.
619 * It does not have to close anything upon error, the caller will. Note that
620 * the caller also knows how to report errors and timeouts.
621 * - if the analyser does not have enough data, it must return 0 without calling
Willy Tarreauedcf6682008-11-30 23:15:34 +0100622 * other ones. It should also probably do a buffer_write_dis() to ensure
623 * that unprocessed data will not be forwarded. But that probably depends on
624 * the protocol.
625 * - if an analyser has enough data, it just has to pass on to the next
626 * analyser without using buffer_write_dis() (enabled by default).
627 * - if an analyser thinks it has no added value anymore staying here, it must
628 * reset its bit from the analysers flags in order not to be called anymore.
629 *
630 * In the future, analysers should be able to indicate that they want to be
631 * called after XXX bytes have been received (or transfered), and the min of
632 * all's wishes will be used to ring back (unless a special condition occurs).
633 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200634int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100635{
636 struct tcp_rule *rule;
637 int partial;
638
639 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
640 now_ms, __FUNCTION__,
641 s,
642 req,
643 req->rex, req->wex,
644 req->flags,
645 req->l,
646 req->analysers);
647
Willy Tarreauedcf6682008-11-30 23:15:34 +0100648 /* We don't know whether we have enough data, so must proceed
649 * this way :
650 * - iterate through all rules in their declaration order
651 * - if one rule returns MISS, it means the inspect delay is
652 * not over yet, then return immediately, otherwise consider
653 * it as a non-match.
654 * - if one rule returns OK, then return OK
655 * - if one rule returns KO, then return KO
656 */
657
Willy Tarreaud869b242009-03-15 14:43:58 +0100658 if (req->flags & BF_SHUTR || !s->fe->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100659 partial = 0;
660 else
661 partial = ACL_PARTIAL;
662
663 list_for_each_entry(rule, &s->fe->tcp_req.inspect_rules, list) {
664 int ret = ACL_PAT_PASS;
665
666 if (rule->cond) {
Willy Tarreau51d5dad2009-07-12 10:10:05 +0200667 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100668 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200669 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100670 /* just set the request timeout once at the beginning of the request */
Willy Tarreaud869b242009-03-15 14:43:58 +0100671 if (!tick_isset(req->analyse_exp) && s->fe->tcp_req.inspect_delay)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100672 req->analyse_exp = tick_add_ifset(now_ms, s->fe->tcp_req.inspect_delay);
673 return 0;
674 }
675
676 ret = acl_pass(ret);
677 if (rule->cond->pol == ACL_COND_UNLESS)
678 ret = !ret;
679 }
680
681 if (ret) {
682 /* we have a matching rule. */
683 if (rule->action == TCP_ACT_REJECT) {
684 buffer_abort(req);
685 buffer_abort(s->rep);
686 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200687
Willy Tarreau23968d82010-05-23 23:50:44 +0200688 s->fe->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200689 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200690 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200691
Willy Tarreauedcf6682008-11-30 23:15:34 +0100692 if (!(s->flags & SN_ERR_MASK))
693 s->flags |= SN_ERR_PRXCOND;
694 if (!(s->flags & SN_FINST_MASK))
695 s->flags |= SN_FINST_R;
696 return 0;
697 }
698 /* otherwise accept */
699 break;
700 }
701 }
702
703 /* if we get there, it means we have no rule which matches, or
704 * we have an explicit accept, so we apply the default accept.
705 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200706 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100707 req->analyse_exp = TICK_ETERNITY;
708 return 1;
709}
710
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200711/* This function performs the TCP layer4 analysis on the current request. It
712 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
713 * matches or if no more rule matches. It can only use rules which don't need
714 * any data.
715 */
716int tcp_exec_req_rules(struct session *s)
717{
718 struct tcp_rule *rule;
719 int ret;
720
721 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
722 ret = ACL_PAT_PASS;
723
724 if (rule->cond) {
725 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ);
726 ret = acl_pass(ret);
727 if (rule->cond->pol == ACL_COND_UNLESS)
728 ret = !ret;
729 }
730
731 if (ret) {
732 /* we have a matching rule. */
733 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau2799e982010-06-05 15:43:21 +0200734 s->fe->counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200735 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200736 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200737
738 if (!(s->flags & SN_ERR_MASK))
739 s->flags |= SN_ERR_PRXCOND;
740 if (!(s->flags & SN_FINST_MASK))
741 s->flags |= SN_FINST_R;
742 return 0;
743 }
744 /* otherwise it's an accept */
745 break;
746 }
747 }
748 return 1;
749}
750
Willy Tarreaub6866442008-07-14 23:54:42 +0200751/* This function should be called to parse a line starting with the "tcp-request"
752 * keyword.
753 */
754static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
755 struct proxy *defpx, char *err, int errlen)
756{
757 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200758 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +0200759 int retlen;
Willy Tarreau1a687942010-05-23 22:40:30 +0200760 int action;
761 int warn = 0;
762 int pol = ACL_COND_NONE;
763 struct acl_cond *cond;
764 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +0200765
766 if (!*args[1]) {
767 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
768 args[0], proxy_type_str(proxy), curpx->id);
769 return -1;
770 }
771
772 if (!strcmp(args[1], "inspect-delay")) {
773 if (curpx == defpx) {
774 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
775 args[0], args[1]);
776 return -1;
777 }
778
779 if (!(curpx->cap & PR_CAP_FE)) {
780 snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability",
781 args[0], args[1], proxy_type_str(proxy), curpx->id,
782 "frontend");
783 return 1;
784 }
785
786 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
787 retlen = snprintf(err, errlen,
788 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
789 args[0], args[1], proxy_type_str(proxy), curpx->id);
790 if (ptr && retlen < errlen)
791 retlen += snprintf(err+retlen, errlen - retlen,
792 " (unexpected character '%c')", *ptr);
793 return -1;
794 }
795
796 if (curpx->tcp_req.inspect_delay) {
797 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
798 args[0], args[1], proxy_type_str(proxy), curpx->id);
799 return 1;
800 }
801 curpx->tcp_req.inspect_delay = val;
802 return 0;
803 }
804
805 if (!strcmp(args[1], "content")) {
Willy Tarreaub6866442008-07-14 23:54:42 +0200806 if (curpx == defpx) {
807 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
808 args[0], args[1]);
809 return -1;
810 }
811
812 if (!strcmp(args[2], "accept"))
813 action = TCP_ACT_ACCEPT;
814 else if (!strcmp(args[2], "reject"))
815 action = TCP_ACT_REJECT;
816 else {
817 retlen = snprintf(err, errlen,
818 "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')",
819 args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]);
820 return -1;
821 }
822
823 pol = ACL_COND_NONE;
824 cond = NULL;
825
Willy Tarreauef6494c2010-01-28 17:12:36 +0100826 if (strcmp(args[3], "if") == 0 || strcmp(args[3], "unless") == 0) {
827 if ((cond = build_acl_cond(NULL, 0, curpx, (const char **)args+3)) == NULL) {
828 retlen = snprintf(err, errlen,
829 "error detected in %s '%s' while parsing '%s' condition",
830 proxy_type_str(curpx), curpx->id, args[3]);
831 return -1;
832 }
833 }
834 else if (*args[3]) {
Willy Tarreau606ad732009-07-14 21:17:05 +0200835 retlen = snprintf(err, errlen,
836 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
837 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[3]);
838 return -1;
839 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200840
Willy Tarreau1a211942009-07-14 13:53:17 +0200841 if (cond && (cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200842 struct acl *acl;
843 const char *name;
844
Willy Tarreau1a211942009-07-14 13:53:17 +0200845 acl = cond_find_require(cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200846 name = acl ? acl->name : "(unknown)";
847
848 retlen = snprintf(err, errlen,
Willy Tarreau1a211942009-07-14 13:53:17 +0200849 "acl '%s' involves some response-only criteria which will be ignored.",
850 name);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200851 warn++;
852 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200853 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
854 rule->cond = cond;
855 rule->action = action;
856 LIST_INIT(&rule->list);
857 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200858 return warn;
Willy Tarreaub6866442008-07-14 23:54:42 +0200859 }
860
Willy Tarreau1a687942010-05-23 22:40:30 +0200861 /* OK so we're in front of plain L4 rules */
862 if (!strcmp(args[1], "accept"))
863 action = TCP_ACT_ACCEPT;
864 else if (!strcmp(args[1], "reject"))
865 action = TCP_ACT_REJECT;
866 else {
867 retlen = snprintf(err, errlen,
868 "'%s' expects 'inspect-delay', 'content', 'accept' or 'reject', in %s '%s' (was '%s')",
869 args[0], proxy_type_str(curpx), curpx->id, args[1]);
870 return -1;
871 }
872
873 if (curpx == defpx) {
874 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
875 args[0], args[1]);
876 return -1;
877 }
878
879 pol = ACL_COND_NONE;
880 cond = NULL;
881
882 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
883 if ((cond = build_acl_cond(NULL, 0, curpx, (const char **)args+2)) == NULL) {
884 retlen = snprintf(err, errlen,
885 "error detected in %s '%s' while parsing '%s' condition",
886 proxy_type_str(curpx), curpx->id, args[2]);
887 return -1;
888 }
889 }
890 else if (*args[2]) {
891 retlen = snprintf(err, errlen,
892 "'%s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
893 args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]);
894 return -1;
895 }
896
897 if (cond && (cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
898 struct acl *acl;
899 const char *name;
900
901 acl = cond_find_require(cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
902 name = acl ? acl->name : "(unknown)";
903
904 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
905 retlen = snprintf(err, errlen,
906 "'%s %s' may not reference acl '%s' which makes use of payload in %s '%s'. Please use '%s content' for this.",
907 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
908 return -1;
909 }
910 if (acl->requires & ACL_USE_RTR_ANY)
911 retlen = snprintf(err, errlen,
912 "acl '%s' involves some response-only criteria which will be ignored.",
913 name);
914
915 warn++;
916 }
917
918 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
919 rule->cond = cond;
920 rule->action = action;
921 LIST_INIT(&rule->list);
922 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
923 return warn;
Willy Tarreaub6866442008-07-14 23:54:42 +0200924}
925
Willy Tarreau645513a2010-05-24 20:55:15 +0200926
927/************************************************************************/
928/* All supported ACL keywords must be declared here. */
929/************************************************************************/
930
931/* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */
932static int
933acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
934 struct acl_expr *expr, struct acl_test *test)
935{
936 test->i = l4->cli_addr.ss_family;
937 if (test->i == AF_INET)
938 test->ptr = (void *)&((struct sockaddr_in *)&l4->cli_addr)->sin_addr;
939 else
940 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_addr;
941 test->flags = ACL_TEST_F_READ_ONLY;
942 return 1;
943}
944
945/* extract the connection's source address */
946static int
947pattern_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
948 const char *arg, int arg_len, union pattern_data *data)
949{
950 data->ip.s_addr = ((struct sockaddr_in *)&l4->cli_addr)->sin_addr.s_addr;
951 return 1;
952}
953
954
955/* set test->i to the connection's source port */
956static int
957acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir,
958 struct acl_expr *expr, struct acl_test *test)
959{
960 if (l4->cli_addr.ss_family == AF_INET)
961 test->i = ntohs(((struct sockaddr_in *)&l4->cli_addr)->sin_port);
962 else
963 test->i = ntohs(((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_port);
964 test->flags = 0;
965 return 1;
966}
967
968
969/* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */
970static int
971acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
972 struct acl_expr *expr, struct acl_test *test)
973{
974 if (!(l4->flags & SN_FRT_ADDR_SET))
975 get_frt_addr(l4);
976
977 test->i = l4->frt_addr.ss_family;
978 if (test->i == AF_INET)
979 test->ptr = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr;
980 else
981 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_addr;
982 test->flags = ACL_TEST_F_READ_ONLY;
983 return 1;
984}
985
986
987/* extract the connection's destination address */
988static int
989pattern_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
990 const char *arg, int arg_len, union pattern_data *data)
991{
992 data->ip.s_addr = ((struct sockaddr_in *)&l4->frt_addr)->sin_addr.s_addr;
993 return 1;
994}
995
996/* set test->i to the frontend connexion's destination port */
997static int
998acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
999 struct acl_expr *expr, struct acl_test *test)
1000{
1001 if (!(l4->flags & SN_FRT_ADDR_SET))
1002 get_frt_addr(l4);
1003
1004 if (l4->frt_addr.ss_family == AF_INET)
1005 test->i = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port);
1006 else
1007 test->i = ntohs(((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_port);
1008 test->flags = 0;
1009 return 1;
1010}
1011
1012static int
1013pattern_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
1014 const char *arg, int arg_len, union pattern_data *data)
1015
1016{
1017 data->integer = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port);
1018 return 1;
1019}
1020
1021
Willy Tarreaub6866442008-07-14 23:54:42 +02001022static struct cfg_kw_list cfg_kws = {{ },{
1023 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1024 { 0, NULL, NULL },
1025}};
1026
Willy Tarreau645513a2010-05-24 20:55:15 +02001027/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaub6866442008-07-14 23:54:42 +02001028static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +02001029 { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT },
1030 { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1031 { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP },
1032 { "dst_port", acl_parse_int, acl_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT },
Willy Tarreaub6866442008-07-14 23:54:42 +02001033 { NULL, NULL, NULL, NULL },
1034}};
1035
Willy Tarreau645513a2010-05-24 20:55:15 +02001036/* Note: must not be declared <const> as its list will be overwritten */
1037static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
1038 { "src", pattern_fetch_src, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
1039 { "dst", pattern_fetch_dst, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
1040 { "dst_port", pattern_fetch_dport, PATTERN_TYPE_INTEGER, PATTERN_FETCH_REQ },
1041 { NULL, NULL, 0, 0 },
1042}};
1043
Willy Tarreaue6b98942007-10-29 01:09:36 +01001044__attribute__((constructor))
1045static void __tcp_protocol_init(void)
1046{
1047 protocol_register(&proto_tcpv4);
1048 protocol_register(&proto_tcpv6);
Willy Tarreau645513a2010-05-24 20:55:15 +02001049 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001050 cfg_register_keywords(&cfg_kws);
1051 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001052}
1053
1054
1055/*
1056 * Local variables:
1057 * c-indent-level: 8
1058 * c-basic-offset: 8
1059 * End:
1060 */