blob: 4d0ae35596d875b25cd81b2d06db06fb87607b02 [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
Willy Tarreaub6866442008-07-14 23:54:42 +020027#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/errors.h>
32#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
35#include <common/time.h>
36#include <common/version.h>
37
Willy Tarreaue6b98942007-10-29 01:09:36 +010038#include <types/global.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039
40#include <proto/acl.h>
41#include <proto/backend.h>
42#include <proto/buffers.h>
43#include <proto/fd.h>
44#include <proto/protocols.h>
45#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020046#include <proto/proxy.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010047#include <proto/queue.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010048#include <proto/session.h>
49#include <proto/stream_sock.h>
50#include <proto/task.h>
51
Willy Tarreaue8c66af2008-01-13 18:40:14 +010052#ifdef CONFIG_HAP_CTTPROXY
53#include <import/ip_tproxy.h>
54#endif
55
Willy Tarreaue6b98942007-10-29 01:09:36 +010056static int tcp_bind_listeners(struct protocol *proto);
57
58/* Note: must not be declared <const> as its list will be overwritten */
59static struct protocol proto_tcpv4 = {
60 .name = "tcpv4",
61 .sock_domain = AF_INET,
62 .sock_type = SOCK_STREAM,
63 .sock_prot = IPPROTO_TCP,
64 .sock_family = AF_INET,
65 .sock_addrlen = sizeof(struct sockaddr_in),
66 .l3_addrlen = 32/8,
67 .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,
85 .read = &stream_sock_read,
86 .write = &stream_sock_write,
87 .bind_all = tcp_bind_listeners,
88 .unbind_all = unbind_all_listeners,
89 .enable_all = enable_all_listeners,
90 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
91 .nb_listeners = 0,
92};
93
Willy Tarreaue8c66af2008-01-13 18:40:14 +010094
95/* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which
96 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
97 * - 0 : ignore remote address (may even be a NULL pointer)
98 * - 1 : use provided address
99 * - 2 : use provided port
100 * - 3 : use both
101 *
102 * The function supports multiple foreign binding methods :
103 * - linux_tproxy: we directly bind to the foreign address
104 * - cttproxy: we bind to a local address then nat.
105 * The second one can be used as a fallback for the first one.
106 * This function returns 0 when everything's OK, 1 if it could not bind, to the
107 * local address, 2 if it could not bind to the foreign address.
108 */
109int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote)
110{
111 struct sockaddr_in bind_addr;
112 int foreign_ok = 0;
113 int ret;
114
115#ifdef CONFIG_HAP_LINUX_TPROXY
116 static int ip_transp_working = 1;
117 if (flags && ip_transp_working) {
118 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0
119 || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0)
120 foreign_ok = 1;
121 else
122 ip_transp_working = 0;
123 }
124#endif
125 if (flags) {
126 memset(&bind_addr, 0, sizeof(bind_addr));
127 if (flags & 1)
128 bind_addr.sin_addr = remote->sin_addr;
129 if (flags & 2)
130 bind_addr.sin_port = remote->sin_port;
131 }
132
133 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
134 if (foreign_ok) {
135 ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
136 if (ret < 0)
137 return 2;
138 }
139 else {
140 ret = bind(fd, (struct sockaddr *)local, sizeof(*local));
141 if (ret < 0)
142 return 1;
143 }
144
145 if (!flags)
146 return 0;
147
148#ifdef CONFIG_HAP_CTTPROXY
149 if (!foreign_ok) {
150 struct in_tproxy itp1, itp2;
151 memset(&itp1, 0, sizeof(itp1));
152
153 itp1.op = TPROXY_ASSIGN;
154 itp1.v.addr.faddr = bind_addr.sin_addr;
155 itp1.v.addr.fport = bind_addr.sin_port;
156
157 /* set connect flag on socket */
158 itp2.op = TPROXY_FLAGS;
159 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
160
161 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
162 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
163 foreign_ok = 1;
164 }
165 }
166#endif
167 if (!foreign_ok)
168 /* we could not bind to a foreign address */
169 return 2;
170
171 return 0;
172}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100173
174/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
175 * an error message in <err> if the message is at most <errlen> bytes long
176 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
177 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
178 * was alright and that no message was returned. ERR_RETRYABLE means that an
179 * error occurred but that it may vanish after a retry (eg: port in use), and
180 * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter
181 * the meaning of the error, but just indicate that a message is present which
182 * should be displayed with the respective level. Last, ERR_ABORT indicates
183 * that it's pointless to try to start other listeners. No error message is
184 * returned if errlen is NULL.
185 */
186int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
187{
188 __label__ tcp_return, tcp_close_return;
189 int fd, err;
190 const char *msg = NULL;
191
192 /* ensure we never return garbage */
193 if (errmsg && errlen)
194 *errmsg = 0;
195
196 if (listener->state != LI_ASSIGNED)
197 return ERR_NONE; /* already bound */
198
199 err = ERR_NONE;
200
201 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
202 err |= ERR_RETRYABLE | ERR_ALERT;
203 msg = "cannot create listening socket";
204 goto tcp_return;
205 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100206
Willy Tarreaue6b98942007-10-29 01:09:36 +0100207 if (fd >= global.maxsock) {
208 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
209 msg = "not enough free sockets (raise '-n' parameter)";
210 goto tcp_close_return;
211 }
212
213 if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) ||
214 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
215 (char *) &one, sizeof(one)) == -1)) {
216 err |= ERR_FATAL | ERR_ALERT;
217 msg = "cannot make socket non-blocking";
218 goto tcp_close_return;
219 }
220
221 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
222 /* not fatal but should be reported */
223 msg = "cannot do so_reuseaddr";
224 err |= ERR_ALERT;
225 }
226
227 if (listener->options & LI_O_NOLINGER)
228 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100229
Willy Tarreaue6b98942007-10-29 01:09:36 +0100230#ifdef SO_REUSEPORT
231 /* OpenBSD supports this. As it's present in old libc versions of Linux,
232 * it might return an error that we will silently ignore.
233 */
234 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
235#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100236#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100237 if ((listener->options & LI_O_FOREIGN)
Willy Tarreau0a459892008-01-13 17:37:16 +0100238 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1)
239 && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100240 msg = "cannot make listening socket transparent";
241 err |= ERR_ALERT;
242 }
243#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100244#ifdef SO_BINDTODEVICE
245 /* Note: this might fail if not CAP_NET_RAW */
246 if (listener->interface) {
247 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100248 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100249 msg = "cannot bind listener to device";
250 err |= ERR_WARN;
251 }
252 }
253#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100254 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
255 err |= ERR_RETRYABLE | ERR_ALERT;
256 msg = "cannot bind socket";
257 goto tcp_close_return;
258 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100259
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100260 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100261 err |= ERR_RETRYABLE | ERR_ALERT;
262 msg = "cannot listen to socket";
263 goto tcp_close_return;
264 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100265
Willy Tarreaue6b98942007-10-29 01:09:36 +0100266 /* the socket is ready */
267 listener->fd = fd;
268 listener->state = LI_LISTEN;
269
270 /* the function for the accept() event */
271 fd_insert(fd);
272 fdtab[fd].cb[DIR_RD].f = listener->accept;
273 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
274 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200275 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100276 fdtab[fd].state = FD_STLISTEN;
277 fdtab[fd].peeraddr = NULL;
278 fdtab[fd].peerlen = 0;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100279 tcp_return:
280 if (msg && errlen)
281 strlcpy2(errmsg, msg, errlen);
282 return err;
283
284 tcp_close_return:
285 close(fd);
286 goto tcp_return;
287}
288
289/* This function creates all TCP sockets bound to the protocol entry <proto>.
290 * It is intended to be used as the protocol's bind_all() function.
291 * The sockets will be registered but not added to any fd_set, in order not to
292 * loose them across the fork(). A call to enable_all_listeners() is needed
293 * to complete initialization. The return value is composed from ERR_*.
294 */
295static int tcp_bind_listeners(struct protocol *proto)
296{
297 struct listener *listener;
298 int err = ERR_NONE;
299
300 list_for_each_entry(listener, &proto->listeners, proto_list) {
301 err |= tcp_bind_listener(listener, NULL, 0);
302 if ((err & ERR_CODE) == ERR_ABORT)
303 break;
304 }
305
306 return err;
307}
308
309/* Add listener to the list of tcpv4 listeners. The listener's state
310 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
311 * listeners is updated. This is the function to use to add a new listener.
312 */
313void tcpv4_add_listener(struct listener *listener)
314{
315 if (listener->state != LI_INIT)
316 return;
317 listener->state = LI_ASSIGNED;
318 listener->proto = &proto_tcpv4;
319 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
320 proto_tcpv4.nb_listeners++;
321}
322
323/* Add listener to the list of tcpv4 listeners. The listener's state
324 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
325 * listeners is updated. This is the function to use to add a new listener.
326 */
327void tcpv6_add_listener(struct listener *listener)
328{
329 if (listener->state != LI_INIT)
330 return;
331 listener->state = LI_ASSIGNED;
332 listener->proto = &proto_tcpv6;
333 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
334 proto_tcpv6.nb_listeners++;
335}
336
Willy Tarreauedcf6682008-11-30 23:15:34 +0100337/* This function performs the TCP request analysis on the current request. It
338 * returns 1 if the processing can continue on next analysers, or zero if it
339 * needs more data, encounters an error, or wants to immediately abort the
340 * request. It relies on buffers flags, and updates s->req->analysers. Its
341 * behaviour is rather simple:
Willy Tarreau86ef7dc2009-03-15 22:55:47 +0100342 * - the analyser should check for errors and timeouts, and react as expected.
343 * It does not have to close anything upon error, the caller will. Note that
344 * the caller also knows how to report errors and timeouts.
345 * - if the analyser does not have enough data, it must return 0 without calling
Willy Tarreauedcf6682008-11-30 23:15:34 +0100346 * other ones. It should also probably do a buffer_write_dis() to ensure
347 * that unprocessed data will not be forwarded. But that probably depends on
348 * the protocol.
349 * - if an analyser has enough data, it just has to pass on to the next
350 * analyser without using buffer_write_dis() (enabled by default).
351 * - if an analyser thinks it has no added value anymore staying here, it must
352 * reset its bit from the analysers flags in order not to be called anymore.
353 *
354 * In the future, analysers should be able to indicate that they want to be
355 * called after XXX bytes have been received (or transfered), and the min of
356 * all's wishes will be used to ring back (unless a special condition occurs).
357 */
358int tcp_inspect_request(struct session *s, struct buffer *req)
359{
360 struct tcp_rule *rule;
361 int partial;
362
363 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
364 now_ms, __FUNCTION__,
365 s,
366 req,
367 req->rex, req->wex,
368 req->flags,
369 req->l,
370 req->analysers);
371
Willy Tarreauedcf6682008-11-30 23:15:34 +0100372 /* We don't know whether we have enough data, so must proceed
373 * this way :
374 * - iterate through all rules in their declaration order
375 * - if one rule returns MISS, it means the inspect delay is
376 * not over yet, then return immediately, otherwise consider
377 * it as a non-match.
378 * - if one rule returns OK, then return OK
379 * - if one rule returns KO, then return KO
380 */
381
Willy Tarreaud869b242009-03-15 14:43:58 +0100382 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 +0100383 partial = 0;
384 else
385 partial = ACL_PARTIAL;
386
387 list_for_each_entry(rule, &s->fe->tcp_req.inspect_rules, list) {
388 int ret = ACL_PAT_PASS;
389
390 if (rule->cond) {
391 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ | partial);
392 if (ret == ACL_PAT_MISS) {
393 buffer_write_dis(req);
394 /* just set the request timeout once at the beginning of the request */
Willy Tarreaud869b242009-03-15 14:43:58 +0100395 if (!tick_isset(req->analyse_exp) && s->fe->tcp_req.inspect_delay)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100396 req->analyse_exp = tick_add_ifset(now_ms, s->fe->tcp_req.inspect_delay);
397 return 0;
398 }
399
400 ret = acl_pass(ret);
401 if (rule->cond->pol == ACL_COND_UNLESS)
402 ret = !ret;
403 }
404
405 if (ret) {
406 /* we have a matching rule. */
407 if (rule->action == TCP_ACT_REJECT) {
408 buffer_abort(req);
409 buffer_abort(s->rep);
410 req->analysers = 0;
411 s->fe->failed_req++;
412 if (!(s->flags & SN_ERR_MASK))
413 s->flags |= SN_ERR_PRXCOND;
414 if (!(s->flags & SN_FINST_MASK))
415 s->flags |= SN_FINST_R;
416 return 0;
417 }
418 /* otherwise accept */
419 break;
420 }
421 }
422
423 /* if we get there, it means we have no rule which matches, or
424 * we have an explicit accept, so we apply the default accept.
425 */
426 req->analysers &= ~AN_REQ_INSPECT;
427 req->analyse_exp = TICK_ETERNITY;
428 return 1;
429}
430
431
Willy Tarreaub6866442008-07-14 23:54:42 +0200432/* This function should be called to parse a line starting with the "tcp-request"
433 * keyword.
434 */
435static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
436 struct proxy *defpx, char *err, int errlen)
437{
438 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200439 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +0200440 int retlen;
441
442 if (!*args[1]) {
443 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
444 args[0], proxy_type_str(proxy), curpx->id);
445 return -1;
446 }
447
448 if (!strcmp(args[1], "inspect-delay")) {
449 if (curpx == defpx) {
450 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
451 args[0], args[1]);
452 return -1;
453 }
454
455 if (!(curpx->cap & PR_CAP_FE)) {
456 snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability",
457 args[0], args[1], proxy_type_str(proxy), curpx->id,
458 "frontend");
459 return 1;
460 }
461
462 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
463 retlen = snprintf(err, errlen,
464 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
465 args[0], args[1], proxy_type_str(proxy), curpx->id);
466 if (ptr && retlen < errlen)
467 retlen += snprintf(err+retlen, errlen - retlen,
468 " (unexpected character '%c')", *ptr);
469 return -1;
470 }
471
472 if (curpx->tcp_req.inspect_delay) {
473 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
474 args[0], args[1], proxy_type_str(proxy), curpx->id);
475 return 1;
476 }
477 curpx->tcp_req.inspect_delay = val;
478 return 0;
479 }
480
481 if (!strcmp(args[1], "content")) {
482 int action;
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200483 int warn = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +0200484 int pol = ACL_COND_NONE;
485 struct acl_cond *cond;
486 struct tcp_rule *rule;
487
488 if (curpx == defpx) {
489 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
490 args[0], args[1]);
491 return -1;
492 }
493
494 if (!strcmp(args[2], "accept"))
495 action = TCP_ACT_ACCEPT;
496 else if (!strcmp(args[2], "reject"))
497 action = TCP_ACT_REJECT;
498 else {
499 retlen = snprintf(err, errlen,
500 "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')",
501 args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]);
502 return -1;
503 }
504
505 pol = ACL_COND_NONE;
506 cond = NULL;
507
508 if (!strcmp(args[3], "if"))
509 pol = ACL_COND_IF;
510 else if (!strcmp(args[3], "unless"))
511 pol = ACL_COND_UNLESS;
512
513 /* Note: we consider "if TRUE" when there is no condition */
514 if (pol != ACL_COND_NONE &&
515 (cond = parse_acl_cond((const char **)args+4, &curpx->acl, pol)) == NULL) {
516 retlen = snprintf(err, errlen,
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200517 "error detected in %s '%s' while parsing '%s' condition",
Willy Tarreaub6866442008-07-14 23:54:42 +0200518 proxy_type_str(curpx), curpx->id, args[3]);
519 return -1;
520 }
521
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200522 // FIXME: how to set this ?
523 // cond->line = linenum;
Willy Tarreau8e80e0b2009-05-10 12:05:46 +0200524 if (cond && cond->requires & (ACL_USE_RTR_ANY | ACL_USE_L7_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200525 struct acl *acl;
526 const char *name;
527
528 acl = cond_find_require(cond, ACL_USE_RTR_ANY|ACL_USE_L7_ANY);
529 name = acl ? acl->name : "(unknown)";
530
531 retlen = snprintf(err, errlen,
532 "acl '%s' involves some %s criteria which will be ignored.",
533 name,
534 (acl->requires & ACL_USE_RTR_ANY) ? "response-only" : "layer 7");
535 warn++;
536 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200537 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
538 rule->cond = cond;
539 rule->action = action;
540 LIST_INIT(&rule->list);
541 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200542 return warn;
Willy Tarreaub6866442008-07-14 23:54:42 +0200543 }
544
545 snprintf(err, errlen, "unknown argument '%s' after '%s' in %s '%s'",
546 args[1], args[0], proxy_type_str(proxy), curpx->id);
547 return -1;
548}
549
550/* return the number of bytes in the request buffer */
551static int
552acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
553 struct acl_expr *expr, struct acl_test *test)
554{
555 if (!l4 || !l4->req)
556 return 0;
557
558 test->i = l4->req->l;
559 test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE;
560 return 1;
561}
562
Willy Tarreau655e26a2008-07-15 18:58:05 +0200563/* Return the version of the SSL protocol in the request. It supports both
564 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
565 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
566 * SSLv2 format is described here, and completed p67 of RFC 2246 :
567 * http://wp.netscape.com/eng/security/SSL_2.html
568 *
569 * Note: this decoder only works with non-wrapping data.
570 */
571static int
572acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
573 struct acl_expr *expr, struct acl_test *test)
574{
575 int version, bleft, msg_len;
576 const unsigned char *data;
577
578 if (!l4 || !l4->req)
579 return 0;
580
581 msg_len = 0;
582 bleft = l4->req->l;
583 if (!bleft)
584 goto too_short;
585
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200586 data = (const unsigned char *)l4->req->w;
Willy Tarreau655e26a2008-07-15 18:58:05 +0200587 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
588 /* SSLv3 header format */
589 if (bleft < 5)
590 goto too_short;
591
592 version = (data[1] << 16) + data[2]; /* version: major, minor */
593 msg_len = (data[3] << 8) + data[4]; /* record length */
594
595 /* format introduced with SSLv3 */
596 if (version < 0x00030000)
597 goto not_ssl;
598
599 /* message length between 1 and 2^14 + 2048 */
600 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
601 goto not_ssl;
602
603 bleft -= 5; data += 5;
604 } else {
605 /* SSLv2 header format, only supported for hello (msg type 1) */
606 int rlen, plen, cilen, silen, chlen;
607
608 if (*data & 0x80) {
609 if (bleft < 3)
610 goto too_short;
611 /* short header format : 15 bits for length */
612 rlen = ((data[0] & 0x7F) << 8) | data[1];
613 plen = 0;
614 bleft -= 2; data += 2;
615 } else {
616 if (bleft < 4)
617 goto too_short;
618 /* long header format : 14 bits for length + pad length */
619 rlen = ((data[0] & 0x3F) << 8) | data[1];
620 plen = data[2];
621 bleft -= 3; data += 2;
622 }
623
624 if (*data != 0x01)
625 goto not_ssl;
626 bleft--; data++;
627
628 if (bleft < 8)
629 goto too_short;
630 version = (data[0] << 16) + data[1]; /* version: major, minor */
631 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
632 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
633 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
634
635 bleft -= 8; data += 8;
636 if (cilen % 3 != 0)
637 goto not_ssl;
638 if (silen && silen != 16)
639 goto not_ssl;
640 if (chlen < 16 || chlen > 32)
641 goto not_ssl;
642 if (rlen != 9 + cilen + silen + chlen)
643 goto not_ssl;
644
645 /* focus on the remaining data length */
646 msg_len = cilen + silen + chlen + plen;
647 }
648 /* We could recursively check that the buffer ends exactly on an SSL
649 * fragment boundary and that a possible next segment is still SSL,
650 * but that's a bit pointless. However, we could still check that
651 * all the part of the request which fits in a buffer is already
652 * there.
653 */
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100654 if (msg_len > l4->req->max_len + l4->req->data - l4->req->w)
655 msg_len = l4->req->max_len + l4->req->data - l4->req->w;
Willy Tarreau655e26a2008-07-15 18:58:05 +0200656
657 if (bleft < msg_len)
658 goto too_short;
659
660 /* OK that's enough. We have at least the whole message, and we have
661 * the protocol version.
662 */
663 test->i = version;
664 test->flags = ACL_TEST_F_VOLATILE;
665 return 1;
666
667 too_short:
668 test->flags = ACL_TEST_F_MAY_CHANGE;
669 not_ssl:
670 return 0;
671}
672
Willy Tarreaub6866442008-07-14 23:54:42 +0200673
674static struct cfg_kw_list cfg_kws = {{ },{
675 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
676 { 0, NULL, NULL },
677}};
678
679static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +0200680 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L4REQ_VOLATILE },
681 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L4REQ_VOLATILE },
Willy Tarreaub6866442008-07-14 23:54:42 +0200682 { NULL, NULL, NULL, NULL },
683}};
684
Willy Tarreaue6b98942007-10-29 01:09:36 +0100685__attribute__((constructor))
686static void __tcp_protocol_init(void)
687{
688 protocol_register(&proto_tcpv4);
689 protocol_register(&proto_tcpv6);
Willy Tarreaub6866442008-07-14 23:54:42 +0200690 cfg_register_keywords(&cfg_kws);
691 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100692}
693
694
695/*
696 * Local variables:
697 * c-indent-level: 8
698 * c-basic-offset: 8
699 * End:
700 */