Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp) |
| 3 | * |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 5 | * |
| 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 Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 27 | #include <common/cfgparse.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 28 | #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 Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 38 | #include <types/global.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 39 | |
| 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 Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 46 | #include <proto/proxy.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 47 | #include <proto/queue.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 48 | #include <proto/session.h> |
| 49 | #include <proto/stream_sock.h> |
| 50 | #include <proto/task.h> |
| 51 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 52 | #ifdef CONFIG_HAP_CTTPROXY |
| 53 | #include <import/ip_tproxy.h> |
| 54 | #endif |
| 55 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 56 | static int tcp_bind_listeners(struct protocol *proto); |
| 57 | |
| 58 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 59 | static 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 */ |
| 77 | static 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 Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 94 | |
| 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 | */ |
| 109 | int 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 Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 173 | |
| 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 | */ |
| 186 | int 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 | } |
| 206 | |
| 207 | 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)); |
| 229 | |
| 230 | #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 Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 236 | #ifdef CONFIG_HAP_LINUX_TPROXY |
| 237 | if ((listener->options & LI_O_FOREIGN) |
Willy Tarreau | 0a45989 | 2008-01-13 17:37:16 +0100 | [diff] [blame] | 238 | && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1) |
| 239 | && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) { |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 240 | msg = "cannot make listening socket transparent"; |
| 241 | err |= ERR_ALERT; |
| 242 | } |
| 243 | #endif |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 244 | 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 | } |
| 249 | |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 250 | if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 251 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 252 | msg = "cannot listen to socket"; |
| 253 | goto tcp_close_return; |
| 254 | } |
| 255 | |
| 256 | /* 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 Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 265 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 266 | fdtab[fd].state = FD_STLISTEN; |
| 267 | fdtab[fd].peeraddr = NULL; |
| 268 | fdtab[fd].peerlen = 0; |
| 269 | fdtab[fd].listener = NULL; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 270 | 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 | */ |
| 286 | static 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 | */ |
| 304 | void 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 | */ |
| 318 | void 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 Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 328 | /* This function should be called to parse a line starting with the "tcp-request" |
| 329 | * keyword. |
| 330 | */ |
| 331 | static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx, |
| 332 | struct proxy *defpx, char *err, int errlen) |
| 333 | { |
| 334 | const char *ptr = NULL; |
Willy Tarreau | c7e961e | 2008-08-17 17:13:47 +0200 | [diff] [blame] | 335 | unsigned int val; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 336 | int retlen; |
| 337 | |
| 338 | if (!*args[1]) { |
| 339 | snprintf(err, errlen, "missing argument for '%s' in %s '%s'", |
| 340 | args[0], proxy_type_str(proxy), curpx->id); |
| 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | if (!strcmp(args[1], "inspect-delay")) { |
| 345 | if (curpx == defpx) { |
| 346 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 347 | args[0], args[1]); |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | if (!(curpx->cap & PR_CAP_FE)) { |
| 352 | snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability", |
| 353 | args[0], args[1], proxy_type_str(proxy), curpx->id, |
| 354 | "frontend"); |
| 355 | return 1; |
| 356 | } |
| 357 | |
| 358 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
| 359 | retlen = snprintf(err, errlen, |
| 360 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
| 361 | args[0], args[1], proxy_type_str(proxy), curpx->id); |
| 362 | if (ptr && retlen < errlen) |
| 363 | retlen += snprintf(err+retlen, errlen - retlen, |
| 364 | " (unexpected character '%c')", *ptr); |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | if (curpx->tcp_req.inspect_delay) { |
| 369 | snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'", |
| 370 | args[0], args[1], proxy_type_str(proxy), curpx->id); |
| 371 | return 1; |
| 372 | } |
| 373 | curpx->tcp_req.inspect_delay = val; |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | if (!strcmp(args[1], "content")) { |
| 378 | int action; |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 379 | int warn = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 380 | int pol = ACL_COND_NONE; |
| 381 | struct acl_cond *cond; |
| 382 | struct tcp_rule *rule; |
| 383 | |
| 384 | if (curpx == defpx) { |
| 385 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 386 | args[0], args[1]); |
| 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | if (!strcmp(args[2], "accept")) |
| 391 | action = TCP_ACT_ACCEPT; |
| 392 | else if (!strcmp(args[2], "reject")) |
| 393 | action = TCP_ACT_REJECT; |
| 394 | else { |
| 395 | retlen = snprintf(err, errlen, |
| 396 | "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')", |
| 397 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]); |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | pol = ACL_COND_NONE; |
| 402 | cond = NULL; |
| 403 | |
| 404 | if (!strcmp(args[3], "if")) |
| 405 | pol = ACL_COND_IF; |
| 406 | else if (!strcmp(args[3], "unless")) |
| 407 | pol = ACL_COND_UNLESS; |
| 408 | |
| 409 | /* Note: we consider "if TRUE" when there is no condition */ |
| 410 | if (pol != ACL_COND_NONE && |
| 411 | (cond = parse_acl_cond((const char **)args+4, &curpx->acl, pol)) == NULL) { |
| 412 | retlen = snprintf(err, errlen, |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 413 | "error detected in %s '%s' while parsing '%s' condition", |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 414 | proxy_type_str(curpx), curpx->id, args[3]); |
| 415 | return -1; |
| 416 | } |
| 417 | |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 418 | // FIXME: how to set this ? |
| 419 | // cond->line = linenum; |
| 420 | if (cond->requires & (ACL_USE_RTR_ANY | ACL_USE_L7_ANY)) { |
| 421 | struct acl *acl; |
| 422 | const char *name; |
| 423 | |
| 424 | acl = cond_find_require(cond, ACL_USE_RTR_ANY|ACL_USE_L7_ANY); |
| 425 | name = acl ? acl->name : "(unknown)"; |
| 426 | |
| 427 | retlen = snprintf(err, errlen, |
| 428 | "acl '%s' involves some %s criteria which will be ignored.", |
| 429 | name, |
| 430 | (acl->requires & ACL_USE_RTR_ANY) ? "response-only" : "layer 7"); |
| 431 | warn++; |
| 432 | } |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 433 | rule = (struct tcp_rule *)calloc(1, sizeof(*rule)); |
| 434 | rule->cond = cond; |
| 435 | rule->action = action; |
| 436 | LIST_INIT(&rule->list); |
| 437 | LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 438 | return warn; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | snprintf(err, errlen, "unknown argument '%s' after '%s' in %s '%s'", |
| 442 | args[1], args[0], proxy_type_str(proxy), curpx->id); |
| 443 | return -1; |
| 444 | } |
| 445 | |
| 446 | /* return the number of bytes in the request buffer */ |
| 447 | static int |
| 448 | acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir, |
| 449 | struct acl_expr *expr, struct acl_test *test) |
| 450 | { |
| 451 | if (!l4 || !l4->req) |
| 452 | return 0; |
| 453 | |
| 454 | test->i = l4->req->l; |
| 455 | test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE; |
| 456 | return 1; |
| 457 | } |
| 458 | |
Willy Tarreau | 655e26a | 2008-07-15 18:58:05 +0200 | [diff] [blame] | 459 | /* Return the version of the SSL protocol in the request. It supports both |
| 460 | * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for |
| 461 | * the hello message. The SSLv3 format is described in RFC 2246 p49, and the |
| 462 | * SSLv2 format is described here, and completed p67 of RFC 2246 : |
| 463 | * http://wp.netscape.com/eng/security/SSL_2.html |
| 464 | * |
| 465 | * Note: this decoder only works with non-wrapping data. |
| 466 | */ |
| 467 | static int |
| 468 | acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 469 | struct acl_expr *expr, struct acl_test *test) |
| 470 | { |
| 471 | int version, bleft, msg_len; |
| 472 | const unsigned char *data; |
| 473 | |
| 474 | if (!l4 || !l4->req) |
| 475 | return 0; |
| 476 | |
| 477 | msg_len = 0; |
| 478 | bleft = l4->req->l; |
| 479 | if (!bleft) |
| 480 | goto too_short; |
| 481 | |
Willy Tarreau | c7e961e | 2008-08-17 17:13:47 +0200 | [diff] [blame] | 482 | data = (const unsigned char *)l4->req->w; |
Willy Tarreau | 655e26a | 2008-07-15 18:58:05 +0200 | [diff] [blame] | 483 | if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) { |
| 484 | /* SSLv3 header format */ |
| 485 | if (bleft < 5) |
| 486 | goto too_short; |
| 487 | |
| 488 | version = (data[1] << 16) + data[2]; /* version: major, minor */ |
| 489 | msg_len = (data[3] << 8) + data[4]; /* record length */ |
| 490 | |
| 491 | /* format introduced with SSLv3 */ |
| 492 | if (version < 0x00030000) |
| 493 | goto not_ssl; |
| 494 | |
| 495 | /* message length between 1 and 2^14 + 2048 */ |
| 496 | if (msg_len < 1 || msg_len > ((1<<14) + 2048)) |
| 497 | goto not_ssl; |
| 498 | |
| 499 | bleft -= 5; data += 5; |
| 500 | } else { |
| 501 | /* SSLv2 header format, only supported for hello (msg type 1) */ |
| 502 | int rlen, plen, cilen, silen, chlen; |
| 503 | |
| 504 | if (*data & 0x80) { |
| 505 | if (bleft < 3) |
| 506 | goto too_short; |
| 507 | /* short header format : 15 bits for length */ |
| 508 | rlen = ((data[0] & 0x7F) << 8) | data[1]; |
| 509 | plen = 0; |
| 510 | bleft -= 2; data += 2; |
| 511 | } else { |
| 512 | if (bleft < 4) |
| 513 | goto too_short; |
| 514 | /* long header format : 14 bits for length + pad length */ |
| 515 | rlen = ((data[0] & 0x3F) << 8) | data[1]; |
| 516 | plen = data[2]; |
| 517 | bleft -= 3; data += 2; |
| 518 | } |
| 519 | |
| 520 | if (*data != 0x01) |
| 521 | goto not_ssl; |
| 522 | bleft--; data++; |
| 523 | |
| 524 | if (bleft < 8) |
| 525 | goto too_short; |
| 526 | version = (data[0] << 16) + data[1]; /* version: major, minor */ |
| 527 | cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */ |
| 528 | silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */ |
| 529 | chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */ |
| 530 | |
| 531 | bleft -= 8; data += 8; |
| 532 | if (cilen % 3 != 0) |
| 533 | goto not_ssl; |
| 534 | if (silen && silen != 16) |
| 535 | goto not_ssl; |
| 536 | if (chlen < 16 || chlen > 32) |
| 537 | goto not_ssl; |
| 538 | if (rlen != 9 + cilen + silen + chlen) |
| 539 | goto not_ssl; |
| 540 | |
| 541 | /* focus on the remaining data length */ |
| 542 | msg_len = cilen + silen + chlen + plen; |
| 543 | } |
| 544 | /* We could recursively check that the buffer ends exactly on an SSL |
| 545 | * fragment boundary and that a possible next segment is still SSL, |
| 546 | * but that's a bit pointless. However, we could still check that |
| 547 | * all the part of the request which fits in a buffer is already |
| 548 | * there. |
| 549 | */ |
| 550 | if (msg_len > l4->req->rlim - l4->req->w) |
| 551 | msg_len = l4->req->rlim - l4->req->w; |
| 552 | |
| 553 | if (bleft < msg_len) |
| 554 | goto too_short; |
| 555 | |
| 556 | /* OK that's enough. We have at least the whole message, and we have |
| 557 | * the protocol version. |
| 558 | */ |
| 559 | test->i = version; |
| 560 | test->flags = ACL_TEST_F_VOLATILE; |
| 561 | return 1; |
| 562 | |
| 563 | too_short: |
| 564 | test->flags = ACL_TEST_F_MAY_CHANGE; |
| 565 | not_ssl: |
| 566 | return 0; |
| 567 | } |
| 568 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 569 | |
| 570 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 571 | { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req }, |
| 572 | { 0, NULL, NULL }, |
| 573 | }}; |
| 574 | |
| 575 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 576 | { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L4REQ_VOLATILE }, |
| 577 | { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L4REQ_VOLATILE }, |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 578 | { NULL, NULL, NULL, NULL }, |
| 579 | }}; |
| 580 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 581 | __attribute__((constructor)) |
| 582 | static void __tcp_protocol_init(void) |
| 583 | { |
| 584 | protocol_register(&proto_tcpv4); |
| 585 | protocol_register(&proto_tcpv6); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 586 | cfg_register_keywords(&cfg_kws); |
| 587 | acl_register_keywords(&acl_kws); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
| 591 | /* |
| 592 | * Local variables: |
| 593 | * c-indent-level: 8 |
| 594 | * c-basic-offset: 8 |
| 595 | * End: |
| 596 | */ |