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