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 | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 41 | #include <types/server.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 42 | |
| 43 | #include <proto/acl.h> |
| 44 | #include <proto/backend.h> |
| 45 | #include <proto/buffers.h> |
| 46 | #include <proto/fd.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 47 | #include <proto/log.h> |
| 48 | #include <proto/port_range.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 49 | #include <proto/protocols.h> |
| 50 | #include <proto/proto_tcp.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 51 | #include <proto/proxy.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 52 | #include <proto/queue.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 53 | #include <proto/session.h> |
| 54 | #include <proto/stream_sock.h> |
| 55 | #include <proto/task.h> |
| 56 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 57 | #ifdef CONFIG_HAP_CTTPROXY |
| 58 | #include <import/ip_tproxy.h> |
| 59 | #endif |
| 60 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 61 | static int tcp_bind_listeners(struct protocol *proto); |
| 62 | |
| 63 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 64 | static struct protocol proto_tcpv4 = { |
| 65 | .name = "tcpv4", |
| 66 | .sock_domain = AF_INET, |
| 67 | .sock_type = SOCK_STREAM, |
| 68 | .sock_prot = IPPROTO_TCP, |
| 69 | .sock_family = AF_INET, |
| 70 | .sock_addrlen = sizeof(struct sockaddr_in), |
| 71 | .l3_addrlen = 32/8, |
| 72 | .read = &stream_sock_read, |
| 73 | .write = &stream_sock_write, |
| 74 | .bind_all = tcp_bind_listeners, |
| 75 | .unbind_all = unbind_all_listeners, |
| 76 | .enable_all = enable_all_listeners, |
| 77 | .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners), |
| 78 | .nb_listeners = 0, |
| 79 | }; |
| 80 | |
| 81 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 82 | static struct protocol proto_tcpv6 = { |
| 83 | .name = "tcpv6", |
| 84 | .sock_domain = AF_INET6, |
| 85 | .sock_type = SOCK_STREAM, |
| 86 | .sock_prot = IPPROTO_TCP, |
| 87 | .sock_family = AF_INET6, |
| 88 | .sock_addrlen = sizeof(struct sockaddr_in6), |
| 89 | .l3_addrlen = 128/8, |
| 90 | .read = &stream_sock_read, |
| 91 | .write = &stream_sock_write, |
| 92 | .bind_all = tcp_bind_listeners, |
| 93 | .unbind_all = unbind_all_listeners, |
| 94 | .enable_all = enable_all_listeners, |
| 95 | .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners), |
| 96 | .nb_listeners = 0, |
| 97 | }; |
| 98 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 99 | |
| 100 | /* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which |
| 101 | * case we try to bind <remote>. <flags> is a 2-bit field consisting of : |
| 102 | * - 0 : ignore remote address (may even be a NULL pointer) |
| 103 | * - 1 : use provided address |
| 104 | * - 2 : use provided port |
| 105 | * - 3 : use both |
| 106 | * |
| 107 | * The function supports multiple foreign binding methods : |
| 108 | * - linux_tproxy: we directly bind to the foreign address |
| 109 | * - cttproxy: we bind to a local address then nat. |
| 110 | * The second one can be used as a fallback for the first one. |
| 111 | * This function returns 0 when everything's OK, 1 if it could not bind, to the |
| 112 | * local address, 2 if it could not bind to the foreign address. |
| 113 | */ |
| 114 | int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote) |
| 115 | { |
| 116 | struct sockaddr_in bind_addr; |
| 117 | int foreign_ok = 0; |
| 118 | int ret; |
| 119 | |
| 120 | #ifdef CONFIG_HAP_LINUX_TPROXY |
| 121 | static int ip_transp_working = 1; |
| 122 | if (flags && ip_transp_working) { |
| 123 | if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0 |
| 124 | || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0) |
| 125 | foreign_ok = 1; |
| 126 | else |
| 127 | ip_transp_working = 0; |
| 128 | } |
| 129 | #endif |
| 130 | if (flags) { |
| 131 | memset(&bind_addr, 0, sizeof(bind_addr)); |
| 132 | if (flags & 1) |
| 133 | bind_addr.sin_addr = remote->sin_addr; |
| 134 | if (flags & 2) |
| 135 | bind_addr.sin_port = remote->sin_port; |
| 136 | } |
| 137 | |
| 138 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 139 | if (foreign_ok) { |
| 140 | ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)); |
| 141 | if (ret < 0) |
| 142 | return 2; |
| 143 | } |
| 144 | else { |
| 145 | ret = bind(fd, (struct sockaddr *)local, sizeof(*local)); |
| 146 | if (ret < 0) |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | if (!flags) |
| 151 | return 0; |
| 152 | |
| 153 | #ifdef CONFIG_HAP_CTTPROXY |
| 154 | if (!foreign_ok) { |
| 155 | struct in_tproxy itp1, itp2; |
| 156 | memset(&itp1, 0, sizeof(itp1)); |
| 157 | |
| 158 | itp1.op = TPROXY_ASSIGN; |
| 159 | itp1.v.addr.faddr = bind_addr.sin_addr; |
| 160 | itp1.v.addr.fport = bind_addr.sin_port; |
| 161 | |
| 162 | /* set connect flag on socket */ |
| 163 | itp2.op = TPROXY_FLAGS; |
| 164 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 165 | |
| 166 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 && |
| 167 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) { |
| 168 | foreign_ok = 1; |
| 169 | } |
| 170 | } |
| 171 | #endif |
| 172 | if (!foreign_ok) |
| 173 | /* we could not bind to a foreign address */ |
| 174 | return 2; |
| 175 | |
| 176 | return 0; |
| 177 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 178 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * This function initiates a connection to the server assigned to this session |
| 182 | * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. |
| 183 | * It can return one of : |
| 184 | * - SN_ERR_NONE if everything's OK |
| 185 | * - SN_ERR_SRVTO if there are no more servers |
| 186 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 187 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 188 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 189 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 190 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 191 | */ |
| 192 | int tcpv4_connect_server(struct stream_interface *si, |
| 193 | struct proxy *be, struct server *srv, |
| 194 | struct sockaddr *srv_addr, struct sockaddr *cli_addr) |
| 195 | { |
| 196 | int fd; |
| 197 | |
| 198 | if ((fd = si->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 199 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 200 | |
| 201 | if (errno == ENFILE) |
| 202 | send_log(be, LOG_EMERG, |
| 203 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 204 | be->id, maxfd); |
| 205 | else if (errno == EMFILE) |
| 206 | send_log(be, LOG_EMERG, |
| 207 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 208 | be->id, maxfd); |
| 209 | else if (errno == ENOBUFS || errno == ENOMEM) |
| 210 | send_log(be, LOG_EMERG, |
| 211 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 212 | be->id, maxfd); |
| 213 | /* this is a resource error */ |
| 214 | return SN_ERR_RESOURCE; |
| 215 | } |
| 216 | |
| 217 | if (fd >= global.maxsock) { |
| 218 | /* do not log anything there, it's a normal condition when this option |
| 219 | * is used to serialize connections to a server ! |
| 220 | */ |
| 221 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 222 | close(fd); |
| 223 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
| 224 | } |
| 225 | |
| 226 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 227 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
| 228 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 229 | close(fd); |
| 230 | return SN_ERR_INTERNAL; |
| 231 | } |
| 232 | |
| 233 | if (be->options & PR_O_TCP_SRV_KA) |
| 234 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 235 | |
| 236 | if (be->options & PR_O_TCP_NOLING) |
| 237 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 238 | |
| 239 | /* allow specific binding : |
| 240 | * - server-specific at first |
| 241 | * - proxy-specific next |
| 242 | */ |
| 243 | if (srv != NULL && srv->state & SRV_BIND_SRC) { |
| 244 | struct sockaddr_in *remote = NULL; |
| 245 | int ret, flags = 0; |
| 246 | |
| 247 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
| 248 | switch (srv->state & SRV_TPROXY_MASK) { |
| 249 | case SRV_TPROXY_ADDR: |
| 250 | remote = (struct sockaddr_in *)&srv->tproxy_addr; |
| 251 | flags = 3; |
| 252 | break; |
| 253 | case SRV_TPROXY_CLI: |
| 254 | if (cli_addr) |
| 255 | flags |= 2; |
| 256 | /* fall through */ |
| 257 | case SRV_TPROXY_CIP: |
| 258 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 259 | if (cli_addr) |
| 260 | flags |= 1; |
| 261 | remote = (struct sockaddr_in *)cli_addr; |
| 262 | break; |
| 263 | } |
| 264 | #endif |
| 265 | #ifdef SO_BINDTODEVICE |
| 266 | /* Note: this might fail if not CAP_NET_RAW */ |
| 267 | if (srv->iface_name) |
| 268 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1); |
| 269 | #endif |
| 270 | |
| 271 | if (srv->sport_range) { |
| 272 | int attempts = 10; /* should be more than enough to find a spare port */ |
| 273 | struct sockaddr_in src; |
| 274 | |
| 275 | ret = 1; |
| 276 | src = srv->source_addr; |
| 277 | |
| 278 | do { |
| 279 | /* note: in case of retry, we may have to release a previously |
| 280 | * allocated port, hence this loop's construct. |
| 281 | */ |
| 282 | port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port); |
| 283 | fdtab[fd].port_range = NULL; |
| 284 | |
| 285 | if (!attempts) |
| 286 | break; |
| 287 | attempts--; |
| 288 | |
| 289 | fdtab[fd].local_port = port_range_alloc_port(srv->sport_range); |
| 290 | if (!fdtab[fd].local_port) |
| 291 | break; |
| 292 | |
| 293 | fdtab[fd].port_range = srv->sport_range; |
| 294 | src.sin_port = htons(fdtab[fd].local_port); |
| 295 | |
| 296 | ret = tcpv4_bind_socket(fd, flags, &src, remote); |
| 297 | } while (ret != 0); /* binding NOK */ |
| 298 | } |
| 299 | else { |
| 300 | ret = tcpv4_bind_socket(fd, flags, &srv->source_addr, remote); |
| 301 | } |
| 302 | |
| 303 | if (ret) { |
| 304 | port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port); |
| 305 | fdtab[fd].port_range = NULL; |
| 306 | close(fd); |
| 307 | |
| 308 | if (ret == 1) { |
| 309 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 310 | be->id, srv->id); |
| 311 | send_log(be, LOG_EMERG, |
| 312 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 313 | be->id, srv->id); |
| 314 | } else { |
| 315 | Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n", |
| 316 | be->id, srv->id); |
| 317 | send_log(be, LOG_EMERG, |
| 318 | "Cannot bind to tproxy source address before connect() for server %s/%s.\n", |
| 319 | be->id, srv->id); |
| 320 | } |
| 321 | return SN_ERR_RESOURCE; |
| 322 | } |
| 323 | } |
| 324 | else if (be->options & PR_O_BIND_SRC) { |
| 325 | struct sockaddr_in *remote = NULL; |
| 326 | int ret, flags = 0; |
| 327 | |
| 328 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
| 329 | switch (be->options & PR_O_TPXY_MASK) { |
| 330 | case PR_O_TPXY_ADDR: |
| 331 | remote = (struct sockaddr_in *)&be->tproxy_addr; |
| 332 | flags = 3; |
| 333 | break; |
| 334 | case PR_O_TPXY_CLI: |
| 335 | if (cli_addr) |
| 336 | flags |= 2; |
| 337 | /* fall through */ |
| 338 | case PR_O_TPXY_CIP: |
| 339 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 340 | if (cli_addr) |
| 341 | flags |= 1; |
| 342 | remote = (struct sockaddr_in *)cli_addr; |
| 343 | break; |
| 344 | } |
| 345 | #endif |
| 346 | #ifdef SO_BINDTODEVICE |
| 347 | /* Note: this might fail if not CAP_NET_RAW */ |
| 348 | if (be->iface_name) |
| 349 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1); |
| 350 | #endif |
| 351 | ret = tcpv4_bind_socket(fd, flags, &be->source_addr, remote); |
| 352 | if (ret) { |
| 353 | close(fd); |
| 354 | if (ret == 1) { |
| 355 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", |
| 356 | be->id); |
| 357 | send_log(be, LOG_EMERG, |
| 358 | "Cannot bind to source address before connect() for proxy %s.\n", |
| 359 | be->id); |
| 360 | } else { |
| 361 | Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n", |
| 362 | be->id); |
| 363 | send_log(be, LOG_EMERG, |
| 364 | "Cannot bind to tproxy source address before connect() for proxy %s.\n", |
| 365 | be->id); |
| 366 | } |
| 367 | return SN_ERR_RESOURCE; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | #if defined(TCP_QUICKACK) && defined(SOL_TCP) |
| 372 | /* disabling tcp quick ack now allows the first request to leave the |
| 373 | * machine with the first ACK. We only do this if there are pending |
| 374 | * data in the buffer. |
| 375 | */ |
| 376 | if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max) |
| 377 | setsockopt(fd, SOL_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero)); |
| 378 | #endif |
| 379 | |
| 380 | if ((connect(fd, (struct sockaddr *)srv_addr, sizeof(struct sockaddr_in)) == -1) && |
| 381 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 382 | |
| 383 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 384 | char *msg; |
| 385 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 386 | msg = "no free ports"; |
| 387 | else |
| 388 | msg = "local address already in use"; |
| 389 | |
| 390 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
| 391 | port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port); |
| 392 | fdtab[fd].port_range = NULL; |
| 393 | close(fd); |
| 394 | send_log(be, LOG_EMERG, |
| 395 | "Connect() failed for server %s/%s: %s.\n", |
| 396 | be->id, srv->id, msg); |
| 397 | return SN_ERR_RESOURCE; |
| 398 | } else if (errno == ETIMEDOUT) { |
| 399 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
| 400 | port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port); |
| 401 | fdtab[fd].port_range = NULL; |
| 402 | close(fd); |
| 403 | return SN_ERR_SRVTO; |
| 404 | } else { |
| 405 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 406 | //qfprintf(stderr,"Connect(): %d", errno); |
| 407 | port_range_release_port(fdtab[fd].port_range, fdtab[fd].local_port); |
| 408 | fdtab[fd].port_range = NULL; |
| 409 | close(fd); |
| 410 | return SN_ERR_SRVCL; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | fdtab[fd].owner = si; |
| 415 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 416 | fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY; |
| 417 | fdtab[fd].cb[DIR_RD].f = &stream_sock_read; |
| 418 | fdtab[fd].cb[DIR_RD].b = si->ib; |
| 419 | fdtab[fd].cb[DIR_WR].f = &stream_sock_write; |
| 420 | fdtab[fd].cb[DIR_WR].b = si->ob; |
| 421 | |
| 422 | fdtab[fd].peeraddr = (struct sockaddr *)srv_addr; |
| 423 | fdtab[fd].peerlen = sizeof(struct sockaddr_in); |
| 424 | |
| 425 | fd_insert(fd); |
| 426 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
| 427 | |
| 428 | si->state = SI_ST_CON; |
| 429 | si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */ |
| 430 | si->exp = tick_add_ifset(now_ms, be->timeout.connect); |
| 431 | |
| 432 | return SN_ERR_NONE; /* connection is OK */ |
| 433 | } |
| 434 | |
| 435 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 436 | /* This function tries to bind a TCPv4/v6 listener. It may return a warning or |
| 437 | * an error message in <err> if the message is at most <errlen> bytes long |
| 438 | * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN, |
| 439 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 440 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 441 | * error occurred but that it may vanish after a retry (eg: port in use), and |
| 442 | * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter |
| 443 | * the meaning of the error, but just indicate that a message is present which |
| 444 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 445 | * that it's pointless to try to start other listeners. No error message is |
| 446 | * returned if errlen is NULL. |
| 447 | */ |
| 448 | int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 449 | { |
| 450 | __label__ tcp_return, tcp_close_return; |
| 451 | int fd, err; |
| 452 | const char *msg = NULL; |
| 453 | |
| 454 | /* ensure we never return garbage */ |
| 455 | if (errmsg && errlen) |
| 456 | *errmsg = 0; |
| 457 | |
| 458 | if (listener->state != LI_ASSIGNED) |
| 459 | return ERR_NONE; /* already bound */ |
| 460 | |
| 461 | err = ERR_NONE; |
| 462 | |
| 463 | if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 464 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 465 | msg = "cannot create listening socket"; |
| 466 | goto tcp_return; |
| 467 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 468 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 469 | if (fd >= global.maxsock) { |
| 470 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 471 | msg = "not enough free sockets (raise '-n' parameter)"; |
| 472 | goto tcp_close_return; |
| 473 | } |
| 474 | |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 475 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 476 | err |= ERR_FATAL | ERR_ALERT; |
| 477 | msg = "cannot make socket non-blocking"; |
| 478 | goto tcp_close_return; |
| 479 | } |
| 480 | |
| 481 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 482 | /* not fatal but should be reported */ |
| 483 | msg = "cannot do so_reuseaddr"; |
| 484 | err |= ERR_ALERT; |
| 485 | } |
| 486 | |
| 487 | if (listener->options & LI_O_NOLINGER) |
| 488 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 489 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 490 | #ifdef SO_REUSEPORT |
| 491 | /* OpenBSD supports this. As it's present in old libc versions of Linux, |
| 492 | * it might return an error that we will silently ignore. |
| 493 | */ |
| 494 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one)); |
| 495 | #endif |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 496 | #ifdef CONFIG_HAP_LINUX_TPROXY |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 497 | if ((listener->options & LI_O_FOREIGN) |
Willy Tarreau | 0a45989 | 2008-01-13 17:37:16 +0100 | [diff] [blame] | 498 | && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1) |
| 499 | && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) { |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 500 | msg = "cannot make listening socket transparent"; |
| 501 | err |= ERR_ALERT; |
| 502 | } |
| 503 | #endif |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 504 | #ifdef SO_BINDTODEVICE |
| 505 | /* Note: this might fail if not CAP_NET_RAW */ |
| 506 | if (listener->interface) { |
| 507 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 508 | listener->interface, strlen(listener->interface) + 1) == -1) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 509 | msg = "cannot bind listener to device"; |
| 510 | err |= ERR_WARN; |
| 511 | } |
| 512 | } |
| 513 | #endif |
Willy Tarreau | c9fce2f | 2009-08-16 14:13:47 +0200 | [diff] [blame] | 514 | #if defined(TCP_MAXSEG) && defined(SOL_TCP) |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 515 | if (listener->maxseg) { |
| 516 | if (setsockopt(fd, SOL_TCP, TCP_MAXSEG, |
| 517 | &listener->maxseg, sizeof(listener->maxseg)) == -1) { |
| 518 | msg = "cannot set MSS"; |
| 519 | err |= ERR_WARN; |
| 520 | } |
| 521 | } |
| 522 | #endif |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 523 | if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) { |
| 524 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 525 | msg = "cannot bind socket"; |
| 526 | goto tcp_close_return; |
| 527 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 528 | |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 529 | if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 530 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 531 | msg = "cannot listen to socket"; |
| 532 | goto tcp_close_return; |
| 533 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 534 | |
Willy Tarreau | c9fce2f | 2009-08-16 14:13:47 +0200 | [diff] [blame] | 535 | #if defined(TCP_QUICKACK) && defined(SOL_TCP) |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 536 | if (listener->options & LI_O_NOQUICKACK) |
| 537 | setsockopt(fd, SOL_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero)); |
| 538 | #endif |
| 539 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 540 | /* the socket is ready */ |
| 541 | listener->fd = fd; |
| 542 | listener->state = LI_LISTEN; |
| 543 | |
| 544 | /* the function for the accept() event */ |
| 545 | fd_insert(fd); |
| 546 | fdtab[fd].cb[DIR_RD].f = listener->accept; |
| 547 | fdtab[fd].cb[DIR_WR].f = NULL; /* never called */ |
| 548 | 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] | 549 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 550 | fdtab[fd].state = FD_STLISTEN; |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 551 | fdtab[fd].flags = FD_FL_TCP; |
Willy Tarreau | 5d707e1 | 2009-06-28 11:09:07 +0200 | [diff] [blame] | 552 | if (listener->options & LI_O_NOLINGER) |
| 553 | fdtab[fd].flags |= FD_FL_TCP_NOLING; |
| 554 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 555 | fdtab[fd].peeraddr = NULL; |
| 556 | fdtab[fd].peerlen = 0; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 557 | tcp_return: |
| 558 | if (msg && errlen) |
| 559 | strlcpy2(errmsg, msg, errlen); |
| 560 | return err; |
| 561 | |
| 562 | tcp_close_return: |
| 563 | close(fd); |
| 564 | goto tcp_return; |
| 565 | } |
| 566 | |
| 567 | /* This function creates all TCP sockets bound to the protocol entry <proto>. |
| 568 | * It is intended to be used as the protocol's bind_all() function. |
| 569 | * The sockets will be registered but not added to any fd_set, in order not to |
| 570 | * loose them across the fork(). A call to enable_all_listeners() is needed |
| 571 | * to complete initialization. The return value is composed from ERR_*. |
| 572 | */ |
| 573 | static int tcp_bind_listeners(struct protocol *proto) |
| 574 | { |
| 575 | struct listener *listener; |
| 576 | int err = ERR_NONE; |
| 577 | |
| 578 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
| 579 | err |= tcp_bind_listener(listener, NULL, 0); |
| 580 | if ((err & ERR_CODE) == ERR_ABORT) |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | return err; |
| 585 | } |
| 586 | |
| 587 | /* Add listener to the list of tcpv4 listeners. The listener's state |
| 588 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 589 | * listeners is updated. This is the function to use to add a new listener. |
| 590 | */ |
| 591 | void tcpv4_add_listener(struct listener *listener) |
| 592 | { |
| 593 | if (listener->state != LI_INIT) |
| 594 | return; |
| 595 | listener->state = LI_ASSIGNED; |
| 596 | listener->proto = &proto_tcpv4; |
| 597 | LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list); |
| 598 | proto_tcpv4.nb_listeners++; |
| 599 | } |
| 600 | |
| 601 | /* Add listener to the list of tcpv4 listeners. The listener's state |
| 602 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 603 | * listeners is updated. This is the function to use to add a new listener. |
| 604 | */ |
| 605 | void tcpv6_add_listener(struct listener *listener) |
| 606 | { |
| 607 | if (listener->state != LI_INIT) |
| 608 | return; |
| 609 | listener->state = LI_ASSIGNED; |
| 610 | listener->proto = &proto_tcpv6; |
| 611 | LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list); |
| 612 | proto_tcpv6.nb_listeners++; |
| 613 | } |
| 614 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 615 | /* This function performs the TCP request analysis on the current request. It |
| 616 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 617 | * needs more data, encounters an error, or wants to immediately abort the |
| 618 | * request. It relies on buffers flags, and updates s->req->analysers. Its |
| 619 | * behaviour is rather simple: |
Willy Tarreau | 86ef7dc | 2009-03-15 22:55:47 +0100 | [diff] [blame] | 620 | * - the analyser should check for errors and timeouts, and react as expected. |
| 621 | * It does not have to close anything upon error, the caller will. Note that |
| 622 | * the caller also knows how to report errors and timeouts. |
| 623 | * - 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] | 624 | * other ones. It should also probably do a buffer_write_dis() to ensure |
| 625 | * that unprocessed data will not be forwarded. But that probably depends on |
| 626 | * the protocol. |
| 627 | * - if an analyser has enough data, it just has to pass on to the next |
| 628 | * analyser without using buffer_write_dis() (enabled by default). |
| 629 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 630 | * reset its bit from the analysers flags in order not to be called anymore. |
| 631 | * |
| 632 | * In the future, analysers should be able to indicate that they want to be |
| 633 | * called after XXX bytes have been received (or transfered), and the min of |
| 634 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 635 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 636 | int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 637 | { |
| 638 | struct tcp_rule *rule; |
| 639 | int partial; |
| 640 | |
| 641 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 642 | now_ms, __FUNCTION__, |
| 643 | s, |
| 644 | req, |
| 645 | req->rex, req->wex, |
| 646 | req->flags, |
| 647 | req->l, |
| 648 | req->analysers); |
| 649 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 650 | /* We don't know whether we have enough data, so must proceed |
| 651 | * this way : |
| 652 | * - iterate through all rules in their declaration order |
| 653 | * - if one rule returns MISS, it means the inspect delay is |
| 654 | * not over yet, then return immediately, otherwise consider |
| 655 | * it as a non-match. |
| 656 | * - if one rule returns OK, then return OK |
| 657 | * - if one rule returns KO, then return KO |
| 658 | */ |
| 659 | |
Willy Tarreau | d869b24 | 2009-03-15 14:43:58 +0100 | [diff] [blame] | 660 | 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] | 661 | partial = 0; |
| 662 | else |
| 663 | partial = ACL_PARTIAL; |
| 664 | |
| 665 | list_for_each_entry(rule, &s->fe->tcp_req.inspect_rules, list) { |
| 666 | int ret = ACL_PAT_PASS; |
| 667 | |
| 668 | if (rule->cond) { |
Willy Tarreau | 51d5dad | 2009-07-12 10:10:05 +0200 | [diff] [blame] | 669 | ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ | partial); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 670 | if (ret == ACL_PAT_MISS) { |
| 671 | buffer_write_dis(req); |
| 672 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | d869b24 | 2009-03-15 14:43:58 +0100 | [diff] [blame] | 673 | if (!tick_isset(req->analyse_exp) && s->fe->tcp_req.inspect_delay) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 674 | req->analyse_exp = tick_add_ifset(now_ms, s->fe->tcp_req.inspect_delay); |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | ret = acl_pass(ret); |
| 679 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 680 | ret = !ret; |
| 681 | } |
| 682 | |
| 683 | if (ret) { |
| 684 | /* we have a matching rule. */ |
| 685 | if (rule->action == TCP_ACT_REJECT) { |
| 686 | buffer_abort(req); |
| 687 | buffer_abort(s->rep); |
| 688 | req->analysers = 0; |
| 689 | s->fe->failed_req++; |
| 690 | if (!(s->flags & SN_ERR_MASK)) |
| 691 | s->flags |= SN_ERR_PRXCOND; |
| 692 | if (!(s->flags & SN_FINST_MASK)) |
| 693 | s->flags |= SN_FINST_R; |
| 694 | return 0; |
| 695 | } |
| 696 | /* otherwise accept */ |
| 697 | break; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | /* if we get there, it means we have no rule which matches, or |
| 702 | * we have an explicit accept, so we apply the default accept. |
| 703 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 704 | req->analysers &= ~an_bit; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 705 | req->analyse_exp = TICK_ETERNITY; |
| 706 | return 1; |
| 707 | } |
| 708 | |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 709 | /* Apply RDP cookie persistence to the current session. For this, the function |
| 710 | * tries to extract an RDP cookie from the request buffer, and look for the |
| 711 | * matching server in the list. If the server is found, it is assigned to the |
| 712 | * session. This always returns 1, and the analyser removes itself from the |
| 713 | * list. Nothing is performed if a server was already assigned. |
| 714 | */ |
| 715 | int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit) |
| 716 | { |
| 717 | struct proxy *px = s->be; |
| 718 | int ret; |
| 719 | struct acl_expr expr; |
| 720 | struct acl_test test; |
| 721 | struct server *srv = px->srv; |
| 722 | struct sockaddr_in addr; |
| 723 | char *p; |
| 724 | |
| 725 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 726 | now_ms, __FUNCTION__, |
| 727 | s, |
| 728 | req, |
| 729 | req->rex, req->wex, |
| 730 | req->flags, |
| 731 | req->l, |
| 732 | req->analysers); |
| 733 | |
| 734 | if (s->flags & SN_ASSIGNED) |
| 735 | goto no_cookie; |
| 736 | |
| 737 | memset(&expr, 0, sizeof(expr)); |
| 738 | memset(&test, 0, sizeof(test)); |
| 739 | |
| 740 | expr.arg.str = s->be->rdp_cookie_name; |
| 741 | expr.arg_len = s->be->rdp_cookie_len; |
| 742 | |
| 743 | ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test); |
| 744 | if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0) |
| 745 | goto no_cookie; |
| 746 | |
| 747 | memset(&addr, 0, sizeof(addr)); |
| 748 | addr.sin_family = AF_INET; |
| 749 | |
| 750 | /* Considering an rdp cookie detected using acl, test.ptr ended with <cr><lf> and should return */ |
| 751 | addr.sin_addr.s_addr = strtoul(test.ptr, &p, 10); |
| 752 | if (*p != '.') |
| 753 | goto no_cookie; |
| 754 | p++; |
| 755 | addr.sin_port = (unsigned short)strtoul(p, &p, 10); |
| 756 | if (*p != '.') |
| 757 | goto no_cookie; |
| 758 | |
| 759 | while (srv) { |
| 760 | if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) { |
| 761 | if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) { |
| 762 | /* we found the server and it is usable */ |
| 763 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 764 | s->srv = srv; |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | srv = srv->next; |
| 769 | } |
| 770 | |
| 771 | no_cookie: |
| 772 | req->analysers &= ~an_bit; |
| 773 | req->analyse_exp = TICK_ETERNITY; |
| 774 | return 1; |
| 775 | } |
| 776 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 777 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 778 | /* This function should be called to parse a line starting with the "tcp-request" |
| 779 | * keyword. |
| 780 | */ |
| 781 | static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx, |
| 782 | struct proxy *defpx, char *err, int errlen) |
| 783 | { |
| 784 | const char *ptr = NULL; |
Willy Tarreau | c7e961e | 2008-08-17 17:13:47 +0200 | [diff] [blame] | 785 | unsigned int val; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 786 | int retlen; |
| 787 | |
| 788 | if (!*args[1]) { |
| 789 | snprintf(err, errlen, "missing argument for '%s' in %s '%s'", |
| 790 | args[0], proxy_type_str(proxy), curpx->id); |
| 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | if (!strcmp(args[1], "inspect-delay")) { |
| 795 | if (curpx == defpx) { |
| 796 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 797 | args[0], args[1]); |
| 798 | return -1; |
| 799 | } |
| 800 | |
| 801 | if (!(curpx->cap & PR_CAP_FE)) { |
| 802 | snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability", |
| 803 | args[0], args[1], proxy_type_str(proxy), curpx->id, |
| 804 | "frontend"); |
| 805 | return 1; |
| 806 | } |
| 807 | |
| 808 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
| 809 | retlen = snprintf(err, errlen, |
| 810 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
| 811 | args[0], args[1], proxy_type_str(proxy), curpx->id); |
| 812 | if (ptr && retlen < errlen) |
| 813 | retlen += snprintf(err+retlen, errlen - retlen, |
| 814 | " (unexpected character '%c')", *ptr); |
| 815 | return -1; |
| 816 | } |
| 817 | |
| 818 | if (curpx->tcp_req.inspect_delay) { |
| 819 | snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'", |
| 820 | args[0], args[1], proxy_type_str(proxy), curpx->id); |
| 821 | return 1; |
| 822 | } |
| 823 | curpx->tcp_req.inspect_delay = val; |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | if (!strcmp(args[1], "content")) { |
| 828 | int action; |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 829 | int warn = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 830 | int pol = ACL_COND_NONE; |
| 831 | struct acl_cond *cond; |
| 832 | struct tcp_rule *rule; |
| 833 | |
| 834 | if (curpx == defpx) { |
| 835 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 836 | args[0], args[1]); |
| 837 | return -1; |
| 838 | } |
| 839 | |
| 840 | if (!strcmp(args[2], "accept")) |
| 841 | action = TCP_ACT_ACCEPT; |
| 842 | else if (!strcmp(args[2], "reject")) |
| 843 | action = TCP_ACT_REJECT; |
| 844 | else { |
| 845 | retlen = snprintf(err, errlen, |
| 846 | "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')", |
| 847 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]); |
| 848 | return -1; |
| 849 | } |
| 850 | |
| 851 | pol = ACL_COND_NONE; |
| 852 | cond = NULL; |
| 853 | |
Willy Tarreau | 606ad73 | 2009-07-14 21:17:05 +0200 | [diff] [blame] | 854 | if (!*args[3]) |
| 855 | pol = ACL_COND_NONE; |
| 856 | else if (!strcmp(args[3], "if")) |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 857 | pol = ACL_COND_IF; |
| 858 | else if (!strcmp(args[3], "unless")) |
| 859 | pol = ACL_COND_UNLESS; |
Willy Tarreau | 606ad73 | 2009-07-14 21:17:05 +0200 | [diff] [blame] | 860 | else { |
| 861 | retlen = snprintf(err, errlen, |
| 862 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')", |
| 863 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[3]); |
| 864 | return -1; |
| 865 | } |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 866 | |
| 867 | /* Note: we consider "if TRUE" when there is no condition */ |
| 868 | if (pol != ACL_COND_NONE && |
| 869 | (cond = parse_acl_cond((const char **)args+4, &curpx->acl, pol)) == NULL) { |
| 870 | retlen = snprintf(err, errlen, |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 871 | "error detected in %s '%s' while parsing '%s' condition", |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 872 | proxy_type_str(curpx), curpx->id, args[3]); |
| 873 | return -1; |
| 874 | } |
| 875 | |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 876 | // FIXME: how to set this ? |
| 877 | // cond->line = linenum; |
Willy Tarreau | a9fb083 | 2009-07-10 20:53:53 +0200 | [diff] [blame] | 878 | if (cond) |
| 879 | curpx->acl_requires |= cond->requires; |
Willy Tarreau | 1a21194 | 2009-07-14 13:53:17 +0200 | [diff] [blame] | 880 | if (cond && (cond->requires & ACL_USE_RTR_ANY)) { |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 881 | struct acl *acl; |
| 882 | const char *name; |
| 883 | |
Willy Tarreau | 1a21194 | 2009-07-14 13:53:17 +0200 | [diff] [blame] | 884 | acl = cond_find_require(cond, ACL_USE_RTR_ANY); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 885 | name = acl ? acl->name : "(unknown)"; |
| 886 | |
| 887 | retlen = snprintf(err, errlen, |
Willy Tarreau | 1a21194 | 2009-07-14 13:53:17 +0200 | [diff] [blame] | 888 | "acl '%s' involves some response-only criteria which will be ignored.", |
| 889 | name); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 890 | warn++; |
| 891 | } |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 892 | rule = (struct tcp_rule *)calloc(1, sizeof(*rule)); |
| 893 | rule->cond = cond; |
| 894 | rule->action = action; |
| 895 | LIST_INIT(&rule->list); |
| 896 | LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 897 | return warn; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | snprintf(err, errlen, "unknown argument '%s' after '%s' in %s '%s'", |
| 901 | args[1], args[0], proxy_type_str(proxy), curpx->id); |
| 902 | return -1; |
| 903 | } |
| 904 | |
| 905 | /* return the number of bytes in the request buffer */ |
| 906 | static int |
| 907 | acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir, |
| 908 | struct acl_expr *expr, struct acl_test *test) |
| 909 | { |
| 910 | if (!l4 || !l4->req) |
| 911 | return 0; |
| 912 | |
| 913 | test->i = l4->req->l; |
| 914 | test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE; |
| 915 | return 1; |
| 916 | } |
| 917 | |
Willy Tarreau | 655e26a | 2008-07-15 18:58:05 +0200 | [diff] [blame] | 918 | /* Return the version of the SSL protocol in the request. It supports both |
| 919 | * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for |
| 920 | * the hello message. The SSLv3 format is described in RFC 2246 p49, and the |
| 921 | * SSLv2 format is described here, and completed p67 of RFC 2246 : |
| 922 | * http://wp.netscape.com/eng/security/SSL_2.html |
| 923 | * |
| 924 | * Note: this decoder only works with non-wrapping data. |
| 925 | */ |
| 926 | static int |
| 927 | acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 928 | struct acl_expr *expr, struct acl_test *test) |
| 929 | { |
| 930 | int version, bleft, msg_len; |
| 931 | const unsigned char *data; |
| 932 | |
| 933 | if (!l4 || !l4->req) |
| 934 | return 0; |
| 935 | |
| 936 | msg_len = 0; |
| 937 | bleft = l4->req->l; |
| 938 | if (!bleft) |
| 939 | goto too_short; |
| 940 | |
Willy Tarreau | c7e961e | 2008-08-17 17:13:47 +0200 | [diff] [blame] | 941 | data = (const unsigned char *)l4->req->w; |
Willy Tarreau | 655e26a | 2008-07-15 18:58:05 +0200 | [diff] [blame] | 942 | if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) { |
| 943 | /* SSLv3 header format */ |
| 944 | if (bleft < 5) |
| 945 | goto too_short; |
| 946 | |
| 947 | version = (data[1] << 16) + data[2]; /* version: major, minor */ |
| 948 | msg_len = (data[3] << 8) + data[4]; /* record length */ |
| 949 | |
| 950 | /* format introduced with SSLv3 */ |
| 951 | if (version < 0x00030000) |
| 952 | goto not_ssl; |
| 953 | |
| 954 | /* message length between 1 and 2^14 + 2048 */ |
| 955 | if (msg_len < 1 || msg_len > ((1<<14) + 2048)) |
| 956 | goto not_ssl; |
| 957 | |
| 958 | bleft -= 5; data += 5; |
| 959 | } else { |
| 960 | /* SSLv2 header format, only supported for hello (msg type 1) */ |
| 961 | int rlen, plen, cilen, silen, chlen; |
| 962 | |
| 963 | if (*data & 0x80) { |
| 964 | if (bleft < 3) |
| 965 | goto too_short; |
| 966 | /* short header format : 15 bits for length */ |
| 967 | rlen = ((data[0] & 0x7F) << 8) | data[1]; |
| 968 | plen = 0; |
| 969 | bleft -= 2; data += 2; |
| 970 | } else { |
| 971 | if (bleft < 4) |
| 972 | goto too_short; |
| 973 | /* long header format : 14 bits for length + pad length */ |
| 974 | rlen = ((data[0] & 0x3F) << 8) | data[1]; |
| 975 | plen = data[2]; |
| 976 | bleft -= 3; data += 2; |
| 977 | } |
| 978 | |
| 979 | if (*data != 0x01) |
| 980 | goto not_ssl; |
| 981 | bleft--; data++; |
| 982 | |
| 983 | if (bleft < 8) |
| 984 | goto too_short; |
| 985 | version = (data[0] << 16) + data[1]; /* version: major, minor */ |
| 986 | cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */ |
| 987 | silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */ |
| 988 | chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */ |
| 989 | |
| 990 | bleft -= 8; data += 8; |
| 991 | if (cilen % 3 != 0) |
| 992 | goto not_ssl; |
| 993 | if (silen && silen != 16) |
| 994 | goto not_ssl; |
| 995 | if (chlen < 16 || chlen > 32) |
| 996 | goto not_ssl; |
| 997 | if (rlen != 9 + cilen + silen + chlen) |
| 998 | goto not_ssl; |
| 999 | |
| 1000 | /* focus on the remaining data length */ |
| 1001 | msg_len = cilen + silen + chlen + plen; |
| 1002 | } |
| 1003 | /* We could recursively check that the buffer ends exactly on an SSL |
| 1004 | * fragment boundary and that a possible next segment is still SSL, |
| 1005 | * but that's a bit pointless. However, we could still check that |
| 1006 | * all the part of the request which fits in a buffer is already |
| 1007 | * there. |
| 1008 | */ |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 1009 | if (msg_len > l4->req->max_len + l4->req->data - l4->req->w) |
| 1010 | msg_len = l4->req->max_len + l4->req->data - l4->req->w; |
Willy Tarreau | 655e26a | 2008-07-15 18:58:05 +0200 | [diff] [blame] | 1011 | |
| 1012 | if (bleft < msg_len) |
| 1013 | goto too_short; |
| 1014 | |
| 1015 | /* OK that's enough. We have at least the whole message, and we have |
| 1016 | * the protocol version. |
| 1017 | */ |
| 1018 | test->i = version; |
| 1019 | test->flags = ACL_TEST_F_VOLATILE; |
| 1020 | return 1; |
| 1021 | |
| 1022 | too_short: |
| 1023 | test->flags = ACL_TEST_F_MAY_CHANGE; |
| 1024 | not_ssl: |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
Emeric Brun | bede3d0 | 2009-06-30 17:54:00 +0200 | [diff] [blame] | 1028 | int |
| 1029 | acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1030 | struct acl_expr *expr, struct acl_test *test) |
| 1031 | { |
| 1032 | int bleft; |
| 1033 | const unsigned char *data; |
| 1034 | |
| 1035 | if (!l4 || !l4->req) |
| 1036 | return 0; |
| 1037 | |
| 1038 | test->flags = 0; |
| 1039 | |
| 1040 | bleft = l4->req->l; |
| 1041 | if (bleft <= 11) |
| 1042 | goto too_short; |
| 1043 | |
| 1044 | data = (const unsigned char *)l4->req->w + 11; |
| 1045 | bleft -= 11; |
| 1046 | |
| 1047 | if (bleft <= 7) |
| 1048 | goto too_short; |
| 1049 | |
| 1050 | if (strncasecmp((const char *)data, "Cookie:", 7) != 0) |
| 1051 | goto not_cookie; |
| 1052 | |
| 1053 | data += 7; |
| 1054 | bleft -= 7; |
| 1055 | |
| 1056 | while (bleft > 0 && *data == ' ') { |
| 1057 | data++; |
| 1058 | bleft--; |
| 1059 | } |
| 1060 | |
| 1061 | if (expr->arg_len) { |
| 1062 | |
| 1063 | if (bleft <= expr->arg_len) |
| 1064 | goto too_short; |
| 1065 | |
| 1066 | if ((data[expr->arg_len] != '=') || |
| 1067 | strncasecmp(expr->arg.str, (const char *)data, expr->arg_len) != 0) |
| 1068 | goto not_cookie; |
| 1069 | |
| 1070 | data += expr->arg_len + 1; |
| 1071 | bleft -= expr->arg_len + 1; |
| 1072 | } else { |
| 1073 | while (bleft > 0 && *data != '=') { |
| 1074 | if (*data == '\r' || *data == '\n') |
| 1075 | goto not_cookie; |
| 1076 | data++; |
| 1077 | bleft--; |
| 1078 | } |
| 1079 | |
| 1080 | if (bleft < 1) |
| 1081 | goto too_short; |
| 1082 | |
| 1083 | if (*data != '=') |
| 1084 | goto not_cookie; |
| 1085 | |
| 1086 | data++; |
| 1087 | bleft--; |
| 1088 | } |
| 1089 | |
| 1090 | /* data points to cookie value */ |
| 1091 | test->ptr = (char *)data; |
| 1092 | test->len = 0; |
| 1093 | |
| 1094 | while (bleft > 0 && *data != '\r') { |
| 1095 | data++; |
| 1096 | bleft--; |
| 1097 | } |
| 1098 | |
| 1099 | if (bleft < 2) |
| 1100 | goto too_short; |
| 1101 | |
| 1102 | if (data[0] != '\r' || data[1] != '\n') |
| 1103 | goto not_cookie; |
| 1104 | |
| 1105 | test->len = (char *)data - test->ptr; |
| 1106 | test->flags = ACL_TEST_F_VOLATILE; |
| 1107 | return 1; |
| 1108 | |
| 1109 | too_short: |
| 1110 | test->flags = ACL_TEST_F_MAY_CHANGE; |
| 1111 | not_cookie: |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | static int |
| 1116 | acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1117 | struct acl_expr *expr, struct acl_test *test) |
| 1118 | { |
| 1119 | int ret; |
| 1120 | |
| 1121 | ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, test); |
| 1122 | |
| 1123 | test->ptr = NULL; |
| 1124 | test->len = 0; |
| 1125 | |
| 1126 | if (test->flags & ACL_TEST_F_MAY_CHANGE) |
| 1127 | return 0; |
| 1128 | |
| 1129 | test->flags = ACL_TEST_F_VOLATILE; |
| 1130 | test->i = ret; |
| 1131 | |
| 1132 | return 1; |
| 1133 | } |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1134 | |
| 1135 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 1136 | { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req }, |
| 1137 | { 0, NULL, NULL }, |
| 1138 | }}; |
| 1139 | |
| 1140 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 1141 | { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L4REQ_VOLATILE }, |
| 1142 | { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L4REQ_VOLATILE }, |
Emeric Brun | bede3d0 | 2009-06-30 17:54:00 +0200 | [diff] [blame] | 1143 | { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L4REQ_VOLATILE }, |
| 1144 | { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L4REQ_VOLATILE }, |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1145 | { NULL, NULL, NULL, NULL }, |
| 1146 | }}; |
| 1147 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1148 | __attribute__((constructor)) |
| 1149 | static void __tcp_protocol_init(void) |
| 1150 | { |
| 1151 | protocol_register(&proto_tcpv4); |
| 1152 | protocol_register(&proto_tcpv6); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1153 | cfg_register_keywords(&cfg_kws); |
| 1154 | acl_register_keywords(&acl_kws); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | /* |
| 1159 | * Local variables: |
| 1160 | * c-indent-level: 8 |
| 1161 | * c-basic-offset: 8 |
| 1162 | * End: |
| 1163 | */ |