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