blob: 2fb6a85c70e84c0eee3a88d2e86911036d140ec0 [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:
342 * - the analyser must check for errors and timeouts, and react as expected.
343 * It does not have to close anything upon error, the caller will.
344 * - if the analyser does not have enough data, it must return 0without calling
345 * other ones. It should also probably do a buffer_write_dis() to ensure
346 * that unprocessed data will not be forwarded. But that probably depends on
347 * the protocol.
348 * - if an analyser has enough data, it just has to pass on to the next
349 * analyser without using buffer_write_dis() (enabled by default).
350 * - if an analyser thinks it has no added value anymore staying here, it must
351 * reset its bit from the analysers flags in order not to be called anymore.
352 *
353 * In the future, analysers should be able to indicate that they want to be
354 * called after XXX bytes have been received (or transfered), and the min of
355 * all's wishes will be used to ring back (unless a special condition occurs).
356 */
357int tcp_inspect_request(struct session *s, struct buffer *req)
358{
359 struct tcp_rule *rule;
360 int partial;
361
362 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
363 now_ms, __FUNCTION__,
364 s,
365 req,
366 req->rex, req->wex,
367 req->flags,
368 req->l,
369 req->analysers);
370
371 /* We will abort if we encounter a read error. In theory, we
372 * should not abort if we get a close, it might be valid,
373 * although very unlikely. FIXME: we'll abort for now, this
374 * will be easier to change later.
375 */
376 if (req->flags & BF_READ_ERROR) {
377 req->analysers = 0;
378 s->fe->failed_req++;
379 if (!(s->flags & SN_ERR_MASK))
380 s->flags |= SN_ERR_CLICL;
381 if (!(s->flags & SN_FINST_MASK))
382 s->flags |= SN_FINST_R;
383 return 0;
384 }
385
386 /* Abort if client read timeout has expired */
387 else if (req->flags & BF_READ_TIMEOUT) {
388 req->analysers = 0;
389 s->fe->failed_req++;
390 if (!(s->flags & SN_ERR_MASK))
391 s->flags |= SN_ERR_CLITO;
392 if (!(s->flags & SN_FINST_MASK))
393 s->flags |= SN_FINST_R;
394 return 0;
395 }
396
397 /* We don't know whether we have enough data, so must proceed
398 * this way :
399 * - iterate through all rules in their declaration order
400 * - if one rule returns MISS, it means the inspect delay is
401 * not over yet, then return immediately, otherwise consider
402 * it as a non-match.
403 * - if one rule returns OK, then return OK
404 * - if one rule returns KO, then return KO
405 */
406
407 if (req->flags & BF_SHUTR || tick_is_expired(req->analyse_exp, now_ms))
408 partial = 0;
409 else
410 partial = ACL_PARTIAL;
411
412 list_for_each_entry(rule, &s->fe->tcp_req.inspect_rules, list) {
413 int ret = ACL_PAT_PASS;
414
415 if (rule->cond) {
416 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ | partial);
417 if (ret == ACL_PAT_MISS) {
418 buffer_write_dis(req);
419 /* just set the request timeout once at the beginning of the request */
420 if (!tick_isset(req->analyse_exp))
421 req->analyse_exp = tick_add_ifset(now_ms, s->fe->tcp_req.inspect_delay);
422 return 0;
423 }
424
425 ret = acl_pass(ret);
426 if (rule->cond->pol == ACL_COND_UNLESS)
427 ret = !ret;
428 }
429
430 if (ret) {
431 /* we have a matching rule. */
432 if (rule->action == TCP_ACT_REJECT) {
433 buffer_abort(req);
434 buffer_abort(s->rep);
435 req->analysers = 0;
436 s->fe->failed_req++;
437 if (!(s->flags & SN_ERR_MASK))
438 s->flags |= SN_ERR_PRXCOND;
439 if (!(s->flags & SN_FINST_MASK))
440 s->flags |= SN_FINST_R;
441 return 0;
442 }
443 /* otherwise accept */
444 break;
445 }
446 }
447
448 /* if we get there, it means we have no rule which matches, or
449 * we have an explicit accept, so we apply the default accept.
450 */
451 req->analysers &= ~AN_REQ_INSPECT;
452 req->analyse_exp = TICK_ETERNITY;
453 return 1;
454}
455
456
Willy Tarreaub6866442008-07-14 23:54:42 +0200457/* This function should be called to parse a line starting with the "tcp-request"
458 * keyword.
459 */
460static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
461 struct proxy *defpx, char *err, int errlen)
462{
463 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200464 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +0200465 int retlen;
466
467 if (!*args[1]) {
468 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
469 args[0], proxy_type_str(proxy), curpx->id);
470 return -1;
471 }
472
473 if (!strcmp(args[1], "inspect-delay")) {
474 if (curpx == defpx) {
475 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
476 args[0], args[1]);
477 return -1;
478 }
479
480 if (!(curpx->cap & PR_CAP_FE)) {
481 snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability",
482 args[0], args[1], proxy_type_str(proxy), curpx->id,
483 "frontend");
484 return 1;
485 }
486
487 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
488 retlen = snprintf(err, errlen,
489 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
490 args[0], args[1], proxy_type_str(proxy), curpx->id);
491 if (ptr && retlen < errlen)
492 retlen += snprintf(err+retlen, errlen - retlen,
493 " (unexpected character '%c')", *ptr);
494 return -1;
495 }
496
497 if (curpx->tcp_req.inspect_delay) {
498 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
499 args[0], args[1], proxy_type_str(proxy), curpx->id);
500 return 1;
501 }
502 curpx->tcp_req.inspect_delay = val;
503 return 0;
504 }
505
506 if (!strcmp(args[1], "content")) {
507 int action;
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200508 int warn = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +0200509 int pol = ACL_COND_NONE;
510 struct acl_cond *cond;
511 struct tcp_rule *rule;
512
513 if (curpx == defpx) {
514 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
515 args[0], args[1]);
516 return -1;
517 }
518
519 if (!strcmp(args[2], "accept"))
520 action = TCP_ACT_ACCEPT;
521 else if (!strcmp(args[2], "reject"))
522 action = TCP_ACT_REJECT;
523 else {
524 retlen = snprintf(err, errlen,
525 "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')",
526 args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]);
527 return -1;
528 }
529
530 pol = ACL_COND_NONE;
531 cond = NULL;
532
533 if (!strcmp(args[3], "if"))
534 pol = ACL_COND_IF;
535 else if (!strcmp(args[3], "unless"))
536 pol = ACL_COND_UNLESS;
537
538 /* Note: we consider "if TRUE" when there is no condition */
539 if (pol != ACL_COND_NONE &&
540 (cond = parse_acl_cond((const char **)args+4, &curpx->acl, pol)) == NULL) {
541 retlen = snprintf(err, errlen,
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200542 "error detected in %s '%s' while parsing '%s' condition",
Willy Tarreaub6866442008-07-14 23:54:42 +0200543 proxy_type_str(curpx), curpx->id, args[3]);
544 return -1;
545 }
546
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200547 // FIXME: how to set this ?
548 // cond->line = linenum;
549 if (cond->requires & (ACL_USE_RTR_ANY | ACL_USE_L7_ANY)) {
550 struct acl *acl;
551 const char *name;
552
553 acl = cond_find_require(cond, ACL_USE_RTR_ANY|ACL_USE_L7_ANY);
554 name = acl ? acl->name : "(unknown)";
555
556 retlen = snprintf(err, errlen,
557 "acl '%s' involves some %s criteria which will be ignored.",
558 name,
559 (acl->requires & ACL_USE_RTR_ANY) ? "response-only" : "layer 7");
560 warn++;
561 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200562 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
563 rule->cond = cond;
564 rule->action = action;
565 LIST_INIT(&rule->list);
566 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200567 return warn;
Willy Tarreaub6866442008-07-14 23:54:42 +0200568 }
569
570 snprintf(err, errlen, "unknown argument '%s' after '%s' in %s '%s'",
571 args[1], args[0], proxy_type_str(proxy), curpx->id);
572 return -1;
573}
574
575/* return the number of bytes in the request buffer */
576static int
577acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
578 struct acl_expr *expr, struct acl_test *test)
579{
580 if (!l4 || !l4->req)
581 return 0;
582
583 test->i = l4->req->l;
584 test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE;
585 return 1;
586}
587
Willy Tarreau655e26a2008-07-15 18:58:05 +0200588/* Return the version of the SSL protocol in the request. It supports both
589 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
590 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
591 * SSLv2 format is described here, and completed p67 of RFC 2246 :
592 * http://wp.netscape.com/eng/security/SSL_2.html
593 *
594 * Note: this decoder only works with non-wrapping data.
595 */
596static int
597acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
598 struct acl_expr *expr, struct acl_test *test)
599{
600 int version, bleft, msg_len;
601 const unsigned char *data;
602
603 if (!l4 || !l4->req)
604 return 0;
605
606 msg_len = 0;
607 bleft = l4->req->l;
608 if (!bleft)
609 goto too_short;
610
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200611 data = (const unsigned char *)l4->req->w;
Willy Tarreau655e26a2008-07-15 18:58:05 +0200612 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
613 /* SSLv3 header format */
614 if (bleft < 5)
615 goto too_short;
616
617 version = (data[1] << 16) + data[2]; /* version: major, minor */
618 msg_len = (data[3] << 8) + data[4]; /* record length */
619
620 /* format introduced with SSLv3 */
621 if (version < 0x00030000)
622 goto not_ssl;
623
624 /* message length between 1 and 2^14 + 2048 */
625 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
626 goto not_ssl;
627
628 bleft -= 5; data += 5;
629 } else {
630 /* SSLv2 header format, only supported for hello (msg type 1) */
631 int rlen, plen, cilen, silen, chlen;
632
633 if (*data & 0x80) {
634 if (bleft < 3)
635 goto too_short;
636 /* short header format : 15 bits for length */
637 rlen = ((data[0] & 0x7F) << 8) | data[1];
638 plen = 0;
639 bleft -= 2; data += 2;
640 } else {
641 if (bleft < 4)
642 goto too_short;
643 /* long header format : 14 bits for length + pad length */
644 rlen = ((data[0] & 0x3F) << 8) | data[1];
645 plen = data[2];
646 bleft -= 3; data += 2;
647 }
648
649 if (*data != 0x01)
650 goto not_ssl;
651 bleft--; data++;
652
653 if (bleft < 8)
654 goto too_short;
655 version = (data[0] << 16) + data[1]; /* version: major, minor */
656 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
657 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
658 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
659
660 bleft -= 8; data += 8;
661 if (cilen % 3 != 0)
662 goto not_ssl;
663 if (silen && silen != 16)
664 goto not_ssl;
665 if (chlen < 16 || chlen > 32)
666 goto not_ssl;
667 if (rlen != 9 + cilen + silen + chlen)
668 goto not_ssl;
669
670 /* focus on the remaining data length */
671 msg_len = cilen + silen + chlen + plen;
672 }
673 /* We could recursively check that the buffer ends exactly on an SSL
674 * fragment boundary and that a possible next segment is still SSL,
675 * but that's a bit pointless. However, we could still check that
676 * all the part of the request which fits in a buffer is already
677 * there.
678 */
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100679 if (msg_len > l4->req->max_len + l4->req->data - l4->req->w)
680 msg_len = l4->req->max_len + l4->req->data - l4->req->w;
Willy Tarreau655e26a2008-07-15 18:58:05 +0200681
682 if (bleft < msg_len)
683 goto too_short;
684
685 /* OK that's enough. We have at least the whole message, and we have
686 * the protocol version.
687 */
688 test->i = version;
689 test->flags = ACL_TEST_F_VOLATILE;
690 return 1;
691
692 too_short:
693 test->flags = ACL_TEST_F_MAY_CHANGE;
694 not_ssl:
695 return 0;
696}
697
Willy Tarreaub6866442008-07-14 23:54:42 +0200698
699static struct cfg_kw_list cfg_kws = {{ },{
700 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
701 { 0, NULL, NULL },
702}};
703
704static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +0200705 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L4REQ_VOLATILE },
706 { "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 +0200707 { NULL, NULL, NULL, NULL },
708}};
709
Willy Tarreaue6b98942007-10-29 01:09:36 +0100710__attribute__((constructor))
711static void __tcp_protocol_init(void)
712{
713 protocol_register(&proto_tcpv4);
714 protocol_register(&proto_tcpv6);
Willy Tarreaub6866442008-07-14 23:54:42 +0200715 cfg_register_keywords(&cfg_kws);
716 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100717}
718
719
720/*
721 * Local variables:
722 * c-indent-level: 8
723 * c-basic-offset: 8
724 * End:
725 */