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