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