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