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