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