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 | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 4 | * Copyright 2000-2013 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> |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 36 | #include <common/namespace.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 37 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 38 | #include <types/global.h> |
Willy Tarreau | 18bf01e | 2014-06-13 16:18:52 +0200 | [diff] [blame] | 39 | #include <types/capture.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 40 | #include <types/server.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 41 | |
| 42 | #include <proto/acl.h> |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 43 | #include <proto/arg.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 44 | #include <proto/channel.h> |
Willy Tarreau | d2274c6 | 2012-07-06 14:29:45 +0200 | [diff] [blame] | 45 | #include <proto/connection.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 46 | #include <proto/fd.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 47 | #include <proto/listener.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 48 | #include <proto/log.h> |
| 49 | #include <proto/port_range.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 50 | #include <proto/protocol.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 51 | #include <proto/proto_tcp.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 52 | #include <proto/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 53 | #include <proto/sample.h> |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 54 | #include <proto/session.h> |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 55 | #include <proto/stick_table.h> |
Willy Tarreau | cc1e04b | 2013-09-11 23:20:29 +0200 | [diff] [blame] | 56 | #include <proto/stream_interface.h> |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 57 | #include <proto/task.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 58 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 59 | #ifdef CONFIG_HAP_CTTPROXY |
| 60 | #include <import/ip_tproxy.h> |
| 61 | #endif |
| 62 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 63 | static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen); |
| 64 | static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 65 | |
| 66 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 67 | static struct protocol proto_tcpv4 = { |
| 68 | .name = "tcpv4", |
| 69 | .sock_domain = AF_INET, |
| 70 | .sock_type = SOCK_STREAM, |
| 71 | .sock_prot = IPPROTO_TCP, |
| 72 | .sock_family = AF_INET, |
| 73 | .sock_addrlen = sizeof(struct sockaddr_in), |
| 74 | .l3_addrlen = 32/8, |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 75 | .accept = &listener_accept, |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 76 | .connect = tcp_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 77 | .bind = tcp_bind_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 78 | .bind_all = tcp_bind_listeners, |
| 79 | .unbind_all = unbind_all_listeners, |
| 80 | .enable_all = enable_all_listeners, |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 81 | .get_src = tcp_get_src, |
| 82 | .get_dst = tcp_get_dst, |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 83 | .drain = tcp_drain, |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 84 | .pause = tcp_pause_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 85 | .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners), |
| 86 | .nb_listeners = 0, |
| 87 | }; |
| 88 | |
| 89 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 90 | static struct protocol proto_tcpv6 = { |
| 91 | .name = "tcpv6", |
| 92 | .sock_domain = AF_INET6, |
| 93 | .sock_type = SOCK_STREAM, |
| 94 | .sock_prot = IPPROTO_TCP, |
| 95 | .sock_family = AF_INET6, |
| 96 | .sock_addrlen = sizeof(struct sockaddr_in6), |
| 97 | .l3_addrlen = 128/8, |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 98 | .accept = &listener_accept, |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 99 | .connect = tcp_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 100 | .bind = tcp_bind_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 101 | .bind_all = tcp_bind_listeners, |
| 102 | .unbind_all = unbind_all_listeners, |
| 103 | .enable_all = enable_all_listeners, |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 104 | .get_src = tcp_get_src, |
| 105 | .get_dst = tcp_get_dst, |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 106 | .drain = tcp_drain, |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 107 | .pause = tcp_pause_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 108 | .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners), |
| 109 | .nb_listeners = 0, |
| 110 | }; |
| 111 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 112 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 113 | /* 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] | 114 | * case we try to bind <remote>. <flags> is a 2-bit field consisting of : |
| 115 | * - 0 : ignore remote address (may even be a NULL pointer) |
| 116 | * - 1 : use provided address |
| 117 | * - 2 : use provided port |
| 118 | * - 3 : use both |
| 119 | * |
| 120 | * The function supports multiple foreign binding methods : |
| 121 | * - linux_tproxy: we directly bind to the foreign address |
| 122 | * - cttproxy: we bind to a local address then nat. |
| 123 | * The second one can be used as a fallback for the first one. |
| 124 | * This function returns 0 when everything's OK, 1 if it could not bind, to the |
| 125 | * local address, 2 if it could not bind to the foreign address. |
| 126 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 127 | 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] | 128 | { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 129 | struct sockaddr_storage bind_addr; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 130 | int foreign_ok = 0; |
| 131 | int ret; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 132 | static int ip_transp_working = 1; |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 133 | static int ip6_transp_working = 1; |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 134 | |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 135 | switch (local->ss_family) { |
| 136 | case AF_INET: |
| 137 | if (flags && ip_transp_working) { |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 138 | /* This deserves some explanation. Some platforms will support |
| 139 | * multiple combinations of certain methods, so we try the |
| 140 | * supported ones until one succeeds. |
| 141 | */ |
| 142 | if (0 |
| 143 | #if defined(IP_TRANSPARENT) |
| 144 | || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0) |
| 145 | #endif |
| 146 | #if defined(IP_FREEBIND) |
| 147 | || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0) |
| 148 | #endif |
Pieter Baauw | ff30b66 | 2013-05-08 23:22:39 +0200 | [diff] [blame] | 149 | #if defined(IP_BINDANY) |
| 150 | || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0) |
| 151 | #endif |
Pieter Baauw | 1eb7592 | 2013-05-08 23:30:23 +0200 | [diff] [blame] | 152 | #if defined(SO_BINDANY) |
| 153 | || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0) |
| 154 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 155 | ) |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 156 | foreign_ok = 1; |
| 157 | else |
| 158 | ip_transp_working = 0; |
| 159 | } |
| 160 | break; |
| 161 | case AF_INET6: |
| 162 | if (flags && ip6_transp_working) { |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 163 | if (0 |
| 164 | #if defined(IPV6_TRANSPARENT) |
| 165 | || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0) |
| 166 | #endif |
Lukas Tribus | 7640e72 | 2014-03-03 21:10:51 +0100 | [diff] [blame] | 167 | #if defined(IP_FREEBIND) |
| 168 | || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0) |
| 169 | #endif |
Pieter Baauw | ff30b66 | 2013-05-08 23:22:39 +0200 | [diff] [blame] | 170 | #if defined(IPV6_BINDANY) |
| 171 | || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0) |
| 172 | #endif |
Pieter Baauw | 1eb7592 | 2013-05-08 23:30:23 +0200 | [diff] [blame] | 173 | #if defined(SO_BINDANY) |
| 174 | || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0) |
| 175 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 176 | ) |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 177 | foreign_ok = 1; |
| 178 | else |
| 179 | ip6_transp_working = 0; |
| 180 | } |
| 181 | break; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 182 | } |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 183 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 184 | if (flags) { |
| 185 | memset(&bind_addr, 0, sizeof(bind_addr)); |
Willy Tarreau | 96dd079 | 2011-04-19 07:20:57 +0200 | [diff] [blame] | 186 | bind_addr.ss_family = remote->ss_family; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 187 | switch (remote->ss_family) { |
| 188 | case AF_INET: |
| 189 | if (flags & 1) |
| 190 | ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr; |
| 191 | if (flags & 2) |
| 192 | ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port; |
| 193 | break; |
| 194 | case AF_INET6: |
| 195 | if (flags & 1) |
| 196 | ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr; |
| 197 | if (flags & 2) |
| 198 | ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port; |
| 199 | break; |
Willy Tarreau | 5dc1e98 | 2011-12-16 21:25:11 +0100 | [diff] [blame] | 200 | default: |
| 201 | /* we don't want to try to bind to an unknown address family */ |
| 202 | foreign_ok = 0; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 203 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 206 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 207 | if (foreign_ok) { |
Willy Tarreau | 9cf8d3f | 2014-05-09 22:56:10 +0200 | [diff] [blame] | 208 | if (is_inet_addr(&bind_addr)) { |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 209 | ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr)); |
| 210 | if (ret < 0) |
| 211 | return 2; |
| 212 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 213 | } |
| 214 | else { |
Willy Tarreau | 9cf8d3f | 2014-05-09 22:56:10 +0200 | [diff] [blame] | 215 | if (is_inet_addr(local)) { |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 216 | ret = bind(fd, (struct sockaddr *)local, get_addr_len(local)); |
| 217 | if (ret < 0) |
| 218 | return 1; |
| 219 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | if (!flags) |
| 223 | return 0; |
| 224 | |
| 225 | #ifdef CONFIG_HAP_CTTPROXY |
Willy Tarreau | 6f831b4 | 2011-03-20 14:03:54 +0100 | [diff] [blame] | 226 | if (!foreign_ok && remote->ss_family == AF_INET) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 227 | struct in_tproxy itp1, itp2; |
| 228 | memset(&itp1, 0, sizeof(itp1)); |
| 229 | |
| 230 | itp1.op = TPROXY_ASSIGN; |
Willy Tarreau | 6f831b4 | 2011-03-20 14:03:54 +0100 | [diff] [blame] | 231 | itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr; |
| 232 | itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 233 | |
| 234 | /* set connect flag on socket */ |
| 235 | itp2.op = TPROXY_FLAGS; |
| 236 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 237 | |
| 238 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 && |
| 239 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) { |
| 240 | foreign_ok = 1; |
| 241 | } |
| 242 | } |
| 243 | #endif |
| 244 | if (!foreign_ok) |
| 245 | /* we could not bind to a foreign address */ |
| 246 | return 2; |
| 247 | |
| 248 | return 0; |
| 249 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 250 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 251 | static int create_server_socket(struct connection *conn) |
| 252 | { |
Willy Tarreau | 529c139 | 2014-12-24 13:47:55 +0100 | [diff] [blame] | 253 | const struct netns_entry *ns = NULL; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 254 | |
Willy Tarreau | 529c139 | 2014-12-24 13:47:55 +0100 | [diff] [blame] | 255 | #ifdef CONFIG_HAP_NS |
| 256 | if (objt_server(conn->target)) { |
| 257 | if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP) |
| 258 | ns = conn->proxy_netns; |
| 259 | else |
| 260 | ns = __objt_server(conn->target)->netns; |
| 261 | } |
| 262 | #endif |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 263 | return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP); |
| 264 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 265 | |
| 266 | /* |
Willy Tarreau | 14f8e86 | 2012-08-30 22:23:13 +0200 | [diff] [blame] | 267 | * This function initiates a TCP connection establishment to the target assigned |
| 268 | * to connection <conn> using (si->{target,addr.to}). A source address may be |
| 269 | * pointed to by conn->addr.from in case of transparent proxying. Normal source |
| 270 | * bind addresses are still determined locally (due to the possible need of a |
| 271 | * source port). conn->target may point either to a valid server or to a backend, |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 272 | * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 273 | * supported. The <data> parameter is a boolean indicating whether there are data |
| 274 | * waiting for being sent or not, in order to adjust data write polling and on |
| 275 | * some platforms, the ability to avoid an empty initial ACK. The <delack> argument |
| 276 | * allows the caller to force using a delayed ACK when establishing the connection : |
| 277 | * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect |
| 278 | * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data |
| 279 | * - 2 = delayed ACK regardless of backend options |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 280 | * |
Willy Tarreau | 1ec74bf | 2013-10-24 21:45:00 +0200 | [diff] [blame] | 281 | * Note that a pending send_proxy message accounts for data. |
| 282 | * |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 283 | * It can return one of : |
| 284 | * - SN_ERR_NONE if everything's OK |
| 285 | * - SN_ERR_SRVTO if there are no more servers |
| 286 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 287 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 288 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 289 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 290 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
Willy Tarreau | 6b0a850 | 2012-11-23 08:51:32 +0100 | [diff] [blame] | 291 | * |
| 292 | * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise |
| 293 | * it's invalid and the caller has nothing to do. |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 294 | */ |
Willy Tarreau | f153686 | 2011-03-03 18:27:32 +0100 | [diff] [blame] | 295 | |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 296 | int tcp_connect_server(struct connection *conn, int data, int delack) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 297 | { |
| 298 | int fd; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 299 | struct server *srv; |
| 300 | struct proxy *be; |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 301 | struct conn_src *src; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 302 | |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 303 | conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */ |
| 304 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 305 | switch (obj_type(conn->target)) { |
| 306 | case OBJ_TYPE_PROXY: |
| 307 | be = objt_proxy(conn->target); |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 308 | srv = NULL; |
| 309 | break; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 310 | case OBJ_TYPE_SERVER: |
| 311 | srv = objt_server(conn->target); |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 312 | be = srv->proxy; |
| 313 | break; |
| 314 | default: |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 315 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 316 | return SN_ERR_INTERNAL; |
| 317 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 318 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 319 | fd = conn->t.sock.fd = create_server_socket(conn); |
| 320 | |
| 321 | if (fd == -1) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 322 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 323 | |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 324 | if (errno == ENFILE) { |
| 325 | conn->err_code = CO_ER_SYS_FDLIM; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 326 | send_log(be, LOG_EMERG, |
| 327 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 328 | be->id, maxfd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 329 | } |
| 330 | else if (errno == EMFILE) { |
| 331 | conn->err_code = CO_ER_PROC_FDLIM; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 332 | send_log(be, LOG_EMERG, |
| 333 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 334 | be->id, maxfd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 335 | } |
| 336 | else if (errno == ENOBUFS || errno == ENOMEM) { |
| 337 | conn->err_code = CO_ER_SYS_MEMLIM; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 338 | send_log(be, LOG_EMERG, |
| 339 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 340 | be->id, maxfd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 341 | } |
| 342 | else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) { |
| 343 | conn->err_code = CO_ER_NOPROTO; |
| 344 | } |
| 345 | else |
| 346 | conn->err_code = CO_ER_SOCK_ERR; |
| 347 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 348 | /* this is a resource error */ |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 349 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 350 | return SN_ERR_RESOURCE; |
| 351 | } |
| 352 | |
| 353 | if (fd >= global.maxsock) { |
| 354 | /* do not log anything there, it's a normal condition when this option |
| 355 | * is used to serialize connections to a server ! |
| 356 | */ |
| 357 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 358 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 359 | conn->err_code = CO_ER_CONF_FDLIM; |
| 360 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 361 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
| 362 | } |
| 363 | |
| 364 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 365 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 366 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 367 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 368 | conn->err_code = CO_ER_SOCK_ERR; |
| 369 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 370 | return SN_ERR_INTERNAL; |
| 371 | } |
| 372 | |
| 373 | if (be->options & PR_O_TCP_SRV_KA) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 374 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 375 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 376 | /* allow specific binding : |
| 377 | * - server-specific at first |
| 378 | * - proxy-specific next |
| 379 | */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 380 | if (srv && srv->conn_src.opts & CO_SRC_BIND) |
| 381 | src = &srv->conn_src; |
| 382 | else if (be->conn_src.opts & CO_SRC_BIND) |
| 383 | src = &be->conn_src; |
| 384 | else |
| 385 | src = NULL; |
| 386 | |
| 387 | if (src) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 388 | int ret, flags = 0; |
| 389 | |
Willy Tarreau | 9cf8d3f | 2014-05-09 22:56:10 +0200 | [diff] [blame] | 390 | if (is_inet_addr(&conn->addr.from)) { |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 391 | switch (src->opts & CO_SRC_TPROXY_MASK) { |
Willy Tarreau | ef9a360 | 2012-12-08 22:29:20 +0100 | [diff] [blame] | 392 | case CO_SRC_TPROXY_ADDR: |
| 393 | case CO_SRC_TPROXY_CLI: |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 394 | flags = 3; |
| 395 | break; |
Willy Tarreau | ef9a360 | 2012-12-08 22:29:20 +0100 | [diff] [blame] | 396 | case CO_SRC_TPROXY_CIP: |
| 397 | case CO_SRC_TPROXY_DYN: |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 398 | flags = 1; |
| 399 | break; |
| 400 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 401 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 402 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 403 | #ifdef SO_BINDTODEVICE |
| 404 | /* Note: this might fail if not CAP_NET_RAW */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 405 | if (src->iface_name) |
| 406 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 407 | #endif |
| 408 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 409 | if (src->sport_range) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 410 | int attempts = 10; /* should be more than enough to find a spare port */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 411 | struct sockaddr_storage sa; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 412 | |
| 413 | ret = 1; |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 414 | sa = src->source_addr; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 415 | |
| 416 | do { |
| 417 | /* note: in case of retry, we may have to release a previously |
| 418 | * allocated port, hence this loop's construct. |
| 419 | */ |
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 | |
| 423 | if (!attempts) |
| 424 | break; |
| 425 | attempts--; |
| 426 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 427 | fdinfo[fd].local_port = port_range_alloc_port(src->sport_range); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 428 | if (!fdinfo[fd].local_port) { |
| 429 | conn->err_code = CO_ER_PORT_RANGE; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 430 | break; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 431 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 432 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 433 | fdinfo[fd].port_range = src->sport_range; |
| 434 | set_host_port(&sa, fdinfo[fd].local_port); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 435 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 436 | ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 437 | if (ret != 0) |
| 438 | conn->err_code = CO_ER_CANT_BIND; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 439 | } while (ret != 0); /* binding NOK */ |
| 440 | } |
| 441 | else { |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 442 | ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 443 | if (ret != 0) |
| 444 | conn->err_code = CO_ER_CANT_BIND; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 447 | if (unlikely(ret != 0)) { |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 448 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 449 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 450 | close(fd); |
| 451 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 452 | if (ret == 1) { |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 453 | Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 454 | be->id); |
| 455 | send_log(be, LOG_EMERG, |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 456 | "Cannot bind to source address before connect() for backend %s.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 457 | be->id); |
| 458 | } else { |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 459 | Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 460 | be->id); |
| 461 | send_log(be, LOG_EMERG, |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 462 | "Cannot bind to tproxy source address before connect() for backend %s.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 463 | be->id); |
| 464 | } |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 465 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 466 | return SN_ERR_RESOURCE; |
| 467 | } |
| 468 | } |
| 469 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 470 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 471 | /* disabling tcp quick ack now allows the first request to leave the |
| 472 | * machine with the first ACK. We only do this if there are pending |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 473 | * data in the buffer. |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 474 | */ |
Willy Tarreau | fb20e46 | 2014-10-24 12:02:24 +0200 | [diff] [blame] | 475 | if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON))) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 476 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 477 | #endif |
| 478 | |
Willy Tarreau | e803de2 | 2010-01-21 17:43:04 +0100 | [diff] [blame] | 479 | if (global.tune.server_sndbuf) |
| 480 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 481 | |
| 482 | if (global.tune.server_rcvbuf) |
| 483 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 484 | |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 485 | if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) && |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 486 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 487 | |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 488 | if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 489 | char *msg; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 490 | if (errno == EAGAIN || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 491 | msg = "no free ports"; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 492 | conn->err_code = CO_ER_FREE_PORTS; |
| 493 | } |
| 494 | else { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 495 | msg = "local address already in use"; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 496 | conn->err_code = CO_ER_ADDR_INUSE; |
| 497 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 498 | |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 499 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 500 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 501 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 502 | close(fd); |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 503 | send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 504 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 505 | return SN_ERR_RESOURCE; |
| 506 | } else if (errno == ETIMEDOUT) { |
| 507 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 508 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 509 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 510 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 511 | conn->err_code = CO_ER_SOCK_ERR; |
| 512 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 513 | return SN_ERR_SRVTO; |
| 514 | } else { |
| 515 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 516 | //qfprintf(stderr,"Connect(): %d", errno); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 517 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 518 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 519 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 520 | conn->err_code = CO_ER_SOCK_ERR; |
| 521 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 522 | return SN_ERR_SRVCL; |
| 523 | } |
| 524 | } |
| 525 | |
Willy Tarreau | fc8f1f0 | 2012-12-08 18:53:44 +0100 | [diff] [blame] | 526 | conn->flags |= CO_FL_ADDR_TO_SET; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 527 | |
Willy Tarreau | 1ec74bf | 2013-10-24 21:45:00 +0200 | [diff] [blame] | 528 | /* Prepare to send a few handshakes related to the on-wire protocol. */ |
| 529 | if (conn->send_proxy_ofs) |
Willy Tarreau | 57cd3e4 | 2013-10-24 22:01:26 +0200 | [diff] [blame] | 530 | conn->flags |= CO_FL_SEND_PROXY; |
Willy Tarreau | 1ec74bf | 2013-10-24 21:45:00 +0200 | [diff] [blame] | 531 | |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 532 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 533 | fdtab[fd].linger_risk = 1; /* close hard if needed */ |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 534 | conn_sock_want_send(conn); /* for connect status */ |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 535 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 536 | if (conn_xprt_init(conn) < 0) { |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 537 | conn_force_close(conn); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 538 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 539 | return SN_ERR_RESOURCE; |
Willy Tarreau | 184636e | 2012-09-06 14:04:41 +0200 | [diff] [blame] | 540 | } |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 541 | |
Willy Tarreau | 14f8e86 | 2012-08-30 22:23:13 +0200 | [diff] [blame] | 542 | if (data) |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 543 | conn_data_want_send(conn); /* prepare to send data if any */ |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 544 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 545 | return SN_ERR_NONE; /* connection is OK */ |
| 546 | } |
| 547 | |
| 548 | |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 549 | /* |
| 550 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 551 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 552 | * success, -1 in case of error. The socket's source address is stored in |
| 553 | * <sa> for <salen> bytes. |
| 554 | */ |
| 555 | int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 556 | { |
| 557 | if (dir) |
| 558 | return getsockname(fd, sa, &salen); |
| 559 | else |
| 560 | return getpeername(fd, sa, &salen); |
| 561 | } |
| 562 | |
| 563 | |
| 564 | /* |
| 565 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 566 | * indicating if we're a listener (=0) or an initiator (!=0). In the case of a |
| 567 | * listener, if the original destination address was translated, the original |
| 568 | * address is retrieved. It returns 0 in case of success, -1 in case of error. |
| 569 | * The socket's source address is stored in <sa> for <salen> bytes. |
| 570 | */ |
| 571 | int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 572 | { |
| 573 | if (dir) |
| 574 | return getpeername(fd, sa, &salen); |
Willy Tarreau | 5e0d0e0 | 2014-10-29 21:46:01 +0100 | [diff] [blame] | 575 | else { |
| 576 | int ret = getsockname(fd, sa, &salen); |
| 577 | |
| 578 | if (ret < 0) |
| 579 | return ret; |
| 580 | |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 581 | #if defined(TPROXY) && defined(SO_ORIGINAL_DST) |
Willy Tarreau | 5e0d0e0 | 2014-10-29 21:46:01 +0100 | [diff] [blame] | 582 | /* For TPROXY and Netfilter's NAT, we can retrieve the original |
| 583 | * IPv4 address before DNAT/REDIRECT. We must not do that with |
| 584 | * other families because v6-mapped IPv4 addresses are still |
| 585 | * reported as v4. |
| 586 | */ |
| 587 | if (((struct sockaddr_storage *)sa)->ss_family == AF_INET |
| 588 | && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0) |
| 589 | return 0; |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 590 | #endif |
Willy Tarreau | 5e0d0e0 | 2014-10-29 21:46:01 +0100 | [diff] [blame] | 591 | return ret; |
| 592 | } |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 595 | /* Tries to drain any pending incoming data from the socket to reach the |
Willy Tarreau | 7f4bcc3 | 2014-01-20 11:26:12 +0100 | [diff] [blame] | 596 | * receive shutdown. Returns positive if the shutdown was found, negative |
| 597 | * if EAGAIN was hit, otherwise zero. This is useful to decide whether we |
| 598 | * can close a connection cleanly are we must kill it hard. |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 599 | */ |
| 600 | int tcp_drain(int fd) |
| 601 | { |
| 602 | int turns = 2; |
| 603 | int len; |
| 604 | |
| 605 | while (turns) { |
| 606 | #ifdef MSG_TRUNC_CLEARS_INPUT |
| 607 | len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC); |
| 608 | if (len == -1 && errno == EFAULT) |
| 609 | #endif |
| 610 | len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 611 | |
Willy Tarreau | 3bd3e57 | 2014-01-20 11:56:37 +0100 | [diff] [blame] | 612 | if (len == 0) { |
| 613 | /* cool, shutdown received */ |
| 614 | fdtab[fd].linger_risk = 0; |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 615 | return 1; |
Willy Tarreau | 3bd3e57 | 2014-01-20 11:56:37 +0100 | [diff] [blame] | 616 | } |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 617 | |
| 618 | if (len < 0) { |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 619 | if (errno == EAGAIN) { |
| 620 | /* connection not closed yet */ |
| 621 | fd_cant_recv(fd); |
Willy Tarreau | 7f4bcc3 | 2014-01-20 11:26:12 +0100 | [diff] [blame] | 622 | return -1; |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 623 | } |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 624 | if (errno == EINTR) /* oops, try again */ |
| 625 | continue; |
| 626 | /* other errors indicate a dead connection, fine. */ |
Willy Tarreau | 3bd3e57 | 2014-01-20 11:56:37 +0100 | [diff] [blame] | 627 | fdtab[fd].linger_risk = 0; |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 628 | return 1; |
| 629 | } |
| 630 | /* OK we read some data, let's try again once */ |
| 631 | turns--; |
| 632 | } |
| 633 | /* some data are still present, give up */ |
| 634 | return 0; |
| 635 | } |
| 636 | |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 637 | /* This is the callback which is set when a connection establishment is pending |
Willy Tarreau | f12a20e | 2013-12-04 16:11:04 +0100 | [diff] [blame] | 638 | * and we have nothing to send. It updates the FD polling status. It returns 0 |
| 639 | * if it fails in a fatal way or needs to poll to go further, otherwise it |
| 640 | * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's |
| 641 | * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in |
| 642 | * errno. The error checking is done in two passes in order to limit the number |
| 643 | * of syscalls in the normal case : |
| 644 | * - if POLL_ERR was reported by the poller, we check for a pending error on |
| 645 | * the socket before proceeding. If found, it's assigned to errno so that |
| 646 | * upper layers can see it. |
| 647 | * - otherwise connect() is used to check the connection state again, since |
| 648 | * the getsockopt return cannot reliably be used to know if the connection |
| 649 | * is still pending or ready. This one may often return an error as well, |
| 650 | * since we don't always have POLL_ERR (eg: OSX or cached events). |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 651 | */ |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 652 | int tcp_connect_probe(struct connection *conn) |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 653 | { |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 654 | int fd = conn->t.sock.fd; |
Willy Tarreau | f12a20e | 2013-12-04 16:11:04 +0100 | [diff] [blame] | 655 | socklen_t lskerr; |
| 656 | int skerr; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 657 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 658 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 659 | return 0; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 660 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 661 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 662 | return 0; |
| 663 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 664 | if (!(conn->flags & CO_FL_WAIT_L4_CONN)) |
Willy Tarreau | 0b0c097 | 2012-07-23 20:05:00 +0200 | [diff] [blame] | 665 | return 1; /* strange we were called while ready */ |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 666 | |
Willy Tarreau | fd803bb | 2014-01-20 15:13:07 +0100 | [diff] [blame] | 667 | if (!fd_send_ready(fd)) |
| 668 | return 0; |
| 669 | |
Willy Tarreau | f12a20e | 2013-12-04 16:11:04 +0100 | [diff] [blame] | 670 | /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP |
| 671 | * without FD_POLL_IN also indicates a hangup without input data meaning |
| 672 | * there was no connection. |
| 673 | */ |
| 674 | if (fdtab[fd].ev & FD_POLL_ERR || |
| 675 | (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) { |
| 676 | skerr = 0; |
| 677 | lskerr = sizeof(skerr); |
| 678 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 679 | errno = skerr; |
| 680 | if (errno == EAGAIN) |
| 681 | errno = 0; |
| 682 | if (errno) |
| 683 | goto out_error; |
| 684 | } |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 685 | |
Willy Tarreau | f12a20e | 2013-12-04 16:11:04 +0100 | [diff] [blame] | 686 | /* Use connect() to check the state of the socket. This has the |
| 687 | * advantage of giving us the following info : |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 688 | * - error |
| 689 | * - connecting (EALREADY, EINPROGRESS) |
| 690 | * - connected (EISCONN, 0) |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 691 | */ |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 692 | if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) { |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 693 | if (errno == EALREADY || errno == EINPROGRESS) { |
Willy Tarreau | d486ef5 | 2012-12-10 17:03:52 +0100 | [diff] [blame] | 694 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 695 | fd_cant_send(fd); |
Willy Tarreau | 0b0c097 | 2012-07-23 20:05:00 +0200 | [diff] [blame] | 696 | return 0; |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 697 | } |
Willy Tarreau | a190d59 | 2012-05-20 18:35:19 +0200 | [diff] [blame] | 698 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 699 | if (errno && errno != EISCONN) |
Willy Tarreau | a190d59 | 2012-05-20 18:35:19 +0200 | [diff] [blame] | 700 | goto out_error; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 701 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 702 | /* otherwise we're connected */ |
Willy Tarreau | a190d59 | 2012-05-20 18:35:19 +0200 | [diff] [blame] | 703 | } |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 704 | |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 705 | /* The FD is ready now, we'll mark the connection as complete and |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 706 | * forward the event to the transport layer which will notify the |
| 707 | * data layer. |
Willy Tarreau | a190d59 | 2012-05-20 18:35:19 +0200 | [diff] [blame] | 708 | */ |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 709 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 0b0c097 | 2012-07-23 20:05:00 +0200 | [diff] [blame] | 710 | return 1; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 711 | |
| 712 | out_error: |
Willy Tarreau | 0b0c097 | 2012-07-23 20:05:00 +0200 | [diff] [blame] | 713 | /* Write error on the file descriptor. Report it to the connection |
| 714 | * and disable polling on this FD. |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 715 | */ |
Willy Tarreau | 3bd3e57 | 2014-01-20 11:56:37 +0100 | [diff] [blame] | 716 | fdtab[fd].linger_risk = 0; |
Willy Tarreau | 26f4a04 | 2013-12-04 23:44:10 +0100 | [diff] [blame] | 717 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
Willy Tarreau | d486ef5 | 2012-12-10 17:03:52 +0100 | [diff] [blame] | 718 | __conn_sock_stop_both(conn); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 719 | return 0; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 722 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 723 | /* This function tries to bind a TCPv4/v6 listener. It may return a warning or |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 724 | * an error message in <errmsg> if the message is at most <errlen> bytes long |
| 725 | * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero. |
| 726 | * The return value is composed from ERR_ABORT, ERR_WARN, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 727 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 728 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 729 | * 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] | 730 | * 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] | 731 | * the meaning of the error, but just indicate that a message is present which |
| 732 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 733 | * that it's pointless to try to start other listeners. No error message is |
| 734 | * returned if errlen is NULL. |
| 735 | */ |
| 736 | int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 737 | { |
| 738 | __label__ tcp_return, tcp_close_return; |
| 739 | int fd, err; |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 740 | int ext, ready; |
| 741 | socklen_t ready_len; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 742 | const char *msg = NULL; |
| 743 | |
| 744 | /* ensure we never return garbage */ |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 745 | if (errlen) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 746 | *errmsg = 0; |
| 747 | |
| 748 | if (listener->state != LI_ASSIGNED) |
| 749 | return ERR_NONE; /* already bound */ |
| 750 | |
| 751 | err = ERR_NONE; |
| 752 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 753 | /* if the listener already has an fd assigned, then we were offered the |
| 754 | * fd by an external process (most likely the parent), and we don't want |
| 755 | * to create a new socket. However we still want to set a few flags on |
| 756 | * the socket. |
| 757 | */ |
| 758 | fd = listener->fd; |
| 759 | ext = (fd >= 0); |
| 760 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 761 | if (!ext) { |
| 762 | fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP); |
| 763 | |
| 764 | if (fd == -1) { |
| 765 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 766 | msg = "cannot create listening socket"; |
| 767 | goto tcp_return; |
| 768 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 769 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 770 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 771 | if (fd >= global.maxsock) { |
| 772 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 773 | msg = "not enough free sockets (raise '-n' parameter)"; |
| 774 | goto tcp_close_return; |
| 775 | } |
| 776 | |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 777 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 778 | err |= ERR_FATAL | ERR_ALERT; |
| 779 | msg = "cannot make socket non-blocking"; |
| 780 | goto tcp_close_return; |
| 781 | } |
| 782 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 783 | if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 784 | /* not fatal but should be reported */ |
| 785 | msg = "cannot do so_reuseaddr"; |
| 786 | err |= ERR_ALERT; |
| 787 | } |
| 788 | |
| 789 | if (listener->options & LI_O_NOLINGER) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 790 | setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger)); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 791 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 792 | #ifdef SO_REUSEPORT |
| 793 | /* OpenBSD supports this. As it's present in old libc versions of Linux, |
| 794 | * it might return an error that we will silently ignore. |
| 795 | */ |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 796 | if (!ext) |
| 797 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 798 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 799 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 800 | if (!ext && (listener->options & LI_O_FOREIGN)) { |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 801 | switch (listener->addr.ss_family) { |
| 802 | case AF_INET: |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 803 | if (1 |
| 804 | #if defined(IP_TRANSPARENT) |
| 805 | && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1) |
| 806 | #endif |
| 807 | #if defined(IP_FREEBIND) |
| 808 | && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1) |
| 809 | #endif |
Pieter Baauw | ff30b66 | 2013-05-08 23:22:39 +0200 | [diff] [blame] | 810 | #if defined(IP_BINDANY) |
| 811 | && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1) |
| 812 | #endif |
Pieter Baauw | 1eb7592 | 2013-05-08 23:30:23 +0200 | [diff] [blame] | 813 | #if defined(SO_BINDANY) |
| 814 | && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1) |
| 815 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 816 | ) { |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 817 | msg = "cannot make listening socket transparent"; |
| 818 | err |= ERR_ALERT; |
| 819 | } |
| 820 | break; |
| 821 | case AF_INET6: |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 822 | if (1 |
| 823 | #if defined(IPV6_TRANSPARENT) |
| 824 | && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) |
| 825 | #endif |
Lukas Tribus | 7640e72 | 2014-03-03 21:10:51 +0100 | [diff] [blame] | 826 | #if defined(IP_FREEBIND) |
| 827 | && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1) |
| 828 | #endif |
Pieter Baauw | ff30b66 | 2013-05-08 23:22:39 +0200 | [diff] [blame] | 829 | #if defined(IPV6_BINDANY) |
| 830 | && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1) |
| 831 | #endif |
Pieter Baauw | 1eb7592 | 2013-05-08 23:30:23 +0200 | [diff] [blame] | 832 | #if defined(SO_BINDANY) |
| 833 | && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1) |
| 834 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 835 | ) { |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 836 | msg = "cannot make listening socket transparent"; |
| 837 | err |= ERR_ALERT; |
| 838 | } |
| 839 | break; |
| 840 | } |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 841 | } |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 842 | |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 843 | #ifdef SO_BINDTODEVICE |
| 844 | /* Note: this might fail if not CAP_NET_RAW */ |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 845 | if (!ext && listener->interface) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 846 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 847 | listener->interface, strlen(listener->interface) + 1) == -1) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 848 | msg = "cannot bind listener to device"; |
| 849 | err |= ERR_WARN; |
| 850 | } |
| 851 | } |
| 852 | #endif |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 853 | #if defined(TCP_MAXSEG) |
Willy Tarreau | 48a7e72 | 2010-12-24 15:26:39 +0100 | [diff] [blame] | 854 | if (listener->maxseg > 0) { |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 855 | if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 856 | &listener->maxseg, sizeof(listener->maxseg)) == -1) { |
| 857 | msg = "cannot set MSS"; |
| 858 | err |= ERR_WARN; |
| 859 | } |
| 860 | } |
| 861 | #endif |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 862 | #if defined(TCP_DEFER_ACCEPT) |
| 863 | if (listener->options & LI_O_DEF_ACCEPT) { |
| 864 | /* defer accept by up to one second */ |
| 865 | int accept_delay = 1; |
| 866 | if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) { |
| 867 | msg = "cannot enable DEFER_ACCEPT"; |
| 868 | err |= ERR_WARN; |
| 869 | } |
| 870 | } |
| 871 | #endif |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 872 | #if defined(TCP_FASTOPEN) |
| 873 | if (listener->options & LI_O_TCP_FO) { |
| 874 | /* TFO needs a queue length, let's use the configured backlog */ |
| 875 | int qlen = listener->backlog ? listener->backlog : listener->maxconn; |
| 876 | if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) { |
| 877 | msg = "cannot enable TCP_FASTOPEN"; |
| 878 | err |= ERR_WARN; |
| 879 | } |
| 880 | } |
| 881 | #endif |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 882 | #if defined(IPV6_V6ONLY) |
| 883 | if (listener->options & LI_O_V6ONLY) |
| 884 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 885 | else if (listener->options & LI_O_V4V6) |
| 886 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)); |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 887 | #endif |
| 888 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 889 | if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 890 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 891 | msg = "cannot bind socket"; |
| 892 | goto tcp_close_return; |
| 893 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 894 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 895 | ready = 0; |
| 896 | ready_len = sizeof(ready); |
| 897 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1) |
| 898 | ready = 0; |
| 899 | |
| 900 | if (!(ext && ready) && /* only listen if not already done by external process */ |
| 901 | listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 902 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 903 | msg = "cannot listen to socket"; |
| 904 | goto tcp_close_return; |
| 905 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 906 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 907 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 908 | if (listener->options & LI_O_NOQUICKACK) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 909 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero)); |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 910 | #endif |
| 911 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 912 | /* the socket is ready */ |
| 913 | listener->fd = fd; |
| 914 | listener->state = LI_LISTEN; |
| 915 | |
Willy Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 916 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 917 | fdtab[fd].iocb = listener->proto->accept; |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 918 | fd_insert(fd); |
| 919 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 920 | tcp_return: |
Cyril Bonté | 43ba1b3 | 2010-11-01 19:26:01 +0100 | [diff] [blame] | 921 | if (msg && errlen) { |
| 922 | char pn[INET6_ADDRSTRLEN]; |
| 923 | |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 924 | addr_to_str(&listener->addr, pn, sizeof(pn)); |
| 925 | 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] | 926 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 927 | return err; |
| 928 | |
| 929 | tcp_close_return: |
| 930 | close(fd); |
| 931 | goto tcp_return; |
| 932 | } |
| 933 | |
| 934 | /* This function creates all TCP sockets bound to the protocol entry <proto>. |
| 935 | * It is intended to be used as the protocol's bind_all() function. |
| 936 | * The sockets will be registered but not added to any fd_set, in order not to |
| 937 | * loose them across the fork(). A call to enable_all_listeners() is needed |
| 938 | * to complete initialization. The return value is composed from ERR_*. |
| 939 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 940 | static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 941 | { |
| 942 | struct listener *listener; |
| 943 | int err = ERR_NONE; |
| 944 | |
| 945 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 946 | err |= tcp_bind_listener(listener, errmsg, errlen); |
| 947 | if (err & ERR_ABORT) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 948 | break; |
| 949 | } |
| 950 | |
| 951 | return err; |
| 952 | } |
| 953 | |
| 954 | /* Add listener to the list of tcpv4 listeners. The listener's state |
| 955 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 956 | * listeners is updated. This is the function to use to add a new listener. |
| 957 | */ |
| 958 | void tcpv4_add_listener(struct listener *listener) |
| 959 | { |
| 960 | if (listener->state != LI_INIT) |
| 961 | return; |
| 962 | listener->state = LI_ASSIGNED; |
| 963 | listener->proto = &proto_tcpv4; |
| 964 | LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list); |
| 965 | proto_tcpv4.nb_listeners++; |
| 966 | } |
| 967 | |
| 968 | /* Add listener to the list of tcpv4 listeners. The listener's state |
| 969 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 970 | * listeners is updated. This is the function to use to add a new listener. |
| 971 | */ |
| 972 | void tcpv6_add_listener(struct listener *listener) |
| 973 | { |
| 974 | if (listener->state != LI_INIT) |
| 975 | return; |
| 976 | listener->state = LI_ASSIGNED; |
| 977 | listener->proto = &proto_tcpv6; |
| 978 | LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list); |
| 979 | proto_tcpv6.nb_listeners++; |
| 980 | } |
| 981 | |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 982 | /* Pause a listener. Returns < 0 in case of failure, 0 if the listener |
| 983 | * was totally stopped, or > 0 if correctly paused. |
| 984 | */ |
| 985 | int tcp_pause_listener(struct listener *l) |
| 986 | { |
| 987 | if (shutdown(l->fd, SHUT_WR) != 0) |
| 988 | return -1; /* Solaris dies here */ |
| 989 | |
| 990 | if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) |
| 991 | return -1; /* OpenBSD dies here */ |
| 992 | |
| 993 | if (shutdown(l->fd, SHUT_RD) != 0) |
| 994 | return -1; /* should always be OK */ |
| 995 | return 1; |
| 996 | } |
| 997 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 998 | /* This function performs the TCP request analysis on the current request. It |
| 999 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 1000 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 1001 | * request. It relies on buffers flags, and updates s->req->analysers. The |
| 1002 | * function may be called for frontend rules and backend rules. It only relies |
| 1003 | * on the backend pointer so this works for both cases. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1004 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 1005 | int tcp_inspect_request(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1006 | { |
| 1007 | struct tcp_rule *rule; |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 1008 | struct stksess *ts; |
| 1009 | struct stktable *t; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1010 | int partial; |
| 1011 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 1012 | 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] | 1013 | now_ms, __FUNCTION__, |
| 1014 | s, |
| 1015 | req, |
| 1016 | req->rex, req->wex, |
| 1017 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1018 | req->buf->i, |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1019 | req->analysers); |
| 1020 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1021 | /* We don't know whether we have enough data, so must proceed |
| 1022 | * this way : |
| 1023 | * - iterate through all rules in their declaration order |
| 1024 | * - if one rule returns MISS, it means the inspect delay is |
| 1025 | * not over yet, then return immediately, otherwise consider |
| 1026 | * it as a non-match. |
| 1027 | * - if one rule returns OK, then return OK |
| 1028 | * - if one rule returns KO, then return KO |
| 1029 | */ |
| 1030 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1031 | if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) || |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 1032 | !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms)) |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1033 | partial = SMP_OPT_FINAL; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1034 | else |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1035 | partial = 0; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1036 | |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 1037 | list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) { |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 1038 | enum acl_test_res ret = ACL_TEST_PASS; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1039 | |
| 1040 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1041 | ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial); |
Willy Tarreau | 18bf01e | 2014-06-13 16:18:52 +0200 | [diff] [blame] | 1042 | if (ret == ACL_TEST_MISS) |
| 1043 | goto missing_data; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1044 | |
| 1045 | ret = acl_pass(ret); |
| 1046 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1047 | ret = !ret; |
| 1048 | } |
| 1049 | |
| 1050 | if (ret) { |
| 1051 | /* we have a matching rule. */ |
| 1052 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1053 | channel_abort(req); |
| 1054 | channel_abort(s->rep); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1055 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1056 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 1057 | s->be->be_counters.denied_req++; |
| 1058 | s->fe->fe_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1059 | if (s->listener->counters) |
Willy Tarreau | 23968d8 | 2010-05-23 23:50:44 +0200 | [diff] [blame] | 1060 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1061 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1062 | if (!(s->flags & SN_ERR_MASK)) |
| 1063 | s->flags |= SN_ERR_PRXCOND; |
| 1064 | if (!(s->flags & SN_FINST_MASK)) |
| 1065 | s->flags |= SN_FINST_R; |
| 1066 | return 0; |
| 1067 | } |
Willy Tarreau | 44778ad | 2013-10-30 19:24:00 +0100 | [diff] [blame] | 1068 | else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) { |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1069 | /* Note: only the first valid tracking parameter of each |
| 1070 | * applies. |
| 1071 | */ |
| 1072 | struct stktable_key *key; |
Willy Tarreau | 1b71eb5 | 2014-06-25 17:01:56 +0200 | [diff] [blame] | 1073 | struct sample smp; |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1074 | |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 1075 | if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)])) |
Willy Tarreau | 44778ad | 2013-10-30 19:24:00 +0100 | [diff] [blame] | 1076 | continue; |
| 1077 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1078 | t = rule->act_prm.trk_ctr.table.t; |
Willy Tarreau | 1b71eb5 | 2014-06-25 17:01:56 +0200 | [diff] [blame] | 1079 | key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial, rule->act_prm.trk_ctr.expr, &smp); |
| 1080 | |
Willy Tarreau | 6bcb0a8 | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 1081 | if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL)) |
| 1082 | goto missing_data; /* key might appear later */ |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1083 | |
| 1084 | if (key && (ts = stktable_get_entry(t, key))) { |
Willy Tarreau | d5ca9ab | 2013-05-28 17:40:25 +0200 | [diff] [blame] | 1085 | session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts); |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 1086 | stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | d5ca9ab | 2013-05-28 17:40:25 +0200 | [diff] [blame] | 1087 | if (s->fe != s->be) |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 1088 | stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 1089 | } |
| 1090 | } |
Willy Tarreau | 18bf01e | 2014-06-13 16:18:52 +0200 | [diff] [blame] | 1091 | else if (rule->action == TCP_ACT_CAPTURE) { |
| 1092 | struct sample *key; |
| 1093 | struct cap_hdr *h = rule->act_prm.cap.hdr; |
| 1094 | char **cap = s->txn.req.cap; |
| 1095 | int len; |
| 1096 | |
| 1097 | key = sample_fetch_string(s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial, rule->act_prm.cap.expr); |
| 1098 | if (!key) |
| 1099 | continue; |
| 1100 | |
| 1101 | if (key->flags & SMP_F_MAY_CHANGE) |
| 1102 | goto missing_data; |
| 1103 | |
| 1104 | if (cap[h->index] == NULL) |
| 1105 | cap[h->index] = pool_alloc2(h->pool); |
| 1106 | |
| 1107 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 1108 | continue; |
| 1109 | |
| 1110 | len = key->data.str.len; |
| 1111 | if (len > h->len) |
| 1112 | len = h->len; |
| 1113 | |
| 1114 | memcpy(cap[h->index], key->data.str.str, len); |
| 1115 | cap[h->index][len] = 0; |
| 1116 | } |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 1117 | else { |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1118 | /* otherwise accept */ |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 1119 | break; |
| 1120 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | /* if we get there, it means we have no rule which matches, or |
| 1125 | * we have an explicit accept, so we apply the default accept. |
| 1126 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1127 | req->analysers &= ~an_bit; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1128 | req->analyse_exp = TICK_ETERNITY; |
| 1129 | return 1; |
Willy Tarreau | 18bf01e | 2014-06-13 16:18:52 +0200 | [diff] [blame] | 1130 | |
| 1131 | missing_data: |
| 1132 | channel_dont_connect(req); |
| 1133 | /* just set the request timeout once at the beginning of the request */ |
| 1134 | if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay) |
| 1135 | req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay); |
| 1136 | return 0; |
| 1137 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1140 | /* This function performs the TCP response analysis on the current response. It |
| 1141 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 1142 | * needs more data, encounters an error, or wants to immediately abort the |
| 1143 | * response. It relies on buffers flags, and updates s->rep->analysers. The |
| 1144 | * function may be called for backend rules. |
| 1145 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 1146 | int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit) |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1147 | { |
| 1148 | struct tcp_rule *rule; |
| 1149 | int partial; |
| 1150 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 1151 | 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] | 1152 | now_ms, __FUNCTION__, |
| 1153 | s, |
| 1154 | rep, |
| 1155 | rep->rex, rep->wex, |
| 1156 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1157 | rep->buf->i, |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1158 | rep->analysers); |
| 1159 | |
| 1160 | /* We don't know whether we have enough data, so must proceed |
| 1161 | * this way : |
| 1162 | * - iterate through all rules in their declaration order |
| 1163 | * - if one rule returns MISS, it means the inspect delay is |
| 1164 | * not over yet, then return immediately, otherwise consider |
| 1165 | * it as a non-match. |
| 1166 | * - if one rule returns OK, then return OK |
| 1167 | * - if one rule returns KO, then return KO |
| 1168 | */ |
| 1169 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1170 | if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms)) |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1171 | partial = SMP_OPT_FINAL; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1172 | else |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1173 | partial = 0; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1174 | |
| 1175 | list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) { |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 1176 | enum acl_test_res ret = ACL_TEST_PASS; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1177 | |
| 1178 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1179 | ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial); |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1180 | if (ret == ACL_TEST_MISS) { |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1181 | /* just set the analyser timeout once at the beginning of the response */ |
| 1182 | if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay) |
Willy Tarreau | 0bb166b | 2013-11-04 15:56:53 +0100 | [diff] [blame] | 1183 | rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | ret = acl_pass(ret); |
| 1188 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1189 | ret = !ret; |
| 1190 | } |
| 1191 | |
| 1192 | if (ret) { |
| 1193 | /* we have a matching rule. */ |
| 1194 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1195 | channel_abort(rep); |
| 1196 | channel_abort(s->req); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1197 | rep->analysers = 0; |
| 1198 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 1199 | s->be->be_counters.denied_resp++; |
| 1200 | s->fe->fe_counters.denied_resp++; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1201 | if (s->listener->counters) |
| 1202 | s->listener->counters->denied_resp++; |
| 1203 | |
| 1204 | if (!(s->flags & SN_ERR_MASK)) |
| 1205 | s->flags |= SN_ERR_PRXCOND; |
| 1206 | if (!(s->flags & SN_FINST_MASK)) |
| 1207 | s->flags |= SN_FINST_D; |
| 1208 | return 0; |
| 1209 | } |
Willy Tarreau | cc1e04b | 2013-09-11 23:20:29 +0200 | [diff] [blame] | 1210 | else if (rule->action == TCP_ACT_CLOSE) { |
| 1211 | rep->prod->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 1212 | si_shutr(rep->prod); |
| 1213 | si_shutw(rep->prod); |
| 1214 | break; |
| 1215 | } |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1216 | else { |
| 1217 | /* otherwise accept */ |
| 1218 | break; |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | /* if we get there, it means we have no rule which matches, or |
| 1224 | * we have an explicit accept, so we apply the default accept. |
| 1225 | */ |
| 1226 | rep->analysers &= ~an_bit; |
| 1227 | rep->analyse_exp = TICK_ETERNITY; |
| 1228 | return 1; |
| 1229 | } |
| 1230 | |
| 1231 | |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1232 | /* This function performs the TCP layer4 analysis on the current request. It |
| 1233 | * returns 0 if a reject rule matches, otherwise 1 if either an accept rule |
| 1234 | * matches or if no more rule matches. It can only use rules which don't need |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1235 | * any data. This only works on connection-based client-facing stream interfaces. |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1236 | */ |
| 1237 | int tcp_exec_req_rules(struct session *s) |
| 1238 | { |
| 1239 | struct tcp_rule *rule; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 1240 | struct stksess *ts; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1241 | struct stktable *t = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1242 | struct connection *conn = objt_conn(s->si[0].end); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1243 | int result = 1; |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 1244 | enum acl_test_res ret; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1245 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1246 | if (!conn) |
| 1247 | return result; |
| 1248 | |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1249 | list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1250 | ret = ACL_TEST_PASS; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1251 | |
| 1252 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1253 | ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1254 | ret = acl_pass(ret); |
| 1255 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1256 | ret = !ret; |
| 1257 | } |
| 1258 | |
| 1259 | if (ret) { |
| 1260 | /* we have a matching rule. */ |
| 1261 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 1262 | s->fe->fe_counters.denied_conn++; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1263 | if (s->listener->counters) |
Willy Tarreau | 2799e98 | 2010-06-05 15:43:21 +0200 | [diff] [blame] | 1264 | s->listener->counters->denied_conn++; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1265 | |
| 1266 | if (!(s->flags & SN_ERR_MASK)) |
| 1267 | s->flags |= SN_ERR_PRXCOND; |
| 1268 | if (!(s->flags & SN_FINST_MASK)) |
| 1269 | s->flags |= SN_FINST_R; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1270 | result = 0; |
| 1271 | break; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1272 | } |
Willy Tarreau | 44778ad | 2013-10-30 19:24:00 +0100 | [diff] [blame] | 1273 | else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) { |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1274 | /* Note: only the first valid tracking parameter of each |
| 1275 | * applies. |
| 1276 | */ |
| 1277 | struct stktable_key *key; |
| 1278 | |
Willy Tarreau | cc08d2c | 2014-01-28 23:18:23 +0100 | [diff] [blame] | 1279 | if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)])) |
Willy Tarreau | 44778ad | 2013-10-30 19:24:00 +0100 | [diff] [blame] | 1280 | continue; |
| 1281 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1282 | t = rule->act_prm.trk_ctr.table.t; |
Willy Tarreau | b5975de | 2014-06-25 16:20:53 +0200 | [diff] [blame] | 1283 | key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1284 | |
Willy Tarreau | d5ca9ab | 2013-05-28 17:40:25 +0200 | [diff] [blame] | 1285 | if (key && (ts = stktable_get_entry(t, key))) |
| 1286 | session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 1287 | } |
Willy Tarreau | 4f0d919 | 2013-06-11 20:40:55 +0200 | [diff] [blame] | 1288 | else if (rule->action == TCP_ACT_EXPECT_PX) { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1289 | conn->flags |= CO_FL_ACCEPT_PROXY; |
| 1290 | conn_sock_want_recv(conn); |
Willy Tarreau | 4f0d919 | 2013-06-11 20:40:55 +0200 | [diff] [blame] | 1291 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1292 | else { |
| 1293 | /* otherwise it's an accept */ |
| 1294 | break; |
| 1295 | } |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1296 | } |
| 1297 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1298 | return result; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1301 | /* Parse a tcp-response rule. Return a negative value in case of failure */ |
| 1302 | static int tcp_parse_response_rule(char **args, int arg, int section_type, |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1303 | struct proxy *curpx, struct proxy *defpx, |
| 1304 | struct tcp_rule *rule, char **err, |
| 1305 | unsigned int where, |
| 1306 | const char *file, int line) |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1307 | { |
| 1308 | if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1309 | memprintf(err, "%s %s is only allowed in 'backend' sections", |
| 1310 | args[0], args[1]); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1311 | return -1; |
| 1312 | } |
| 1313 | |
| 1314 | if (strcmp(args[arg], "accept") == 0) { |
| 1315 | arg++; |
| 1316 | rule->action = TCP_ACT_ACCEPT; |
| 1317 | } |
| 1318 | else if (strcmp(args[arg], "reject") == 0) { |
| 1319 | arg++; |
| 1320 | rule->action = TCP_ACT_REJECT; |
| 1321 | } |
Willy Tarreau | cc1e04b | 2013-09-11 23:20:29 +0200 | [diff] [blame] | 1322 | else if (strcmp(args[arg], "close") == 0) { |
| 1323 | arg++; |
| 1324 | rule->action = TCP_ACT_CLOSE; |
| 1325 | } |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1326 | else { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1327 | memprintf(err, |
Willy Tarreau | cc1e04b | 2013-09-11 23:20:29 +0200 | [diff] [blame] | 1328 | "'%s %s' expects 'accept', 'close' or 'reject' in %s '%s' (got '%s')", |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1329 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1330 | return -1; |
| 1331 | } |
| 1332 | |
| 1333 | if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) { |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1334 | if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1335 | memprintf(err, |
| 1336 | "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s", |
| 1337 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1338 | return -1; |
| 1339 | } |
| 1340 | } |
| 1341 | else if (*args[arg]) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1342 | memprintf(err, |
| 1343 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')", |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1344 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]); |
| 1345 | return -1; |
| 1346 | } |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1352 | /* Parse a tcp-request rule. Return a negative value in case of failure */ |
| 1353 | static int tcp_parse_request_rule(char **args, int arg, int section_type, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 1354 | struct proxy *curpx, struct proxy *defpx, |
| 1355 | struct tcp_rule *rule, char **err, |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1356 | unsigned int where, const char *file, int line) |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1357 | { |
| 1358 | if (curpx == defpx) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1359 | memprintf(err, "%s %s is not allowed in 'defaults' sections", |
| 1360 | args[0], args[1]); |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1361 | return -1; |
| 1362 | } |
| 1363 | |
| 1364 | if (!strcmp(args[arg], "accept")) { |
| 1365 | arg++; |
| 1366 | rule->action = TCP_ACT_ACCEPT; |
| 1367 | } |
| 1368 | else if (!strcmp(args[arg], "reject")) { |
| 1369 | arg++; |
| 1370 | rule->action = TCP_ACT_REJECT; |
| 1371 | } |
Willy Tarreau | 18bf01e | 2014-06-13 16:18:52 +0200 | [diff] [blame] | 1372 | else if (strcmp(args[arg], "capture") == 0) { |
| 1373 | struct sample_expr *expr; |
| 1374 | struct cap_hdr *hdr; |
| 1375 | int kw = arg; |
| 1376 | int len = 0; |
| 1377 | |
| 1378 | if (!(curpx->cap & PR_CAP_FE)) { |
| 1379 | memprintf(err, |
| 1380 | "'%s %s %s' : proxy '%s' has no frontend capability", |
| 1381 | args[0], args[1], args[kw], curpx->id); |
| 1382 | return -1; |
| 1383 | } |
| 1384 | |
| 1385 | if (!(where & SMP_VAL_FE_REQ_CNT)) { |
| 1386 | memprintf(err, |
| 1387 | "'%s %s' is not allowed in '%s %s' rules in %s '%s'", |
| 1388 | args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1389 | return -1; |
| 1390 | } |
| 1391 | |
| 1392 | arg++; |
| 1393 | |
| 1394 | curpx->conf.args.ctx = ARGC_CAP; |
| 1395 | expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args); |
| 1396 | if (!expr) { |
| 1397 | memprintf(err, |
| 1398 | "'%s %s %s' : %s", |
| 1399 | args[0], args[1], args[kw], *err); |
| 1400 | return -1; |
| 1401 | } |
| 1402 | |
| 1403 | if (!(expr->fetch->val & where)) { |
| 1404 | memprintf(err, |
| 1405 | "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here", |
| 1406 | args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use)); |
| 1407 | free(expr); |
| 1408 | return -1; |
| 1409 | } |
| 1410 | |
| 1411 | if (strcmp(args[arg], "len") == 0) { |
| 1412 | arg++; |
| 1413 | if (!args[arg]) { |
| 1414 | memprintf(err, |
| 1415 | "'%s %s %s' : missing length value", |
| 1416 | args[0], args[1], args[kw]); |
| 1417 | free(expr); |
| 1418 | return -1; |
| 1419 | } |
| 1420 | /* we copy the table name for now, it will be resolved later */ |
| 1421 | len = atoi(args[arg]); |
| 1422 | if (len <= 0) { |
| 1423 | memprintf(err, |
| 1424 | "'%s %s %s' : length must be > 0", |
| 1425 | args[0], args[1], args[kw]); |
| 1426 | free(expr); |
| 1427 | return -1; |
| 1428 | } |
| 1429 | arg++; |
| 1430 | } |
| 1431 | |
| 1432 | if (!len) { |
| 1433 | memprintf(err, |
| 1434 | "'%s %s %s' : a positive 'len' argument is mandatory", |
| 1435 | args[0], args[1], args[kw]); |
| 1436 | free(expr); |
| 1437 | return -1; |
| 1438 | } |
| 1439 | |
| 1440 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 1441 | hdr->next = curpx->req_cap; |
| 1442 | hdr->name = NULL; /* not a header capture */ |
| 1443 | hdr->namelen = 0; |
| 1444 | hdr->len = len; |
| 1445 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 1446 | hdr->index = curpx->nb_req_cap++; |
| 1447 | |
| 1448 | curpx->req_cap = hdr; |
| 1449 | curpx->to_log |= LW_REQHDR; |
| 1450 | |
| 1451 | /* check if we need to allocate an hdr_idx struct for HTTP parsing */ |
| 1452 | curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 1453 | |
| 1454 | rule->act_prm.cap.expr = expr; |
| 1455 | rule->act_prm.cap.hdr = hdr; |
| 1456 | rule->action = TCP_ACT_CAPTURE; |
| 1457 | } |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 1458 | else if (strncmp(args[arg], "track-sc", 8) == 0 && |
| 1459 | args[arg][9] == '\0' && args[arg][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 1460 | args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1461 | struct sample_expr *expr; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1462 | int kw = arg; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1463 | |
| 1464 | arg++; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1465 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1466 | curpx->conf.args.ctx = ARGC_TRK; |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 1467 | expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1468 | if (!expr) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1469 | memprintf(err, |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1470 | "'%s %s %s' : %s", |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 1471 | args[0], args[1], args[kw], *err); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1472 | return -1; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1473 | } |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1474 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 1475 | if (!(expr->fetch->val & where)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1476 | memprintf(err, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 1477 | "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here", |
Willy Tarreau | 33c60de | 2013-04-12 08:26:32 +0200 | [diff] [blame] | 1478 | args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use)); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1479 | free(expr); |
| 1480 | return -1; |
| 1481 | } |
| 1482 | |
| 1483 | /* check if we need to allocate an hdr_idx struct for HTTP parsing */ |
Willy Tarreau | 25320b2 | 2013-03-24 07:22:08 +0100 | [diff] [blame] | 1484 | curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1485 | |
| 1486 | if (strcmp(args[arg], "table") == 0) { |
Willy Tarreau | 598718a | 2012-12-09 16:57:27 +0100 | [diff] [blame] | 1487 | arg++; |
| 1488 | if (!args[arg]) { |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1489 | memprintf(err, |
| 1490 | "'%s %s %s' : missing table name", |
| 1491 | args[0], args[1], args[kw]); |
| 1492 | free(expr); |
| 1493 | return -1; |
| 1494 | } |
| 1495 | /* we copy the table name for now, it will be resolved later */ |
Willy Tarreau | 598718a | 2012-12-09 16:57:27 +0100 | [diff] [blame] | 1496 | rule->act_prm.trk_ctr.table.n = strdup(args[arg]); |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1497 | arg++; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1498 | } |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1499 | rule->act_prm.trk_ctr.expr = expr; |
Willy Tarreau | be4a3ef | 2013-06-17 15:04:07 +0200 | [diff] [blame] | 1500 | rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0'; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1501 | } |
Willy Tarreau | 4f0d919 | 2013-06-11 20:40:55 +0200 | [diff] [blame] | 1502 | else if (strcmp(args[arg], "expect-proxy") == 0) { |
| 1503 | if (strcmp(args[arg+1], "layer4") != 0) { |
| 1504 | memprintf(err, |
| 1505 | "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')", |
| 1506 | args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]); |
| 1507 | return -1; |
| 1508 | } |
| 1509 | |
| 1510 | if (!(where & SMP_VAL_FE_CON_ACC)) { |
| 1511 | memprintf(err, |
| 1512 | "'%s %s' is not allowed in '%s %s' rules in %s '%s'", |
| 1513 | args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1514 | return -1; |
| 1515 | } |
| 1516 | |
| 1517 | arg += 2; |
| 1518 | rule->action = TCP_ACT_EXPECT_PX; |
| 1519 | } |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1520 | else { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1521 | memprintf(err, |
Willy Tarreau | b4c8493 | 2013-07-23 19:15:30 +0200 | [diff] [blame] | 1522 | "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d' " |
| 1523 | " in %s '%s' (got '%s')", |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 1524 | args[0], args[1], MAX_SESS_STKCTR-1, proxy_type_str(curpx), curpx->id, args[arg]); |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1525 | return -1; |
| 1526 | } |
| 1527 | |
| 1528 | if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) { |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1529 | if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1530 | memprintf(err, |
| 1531 | "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s", |
| 1532 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err); |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1533 | return -1; |
| 1534 | } |
| 1535 | } |
| 1536 | else if (*args[arg]) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1537 | memprintf(err, |
| 1538 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')", |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1539 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]); |
| 1540 | return -1; |
| 1541 | } |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1545 | /* This function should be called to parse a line starting with the "tcp-response" |
| 1546 | * keyword. |
| 1547 | */ |
| 1548 | static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 28a47d6 | 2012-09-18 20:02:48 +0200 | [diff] [blame] | 1549 | struct proxy *defpx, const char *file, int line, |
| 1550 | char **err) |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1551 | { |
| 1552 | const char *ptr = NULL; |
| 1553 | unsigned int val; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1554 | int warn = 0; |
| 1555 | int arg; |
| 1556 | struct tcp_rule *rule; |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 1557 | unsigned int where; |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1558 | const struct acl *acl; |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1559 | const char *kw; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1560 | |
| 1561 | if (!*args[1]) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1562 | memprintf(err, "missing argument for '%s' in %s '%s'", |
| 1563 | args[0], proxy_type_str(curpx), curpx->id); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1564 | return -1; |
| 1565 | } |
| 1566 | |
| 1567 | if (strcmp(args[1], "inspect-delay") == 0) { |
| 1568 | if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1569 | memprintf(err, "%s %s is only allowed in 'backend' sections", |
| 1570 | args[0], args[1]); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1571 | return -1; |
| 1572 | } |
| 1573 | |
| 1574 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1575 | memprintf(err, |
| 1576 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
| 1577 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1578 | if (ptr) |
| 1579 | memprintf(err, "%s (unexpected character '%c')", *err, *ptr); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1580 | return -1; |
| 1581 | } |
| 1582 | |
| 1583 | if (curpx->tcp_rep.inspect_delay) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1584 | memprintf(err, "ignoring %s %s (was already defined) in %s '%s'", |
| 1585 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1586 | return 1; |
| 1587 | } |
| 1588 | curpx->tcp_rep.inspect_delay = val; |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1592 | rule = calloc(1, sizeof(*rule)); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1593 | LIST_INIT(&rule->list); |
| 1594 | arg = 1; |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 1595 | where = 0; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1596 | |
| 1597 | if (strcmp(args[1], "content") == 0) { |
| 1598 | arg++; |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 1599 | |
| 1600 | if (curpx->cap & PR_CAP_FE) |
| 1601 | where |= SMP_VAL_FE_RES_CNT; |
| 1602 | if (curpx->cap & PR_CAP_BE) |
| 1603 | where |= SMP_VAL_BE_RES_CNT; |
| 1604 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1605 | if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0) |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1606 | goto error; |
| 1607 | |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1608 | acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL; |
| 1609 | if (acl) { |
| 1610 | if (acl->name && *acl->name) |
| 1611 | memprintf(err, |
| 1612 | "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'", |
| 1613 | acl->name, args[0], args[1], sample_ckp_names(where)); |
| 1614 | else |
| 1615 | memprintf(err, |
| 1616 | "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'", |
| 1617 | args[0], args[1], |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1618 | LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1619 | sample_ckp_names(where)); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1620 | |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1621 | warn++; |
| 1622 | } |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1623 | else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) { |
| 1624 | if (acl->name && *acl->name) |
| 1625 | memprintf(err, |
| 1626 | "acl '%s' involves keyword '%s' which is incompatible with '%s'", |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1627 | acl->name, kw, sample_ckp_names(where)); |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1628 | else |
| 1629 | memprintf(err, |
| 1630 | "anonymous acl involves keyword '%s' which is incompatible with '%s'", |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1631 | kw, sample_ckp_names(where)); |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1632 | warn++; |
| 1633 | } |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1634 | |
| 1635 | LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list); |
| 1636 | } |
| 1637 | else { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1638 | memprintf(err, |
| 1639 | "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')", |
| 1640 | args[0], proxy_type_str(curpx), curpx->id, args[1]); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1641 | goto error; |
| 1642 | } |
| 1643 | |
| 1644 | return warn; |
| 1645 | error: |
| 1646 | free(rule); |
| 1647 | return -1; |
| 1648 | } |
| 1649 | |
| 1650 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1651 | /* This function should be called to parse a line starting with the "tcp-request" |
| 1652 | * keyword. |
| 1653 | */ |
| 1654 | static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 28a47d6 | 2012-09-18 20:02:48 +0200 | [diff] [blame] | 1655 | struct proxy *defpx, const char *file, int line, |
| 1656 | char **err) |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1657 | { |
| 1658 | const char *ptr = NULL; |
Willy Tarreau | c7e961e | 2008-08-17 17:13:47 +0200 | [diff] [blame] | 1659 | unsigned int val; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1660 | int warn = 0; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1661 | int arg; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1662 | struct tcp_rule *rule; |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 1663 | unsigned int where; |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1664 | const struct acl *acl; |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1665 | const char *kw; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1666 | |
| 1667 | if (!*args[1]) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1668 | if (curpx == defpx) |
| 1669 | memprintf(err, "missing argument for '%s' in defaults section", args[0]); |
| 1670 | else |
| 1671 | memprintf(err, "missing argument for '%s' in %s '%s'", |
| 1672 | args[0], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1673 | return -1; |
| 1674 | } |
| 1675 | |
| 1676 | if (!strcmp(args[1], "inspect-delay")) { |
| 1677 | if (curpx == defpx) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1678 | memprintf(err, "%s %s is not allowed in 'defaults' sections", |
| 1679 | args[0], args[1]); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1680 | return -1; |
| 1681 | } |
| 1682 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1683 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1684 | memprintf(err, |
| 1685 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
| 1686 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1687 | if (ptr) |
| 1688 | memprintf(err, "%s (unexpected character '%c')", *err, *ptr); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1689 | return -1; |
| 1690 | } |
| 1691 | |
| 1692 | if (curpx->tcp_req.inspect_delay) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1693 | memprintf(err, "ignoring %s %s (was already defined) in %s '%s'", |
| 1694 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1695 | return 1; |
| 1696 | } |
| 1697 | curpx->tcp_req.inspect_delay = val; |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1701 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 1702 | LIST_INIT(&rule->list); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1703 | arg = 1; |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 1704 | where = 0; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1705 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1706 | if (strcmp(args[1], "content") == 0) { |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 1707 | arg++; |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 1708 | |
| 1709 | if (curpx->cap & PR_CAP_FE) |
| 1710 | where |= SMP_VAL_FE_REQ_CNT; |
| 1711 | if (curpx->cap & PR_CAP_BE) |
| 1712 | where |= SMP_VAL_BE_REQ_CNT; |
| 1713 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1714 | if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0) |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1715 | goto error; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1716 | |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1717 | acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL; |
| 1718 | if (acl) { |
| 1719 | if (acl->name && *acl->name) |
| 1720 | memprintf(err, |
| 1721 | "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'", |
| 1722 | acl->name, args[0], args[1], sample_ckp_names(where)); |
| 1723 | else |
| 1724 | memprintf(err, |
| 1725 | "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'", |
| 1726 | args[0], args[1], |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1727 | LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1728 | sample_ckp_names(where)); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1729 | |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1730 | warn++; |
| 1731 | } |
| 1732 | else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) { |
| 1733 | if (acl->name && *acl->name) |
| 1734 | memprintf(err, |
| 1735 | "acl '%s' involves keyword '%s' which is incompatible with '%s'", |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1736 | acl->name, kw, sample_ckp_names(where)); |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1737 | else |
| 1738 | memprintf(err, |
| 1739 | "anonymous acl involves keyword '%s' which is incompatible with '%s'", |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1740 | kw, sample_ckp_names(where)); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1741 | warn++; |
| 1742 | } |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1743 | |
Willy Tarreau | 3986b9c | 2014-09-16 15:39:51 +0200 | [diff] [blame] | 1744 | /* the following function directly emits the warning */ |
| 1745 | warnif_misplaced_tcp_cont(curpx, file, line, args[0]); |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 1746 | LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1747 | } |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1748 | else if (strcmp(args[1], "connection") == 0) { |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1749 | arg++; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1750 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1751 | if (!(curpx->cap & PR_CAP_FE)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1752 | memprintf(err, "%s %s is not allowed because %s %s is not a frontend", |
| 1753 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Simon Horman | 6c54d8b | 2011-07-15 13:14:06 +0900 | [diff] [blame] | 1754 | goto error; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1755 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1756 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 1757 | where |= SMP_VAL_FE_CON_ACC; |
| 1758 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1759 | if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0) |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1760 | goto error; |
| 1761 | |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1762 | acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL; |
| 1763 | if (acl) { |
| 1764 | if (acl->name && *acl->name) |
| 1765 | memprintf(err, |
| 1766 | "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'", |
| 1767 | acl->name, args[0], args[1], sample_ckp_names(where)); |
| 1768 | else |
| 1769 | memprintf(err, |
| 1770 | "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'", |
| 1771 | args[0], args[1], |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1772 | LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1773 | sample_ckp_names(where)); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 1774 | |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1775 | warn++; |
| 1776 | } |
| 1777 | else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) { |
| 1778 | if (acl->name && *acl->name) |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1779 | memprintf(err, |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1780 | "acl '%s' involves keyword '%s' which is incompatible with '%s'", |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1781 | acl->name, kw, sample_ckp_names(where)); |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1782 | else |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1783 | memprintf(err, |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 1784 | "anonymous acl involves keyword '%s' which is incompatible with '%s'", |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 1785 | kw, sample_ckp_names(where)); |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1786 | warn++; |
| 1787 | } |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 1788 | |
Willy Tarreau | 3986b9c | 2014-09-16 15:39:51 +0200 | [diff] [blame] | 1789 | /* the following function directly emits the warning */ |
| 1790 | warnif_misplaced_tcp_conn(curpx, file, line, args[0]); |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 1791 | LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1792 | } |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1793 | else { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 1794 | if (curpx == defpx) |
| 1795 | memprintf(err, |
| 1796 | "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')", |
| 1797 | args[0], args[1]); |
| 1798 | else |
| 1799 | memprintf(err, |
| 1800 | "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')", |
Willy Tarreau | deaec2f | 2013-04-10 16:31:11 +0200 | [diff] [blame] | 1801 | args[0], proxy_type_str(curpx), curpx->id, args[1]); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1802 | goto error; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1803 | } |
| 1804 | |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1805 | return warn; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1806 | error: |
| 1807 | free(rule); |
| 1808 | return -1; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1809 | } |
| 1810 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1811 | |
| 1812 | /************************************************************************/ |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 1813 | /* All supported sample fetch functions must be declared here */ |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1814 | /************************************************************************/ |
| 1815 | |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1816 | /* fetch the connection's source IPv4/IPv6 address */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1817 | static int |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1818 | smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1819 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1820 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1821 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 1822 | |
| 1823 | if (!cli_conn) |
| 1824 | return 0; |
| 1825 | |
| 1826 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1827 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1828 | smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1829 | smp->type = SMP_T_IPV4; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1830 | break; |
| 1831 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1832 | smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1833 | smp->type = SMP_T_IPV6; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1834 | break; |
| 1835 | default: |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1836 | return 0; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1837 | } |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1838 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1839 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1840 | return 1; |
| 1841 | } |
| 1842 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1843 | /* set temp integer to the connection's source port */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1844 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 1845 | smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1846 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1847 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1848 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 1849 | |
| 1850 | if (!cli_conn) |
| 1851 | return 0; |
| 1852 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1853 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1854 | if (!(smp->data.uint = get_host_port(&cli_conn->addr.from))) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1855 | return 0; |
| 1856 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1857 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1858 | return 1; |
| 1859 | } |
| 1860 | |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1861 | /* fetch the connection's destination IPv4/IPv6 address */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1862 | static int |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1863 | smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1864 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1865 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1866 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1867 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1868 | if (!cli_conn) |
| 1869 | return 0; |
| 1870 | |
| 1871 | conn_get_to_addr(cli_conn); |
| 1872 | |
| 1873 | switch (cli_conn->addr.to.ss_family) { |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1874 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1875 | smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1876 | smp->type = SMP_T_IPV4; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1877 | break; |
| 1878 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1879 | smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1880 | smp->type = SMP_T_IPV6; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1881 | break; |
| 1882 | default: |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1883 | return 0; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1884 | } |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1885 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1886 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1887 | return 1; |
| 1888 | } |
| 1889 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1890 | /* set temp integer to the frontend connexion's destination port */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1891 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 1892 | smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1893 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1894 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1895 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 1896 | |
| 1897 | if (!cli_conn) |
| 1898 | return 0; |
| 1899 | |
| 1900 | conn_get_to_addr(cli_conn); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1901 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1902 | smp->type = SMP_T_UINT; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1903 | if (!(smp->data.uint = get_host_port(&cli_conn->addr.to))) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1904 | return 0; |
| 1905 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1906 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1907 | return 1; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1908 | } |
| 1909 | |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1910 | #ifdef IPV6_V6ONLY |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 1911 | /* parse the "v4v6" bind keyword */ |
| 1912 | static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1913 | { |
| 1914 | struct listener *l; |
| 1915 | |
| 1916 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1917 | if (l->addr.ss_family == AF_INET6) |
| 1918 | l->options |= LI_O_V4V6; |
| 1919 | } |
| 1920 | |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1924 | /* parse the "v6only" bind keyword */ |
| 1925 | static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1926 | { |
| 1927 | struct listener *l; |
| 1928 | |
| 1929 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1930 | if (l->addr.ss_family == AF_INET6) |
| 1931 | l->options |= LI_O_V6ONLY; |
| 1932 | } |
| 1933 | |
| 1934 | return 0; |
| 1935 | } |
| 1936 | #endif |
| 1937 | |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 1938 | #ifdef CONFIG_HAP_TRANSPARENT |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1939 | /* parse the "transparent" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1940 | static int bind_parse_transparent(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1941 | { |
| 1942 | struct listener *l; |
| 1943 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1944 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1945 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1946 | l->options |= LI_O_FOREIGN; |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1947 | } |
| 1948 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1949 | return 0; |
| 1950 | } |
| 1951 | #endif |
| 1952 | |
| 1953 | #ifdef TCP_DEFER_ACCEPT |
| 1954 | /* parse the "defer-accept" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1955 | static int bind_parse_defer_accept(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1956 | { |
| 1957 | struct listener *l; |
| 1958 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1959 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1960 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1961 | l->options |= LI_O_DEF_ACCEPT; |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1962 | } |
| 1963 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1964 | return 0; |
| 1965 | } |
| 1966 | #endif |
| 1967 | |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 1968 | #ifdef TCP_FASTOPEN |
Lukas Tribus | 0defb90 | 2013-02-13 23:35:39 +0100 | [diff] [blame] | 1969 | /* parse the "tfo" bind keyword */ |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 1970 | static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1971 | { |
| 1972 | struct listener *l; |
| 1973 | |
| 1974 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1975 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1976 | l->options |= LI_O_TCP_FO; |
| 1977 | } |
| 1978 | |
| 1979 | return 0; |
| 1980 | } |
| 1981 | #endif |
| 1982 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1983 | #ifdef TCP_MAXSEG |
| 1984 | /* parse the "mss" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1985 | static int bind_parse_mss(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1986 | { |
| 1987 | struct listener *l; |
| 1988 | int mss; |
| 1989 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1990 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1991 | memprintf(err, "'%s' : missing MSS value", args[cur_arg]); |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1992 | return ERR_ALERT | ERR_FATAL; |
| 1993 | } |
| 1994 | |
| 1995 | mss = atoi(args[cur_arg + 1]); |
| 1996 | if (!mss || abs(mss) > 65535) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1997 | memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]); |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1998 | return ERR_ALERT | ERR_FATAL; |
| 1999 | } |
| 2000 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2001 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 2002 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 2003 | l->maxseg = mss; |
| 2004 | } |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2005 | |
| 2006 | return 0; |
| 2007 | } |
| 2008 | #endif |
| 2009 | |
| 2010 | #ifdef SO_BINDTODEVICE |
| 2011 | /* parse the "mss" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2012 | static int bind_parse_interface(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2013 | { |
| 2014 | struct listener *l; |
| 2015 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2016 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2017 | memprintf(err, "'%s' : missing interface name", args[cur_arg]); |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2018 | return ERR_ALERT | ERR_FATAL; |
| 2019 | } |
| 2020 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2021 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 2022 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 2023 | l->interface = strdup(args[cur_arg + 1]); |
| 2024 | } |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2025 | |
| 2026 | global.last_checks |= LSTCHK_NETADM; |
| 2027 | return 0; |
| 2028 | } |
| 2029 | #endif |
| 2030 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 2031 | #ifdef CONFIG_HAP_NS |
| 2032 | /* parse the "namespace" bind keyword */ |
| 2033 | static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 2034 | { |
| 2035 | struct listener *l; |
| 2036 | char *namespace = NULL; |
| 2037 | |
| 2038 | if (!*args[cur_arg + 1]) { |
| 2039 | memprintf(err, "'%s' : missing namespace id", args[cur_arg]); |
| 2040 | return ERR_ALERT | ERR_FATAL; |
| 2041 | } |
| 2042 | namespace = args[cur_arg + 1]; |
| 2043 | |
| 2044 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 2045 | l->netns = netns_store_lookup(namespace, strlen(namespace)); |
| 2046 | |
| 2047 | if (l->netns == NULL) |
| 2048 | l->netns = netns_store_insert(namespace); |
| 2049 | |
| 2050 | if (l->netns == NULL) { |
| 2051 | Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]); |
| 2052 | return ERR_ALERT | ERR_FATAL; |
| 2053 | } |
| 2054 | } |
| 2055 | return 0; |
| 2056 | } |
| 2057 | #endif |
| 2058 | |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 2059 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 2060 | { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req }, |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 2061 | { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep }, |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2062 | { 0, NULL, NULL }, |
| 2063 | }}; |
| 2064 | |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 2065 | |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 2066 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2067 | * Please take care of keeping this list alphabetically sorted. |
| 2068 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 2069 | static struct acl_kw_list acl_kws = {ILH, { |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 2070 | { /* END */ }, |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2071 | }}; |
| 2072 | |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 2073 | |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 2074 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2075 | * Note: fetches that may return multiple types must be declared as the lowest |
| 2076 | * common denominator, the type that can be casted into all other ones. For |
| 2077 | * instance v4/v6 must be declared v4. |
| 2078 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 2079 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 2080 | { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI }, |
| 2081 | { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI }, |
| 2082 | { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI }, |
| 2083 | { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI }, |
| 2084 | { /* END */ }, |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 2085 | }}; |
| 2086 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2087 | /************************************************************************/ |
| 2088 | /* All supported bind keywords must be declared here. */ |
| 2089 | /************************************************************************/ |
| 2090 | |
| 2091 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2092 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 2093 | * all code contributors. |
| 2094 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 2095 | * the config parser can report an appropriate error when a known keyword was |
| 2096 | * not enabled. |
| 2097 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 2098 | static struct bind_kw_list bind_kws = { "TCP", { }, { |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2099 | #ifdef TCP_DEFER_ACCEPT |
| 2100 | { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */ |
| 2101 | #endif |
| 2102 | #ifdef SO_BINDTODEVICE |
| 2103 | { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */ |
| 2104 | #endif |
| 2105 | #ifdef TCP_MAXSEG |
| 2106 | { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */ |
| 2107 | #endif |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 2108 | #ifdef TCP_FASTOPEN |
| 2109 | { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */ |
| 2110 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 2111 | #ifdef CONFIG_HAP_TRANSPARENT |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2112 | { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */ |
| 2113 | #endif |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 2114 | #ifdef IPV6_V6ONLY |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 2115 | { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */ |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 2116 | { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */ |
| 2117 | #endif |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 2118 | #ifdef CONFIG_HAP_NS |
| 2119 | { "namespace", bind_parse_namespace, 1 }, |
| 2120 | #endif |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2121 | /* the versions with the NULL parse function*/ |
| 2122 | { "defer-accept", NULL, 0 }, |
| 2123 | { "interface", NULL, 1 }, |
| 2124 | { "mss", NULL, 1 }, |
| 2125 | { "transparent", NULL, 0 }, |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 2126 | { "v4v6", NULL, 0 }, |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 2127 | { "v6only", NULL, 0 }, |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2128 | { NULL, NULL, 0 }, |
| 2129 | }}; |
| 2130 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 2131 | __attribute__((constructor)) |
| 2132 | static void __tcp_protocol_init(void) |
| 2133 | { |
| 2134 | protocol_register(&proto_tcpv4); |
| 2135 | protocol_register(&proto_tcpv6); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2136 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2137 | cfg_register_keywords(&cfg_kws); |
| 2138 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 2139 | bind_register_keywords(&bind_kws); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | |
| 2143 | /* |
| 2144 | * Local variables: |
| 2145 | * c-indent-level: 8 |
| 2146 | * c-basic-offset: 8 |
| 2147 | * End: |
| 2148 | */ |