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 | b824b00 | 2010-09-29 16:36:16 +0200 | [diff] [blame] | 649 | if (req->flags & (BF_SHUTR|BF_FULL) || !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)); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 698 | if (ts) { |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 699 | session_track_stkctr1(s, t, ts); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 700 | if (s->fe != s->be) |
| 701 | s->flags |= SN_BE_TRACK_SC1; |
| 702 | } |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 703 | } |
| 704 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 705 | else if (rule->action == TCP_ACT_TRK_SC2) { |
| 706 | if (!s->stkctr2_entry) { |
| 707 | /* only the first valid track-sc2 directive applies. |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 708 | * Also, note that right now we can only track SRC so we |
| 709 | * don't check how to get the key, but later we may need |
| 710 | * to consider rule->act_prm->trk_ctr.type. |
| 711 | */ |
| 712 | t = rule->act_prm.trk_ctr.table.t; |
| 713 | ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s)); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 714 | if (ts) { |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 715 | session_track_stkctr2(s, t, ts); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 716 | if (s->fe != s->be) |
| 717 | s->flags |= SN_BE_TRACK_SC2; |
| 718 | } |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | else { |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 722 | /* otherwise accept */ |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 723 | break; |
| 724 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
| 728 | /* if we get there, it means we have no rule which matches, or |
| 729 | * we have an explicit accept, so we apply the default accept. |
| 730 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 731 | req->analysers &= ~an_bit; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 732 | req->analyse_exp = TICK_ETERNITY; |
| 733 | return 1; |
| 734 | } |
| 735 | |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 736 | /* This function performs the TCP layer4 analysis on the current request. It |
| 737 | * returns 0 if a reject rule matches, otherwise 1 if either an accept rule |
| 738 | * matches or if no more rule matches. It can only use rules which don't need |
| 739 | * any data. |
| 740 | */ |
| 741 | int tcp_exec_req_rules(struct session *s) |
| 742 | { |
| 743 | struct tcp_rule *rule; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 744 | struct stksess *ts; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 745 | struct stktable *t = NULL; |
| 746 | int result = 1; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 747 | int ret; |
| 748 | |
| 749 | list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) { |
| 750 | ret = ACL_PAT_PASS; |
| 751 | |
| 752 | if (rule->cond) { |
| 753 | ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ); |
| 754 | ret = acl_pass(ret); |
| 755 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 756 | ret = !ret; |
| 757 | } |
| 758 | |
| 759 | if (ret) { |
| 760 | /* we have a matching rule. */ |
| 761 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 2799e98 | 2010-06-05 15:43:21 +0200 | [diff] [blame] | 762 | s->fe->counters.denied_conn++; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 763 | if (s->listener->counters) |
Willy Tarreau | 2799e98 | 2010-06-05 15:43:21 +0200 | [diff] [blame] | 764 | s->listener->counters->denied_conn++; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 765 | |
| 766 | if (!(s->flags & SN_ERR_MASK)) |
| 767 | s->flags |= SN_ERR_PRXCOND; |
| 768 | if (!(s->flags & SN_FINST_MASK)) |
| 769 | s->flags |= SN_FINST_R; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 770 | result = 0; |
| 771 | break; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 772 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 773 | else if (rule->action == TCP_ACT_TRK_SC1) { |
| 774 | if (!s->stkctr1_entry) { |
| 775 | /* only the first valid track-sc1 directive applies. |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 776 | * Also, note that right now we can only track SRC so we |
| 777 | * don't check how to get the key, but later we may need |
| 778 | * to consider rule->act_prm->trk_ctr.type. |
| 779 | */ |
| 780 | t = rule->act_prm.trk_ctr.table.t; |
| 781 | ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s)); |
| 782 | if (ts) |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 783 | session_track_stkctr1(s, t, ts); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 784 | } |
| 785 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 786 | else if (rule->action == TCP_ACT_TRK_SC2) { |
| 787 | if (!s->stkctr2_entry) { |
| 788 | /* only the first valid track-sc2 directive applies. |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 789 | * Also, note that right now we can only track SRC so we |
| 790 | * don't check how to get the key, but later we may need |
| 791 | * to consider rule->act_prm->trk_ctr.type. |
| 792 | */ |
| 793 | t = rule->act_prm.trk_ctr.table.t; |
| 794 | ts = stktable_get_entry(t, tcpv4_src_to_stktable_key(s)); |
| 795 | if (ts) |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 796 | session_track_stkctr2(s, t, ts); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 797 | } |
| 798 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 799 | else { |
| 800 | /* otherwise it's an accept */ |
| 801 | break; |
| 802 | } |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 803 | } |
| 804 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 805 | return result; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 806 | } |
| 807 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 808 | /* Parse a tcp-request rule. Return a negative value in case of failure */ |
| 809 | static int tcp_parse_request_rule(char **args, int arg, int section_type, |
| 810 | struct proxy *curpx, struct proxy *defpx, |
| 811 | struct tcp_rule *rule, char *err, int errlen) |
| 812 | { |
| 813 | if (curpx == defpx) { |
| 814 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 815 | args[0], args[1]); |
| 816 | return -1; |
| 817 | } |
| 818 | |
| 819 | if (!strcmp(args[arg], "accept")) { |
| 820 | arg++; |
| 821 | rule->action = TCP_ACT_ACCEPT; |
| 822 | } |
| 823 | else if (!strcmp(args[arg], "reject")) { |
| 824 | arg++; |
| 825 | rule->action = TCP_ACT_REJECT; |
| 826 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 827 | else if (strcmp(args[arg], "track-sc1") == 0) { |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 828 | int ret; |
| 829 | |
| 830 | arg++; |
| 831 | ret = parse_track_counters(args, &arg, section_type, curpx, |
| 832 | &rule->act_prm.trk_ctr, defpx, err, errlen); |
| 833 | |
| 834 | if (ret < 0) /* nb: warnings are not handled yet */ |
| 835 | return -1; |
| 836 | |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 837 | rule->action = TCP_ACT_TRK_SC1; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 838 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 839 | else if (strcmp(args[arg], "track-sc2") == 0) { |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 840 | int ret; |
| 841 | |
| 842 | arg++; |
| 843 | ret = parse_track_counters(args, &arg, section_type, curpx, |
| 844 | &rule->act_prm.trk_ctr, defpx, err, errlen); |
| 845 | |
| 846 | if (ret < 0) /* nb: warnings are not handled yet */ |
| 847 | return -1; |
| 848 | |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 849 | rule->action = TCP_ACT_TRK_SC2; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 850 | } |
| 851 | else { |
| 852 | snprintf(err, errlen, |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 853 | "'%s %s' expects 'accept', 'reject', 'track-sc1' " |
| 854 | "or 'track-sc2' in %s '%s' (was '%s')", |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 855 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]); |
| 856 | return -1; |
| 857 | } |
| 858 | |
| 859 | if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) { |
| 860 | if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) { |
| 861 | snprintf(err, errlen, |
| 862 | "error detected in %s '%s' while parsing '%s' condition", |
| 863 | proxy_type_str(curpx), curpx->id, args[arg]); |
| 864 | return -1; |
| 865 | } |
| 866 | } |
| 867 | else if (*args[arg]) { |
| 868 | snprintf(err, errlen, |
| 869 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')", |
| 870 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]); |
| 871 | return -1; |
| 872 | } |
| 873 | return 0; |
| 874 | } |
| 875 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 876 | /* This function should be called to parse a line starting with the "tcp-request" |
| 877 | * keyword. |
| 878 | */ |
| 879 | static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx, |
| 880 | struct proxy *defpx, char *err, int errlen) |
| 881 | { |
| 882 | const char *ptr = NULL; |
Willy Tarreau | c7e961e | 2008-08-17 17:13:47 +0200 | [diff] [blame] | 883 | unsigned int val; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 884 | int retlen; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 885 | int warn = 0; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 886 | int arg; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 887 | struct tcp_rule *rule; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 888 | |
| 889 | if (!*args[1]) { |
| 890 | snprintf(err, errlen, "missing argument for '%s' in %s '%s'", |
Willy Tarreau | f535683 | 2010-06-14 18:40:26 +0200 | [diff] [blame] | 891 | args[0], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 892 | return -1; |
| 893 | } |
| 894 | |
| 895 | if (!strcmp(args[1], "inspect-delay")) { |
| 896 | if (curpx == defpx) { |
| 897 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 898 | args[0], args[1]); |
| 899 | return -1; |
| 900 | } |
| 901 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 902 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
| 903 | retlen = snprintf(err, errlen, |
| 904 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
Willy Tarreau | f535683 | 2010-06-14 18:40:26 +0200 | [diff] [blame] | 905 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 906 | if (ptr && retlen < errlen) |
| 907 | retlen += snprintf(err+retlen, errlen - retlen, |
| 908 | " (unexpected character '%c')", *ptr); |
| 909 | return -1; |
| 910 | } |
| 911 | |
| 912 | if (curpx->tcp_req.inspect_delay) { |
| 913 | snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'", |
Willy Tarreau | f535683 | 2010-06-14 18:40:26 +0200 | [diff] [blame] | 914 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 915 | return 1; |
| 916 | } |
| 917 | curpx->tcp_req.inspect_delay = val; |
| 918 | return 0; |
| 919 | } |
| 920 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 921 | rule = (struct tcp_rule *)calloc(1, sizeof(*rule)); |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 922 | LIST_INIT(&rule->list); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 923 | arg = 1; |
| 924 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 925 | if (strcmp(args[1], "content") == 0) { |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 926 | arg++; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 927 | 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] | 928 | goto error; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 929 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 930 | if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) { |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 931 | struct acl *acl; |
| 932 | const char *name; |
| 933 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 934 | acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 935 | name = acl ? acl->name : "(unknown)"; |
| 936 | |
| 937 | retlen = snprintf(err, errlen, |
Willy Tarreau | 1a21194 | 2009-07-14 13:53:17 +0200 | [diff] [blame] | 938 | "acl '%s' involves some response-only criteria which will be ignored.", |
| 939 | name); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 940 | warn++; |
| 941 | } |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 942 | LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 943 | } |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 944 | else if (strcmp(args[1], "connection") == 0) { |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 945 | arg++; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 946 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 947 | if (!(curpx->cap & PR_CAP_FE)) { |
| 948 | snprintf(err, errlen, "%s %s is not allowed because %s %s is not a frontend", |
| 949 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 950 | return -1; |
| 951 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 952 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 953 | 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] | 954 | goto error; |
| 955 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 956 | if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) { |
| 957 | struct acl *acl; |
| 958 | const char *name; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 959 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 960 | acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY); |
| 961 | name = acl ? acl->name : "(unknown)"; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 962 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 963 | if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) { |
| 964 | retlen = snprintf(err, errlen, |
| 965 | "'%s %s' may not reference acl '%s' which makes use of " |
| 966 | "payload in %s '%s'. Please use '%s content' for this.", |
| 967 | args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]); |
| 968 | goto error; |
| 969 | } |
| 970 | if (acl->requires & ACL_USE_RTR_ANY) |
| 971 | retlen = snprintf(err, errlen, |
| 972 | "acl '%s' involves some response-only criteria which will be ignored.", |
| 973 | name); |
| 974 | warn++; |
| 975 | } |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 976 | LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 977 | } |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 978 | else { |
| 979 | retlen = snprintf(err, errlen, |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 980 | "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (was '%s')", |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 981 | args[0], proxy_type_str(curpx), curpx->id, args[1]); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 982 | goto error; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 983 | } |
| 984 | |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 985 | return warn; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 986 | error: |
| 987 | free(rule); |
| 988 | return -1; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 989 | } |
| 990 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 991 | |
| 992 | /************************************************************************/ |
| 993 | /* All supported ACL keywords must be declared here. */ |
| 994 | /************************************************************************/ |
| 995 | |
| 996 | /* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */ |
| 997 | static int |
| 998 | acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir, |
| 999 | struct acl_expr *expr, struct acl_test *test) |
| 1000 | { |
| 1001 | test->i = l4->cli_addr.ss_family; |
| 1002 | if (test->i == AF_INET) |
| 1003 | test->ptr = (void *)&((struct sockaddr_in *)&l4->cli_addr)->sin_addr; |
| 1004 | else |
| 1005 | test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_addr; |
| 1006 | test->flags = ACL_TEST_F_READ_ONLY; |
| 1007 | return 1; |
| 1008 | } |
| 1009 | |
| 1010 | /* extract the connection's source address */ |
| 1011 | static int |
| 1012 | pattern_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1013 | const char *arg, int arg_len, union pattern_data *data) |
| 1014 | { |
| 1015 | data->ip.s_addr = ((struct sockaddr_in *)&l4->cli_addr)->sin_addr.s_addr; |
| 1016 | return 1; |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | /* set test->i to the connection's source port */ |
| 1021 | static int |
| 1022 | acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1023 | struct acl_expr *expr, struct acl_test *test) |
| 1024 | { |
| 1025 | if (l4->cli_addr.ss_family == AF_INET) |
| 1026 | test->i = ntohs(((struct sockaddr_in *)&l4->cli_addr)->sin_port); |
| 1027 | else |
| 1028 | test->i = ntohs(((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_port); |
| 1029 | test->flags = 0; |
| 1030 | return 1; |
| 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | /* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */ |
| 1035 | static int |
| 1036 | acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1037 | struct acl_expr *expr, struct acl_test *test) |
| 1038 | { |
| 1039 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 1040 | get_frt_addr(l4); |
| 1041 | |
| 1042 | test->i = l4->frt_addr.ss_family; |
| 1043 | if (test->i == AF_INET) |
| 1044 | test->ptr = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr; |
| 1045 | else |
| 1046 | test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_addr; |
| 1047 | test->flags = ACL_TEST_F_READ_ONLY; |
| 1048 | return 1; |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | /* extract the connection's destination address */ |
| 1053 | static int |
| 1054 | pattern_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1055 | const char *arg, int arg_len, union pattern_data *data) |
| 1056 | { |
emeric | 8aa6b37 | 2010-10-22 17:06:26 +0200 | [diff] [blame] | 1057 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 1058 | get_frt_addr(l4); |
| 1059 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1060 | data->ip.s_addr = ((struct sockaddr_in *)&l4->frt_addr)->sin_addr.s_addr; |
| 1061 | return 1; |
| 1062 | } |
| 1063 | |
| 1064 | /* set test->i to the frontend connexion's destination port */ |
| 1065 | static int |
| 1066 | acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1067 | struct acl_expr *expr, struct acl_test *test) |
| 1068 | { |
| 1069 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 1070 | get_frt_addr(l4); |
| 1071 | |
| 1072 | if (l4->frt_addr.ss_family == AF_INET) |
| 1073 | test->i = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port); |
| 1074 | else |
| 1075 | test->i = ntohs(((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_port); |
| 1076 | test->flags = 0; |
| 1077 | return 1; |
| 1078 | } |
| 1079 | |
| 1080 | static int |
| 1081 | pattern_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1082 | const char *arg, int arg_len, union pattern_data *data) |
| 1083 | |
| 1084 | { |
emeric | 8aa6b37 | 2010-10-22 17:06:26 +0200 | [diff] [blame] | 1085 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 1086 | get_frt_addr(l4); |
| 1087 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1088 | data->integer = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port); |
| 1089 | return 1; |
| 1090 | } |
| 1091 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1092 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 1093 | { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req }, |
| 1094 | { 0, NULL, NULL }, |
| 1095 | }}; |
| 1096 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1097 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1098 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1099 | { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT }, |
| 1100 | { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP }, |
| 1101 | { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP }, |
| 1102 | { "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] | 1103 | { NULL, NULL, NULL, NULL }, |
| 1104 | }}; |
| 1105 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1106 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1107 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
| 1108 | { "src", pattern_fetch_src, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
| 1109 | { "dst", pattern_fetch_dst, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
| 1110 | { "dst_port", pattern_fetch_dport, PATTERN_TYPE_INTEGER, PATTERN_FETCH_REQ }, |
| 1111 | { NULL, NULL, 0, 0 }, |
| 1112 | }}; |
| 1113 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1114 | __attribute__((constructor)) |
| 1115 | static void __tcp_protocol_init(void) |
| 1116 | { |
| 1117 | protocol_register(&proto_tcpv4); |
| 1118 | protocol_register(&proto_tcpv6); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1119 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1120 | cfg_register_keywords(&cfg_kws); |
| 1121 | acl_register_keywords(&acl_kws); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | |
| 1125 | /* |
| 1126 | * Local variables: |
| 1127 | * c-indent-level: 8 |
| 1128 | * c-basic-offset: 8 |
| 1129 | * End: |
| 1130 | */ |