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