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