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