blob: 7c958fc4d8769f414c9b20220ec9518f52e96cf7 [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>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010047#include <proto/fd.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020048#include <proto/log.h>
49#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010050#include <proto/protocols.h>
51#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020052#include <proto/proxy.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010053#include <proto/queue.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010054#include <proto/session.h>
55#include <proto/stream_sock.h>
56#include <proto/task.h>
57
Willy Tarreaue8c66af2008-01-13 18:40:14 +010058#ifdef CONFIG_HAP_CTTPROXY
59#include <import/ip_tproxy.h>
60#endif
61
Willy Tarreaue6b98942007-10-29 01:09:36 +010062static int tcp_bind_listeners(struct protocol *proto);
63
64/* Note: must not be declared <const> as its list will be overwritten */
65static struct protocol proto_tcpv4 = {
66 .name = "tcpv4",
67 .sock_domain = AF_INET,
68 .sock_type = SOCK_STREAM,
69 .sock_prot = IPPROTO_TCP,
70 .sock_family = AF_INET,
71 .sock_addrlen = sizeof(struct sockaddr_in),
72 .l3_addrlen = 32/8,
73 .read = &stream_sock_read,
74 .write = &stream_sock_write,
75 .bind_all = tcp_bind_listeners,
76 .unbind_all = unbind_all_listeners,
77 .enable_all = enable_all_listeners,
78 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
79 .nb_listeners = 0,
80};
81
82/* Note: must not be declared <const> as its list will be overwritten */
83static struct protocol proto_tcpv6 = {
84 .name = "tcpv6",
85 .sock_domain = AF_INET6,
86 .sock_type = SOCK_STREAM,
87 .sock_prot = IPPROTO_TCP,
88 .sock_family = AF_INET6,
89 .sock_addrlen = sizeof(struct sockaddr_in6),
90 .l3_addrlen = 128/8,
91 .read = &stream_sock_read,
92 .write = &stream_sock_write,
93 .bind_all = tcp_bind_listeners,
94 .unbind_all = unbind_all_listeners,
95 .enable_all = enable_all_listeners,
96 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
97 .nb_listeners = 0,
98};
99
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100100
101/* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which
102 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
103 * - 0 : ignore remote address (may even be a NULL pointer)
104 * - 1 : use provided address
105 * - 2 : use provided port
106 * - 3 : use both
107 *
108 * The function supports multiple foreign binding methods :
109 * - linux_tproxy: we directly bind to the foreign address
110 * - cttproxy: we bind to a local address then nat.
111 * The second one can be used as a fallback for the first one.
112 * This function returns 0 when everything's OK, 1 if it could not bind, to the
113 * local address, 2 if it could not bind to the foreign address.
114 */
115int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote)
116{
117 struct sockaddr_in bind_addr;
118 int foreign_ok = 0;
119 int ret;
120
121#ifdef CONFIG_HAP_LINUX_TPROXY
122 static int ip_transp_working = 1;
123 if (flags && ip_transp_working) {
124 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0
125 || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0)
126 foreign_ok = 1;
127 else
128 ip_transp_working = 0;
129 }
130#endif
131 if (flags) {
132 memset(&bind_addr, 0, sizeof(bind_addr));
133 if (flags & 1)
134 bind_addr.sin_addr = remote->sin_addr;
135 if (flags & 2)
136 bind_addr.sin_port = remote->sin_port;
137 }
138
139 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
140 if (foreign_ok) {
141 ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
142 if (ret < 0)
143 return 2;
144 }
145 else {
146 ret = bind(fd, (struct sockaddr *)local, sizeof(*local));
147 if (ret < 0)
148 return 1;
149 }
150
151 if (!flags)
152 return 0;
153
154#ifdef CONFIG_HAP_CTTPROXY
155 if (!foreign_ok) {
156 struct in_tproxy itp1, itp2;
157 memset(&itp1, 0, sizeof(itp1));
158
159 itp1.op = TPROXY_ASSIGN;
160 itp1.v.addr.faddr = bind_addr.sin_addr;
161 itp1.v.addr.fport = bind_addr.sin_port;
162
163 /* set connect flag on socket */
164 itp2.op = TPROXY_FLAGS;
165 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
166
167 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
168 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
169 foreign_ok = 1;
170 }
171 }
172#endif
173 if (!foreign_ok)
174 /* we could not bind to a foreign address */
175 return 2;
176
177 return 0;
178}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100179
Willy Tarreau9650f372009-08-16 14:02:45 +0200180
181/*
182 * This function initiates a connection to the server assigned to this session
Willy Tarreaub1d67742010-03-29 19:36:59 +0200183 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. A
184 * source address may be pointed to by <from_addr>. Note that this is only used
185 * in case of transparent proxying. Normal source bind addresses are still
186 * determined locally (due to the possible need of a source port).
187 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200188 * It can return one of :
189 * - SN_ERR_NONE if everything's OK
190 * - SN_ERR_SRVTO if there are no more servers
191 * - SN_ERR_SRVCL if the connection was refused by the server
192 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
193 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
194 * - SN_ERR_INTERNAL for any other purely internal errors
195 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
196 */
197int tcpv4_connect_server(struct stream_interface *si,
198 struct proxy *be, struct server *srv,
Willy Tarreaub1d67742010-03-29 19:36:59 +0200199 struct sockaddr *srv_addr, struct sockaddr *from_addr)
Willy Tarreau9650f372009-08-16 14:02:45 +0200200{
201 int fd;
202
203 if ((fd = si->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
204 qfprintf(stderr, "Cannot get a server socket.\n");
205
206 if (errno == ENFILE)
207 send_log(be, LOG_EMERG,
208 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
209 be->id, maxfd);
210 else if (errno == EMFILE)
211 send_log(be, LOG_EMERG,
212 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
213 be->id, maxfd);
214 else if (errno == ENOBUFS || errno == ENOMEM)
215 send_log(be, LOG_EMERG,
216 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
217 be->id, maxfd);
218 /* this is a resource error */
219 return SN_ERR_RESOURCE;
220 }
221
222 if (fd >= global.maxsock) {
223 /* do not log anything there, it's a normal condition when this option
224 * is used to serialize connections to a server !
225 */
226 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
227 close(fd);
228 return SN_ERR_PRXCOND; /* it is a configuration limit */
229 }
230
231 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
232 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
233 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
234 close(fd);
235 return SN_ERR_INTERNAL;
236 }
237
238 if (be->options & PR_O_TCP_SRV_KA)
239 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
240
241 if (be->options & PR_O_TCP_NOLING)
242 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
243
244 /* allow specific binding :
245 * - server-specific at first
246 * - proxy-specific next
247 */
248 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200249 int ret, flags = 0;
250
Willy Tarreau9650f372009-08-16 14:02:45 +0200251 switch (srv->state & SRV_TPROXY_MASK) {
252 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200253 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200254 flags = 3;
255 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200256 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200257 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200258 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200259 break;
260 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200261
Willy Tarreau9650f372009-08-16 14:02:45 +0200262#ifdef SO_BINDTODEVICE
263 /* Note: this might fail if not CAP_NET_RAW */
264 if (srv->iface_name)
265 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
266#endif
267
268 if (srv->sport_range) {
269 int attempts = 10; /* should be more than enough to find a spare port */
270 struct sockaddr_in src;
271
272 ret = 1;
273 src = srv->source_addr;
274
275 do {
276 /* note: in case of retry, we may have to release a previously
277 * allocated port, hence this loop's construct.
278 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200279 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
280 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200281
282 if (!attempts)
283 break;
284 attempts--;
285
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200286 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
287 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200288 break;
289
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200290 fdinfo[fd].port_range = srv->sport_range;
291 src.sin_port = htons(fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200292
Willy Tarreaub1d67742010-03-29 19:36:59 +0200293 ret = tcpv4_bind_socket(fd, flags, &src, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200294 } while (ret != 0); /* binding NOK */
295 }
296 else {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200297 ret = tcpv4_bind_socket(fd, flags, &srv->source_addr, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200298 }
299
300 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200301 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
302 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200303 close(fd);
304
305 if (ret == 1) {
306 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
307 be->id, srv->id);
308 send_log(be, LOG_EMERG,
309 "Cannot bind to source address before connect() for server %s/%s.\n",
310 be->id, srv->id);
311 } else {
312 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
313 be->id, srv->id);
314 send_log(be, LOG_EMERG,
315 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
316 be->id, srv->id);
317 }
318 return SN_ERR_RESOURCE;
319 }
320 }
321 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200322 int ret, flags = 0;
323
Willy Tarreau9650f372009-08-16 14:02:45 +0200324 switch (be->options & PR_O_TPXY_MASK) {
325 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200326 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200327 flags = 3;
328 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200329 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200330 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200331 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200332 break;
333 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200334
Willy Tarreau9650f372009-08-16 14:02:45 +0200335#ifdef SO_BINDTODEVICE
336 /* Note: this might fail if not CAP_NET_RAW */
337 if (be->iface_name)
338 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
339#endif
Willy Tarreaub1d67742010-03-29 19:36:59 +0200340 ret = tcpv4_bind_socket(fd, flags, &be->source_addr, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200341 if (ret) {
342 close(fd);
343 if (ret == 1) {
344 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
345 be->id);
346 send_log(be, LOG_EMERG,
347 "Cannot bind to source address before connect() for proxy %s.\n",
348 be->id);
349 } else {
350 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
351 be->id);
352 send_log(be, LOG_EMERG,
353 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
354 be->id);
355 }
356 return SN_ERR_RESOURCE;
357 }
358 }
359
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400360#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200361 /* disabling tcp quick ack now allows the first request to leave the
362 * machine with the first ACK. We only do this if there are pending
363 * data in the buffer.
364 */
365 if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400366 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200367#endif
368
Willy Tarreaue803de22010-01-21 17:43:04 +0100369 if (global.tune.server_sndbuf)
370 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
371
372 if (global.tune.server_rcvbuf)
373 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
374
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 if ((connect(fd, (struct sockaddr *)srv_addr, sizeof(struct sockaddr_in)) == -1) &&
376 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
377
378 if (errno == EAGAIN || errno == EADDRINUSE) {
379 char *msg;
380 if (errno == EAGAIN) /* no free ports left, try again later */
381 msg = "no free ports";
382 else
383 msg = "local address already in use";
384
385 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200386 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
387 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200388 close(fd);
389 send_log(be, LOG_EMERG,
390 "Connect() failed for server %s/%s: %s.\n",
391 be->id, srv->id, msg);
392 return SN_ERR_RESOURCE;
393 } else if (errno == ETIMEDOUT) {
394 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200395 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
396 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200397 close(fd);
398 return SN_ERR_SRVTO;
399 } else {
400 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
401 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200402 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
403 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200404 close(fd);
405 return SN_ERR_SRVCL;
406 }
407 }
408
409 fdtab[fd].owner = si;
410 fdtab[fd].state = FD_STCONN; /* connection in progress */
411 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
412 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
413 fdtab[fd].cb[DIR_RD].b = si->ib;
414 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
415 fdtab[fd].cb[DIR_WR].b = si->ob;
416
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200417 fdinfo[fd].peeraddr = (struct sockaddr *)srv_addr;
418 fdinfo[fd].peerlen = sizeof(struct sockaddr_in);
Willy Tarreau9650f372009-08-16 14:02:45 +0200419
420 fd_insert(fd);
421 EV_FD_SET(fd, DIR_WR); /* for connect status */
422
423 si->state = SI_ST_CON;
424 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
425 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
426
427 return SN_ERR_NONE; /* connection is OK */
428}
429
430
Willy Tarreaue6b98942007-10-29 01:09:36 +0100431/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
432 * an error message in <err> if the message is at most <errlen> bytes long
433 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
434 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
435 * was alright and that no message was returned. ERR_RETRYABLE means that an
436 * error occurred but that it may vanish after a retry (eg: port in use), and
437 * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter
438 * the meaning of the error, but just indicate that a message is present which
439 * should be displayed with the respective level. Last, ERR_ABORT indicates
440 * that it's pointless to try to start other listeners. No error message is
441 * returned if errlen is NULL.
442 */
443int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
444{
445 __label__ tcp_return, tcp_close_return;
446 int fd, err;
447 const char *msg = NULL;
448
449 /* ensure we never return garbage */
450 if (errmsg && errlen)
451 *errmsg = 0;
452
453 if (listener->state != LI_ASSIGNED)
454 return ERR_NONE; /* already bound */
455
456 err = ERR_NONE;
457
458 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
459 err |= ERR_RETRYABLE | ERR_ALERT;
460 msg = "cannot create listening socket";
461 goto tcp_return;
462 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100463
Willy Tarreaue6b98942007-10-29 01:09:36 +0100464 if (fd >= global.maxsock) {
465 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
466 msg = "not enough free sockets (raise '-n' parameter)";
467 goto tcp_close_return;
468 }
469
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200470 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100471 err |= ERR_FATAL | ERR_ALERT;
472 msg = "cannot make socket non-blocking";
473 goto tcp_close_return;
474 }
475
476 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
477 /* not fatal but should be reported */
478 msg = "cannot do so_reuseaddr";
479 err |= ERR_ALERT;
480 }
481
482 if (listener->options & LI_O_NOLINGER)
483 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100484
Willy Tarreaue6b98942007-10-29 01:09:36 +0100485#ifdef SO_REUSEPORT
486 /* OpenBSD supports this. As it's present in old libc versions of Linux,
487 * it might return an error that we will silently ignore.
488 */
489 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
490#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100491#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100492 if ((listener->options & LI_O_FOREIGN)
Willy Tarreau0a459892008-01-13 17:37:16 +0100493 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1)
494 && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100495 msg = "cannot make listening socket transparent";
496 err |= ERR_ALERT;
497 }
498#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100499#ifdef SO_BINDTODEVICE
500 /* Note: this might fail if not CAP_NET_RAW */
501 if (listener->interface) {
502 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100503 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100504 msg = "cannot bind listener to device";
505 err |= ERR_WARN;
506 }
507 }
508#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400509#if defined(TCP_MAXSEG)
Willy Tarreaube1b9182009-06-14 18:48:19 +0200510 if (listener->maxseg) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400511 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200512 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
513 msg = "cannot set MSS";
514 err |= ERR_WARN;
515 }
516 }
517#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200518#if defined(TCP_DEFER_ACCEPT)
519 if (listener->options & LI_O_DEF_ACCEPT) {
520 /* defer accept by up to one second */
521 int accept_delay = 1;
522 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
523 msg = "cannot enable DEFER_ACCEPT";
524 err |= ERR_WARN;
525 }
526 }
527#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100528 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
529 err |= ERR_RETRYABLE | ERR_ALERT;
530 msg = "cannot bind socket";
531 goto tcp_close_return;
532 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100533
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100534 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100535 err |= ERR_RETRYABLE | ERR_ALERT;
536 msg = "cannot listen to socket";
537 goto tcp_close_return;
538 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100539
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400540#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200541 if (listener->options & LI_O_NOQUICKACK)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400542 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200543#endif
544
Willy Tarreaue6b98942007-10-29 01:09:36 +0100545 /* the socket is ready */
546 listener->fd = fd;
547 listener->state = LI_LISTEN;
548
549 /* the function for the accept() event */
550 fd_insert(fd);
551 fdtab[fd].cb[DIR_RD].f = listener->accept;
552 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
553 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200554 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100555 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200556 fdtab[fd].flags = FD_FL_TCP;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200557 if (listener->options & LI_O_NOLINGER)
558 fdtab[fd].flags |= FD_FL_TCP_NOLING;
559
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200560 fdinfo[fd].peeraddr = NULL;
561 fdinfo[fd].peerlen = 0;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100562 tcp_return:
563 if (msg && errlen)
564 strlcpy2(errmsg, msg, errlen);
565 return err;
566
567 tcp_close_return:
568 close(fd);
569 goto tcp_return;
570}
571
572/* This function creates all TCP sockets bound to the protocol entry <proto>.
573 * It is intended to be used as the protocol's bind_all() function.
574 * The sockets will be registered but not added to any fd_set, in order not to
575 * loose them across the fork(). A call to enable_all_listeners() is needed
576 * to complete initialization. The return value is composed from ERR_*.
577 */
578static int tcp_bind_listeners(struct protocol *proto)
579{
580 struct listener *listener;
581 int err = ERR_NONE;
582
583 list_for_each_entry(listener, &proto->listeners, proto_list) {
584 err |= tcp_bind_listener(listener, NULL, 0);
585 if ((err & ERR_CODE) == ERR_ABORT)
586 break;
587 }
588
589 return err;
590}
591
592/* Add listener to the list of tcpv4 listeners. The listener's state
593 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
594 * listeners is updated. This is the function to use to add a new listener.
595 */
596void tcpv4_add_listener(struct listener *listener)
597{
598 if (listener->state != LI_INIT)
599 return;
600 listener->state = LI_ASSIGNED;
601 listener->proto = &proto_tcpv4;
602 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
603 proto_tcpv4.nb_listeners++;
604}
605
606/* Add listener to the list of tcpv4 listeners. The listener's state
607 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
608 * listeners is updated. This is the function to use to add a new listener.
609 */
610void tcpv6_add_listener(struct listener *listener)
611{
612 if (listener->state != LI_INIT)
613 return;
614 listener->state = LI_ASSIGNED;
615 listener->proto = &proto_tcpv6;
616 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
617 proto_tcpv6.nb_listeners++;
618}
619
Willy Tarreauedcf6682008-11-30 23:15:34 +0100620/* This function performs the TCP request analysis on the current request. It
621 * returns 1 if the processing can continue on next analysers, or zero if it
622 * needs more data, encounters an error, or wants to immediately abort the
623 * request. It relies on buffers flags, and updates s->req->analysers. Its
624 * behaviour is rather simple:
Willy Tarreau86ef7dc2009-03-15 22:55:47 +0100625 * - the analyser should check for errors and timeouts, and react as expected.
626 * It does not have to close anything upon error, the caller will. Note that
627 * the caller also knows how to report errors and timeouts.
628 * - if the analyser does not have enough data, it must return 0 without calling
Willy Tarreauedcf6682008-11-30 23:15:34 +0100629 * other ones. It should also probably do a buffer_write_dis() to ensure
630 * that unprocessed data will not be forwarded. But that probably depends on
631 * the protocol.
632 * - if an analyser has enough data, it just has to pass on to the next
633 * analyser without using buffer_write_dis() (enabled by default).
634 * - if an analyser thinks it has no added value anymore staying here, it must
635 * reset its bit from the analysers flags in order not to be called anymore.
636 *
637 * In the future, analysers should be able to indicate that they want to be
638 * called after XXX bytes have been received (or transfered), and the min of
639 * all's wishes will be used to ring back (unless a special condition occurs).
640 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200641int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100642{
643 struct tcp_rule *rule;
644 int partial;
645
646 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
647 now_ms, __FUNCTION__,
648 s,
649 req,
650 req->rex, req->wex,
651 req->flags,
652 req->l,
653 req->analysers);
654
Willy Tarreauedcf6682008-11-30 23:15:34 +0100655 /* We don't know whether we have enough data, so must proceed
656 * this way :
657 * - iterate through all rules in their declaration order
658 * - if one rule returns MISS, it means the inspect delay is
659 * not over yet, then return immediately, otherwise consider
660 * it as a non-match.
661 * - if one rule returns OK, then return OK
662 * - if one rule returns KO, then return KO
663 */
664
Willy Tarreaud869b242009-03-15 14:43:58 +0100665 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 +0100666 partial = 0;
667 else
668 partial = ACL_PARTIAL;
669
670 list_for_each_entry(rule, &s->fe->tcp_req.inspect_rules, list) {
671 int ret = ACL_PAT_PASS;
672
673 if (rule->cond) {
Willy Tarreau51d5dad2009-07-12 10:10:05 +0200674 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100675 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200676 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100677 /* just set the request timeout once at the beginning of the request */
Willy Tarreaud869b242009-03-15 14:43:58 +0100678 if (!tick_isset(req->analyse_exp) && s->fe->tcp_req.inspect_delay)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100679 req->analyse_exp = tick_add_ifset(now_ms, s->fe->tcp_req.inspect_delay);
680 return 0;
681 }
682
683 ret = acl_pass(ret);
684 if (rule->cond->pol == ACL_COND_UNLESS)
685 ret = !ret;
686 }
687
688 if (ret) {
689 /* we have a matching rule. */
690 if (rule->action == TCP_ACT_REJECT) {
691 buffer_abort(req);
692 buffer_abort(s->rep);
693 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200694
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200695 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200696 if (s->listener->counters)
697 s->listener->counters->failed_req++;
698
Willy Tarreauedcf6682008-11-30 23:15:34 +0100699 if (!(s->flags & SN_ERR_MASK))
700 s->flags |= SN_ERR_PRXCOND;
701 if (!(s->flags & SN_FINST_MASK))
702 s->flags |= SN_FINST_R;
703 return 0;
704 }
705 /* otherwise accept */
706 break;
707 }
708 }
709
710 /* if we get there, it means we have no rule which matches, or
711 * we have an explicit accept, so we apply the default accept.
712 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200713 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100714 req->analyse_exp = TICK_ETERNITY;
715 return 1;
716}
717
Emeric Brun647caf12009-06-30 17:57:00 +0200718/* Apply RDP cookie persistence to the current session. For this, the function
719 * tries to extract an RDP cookie from the request buffer, and look for the
720 * matching server in the list. If the server is found, it is assigned to the
721 * session. This always returns 1, and the analyser removes itself from the
722 * list. Nothing is performed if a server was already assigned.
723 */
724int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
725{
726 struct proxy *px = s->be;
727 int ret;
728 struct acl_expr expr;
729 struct acl_test test;
730 struct server *srv = px->srv;
731 struct sockaddr_in addr;
732 char *p;
733
734 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
735 now_ms, __FUNCTION__,
736 s,
737 req,
738 req->rex, req->wex,
739 req->flags,
740 req->l,
741 req->analysers);
742
743 if (s->flags & SN_ASSIGNED)
744 goto no_cookie;
745
746 memset(&expr, 0, sizeof(expr));
747 memset(&test, 0, sizeof(test));
748
749 expr.arg.str = s->be->rdp_cookie_name;
750 expr.arg_len = s->be->rdp_cookie_len;
751
752 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test);
753 if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0)
754 goto no_cookie;
755
756 memset(&addr, 0, sizeof(addr));
757 addr.sin_family = AF_INET;
758
759 /* Considering an rdp cookie detected using acl, test.ptr ended with <cr><lf> and should return */
760 addr.sin_addr.s_addr = strtoul(test.ptr, &p, 10);
761 if (*p != '.')
762 goto no_cookie;
763 p++;
764 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
765 if (*p != '.')
766 goto no_cookie;
767
768 while (srv) {
769 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
770 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
771 /* we found the server and it is usable */
772 s->flags |= SN_DIRECT | SN_ASSIGNED;
773 s->srv = srv;
774 break;
775 }
776 }
777 srv = srv->next;
778 }
779
780no_cookie:
781 req->analysers &= ~an_bit;
782 req->analyse_exp = TICK_ETERNITY;
783 return 1;
784}
785
Willy Tarreauedcf6682008-11-30 23:15:34 +0100786
Willy Tarreaub6866442008-07-14 23:54:42 +0200787/* This function should be called to parse a line starting with the "tcp-request"
788 * keyword.
789 */
790static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
791 struct proxy *defpx, char *err, int errlen)
792{
793 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200794 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +0200795 int retlen;
796
797 if (!*args[1]) {
798 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
799 args[0], proxy_type_str(proxy), curpx->id);
800 return -1;
801 }
802
803 if (!strcmp(args[1], "inspect-delay")) {
804 if (curpx == defpx) {
805 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
806 args[0], args[1]);
807 return -1;
808 }
809
810 if (!(curpx->cap & PR_CAP_FE)) {
811 snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability",
812 args[0], args[1], proxy_type_str(proxy), curpx->id,
813 "frontend");
814 return 1;
815 }
816
817 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
818 retlen = snprintf(err, errlen,
819 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
820 args[0], args[1], proxy_type_str(proxy), curpx->id);
821 if (ptr && retlen < errlen)
822 retlen += snprintf(err+retlen, errlen - retlen,
823 " (unexpected character '%c')", *ptr);
824 return -1;
825 }
826
827 if (curpx->tcp_req.inspect_delay) {
828 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
829 args[0], args[1], proxy_type_str(proxy), curpx->id);
830 return 1;
831 }
832 curpx->tcp_req.inspect_delay = val;
833 return 0;
834 }
835
836 if (!strcmp(args[1], "content")) {
837 int action;
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200838 int warn = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +0200839 int pol = ACL_COND_NONE;
840 struct acl_cond *cond;
841 struct tcp_rule *rule;
842
843 if (curpx == defpx) {
844 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
845 args[0], args[1]);
846 return -1;
847 }
848
849 if (!strcmp(args[2], "accept"))
850 action = TCP_ACT_ACCEPT;
851 else if (!strcmp(args[2], "reject"))
852 action = TCP_ACT_REJECT;
853 else {
854 retlen = snprintf(err, errlen,
855 "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')",
856 args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]);
857 return -1;
858 }
859
860 pol = ACL_COND_NONE;
861 cond = NULL;
862
Willy Tarreauef6494c2010-01-28 17:12:36 +0100863 if (strcmp(args[3], "if") == 0 || strcmp(args[3], "unless") == 0) {
864 if ((cond = build_acl_cond(NULL, 0, curpx, (const char **)args+3)) == NULL) {
865 retlen = snprintf(err, errlen,
866 "error detected in %s '%s' while parsing '%s' condition",
867 proxy_type_str(curpx), curpx->id, args[3]);
868 return -1;
869 }
870 }
871 else if (*args[3]) {
Willy Tarreau606ad732009-07-14 21:17:05 +0200872 retlen = snprintf(err, errlen,
873 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
874 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[3]);
875 return -1;
876 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200877
Willy Tarreau1a211942009-07-14 13:53:17 +0200878 if (cond && (cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200879 struct acl *acl;
880 const char *name;
881
Willy Tarreau1a211942009-07-14 13:53:17 +0200882 acl = cond_find_require(cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200883 name = acl ? acl->name : "(unknown)";
884
885 retlen = snprintf(err, errlen,
Willy Tarreau1a211942009-07-14 13:53:17 +0200886 "acl '%s' involves some response-only criteria which will be ignored.",
887 name);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200888 warn++;
889 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200890 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
891 rule->cond = cond;
892 rule->action = action;
893 LIST_INIT(&rule->list);
894 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200895 return warn;
Willy Tarreaub6866442008-07-14 23:54:42 +0200896 }
897
898 snprintf(err, errlen, "unknown argument '%s' after '%s' in %s '%s'",
899 args[1], args[0], proxy_type_str(proxy), curpx->id);
900 return -1;
901}
902
903/* return the number of bytes in the request buffer */
904static int
905acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
906 struct acl_expr *expr, struct acl_test *test)
907{
908 if (!l4 || !l4->req)
909 return 0;
910
911 test->i = l4->req->l;
912 test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE;
913 return 1;
914}
915
Willy Tarreau655e26a2008-07-15 18:58:05 +0200916/* Return the version of the SSL protocol in the request. It supports both
917 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
918 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
919 * SSLv2 format is described here, and completed p67 of RFC 2246 :
920 * http://wp.netscape.com/eng/security/SSL_2.html
921 *
922 * Note: this decoder only works with non-wrapping data.
923 */
924static int
925acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
926 struct acl_expr *expr, struct acl_test *test)
927{
928 int version, bleft, msg_len;
929 const unsigned char *data;
930
931 if (!l4 || !l4->req)
932 return 0;
933
934 msg_len = 0;
935 bleft = l4->req->l;
936 if (!bleft)
937 goto too_short;
938
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200939 data = (const unsigned char *)l4->req->w;
Willy Tarreau655e26a2008-07-15 18:58:05 +0200940 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
941 /* SSLv3 header format */
942 if (bleft < 5)
943 goto too_short;
944
945 version = (data[1] << 16) + data[2]; /* version: major, minor */
946 msg_len = (data[3] << 8) + data[4]; /* record length */
947
948 /* format introduced with SSLv3 */
949 if (version < 0x00030000)
950 goto not_ssl;
951
952 /* message length between 1 and 2^14 + 2048 */
953 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
954 goto not_ssl;
955
956 bleft -= 5; data += 5;
957 } else {
958 /* SSLv2 header format, only supported for hello (msg type 1) */
959 int rlen, plen, cilen, silen, chlen;
960
961 if (*data & 0x80) {
962 if (bleft < 3)
963 goto too_short;
964 /* short header format : 15 bits for length */
965 rlen = ((data[0] & 0x7F) << 8) | data[1];
966 plen = 0;
967 bleft -= 2; data += 2;
968 } else {
969 if (bleft < 4)
970 goto too_short;
971 /* long header format : 14 bits for length + pad length */
972 rlen = ((data[0] & 0x3F) << 8) | data[1];
973 plen = data[2];
974 bleft -= 3; data += 2;
975 }
976
977 if (*data != 0x01)
978 goto not_ssl;
979 bleft--; data++;
980
981 if (bleft < 8)
982 goto too_short;
983 version = (data[0] << 16) + data[1]; /* version: major, minor */
984 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
985 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
986 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
987
988 bleft -= 8; data += 8;
989 if (cilen % 3 != 0)
990 goto not_ssl;
991 if (silen && silen != 16)
992 goto not_ssl;
993 if (chlen < 16 || chlen > 32)
994 goto not_ssl;
995 if (rlen != 9 + cilen + silen + chlen)
996 goto not_ssl;
997
998 /* focus on the remaining data length */
999 msg_len = cilen + silen + chlen + plen;
1000 }
1001 /* We could recursively check that the buffer ends exactly on an SSL
1002 * fragment boundary and that a possible next segment is still SSL,
1003 * but that's a bit pointless. However, we could still check that
1004 * all the part of the request which fits in a buffer is already
1005 * there.
1006 */
Willy Tarreau7c3c5412009-12-13 15:53:05 +01001007 if (msg_len > buffer_max_len(l4->req) + l4->req->data - l4->req->w)
1008 msg_len = buffer_max_len(l4->req) + l4->req->data - l4->req->w;
Willy Tarreau655e26a2008-07-15 18:58:05 +02001009
1010 if (bleft < msg_len)
1011 goto too_short;
1012
1013 /* OK that's enough. We have at least the whole message, and we have
1014 * the protocol version.
1015 */
1016 test->i = version;
1017 test->flags = ACL_TEST_F_VOLATILE;
1018 return 1;
1019
1020 too_short:
1021 test->flags = ACL_TEST_F_MAY_CHANGE;
1022 not_ssl:
1023 return 0;
1024}
1025
Emeric Brunbede3d02009-06-30 17:54:00 +02001026int
1027acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
1028 struct acl_expr *expr, struct acl_test *test)
1029{
1030 int bleft;
1031 const unsigned char *data;
1032
1033 if (!l4 || !l4->req)
1034 return 0;
1035
1036 test->flags = 0;
1037
1038 bleft = l4->req->l;
1039 if (bleft <= 11)
1040 goto too_short;
1041
1042 data = (const unsigned char *)l4->req->w + 11;
1043 bleft -= 11;
1044
1045 if (bleft <= 7)
1046 goto too_short;
1047
1048 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1049 goto not_cookie;
1050
1051 data += 7;
1052 bleft -= 7;
1053
1054 while (bleft > 0 && *data == ' ') {
1055 data++;
1056 bleft--;
1057 }
1058
1059 if (expr->arg_len) {
1060
1061 if (bleft <= expr->arg_len)
1062 goto too_short;
1063
1064 if ((data[expr->arg_len] != '=') ||
1065 strncasecmp(expr->arg.str, (const char *)data, expr->arg_len) != 0)
1066 goto not_cookie;
1067
1068 data += expr->arg_len + 1;
1069 bleft -= expr->arg_len + 1;
1070 } else {
1071 while (bleft > 0 && *data != '=') {
1072 if (*data == '\r' || *data == '\n')
1073 goto not_cookie;
1074 data++;
1075 bleft--;
1076 }
1077
1078 if (bleft < 1)
1079 goto too_short;
1080
1081 if (*data != '=')
1082 goto not_cookie;
1083
1084 data++;
1085 bleft--;
1086 }
1087
1088 /* data points to cookie value */
1089 test->ptr = (char *)data;
1090 test->len = 0;
1091
1092 while (bleft > 0 && *data != '\r') {
1093 data++;
1094 bleft--;
1095 }
1096
1097 if (bleft < 2)
1098 goto too_short;
1099
1100 if (data[0] != '\r' || data[1] != '\n')
1101 goto not_cookie;
1102
1103 test->len = (char *)data - test->ptr;
1104 test->flags = ACL_TEST_F_VOLATILE;
1105 return 1;
1106
1107 too_short:
1108 test->flags = ACL_TEST_F_MAY_CHANGE;
1109 not_cookie:
1110 return 0;
1111}
1112
1113static int
1114acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
1115 struct acl_expr *expr, struct acl_test *test)
1116{
1117 int ret;
1118
1119 ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, test);
1120
1121 test->ptr = NULL;
1122 test->len = 0;
1123
1124 if (test->flags & ACL_TEST_F_MAY_CHANGE)
1125 return 0;
1126
1127 test->flags = ACL_TEST_F_VOLATILE;
1128 test->i = ret;
1129
1130 return 1;
1131}
Willy Tarreaub6866442008-07-14 23:54:42 +02001132
1133static struct cfg_kw_list cfg_kws = {{ },{
1134 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1135 { 0, NULL, NULL },
1136}};
1137
1138static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02001139 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L4REQ_VOLATILE },
1140 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L4REQ_VOLATILE },
Willy Tarreauc4262962010-05-10 23:42:40 +02001141 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L4REQ_VOLATILE|ACL_MAY_LOOKUP },
Emeric Brunbede3d02009-06-30 17:54:00 +02001142 { "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 +02001143 { NULL, NULL, NULL, NULL },
1144}};
1145
Willy Tarreaue6b98942007-10-29 01:09:36 +01001146__attribute__((constructor))
1147static void __tcp_protocol_init(void)
1148{
1149 protocol_register(&proto_tcpv4);
1150 protocol_register(&proto_tcpv6);
Willy Tarreaub6866442008-07-14 23:54:42 +02001151 cfg_register_keywords(&cfg_kws);
1152 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001153}
1154
1155
1156/*
1157 * Local variables:
1158 * c-indent-level: 8
1159 * c-basic-offset: 8
1160 * End:
1161 */