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