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