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 | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 41 | #include <proto/arg.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 42 | #include <proto/channel.h> |
Willy Tarreau | d2274c6 | 2012-07-06 14:29:45 +0200 | [diff] [blame] | 43 | #include <proto/connection.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 44 | #include <proto/fd.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 45 | #include <proto/listener.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 46 | #include <proto/log.h> |
| 47 | #include <proto/port_range.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 48 | #include <proto/protocol.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 49 | #include <proto/proto_tcp.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 50 | #include <proto/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 51 | #include <proto/sample.h> |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 52 | #include <proto/session.h> |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 53 | #include <proto/stick_table.h> |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 54 | #include <proto/task.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 55 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 56 | #ifdef CONFIG_HAP_CTTPROXY |
| 57 | #include <import/ip_tproxy.h> |
| 58 | #endif |
| 59 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 60 | static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen); |
| 61 | static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 62 | |
| 63 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 64 | static struct protocol proto_tcpv4 = { |
| 65 | .name = "tcpv4", |
| 66 | .sock_domain = AF_INET, |
| 67 | .sock_type = SOCK_STREAM, |
| 68 | .sock_prot = IPPROTO_TCP, |
| 69 | .sock_family = AF_INET, |
| 70 | .sock_addrlen = sizeof(struct sockaddr_in), |
| 71 | .l3_addrlen = 32/8, |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 72 | .accept = &listener_accept, |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 73 | .connect = tcp_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 74 | .bind = tcp_bind_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 75 | .bind_all = tcp_bind_listeners, |
| 76 | .unbind_all = unbind_all_listeners, |
| 77 | .enable_all = enable_all_listeners, |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 78 | .get_src = tcp_get_src, |
| 79 | .get_dst = tcp_get_dst, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 80 | .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners), |
| 81 | .nb_listeners = 0, |
| 82 | }; |
| 83 | |
| 84 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 85 | static struct protocol proto_tcpv6 = { |
| 86 | .name = "tcpv6", |
| 87 | .sock_domain = AF_INET6, |
| 88 | .sock_type = SOCK_STREAM, |
| 89 | .sock_prot = IPPROTO_TCP, |
| 90 | .sock_family = AF_INET6, |
| 91 | .sock_addrlen = sizeof(struct sockaddr_in6), |
| 92 | .l3_addrlen = 128/8, |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 93 | .accept = &listener_accept, |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 94 | .connect = tcp_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 95 | .bind = tcp_bind_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 96 | .bind_all = tcp_bind_listeners, |
| 97 | .unbind_all = unbind_all_listeners, |
| 98 | .enable_all = enable_all_listeners, |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 99 | .get_src = tcp_get_src, |
| 100 | .get_dst = tcp_get_dst, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 101 | .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners), |
| 102 | .nb_listeners = 0, |
| 103 | }; |
| 104 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 105 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 106 | /* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 107 | * case we try to bind <remote>. <flags> is a 2-bit field consisting of : |
| 108 | * - 0 : ignore remote address (may even be a NULL pointer) |
| 109 | * - 1 : use provided address |
| 110 | * - 2 : use provided port |
| 111 | * - 3 : use both |
| 112 | * |
| 113 | * The function supports multiple foreign binding methods : |
| 114 | * - linux_tproxy: we directly bind to the foreign address |
| 115 | * - cttproxy: we bind to a local address then nat. |
| 116 | * The second one can be used as a fallback for the first one. |
| 117 | * This function returns 0 when everything's OK, 1 if it could not bind, to the |
| 118 | * local address, 2 if it could not bind to the foreign address. |
| 119 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 120 | int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote) |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 121 | { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 122 | struct sockaddr_storage bind_addr; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 123 | int foreign_ok = 0; |
| 124 | int ret; |
| 125 | |
| 126 | #ifdef CONFIG_HAP_LINUX_TPROXY |
| 127 | static int ip_transp_working = 1; |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 128 | static int ip6_transp_working = 1; |
| 129 | switch (local->ss_family) { |
| 130 | case AF_INET: |
| 131 | if (flags && ip_transp_working) { |
| 132 | if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0 |
| 133 | || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0) |
| 134 | foreign_ok = 1; |
| 135 | else |
| 136 | ip_transp_working = 0; |
| 137 | } |
| 138 | break; |
| 139 | case AF_INET6: |
| 140 | if (flags && ip6_transp_working) { |
| 141 | if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0) |
| 142 | foreign_ok = 1; |
| 143 | else |
| 144 | ip6_transp_working = 0; |
| 145 | } |
| 146 | break; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 147 | } |
| 148 | #endif |
| 149 | if (flags) { |
| 150 | memset(&bind_addr, 0, sizeof(bind_addr)); |
Willy Tarreau | 96dd079 | 2011-04-19 07:20:57 +0200 | [diff] [blame] | 151 | bind_addr.ss_family = remote->ss_family; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 152 | switch (remote->ss_family) { |
| 153 | case AF_INET: |
| 154 | if (flags & 1) |
| 155 | ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr; |
| 156 | if (flags & 2) |
| 157 | ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port; |
| 158 | break; |
| 159 | case AF_INET6: |
| 160 | if (flags & 1) |
| 161 | ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr; |
| 162 | if (flags & 2) |
| 163 | ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port; |
| 164 | break; |
Willy Tarreau | 5dc1e98 | 2011-12-16 21:25:11 +0100 | [diff] [blame] | 165 | default: |
| 166 | /* we don't want to try to bind to an unknown address family */ |
| 167 | foreign_ok = 0; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 168 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 171 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 172 | if (foreign_ok) { |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 173 | if (is_addr(&bind_addr)) { |
| 174 | ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr)); |
| 175 | if (ret < 0) |
| 176 | return 2; |
| 177 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 178 | } |
| 179 | else { |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 180 | if (is_addr(local)) { |
| 181 | ret = bind(fd, (struct sockaddr *)local, get_addr_len(local)); |
| 182 | if (ret < 0) |
| 183 | return 1; |
| 184 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if (!flags) |
| 188 | return 0; |
| 189 | |
| 190 | #ifdef CONFIG_HAP_CTTPROXY |
Willy Tarreau | 6f831b4 | 2011-03-20 14:03:54 +0100 | [diff] [blame] | 191 | if (!foreign_ok && remote->ss_family == AF_INET) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 192 | struct in_tproxy itp1, itp2; |
| 193 | memset(&itp1, 0, sizeof(itp1)); |
| 194 | |
| 195 | itp1.op = TPROXY_ASSIGN; |
Willy Tarreau | 6f831b4 | 2011-03-20 14:03:54 +0100 | [diff] [blame] | 196 | itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr; |
| 197 | itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 198 | |
| 199 | /* set connect flag on socket */ |
| 200 | itp2.op = TPROXY_FLAGS; |
| 201 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 202 | |
| 203 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 && |
| 204 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) { |
| 205 | foreign_ok = 1; |
| 206 | } |
| 207 | } |
| 208 | #endif |
| 209 | if (!foreign_ok) |
| 210 | /* we could not bind to a foreign address */ |
| 211 | return 2; |
| 212 | |
| 213 | return 0; |
| 214 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 215 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 216 | |
| 217 | /* |
Willy Tarreau | 14f8e86 | 2012-08-30 22:23:13 +0200 | [diff] [blame] | 218 | * This function initiates a TCP connection establishment to the target assigned |
| 219 | * to connection <conn> using (si->{target,addr.to}). A source address may be |
| 220 | * pointed to by conn->addr.from in case of transparent proxying. Normal source |
| 221 | * bind addresses are still determined locally (due to the possible need of a |
| 222 | * source port). conn->target may point either to a valid server or to a backend, |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 223 | * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 224 | * supported. The <data> parameter is a boolean indicating whether there are data |
| 225 | * waiting for being sent or not, in order to adjust data write polling and on |
| 226 | * some platforms, the ability to avoid an empty initial ACK. The <delack> argument |
| 227 | * allows the caller to force using a delayed ACK when establishing the connection : |
| 228 | * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect |
| 229 | * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data |
| 230 | * - 2 = delayed ACK regardless of backend options |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 231 | * |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 232 | * It can return one of : |
| 233 | * - SN_ERR_NONE if everything's OK |
| 234 | * - SN_ERR_SRVTO if there are no more servers |
| 235 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 236 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 237 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 238 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 239 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 240 | * |
| 241 | * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise |
| 242 | * it's invalid and the caller has nothing to do. |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 243 | */ |
Willy Tarreau | f153686 | 2011-03-03 18:27:32 +0100 | [diff] [blame] | 244 | |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 245 | int tcp_connect_server(struct connection *conn, int data, int delack) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 246 | { |
| 247 | int fd; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 248 | struct server *srv; |
| 249 | struct proxy *be; |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 250 | struct conn_src *src; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 251 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 252 | switch (obj_type(conn->target)) { |
| 253 | case OBJ_TYPE_PROXY: |
| 254 | be = objt_proxy(conn->target); |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 255 | srv = NULL; |
| 256 | break; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 257 | case OBJ_TYPE_SERVER: |
| 258 | srv = objt_server(conn->target); |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 259 | be = srv->proxy; |
| 260 | break; |
| 261 | default: |
| 262 | return SN_ERR_INTERNAL; |
| 263 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 264 | |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 265 | if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 266 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 267 | |
| 268 | if (errno == ENFILE) |
| 269 | send_log(be, LOG_EMERG, |
| 270 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 271 | be->id, maxfd); |
| 272 | else if (errno == EMFILE) |
| 273 | send_log(be, LOG_EMERG, |
| 274 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 275 | be->id, maxfd); |
| 276 | else if (errno == ENOBUFS || errno == ENOMEM) |
| 277 | send_log(be, LOG_EMERG, |
| 278 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 279 | be->id, maxfd); |
| 280 | /* this is a resource error */ |
| 281 | return SN_ERR_RESOURCE; |
| 282 | } |
| 283 | |
| 284 | if (fd >= global.maxsock) { |
| 285 | /* do not log anything there, it's a normal condition when this option |
| 286 | * is used to serialize connections to a server ! |
| 287 | */ |
| 288 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 289 | close(fd); |
| 290 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
| 291 | } |
| 292 | |
| 293 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 294 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 295 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 296 | close(fd); |
| 297 | return SN_ERR_INTERNAL; |
| 298 | } |
| 299 | |
| 300 | if (be->options & PR_O_TCP_SRV_KA) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 301 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 302 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 303 | /* allow specific binding : |
| 304 | * - server-specific at first |
| 305 | * - proxy-specific next |
| 306 | */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 307 | if (srv && srv->conn_src.opts & CO_SRC_BIND) |
| 308 | src = &srv->conn_src; |
| 309 | else if (be->conn_src.opts & CO_SRC_BIND) |
| 310 | src = &be->conn_src; |
| 311 | else |
| 312 | src = NULL; |
| 313 | |
| 314 | if (src) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 315 | int ret, flags = 0; |
| 316 | |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 317 | if (is_addr(&conn->addr.from)) { |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 318 | switch (src->opts & CO_SRC_TPROXY_MASK) { |
Willy Tarreau | ef9a360 | 2012-12-08 22:29:20 +0100 | [diff] [blame] | 319 | case CO_SRC_TPROXY_ADDR: |
| 320 | case CO_SRC_TPROXY_CLI: |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 321 | flags = 3; |
| 322 | break; |
Willy Tarreau | ef9a360 | 2012-12-08 22:29:20 +0100 | [diff] [blame] | 323 | case CO_SRC_TPROXY_CIP: |
| 324 | case CO_SRC_TPROXY_DYN: |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 325 | flags = 1; |
| 326 | break; |
| 327 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 328 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 329 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 330 | #ifdef SO_BINDTODEVICE |
| 331 | /* Note: this might fail if not CAP_NET_RAW */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 332 | if (src->iface_name) |
| 333 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 334 | #endif |
| 335 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 336 | if (src->sport_range) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 337 | int attempts = 10; /* should be more than enough to find a spare port */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 338 | struct sockaddr_storage sa; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 339 | |
| 340 | ret = 1; |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 341 | sa = src->source_addr; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 342 | |
| 343 | do { |
| 344 | /* note: in case of retry, we may have to release a previously |
| 345 | * allocated port, hence this loop's construct. |
| 346 | */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 347 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 348 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 349 | |
| 350 | if (!attempts) |
| 351 | break; |
| 352 | attempts--; |
| 353 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 354 | fdinfo[fd].local_port = port_range_alloc_port(src->sport_range); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 355 | if (!fdinfo[fd].local_port) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 356 | break; |
| 357 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 358 | fdinfo[fd].port_range = src->sport_range; |
| 359 | set_host_port(&sa, fdinfo[fd].local_port); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 360 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 361 | ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 362 | } while (ret != 0); /* binding NOK */ |
| 363 | } |
| 364 | else { |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 365 | ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 368 | if (unlikely(ret != 0)) { |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 369 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 370 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 371 | close(fd); |
| 372 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 373 | if (ret == 1) { |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 374 | Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 375 | be->id); |
| 376 | send_log(be, LOG_EMERG, |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 377 | "Cannot bind to source address before connect() for backend %s.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 378 | be->id); |
| 379 | } else { |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 380 | Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 381 | be->id); |
| 382 | send_log(be, LOG_EMERG, |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 383 | "Cannot bind to tproxy source address before connect() for backend %s.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 384 | be->id); |
| 385 | } |
| 386 | return SN_ERR_RESOURCE; |
| 387 | } |
| 388 | } |
| 389 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 390 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 391 | /* disabling tcp quick ack now allows the first request to leave the |
| 392 | * machine with the first ACK. We only do this if there are pending |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 393 | * data in the buffer. |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 394 | */ |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 395 | if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON))) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 396 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 397 | #endif |
| 398 | |
Willy Tarreau | e803de2 | 2010-01-21 17:43:04 +0100 | [diff] [blame] | 399 | if (global.tune.server_sndbuf) |
| 400 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 401 | |
| 402 | if (global.tune.server_rcvbuf) |
| 403 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 404 | |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 405 | if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) && |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 406 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 407 | |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 408 | if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 409 | char *msg; |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 410 | if (errno == EAGAIN || errno == EADDRNOTAVAIL) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 411 | msg = "no free ports"; |
| 412 | else |
| 413 | msg = "local address already in use"; |
| 414 | |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 415 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 416 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 417 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 418 | close(fd); |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 419 | send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 420 | return SN_ERR_RESOURCE; |
| 421 | } else if (errno == ETIMEDOUT) { |
| 422 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 423 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 424 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 425 | close(fd); |
| 426 | return SN_ERR_SRVTO; |
| 427 | } else { |
| 428 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 429 | //qfprintf(stderr,"Connect(): %d", errno); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 430 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 431 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 432 | close(fd); |
| 433 | return SN_ERR_SRVCL; |
| 434 | } |
| 435 | } |
| 436 | |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 437 | fdtab[fd].owner = conn; |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 438 | conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */ |
Willy Tarreau | fc8f1f0 | 2012-12-08 18:53:44 +0100 | [diff] [blame] | 439 | conn->flags |= CO_FL_ADDR_TO_SET; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 440 | |
Willy Tarreau | d2274c6 | 2012-07-06 14:29:45 +0200 | [diff] [blame] | 441 | fdtab[fd].iocb = conn_fd_handler; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 442 | fd_insert(fd); |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 443 | conn_sock_want_send(conn); /* for connect status */ |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 444 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 445 | if (conn_xprt_init(conn) < 0) { |
Willy Tarreau | 184636e | 2012-09-06 14:04:41 +0200 | [diff] [blame] | 446 | fd_delete(fd); |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 447 | return SN_ERR_RESOURCE; |
Willy Tarreau | 184636e | 2012-09-06 14:04:41 +0200 | [diff] [blame] | 448 | } |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 449 | |
Willy Tarreau | 14f8e86 | 2012-08-30 22:23:13 +0200 | [diff] [blame] | 450 | if (data) |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 451 | conn_data_want_send(conn); /* prepare to send data if any */ |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 452 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 453 | return SN_ERR_NONE; /* connection is OK */ |
| 454 | } |
| 455 | |
| 456 | |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 457 | /* |
| 458 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 459 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 460 | * success, -1 in case of error. The socket's source address is stored in |
| 461 | * <sa> for <salen> bytes. |
| 462 | */ |
| 463 | int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 464 | { |
| 465 | if (dir) |
| 466 | return getsockname(fd, sa, &salen); |
| 467 | else |
| 468 | return getpeername(fd, sa, &salen); |
| 469 | } |
| 470 | |
| 471 | |
| 472 | /* |
| 473 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 474 | * indicating if we're a listener (=0) or an initiator (!=0). In the case of a |
| 475 | * listener, if the original destination address was translated, the original |
| 476 | * address is retrieved. It returns 0 in case of success, -1 in case of error. |
| 477 | * The socket's source address is stored in <sa> for <salen> bytes. |
| 478 | */ |
| 479 | int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 480 | { |
| 481 | if (dir) |
| 482 | return getpeername(fd, sa, &salen); |
| 483 | #if defined(TPROXY) && defined(SO_ORIGINAL_DST) |
| 484 | else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0) |
| 485 | return 0; |
| 486 | #endif |
| 487 | else |
| 488 | return getsockname(fd, sa, &salen); |
| 489 | } |
| 490 | |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 491 | /* This is the callback which is set when a connection establishment is pending |
| 492 | * and we have nothing to send, or if we have an init function we want to call |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 493 | * once the connection is established. It updates the FD polling status. It |
| 494 | * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise |
| 495 | * it returns non-zero and removes itself from the connection's flags (the bit is |
| 496 | * provided in <flag> by the caller). |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 497 | */ |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 498 | int tcp_connect_probe(struct connection *conn) |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 499 | { |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 500 | int fd = conn->t.sock.fd; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 501 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 502 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 503 | return 0; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 504 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 505 | if (!(conn->flags & CO_FL_WAIT_L4_CONN)) |
Willy Tarreau | 0b0c097 | 2012-07-23 20:05:00 +0200 | [diff] [blame] | 506 | return 1; /* strange we were called while ready */ |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 507 | |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 508 | /* stop here if we reached the end of data */ |
| 509 | if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) |
| 510 | goto out_error; |
| 511 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 512 | /* We have no data to send to check the connection, and |
| 513 | * getsockopt() will not inform us whether the connection |
| 514 | * is still pending. So we'll reuse connect() to check the |
| 515 | * state of the socket. This has the advantage of giving us |
| 516 | * the following info : |
| 517 | * - error |
| 518 | * - connecting (EALREADY, EINPROGRESS) |
| 519 | * - connected (EISCONN, 0) |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 520 | */ |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 521 | if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) { |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 522 | if (errno == EALREADY || errno == EINPROGRESS) { |
Willy Tarreau | d486ef5 | 2012-12-10 17:03:52 +0100 | [diff] [blame] | 523 | __conn_sock_stop_recv(conn); |
| 524 | __conn_sock_poll_send(conn); |
Willy Tarreau | 0b0c097 | 2012-07-23 20:05:00 +0200 | [diff] [blame] | 525 | return 0; |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 526 | } |
Willy Tarreau | a190d59 | 2012-05-20 18:35:19 +0200 | [diff] [blame] | 527 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 528 | if (errno && errno != EISCONN) |
Willy Tarreau | a190d59 | 2012-05-20 18:35:19 +0200 | [diff] [blame] | 529 | goto out_error; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 530 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 531 | /* otherwise we're connected */ |
Willy Tarreau | a190d59 | 2012-05-20 18:35:19 +0200 | [diff] [blame] | 532 | } |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 533 | |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 534 | /* The FD is ready now, we'll mark the connection as complete and |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 535 | * forward the event to the transport layer which will notify the |
| 536 | * data layer. |
Willy Tarreau | a190d59 | 2012-05-20 18:35:19 +0200 | [diff] [blame] | 537 | */ |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 538 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 0b0c097 | 2012-07-23 20:05:00 +0200 | [diff] [blame] | 539 | return 1; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 540 | |
| 541 | out_error: |
Willy Tarreau | 0b0c097 | 2012-07-23 20:05:00 +0200 | [diff] [blame] | 542 | /* Write error on the file descriptor. Report it to the connection |
| 543 | * and disable polling on this FD. |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 544 | */ |
| 545 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 546 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | d486ef5 | 2012-12-10 17:03:52 +0100 | [diff] [blame] | 547 | __conn_sock_stop_both(conn); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 548 | return 0; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 549 | } |
| 550 | |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 551 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 552 | /* This function tries to bind a TCPv4/v6 listener. It may return a warning or |
| 553 | * an error message in <err> if the message is at most <errlen> bytes long |
| 554 | * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN, |
| 555 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 556 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 557 | * error occurred but that it may vanish after a retry (eg: port in use), and |
Aman Gupta | d94991d | 2012-04-06 17:39:26 -0700 | [diff] [blame] | 558 | * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 559 | * the meaning of the error, but just indicate that a message is present which |
| 560 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 561 | * that it's pointless to try to start other listeners. No error message is |
| 562 | * returned if errlen is NULL. |
| 563 | */ |
| 564 | int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 565 | { |
| 566 | __label__ tcp_return, tcp_close_return; |
| 567 | int fd, err; |
| 568 | const char *msg = NULL; |
| 569 | |
| 570 | /* ensure we never return garbage */ |
| 571 | if (errmsg && errlen) |
| 572 | *errmsg = 0; |
| 573 | |
| 574 | if (listener->state != LI_ASSIGNED) |
| 575 | return ERR_NONE; /* already bound */ |
| 576 | |
| 577 | err = ERR_NONE; |
| 578 | |
| 579 | if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 580 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 581 | msg = "cannot create listening socket"; |
| 582 | goto tcp_return; |
| 583 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 584 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 585 | if (fd >= global.maxsock) { |
| 586 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 587 | msg = "not enough free sockets (raise '-n' parameter)"; |
| 588 | goto tcp_close_return; |
| 589 | } |
| 590 | |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 591 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 592 | err |= ERR_FATAL | ERR_ALERT; |
| 593 | msg = "cannot make socket non-blocking"; |
| 594 | goto tcp_close_return; |
| 595 | } |
| 596 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 597 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 598 | /* not fatal but should be reported */ |
| 599 | msg = "cannot do so_reuseaddr"; |
| 600 | err |= ERR_ALERT; |
| 601 | } |
| 602 | |
| 603 | if (listener->options & LI_O_NOLINGER) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 604 | setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger)); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 605 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 606 | #ifdef SO_REUSEPORT |
| 607 | /* OpenBSD supports this. As it's present in old libc versions of Linux, |
| 608 | * it might return an error that we will silently ignore. |
| 609 | */ |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 610 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 611 | #endif |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 612 | #ifdef CONFIG_HAP_LINUX_TPROXY |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 613 | if (listener->options & LI_O_FOREIGN) { |
| 614 | switch (listener->addr.ss_family) { |
| 615 | case AF_INET: |
| 616 | if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1) |
| 617 | && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) { |
| 618 | msg = "cannot make listening socket transparent"; |
| 619 | err |= ERR_ALERT; |
| 620 | } |
| 621 | break; |
| 622 | case AF_INET6: |
| 623 | if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) { |
| 624 | msg = "cannot make listening socket transparent"; |
| 625 | err |= ERR_ALERT; |
| 626 | } |
| 627 | break; |
| 628 | } |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 629 | } |
| 630 | #endif |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 631 | #ifdef SO_BINDTODEVICE |
| 632 | /* Note: this might fail if not CAP_NET_RAW */ |
| 633 | if (listener->interface) { |
| 634 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 635 | listener->interface, strlen(listener->interface) + 1) == -1) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 636 | msg = "cannot bind listener to device"; |
| 637 | err |= ERR_WARN; |
| 638 | } |
| 639 | } |
| 640 | #endif |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 641 | #if defined(TCP_MAXSEG) |
Willy Tarreau | 48a7e72 | 2010-12-24 15:26:39 +0100 | [diff] [blame] | 642 | if (listener->maxseg > 0) { |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 643 | if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 644 | &listener->maxseg, sizeof(listener->maxseg)) == -1) { |
| 645 | msg = "cannot set MSS"; |
| 646 | err |= ERR_WARN; |
| 647 | } |
| 648 | } |
| 649 | #endif |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 650 | #if defined(TCP_DEFER_ACCEPT) |
| 651 | if (listener->options & LI_O_DEF_ACCEPT) { |
| 652 | /* defer accept by up to one second */ |
| 653 | int accept_delay = 1; |
| 654 | if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) { |
| 655 | msg = "cannot enable DEFER_ACCEPT"; |
| 656 | err |= ERR_WARN; |
| 657 | } |
| 658 | } |
| 659 | #endif |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 660 | #if defined(TCP_FASTOPEN) |
| 661 | if (listener->options & LI_O_TCP_FO) { |
| 662 | /* TFO needs a queue length, let's use the configured backlog */ |
| 663 | int qlen = listener->backlog ? listener->backlog : listener->maxconn; |
| 664 | if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) { |
| 665 | msg = "cannot enable TCP_FASTOPEN"; |
| 666 | err |= ERR_WARN; |
| 667 | } |
| 668 | } |
| 669 | #endif |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 670 | #if defined(IPV6_V6ONLY) |
| 671 | if (listener->options & LI_O_V6ONLY) |
| 672 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 673 | else if (listener->options & LI_O_V4V6) |
| 674 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)); |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 675 | #endif |
| 676 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 677 | if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) { |
| 678 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 679 | msg = "cannot bind socket"; |
| 680 | goto tcp_close_return; |
| 681 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 682 | |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 683 | if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 684 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 685 | msg = "cannot listen to socket"; |
| 686 | goto tcp_close_return; |
| 687 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 688 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 689 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 690 | if (listener->options & LI_O_NOQUICKACK) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 691 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero)); |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 692 | #endif |
| 693 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 694 | /* the socket is ready */ |
| 695 | listener->fd = fd; |
| 696 | listener->state = LI_LISTEN; |
| 697 | |
Willy Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 698 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 699 | fdtab[fd].iocb = listener->proto->accept; |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 700 | fd_insert(fd); |
| 701 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 702 | tcp_return: |
Cyril Bonté | 43ba1b3 | 2010-11-01 19:26:01 +0100 | [diff] [blame] | 703 | if (msg && errlen) { |
| 704 | char pn[INET6_ADDRSTRLEN]; |
| 705 | |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 706 | addr_to_str(&listener->addr, pn, sizeof(pn)); |
| 707 | snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr)); |
Cyril Bonté | 43ba1b3 | 2010-11-01 19:26:01 +0100 | [diff] [blame] | 708 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 709 | return err; |
| 710 | |
| 711 | tcp_close_return: |
| 712 | close(fd); |
| 713 | goto tcp_return; |
| 714 | } |
| 715 | |
| 716 | /* This function creates all TCP sockets bound to the protocol entry <proto>. |
| 717 | * It is intended to be used as the protocol's bind_all() function. |
| 718 | * The sockets will be registered but not added to any fd_set, in order not to |
| 719 | * loose them across the fork(). A call to enable_all_listeners() is needed |
| 720 | * to complete initialization. The return value is composed from ERR_*. |
| 721 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 722 | static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 723 | { |
| 724 | struct listener *listener; |
| 725 | int err = ERR_NONE; |
| 726 | |
| 727 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 728 | err |= tcp_bind_listener(listener, errmsg, errlen); |
| 729 | if (err & ERR_ABORT) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 730 | break; |
| 731 | } |
| 732 | |
| 733 | return err; |
| 734 | } |
| 735 | |
| 736 | /* Add listener to the list of tcpv4 listeners. The listener's state |
| 737 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 738 | * listeners is updated. This is the function to use to add a new listener. |
| 739 | */ |
| 740 | void tcpv4_add_listener(struct listener *listener) |
| 741 | { |
| 742 | if (listener->state != LI_INIT) |
| 743 | return; |
| 744 | listener->state = LI_ASSIGNED; |
| 745 | listener->proto = &proto_tcpv4; |
| 746 | LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list); |
| 747 | proto_tcpv4.nb_listeners++; |
| 748 | } |
| 749 | |
| 750 | /* Add listener to the list of tcpv4 listeners. The listener's state |
| 751 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 752 | * listeners is updated. This is the function to use to add a new listener. |
| 753 | */ |
| 754 | void tcpv6_add_listener(struct listener *listener) |
| 755 | { |
| 756 | if (listener->state != LI_INIT) |
| 757 | return; |
| 758 | listener->state = LI_ASSIGNED; |
| 759 | listener->proto = &proto_tcpv6; |
| 760 | LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list); |
| 761 | proto_tcpv6.nb_listeners++; |
| 762 | } |
| 763 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 764 | /* This function performs the TCP request analysis on the current request. It |
| 765 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 766 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 767 | * request. It relies on buffers flags, and updates s->req->analysers. The |
| 768 | * function may be called for frontend rules and backend rules. It only relies |
| 769 | * on the backend pointer so this works for both cases. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 770 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 771 | int tcp_inspect_request(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 772 | { |
| 773 | struct tcp_rule *rule; |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 774 | struct stksess *ts; |
| 775 | struct stktable *t; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 776 | int partial; |
| 777 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 778 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 779 | now_ms, __FUNCTION__, |
| 780 | s, |
| 781 | req, |
| 782 | req->rex, req->wex, |
| 783 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 784 | req->buf->i, |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 785 | req->analysers); |
| 786 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 787 | /* We don't know whether we have enough data, so must proceed |
| 788 | * this way : |
| 789 | * - iterate through all rules in their declaration order |
| 790 | * - if one rule returns MISS, it means the inspect delay is |
| 791 | * not over yet, then return immediately, otherwise consider |
| 792 | * it as a non-match. |
| 793 | * - if one rule returns OK, then return OK |
| 794 | * - if one rule returns KO, then return KO |
| 795 | */ |
| 796 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 797 | if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) || |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 798 | !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms)) |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 799 | partial = SMP_OPT_FINAL; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 800 | else |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 801 | partial = 0; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 802 | |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 803 | list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) { |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 804 | int ret = ACL_PAT_PASS; |
| 805 | |
| 806 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 807 | ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 808 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 809 | channel_dont_connect(req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 810 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 811 | if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay) |
| 812 | 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] | 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | ret = acl_pass(ret); |
| 817 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 818 | ret = !ret; |
| 819 | } |
| 820 | |
| 821 | if (ret) { |
| 822 | /* we have a matching rule. */ |
| 823 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 824 | channel_abort(req); |
| 825 | channel_abort(s->rep); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 826 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 827 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 828 | s->be->be_counters.denied_req++; |
| 829 | s->fe->fe_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 830 | if (s->listener->counters) |
Willy Tarreau | 23968d8 | 2010-05-23 23:50:44 +0200 | [diff] [blame] | 831 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 832 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 833 | if (!(s->flags & SN_ERR_MASK)) |
| 834 | s->flags |= SN_ERR_PRXCOND; |
| 835 | if (!(s->flags & SN_FINST_MASK)) |
| 836 | s->flags |= SN_FINST_R; |
| 837 | return 0; |
| 838 | } |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 839 | else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) || |
| 840 | (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) { |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 841 | /* Note: only the first valid tracking parameter of each |
| 842 | * applies. |
| 843 | */ |
| 844 | struct stktable_key *key; |
| 845 | |
| 846 | t = rule->act_prm.trk_ctr.table.t; |
| 847 | key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr); |
| 848 | |
| 849 | if (key && (ts = stktable_get_entry(t, key))) { |
| 850 | if (rule->action == TCP_ACT_TRK_SC1) { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 851 | session_track_stkctr(&s->stkctr[0], t, ts); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 852 | if (s->fe != s->be) |
| 853 | s->flags |= SN_BE_TRACK_SC1; |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 854 | } else { |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 855 | session_track_stkctr(&s->stkctr[1], t, ts); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 856 | if (s->fe != s->be) |
| 857 | s->flags |= SN_BE_TRACK_SC2; |
| 858 | } |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | else { |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 862 | /* otherwise accept */ |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 863 | break; |
| 864 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
| 868 | /* if we get there, it means we have no rule which matches, or |
| 869 | * we have an explicit accept, so we apply the default accept. |
| 870 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 871 | req->analysers &= ~an_bit; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 872 | req->analyse_exp = TICK_ETERNITY; |
| 873 | return 1; |
| 874 | } |
| 875 | |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 876 | /* This function performs the TCP response analysis on the current response. It |
| 877 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 878 | * needs more data, encounters an error, or wants to immediately abort the |
| 879 | * response. It relies on buffers flags, and updates s->rep->analysers. The |
| 880 | * function may be called for backend rules. |
| 881 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 882 | int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit) |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 883 | { |
| 884 | struct tcp_rule *rule; |
| 885 | int partial; |
| 886 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 887 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 888 | now_ms, __FUNCTION__, |
| 889 | s, |
| 890 | rep, |
| 891 | rep->rex, rep->wex, |
| 892 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 893 | rep->buf->i, |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 894 | rep->analysers); |
| 895 | |
| 896 | /* We don't know whether we have enough data, so must proceed |
| 897 | * this way : |
| 898 | * - iterate through all rules in their declaration order |
| 899 | * - if one rule returns MISS, it means the inspect delay is |
| 900 | * not over yet, then return immediately, otherwise consider |
| 901 | * it as a non-match. |
| 902 | * - if one rule returns OK, then return OK |
| 903 | * - if one rule returns KO, then return KO |
| 904 | */ |
| 905 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 906 | if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms)) |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 907 | partial = SMP_OPT_FINAL; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 908 | else |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 909 | partial = 0; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 910 | |
| 911 | list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) { |
| 912 | int ret = ACL_PAT_PASS; |
| 913 | |
| 914 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 915 | ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 916 | if (ret == ACL_PAT_MISS) { |
| 917 | /* just set the analyser timeout once at the beginning of the response */ |
| 918 | if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay) |
| 919 | rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay); |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | ret = acl_pass(ret); |
| 924 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 925 | ret = !ret; |
| 926 | } |
| 927 | |
| 928 | if (ret) { |
| 929 | /* we have a matching rule. */ |
| 930 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 931 | channel_abort(rep); |
| 932 | channel_abort(s->req); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 933 | rep->analysers = 0; |
| 934 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 935 | s->be->be_counters.denied_resp++; |
| 936 | s->fe->fe_counters.denied_resp++; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 937 | if (s->listener->counters) |
| 938 | s->listener->counters->denied_resp++; |
| 939 | |
| 940 | if (!(s->flags & SN_ERR_MASK)) |
| 941 | s->flags |= SN_ERR_PRXCOND; |
| 942 | if (!(s->flags & SN_FINST_MASK)) |
| 943 | s->flags |= SN_FINST_D; |
| 944 | return 0; |
| 945 | } |
| 946 | else { |
| 947 | /* otherwise accept */ |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /* if we get there, it means we have no rule which matches, or |
| 954 | * we have an explicit accept, so we apply the default accept. |
| 955 | */ |
| 956 | rep->analysers &= ~an_bit; |
| 957 | rep->analyse_exp = TICK_ETERNITY; |
| 958 | return 1; |
| 959 | } |
| 960 | |
| 961 | |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 962 | /* This function performs the TCP layer4 analysis on the current request. It |
| 963 | * returns 0 if a reject rule matches, otherwise 1 if either an accept rule |
| 964 | * matches or if no more rule matches. It can only use rules which don't need |
| 965 | * any data. |
| 966 | */ |
| 967 | int tcp_exec_req_rules(struct session *s) |
| 968 | { |
| 969 | struct tcp_rule *rule; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 970 | struct stksess *ts; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 971 | struct stktable *t = NULL; |
| 972 | int result = 1; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 973 | int ret; |
| 974 | |
| 975 | list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) { |
| 976 | ret = ACL_PAT_PASS; |
| 977 | |
| 978 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 979 | ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 980 | ret = acl_pass(ret); |
| 981 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 982 | ret = !ret; |
| 983 | } |
| 984 | |
| 985 | if (ret) { |
| 986 | /* we have a matching rule. */ |
| 987 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 988 | s->fe->fe_counters.denied_conn++; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 989 | if (s->listener->counters) |
Willy Tarreau | 2799e98 | 2010-06-05 15:43:21 +0200 | [diff] [blame] | 990 | s->listener->counters->denied_conn++; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 991 | |
| 992 | if (!(s->flags & SN_ERR_MASK)) |
| 993 | s->flags |= SN_ERR_PRXCOND; |
| 994 | if (!(s->flags & SN_FINST_MASK)) |
| 995 | s->flags |= SN_FINST_R; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 996 | result = 0; |
| 997 | break; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 998 | } |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 999 | else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) || |
| 1000 | (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) { |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1001 | /* Note: only the first valid tracking parameter of each |
| 1002 | * applies. |
| 1003 | */ |
| 1004 | struct stktable_key *key; |
| 1005 | |
| 1006 | t = rule->act_prm.trk_ctr.table.t; |
| 1007 | key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr); |
| 1008 | |
| 1009 | if (key && (ts = stktable_get_entry(t, key))) { |
| 1010 | if (rule->action == TCP_ACT_TRK_SC1) |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 1011 | session_track_stkctr(&s->stkctr[0], t, ts); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1012 | else |
Willy Tarreau | 20d46a5 | 2012-12-09 15:55:40 +0100 | [diff] [blame] | 1013 | session_track_stkctr(&s->stkctr[1], t, ts); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 1014 | } |
| 1015 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1016 | else { |
| 1017 | /* otherwise it's an accept */ |
| 1018 | break; |
| 1019 | } |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1020 | } |
| 1021 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1022 | return result; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1025 | /* Parse a tcp-response rule. Return a negative value in case of failure */ |
| 1026 | static int tcp_parse_response_rule(char **args, int arg, int section_type, |
| 1027 | struct proxy *curpx, struct proxy *defpx, |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1028 | struct tcp_rule *rule, char **err) |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1029 | { |
| 1030 | if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1031 | memprintf(err, "%s %s is only allowed in 'backend' sections", |
| 1032 | args[0], args[1]); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1033 | return -1; |
| 1034 | } |
| 1035 | |
| 1036 | if (strcmp(args[arg], "accept") == 0) { |
| 1037 | arg++; |
| 1038 | rule->action = TCP_ACT_ACCEPT; |
| 1039 | } |
| 1040 | else if (strcmp(args[arg], "reject") == 0) { |
| 1041 | arg++; |
| 1042 | rule->action = TCP_ACT_REJECT; |
| 1043 | } |
| 1044 | else { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1045 | memprintf(err, |
| 1046 | "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')", |
| 1047 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1048 | return -1; |
| 1049 | } |
| 1050 | |
| 1051 | if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1052 | if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) { |
| 1053 | memprintf(err, |
| 1054 | "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s", |
| 1055 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1056 | return -1; |
| 1057 | } |
| 1058 | } |
| 1059 | else if (*args[arg]) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1060 | memprintf(err, |
| 1061 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')", |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1062 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]); |
| 1063 | return -1; |
| 1064 | } |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1070 | /* Parse a tcp-request rule. Return a negative value in case of failure */ |
| 1071 | static int tcp_parse_request_rule(char **args, int arg, int section_type, |
| 1072 | struct proxy *curpx, struct proxy *defpx, |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1073 | struct tcp_rule *rule, char **err) |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1074 | { |
| 1075 | if (curpx == defpx) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1076 | memprintf(err, "%s %s is not allowed in 'defaults' sections", |
| 1077 | args[0], args[1]); |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1078 | return -1; |
| 1079 | } |
| 1080 | |
| 1081 | if (!strcmp(args[arg], "accept")) { |
| 1082 | arg++; |
| 1083 | rule->action = TCP_ACT_ACCEPT; |
| 1084 | } |
| 1085 | else if (!strcmp(args[arg], "reject")) { |
| 1086 | arg++; |
| 1087 | rule->action = TCP_ACT_REJECT; |
| 1088 | } |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1089 | else if (strcmp(args[arg], "track-sc1") == 0 || strcmp(args[arg], "track-sc2") == 0) { |
| 1090 | struct sample_expr *expr; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1091 | int kw = arg; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1092 | |
| 1093 | arg++; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1094 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1095 | expr = sample_parse_expr(args, &arg, trash.str, trash.size); |
| 1096 | if (!expr) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1097 | memprintf(err, |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1098 | "'%s %s %s' : %s", |
| 1099 | args[0], args[1], args[kw], trash.str); |
| 1100 | return -1; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1101 | } |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1102 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1103 | if (!(expr->fetch->cap & SMP_CAP_REQ)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1104 | memprintf(err, |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1105 | "'%s %s %s' : fetch method '%s' cannot be used on request", |
| 1106 | args[0], args[1], args[kw], trash.str); |
| 1107 | free(expr); |
| 1108 | return -1; |
| 1109 | } |
| 1110 | |
| 1111 | /* check if we need to allocate an hdr_idx struct for HTTP parsing */ |
| 1112 | if (expr->fetch->cap & SMP_CAP_L7) |
| 1113 | curpx->acl_requires |= ACL_USE_L7_ANY; |
| 1114 | |
| 1115 | if (strcmp(args[arg], "table") == 0) { |
Willy Tarreau | 598718a | 2012-12-09 16:57:27 +0100 | [diff] [blame] | 1116 | arg++; |
| 1117 | if (!args[arg]) { |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1118 | memprintf(err, |
| 1119 | "'%s %s %s' : missing table name", |
| 1120 | args[0], args[1], args[kw]); |
| 1121 | free(expr); |
| 1122 | return -1; |
| 1123 | } |
| 1124 | /* we copy the table name for now, it will be resolved later */ |
Willy Tarreau | 598718a | 2012-12-09 16:57:27 +0100 | [diff] [blame] | 1125 | rule->act_prm.trk_ctr.table.n = strdup(args[arg]); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1126 | arg++; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1127 | } |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1128 | rule->act_prm.trk_ctr.expr = expr; |
| 1129 | |
| 1130 | if (args[kw][8] == '1') |
| 1131 | rule->action = TCP_ACT_TRK_SC1; |
| 1132 | else |
| 1133 | rule->action = TCP_ACT_TRK_SC2; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1134 | } |
| 1135 | else { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1136 | memprintf(err, |
| 1137 | "'%s %s' expects 'accept', 'reject', 'track-sc1' " |
| 1138 | "or 'track-sc2' in %s '%s' (got '%s')", |
| 1139 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]); |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1140 | return -1; |
| 1141 | } |
| 1142 | |
| 1143 | if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1144 | if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) { |
| 1145 | memprintf(err, |
| 1146 | "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s", |
| 1147 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err); |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1148 | return -1; |
| 1149 | } |
| 1150 | } |
| 1151 | else if (*args[arg]) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1152 | memprintf(err, |
| 1153 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')", |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1154 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]); |
| 1155 | return -1; |
| 1156 | } |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1160 | /* This function should be called to parse a line starting with the "tcp-response" |
| 1161 | * keyword. |
| 1162 | */ |
| 1163 | static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 28a47d6 | 2012-09-18 20:02:48 +0200 | [diff] [blame] | 1164 | struct proxy *defpx, const char *file, int line, |
| 1165 | char **err) |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1166 | { |
| 1167 | const char *ptr = NULL; |
| 1168 | unsigned int val; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1169 | int warn = 0; |
| 1170 | int arg; |
| 1171 | struct tcp_rule *rule; |
| 1172 | |
| 1173 | if (!*args[1]) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1174 | memprintf(err, "missing argument for '%s' in %s '%s'", |
| 1175 | args[0], proxy_type_str(curpx), curpx->id); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1176 | return -1; |
| 1177 | } |
| 1178 | |
| 1179 | if (strcmp(args[1], "inspect-delay") == 0) { |
| 1180 | if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1181 | memprintf(err, "%s %s is only allowed in 'backend' sections", |
| 1182 | args[0], args[1]); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1183 | return -1; |
| 1184 | } |
| 1185 | |
| 1186 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1187 | memprintf(err, |
| 1188 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
| 1189 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1190 | if (ptr) |
| 1191 | memprintf(err, "%s (unexpected character '%c')", *err, *ptr); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1192 | return -1; |
| 1193 | } |
| 1194 | |
| 1195 | if (curpx->tcp_rep.inspect_delay) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1196 | memprintf(err, "ignoring %s %s (was already defined) in %s '%s'", |
| 1197 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1198 | return 1; |
| 1199 | } |
| 1200 | curpx->tcp_rep.inspect_delay = val; |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1204 | rule = calloc(1, sizeof(*rule)); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1205 | LIST_INIT(&rule->list); |
| 1206 | arg = 1; |
| 1207 | |
| 1208 | if (strcmp(args[1], "content") == 0) { |
| 1209 | arg++; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1210 | if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0) |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1211 | goto error; |
| 1212 | |
| 1213 | if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) { |
| 1214 | struct acl *acl; |
| 1215 | const char *name; |
| 1216 | |
| 1217 | acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE); |
| 1218 | name = acl ? acl->name : "(unknown)"; |
| 1219 | |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1220 | memprintf(err, |
| 1221 | "acl '%s' involves some request-only criteria which will be ignored in '%s %s'", |
| 1222 | name, args[0], args[1]); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1223 | warn++; |
| 1224 | } |
| 1225 | |
| 1226 | LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list); |
| 1227 | } |
| 1228 | else { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1229 | memprintf(err, |
| 1230 | "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')", |
| 1231 | args[0], proxy_type_str(curpx), curpx->id, args[1]); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1232 | goto error; |
| 1233 | } |
| 1234 | |
| 1235 | return warn; |
| 1236 | error: |
| 1237 | free(rule); |
| 1238 | return -1; |
| 1239 | } |
| 1240 | |
| 1241 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1242 | /* This function should be called to parse a line starting with the "tcp-request" |
| 1243 | * keyword. |
| 1244 | */ |
| 1245 | static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 28a47d6 | 2012-09-18 20:02:48 +0200 | [diff] [blame] | 1246 | struct proxy *defpx, const char *file, int line, |
| 1247 | char **err) |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1248 | { |
| 1249 | const char *ptr = NULL; |
Willy Tarreau | c7e961e | 2008-08-17 17:13:47 +0200 | [diff] [blame] | 1250 | unsigned int val; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1251 | int warn = 0; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1252 | int arg; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1253 | struct tcp_rule *rule; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1254 | |
| 1255 | if (!*args[1]) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1256 | if (curpx == defpx) |
| 1257 | memprintf(err, "missing argument for '%s' in defaults section", args[0]); |
| 1258 | else |
| 1259 | memprintf(err, "missing argument for '%s' in %s '%s'", |
| 1260 | args[0], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1261 | return -1; |
| 1262 | } |
| 1263 | |
| 1264 | if (!strcmp(args[1], "inspect-delay")) { |
| 1265 | if (curpx == defpx) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1266 | memprintf(err, "%s %s is not allowed in 'defaults' sections", |
| 1267 | args[0], args[1]); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1268 | return -1; |
| 1269 | } |
| 1270 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1271 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1272 | memprintf(err, |
| 1273 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
| 1274 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1275 | if (ptr) |
| 1276 | memprintf(err, "%s (unexpected character '%c')", *err, *ptr); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | if (curpx->tcp_req.inspect_delay) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1281 | memprintf(err, "ignoring %s %s (was already defined) in %s '%s'", |
| 1282 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1283 | return 1; |
| 1284 | } |
| 1285 | curpx->tcp_req.inspect_delay = val; |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1289 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 1290 | LIST_INIT(&rule->list); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1291 | arg = 1; |
| 1292 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1293 | if (strcmp(args[1], "content") == 0) { |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 1294 | arg++; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1295 | if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0) |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1296 | goto error; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1297 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1298 | if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) { |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1299 | struct acl *acl; |
| 1300 | const char *name; |
| 1301 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1302 | acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1303 | name = acl ? acl->name : "(unknown)"; |
| 1304 | |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1305 | memprintf(err, |
| 1306 | "acl '%s' involves some response-only criteria which will be ignored in '%s %s'", |
| 1307 | name, args[0], args[1]); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1308 | warn++; |
| 1309 | } |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1310 | |
| 1311 | if ((rule->action == TCP_ACT_TRK_SC1 || rule->action == TCP_ACT_TRK_SC2) && |
Willy Tarreau | b54b6ca | 2012-12-09 17:04:41 +0100 | [diff] [blame] | 1312 | !(rule->act_prm.trk_ctr.expr->fetch->cap & SMP_CAP_REQ)) { |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1313 | memprintf(err, |
Willy Tarreau | b54b6ca | 2012-12-09 17:04:41 +0100 | [diff] [blame] | 1314 | "fetch '%s' cannot be used on requests and will be ignored in '%s %s'", |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1315 | rule->act_prm.trk_ctr.expr->fetch->kw, args[0], args[1]); |
| 1316 | warn++; |
| 1317 | } |
| 1318 | |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 1319 | LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1320 | } |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1321 | else if (strcmp(args[1], "connection") == 0) { |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1322 | arg++; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1323 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1324 | if (!(curpx->cap & PR_CAP_FE)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1325 | memprintf(err, "%s %s is not allowed because %s %s is not a frontend", |
| 1326 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Simon Horman | 6c54d8b | 2011-07-15 13:14:06 +0900 | [diff] [blame] | 1327 | goto error; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1328 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1329 | |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1330 | if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0) |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1331 | goto error; |
| 1332 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1333 | if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) { |
| 1334 | struct acl *acl; |
| 1335 | const char *name; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 1336 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1337 | acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY); |
| 1338 | name = acl ? acl->name : "(unknown)"; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 1339 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1340 | if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1341 | memprintf(err, |
| 1342 | "'%s %s' may not reference acl '%s' which makes use of " |
| 1343 | "payload in %s '%s'. Please use '%s content' for this.", |
| 1344 | args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]); |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1345 | goto error; |
| 1346 | } |
| 1347 | if (acl->requires & ACL_USE_RTR_ANY) |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1348 | memprintf(err, |
| 1349 | "acl '%s' involves some response-only criteria which will be ignored in '%s %s'", |
| 1350 | name, args[0], args[1]); |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1351 | warn++; |
| 1352 | } |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1353 | |
| 1354 | if ((rule->action == TCP_ACT_TRK_SC1 || rule->action == TCP_ACT_TRK_SC2) && |
Willy Tarreau | b54b6ca | 2012-12-09 17:04:41 +0100 | [diff] [blame] | 1355 | !(rule->act_prm.trk_ctr.expr->fetch->cap & SMP_CAP_REQ)) { |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1356 | memprintf(err, |
Willy Tarreau | b54b6ca | 2012-12-09 17:04:41 +0100 | [diff] [blame] | 1357 | "fetch '%s' cannot be used on requests and will be ignored in '%s %s'", |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1358 | rule->act_prm.trk_ctr.expr->fetch->kw, args[0], args[1]); |
| 1359 | warn++; |
| 1360 | } |
| 1361 | |
| 1362 | if ((rule->action == TCP_ACT_TRK_SC1 || rule->action == TCP_ACT_TRK_SC2) && |
| 1363 | (rule->act_prm.trk_ctr.expr->fetch->cap & SMP_CAP_L7)) { |
| 1364 | memprintf(err, |
| 1365 | "fetch '%s' involves some layer7-only criteria which will be ignored in '%s %s'", |
| 1366 | rule->act_prm.trk_ctr.expr->fetch->kw, args[0], args[1]); |
| 1367 | warn++; |
| 1368 | } |
| 1369 | |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 1370 | LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1371 | } |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1372 | else { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1373 | if (curpx == defpx) |
| 1374 | memprintf(err, |
| 1375 | "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')", |
| 1376 | args[0], args[1]); |
| 1377 | else |
| 1378 | memprintf(err, |
| 1379 | "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')", |
| 1380 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1381 | goto error; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1384 | return warn; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1385 | error: |
| 1386 | free(rule); |
| 1387 | return -1; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1388 | } |
| 1389 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1390 | |
| 1391 | /************************************************************************/ |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1392 | /* All supported sample fetch functios must be declared here */ |
| 1393 | /************************************************************************/ |
| 1394 | |
| 1395 | /* Fetch the request RDP cookie identified in the args, or any cookie if no arg |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1396 | * is passed. It is usable both for ACL and for samples. Note: this decoder |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1397 | * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument |
| 1398 | * is a string (cookie name), other types will lead to undefined behaviour. |
| 1399 | */ |
| 1400 | int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1401 | smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1402 | const struct arg *args, struct sample *smp) |
| 1403 | { |
| 1404 | int bleft; |
| 1405 | const unsigned char *data; |
| 1406 | |
| 1407 | if (!l4 || !l4->req) |
| 1408 | return 0; |
| 1409 | |
| 1410 | smp->flags = 0; |
| 1411 | smp->type = SMP_T_CSTR; |
| 1412 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1413 | bleft = l4->req->buf->i; |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1414 | if (bleft <= 11) |
| 1415 | goto too_short; |
| 1416 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1417 | data = (const unsigned char *)l4->req->buf->p + 11; |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1418 | bleft -= 11; |
| 1419 | |
| 1420 | if (bleft <= 7) |
| 1421 | goto too_short; |
| 1422 | |
| 1423 | if (strncasecmp((const char *)data, "Cookie:", 7) != 0) |
| 1424 | goto not_cookie; |
| 1425 | |
| 1426 | data += 7; |
| 1427 | bleft -= 7; |
| 1428 | |
| 1429 | while (bleft > 0 && *data == ' ') { |
| 1430 | data++; |
| 1431 | bleft--; |
| 1432 | } |
| 1433 | |
| 1434 | if (args) { |
| 1435 | |
| 1436 | if (bleft <= args->data.str.len) |
| 1437 | goto too_short; |
| 1438 | |
| 1439 | if ((data[args->data.str.len] != '=') || |
| 1440 | strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0) |
| 1441 | goto not_cookie; |
| 1442 | |
| 1443 | data += args->data.str.len + 1; |
| 1444 | bleft -= args->data.str.len + 1; |
| 1445 | } else { |
| 1446 | while (bleft > 0 && *data != '=') { |
| 1447 | if (*data == '\r' || *data == '\n') |
| 1448 | goto not_cookie; |
| 1449 | data++; |
| 1450 | bleft--; |
| 1451 | } |
| 1452 | |
| 1453 | if (bleft < 1) |
| 1454 | goto too_short; |
| 1455 | |
| 1456 | if (*data != '=') |
| 1457 | goto not_cookie; |
| 1458 | |
| 1459 | data++; |
| 1460 | bleft--; |
| 1461 | } |
| 1462 | |
| 1463 | /* data points to cookie value */ |
| 1464 | smp->data.str.str = (char *)data; |
| 1465 | smp->data.str.len = 0; |
| 1466 | |
| 1467 | while (bleft > 0 && *data != '\r') { |
| 1468 | data++; |
| 1469 | bleft--; |
| 1470 | } |
| 1471 | |
| 1472 | if (bleft < 2) |
| 1473 | goto too_short; |
| 1474 | |
| 1475 | if (data[0] != '\r' || data[1] != '\n') |
| 1476 | goto not_cookie; |
| 1477 | |
| 1478 | smp->data.str.len = (char *)data - smp->data.str.str; |
| 1479 | smp->flags = SMP_F_VOLATILE; |
| 1480 | return 1; |
| 1481 | |
| 1482 | too_short: |
| 1483 | smp->flags = SMP_F_MAY_CHANGE; |
| 1484 | not_cookie: |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1488 | /************************************************************************/ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1489 | /* All supported ACL keywords must be declared here. */ |
| 1490 | /************************************************************************/ |
| 1491 | |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1492 | /* returns either 1 or 0 depending on whether an RDP cookie is found or not */ |
| 1493 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1494 | acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1495 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1496 | { |
| 1497 | int ret; |
| 1498 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1499 | ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp); |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1500 | |
| 1501 | if (smp->flags & SMP_F_MAY_CHANGE) |
| 1502 | return 0; |
| 1503 | |
| 1504 | smp->flags = SMP_F_VOLATILE; |
| 1505 | smp->type = SMP_T_UINT; |
| 1506 | smp->data.uint = ret; |
| 1507 | return 1; |
| 1508 | } |
| 1509 | |
| 1510 | |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1511 | /* fetch the connection's source IPv4/IPv6 address */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1512 | static int |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1513 | smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1514 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1515 | { |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1516 | switch (l4->si[0].conn->addr.from.ss_family) { |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1517 | case AF_INET: |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1518 | smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1519 | smp->type = SMP_T_IPV4; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1520 | break; |
| 1521 | case AF_INET6: |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1522 | smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1523 | smp->type = SMP_T_IPV6; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1524 | break; |
| 1525 | default: |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1526 | return 0; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1527 | } |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1528 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1529 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1530 | return 1; |
| 1531 | } |
| 1532 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1533 | /* set temp integer to the connection's source port */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1534 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 1535 | smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1536 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1537 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1538 | smp->type = SMP_T_UINT; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1539 | if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.from))) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1540 | return 0; |
| 1541 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1542 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1543 | return 1; |
| 1544 | } |
| 1545 | |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1546 | /* fetch the connection's destination IPv4/IPv6 address */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1547 | static int |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1548 | smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1549 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1550 | { |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1551 | conn_get_to_addr(l4->si[0].conn); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1552 | |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1553 | switch (l4->si[0].conn->addr.to.ss_family) { |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1554 | case AF_INET: |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1555 | smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.to)->sin_addr; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1556 | smp->type = SMP_T_IPV4; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1557 | break; |
| 1558 | case AF_INET6: |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1559 | smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.to))->sin6_addr; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1560 | smp->type = SMP_T_IPV6; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1561 | break; |
| 1562 | default: |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1563 | return 0; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1564 | } |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1565 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1566 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1567 | return 1; |
| 1568 | } |
| 1569 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1570 | /* set temp integer to the frontend connexion's destination port */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1571 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 1572 | smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1573 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1574 | { |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1575 | conn_get_to_addr(l4->si[0].conn); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1576 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1577 | smp->type = SMP_T_UINT; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 1578 | if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.to))) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1579 | return 0; |
| 1580 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1581 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1582 | return 1; |
| 1583 | } |
| 1584 | |
| 1585 | static int |
Willy Tarreau | 0ce3aa0 | 2012-04-25 18:46:33 +0200 | [diff] [blame] | 1586 | smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 1587 | const struct arg *arg_p, struct sample *smp) |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1588 | { |
Willy Tarreau | 82ea800 | 2012-04-25 19:19:43 +0200 | [diff] [blame] | 1589 | unsigned int len_offset = arg_p[0].data.uint; |
| 1590 | unsigned int len_size = arg_p[1].data.uint; |
| 1591 | unsigned int buf_offset; |
| 1592 | unsigned int buf_size = 0; |
Willy Tarreau | 697d850 | 2012-10-12 23:53:39 +0200 | [diff] [blame] | 1593 | struct channel *chn; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1594 | int i; |
| 1595 | |
| 1596 | /* Format is (len offset, len size, buf offset) or (len offset, len size) */ |
| 1597 | /* by default buf offset == len offset + len size */ |
| 1598 | /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */ |
| 1599 | |
| 1600 | if (!l4) |
| 1601 | return 0; |
| 1602 | |
Willy Tarreau | 697d850 | 2012-10-12 23:53:39 +0200 | [diff] [blame] | 1603 | chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1604 | |
Willy Tarreau | 697d850 | 2012-10-12 23:53:39 +0200 | [diff] [blame] | 1605 | if (!chn) |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1606 | return 0; |
| 1607 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1608 | if (len_offset + len_size > chn->buf->i) |
Willy Tarreau | 0ce3aa0 | 2012-04-25 18:46:33 +0200 | [diff] [blame] | 1609 | goto too_short; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1610 | |
| 1611 | for (i = 0; i < len_size; i++) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1612 | buf_size = (buf_size << 8) + ((unsigned char *)chn->buf->p)[i + len_offset]; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1613 | } |
| 1614 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 1615 | /* buf offset may be implicit, absolute or relative */ |
| 1616 | buf_offset = len_offset + len_size; |
| 1617 | if (arg_p[2].type == ARGT_UINT) |
| 1618 | buf_offset = arg_p[2].data.uint; |
| 1619 | else if (arg_p[2].type == ARGT_SINT) |
| 1620 | buf_offset += arg_p[2].data.sint; |
| 1621 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1622 | if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) { |
Willy Tarreau | 82ea800 | 2012-04-25 19:19:43 +0200 | [diff] [blame] | 1623 | /* will never match */ |
| 1624 | smp->flags = 0; |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1628 | if (buf_offset + buf_size > chn->buf->i) |
Willy Tarreau | 0ce3aa0 | 2012-04-25 18:46:33 +0200 | [diff] [blame] | 1629 | goto too_short; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1630 | |
| 1631 | /* init chunk as read only */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1632 | smp->type = SMP_T_CBIN; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1633 | chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size); |
Willy Tarreau | 0ce3aa0 | 2012-04-25 18:46:33 +0200 | [diff] [blame] | 1634 | smp->flags = SMP_F_VOLATILE; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1635 | return 1; |
Willy Tarreau | 0ce3aa0 | 2012-04-25 18:46:33 +0200 | [diff] [blame] | 1636 | |
| 1637 | too_short: |
| 1638 | smp->flags = SMP_F_MAY_CHANGE; |
| 1639 | return 0; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | static int |
Willy Tarreau | 0ce3aa0 | 2012-04-25 18:46:33 +0200 | [diff] [blame] | 1643 | smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 1644 | const struct arg *arg_p, struct sample *smp) |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1645 | { |
Willy Tarreau | 82ea800 | 2012-04-25 19:19:43 +0200 | [diff] [blame] | 1646 | unsigned int buf_offset = arg_p[0].data.uint; |
| 1647 | unsigned int buf_size = arg_p[1].data.uint; |
Willy Tarreau | 697d850 | 2012-10-12 23:53:39 +0200 | [diff] [blame] | 1648 | struct channel *chn; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1649 | |
| 1650 | if (!l4) |
| 1651 | return 0; |
| 1652 | |
Willy Tarreau | 697d850 | 2012-10-12 23:53:39 +0200 | [diff] [blame] | 1653 | chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1654 | |
Willy Tarreau | 697d850 | 2012-10-12 23:53:39 +0200 | [diff] [blame] | 1655 | if (!chn) |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1656 | return 0; |
Willy Tarreau | 82ea800 | 2012-04-25 19:19:43 +0200 | [diff] [blame] | 1657 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1658 | if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) { |
Willy Tarreau | 82ea800 | 2012-04-25 19:19:43 +0200 | [diff] [blame] | 1659 | /* will never match */ |
| 1660 | smp->flags = 0; |
| 1661 | return 0; |
| 1662 | } |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1663 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1664 | if (buf_offset + buf_size > chn->buf->i) |
Willy Tarreau | 0ce3aa0 | 2012-04-25 18:46:33 +0200 | [diff] [blame] | 1665 | goto too_short; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1666 | |
| 1667 | /* init chunk as read only */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1668 | smp->type = SMP_T_CBIN; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1669 | chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size); |
Willy Tarreau | 0ce3aa0 | 2012-04-25 18:46:33 +0200 | [diff] [blame] | 1670 | smp->flags = SMP_F_VOLATILE; |
Simon Horman | ab814e0 | 2011-06-24 14:50:20 +0900 | [diff] [blame] | 1671 | return 1; |
Willy Tarreau | 0ce3aa0 | 2012-04-25 18:46:33 +0200 | [diff] [blame] | 1672 | |
| 1673 | too_short: |
| 1674 | smp->flags = SMP_F_MAY_CHANGE; |
| 1675 | return 0; |
Simon Horman | ab814e0 | 2011-06-24 14:50:20 +0900 | [diff] [blame] | 1676 | } |
| 1677 | |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 1678 | /* This function is used to validate the arguments passed to a "payload" fetch |
| 1679 | * keyword. This keyword expects two positive integers, with the second one |
| 1680 | * being strictly positive. It is assumed that the types are already the correct |
| 1681 | * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be |
| 1682 | * filled with a pointer to an error message in case of error, that the caller |
| 1683 | * is responsible for freeing. The initial location must either be freeable or |
| 1684 | * NULL. |
| 1685 | */ |
| 1686 | static int val_payload(struct arg *arg, char **err_msg) |
| 1687 | { |
| 1688 | if (!arg[1].data.uint) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1689 | memprintf(err_msg, "payload length must be > 0"); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 1690 | return 0; |
| 1691 | } |
| 1692 | return 1; |
| 1693 | } |
| 1694 | |
| 1695 | /* This function is used to validate the arguments passed to a "payload_lv" fetch |
| 1696 | * keyword. This keyword allows two positive integers and an optional signed one, |
| 1697 | * with the second one being strictly positive and the third one being greater than |
| 1698 | * the opposite of the two others if negative. It is assumed that the types are |
| 1699 | * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is |
| 1700 | * not NULL, it will be filled with a pointer to an error message in case of |
| 1701 | * error, that the caller is responsible for freeing. The initial location must |
| 1702 | * either be freeable or NULL. |
| 1703 | */ |
| 1704 | static int val_payload_lv(struct arg *arg, char **err_msg) |
| 1705 | { |
| 1706 | if (!arg[1].data.uint) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1707 | memprintf(err_msg, "payload length must be > 0"); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | if (arg[2].type == ARGT_SINT && |
| 1712 | (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1713 | memprintf(err_msg, "payload offset too negative"); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 1714 | return 0; |
| 1715 | } |
| 1716 | return 1; |
| 1717 | } |
| 1718 | |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1719 | #ifdef IPV6_V6ONLY |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 1720 | /* parse the "v4v6" bind keyword */ |
| 1721 | static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1722 | { |
| 1723 | struct listener *l; |
| 1724 | |
| 1725 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1726 | if (l->addr.ss_family == AF_INET6) |
| 1727 | l->options |= LI_O_V4V6; |
| 1728 | } |
| 1729 | |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1733 | /* parse the "v6only" bind keyword */ |
| 1734 | static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1735 | { |
| 1736 | struct listener *l; |
| 1737 | |
| 1738 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1739 | if (l->addr.ss_family == AF_INET6) |
| 1740 | l->options |= LI_O_V6ONLY; |
| 1741 | } |
| 1742 | |
| 1743 | return 0; |
| 1744 | } |
| 1745 | #endif |
| 1746 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1747 | #ifdef CONFIG_HAP_LINUX_TPROXY |
| 1748 | /* parse the "transparent" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1749 | static int bind_parse_transparent(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1750 | { |
| 1751 | struct listener *l; |
| 1752 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1753 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1754 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1755 | l->options |= LI_O_FOREIGN; |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1756 | } |
| 1757 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1758 | return 0; |
| 1759 | } |
| 1760 | #endif |
| 1761 | |
| 1762 | #ifdef TCP_DEFER_ACCEPT |
| 1763 | /* parse the "defer-accept" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1764 | static int bind_parse_defer_accept(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1765 | { |
| 1766 | struct listener *l; |
| 1767 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1768 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1769 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1770 | l->options |= LI_O_DEF_ACCEPT; |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1771 | } |
| 1772 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1773 | return 0; |
| 1774 | } |
| 1775 | #endif |
| 1776 | |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 1777 | #ifdef TCP_FASTOPEN |
| 1778 | /* parse the "defer-accept" bind keyword */ |
| 1779 | static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1780 | { |
| 1781 | struct listener *l; |
| 1782 | |
| 1783 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1784 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1785 | l->options |= LI_O_TCP_FO; |
| 1786 | } |
| 1787 | |
| 1788 | return 0; |
| 1789 | } |
| 1790 | #endif |
| 1791 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1792 | #ifdef TCP_MAXSEG |
| 1793 | /* parse the "mss" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1794 | static int bind_parse_mss(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1795 | { |
| 1796 | struct listener *l; |
| 1797 | int mss; |
| 1798 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1799 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1800 | memprintf(err, "'%s' : missing MSS value", args[cur_arg]); |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1801 | return ERR_ALERT | ERR_FATAL; |
| 1802 | } |
| 1803 | |
| 1804 | mss = atoi(args[cur_arg + 1]); |
| 1805 | if (!mss || abs(mss) > 65535) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1806 | memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]); |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1807 | return ERR_ALERT | ERR_FATAL; |
| 1808 | } |
| 1809 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1810 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1811 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1812 | l->maxseg = mss; |
| 1813 | } |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1814 | |
| 1815 | return 0; |
| 1816 | } |
| 1817 | #endif |
| 1818 | |
| 1819 | #ifdef SO_BINDTODEVICE |
| 1820 | /* parse the "mss" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1821 | static int bind_parse_interface(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1822 | { |
| 1823 | struct listener *l; |
| 1824 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1825 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1826 | memprintf(err, "'%s' : missing interface name", args[cur_arg]); |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1827 | return ERR_ALERT | ERR_FATAL; |
| 1828 | } |
| 1829 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1830 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1831 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1832 | l->interface = strdup(args[cur_arg + 1]); |
| 1833 | } |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1834 | |
| 1835 | global.last_checks |= LSTCHK_NETADM; |
| 1836 | return 0; |
| 1837 | } |
| 1838 | #endif |
| 1839 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1840 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 1841 | { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req }, |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1842 | { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep }, |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1843 | { 0, NULL, NULL }, |
| 1844 | }}; |
| 1845 | |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 1846 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1847 | * Please take care of keeping this list alphabetically sorted. |
| 1848 | */ |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1849 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1850 | { "dst", acl_parse_ip, smp_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 1851 | { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 }, |
Willy Tarreau | 0d5fe14 | 2012-04-26 12:24:45 +0200 | [diff] [blame] | 1852 | { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload }, |
| 1853 | { "payload_lv", acl_parse_str, smp_fetch_payload_lv, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG3(2,UINT,UINT,SINT), val_payload_lv }, |
Willy Tarreau | 9fb4bc7 | 2012-04-24 00:09:26 +0200 | [diff] [blame] | 1854 | { "req_rdp_cookie", acl_parse_str, smp_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1855 | { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1856 | { "src", acl_parse_ip, smp_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP, 0 }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 1857 | { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 }, |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1858 | { NULL, NULL, NULL, NULL }, |
| 1859 | }}; |
| 1860 | |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1861 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1862 | * Note: fetches that may return multiple types must be declared as the lowest |
| 1863 | * common denominator, the type that can be casted into all other ones. For |
| 1864 | * instance v4/v6 must be declared v4. |
| 1865 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1866 | static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{ |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1867 | { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES }, |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1868 | { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 1869 | { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES }, |
Willy Tarreau | 0ce3aa0 | 2012-04-25 18:46:33 +0200 | [diff] [blame] | 1870 | { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES }, |
| 1871 | { "payload_lv", smp_fetch_payload_lv, ARG3(2,UINT,UINT,SINT), val_payload_lv, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES }, |
Willy Tarreau | d6281ae | 2012-04-26 11:23:39 +0200 | [diff] [blame] | 1872 | { "rdp_cookie", smp_fetch_rdp_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 1873 | { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES }, |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 1874 | { NULL, NULL, 0, 0, 0 }, |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1875 | }}; |
| 1876 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1877 | /************************************************************************/ |
| 1878 | /* All supported bind keywords must be declared here. */ |
| 1879 | /************************************************************************/ |
| 1880 | |
| 1881 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1882 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1883 | * all code contributors. |
| 1884 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1885 | * the config parser can report an appropriate error when a known keyword was |
| 1886 | * not enabled. |
| 1887 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 1888 | static struct bind_kw_list bind_kws = { "TCP", { }, { |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1889 | #ifdef TCP_DEFER_ACCEPT |
| 1890 | { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */ |
| 1891 | #endif |
| 1892 | #ifdef SO_BINDTODEVICE |
| 1893 | { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */ |
| 1894 | #endif |
| 1895 | #ifdef TCP_MAXSEG |
| 1896 | { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */ |
| 1897 | #endif |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 1898 | #ifdef TCP_FASTOPEN |
| 1899 | { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */ |
| 1900 | #endif |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1901 | #ifdef CONFIG_HAP_LINUX_TPROXY |
| 1902 | { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */ |
| 1903 | #endif |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1904 | #ifdef IPV6_V6ONLY |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 1905 | { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */ |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1906 | { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */ |
| 1907 | #endif |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1908 | /* the versions with the NULL parse function*/ |
| 1909 | { "defer-accept", NULL, 0 }, |
| 1910 | { "interface", NULL, 1 }, |
| 1911 | { "mss", NULL, 1 }, |
| 1912 | { "transparent", NULL, 0 }, |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 1913 | { "v4v6", NULL, 0 }, |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1914 | { "v6only", NULL, 0 }, |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1915 | { NULL, NULL, 0 }, |
| 1916 | }}; |
| 1917 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1918 | __attribute__((constructor)) |
| 1919 | static void __tcp_protocol_init(void) |
| 1920 | { |
| 1921 | protocol_register(&proto_tcpv4); |
| 1922 | protocol_register(&proto_tcpv6); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1923 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1924 | cfg_register_keywords(&cfg_kws); |
| 1925 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1926 | bind_register_keywords(&bind_kws); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | |
| 1930 | /* |
| 1931 | * Local variables: |
| 1932 | * c-indent-level: 8 |
| 1933 | * c-basic-offset: 8 |
| 1934 | * End: |
| 1935 | */ |