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