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