blob: 68da06336e431ce326e060f912e7be1f0d8f77d5 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaue8c66af2008-01-13 18:40:14 +01004 * Copyright 2000-2008 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>
34#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
37#include <common/time.h>
38#include <common/version.h>
39
Willy Tarreaue6b98942007-10-29 01:09:36 +010040#include <types/global.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020041#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010042
43#include <proto/acl.h>
44#include <proto/backend.h>
45#include <proto/buffers.h>
46#include <proto/fd.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020047#include <proto/log.h>
48#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010049#include <proto/protocols.h>
50#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020051#include <proto/proxy.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010052#include <proto/queue.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010053#include <proto/session.h>
54#include <proto/stream_sock.h>
55#include <proto/task.h>
56
Willy Tarreaue8c66af2008-01-13 18:40:14 +010057#ifdef CONFIG_HAP_CTTPROXY
58#include <import/ip_tproxy.h>
59#endif
60
Willy Tarreaue6b98942007-10-29 01:09:36 +010061static int tcp_bind_listeners(struct protocol *proto);
62
63/* Note: must not be declared <const> as its list will be overwritten */
64static struct protocol proto_tcpv4 = {
65 .name = "tcpv4",
66 .sock_domain = AF_INET,
67 .sock_type = SOCK_STREAM,
68 .sock_prot = IPPROTO_TCP,
69 .sock_family = AF_INET,
70 .sock_addrlen = sizeof(struct sockaddr_in),
71 .l3_addrlen = 32/8,
72 .read = &stream_sock_read,
73 .write = &stream_sock_write,
74 .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,
90 .read = &stream_sock_read,
91 .write = &stream_sock_write,
92 .bind_all = tcp_bind_listeners,
93 .unbind_all = unbind_all_listeners,
94 .enable_all = enable_all_listeners,
95 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
96 .nb_listeners = 0,
97};
98
Willy Tarreaue8c66af2008-01-13 18:40:14 +010099
100/* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which
101 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
102 * - 0 : ignore remote address (may even be a NULL pointer)
103 * - 1 : use provided address
104 * - 2 : use provided port
105 * - 3 : use both
106 *
107 * The function supports multiple foreign binding methods :
108 * - linux_tproxy: we directly bind to the foreign address
109 * - cttproxy: we bind to a local address then nat.
110 * The second one can be used as a fallback for the first one.
111 * This function returns 0 when everything's OK, 1 if it could not bind, to the
112 * local address, 2 if it could not bind to the foreign address.
113 */
114int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote)
115{
116 struct sockaddr_in bind_addr;
117 int foreign_ok = 0;
118 int ret;
119
120#ifdef CONFIG_HAP_LINUX_TPROXY
121 static int ip_transp_working = 1;
122 if (flags && ip_transp_working) {
123 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0
124 || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0)
125 foreign_ok = 1;
126 else
127 ip_transp_working = 0;
128 }
129#endif
130 if (flags) {
131 memset(&bind_addr, 0, sizeof(bind_addr));
132 if (flags & 1)
133 bind_addr.sin_addr = remote->sin_addr;
134 if (flags & 2)
135 bind_addr.sin_port = remote->sin_port;
136 }
137
138 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
139 if (foreign_ok) {
140 ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
141 if (ret < 0)
142 return 2;
143 }
144 else {
145 ret = bind(fd, (struct sockaddr *)local, sizeof(*local));
146 if (ret < 0)
147 return 1;
148 }
149
150 if (!flags)
151 return 0;
152
153#ifdef CONFIG_HAP_CTTPROXY
154 if (!foreign_ok) {
155 struct in_tproxy itp1, itp2;
156 memset(&itp1, 0, sizeof(itp1));
157
158 itp1.op = TPROXY_ASSIGN;
159 itp1.v.addr.faddr = bind_addr.sin_addr;
160 itp1.v.addr.fport = bind_addr.sin_port;
161
162 /* set connect flag on socket */
163 itp2.op = TPROXY_FLAGS;
164 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
165
166 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
167 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
168 foreign_ok = 1;
169 }
170 }
171#endif
172 if (!foreign_ok)
173 /* we could not bind to a foreign address */
174 return 2;
175
176 return 0;
177}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100178
Willy Tarreau9650f372009-08-16 14:02:45 +0200179
180/*
181 * This function initiates a connection to the server assigned to this session
182 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet.
183 * 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,
194 struct sockaddr *srv_addr, struct sockaddr *cli_addr)
195{
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) {
244 struct sockaddr_in *remote = NULL;
245 int ret, flags = 0;
246
247#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
248 switch (srv->state & SRV_TPROXY_MASK) {
249 case SRV_TPROXY_ADDR:
250 remote = (struct sockaddr_in *)&srv->tproxy_addr;
251 flags = 3;
252 break;
253 case SRV_TPROXY_CLI:
254 if (cli_addr)
255 flags |= 2;
256 /* fall through */
257 case SRV_TPROXY_CIP:
258 /* FIXME: what can we do if the client connects in IPv6 ? */
259 if (cli_addr)
260 flags |= 1;
261 remote = (struct sockaddr_in *)cli_addr;
262 break;
263 }
264#endif
265#ifdef SO_BINDTODEVICE
266 /* Note: this might fail if not CAP_NET_RAW */
267 if (srv->iface_name)
268 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
269#endif
270
271 if (srv->sport_range) {
272 int attempts = 10; /* should be more than enough to find a spare port */
273 struct sockaddr_in src;
274
275 ret = 1;
276 src = srv->source_addr;
277
278 do {
279 /* note: in case of retry, we may have to release a previously
280 * allocated port, hence this loop's construct.
281 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200282 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
283 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200284
285 if (!attempts)
286 break;
287 attempts--;
288
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200289 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
290 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200291 break;
292
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200293 fdinfo[fd].port_range = srv->sport_range;
294 src.sin_port = htons(fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200295
296 ret = tcpv4_bind_socket(fd, flags, &src, remote);
297 } while (ret != 0); /* binding NOK */
298 }
299 else {
300 ret = tcpv4_bind_socket(fd, flags, &srv->source_addr, remote);
301 }
302
303 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200304 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
305 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200306 close(fd);
307
308 if (ret == 1) {
309 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
310 be->id, srv->id);
311 send_log(be, LOG_EMERG,
312 "Cannot bind to source address before connect() for server %s/%s.\n",
313 be->id, srv->id);
314 } else {
315 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
316 be->id, srv->id);
317 send_log(be, LOG_EMERG,
318 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
319 be->id, srv->id);
320 }
321 return SN_ERR_RESOURCE;
322 }
323 }
324 else if (be->options & PR_O_BIND_SRC) {
325 struct sockaddr_in *remote = NULL;
326 int ret, flags = 0;
327
328#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
329 switch (be->options & PR_O_TPXY_MASK) {
330 case PR_O_TPXY_ADDR:
331 remote = (struct sockaddr_in *)&be->tproxy_addr;
332 flags = 3;
333 break;
334 case PR_O_TPXY_CLI:
335 if (cli_addr)
336 flags |= 2;
337 /* fall through */
338 case PR_O_TPXY_CIP:
339 /* FIXME: what can we do if the client connects in IPv6 ? */
340 if (cli_addr)
341 flags |= 1;
342 remote = (struct sockaddr_in *)cli_addr;
343 break;
344 }
345#endif
346#ifdef SO_BINDTODEVICE
347 /* Note: this might fail if not CAP_NET_RAW */
348 if (be->iface_name)
349 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
350#endif
351 ret = tcpv4_bind_socket(fd, flags, &be->source_addr, remote);
352 if (ret) {
353 close(fd);
354 if (ret == 1) {
355 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
356 be->id);
357 send_log(be, LOG_EMERG,
358 "Cannot bind to source address before connect() for proxy %s.\n",
359 be->id);
360 } else {
361 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
362 be->id);
363 send_log(be, LOG_EMERG,
364 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
365 be->id);
366 }
367 return SN_ERR_RESOURCE;
368 }
369 }
370
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400371#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200372 /* disabling tcp quick ack now allows the first request to leave the
373 * machine with the first ACK. We only do this if there are pending
374 * data in the buffer.
375 */
376 if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400377 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200378#endif
379
380 if ((connect(fd, (struct sockaddr *)srv_addr, sizeof(struct sockaddr_in)) == -1) &&
381 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
382
383 if (errno == EAGAIN || errno == EADDRINUSE) {
384 char *msg;
385 if (errno == EAGAIN) /* no free ports left, try again later */
386 msg = "no free ports";
387 else
388 msg = "local address already in use";
389
390 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200391 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
392 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200393 close(fd);
394 send_log(be, LOG_EMERG,
395 "Connect() failed for server %s/%s: %s.\n",
396 be->id, srv->id, msg);
397 return SN_ERR_RESOURCE;
398 } else if (errno == ETIMEDOUT) {
399 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200400 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
401 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200402 close(fd);
403 return SN_ERR_SRVTO;
404 } else {
405 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
406 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200407 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
408 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200409 close(fd);
410 return SN_ERR_SRVCL;
411 }
412 }
413
414 fdtab[fd].owner = si;
415 fdtab[fd].state = FD_STCONN; /* connection in progress */
416 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
417 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
418 fdtab[fd].cb[DIR_RD].b = si->ib;
419 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
420 fdtab[fd].cb[DIR_WR].b = si->ob;
421
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200422 fdinfo[fd].peeraddr = (struct sockaddr *)srv_addr;
423 fdinfo[fd].peerlen = sizeof(struct sockaddr_in);
Willy Tarreau9650f372009-08-16 14:02:45 +0200424
425 fd_insert(fd);
426 EV_FD_SET(fd, DIR_WR); /* for connect status */
427
428 si->state = SI_ST_CON;
429 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
430 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
431
432 return SN_ERR_NONE; /* connection is OK */
433}
434
435
Willy Tarreaue6b98942007-10-29 01:09:36 +0100436/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
437 * an error message in <err> if the message is at most <errlen> bytes long
438 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
439 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
440 * was alright and that no message was returned. ERR_RETRYABLE means that an
441 * error occurred but that it may vanish after a retry (eg: port in use), and
442 * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter
443 * the meaning of the error, but just indicate that a message is present which
444 * should be displayed with the respective level. Last, ERR_ABORT indicates
445 * that it's pointless to try to start other listeners. No error message is
446 * returned if errlen is NULL.
447 */
448int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
449{
450 __label__ tcp_return, tcp_close_return;
451 int fd, err;
452 const char *msg = NULL;
453
454 /* ensure we never return garbage */
455 if (errmsg && errlen)
456 *errmsg = 0;
457
458 if (listener->state != LI_ASSIGNED)
459 return ERR_NONE; /* already bound */
460
461 err = ERR_NONE;
462
463 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
464 err |= ERR_RETRYABLE | ERR_ALERT;
465 msg = "cannot create listening socket";
466 goto tcp_return;
467 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100468
Willy Tarreaue6b98942007-10-29 01:09:36 +0100469 if (fd >= global.maxsock) {
470 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
471 msg = "not enough free sockets (raise '-n' parameter)";
472 goto tcp_close_return;
473 }
474
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200475 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100476 err |= ERR_FATAL | ERR_ALERT;
477 msg = "cannot make socket non-blocking";
478 goto tcp_close_return;
479 }
480
481 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
482 /* not fatal but should be reported */
483 msg = "cannot do so_reuseaddr";
484 err |= ERR_ALERT;
485 }
486
487 if (listener->options & LI_O_NOLINGER)
488 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100489
Willy Tarreaue6b98942007-10-29 01:09:36 +0100490#ifdef SO_REUSEPORT
491 /* OpenBSD supports this. As it's present in old libc versions of Linux,
492 * it might return an error that we will silently ignore.
493 */
494 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
495#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100496#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100497 if ((listener->options & LI_O_FOREIGN)
Willy Tarreau0a459892008-01-13 17:37:16 +0100498 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1)
499 && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100500 msg = "cannot make listening socket transparent";
501 err |= ERR_ALERT;
502 }
503#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100504#ifdef SO_BINDTODEVICE
505 /* Note: this might fail if not CAP_NET_RAW */
506 if (listener->interface) {
507 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100508 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100509 msg = "cannot bind listener to device";
510 err |= ERR_WARN;
511 }
512 }
513#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400514#if defined(TCP_MAXSEG)
Willy Tarreaube1b9182009-06-14 18:48:19 +0200515 if (listener->maxseg) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400516 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200517 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
518 msg = "cannot set MSS";
519 err |= ERR_WARN;
520 }
521 }
522#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200523#if defined(TCP_DEFER_ACCEPT)
524 if (listener->options & LI_O_DEF_ACCEPT) {
525 /* defer accept by up to one second */
526 int accept_delay = 1;
527 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
528 msg = "cannot enable DEFER_ACCEPT";
529 err |= ERR_WARN;
530 }
531 }
532#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100533 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
534 err |= ERR_RETRYABLE | ERR_ALERT;
535 msg = "cannot bind socket";
536 goto tcp_close_return;
537 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100538
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100539 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100540 err |= ERR_RETRYABLE | ERR_ALERT;
541 msg = "cannot listen to socket";
542 goto tcp_close_return;
543 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100544
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400545#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200546 if (listener->options & LI_O_NOQUICKACK)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400547 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200548#endif
549
Willy Tarreaue6b98942007-10-29 01:09:36 +0100550 /* the socket is ready */
551 listener->fd = fd;
552 listener->state = LI_LISTEN;
553
554 /* the function for the accept() event */
555 fd_insert(fd);
556 fdtab[fd].cb[DIR_RD].f = listener->accept;
557 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
558 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200559 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100560 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200561 fdtab[fd].flags = FD_FL_TCP;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200562 if (listener->options & LI_O_NOLINGER)
563 fdtab[fd].flags |= FD_FL_TCP_NOLING;
564
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200565 fdinfo[fd].peeraddr = NULL;
566 fdinfo[fd].peerlen = 0;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100567 tcp_return:
568 if (msg && errlen)
569 strlcpy2(errmsg, msg, errlen);
570 return err;
571
572 tcp_close_return:
573 close(fd);
574 goto tcp_return;
575}
576
577/* This function creates all TCP sockets bound to the protocol entry <proto>.
578 * It is intended to be used as the protocol's bind_all() function.
579 * The sockets will be registered but not added to any fd_set, in order not to
580 * loose them across the fork(). A call to enable_all_listeners() is needed
581 * to complete initialization. The return value is composed from ERR_*.
582 */
583static int tcp_bind_listeners(struct protocol *proto)
584{
585 struct listener *listener;
586 int err = ERR_NONE;
587
588 list_for_each_entry(listener, &proto->listeners, proto_list) {
589 err |= tcp_bind_listener(listener, NULL, 0);
590 if ((err & ERR_CODE) == ERR_ABORT)
591 break;
592 }
593
594 return err;
595}
596
597/* Add listener to the list of tcpv4 listeners. The listener's state
598 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
599 * listeners is updated. This is the function to use to add a new listener.
600 */
601void tcpv4_add_listener(struct listener *listener)
602{
603 if (listener->state != LI_INIT)
604 return;
605 listener->state = LI_ASSIGNED;
606 listener->proto = &proto_tcpv4;
607 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
608 proto_tcpv4.nb_listeners++;
609}
610
611/* Add listener to the list of tcpv4 listeners. The listener's state
612 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
613 * listeners is updated. This is the function to use to add a new listener.
614 */
615void tcpv6_add_listener(struct listener *listener)
616{
617 if (listener->state != LI_INIT)
618 return;
619 listener->state = LI_ASSIGNED;
620 listener->proto = &proto_tcpv6;
621 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
622 proto_tcpv6.nb_listeners++;
623}
624
Willy Tarreauedcf6682008-11-30 23:15:34 +0100625/* This function performs the TCP request analysis on the current request. It
626 * returns 1 if the processing can continue on next analysers, or zero if it
627 * needs more data, encounters an error, or wants to immediately abort the
628 * request. It relies on buffers flags, and updates s->req->analysers. Its
629 * behaviour is rather simple:
Willy Tarreau86ef7dc2009-03-15 22:55:47 +0100630 * - the analyser should check for errors and timeouts, and react as expected.
631 * It does not have to close anything upon error, the caller will. Note that
632 * the caller also knows how to report errors and timeouts.
633 * - if the analyser does not have enough data, it must return 0 without calling
Willy Tarreauedcf6682008-11-30 23:15:34 +0100634 * other ones. It should also probably do a buffer_write_dis() to ensure
635 * that unprocessed data will not be forwarded. But that probably depends on
636 * the protocol.
637 * - if an analyser has enough data, it just has to pass on to the next
638 * analyser without using buffer_write_dis() (enabled by default).
639 * - if an analyser thinks it has no added value anymore staying here, it must
640 * reset its bit from the analysers flags in order not to be called anymore.
641 *
642 * In the future, analysers should be able to indicate that they want to be
643 * called after XXX bytes have been received (or transfered), and the min of
644 * all's wishes will be used to ring back (unless a special condition occurs).
645 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200646int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100647{
648 struct tcp_rule *rule;
649 int partial;
650
651 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
652 now_ms, __FUNCTION__,
653 s,
654 req,
655 req->rex, req->wex,
656 req->flags,
657 req->l,
658 req->analysers);
659
Willy Tarreauedcf6682008-11-30 23:15:34 +0100660 /* We don't know whether we have enough data, so must proceed
661 * this way :
662 * - iterate through all rules in their declaration order
663 * - if one rule returns MISS, it means the inspect delay is
664 * not over yet, then return immediately, otherwise consider
665 * it as a non-match.
666 * - if one rule returns OK, then return OK
667 * - if one rule returns KO, then return KO
668 */
669
Willy Tarreaud869b242009-03-15 14:43:58 +0100670 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 +0100671 partial = 0;
672 else
673 partial = ACL_PARTIAL;
674
675 list_for_each_entry(rule, &s->fe->tcp_req.inspect_rules, list) {
676 int ret = ACL_PAT_PASS;
677
678 if (rule->cond) {
Willy Tarreau51d5dad2009-07-12 10:10:05 +0200679 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100680 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200681 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100682 /* just set the request timeout once at the beginning of the request */
Willy Tarreaud869b242009-03-15 14:43:58 +0100683 if (!tick_isset(req->analyse_exp) && s->fe->tcp_req.inspect_delay)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100684 req->analyse_exp = tick_add_ifset(now_ms, s->fe->tcp_req.inspect_delay);
685 return 0;
686 }
687
688 ret = acl_pass(ret);
689 if (rule->cond->pol == ACL_COND_UNLESS)
690 ret = !ret;
691 }
692
693 if (ret) {
694 /* we have a matching rule. */
695 if (rule->action == TCP_ACT_REJECT) {
696 buffer_abort(req);
697 buffer_abort(s->rep);
698 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200699
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200700 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200701 if (s->listener->counters)
702 s->listener->counters->failed_req++;
703
Willy Tarreauedcf6682008-11-30 23:15:34 +0100704 if (!(s->flags & SN_ERR_MASK))
705 s->flags |= SN_ERR_PRXCOND;
706 if (!(s->flags & SN_FINST_MASK))
707 s->flags |= SN_FINST_R;
708 return 0;
709 }
710 /* otherwise accept */
711 break;
712 }
713 }
714
715 /* if we get there, it means we have no rule which matches, or
716 * we have an explicit accept, so we apply the default accept.
717 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200718 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100719 req->analyse_exp = TICK_ETERNITY;
720 return 1;
721}
722
Emeric Brun647caf12009-06-30 17:57:00 +0200723/* Apply RDP cookie persistence to the current session. For this, the function
724 * tries to extract an RDP cookie from the request buffer, and look for the
725 * matching server in the list. If the server is found, it is assigned to the
726 * session. This always returns 1, and the analyser removes itself from the
727 * list. Nothing is performed if a server was already assigned.
728 */
729int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
730{
731 struct proxy *px = s->be;
732 int ret;
733 struct acl_expr expr;
734 struct acl_test test;
735 struct server *srv = px->srv;
736 struct sockaddr_in addr;
737 char *p;
738
739 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
740 now_ms, __FUNCTION__,
741 s,
742 req,
743 req->rex, req->wex,
744 req->flags,
745 req->l,
746 req->analysers);
747
748 if (s->flags & SN_ASSIGNED)
749 goto no_cookie;
750
751 memset(&expr, 0, sizeof(expr));
752 memset(&test, 0, sizeof(test));
753
754 expr.arg.str = s->be->rdp_cookie_name;
755 expr.arg_len = s->be->rdp_cookie_len;
756
757 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
758 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
759 goto no_cookie;
760
761 memset(&addr, 0, sizeof(addr));
762 addr.sin_family = AF_INET;
763
764 /* Considering an rdp cookie detected using acl, test.ptr ended with <cr><lf> and should return */
765 addr.sin_addr.s_addr = strtoul(test.ptr, &p, 10);
766 if (*p != '.')
767 goto no_cookie;
768 p++;
769 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
770 if (*p != '.')
771 goto no_cookie;
772
773 while (srv) {
774 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
775 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
776 /* we found the server and it is usable */
777 s->flags |= SN_DIRECT | SN_ASSIGNED;
778 s->srv = srv;
779 break;
780 }
781 }
782 srv = srv->next;
783 }
784
785no_cookie:
786 req->analysers &= ~an_bit;
787 req->analyse_exp = TICK_ETERNITY;
788 return 1;
789}
790
Willy Tarreauedcf6682008-11-30 23:15:34 +0100791
Willy Tarreaub6866442008-07-14 23:54:42 +0200792/* This function should be called to parse a line starting with the "tcp-request"
793 * keyword.
794 */
795static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
796 struct proxy *defpx, char *err, int errlen)
797{
798 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200799 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +0200800 int retlen;
801
802 if (!*args[1]) {
803 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
804 args[0], proxy_type_str(proxy), curpx->id);
805 return -1;
806 }
807
808 if (!strcmp(args[1], "inspect-delay")) {
809 if (curpx == defpx) {
810 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
811 args[0], args[1]);
812 return -1;
813 }
814
815 if (!(curpx->cap & PR_CAP_FE)) {
816 snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability",
817 args[0], args[1], proxy_type_str(proxy), curpx->id,
818 "frontend");
819 return 1;
820 }
821
822 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
823 retlen = snprintf(err, errlen,
824 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
825 args[0], args[1], proxy_type_str(proxy), curpx->id);
826 if (ptr && retlen < errlen)
827 retlen += snprintf(err+retlen, errlen - retlen,
828 " (unexpected character '%c')", *ptr);
829 return -1;
830 }
831
832 if (curpx->tcp_req.inspect_delay) {
833 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
834 args[0], args[1], proxy_type_str(proxy), curpx->id);
835 return 1;
836 }
837 curpx->tcp_req.inspect_delay = val;
838 return 0;
839 }
840
841 if (!strcmp(args[1], "content")) {
842 int action;
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200843 int warn = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +0200844 int pol = ACL_COND_NONE;
845 struct acl_cond *cond;
846 struct tcp_rule *rule;
847
848 if (curpx == defpx) {
849 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
850 args[0], args[1]);
851 return -1;
852 }
853
854 if (!strcmp(args[2], "accept"))
855 action = TCP_ACT_ACCEPT;
856 else if (!strcmp(args[2], "reject"))
857 action = TCP_ACT_REJECT;
858 else {
859 retlen = snprintf(err, errlen,
860 "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')",
861 args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]);
862 return -1;
863 }
864
865 pol = ACL_COND_NONE;
866 cond = NULL;
867
Willy Tarreau606ad732009-07-14 21:17:05 +0200868 if (!*args[3])
869 pol = ACL_COND_NONE;
870 else if (!strcmp(args[3], "if"))
Willy Tarreaub6866442008-07-14 23:54:42 +0200871 pol = ACL_COND_IF;
872 else if (!strcmp(args[3], "unless"))
873 pol = ACL_COND_UNLESS;
Willy Tarreau606ad732009-07-14 21:17:05 +0200874 else {
875 retlen = snprintf(err, errlen,
876 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
877 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[3]);
878 return -1;
879 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200880
881 /* Note: we consider "if TRUE" when there is no condition */
882 if (pol != ACL_COND_NONE &&
883 (cond = parse_acl_cond((const char **)args+4, &curpx->acl, pol)) == NULL) {
884 retlen = snprintf(err, errlen,
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200885 "error detected in %s '%s' while parsing '%s' condition",
Willy Tarreaub6866442008-07-14 23:54:42 +0200886 proxy_type_str(curpx), curpx->id, args[3]);
887 return -1;
888 }
889
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200890 // FIXME: how to set this ?
891 // cond->line = linenum;
Willy Tarreaua9fb0832009-07-10 20:53:53 +0200892 if (cond)
893 curpx->acl_requires |= cond->requires;
Willy Tarreau1a211942009-07-14 13:53:17 +0200894 if (cond && (cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200895 struct acl *acl;
896 const char *name;
897
Willy Tarreau1a211942009-07-14 13:53:17 +0200898 acl = cond_find_require(cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200899 name = acl ? acl->name : "(unknown)";
900
901 retlen = snprintf(err, errlen,
Willy Tarreau1a211942009-07-14 13:53:17 +0200902 "acl '%s' involves some response-only criteria which will be ignored.",
903 name);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200904 warn++;
905 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200906 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
907 rule->cond = cond;
908 rule->action = action;
909 LIST_INIT(&rule->list);
910 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200911 return warn;
Willy Tarreaub6866442008-07-14 23:54:42 +0200912 }
913
914 snprintf(err, errlen, "unknown argument '%s' after '%s' in %s '%s'",
915 args[1], args[0], proxy_type_str(proxy), curpx->id);
916 return -1;
917}
918
919/* return the number of bytes in the request buffer */
920static int
921acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
922 struct acl_expr *expr, struct acl_test *test)
923{
924 if (!l4 || !l4->req)
925 return 0;
926
927 test->i = l4->req->l;
928 test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE;
929 return 1;
930}
931
Willy Tarreau655e26a2008-07-15 18:58:05 +0200932/* Return the version of the SSL protocol in the request. It supports both
933 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
934 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
935 * SSLv2 format is described here, and completed p67 of RFC 2246 :
936 * http://wp.netscape.com/eng/security/SSL_2.html
937 *
938 * Note: this decoder only works with non-wrapping data.
939 */
940static int
941acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
942 struct acl_expr *expr, struct acl_test *test)
943{
944 int version, bleft, msg_len;
945 const unsigned char *data;
946
947 if (!l4 || !l4->req)
948 return 0;
949
950 msg_len = 0;
951 bleft = l4->req->l;
952 if (!bleft)
953 goto too_short;
954
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200955 data = (const unsigned char *)l4->req->w;
Willy Tarreau655e26a2008-07-15 18:58:05 +0200956 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
957 /* SSLv3 header format */
958 if (bleft < 5)
959 goto too_short;
960
961 version = (data[1] << 16) + data[2]; /* version: major, minor */
962 msg_len = (data[3] << 8) + data[4]; /* record length */
963
964 /* format introduced with SSLv3 */
965 if (version < 0x00030000)
966 goto not_ssl;
967
968 /* message length between 1 and 2^14 + 2048 */
969 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
970 goto not_ssl;
971
972 bleft -= 5; data += 5;
973 } else {
974 /* SSLv2 header format, only supported for hello (msg type 1) */
975 int rlen, plen, cilen, silen, chlen;
976
977 if (*data & 0x80) {
978 if (bleft < 3)
979 goto too_short;
980 /* short header format : 15 bits for length */
981 rlen = ((data[0] & 0x7F) << 8) | data[1];
982 plen = 0;
983 bleft -= 2; data += 2;
984 } else {
985 if (bleft < 4)
986 goto too_short;
987 /* long header format : 14 bits for length + pad length */
988 rlen = ((data[0] & 0x3F) << 8) | data[1];
989 plen = data[2];
990 bleft -= 3; data += 2;
991 }
992
993 if (*data != 0x01)
994 goto not_ssl;
995 bleft--; data++;
996
997 if (bleft < 8)
998 goto too_short;
999 version = (data[0] << 16) + data[1]; /* version: major, minor */
1000 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
1001 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
1002 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
1003
1004 bleft -= 8; data += 8;
1005 if (cilen % 3 != 0)
1006 goto not_ssl;
1007 if (silen && silen != 16)
1008 goto not_ssl;
1009 if (chlen < 16 || chlen > 32)
1010 goto not_ssl;
1011 if (rlen != 9 + cilen + silen + chlen)
1012 goto not_ssl;
1013
1014 /* focus on the remaining data length */
1015 msg_len = cilen + silen + chlen + plen;
1016 }
1017 /* We could recursively check that the buffer ends exactly on an SSL
1018 * fragment boundary and that a possible next segment is still SSL,
1019 * but that's a bit pointless. However, we could still check that
1020 * all the part of the request which fits in a buffer is already
1021 * there.
1022 */
Willy Tarreau03d60bb2009-01-09 11:13:00 +01001023 if (msg_len > l4->req->max_len + l4->req->data - l4->req->w)
1024 msg_len = l4->req->max_len + l4->req->data - l4->req->w;
Willy Tarreau655e26a2008-07-15 18:58:05 +02001025
1026 if (bleft < msg_len)
1027 goto too_short;
1028
1029 /* OK that's enough. We have at least the whole message, and we have
1030 * the protocol version.
1031 */
1032 test->i = version;
1033 test->flags = ACL_TEST_F_VOLATILE;
1034 return 1;
1035
1036 too_short:
1037 test->flags = ACL_TEST_F_MAY_CHANGE;
1038 not_ssl:
1039 return 0;
1040}
1041
Emeric Brunbede3d02009-06-30 17:54:00 +02001042int
1043acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
1044 struct acl_expr *expr, struct acl_test *test)
1045{
1046 int bleft;
1047 const unsigned char *data;
1048
1049 if (!l4 || !l4->req)
1050 return 0;
1051
1052 test->flags = 0;
1053
1054 bleft = l4->req->l;
1055 if (bleft <= 11)
1056 goto too_short;
1057
1058 data = (const unsigned char *)l4->req->w + 11;
1059 bleft -= 11;
1060
1061 if (bleft <= 7)
1062 goto too_short;
1063
1064 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1065 goto not_cookie;
1066
1067 data += 7;
1068 bleft -= 7;
1069
1070 while (bleft > 0 && *data == ' ') {
1071 data++;
1072 bleft--;
1073 }
1074
1075 if (expr->arg_len) {
1076
1077 if (bleft <= expr->arg_len)
1078 goto too_short;
1079
1080 if ((data[expr->arg_len] != '=') ||
1081 strncasecmp(expr->arg.str, (const char *)data, expr->arg_len) != 0)
1082 goto not_cookie;
1083
1084 data += expr->arg_len + 1;
1085 bleft -= expr->arg_len + 1;
1086 } else {
1087 while (bleft > 0 && *data != '=') {
1088 if (*data == '\r' || *data == '\n')
1089 goto not_cookie;
1090 data++;
1091 bleft--;
1092 }
1093
1094 if (bleft < 1)
1095 goto too_short;
1096
1097 if (*data != '=')
1098 goto not_cookie;
1099
1100 data++;
1101 bleft--;
1102 }
1103
1104 /* data points to cookie value */
1105 test->ptr = (char *)data;
1106 test->len = 0;
1107
1108 while (bleft > 0 && *data != '\r') {
1109 data++;
1110 bleft--;
1111 }
1112
1113 if (bleft < 2)
1114 goto too_short;
1115
1116 if (data[0] != '\r' || data[1] != '\n')
1117 goto not_cookie;
1118
1119 test->len = (char *)data - test->ptr;
1120 test->flags = ACL_TEST_F_VOLATILE;
1121 return 1;
1122
1123 too_short:
1124 test->flags = ACL_TEST_F_MAY_CHANGE;
1125 not_cookie:
1126 return 0;
1127}
1128
1129static int
1130acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
1131 struct acl_expr *expr, struct acl_test *test)
1132{
1133 int ret;
1134
1135 ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, test);
1136
1137 test->ptr = NULL;
1138 test->len = 0;
1139
1140 if (test->flags & ACL_TEST_F_MAY_CHANGE)
1141 return 0;
1142
1143 test->flags = ACL_TEST_F_VOLATILE;
1144 test->i = ret;
1145
1146 return 1;
1147}
Willy Tarreaub6866442008-07-14 23:54:42 +02001148
1149static struct cfg_kw_list cfg_kws = {{ },{
1150 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1151 { 0, NULL, NULL },
1152}};
1153
1154static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02001155 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L4REQ_VOLATILE },
1156 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L4REQ_VOLATILE },
Emeric Brunbede3d02009-06-30 17:54:00 +02001157 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L4REQ_VOLATILE },
1158 { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L4REQ_VOLATILE },
Willy Tarreaub6866442008-07-14 23:54:42 +02001159 { NULL, NULL, NULL, NULL },
1160}};
1161
Willy Tarreaue6b98942007-10-29 01:09:36 +01001162__attribute__((constructor))
1163static void __tcp_protocol_init(void)
1164{
1165 protocol_register(&proto_tcpv4);
1166 protocol_register(&proto_tcpv6);
Willy Tarreaub6866442008-07-14 23:54:42 +02001167 cfg_register_keywords(&cfg_kws);
1168 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001169}
1170
1171
1172/*
1173 * Local variables:
1174 * c-indent-level: 8
1175 * c-basic-offset: 8
1176 * End:
1177 */