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