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