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