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