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 | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 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> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 36 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 37 | #include <types/global.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 38 | #include <types/server.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 39 | |
| 40 | #include <proto/acl.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 41 | #include <proto/buffers.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 42 | #include <proto/frontend.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 43 | #include <proto/log.h> |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 44 | #include <proto/pattern.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 45 | #include <proto/port_range.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 46 | #include <proto/protocols.h> |
| 47 | #include <proto/proto_tcp.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 48 | #include <proto/proxy.h> |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 49 | #include <proto/stick_table.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 50 | #include <proto/stream_sock.h> |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 51 | #include <proto/task.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 52 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 53 | #ifdef CONFIG_HAP_CTTPROXY |
| 54 | #include <import/ip_tproxy.h> |
| 55 | #endif |
| 56 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 57 | static int tcp_bind_listeners(struct protocol *proto); |
| 58 | |
| 59 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 60 | static struct protocol proto_tcpv4 = { |
| 61 | .name = "tcpv4", |
| 62 | .sock_domain = AF_INET, |
| 63 | .sock_type = SOCK_STREAM, |
| 64 | .sock_prot = IPPROTO_TCP, |
| 65 | .sock_family = AF_INET, |
| 66 | .sock_addrlen = sizeof(struct sockaddr_in), |
| 67 | .l3_addrlen = 32/8, |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 68 | .accept = &stream_sock_accept, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 69 | .read = &stream_sock_read, |
| 70 | .write = &stream_sock_write, |
| 71 | .bind_all = tcp_bind_listeners, |
| 72 | .unbind_all = unbind_all_listeners, |
| 73 | .enable_all = enable_all_listeners, |
| 74 | .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners), |
| 75 | .nb_listeners = 0, |
| 76 | }; |
| 77 | |
| 78 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 79 | static struct protocol proto_tcpv6 = { |
| 80 | .name = "tcpv6", |
| 81 | .sock_domain = AF_INET6, |
| 82 | .sock_type = SOCK_STREAM, |
| 83 | .sock_prot = IPPROTO_TCP, |
| 84 | .sock_family = AF_INET6, |
| 85 | .sock_addrlen = sizeof(struct sockaddr_in6), |
| 86 | .l3_addrlen = 128/8, |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 87 | .accept = &stream_sock_accept, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 88 | .read = &stream_sock_read, |
| 89 | .write = &stream_sock_write, |
| 90 | .bind_all = tcp_bind_listeners, |
| 91 | .unbind_all = unbind_all_listeners, |
| 92 | .enable_all = enable_all_listeners, |
| 93 | .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners), |
| 94 | .nb_listeners = 0, |
| 95 | }; |
| 96 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 97 | |
| 98 | /* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which |
| 99 | * case we try to bind <remote>. <flags> is a 2-bit field consisting of : |
| 100 | * - 0 : ignore remote address (may even be a NULL pointer) |
| 101 | * - 1 : use provided address |
| 102 | * - 2 : use provided port |
| 103 | * - 3 : use both |
| 104 | * |
| 105 | * The function supports multiple foreign binding methods : |
| 106 | * - linux_tproxy: we directly bind to the foreign address |
| 107 | * - cttproxy: we bind to a local address then nat. |
| 108 | * The second one can be used as a fallback for the first one. |
| 109 | * This function returns 0 when everything's OK, 1 if it could not bind, to the |
| 110 | * local address, 2 if it could not bind to the foreign address. |
| 111 | */ |
| 112 | int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote) |
| 113 | { |
| 114 | struct sockaddr_in bind_addr; |
| 115 | int foreign_ok = 0; |
| 116 | int ret; |
| 117 | |
| 118 | #ifdef CONFIG_HAP_LINUX_TPROXY |
| 119 | static int ip_transp_working = 1; |
| 120 | if (flags && ip_transp_working) { |
| 121 | if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0 |
| 122 | || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0) |
| 123 | foreign_ok = 1; |
| 124 | else |
| 125 | ip_transp_working = 0; |
| 126 | } |
| 127 | #endif |
| 128 | if (flags) { |
| 129 | memset(&bind_addr, 0, sizeof(bind_addr)); |
| 130 | if (flags & 1) |
| 131 | bind_addr.sin_addr = remote->sin_addr; |
| 132 | if (flags & 2) |
| 133 | bind_addr.sin_port = remote->sin_port; |
| 134 | } |
| 135 | |
| 136 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 137 | if (foreign_ok) { |
| 138 | ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)); |
| 139 | if (ret < 0) |
| 140 | return 2; |
| 141 | } |
| 142 | else { |
| 143 | ret = bind(fd, (struct sockaddr *)local, sizeof(*local)); |
| 144 | if (ret < 0) |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | if (!flags) |
| 149 | return 0; |
| 150 | |
| 151 | #ifdef CONFIG_HAP_CTTPROXY |
| 152 | if (!foreign_ok) { |
| 153 | struct in_tproxy itp1, itp2; |
| 154 | memset(&itp1, 0, sizeof(itp1)); |
| 155 | |
| 156 | itp1.op = TPROXY_ASSIGN; |
| 157 | itp1.v.addr.faddr = bind_addr.sin_addr; |
| 158 | itp1.v.addr.fport = bind_addr.sin_port; |
| 159 | |
| 160 | /* set connect flag on socket */ |
| 161 | itp2.op = TPROXY_FLAGS; |
| 162 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 163 | |
| 164 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 && |
| 165 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) { |
| 166 | foreign_ok = 1; |
| 167 | } |
| 168 | } |
| 169 | #endif |
| 170 | if (!foreign_ok) |
| 171 | /* we could not bind to a foreign address */ |
| 172 | return 2; |
| 173 | |
| 174 | return 0; |
| 175 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 176 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * This function initiates a connection to the server assigned to this session |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 180 | * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. A |
| 181 | * source address may be pointed to by <from_addr>. Note that this is only used |
| 182 | * in case of transparent proxying. Normal source bind addresses are still |
| 183 | * determined locally (due to the possible need of a source port). |
| 184 | * |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 185 | * It can return one of : |
| 186 | * - SN_ERR_NONE if everything's OK |
| 187 | * - SN_ERR_SRVTO if there are no more servers |
| 188 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 189 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 190 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 191 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 192 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 193 | */ |
| 194 | int tcpv4_connect_server(struct stream_interface *si, |
| 195 | struct proxy *be, struct server *srv, |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 196 | struct sockaddr *srv_addr, struct sockaddr *from_addr) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 197 | { |
| 198 | int fd; |
| 199 | |
| 200 | if ((fd = si->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 201 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 202 | |
| 203 | if (errno == ENFILE) |
| 204 | send_log(be, LOG_EMERG, |
| 205 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 206 | be->id, maxfd); |
| 207 | else if (errno == EMFILE) |
| 208 | send_log(be, LOG_EMERG, |
| 209 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 210 | be->id, maxfd); |
| 211 | else if (errno == ENOBUFS || errno == ENOMEM) |
| 212 | send_log(be, LOG_EMERG, |
| 213 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 214 | be->id, maxfd); |
| 215 | /* this is a resource error */ |
| 216 | return SN_ERR_RESOURCE; |
| 217 | } |
| 218 | |
| 219 | if (fd >= global.maxsock) { |
| 220 | /* do not log anything there, it's a normal condition when this option |
| 221 | * is used to serialize connections to a server ! |
| 222 | */ |
| 223 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 224 | close(fd); |
| 225 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
| 226 | } |
| 227 | |
| 228 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 229 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
| 230 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 231 | close(fd); |
| 232 | return SN_ERR_INTERNAL; |
| 233 | } |
| 234 | |
| 235 | if (be->options & PR_O_TCP_SRV_KA) |
| 236 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 237 | |
| 238 | if (be->options & PR_O_TCP_NOLING) |
| 239 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 240 | |
| 241 | /* allow specific binding : |
| 242 | * - server-specific at first |
| 243 | * - proxy-specific next |
| 244 | */ |
| 245 | if (srv != NULL && srv->state & SRV_BIND_SRC) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 246 | int ret, flags = 0; |
| 247 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 248 | switch (srv->state & SRV_TPROXY_MASK) { |
| 249 | case SRV_TPROXY_ADDR: |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 250 | case SRV_TPROXY_CLI: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 251 | flags = 3; |
| 252 | break; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 253 | case SRV_TPROXY_CIP: |
Willy Tarreau | 090466c | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 254 | case SRV_TPROXY_DYN: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 255 | flags = 1; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 256 | break; |
| 257 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 258 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 259 | #ifdef SO_BINDTODEVICE |
| 260 | /* Note: this might fail if not CAP_NET_RAW */ |
| 261 | if (srv->iface_name) |
| 262 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1); |
| 263 | #endif |
| 264 | |
| 265 | if (srv->sport_range) { |
| 266 | int attempts = 10; /* should be more than enough to find a spare port */ |
| 267 | struct sockaddr_in src; |
| 268 | |
| 269 | ret = 1; |
| 270 | src = srv->source_addr; |
| 271 | |
| 272 | do { |
| 273 | /* note: in case of retry, we may have to release a previously |
| 274 | * allocated port, hence this loop's construct. |
| 275 | */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 276 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 277 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 278 | |
| 279 | if (!attempts) |
| 280 | break; |
| 281 | attempts--; |
| 282 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 283 | fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range); |
| 284 | if (!fdinfo[fd].local_port) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 285 | break; |
| 286 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 287 | fdinfo[fd].port_range = srv->sport_range; |
| 288 | src.sin_port = htons(fdinfo[fd].local_port); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 289 | |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 290 | ret = tcpv4_bind_socket(fd, flags, &src, (struct sockaddr_in *)from_addr); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 291 | } while (ret != 0); /* binding NOK */ |
| 292 | } |
| 293 | else { |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 294 | ret = tcpv4_bind_socket(fd, flags, &srv->source_addr, (struct sockaddr_in *)from_addr); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | if (ret) { |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 298 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 299 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 300 | close(fd); |
| 301 | |
| 302 | if (ret == 1) { |
| 303 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 304 | be->id, srv->id); |
| 305 | send_log(be, LOG_EMERG, |
| 306 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 307 | be->id, srv->id); |
| 308 | } else { |
| 309 | Alert("Cannot bind to tproxy 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 tproxy source address before connect() for server %s/%s.\n", |
| 313 | be->id, srv->id); |
| 314 | } |
| 315 | return SN_ERR_RESOURCE; |
| 316 | } |
| 317 | } |
| 318 | else if (be->options & PR_O_BIND_SRC) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 319 | int ret, flags = 0; |
| 320 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 321 | switch (be->options & PR_O_TPXY_MASK) { |
| 322 | case PR_O_TPXY_ADDR: |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 323 | case PR_O_TPXY_CLI: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 324 | flags = 3; |
| 325 | break; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 326 | case PR_O_TPXY_CIP: |
Willy Tarreau | 090466c | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 327 | case PR_O_TPXY_DYN: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 328 | flags = 1; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 329 | break; |
| 330 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 331 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 332 | #ifdef SO_BINDTODEVICE |
| 333 | /* Note: this might fail if not CAP_NET_RAW */ |
| 334 | if (be->iface_name) |
| 335 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1); |
| 336 | #endif |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 337 | ret = tcpv4_bind_socket(fd, flags, &be->source_addr, (struct sockaddr_in *)from_addr); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 338 | if (ret) { |
| 339 | close(fd); |
| 340 | if (ret == 1) { |
| 341 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", |
| 342 | be->id); |
| 343 | send_log(be, LOG_EMERG, |
| 344 | "Cannot bind to source address before connect() for proxy %s.\n", |
| 345 | be->id); |
| 346 | } else { |
| 347 | Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n", |
| 348 | be->id); |
| 349 | send_log(be, LOG_EMERG, |
| 350 | "Cannot bind to tproxy source address before connect() for proxy %s.\n", |
| 351 | be->id); |
| 352 | } |
| 353 | return SN_ERR_RESOURCE; |
| 354 | } |
| 355 | } |
| 356 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 357 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 358 | /* disabling tcp quick ack now allows the first request to leave the |
| 359 | * machine with the first ACK. We only do this if there are pending |
| 360 | * data in the buffer. |
| 361 | */ |
| 362 | if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max) |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 363 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 364 | #endif |
| 365 | |
Willy Tarreau | e803de2 | 2010-01-21 17:43:04 +0100 | [diff] [blame] | 366 | if (global.tune.server_sndbuf) |
| 367 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 368 | |
| 369 | if (global.tune.server_rcvbuf) |
| 370 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 371 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 372 | if ((connect(fd, (struct sockaddr *)srv_addr, sizeof(struct sockaddr_in)) == -1) && |
| 373 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 374 | |
| 375 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 376 | char *msg; |
| 377 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 378 | msg = "no free ports"; |
| 379 | else |
| 380 | msg = "local address already in use"; |
| 381 | |
| 382 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 383 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 384 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 385 | close(fd); |
| 386 | send_log(be, LOG_EMERG, |
| 387 | "Connect() failed for server %s/%s: %s.\n", |
| 388 | be->id, srv->id, msg); |
| 389 | return SN_ERR_RESOURCE; |
| 390 | } else if (errno == ETIMEDOUT) { |
| 391 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
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 | return SN_ERR_SRVTO; |
| 396 | } else { |
| 397 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 398 | //qfprintf(stderr,"Connect(): %d", errno); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 399 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 400 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 401 | close(fd); |
| 402 | return SN_ERR_SRVCL; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | fdtab[fd].owner = si; |
| 407 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 408 | fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY; |
| 409 | fdtab[fd].cb[DIR_RD].f = &stream_sock_read; |
| 410 | fdtab[fd].cb[DIR_RD].b = si->ib; |
| 411 | fdtab[fd].cb[DIR_WR].f = &stream_sock_write; |
| 412 | fdtab[fd].cb[DIR_WR].b = si->ob; |
| 413 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 414 | fdinfo[fd].peeraddr = (struct sockaddr *)srv_addr; |
| 415 | fdinfo[fd].peerlen = sizeof(struct sockaddr_in); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 416 | |
| 417 | fd_insert(fd); |
| 418 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
| 419 | |
| 420 | si->state = SI_ST_CON; |
| 421 | si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */ |
| 422 | si->exp = tick_add_ifset(now_ms, be->timeout.connect); |
| 423 | |
| 424 | return SN_ERR_NONE; /* connection is OK */ |
| 425 | } |
| 426 | |
| 427 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 428 | /* This function tries to bind a TCPv4/v6 listener. It may return a warning or |
| 429 | * an error message in <err> if the message is at most <errlen> bytes long |
| 430 | * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN, |
| 431 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 432 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 433 | * error occurred but that it may vanish after a retry (eg: port in use), and |
| 434 | * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter |
| 435 | * the meaning of the error, but just indicate that a message is present which |
| 436 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 437 | * that it's pointless to try to start other listeners. No error message is |
| 438 | * returned if errlen is NULL. |
| 439 | */ |
| 440 | int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 441 | { |
| 442 | __label__ tcp_return, tcp_close_return; |
| 443 | int fd, err; |
| 444 | const char *msg = NULL; |
| 445 | |
| 446 | /* ensure we never return garbage */ |
| 447 | if (errmsg && errlen) |
| 448 | *errmsg = 0; |
| 449 | |
| 450 | if (listener->state != LI_ASSIGNED) |
| 451 | return ERR_NONE; /* already bound */ |
| 452 | |
| 453 | err = ERR_NONE; |
| 454 | |
| 455 | if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 456 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 457 | msg = "cannot create listening socket"; |
| 458 | goto tcp_return; |
| 459 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 460 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 461 | if (fd >= global.maxsock) { |
| 462 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 463 | msg = "not enough free sockets (raise '-n' parameter)"; |
| 464 | goto tcp_close_return; |
| 465 | } |
| 466 | |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 467 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 468 | err |= ERR_FATAL | ERR_ALERT; |
| 469 | msg = "cannot make socket non-blocking"; |
| 470 | goto tcp_close_return; |
| 471 | } |
| 472 | |
| 473 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 474 | /* not fatal but should be reported */ |
| 475 | msg = "cannot do so_reuseaddr"; |
| 476 | err |= ERR_ALERT; |
| 477 | } |
| 478 | |
| 479 | if (listener->options & LI_O_NOLINGER) |
| 480 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 481 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 482 | #ifdef SO_REUSEPORT |
| 483 | /* OpenBSD supports this. As it's present in old libc versions of Linux, |
| 484 | * it might return an error that we will silently ignore. |
| 485 | */ |
| 486 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one)); |
| 487 | #endif |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 488 | #ifdef CONFIG_HAP_LINUX_TPROXY |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 489 | if ((listener->options & LI_O_FOREIGN) |
Willy Tarreau | 0a45989 | 2008-01-13 17:37:16 +0100 | [diff] [blame] | 490 | && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1) |
| 491 | && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) { |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 492 | msg = "cannot make listening socket transparent"; |
| 493 | err |= ERR_ALERT; |
| 494 | } |
| 495 | #endif |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 496 | #ifdef SO_BINDTODEVICE |
| 497 | /* Note: this might fail if not CAP_NET_RAW */ |
| 498 | if (listener->interface) { |
| 499 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 500 | listener->interface, strlen(listener->interface) + 1) == -1) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 501 | msg = "cannot bind listener to device"; |
| 502 | err |= ERR_WARN; |
| 503 | } |
| 504 | } |
| 505 | #endif |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 506 | #if defined(TCP_MAXSEG) |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 507 | if (listener->maxseg) { |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 508 | if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 509 | &listener->maxseg, sizeof(listener->maxseg)) == -1) { |
| 510 | msg = "cannot set MSS"; |
| 511 | err |= ERR_WARN; |
| 512 | } |
| 513 | } |
| 514 | #endif |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 515 | #if defined(TCP_DEFER_ACCEPT) |
| 516 | if (listener->options & LI_O_DEF_ACCEPT) { |
| 517 | /* defer accept by up to one second */ |
| 518 | int accept_delay = 1; |
| 519 | if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) { |
| 520 | msg = "cannot enable DEFER_ACCEPT"; |
| 521 | err |= ERR_WARN; |
| 522 | } |
| 523 | } |
| 524 | #endif |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 525 | if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) { |
| 526 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 527 | msg = "cannot bind socket"; |
| 528 | goto tcp_close_return; |
| 529 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 530 | |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 531 | if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 532 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 533 | msg = "cannot listen to socket"; |
| 534 | goto tcp_close_return; |
| 535 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 536 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 537 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 538 | if (listener->options & LI_O_NOQUICKACK) |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 539 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero)); |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 540 | #endif |
| 541 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 542 | /* the socket is ready */ |
| 543 | listener->fd = fd; |
| 544 | listener->state = LI_LISTEN; |
| 545 | |
Willy Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 546 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 547 | fdtab[fd].state = FD_STLISTEN; |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 548 | fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0); |
| 549 | fdtab[fd].cb[DIR_RD].f = listener->proto->accept; |
| 550 | fdtab[fd].cb[DIR_WR].f = NULL; /* never called */ |
| 551 | fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL; |
Willy Tarreau | 5d707e1 | 2009-06-28 11:09:07 +0200 | [diff] [blame] | 552 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 553 | fdinfo[fd].peeraddr = NULL; |
| 554 | fdinfo[fd].peerlen = 0; |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 555 | fd_insert(fd); |
| 556 | |
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 |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame^] | 618 | * request. It relies on buffers flags, and updates s->req->analysers. The |
| 619 | * function may be called for frontend rules and backend rules. It only relies |
| 620 | * on the backend pointer so this works for both cases. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 621 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 622 | 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] | 623 | { |
| 624 | struct tcp_rule *rule; |
| 625 | int partial; |
| 626 | |
| 627 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 628 | now_ms, __FUNCTION__, |
| 629 | s, |
| 630 | req, |
| 631 | req->rex, req->wex, |
| 632 | req->flags, |
| 633 | req->l, |
| 634 | req->analysers); |
| 635 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 636 | /* We don't know whether we have enough data, so must proceed |
| 637 | * this way : |
| 638 | * - iterate through all rules in their declaration order |
| 639 | * - if one rule returns MISS, it means the inspect delay is |
| 640 | * not over yet, then return immediately, otherwise consider |
| 641 | * it as a non-match. |
| 642 | * - if one rule returns OK, then return OK |
| 643 | * - if one rule returns KO, then return KO |
| 644 | */ |
| 645 | |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame^] | 646 | if (req->flags & BF_SHUTR || !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms)) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 647 | partial = 0; |
| 648 | else |
| 649 | partial = ACL_PARTIAL; |
| 650 | |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame^] | 651 | list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) { |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 652 | int ret = ACL_PAT_PASS; |
| 653 | |
| 654 | if (rule->cond) { |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame^] | 655 | ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_REQ | partial); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 656 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 657 | buffer_dont_connect(req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 658 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame^] | 659 | if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay) |
| 660 | req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | ret = acl_pass(ret); |
| 665 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 666 | ret = !ret; |
| 667 | } |
| 668 | |
| 669 | if (ret) { |
| 670 | /* we have a matching rule. */ |
| 671 | if (rule->action == TCP_ACT_REJECT) { |
| 672 | buffer_abort(req); |
| 673 | buffer_abort(s->rep); |
| 674 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 675 | |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame^] | 676 | s->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 677 | if (s->listener->counters) |
Willy Tarreau | 23968d8 | 2010-05-23 23:50:44 +0200 | [diff] [blame] | 678 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 679 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 680 | if (!(s->flags & SN_ERR_MASK)) |
| 681 | s->flags |= SN_ERR_PRXCOND; |
| 682 | if (!(s->flags & SN_FINST_MASK)) |
| 683 | s->flags |= SN_FINST_R; |
| 684 | return 0; |
| 685 | } |
| 686 | /* otherwise accept */ |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /* if we get there, it means we have no rule which matches, or |
| 692 | * we have an explicit accept, so we apply the default accept. |
| 693 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 694 | req->analysers &= ~an_bit; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 695 | req->analyse_exp = TICK_ETERNITY; |
| 696 | return 1; |
| 697 | } |
| 698 | |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 699 | /* This function performs the TCP layer4 analysis on the current request. It |
| 700 | * returns 0 if a reject rule matches, otherwise 1 if either an accept rule |
| 701 | * matches or if no more rule matches. It can only use rules which don't need |
| 702 | * any data. |
| 703 | */ |
| 704 | int tcp_exec_req_rules(struct session *s) |
| 705 | { |
| 706 | struct tcp_rule *rule; |
| 707 | int ret; |
| 708 | |
| 709 | list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) { |
| 710 | ret = ACL_PAT_PASS; |
| 711 | |
| 712 | if (rule->cond) { |
| 713 | ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ); |
| 714 | ret = acl_pass(ret); |
| 715 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 716 | ret = !ret; |
| 717 | } |
| 718 | |
| 719 | if (ret) { |
| 720 | /* we have a matching rule. */ |
| 721 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 2799e98 | 2010-06-05 15:43:21 +0200 | [diff] [blame] | 722 | s->fe->counters.denied_conn++; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 723 | if (s->listener->counters) |
Willy Tarreau | 2799e98 | 2010-06-05 15:43:21 +0200 | [diff] [blame] | 724 | s->listener->counters->denied_conn++; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 725 | |
| 726 | if (!(s->flags & SN_ERR_MASK)) |
| 727 | s->flags |= SN_ERR_PRXCOND; |
| 728 | if (!(s->flags & SN_FINST_MASK)) |
| 729 | s->flags |= SN_FINST_R; |
| 730 | return 0; |
| 731 | } |
| 732 | /* otherwise it's an accept */ |
| 733 | break; |
| 734 | } |
| 735 | } |
| 736 | return 1; |
| 737 | } |
| 738 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 739 | /* This function should be called to parse a line starting with the "tcp-request" |
| 740 | * keyword. |
| 741 | */ |
| 742 | static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx, |
| 743 | struct proxy *defpx, char *err, int errlen) |
| 744 | { |
| 745 | const char *ptr = NULL; |
Willy Tarreau | c7e961e | 2008-08-17 17:13:47 +0200 | [diff] [blame] | 746 | unsigned int val; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 747 | int retlen; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 748 | int warn = 0; |
| 749 | int pol = ACL_COND_NONE; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 750 | int arg; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 751 | struct tcp_rule *rule; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 752 | |
| 753 | if (!*args[1]) { |
| 754 | snprintf(err, errlen, "missing argument for '%s' in %s '%s'", |
Willy Tarreau | f535683 | 2010-06-14 18:40:26 +0200 | [diff] [blame] | 755 | args[0], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 756 | return -1; |
| 757 | } |
| 758 | |
| 759 | if (!strcmp(args[1], "inspect-delay")) { |
| 760 | if (curpx == defpx) { |
| 761 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 762 | args[0], args[1]); |
| 763 | return -1; |
| 764 | } |
| 765 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 766 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
| 767 | retlen = snprintf(err, errlen, |
| 768 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
Willy Tarreau | f535683 | 2010-06-14 18:40:26 +0200 | [diff] [blame] | 769 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 770 | if (ptr && retlen < errlen) |
| 771 | retlen += snprintf(err+retlen, errlen - retlen, |
| 772 | " (unexpected character '%c')", *ptr); |
| 773 | return -1; |
| 774 | } |
| 775 | |
| 776 | if (curpx->tcp_req.inspect_delay) { |
| 777 | snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'", |
Willy Tarreau | f535683 | 2010-06-14 18:40:26 +0200 | [diff] [blame] | 778 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 779 | return 1; |
| 780 | } |
| 781 | curpx->tcp_req.inspect_delay = val; |
| 782 | return 0; |
| 783 | } |
| 784 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 785 | rule = (struct tcp_rule *)calloc(1, sizeof(*rule)); |
| 786 | arg = 1; |
| 787 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 788 | if (!strcmp(args[1], "content")) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 789 | if (curpx == defpx) { |
| 790 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 791 | args[0], args[1]); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 792 | goto error; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | if (!strcmp(args[2], "accept")) |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 796 | rule->action = TCP_ACT_ACCEPT; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 797 | else if (!strcmp(args[2], "reject")) |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 798 | rule->action = TCP_ACT_REJECT; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 799 | else { |
| 800 | retlen = snprintf(err, errlen, |
| 801 | "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')", |
| 802 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 803 | goto error; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | pol = ACL_COND_NONE; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 807 | rule->cond = NULL; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 808 | |
Willy Tarreau | ef6494c | 2010-01-28 17:12:36 +0100 | [diff] [blame] | 809 | if (strcmp(args[3], "if") == 0 || strcmp(args[3], "unless") == 0) { |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 810 | if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+3)) == NULL) { |
Willy Tarreau | ef6494c | 2010-01-28 17:12:36 +0100 | [diff] [blame] | 811 | retlen = snprintf(err, errlen, |
| 812 | "error detected in %s '%s' while parsing '%s' condition", |
| 813 | proxy_type_str(curpx), curpx->id, args[3]); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 814 | goto error; |
Willy Tarreau | ef6494c | 2010-01-28 17:12:36 +0100 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | else if (*args[3]) { |
Willy Tarreau | 606ad73 | 2009-07-14 21:17:05 +0200 | [diff] [blame] | 818 | retlen = snprintf(err, errlen, |
| 819 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')", |
| 820 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[3]); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 821 | goto error; |
Willy Tarreau | 606ad73 | 2009-07-14 21:17:05 +0200 | [diff] [blame] | 822 | } |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 823 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 824 | if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) { |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 825 | struct acl *acl; |
| 826 | const char *name; |
| 827 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 828 | acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 829 | name = acl ? acl->name : "(unknown)"; |
| 830 | |
| 831 | retlen = snprintf(err, errlen, |
Willy Tarreau | 1a21194 | 2009-07-14 13:53:17 +0200 | [diff] [blame] | 832 | "acl '%s' involves some response-only criteria which will be ignored.", |
| 833 | name); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 834 | warn++; |
| 835 | } |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 836 | LIST_INIT(&rule->list); |
| 837 | LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 838 | return warn; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 841 | /* OK so we're in front of plain L4 rules */ |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 842 | |
| 843 | if (strcmp(args[1], "accept") == 0) |
| 844 | rule->action = TCP_ACT_ACCEPT; |
| 845 | else if (strcmp(args[1], "reject") == 0) |
| 846 | rule->action = TCP_ACT_REJECT; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 847 | else { |
| 848 | retlen = snprintf(err, errlen, |
| 849 | "'%s' expects 'inspect-delay', 'content', 'accept' or 'reject', in %s '%s' (was '%s')", |
| 850 | args[0], proxy_type_str(curpx), curpx->id, args[1]); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 851 | goto error; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | if (curpx == defpx) { |
| 855 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 856 | args[0], args[1]); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 857 | goto error; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 860 | arg++; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 861 | pol = ACL_COND_NONE; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 862 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 863 | if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) { |
| 864 | if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) { |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 865 | retlen = snprintf(err, errlen, |
| 866 | "error detected in %s '%s' while parsing '%s' condition", |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 867 | proxy_type_str(curpx), curpx->id, args[arg]); |
| 868 | goto error; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 869 | } |
| 870 | } |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 871 | else if (*args[arg]) { |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 872 | retlen = snprintf(err, errlen, |
| 873 | "'%s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')", |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 874 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]); |
| 875 | goto error; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 876 | } |
| 877 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 878 | if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) { |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 879 | struct acl *acl; |
| 880 | const char *name; |
| 881 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 882 | acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY); |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 883 | name = acl ? acl->name : "(unknown)"; |
| 884 | |
| 885 | if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) { |
| 886 | retlen = snprintf(err, errlen, |
| 887 | "'%s %s' may not reference acl '%s' which makes use of payload in %s '%s'. Please use '%s content' for this.", |
| 888 | args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 889 | goto error; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 890 | } |
| 891 | if (acl->requires & ACL_USE_RTR_ANY) |
| 892 | retlen = snprintf(err, errlen, |
| 893 | "acl '%s' involves some response-only criteria which will be ignored.", |
| 894 | name); |
| 895 | |
| 896 | warn++; |
| 897 | } |
| 898 | |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 899 | LIST_INIT(&rule->list); |
| 900 | LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list); |
| 901 | return warn; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 902 | error: |
| 903 | free(rule); |
| 904 | return -1; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 905 | } |
| 906 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 907 | |
| 908 | /************************************************************************/ |
| 909 | /* All supported ACL keywords must be declared here. */ |
| 910 | /************************************************************************/ |
| 911 | |
| 912 | /* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */ |
| 913 | static int |
| 914 | acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir, |
| 915 | struct acl_expr *expr, struct acl_test *test) |
| 916 | { |
| 917 | test->i = l4->cli_addr.ss_family; |
| 918 | if (test->i == AF_INET) |
| 919 | test->ptr = (void *)&((struct sockaddr_in *)&l4->cli_addr)->sin_addr; |
| 920 | else |
| 921 | test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_addr; |
| 922 | test->flags = ACL_TEST_F_READ_ONLY; |
| 923 | return 1; |
| 924 | } |
| 925 | |
| 926 | /* extract the connection's source address */ |
| 927 | static int |
| 928 | pattern_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir, |
| 929 | const char *arg, int arg_len, union pattern_data *data) |
| 930 | { |
| 931 | data->ip.s_addr = ((struct sockaddr_in *)&l4->cli_addr)->sin_addr.s_addr; |
| 932 | return 1; |
| 933 | } |
| 934 | |
| 935 | |
| 936 | /* set test->i to the connection's source port */ |
| 937 | static int |
| 938 | acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 939 | struct acl_expr *expr, struct acl_test *test) |
| 940 | { |
| 941 | if (l4->cli_addr.ss_family == AF_INET) |
| 942 | test->i = ntohs(((struct sockaddr_in *)&l4->cli_addr)->sin_port); |
| 943 | else |
| 944 | test->i = ntohs(((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_port); |
| 945 | test->flags = 0; |
| 946 | return 1; |
| 947 | } |
| 948 | |
| 949 | |
| 950 | /* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */ |
| 951 | static int |
| 952 | acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir, |
| 953 | struct acl_expr *expr, struct acl_test *test) |
| 954 | { |
| 955 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 956 | get_frt_addr(l4); |
| 957 | |
| 958 | test->i = l4->frt_addr.ss_family; |
| 959 | if (test->i == AF_INET) |
| 960 | test->ptr = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr; |
| 961 | else |
| 962 | test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_addr; |
| 963 | test->flags = ACL_TEST_F_READ_ONLY; |
| 964 | return 1; |
| 965 | } |
| 966 | |
| 967 | |
| 968 | /* extract the connection's destination address */ |
| 969 | static int |
| 970 | pattern_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir, |
| 971 | const char *arg, int arg_len, union pattern_data *data) |
| 972 | { |
| 973 | data->ip.s_addr = ((struct sockaddr_in *)&l4->frt_addr)->sin_addr.s_addr; |
| 974 | return 1; |
| 975 | } |
| 976 | |
| 977 | /* set test->i to the frontend connexion's destination port */ |
| 978 | static int |
| 979 | acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 980 | struct acl_expr *expr, struct acl_test *test) |
| 981 | { |
| 982 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 983 | get_frt_addr(l4); |
| 984 | |
| 985 | if (l4->frt_addr.ss_family == AF_INET) |
| 986 | test->i = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port); |
| 987 | else |
| 988 | test->i = ntohs(((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_port); |
| 989 | test->flags = 0; |
| 990 | return 1; |
| 991 | } |
| 992 | |
| 993 | static int |
| 994 | pattern_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 995 | const char *arg, int arg_len, union pattern_data *data) |
| 996 | |
| 997 | { |
| 998 | data->integer = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port); |
| 999 | return 1; |
| 1000 | } |
| 1001 | |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 1002 | /* set test->i to the number of connections from the session's source address |
| 1003 | * in the table pointed to by expr. |
| 1004 | */ |
| 1005 | static int |
| 1006 | acl_fetch_src_count(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1007 | struct acl_expr *expr, struct acl_test *test) |
| 1008 | { |
| 1009 | struct stksess *ts; |
| 1010 | |
| 1011 | /* right now we only support IPv4 */ |
| 1012 | if (l4->cli_addr.ss_family != AF_INET) |
| 1013 | return 0; |
| 1014 | |
| 1015 | if (expr->arg_len) { |
| 1016 | /* another table was designated, we must look for it */ |
| 1017 | for (px = proxy; px; px = px->next) |
| 1018 | if (strcmp(px->id, expr->arg.str) == 0) |
| 1019 | break; |
| 1020 | } |
| 1021 | if (!px) |
| 1022 | return 0; /* table not found */ |
| 1023 | |
| 1024 | static_table_key.key = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr; |
| 1025 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1026 | test->i = 0; |
| 1027 | if ((ts = stktable_lookup_key(&px->table, &static_table_key)) != NULL) { |
| 1028 | void *ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CUM); |
| 1029 | if (!ptr) |
| 1030 | return 0; /* parameter not stored */ |
| 1031 | test->i = stktable_data_cast(ptr, conn_cum); |
| 1032 | } |
| 1033 | |
| 1034 | return 1; |
| 1035 | } |
| 1036 | |
| 1037 | /* set test->i to the number of connections from the session's source address |
| 1038 | * in the table pointed to by expr, after updating it. |
| 1039 | */ |
| 1040 | static int |
| 1041 | acl_fetch_src_update_count(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1042 | struct acl_expr *expr, struct acl_test *test) |
| 1043 | { |
| 1044 | struct stksess *ts; |
| 1045 | void *ptr; |
| 1046 | |
| 1047 | /* right now we only support IPv4 */ |
| 1048 | if (l4->cli_addr.ss_family != AF_INET) |
| 1049 | return 0; |
| 1050 | |
| 1051 | if (expr->arg_len) { |
| 1052 | /* another table was designated, we must look for it */ |
| 1053 | for (px = proxy; px; px = px->next) |
| 1054 | if (strcmp(px->id, expr->arg.str) == 0) |
| 1055 | break; |
| 1056 | } |
| 1057 | if (!px) |
| 1058 | return 0; |
| 1059 | |
| 1060 | static_table_key.key = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr; |
| 1061 | if ((ts = stktable_lookup_key(&px->table, &static_table_key)) == NULL) { |
| 1062 | /* entry does not exist, initialize a new one */ |
| 1063 | ts = stksess_new(&px->table, &static_table_key); |
| 1064 | if (!ts) |
| 1065 | return 0; |
| 1066 | stktable_store(&px->table, ts); |
| 1067 | } |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 1068 | else |
| 1069 | stktable_touch(&px->table, ts); |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 1070 | |
| 1071 | ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CUM); |
| 1072 | if (!ptr) |
| 1073 | return 0; /* parameter not stored in this table */ |
| 1074 | |
| 1075 | test->i = ++stktable_data_cast(ptr, conn_cum); |
| 1076 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1077 | return 1; |
| 1078 | } |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1079 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1080 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 1081 | { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req }, |
| 1082 | { 0, NULL, NULL }, |
| 1083 | }}; |
| 1084 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1085 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1086 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1087 | { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT }, |
| 1088 | { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP }, |
| 1089 | { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP }, |
| 1090 | { "dst_port", acl_parse_int, acl_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT }, |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 1091 | { "src_count", acl_parse_int, acl_fetch_src_count,acl_match_int, ACL_USE_TCP4_PERMANENT }, |
| 1092 | { "src_update_count", acl_parse_int, acl_fetch_src_update_count, acl_match_int, ACL_USE_TCP4_PERMANENT }, |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1093 | { NULL, NULL, NULL, NULL }, |
| 1094 | }}; |
| 1095 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1096 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1097 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
| 1098 | { "src", pattern_fetch_src, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
| 1099 | { "dst", pattern_fetch_dst, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
| 1100 | { "dst_port", pattern_fetch_dport, PATTERN_TYPE_INTEGER, PATTERN_FETCH_REQ }, |
| 1101 | { NULL, NULL, 0, 0 }, |
| 1102 | }}; |
| 1103 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1104 | __attribute__((constructor)) |
| 1105 | static void __tcp_protocol_init(void) |
| 1106 | { |
| 1107 | protocol_register(&proto_tcpv4); |
| 1108 | protocol_register(&proto_tcpv6); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1109 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1110 | cfg_register_keywords(&cfg_kws); |
| 1111 | acl_register_keywords(&acl_kws); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | |
| 1115 | /* |
| 1116 | * Local variables: |
| 1117 | * c-indent-level: 8 |
| 1118 | * c-basic-offset: 8 |
| 1119 | * End: |
| 1120 | */ |