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