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