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