blob: 94907ec762f3fc97e0dca623ba0cc73e41b59859 [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 Tarreaue6b98942007-10-29 01:09:36 +0100244 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
245 err |= ERR_RETRYABLE | ERR_ALERT;
246 msg = "cannot bind socket";
247 goto tcp_close_return;
248 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100249
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100250 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100251 err |= ERR_RETRYABLE | ERR_ALERT;
252 msg = "cannot listen to socket";
253 goto tcp_close_return;
254 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100255
Willy Tarreaue6b98942007-10-29 01:09:36 +0100256 /* the socket is ready */
257 listener->fd = fd;
258 listener->state = LI_LISTEN;
259
260 /* the function for the accept() event */
261 fd_insert(fd);
262 fdtab[fd].cb[DIR_RD].f = listener->accept;
263 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
264 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200265 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100266 fdtab[fd].state = FD_STLISTEN;
267 fdtab[fd].peeraddr = NULL;
268 fdtab[fd].peerlen = 0;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100269 tcp_return:
270 if (msg && errlen)
271 strlcpy2(errmsg, msg, errlen);
272 return err;
273
274 tcp_close_return:
275 close(fd);
276 goto tcp_return;
277}
278
279/* This function creates all TCP sockets bound to the protocol entry <proto>.
280 * It is intended to be used as the protocol's bind_all() function.
281 * The sockets will be registered but not added to any fd_set, in order not to
282 * loose them across the fork(). A call to enable_all_listeners() is needed
283 * to complete initialization. The return value is composed from ERR_*.
284 */
285static int tcp_bind_listeners(struct protocol *proto)
286{
287 struct listener *listener;
288 int err = ERR_NONE;
289
290 list_for_each_entry(listener, &proto->listeners, proto_list) {
291 err |= tcp_bind_listener(listener, NULL, 0);
292 if ((err & ERR_CODE) == ERR_ABORT)
293 break;
294 }
295
296 return err;
297}
298
299/* Add listener to the list of tcpv4 listeners. The listener's state
300 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
301 * listeners is updated. This is the function to use to add a new listener.
302 */
303void tcpv4_add_listener(struct listener *listener)
304{
305 if (listener->state != LI_INIT)
306 return;
307 listener->state = LI_ASSIGNED;
308 listener->proto = &proto_tcpv4;
309 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
310 proto_tcpv4.nb_listeners++;
311}
312
313/* Add listener to the list of tcpv4 listeners. The listener's state
314 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
315 * listeners is updated. This is the function to use to add a new listener.
316 */
317void tcpv6_add_listener(struct listener *listener)
318{
319 if (listener->state != LI_INIT)
320 return;
321 listener->state = LI_ASSIGNED;
322 listener->proto = &proto_tcpv6;
323 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
324 proto_tcpv6.nb_listeners++;
325}
326
Willy Tarreauedcf6682008-11-30 23:15:34 +0100327/* This function performs the TCP request analysis on the current request. It
328 * returns 1 if the processing can continue on next analysers, or zero if it
329 * needs more data, encounters an error, or wants to immediately abort the
330 * request. It relies on buffers flags, and updates s->req->analysers. Its
331 * behaviour is rather simple:
332 * - the analyser must check for errors and timeouts, and react as expected.
333 * It does not have to close anything upon error, the caller will.
334 * - if the analyser does not have enough data, it must return 0without calling
335 * other ones. It should also probably do a buffer_write_dis() to ensure
336 * that unprocessed data will not be forwarded. But that probably depends on
337 * the protocol.
338 * - if an analyser has enough data, it just has to pass on to the next
339 * analyser without using buffer_write_dis() (enabled by default).
340 * - if an analyser thinks it has no added value anymore staying here, it must
341 * reset its bit from the analysers flags in order not to be called anymore.
342 *
343 * In the future, analysers should be able to indicate that they want to be
344 * called after XXX bytes have been received (or transfered), and the min of
345 * all's wishes will be used to ring back (unless a special condition occurs).
346 */
347int tcp_inspect_request(struct session *s, struct buffer *req)
348{
349 struct tcp_rule *rule;
350 int partial;
351
352 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
353 now_ms, __FUNCTION__,
354 s,
355 req,
356 req->rex, req->wex,
357 req->flags,
358 req->l,
359 req->analysers);
360
361 /* We will abort if we encounter a read error. In theory, we
362 * should not abort if we get a close, it might be valid,
363 * although very unlikely. FIXME: we'll abort for now, this
364 * will be easier to change later.
365 */
366 if (req->flags & BF_READ_ERROR) {
367 req->analysers = 0;
368 s->fe->failed_req++;
369 if (!(s->flags & SN_ERR_MASK))
370 s->flags |= SN_ERR_CLICL;
371 if (!(s->flags & SN_FINST_MASK))
372 s->flags |= SN_FINST_R;
373 return 0;
374 }
375
376 /* Abort if client read timeout has expired */
377 else if (req->flags & BF_READ_TIMEOUT) {
378 req->analysers = 0;
379 s->fe->failed_req++;
380 if (!(s->flags & SN_ERR_MASK))
381 s->flags |= SN_ERR_CLITO;
382 if (!(s->flags & SN_FINST_MASK))
383 s->flags |= SN_FINST_R;
384 return 0;
385 }
386
387 /* We don't know whether we have enough data, so must proceed
388 * this way :
389 * - iterate through all rules in their declaration order
390 * - if one rule returns MISS, it means the inspect delay is
391 * not over yet, then return immediately, otherwise consider
392 * it as a non-match.
393 * - if one rule returns OK, then return OK
394 * - if one rule returns KO, then return KO
395 */
396
397 if (req->flags & BF_SHUTR || tick_is_expired(req->analyse_exp, now_ms))
398 partial = 0;
399 else
400 partial = ACL_PARTIAL;
401
402 list_for_each_entry(rule, &s->fe->tcp_req.inspect_rules, list) {
403 int ret = ACL_PAT_PASS;
404
405 if (rule->cond) {
406 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ | partial);
407 if (ret == ACL_PAT_MISS) {
408 buffer_write_dis(req);
409 /* just set the request timeout once at the beginning of the request */
410 if (!tick_isset(req->analyse_exp))
411 req->analyse_exp = tick_add_ifset(now_ms, s->fe->tcp_req.inspect_delay);
412 return 0;
413 }
414
415 ret = acl_pass(ret);
416 if (rule->cond->pol == ACL_COND_UNLESS)
417 ret = !ret;
418 }
419
420 if (ret) {
421 /* we have a matching rule. */
422 if (rule->action == TCP_ACT_REJECT) {
423 buffer_abort(req);
424 buffer_abort(s->rep);
425 req->analysers = 0;
426 s->fe->failed_req++;
427 if (!(s->flags & SN_ERR_MASK))
428 s->flags |= SN_ERR_PRXCOND;
429 if (!(s->flags & SN_FINST_MASK))
430 s->flags |= SN_FINST_R;
431 return 0;
432 }
433 /* otherwise accept */
434 break;
435 }
436 }
437
438 /* if we get there, it means we have no rule which matches, or
439 * we have an explicit accept, so we apply the default accept.
440 */
441 req->analysers &= ~AN_REQ_INSPECT;
442 req->analyse_exp = TICK_ETERNITY;
443 return 1;
444}
445
446
Willy Tarreaub6866442008-07-14 23:54:42 +0200447/* This function should be called to parse a line starting with the "tcp-request"
448 * keyword.
449 */
450static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
451 struct proxy *defpx, char *err, int errlen)
452{
453 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200454 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +0200455 int retlen;
456
457 if (!*args[1]) {
458 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
459 args[0], proxy_type_str(proxy), curpx->id);
460 return -1;
461 }
462
463 if (!strcmp(args[1], "inspect-delay")) {
464 if (curpx == defpx) {
465 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
466 args[0], args[1]);
467 return -1;
468 }
469
470 if (!(curpx->cap & PR_CAP_FE)) {
471 snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability",
472 args[0], args[1], proxy_type_str(proxy), curpx->id,
473 "frontend");
474 return 1;
475 }
476
477 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
478 retlen = snprintf(err, errlen,
479 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
480 args[0], args[1], proxy_type_str(proxy), curpx->id);
481 if (ptr && retlen < errlen)
482 retlen += snprintf(err+retlen, errlen - retlen,
483 " (unexpected character '%c')", *ptr);
484 return -1;
485 }
486
487 if (curpx->tcp_req.inspect_delay) {
488 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
489 args[0], args[1], proxy_type_str(proxy), curpx->id);
490 return 1;
491 }
492 curpx->tcp_req.inspect_delay = val;
493 return 0;
494 }
495
496 if (!strcmp(args[1], "content")) {
497 int action;
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200498 int warn = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +0200499 int pol = ACL_COND_NONE;
500 struct acl_cond *cond;
501 struct tcp_rule *rule;
502
503 if (curpx == defpx) {
504 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
505 args[0], args[1]);
506 return -1;
507 }
508
509 if (!strcmp(args[2], "accept"))
510 action = TCP_ACT_ACCEPT;
511 else if (!strcmp(args[2], "reject"))
512 action = TCP_ACT_REJECT;
513 else {
514 retlen = snprintf(err, errlen,
515 "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')",
516 args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]);
517 return -1;
518 }
519
520 pol = ACL_COND_NONE;
521 cond = NULL;
522
523 if (!strcmp(args[3], "if"))
524 pol = ACL_COND_IF;
525 else if (!strcmp(args[3], "unless"))
526 pol = ACL_COND_UNLESS;
527
528 /* Note: we consider "if TRUE" when there is no condition */
529 if (pol != ACL_COND_NONE &&
530 (cond = parse_acl_cond((const char **)args+4, &curpx->acl, pol)) == NULL) {
531 retlen = snprintf(err, errlen,
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200532 "error detected in %s '%s' while parsing '%s' condition",
Willy Tarreaub6866442008-07-14 23:54:42 +0200533 proxy_type_str(curpx), curpx->id, args[3]);
534 return -1;
535 }
536
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200537 // FIXME: how to set this ?
538 // cond->line = linenum;
539 if (cond->requires & (ACL_USE_RTR_ANY | ACL_USE_L7_ANY)) {
540 struct acl *acl;
541 const char *name;
542
543 acl = cond_find_require(cond, ACL_USE_RTR_ANY|ACL_USE_L7_ANY);
544 name = acl ? acl->name : "(unknown)";
545
546 retlen = snprintf(err, errlen,
547 "acl '%s' involves some %s criteria which will be ignored.",
548 name,
549 (acl->requires & ACL_USE_RTR_ANY) ? "response-only" : "layer 7");
550 warn++;
551 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200552 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
553 rule->cond = cond;
554 rule->action = action;
555 LIST_INIT(&rule->list);
556 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200557 return warn;
Willy Tarreaub6866442008-07-14 23:54:42 +0200558 }
559
560 snprintf(err, errlen, "unknown argument '%s' after '%s' in %s '%s'",
561 args[1], args[0], proxy_type_str(proxy), curpx->id);
562 return -1;
563}
564
565/* return the number of bytes in the request buffer */
566static int
567acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
568 struct acl_expr *expr, struct acl_test *test)
569{
570 if (!l4 || !l4->req)
571 return 0;
572
573 test->i = l4->req->l;
574 test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE;
575 return 1;
576}
577
Willy Tarreau655e26a2008-07-15 18:58:05 +0200578/* Return the version of the SSL protocol in the request. It supports both
579 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
580 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
581 * SSLv2 format is described here, and completed p67 of RFC 2246 :
582 * http://wp.netscape.com/eng/security/SSL_2.html
583 *
584 * Note: this decoder only works with non-wrapping data.
585 */
586static int
587acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
588 struct acl_expr *expr, struct acl_test *test)
589{
590 int version, bleft, msg_len;
591 const unsigned char *data;
592
593 if (!l4 || !l4->req)
594 return 0;
595
596 msg_len = 0;
597 bleft = l4->req->l;
598 if (!bleft)
599 goto too_short;
600
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200601 data = (const unsigned char *)l4->req->w;
Willy Tarreau655e26a2008-07-15 18:58:05 +0200602 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
603 /* SSLv3 header format */
604 if (bleft < 5)
605 goto too_short;
606
607 version = (data[1] << 16) + data[2]; /* version: major, minor */
608 msg_len = (data[3] << 8) + data[4]; /* record length */
609
610 /* format introduced with SSLv3 */
611 if (version < 0x00030000)
612 goto not_ssl;
613
614 /* message length between 1 and 2^14 + 2048 */
615 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
616 goto not_ssl;
617
618 bleft -= 5; data += 5;
619 } else {
620 /* SSLv2 header format, only supported for hello (msg type 1) */
621 int rlen, plen, cilen, silen, chlen;
622
623 if (*data & 0x80) {
624 if (bleft < 3)
625 goto too_short;
626 /* short header format : 15 bits for length */
627 rlen = ((data[0] & 0x7F) << 8) | data[1];
628 plen = 0;
629 bleft -= 2; data += 2;
630 } else {
631 if (bleft < 4)
632 goto too_short;
633 /* long header format : 14 bits for length + pad length */
634 rlen = ((data[0] & 0x3F) << 8) | data[1];
635 plen = data[2];
636 bleft -= 3; data += 2;
637 }
638
639 if (*data != 0x01)
640 goto not_ssl;
641 bleft--; data++;
642
643 if (bleft < 8)
644 goto too_short;
645 version = (data[0] << 16) + data[1]; /* version: major, minor */
646 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
647 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
648 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
649
650 bleft -= 8; data += 8;
651 if (cilen % 3 != 0)
652 goto not_ssl;
653 if (silen && silen != 16)
654 goto not_ssl;
655 if (chlen < 16 || chlen > 32)
656 goto not_ssl;
657 if (rlen != 9 + cilen + silen + chlen)
658 goto not_ssl;
659
660 /* focus on the remaining data length */
661 msg_len = cilen + silen + chlen + plen;
662 }
663 /* We could recursively check that the buffer ends exactly on an SSL
664 * fragment boundary and that a possible next segment is still SSL,
665 * but that's a bit pointless. However, we could still check that
666 * all the part of the request which fits in a buffer is already
667 * there.
668 */
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100669 if (msg_len > l4->req->max_len + l4->req->data - l4->req->w)
670 msg_len = l4->req->max_len + l4->req->data - l4->req->w;
Willy Tarreau655e26a2008-07-15 18:58:05 +0200671
672 if (bleft < msg_len)
673 goto too_short;
674
675 /* OK that's enough. We have at least the whole message, and we have
676 * the protocol version.
677 */
678 test->i = version;
679 test->flags = ACL_TEST_F_VOLATILE;
680 return 1;
681
682 too_short:
683 test->flags = ACL_TEST_F_MAY_CHANGE;
684 not_ssl:
685 return 0;
686}
687
Willy Tarreaub6866442008-07-14 23:54:42 +0200688
689static struct cfg_kw_list cfg_kws = {{ },{
690 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
691 { 0, NULL, NULL },
692}};
693
694static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +0200695 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L4REQ_VOLATILE },
696 { "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 +0200697 { NULL, NULL, NULL, NULL },
698}};
699
Willy Tarreaue6b98942007-10-29 01:09:36 +0100700__attribute__((constructor))
701static void __tcp_protocol_init(void)
702{
703 protocol_register(&proto_tcpv4);
704 protocol_register(&proto_tcpv6);
Willy Tarreaub6866442008-07-14 23:54:42 +0200705 cfg_register_keywords(&cfg_kws);
706 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100707}
708
709
710/*
711 * Local variables:
712 * c-indent-level: 8
713 * c-basic-offset: 8
714 * End:
715 */