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