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