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