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