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