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> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 28 | #include <sys/types.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 29 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 30 | #include <netinet/tcp.h> |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 31 | #include <netinet/in.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 32 | |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 33 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 34 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 35 | #include <haproxy/arg.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 36 | #include <haproxy/channel.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 37 | #include <haproxy/connection.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 38 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 39 | #include <haproxy/fd.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 40 | #include <haproxy/global.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 41 | #include <haproxy/http_rules.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 42 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 43 | #include <haproxy/listener.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 44 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 45 | #include <haproxy/namespace.h> |
| 46 | #include <haproxy/port_range.h> |
| 47 | #include <haproxy/proto_tcp.h> |
| 48 | #include <haproxy/protocol.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 49 | #include <haproxy/proxy-t.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 50 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 51 | #include <haproxy/server.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 52 | #include <haproxy/stream-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 53 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 54 | #include <haproxy/tools.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 55 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 56 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 57 | static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen); |
| 58 | static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen); |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 59 | static void tcpv4_add_listener(struct listener *listener, int port); |
| 60 | static void tcpv6_add_listener(struct listener *listener, int port); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 61 | |
| 62 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 63 | static struct protocol proto_tcpv4 = { |
| 64 | .name = "tcpv4", |
| 65 | .sock_domain = AF_INET, |
| 66 | .sock_type = SOCK_STREAM, |
| 67 | .sock_prot = IPPROTO_TCP, |
| 68 | .sock_family = AF_INET, |
| 69 | .sock_addrlen = sizeof(struct sockaddr_in), |
| 70 | .l3_addrlen = 32/8, |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 71 | .accept = &listener_accept, |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 72 | .connect = tcp_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 73 | .bind = tcp_bind_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 74 | .bind_all = tcp_bind_listeners, |
| 75 | .unbind_all = unbind_all_listeners, |
| 76 | .enable_all = enable_all_listeners, |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 77 | .get_src = tcp_get_src, |
| 78 | .get_dst = tcp_get_dst, |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 79 | .pause = tcp_pause_listener, |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 80 | .add = tcpv4_add_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 81 | .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners), |
| 82 | .nb_listeners = 0, |
| 83 | }; |
| 84 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 85 | INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv4); |
| 86 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 87 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 88 | static struct protocol proto_tcpv6 = { |
| 89 | .name = "tcpv6", |
| 90 | .sock_domain = AF_INET6, |
| 91 | .sock_type = SOCK_STREAM, |
| 92 | .sock_prot = IPPROTO_TCP, |
| 93 | .sock_family = AF_INET6, |
| 94 | .sock_addrlen = sizeof(struct sockaddr_in6), |
| 95 | .l3_addrlen = 128/8, |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 96 | .accept = &listener_accept, |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 97 | .connect = tcp_connect_server, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 98 | .bind = tcp_bind_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 99 | .bind_all = tcp_bind_listeners, |
| 100 | .unbind_all = unbind_all_listeners, |
| 101 | .enable_all = enable_all_listeners, |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 102 | .get_src = tcp_get_src, |
| 103 | .get_dst = tcp_get_dst, |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 104 | .pause = tcp_pause_listener, |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 105 | .add = tcpv6_add_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 106 | .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners), |
| 107 | .nb_listeners = 0, |
| 108 | }; |
| 109 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 110 | INITCALL1(STG_REGISTER, protocol_register, &proto_tcpv6); |
| 111 | |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 112 | /* Default TCP parameters, got by opening a temporary TCP socket. */ |
| 113 | #ifdef TCP_MAXSEG |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 114 | static THREAD_LOCAL int default_tcp_maxseg = -1; |
| 115 | static THREAD_LOCAL int default_tcp6_maxseg = -1; |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 116 | #endif |
| 117 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 118 | /* 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] | 119 | * case we try to bind <remote>. <flags> is a 2-bit field consisting of : |
| 120 | * - 0 : ignore remote address (may even be a NULL pointer) |
| 121 | * - 1 : use provided address |
| 122 | * - 2 : use provided port |
| 123 | * - 3 : use both |
| 124 | * |
| 125 | * The function supports multiple foreign binding methods : |
| 126 | * - linux_tproxy: we directly bind to the foreign address |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 127 | * The second one can be used as a fallback for the first one. |
| 128 | * This function returns 0 when everything's OK, 1 if it could not bind, to the |
| 129 | * local address, 2 if it could not bind to the foreign address. |
| 130 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 131 | 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] | 132 | { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 133 | struct sockaddr_storage bind_addr; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 134 | int foreign_ok = 0; |
| 135 | int ret; |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 136 | static THREAD_LOCAL int ip_transp_working = 1; |
| 137 | static THREAD_LOCAL int ip6_transp_working = 1; |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 138 | |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 139 | switch (local->ss_family) { |
| 140 | case AF_INET: |
| 141 | if (flags && ip_transp_working) { |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 142 | /* This deserves some explanation. Some platforms will support |
| 143 | * multiple combinations of certain methods, so we try the |
| 144 | * supported ones until one succeeds. |
| 145 | */ |
| 146 | if (0 |
| 147 | #if defined(IP_TRANSPARENT) |
| 148 | || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0) |
| 149 | #endif |
| 150 | #if defined(IP_FREEBIND) |
| 151 | || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0) |
| 152 | #endif |
Pieter Baauw | ff30b66 | 2013-05-08 23:22:39 +0200 | [diff] [blame] | 153 | #if defined(IP_BINDANY) |
| 154 | || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0) |
| 155 | #endif |
Pieter Baauw | 1eb7592 | 2013-05-08 23:30:23 +0200 | [diff] [blame] | 156 | #if defined(SO_BINDANY) |
| 157 | || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0) |
| 158 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 159 | ) |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 160 | foreign_ok = 1; |
| 161 | else |
| 162 | ip_transp_working = 0; |
| 163 | } |
| 164 | break; |
| 165 | case AF_INET6: |
| 166 | if (flags && ip6_transp_working) { |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 167 | if (0 |
Dinko Korunic | 7276f3a | 2016-09-09 09:41:15 +0200 | [diff] [blame] | 168 | #if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6) |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 169 | || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0) |
| 170 | #endif |
Lukas Tribus | 7640e72 | 2014-03-03 21:10:51 +0100 | [diff] [blame] | 171 | #if defined(IP_FREEBIND) |
| 172 | || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0) |
| 173 | #endif |
Pieter Baauw | ff30b66 | 2013-05-08 23:22:39 +0200 | [diff] [blame] | 174 | #if defined(IPV6_BINDANY) |
| 175 | || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0) |
| 176 | #endif |
Pieter Baauw | 1eb7592 | 2013-05-08 23:30:23 +0200 | [diff] [blame] | 177 | #if defined(SO_BINDANY) |
| 178 | || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0) |
| 179 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 180 | ) |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 181 | foreign_ok = 1; |
| 182 | else |
| 183 | ip6_transp_working = 0; |
| 184 | } |
| 185 | break; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 186 | } |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 187 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 188 | if (flags) { |
| 189 | memset(&bind_addr, 0, sizeof(bind_addr)); |
Willy Tarreau | 96dd079 | 2011-04-19 07:20:57 +0200 | [diff] [blame] | 190 | bind_addr.ss_family = remote->ss_family; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 191 | switch (remote->ss_family) { |
| 192 | case AF_INET: |
| 193 | if (flags & 1) |
| 194 | ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr; |
| 195 | if (flags & 2) |
| 196 | ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port; |
| 197 | break; |
| 198 | case AF_INET6: |
| 199 | if (flags & 1) |
| 200 | ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr; |
| 201 | if (flags & 2) |
| 202 | ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port; |
| 203 | break; |
Willy Tarreau | 5dc1e98 | 2011-12-16 21:25:11 +0100 | [diff] [blame] | 204 | default: |
| 205 | /* we don't want to try to bind to an unknown address family */ |
| 206 | foreign_ok = 0; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 207 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 210 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 211 | if (foreign_ok) { |
Willy Tarreau | 9cf8d3f | 2014-05-09 22:56:10 +0200 | [diff] [blame] | 212 | if (is_inet_addr(&bind_addr)) { |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 213 | ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr)); |
| 214 | if (ret < 0) |
| 215 | return 2; |
| 216 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 217 | } |
| 218 | else { |
Willy Tarreau | 9cf8d3f | 2014-05-09 22:56:10 +0200 | [diff] [blame] | 219 | if (is_inet_addr(local)) { |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 220 | ret = bind(fd, (struct sockaddr *)local, get_addr_len(local)); |
| 221 | if (ret < 0) |
| 222 | return 1; |
| 223 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (!flags) |
| 227 | return 0; |
| 228 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 229 | if (!foreign_ok) |
| 230 | /* we could not bind to a foreign address */ |
| 231 | return 2; |
| 232 | |
| 233 | return 0; |
| 234 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 235 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 236 | /* conn->dst MUST be valid */ |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 237 | static int create_server_socket(struct connection *conn) |
| 238 | { |
Willy Tarreau | 529c139 | 2014-12-24 13:47:55 +0100 | [diff] [blame] | 239 | const struct netns_entry *ns = NULL; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 240 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 241 | #ifdef USE_NS |
Willy Tarreau | 529c139 | 2014-12-24 13:47:55 +0100 | [diff] [blame] | 242 | if (objt_server(conn->target)) { |
| 243 | if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP) |
| 244 | ns = conn->proxy_netns; |
| 245 | else |
| 246 | ns = __objt_server(conn->target)->netns; |
| 247 | } |
| 248 | #endif |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 249 | return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, IPPROTO_TCP); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 250 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 251 | |
| 252 | /* |
Willy Tarreau | 14f8e86 | 2012-08-30 22:23:13 +0200 | [diff] [blame] | 253 | * This function initiates a TCP connection establishment to the target assigned |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 254 | * to connection <conn> using (si->{target,dst}). A source address may be |
| 255 | * pointed to by conn->src in case of transparent proxying. Normal source |
Willy Tarreau | 14f8e86 | 2012-08-30 22:23:13 +0200 | [diff] [blame] | 256 | * bind addresses are still determined locally (due to the possible need of a |
| 257 | * 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] | 258 | * 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] | 259 | * supported. The <data> parameter is a boolean indicating whether there are data |
| 260 | * waiting for being sent or not, in order to adjust data write polling and on |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 261 | * some platforms, the ability to avoid an empty initial ACK. The <flags> argument |
| 262 | * allows the caller to force using a delayed ACK when establishing the connection |
Willy Tarreau | f0837b2 | 2012-11-24 10:24:27 +0100 | [diff] [blame] | 263 | * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 264 | * - CONNECT_DELACK_SMART_CONNECT = delayed ACK if backend has tcp-smart-connect, regardless of data |
| 265 | * - CONNECT_DELACK_ALWAYS = delayed ACK regardless of backend options |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 266 | * |
Willy Tarreau | 1ec74bf | 2013-10-24 21:45:00 +0200 | [diff] [blame] | 267 | * Note that a pending send_proxy message accounts for data. |
| 268 | * |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 269 | * It can return one of : |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 270 | * - SF_ERR_NONE if everything's OK |
| 271 | * - SF_ERR_SRVTO if there are no more servers |
| 272 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 273 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 274 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 275 | * - SF_ERR_INTERNAL for any other purely internal errors |
Tim Düsterhus | 4896c44 | 2016-11-29 02:15:19 +0100 | [diff] [blame] | 276 | * Additionally, 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] | 277 | * |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 278 | * 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] | 279 | * it's invalid and the caller has nothing to do. |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 280 | */ |
Willy Tarreau | f153686 | 2011-03-03 18:27:32 +0100 | [diff] [blame] | 281 | |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 282 | int tcp_connect_server(struct connection *conn, int flags) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 283 | { |
| 284 | int fd; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 285 | struct server *srv; |
| 286 | struct proxy *be; |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 287 | struct conn_src *src; |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 288 | int use_fastopen = 0; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 289 | struct sockaddr_storage *addr; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 290 | |
Olivier Houchard | 637b695 | 2018-11-23 14:23:07 +0100 | [diff] [blame] | 291 | conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */ |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 292 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 293 | switch (obj_type(conn->target)) { |
| 294 | case OBJ_TYPE_PROXY: |
| 295 | be = objt_proxy(conn->target); |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 296 | srv = NULL; |
| 297 | break; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 298 | case OBJ_TYPE_SERVER: |
| 299 | srv = objt_server(conn->target); |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 300 | be = srv->proxy; |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 301 | /* Make sure we check that we have data before activating |
| 302 | * TFO, or we could trigger a kernel issue whereby after |
| 303 | * a successful connect() == 0, any subsequent connect() |
| 304 | * will return EINPROGRESS instead of EISCONN. |
| 305 | */ |
| 306 | use_fastopen = (srv->flags & SRV_F_FASTOPEN) && |
| 307 | ((flags & (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)) == |
| 308 | (CONNECT_CAN_USE_TFO | CONNECT_HAS_DATA)); |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 309 | break; |
| 310 | default: |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 311 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 312 | return SF_ERR_INTERNAL; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 313 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 314 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 315 | if (!conn->dst) { |
| 316 | conn->flags |= CO_FL_ERROR; |
| 317 | return SF_ERR_INTERNAL; |
| 318 | } |
| 319 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 320 | fd = conn->handle.fd = create_server_socket(conn); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 321 | |
| 322 | if (fd == -1) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 323 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 324 | |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 325 | if (errno == ENFILE) { |
| 326 | conn->err_code = CO_ER_SYS_FDLIM; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 327 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 328 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 329 | be->id, global.maxsock); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 330 | } |
| 331 | else if (errno == EMFILE) { |
| 332 | conn->err_code = CO_ER_PROC_FDLIM; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 333 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 334 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 335 | be->id, global.maxsock); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 336 | } |
| 337 | else if (errno == ENOBUFS || errno == ENOMEM) { |
| 338 | conn->err_code = CO_ER_SYS_MEMLIM; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 339 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 340 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 341 | be->id, global.maxsock); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 342 | } |
| 343 | else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) { |
| 344 | conn->err_code = CO_ER_NOPROTO; |
| 345 | } |
| 346 | else |
| 347 | conn->err_code = CO_ER_SOCK_ERR; |
| 348 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 349 | /* this is a resource error */ |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 350 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 351 | return SF_ERR_RESOURCE; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | if (fd >= global.maxsock) { |
| 355 | /* do not log anything there, it's a normal condition when this option |
| 356 | * is used to serialize connections to a server ! |
| 357 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 358 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 359 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 360 | conn->err_code = CO_ER_CONF_FDLIM; |
| 361 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 362 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 366 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 367 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 368 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 369 | conn->err_code = CO_ER_SOCK_ERR; |
| 370 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 371 | return SF_ERR_INTERNAL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 372 | } |
| 373 | |
William Lallemand | c03eb01 | 2018-11-27 12:02:37 +0100 | [diff] [blame] | 374 | if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) { |
| 375 | ha_alert("Cannot set CLOEXEC on client socket.\n"); |
| 376 | close(fd); |
| 377 | conn->err_code = CO_ER_SOCK_ERR; |
| 378 | conn->flags |= CO_FL_ERROR; |
| 379 | return SF_ERR_INTERNAL; |
| 380 | } |
| 381 | |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 382 | if (be->options & PR_O_TCP_SRV_KA) { |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 383 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 384 | |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 385 | #ifdef TCP_KEEPCNT |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 386 | if (be->srvtcpka_cnt) |
| 387 | setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &be->srvtcpka_cnt, sizeof(be->srvtcpka_cnt)); |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 388 | #endif |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 389 | |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 390 | #ifdef TCP_KEEPIDLE |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 391 | if (be->srvtcpka_idle) |
| 392 | setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &be->srvtcpka_idle, sizeof(be->srvtcpka_idle)); |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 393 | #endif |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 394 | |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 395 | #ifdef TCP_KEEPINTVL |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 396 | if (be->srvtcpka_intvl) |
| 397 | setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &be->srvtcpka_intvl, sizeof(be->srvtcpka_intvl)); |
Willy Tarreau | 5254321 | 2020-07-09 05:58:51 +0200 | [diff] [blame] | 398 | #endif |
MIZUTA Takeshi | b24bc0d | 2020-07-09 11:13:20 +0900 | [diff] [blame] | 399 | } |
| 400 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 401 | /* allow specific binding : |
| 402 | * - server-specific at first |
| 403 | * - proxy-specific next |
| 404 | */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 405 | if (srv && srv->conn_src.opts & CO_SRC_BIND) |
| 406 | src = &srv->conn_src; |
| 407 | else if (be->conn_src.opts & CO_SRC_BIND) |
| 408 | src = &be->conn_src; |
| 409 | else |
| 410 | src = NULL; |
| 411 | |
| 412 | if (src) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 413 | int ret, flags = 0; |
| 414 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 415 | if (conn->src && is_inet_addr(conn->src)) { |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 416 | switch (src->opts & CO_SRC_TPROXY_MASK) { |
Willy Tarreau | ef9a360 | 2012-12-08 22:29:20 +0100 | [diff] [blame] | 417 | case CO_SRC_TPROXY_CLI: |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 418 | conn_set_private(conn); |
Willy Tarreau | 387ebf8 | 2015-08-04 19:24:13 +0200 | [diff] [blame] | 419 | /* fall through */ |
| 420 | case CO_SRC_TPROXY_ADDR: |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 421 | flags = 3; |
| 422 | break; |
Willy Tarreau | ef9a360 | 2012-12-08 22:29:20 +0100 | [diff] [blame] | 423 | case CO_SRC_TPROXY_CIP: |
| 424 | case CO_SRC_TPROXY_DYN: |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 425 | conn_set_private(conn); |
Willy Tarreau | 5f2877a | 2012-10-26 19:57:58 +0200 | [diff] [blame] | 426 | flags = 1; |
| 427 | break; |
| 428 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 429 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 430 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 431 | #ifdef SO_BINDTODEVICE |
| 432 | /* Note: this might fail if not CAP_NET_RAW */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 433 | if (src->iface_name) |
| 434 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 435 | #endif |
| 436 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 437 | if (src->sport_range) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 438 | int attempts = 10; /* should be more than enough to find a spare port */ |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 439 | struct sockaddr_storage sa; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 440 | |
| 441 | ret = 1; |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 442 | memcpy(&sa, &src->source_addr, sizeof(sa)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 443 | |
| 444 | do { |
| 445 | /* note: in case of retry, we may have to release a previously |
| 446 | * allocated port, hence this loop's construct. |
| 447 | */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 448 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 449 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 450 | |
| 451 | if (!attempts) |
| 452 | break; |
| 453 | attempts--; |
| 454 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 455 | fdinfo[fd].local_port = port_range_alloc_port(src->sport_range); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 456 | if (!fdinfo[fd].local_port) { |
| 457 | conn->err_code = CO_ER_PORT_RANGE; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 458 | break; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 459 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 460 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 461 | fdinfo[fd].port_range = src->sport_range; |
| 462 | set_host_port(&sa, fdinfo[fd].local_port); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 463 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 464 | ret = tcp_bind_socket(fd, flags, &sa, conn->src); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 465 | if (ret != 0) |
| 466 | conn->err_code = CO_ER_CANT_BIND; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 467 | } while (ret != 0); /* binding NOK */ |
| 468 | } |
| 469 | else { |
Lukas Tribus | 7d56c6d | 2016-09-13 09:51:15 +0000 | [diff] [blame] | 470 | #ifdef IP_BIND_ADDRESS_NO_PORT |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 471 | static THREAD_LOCAL int bind_address_no_port = 1; |
Lukas Tribus | 7d56c6d | 2016-09-13 09:51:15 +0000 | [diff] [blame] | 472 | setsockopt(fd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int)); |
| 473 | #endif |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 474 | ret = tcp_bind_socket(fd, flags, &src->source_addr, conn->src); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 475 | if (ret != 0) |
| 476 | conn->err_code = CO_ER_CANT_BIND; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 479 | if (unlikely(ret != 0)) { |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 480 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 481 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 482 | close(fd); |
| 483 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 484 | if (ret == 1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 485 | ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n", |
| 486 | be->id); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 487 | send_log(be, LOG_EMERG, |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 488 | "Cannot bind to source address before connect() for backend %s.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 489 | be->id); |
| 490 | } else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 491 | ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n", |
| 492 | be->id); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 493 | send_log(be, LOG_EMERG, |
Willy Tarreau | a4380b4 | 2012-12-08 22:49:11 +0100 | [diff] [blame] | 494 | "Cannot bind to tproxy source address before connect() for backend %s.\n", |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 495 | be->id); |
| 496 | } |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 497 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 498 | return SF_ERR_RESOURCE; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 502 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 503 | /* disabling tcp quick ack now allows the first request to leave the |
| 504 | * 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] | 505 | * data in the buffer. |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 506 | */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 507 | if (flags & (CONNECT_DELACK_ALWAYS) || |
| 508 | ((flags & CONNECT_DELACK_SMART_CONNECT || |
| 509 | (flags & CONNECT_HAS_DATA) || conn->send_proxy_ofs) && |
| 510 | (be->options2 & PR_O2_SMARTCON))) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 511 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 512 | #endif |
| 513 | |
Willy Tarreau | 163d462 | 2015-10-13 16:16:41 +0200 | [diff] [blame] | 514 | #ifdef TCP_USER_TIMEOUT |
| 515 | /* there is not much more we can do here when it fails, it's still minor */ |
| 516 | if (srv && srv->tcp_ut) |
| 517 | setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &srv->tcp_ut, sizeof(srv->tcp_ut)); |
| 518 | #endif |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 519 | |
| 520 | if (use_fastopen) { |
| 521 | #if defined(TCP_FASTOPEN_CONNECT) |
| 522 | setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one)); |
| 523 | #endif |
| 524 | } |
Willy Tarreau | e803de2 | 2010-01-21 17:43:04 +0100 | [diff] [blame] | 525 | if (global.tune.server_sndbuf) |
| 526 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 527 | |
| 528 | if (global.tune.server_rcvbuf) |
| 529 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 530 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 531 | addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 532 | if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) { |
Willy Tarreau | 819efbf | 2017-01-25 14:12:22 +0100 | [diff] [blame] | 533 | if (errno == EINPROGRESS || errno == EALREADY) { |
| 534 | /* common case, let's wait for connect status */ |
| 535 | conn->flags |= CO_FL_WAIT_L4_CONN; |
| 536 | } |
| 537 | else if (errno == EISCONN) { |
| 538 | /* should normally not happen but if so, indicates that it's OK */ |
| 539 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 540 | } |
| 541 | else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 542 | char *msg; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 543 | if (errno == EAGAIN || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 544 | msg = "no free ports"; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 545 | conn->err_code = CO_ER_FREE_PORTS; |
| 546 | } |
| 547 | else { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 548 | msg = "local address already in use"; |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 549 | conn->err_code = CO_ER_ADDR_INUSE; |
| 550 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 551 | |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 552 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 553 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 554 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 555 | close(fd); |
Willy Tarreau | b171951 | 2012-12-08 23:03:28 +0100 | [diff] [blame] | 556 | 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] | 557 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 558 | return SF_ERR_RESOURCE; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 559 | } else if (errno == ETIMEDOUT) { |
| 560 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 561 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 562 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 563 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 564 | conn->err_code = CO_ER_SOCK_ERR; |
| 565 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 566 | return SF_ERR_SRVTO; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 567 | } else { |
| 568 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 569 | //qfprintf(stderr,"Connect(): %d", errno); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 570 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 571 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 572 | close(fd); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 573 | conn->err_code = CO_ER_SOCK_ERR; |
| 574 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 575 | return SF_ERR_SRVCL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 576 | } |
| 577 | } |
Willy Tarreau | 819efbf | 2017-01-25 14:12:22 +0100 | [diff] [blame] | 578 | else { |
| 579 | /* connect() == 0, this is great! */ |
| 580 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 581 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 582 | |
Willy Tarreau | fc8f1f0 | 2012-12-08 18:53:44 +0100 | [diff] [blame] | 583 | conn->flags |= CO_FL_ADDR_TO_SET; |
Willy Tarreau | eeda90e | 2012-05-11 19:53:32 +0200 | [diff] [blame] | 584 | |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 585 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 586 | fdtab[fd].linger_risk = 1; /* close hard if needed */ |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 587 | |
Willy Tarreau | 4c69cff | 2020-03-04 16:38:00 +0100 | [diff] [blame] | 588 | if (conn->flags & CO_FL_WAIT_L4_CONN) { |
| 589 | fd_want_send(fd); |
| 590 | fd_cant_send(fd); |
Willy Tarreau | 8dbd1a2 | 2020-07-31 08:59:09 +0200 | [diff] [blame] | 591 | fd_cant_recv(fd); |
Willy Tarreau | 4c69cff | 2020-03-04 16:38:00 +0100 | [diff] [blame] | 592 | } |
Willy Tarreau | ccf3f6d | 2019-09-05 17:05:05 +0200 | [diff] [blame] | 593 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 594 | if (conn_xprt_init(conn) < 0) { |
Willy Tarreau | 3f2770b | 2017-10-05 18:01:29 +0200 | [diff] [blame] | 595 | conn_full_close(conn); |
Willy Tarreau | 9ce7013 | 2014-01-24 16:08:19 +0100 | [diff] [blame] | 596 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 597 | return SF_ERR_RESOURCE; |
Willy Tarreau | 184636e | 2012-09-06 14:04:41 +0200 | [diff] [blame] | 598 | } |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 599 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 600 | return SF_ERR_NONE; /* connection is OK */ |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 604 | /* |
| 605 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 606 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 607 | * success, -1 in case of error. The socket's source address is stored in |
| 608 | * <sa> for <salen> bytes. |
| 609 | */ |
| 610 | int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 611 | { |
| 612 | if (dir) |
| 613 | return getsockname(fd, sa, &salen); |
| 614 | else |
| 615 | return getpeername(fd, sa, &salen); |
| 616 | } |
| 617 | |
| 618 | |
| 619 | /* |
| 620 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 621 | * indicating if we're a listener (=0) or an initiator (!=0). In the case of a |
| 622 | * listener, if the original destination address was translated, the original |
| 623 | * address is retrieved. It returns 0 in case of success, -1 in case of error. |
| 624 | * The socket's source address is stored in <sa> for <salen> bytes. |
| 625 | */ |
| 626 | int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 627 | { |
| 628 | if (dir) |
| 629 | return getpeername(fd, sa, &salen); |
Willy Tarreau | 5e0d0e0 | 2014-10-29 21:46:01 +0100 | [diff] [blame] | 630 | else { |
| 631 | int ret = getsockname(fd, sa, &salen); |
| 632 | |
| 633 | if (ret < 0) |
| 634 | return ret; |
| 635 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 636 | #if defined(USE_TPROXY) && defined(SO_ORIGINAL_DST) |
Willy Tarreau | 5e0d0e0 | 2014-10-29 21:46:01 +0100 | [diff] [blame] | 637 | /* For TPROXY and Netfilter's NAT, we can retrieve the original |
| 638 | * IPv4 address before DNAT/REDIRECT. We must not do that with |
| 639 | * other families because v6-mapped IPv4 addresses are still |
| 640 | * reported as v4. |
| 641 | */ |
| 642 | if (((struct sockaddr_storage *)sa)->ss_family == AF_INET |
| 643 | && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0) |
| 644 | return 0; |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 645 | #endif |
Willy Tarreau | 5e0d0e0 | 2014-10-29 21:46:01 +0100 | [diff] [blame] | 646 | return ret; |
| 647 | } |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 650 | /* XXX: Should probably be elsewhere */ |
| 651 | static int compare_sockaddr(struct sockaddr_storage *a, struct sockaddr_storage *b) |
| 652 | { |
| 653 | if (a->ss_family != b->ss_family) { |
| 654 | return (-1); |
| 655 | } |
| 656 | switch (a->ss_family) { |
| 657 | case AF_INET: |
| 658 | { |
| 659 | struct sockaddr_in *a4 = (void *)a, *b4 = (void *)b; |
| 660 | if (a4->sin_port != b4->sin_port) |
| 661 | return (-1); |
| 662 | return (memcmp(&a4->sin_addr, &b4->sin_addr, |
| 663 | sizeof(a4->sin_addr))); |
| 664 | } |
| 665 | case AF_INET6: |
| 666 | { |
| 667 | struct sockaddr_in6 *a6 = (void *)a, *b6 = (void *)b; |
| 668 | if (a6->sin6_port != b6->sin6_port) |
| 669 | return (-1); |
| 670 | return (memcmp(&a6->sin6_addr, &b6->sin6_addr, |
| 671 | sizeof(a6->sin6_addr))); |
| 672 | } |
| 673 | default: |
| 674 | return (-1); |
| 675 | } |
| 676 | |
| 677 | } |
| 678 | |
| 679 | #define LI_MANDATORY_FLAGS (LI_O_FOREIGN | LI_O_V6ONLY | LI_O_V4V6) |
| 680 | /* When binding the listeners, check if a socket has been sent to us by the |
| 681 | * previous process that we could reuse, instead of creating a new one. |
| 682 | */ |
| 683 | static int tcp_find_compatible_fd(struct listener *l) |
| 684 | { |
| 685 | struct xfer_sock_list *xfer_sock = xfer_sock_list; |
| 686 | int ret = -1; |
| 687 | |
| 688 | while (xfer_sock) { |
| 689 | if (!compare_sockaddr(&xfer_sock->addr, &l->addr)) { |
| 690 | if ((l->interface == NULL && xfer_sock->iface == NULL) || |
| 691 | (l->interface != NULL && xfer_sock->iface != NULL && |
| 692 | !strcmp(l->interface, xfer_sock->iface))) { |
| 693 | if ((l->options & LI_MANDATORY_FLAGS) == |
| 694 | (xfer_sock->options & LI_MANDATORY_FLAGS)) { |
| 695 | if ((xfer_sock->namespace == NULL && |
| 696 | l->netns == NULL) |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 697 | #ifdef USE_NS |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 698 | || (xfer_sock->namespace != NULL && |
| 699 | l->netns != NULL && |
| 700 | !strcmp(xfer_sock->namespace, |
| 701 | l->netns->node.key)) |
| 702 | #endif |
| 703 | ) { |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | xfer_sock = xfer_sock->next; |
| 711 | } |
| 712 | if (xfer_sock != NULL) { |
| 713 | ret = xfer_sock->fd; |
| 714 | if (xfer_sock == xfer_sock_list) |
| 715 | xfer_sock_list = xfer_sock->next; |
| 716 | if (xfer_sock->prev) |
| 717 | xfer_sock->prev->next = xfer_sock->next; |
| 718 | if (xfer_sock->next) |
| 719 | xfer_sock->next->prev = xfer_sock->prev; |
| 720 | free(xfer_sock->iface); |
| 721 | free(xfer_sock->namespace); |
| 722 | free(xfer_sock); |
| 723 | } |
| 724 | return ret; |
| 725 | } |
| 726 | #undef L1_MANDATORY_FLAGS |
Willy Tarreau | 59b9479 | 2012-05-11 16:16:40 +0200 | [diff] [blame] | 727 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 728 | /* 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] | 729 | * an error message in <errmsg> if the message is at most <errlen> bytes long |
| 730 | * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero. |
| 731 | * The return value is composed from ERR_ABORT, ERR_WARN, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 732 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 733 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 734 | * 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] | 735 | * 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] | 736 | * the meaning of the error, but just indicate that a message is present which |
| 737 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 738 | * that it's pointless to try to start other listeners. No error message is |
| 739 | * returned if errlen is NULL. |
| 740 | */ |
| 741 | int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 742 | { |
| 743 | __label__ tcp_return, tcp_close_return; |
| 744 | int fd, err; |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 745 | int ext, ready; |
| 746 | socklen_t ready_len; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 747 | const char *msg = NULL; |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 748 | #ifdef TCP_MAXSEG |
| 749 | |
| 750 | /* Create a temporary TCP socket to get default parameters we can't |
| 751 | * guess. |
| 752 | * */ |
| 753 | ready_len = sizeof(default_tcp_maxseg); |
| 754 | if (default_tcp_maxseg == -1) { |
| 755 | default_tcp_maxseg = -2; |
| 756 | fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 757 | if (fd < 0) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 758 | ha_warning("Failed to create a temporary socket!\n"); |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 759 | else { |
| 760 | if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp_maxseg, |
| 761 | &ready_len) == -1) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 762 | ha_warning("Failed to get the default value of TCP_MAXSEG\n"); |
William Dauchy | c0e23ae | 2020-02-12 10:09:14 +0100 | [diff] [blame] | 763 | close(fd); |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 764 | } |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 765 | } |
| 766 | if (default_tcp6_maxseg == -1) { |
| 767 | default_tcp6_maxseg = -2; |
| 768 | fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); |
| 769 | if (fd >= 0) { |
| 770 | if (getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &default_tcp6_maxseg, |
| 771 | &ready_len) == -1) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 772 | ha_warning("Failed ot get the default value of TCP_MAXSEG for IPv6\n"); |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 773 | close(fd); |
| 774 | } |
| 775 | } |
| 776 | #endif |
| 777 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 778 | |
| 779 | /* ensure we never return garbage */ |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 780 | if (errlen) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 781 | *errmsg = 0; |
| 782 | |
| 783 | if (listener->state != LI_ASSIGNED) |
| 784 | return ERR_NONE; /* already bound */ |
| 785 | |
| 786 | err = ERR_NONE; |
| 787 | |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 788 | if (listener->fd == -1) |
| 789 | listener->fd = tcp_find_compatible_fd(listener); |
| 790 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 791 | /* if the listener already has an fd assigned, then we were offered the |
| 792 | * fd by an external process (most likely the parent), and we don't want |
| 793 | * to create a new socket. However we still want to set a few flags on |
| 794 | * the socket. |
| 795 | */ |
| 796 | fd = listener->fd; |
| 797 | ext = (fd >= 0); |
| 798 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 799 | if (!ext) { |
| 800 | fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP); |
| 801 | |
| 802 | if (fd == -1) { |
| 803 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 804 | msg = "cannot create listening socket"; |
| 805 | goto tcp_return; |
| 806 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 807 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 808 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 809 | if (fd >= global.maxsock) { |
| 810 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 811 | msg = "not enough free sockets (raise '-n' parameter)"; |
| 812 | goto tcp_close_return; |
| 813 | } |
| 814 | |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 815 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 816 | err |= ERR_FATAL | ERR_ALERT; |
| 817 | msg = "cannot make socket non-blocking"; |
| 818 | goto tcp_close_return; |
| 819 | } |
| 820 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 821 | if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 822 | /* not fatal but should be reported */ |
| 823 | msg = "cannot do so_reuseaddr"; |
| 824 | err |= ERR_ALERT; |
| 825 | } |
| 826 | |
| 827 | if (listener->options & LI_O_NOLINGER) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 828 | setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger)); |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 829 | else { |
| 830 | struct linger tmplinger; |
| 831 | socklen_t len = sizeof(tmplinger); |
| 832 | if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, &len) == 0 && |
| 833 | (tmplinger.l_onoff == 1 || tmplinger.l_linger == 0)) { |
| 834 | tmplinger.l_onoff = 0; |
| 835 | tmplinger.l_linger = 0; |
| 836 | setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmplinger, |
| 837 | sizeof(tmplinger)); |
| 838 | } |
| 839 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 840 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 841 | #ifdef SO_REUSEPORT |
Lukas Tribus | a0bcbdc | 2016-09-12 21:42:20 +0000 | [diff] [blame] | 842 | /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of |
| 843 | * Linux, it might return an error that we will silently ignore. |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 844 | */ |
Lukas Tribus | a0bcbdc | 2016-09-12 21:42:20 +0000 | [diff] [blame] | 845 | if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT)) |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 846 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 847 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 848 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 849 | if (!ext && (listener->options & LI_O_FOREIGN)) { |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 850 | switch (listener->addr.ss_family) { |
| 851 | case AF_INET: |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 852 | if (1 |
| 853 | #if defined(IP_TRANSPARENT) |
| 854 | && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1) |
| 855 | #endif |
| 856 | #if defined(IP_FREEBIND) |
| 857 | && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1) |
| 858 | #endif |
Pieter Baauw | ff30b66 | 2013-05-08 23:22:39 +0200 | [diff] [blame] | 859 | #if defined(IP_BINDANY) |
| 860 | && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1) |
| 861 | #endif |
Pieter Baauw | 1eb7592 | 2013-05-08 23:30:23 +0200 | [diff] [blame] | 862 | #if defined(SO_BINDANY) |
| 863 | && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1) |
| 864 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 865 | ) { |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 866 | msg = "cannot make listening socket transparent"; |
| 867 | err |= ERR_ALERT; |
| 868 | } |
| 869 | break; |
| 870 | case AF_INET6: |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 871 | if (1 |
Dinko Korunic | 7276f3a | 2016-09-09 09:41:15 +0200 | [diff] [blame] | 872 | #if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6) |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 873 | && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) |
| 874 | #endif |
Lukas Tribus | 7640e72 | 2014-03-03 21:10:51 +0100 | [diff] [blame] | 875 | #if defined(IP_FREEBIND) |
| 876 | && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1) |
| 877 | #endif |
Pieter Baauw | ff30b66 | 2013-05-08 23:22:39 +0200 | [diff] [blame] | 878 | #if defined(IPV6_BINDANY) |
| 879 | && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1) |
| 880 | #endif |
Pieter Baauw | 1eb7592 | 2013-05-08 23:30:23 +0200 | [diff] [blame] | 881 | #if defined(SO_BINDANY) |
| 882 | && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1) |
| 883 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 884 | ) { |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 885 | msg = "cannot make listening socket transparent"; |
| 886 | err |= ERR_ALERT; |
| 887 | } |
| 888 | break; |
| 889 | } |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 890 | } |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 891 | |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 892 | #ifdef SO_BINDTODEVICE |
| 893 | /* Note: this might fail if not CAP_NET_RAW */ |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 894 | if (!ext && listener->interface) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 895 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 896 | listener->interface, strlen(listener->interface) + 1) == -1) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 897 | msg = "cannot bind listener to device"; |
| 898 | err |= ERR_WARN; |
| 899 | } |
| 900 | } |
| 901 | #endif |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 902 | #if defined(TCP_MAXSEG) |
Willy Tarreau | 48a7e72 | 2010-12-24 15:26:39 +0100 | [diff] [blame] | 903 | if (listener->maxseg > 0) { |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 904 | if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 905 | &listener->maxseg, sizeof(listener->maxseg)) == -1) { |
| 906 | msg = "cannot set MSS"; |
| 907 | err |= ERR_WARN; |
| 908 | } |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 909 | } else if (ext) { |
| 910 | int tmpmaxseg = -1; |
| 911 | int defaultmss; |
| 912 | socklen_t len = sizeof(tmpmaxseg); |
| 913 | |
| 914 | if (listener->addr.ss_family == AF_INET) |
| 915 | defaultmss = default_tcp_maxseg; |
| 916 | else |
| 917 | defaultmss = default_tcp6_maxseg; |
| 918 | |
| 919 | getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &tmpmaxseg, &len); |
William Dauchy | 97a7bda | 2020-02-12 15:53:04 +0100 | [diff] [blame] | 920 | if (defaultmss > 0 && |
| 921 | tmpmaxseg != defaultmss && |
| 922 | setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &defaultmss, sizeof(defaultmss)) == -1) { |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 923 | msg = "cannot set MSS"; |
| 924 | err |= ERR_WARN; |
| 925 | } |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 926 | } |
| 927 | #endif |
Willy Tarreau | 2af207a | 2015-02-04 00:45:58 +0100 | [diff] [blame] | 928 | #if defined(TCP_USER_TIMEOUT) |
| 929 | if (listener->tcp_ut) { |
| 930 | if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, |
| 931 | &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) { |
| 932 | msg = "cannot set TCP User Timeout"; |
| 933 | err |= ERR_WARN; |
| 934 | } |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 935 | } else |
| 936 | setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &zero, |
| 937 | sizeof(zero)); |
Willy Tarreau | 2af207a | 2015-02-04 00:45:58 +0100 | [diff] [blame] | 938 | #endif |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 939 | #if defined(TCP_DEFER_ACCEPT) |
| 940 | if (listener->options & LI_O_DEF_ACCEPT) { |
| 941 | /* defer accept by up to one second */ |
| 942 | int accept_delay = 1; |
| 943 | if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) { |
| 944 | msg = "cannot enable DEFER_ACCEPT"; |
| 945 | err |= ERR_WARN; |
| 946 | } |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 947 | } else |
| 948 | setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &zero, |
| 949 | sizeof(zero)); |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 950 | #endif |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 951 | #if defined(TCP_FASTOPEN) |
| 952 | if (listener->options & LI_O_TCP_FO) { |
| 953 | /* TFO needs a queue length, let's use the configured backlog */ |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 954 | int qlen = listener_backlog(listener); |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 955 | if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) { |
| 956 | msg = "cannot enable TCP_FASTOPEN"; |
| 957 | err |= ERR_WARN; |
| 958 | } |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 959 | } else { |
| 960 | socklen_t len; |
| 961 | int qlen; |
| 962 | len = sizeof(qlen); |
| 963 | /* Only disable fast open if it was enabled, we don't want |
| 964 | * the kernel to create a fast open queue if there's none. |
| 965 | */ |
| 966 | if (getsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, &len) == 0 && |
| 967 | qlen != 0) { |
| 968 | if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &zero, |
| 969 | sizeof(zero)) == -1) { |
| 970 | msg = "cannot disable TCP_FASTOPEN"; |
| 971 | err |= ERR_WARN; |
| 972 | } |
| 973 | } |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 974 | } |
| 975 | #endif |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 976 | #if defined(IPV6_V6ONLY) |
| 977 | if (listener->options & LI_O_V6ONLY) |
| 978 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 979 | else if (listener->options & LI_O_V4V6) |
| 980 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)); |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 981 | #endif |
| 982 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 983 | if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 984 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 985 | msg = "cannot bind socket"; |
| 986 | goto tcp_close_return; |
| 987 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 988 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 989 | ready = 0; |
| 990 | ready_len = sizeof(ready); |
| 991 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1) |
| 992 | ready = 0; |
| 993 | |
| 994 | if (!(ext && ready) && /* only listen if not already done by external process */ |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 995 | listen(fd, listener_backlog(listener)) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 996 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 997 | msg = "cannot listen to socket"; |
| 998 | goto tcp_close_return; |
| 999 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1000 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 1001 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 1002 | if (listener->options & LI_O_NOQUICKACK) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1003 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero)); |
Olivier Houchard | 153659f | 2017-04-05 22:39:56 +0200 | [diff] [blame] | 1004 | else |
| 1005 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 1006 | #endif |
| 1007 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1008 | /* the socket is ready */ |
| 1009 | listener->fd = fd; |
| 1010 | listener->state = LI_LISTEN; |
| 1011 | |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 1012 | fd_insert(fd, listener, listener->proto->accept, |
Willy Tarreau | 0948a78 | 2020-02-12 10:15:34 +0100 | [diff] [blame] | 1013 | thread_mask(listener->bind_conf->bind_thread) & all_threads_mask); |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 1014 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1015 | tcp_return: |
Cyril Bonté | 43ba1b3 | 2010-11-01 19:26:01 +0100 | [diff] [blame] | 1016 | if (msg && errlen) { |
| 1017 | char pn[INET6_ADDRSTRLEN]; |
| 1018 | |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1019 | addr_to_str(&listener->addr, pn, sizeof(pn)); |
| 1020 | 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] | 1021 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1022 | return err; |
| 1023 | |
| 1024 | tcp_close_return: |
| 1025 | close(fd); |
| 1026 | goto tcp_return; |
| 1027 | } |
| 1028 | |
| 1029 | /* This function creates all TCP sockets bound to the protocol entry <proto>. |
| 1030 | * It is intended to be used as the protocol's bind_all() function. |
| 1031 | * The sockets will be registered but not added to any fd_set, in order not to |
| 1032 | * loose them across the fork(). A call to enable_all_listeners() is needed |
| 1033 | * to complete initialization. The return value is composed from ERR_*. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 1034 | * |
| 1035 | * Must be called with proto_lock held. |
| 1036 | * |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1037 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 1038 | static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1039 | { |
| 1040 | struct listener *listener; |
| 1041 | int err = ERR_NONE; |
| 1042 | |
| 1043 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 1044 | err |= tcp_bind_listener(listener, errmsg, errlen); |
| 1045 | if (err & ERR_ABORT) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | return err; |
| 1050 | } |
| 1051 | |
Willy Tarreau | 3228238 | 2017-09-15 07:44:44 +0200 | [diff] [blame] | 1052 | /* Add <listener> to the list of tcpv4 listeners, on port <port>. The |
| 1053 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 1054 | * The number of listeners for the protocol is updated. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 1055 | * |
| 1056 | * Must be called with proto_lock held. |
| 1057 | * |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1058 | */ |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 1059 | static void tcpv4_add_listener(struct listener *listener, int port) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1060 | { |
| 1061 | if (listener->state != LI_INIT) |
| 1062 | return; |
| 1063 | listener->state = LI_ASSIGNED; |
| 1064 | listener->proto = &proto_tcpv4; |
Willy Tarreau | 3228238 | 2017-09-15 07:44:44 +0200 | [diff] [blame] | 1065 | ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1066 | LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list); |
| 1067 | proto_tcpv4.nb_listeners++; |
| 1068 | } |
| 1069 | |
Willy Tarreau | 3228238 | 2017-09-15 07:44:44 +0200 | [diff] [blame] | 1070 | /* Add <listener> to the list of tcpv6 listeners, on port <port>. The |
| 1071 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 1072 | * The number of listeners for the protocol is updated. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 1073 | * |
| 1074 | * Must be called with proto_lock held. |
| 1075 | * |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1076 | */ |
Willy Tarreau | 9d5be5c | 2017-09-15 07:55:51 +0200 | [diff] [blame] | 1077 | static void tcpv6_add_listener(struct listener *listener, int port) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1078 | { |
| 1079 | if (listener->state != LI_INIT) |
| 1080 | return; |
| 1081 | listener->state = LI_ASSIGNED; |
| 1082 | listener->proto = &proto_tcpv6; |
Willy Tarreau | 3228238 | 2017-09-15 07:44:44 +0200 | [diff] [blame] | 1083 | ((struct sockaddr_in *)(&listener->addr))->sin_port = htons(port); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1084 | LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list); |
| 1085 | proto_tcpv6.nb_listeners++; |
| 1086 | } |
| 1087 | |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 1088 | /* Pause a listener. Returns < 0 in case of failure, 0 if the listener |
| 1089 | * was totally stopped, or > 0 if correctly paused. |
| 1090 | */ |
| 1091 | int tcp_pause_listener(struct listener *l) |
| 1092 | { |
| 1093 | if (shutdown(l->fd, SHUT_WR) != 0) |
| 1094 | return -1; /* Solaris dies here */ |
| 1095 | |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 1096 | if (listen(l->fd, listener_backlog(l)) != 0) |
Willy Tarreau | 092d865 | 2014-07-07 20:22:12 +0200 | [diff] [blame] | 1097 | return -1; /* OpenBSD dies here */ |
| 1098 | |
| 1099 | if (shutdown(l->fd, SHUT_RD) != 0) |
| 1100 | return -1; /* should always be OK */ |
| 1101 | return 1; |
| 1102 | } |
| 1103 | |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 1104 | /* |
Willy Tarreau | 00005ce | 2016-10-21 15:07:45 +0200 | [diff] [blame] | 1105 | * Execute the "set-src" action. May be called from {tcp,http}request. |
| 1106 | * It only changes the address and tries to preserve the original port. If the |
| 1107 | * 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] | 1108 | */ |
| 1109 | enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px, |
| 1110 | struct session *sess, struct stream *s, int flags) |
| 1111 | { |
| 1112 | struct connection *cli_conn; |
| 1113 | |
Willy Tarreau | dddd2b4 | 2019-07-17 11:37:29 +0200 | [diff] [blame] | 1114 | if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) { |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 1115 | struct sample *smp; |
| 1116 | |
| 1117 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR); |
| 1118 | if (smp) { |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1119 | int port = get_net_port(cli_conn->src); |
Willy Tarreau | 00005ce | 2016-10-21 15:07:45 +0200 | [diff] [blame] | 1120 | |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 1121 | if (smp->data.type == SMP_T_IPV4) { |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1122 | ((struct sockaddr_in *)cli_conn->src)->sin_family = AF_INET; |
| 1123 | ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = smp->data.u.ipv4.s_addr; |
| 1124 | ((struct sockaddr_in *)cli_conn->src)->sin_port = port; |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 1125 | } else if (smp->data.type == SMP_T_IPV6) { |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1126 | ((struct sockaddr_in6 *)cli_conn->src)->sin6_family = AF_INET6; |
| 1127 | memcpy(&((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr)); |
| 1128 | ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = port; |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 1129 | } |
| 1130 | } |
William Lallemand | 01252ed | 2016-05-25 02:33:16 +0200 | [diff] [blame] | 1131 | cli_conn->flags |= CO_FL_ADDR_FROM_SET; |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 1132 | } |
| 1133 | return ACT_RET_CONT; |
| 1134 | } |
| 1135 | |
William Lallemand | 44be640 | 2016-05-25 01:51:35 +0200 | [diff] [blame] | 1136 | /* |
Willy Tarreau | 00005ce | 2016-10-21 15:07:45 +0200 | [diff] [blame] | 1137 | * Execute the "set-dst" action. May be called from {tcp,http}request. |
| 1138 | * It only changes the address and tries to preserve the original port. If the |
| 1139 | * 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] | 1140 | */ |
| 1141 | enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px, |
| 1142 | struct session *sess, struct stream *s, int flags) |
| 1143 | { |
| 1144 | struct connection *cli_conn; |
| 1145 | |
Willy Tarreau | dddd2b4 | 2019-07-17 11:37:29 +0200 | [diff] [blame] | 1146 | if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) { |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1147 | struct sample *smp; |
| 1148 | |
| 1149 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR); |
| 1150 | if (smp) { |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1151 | int port = get_net_port(cli_conn->dst); |
Willy Tarreau | 00005ce | 2016-10-21 15:07:45 +0200 | [diff] [blame] | 1152 | |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1153 | if (smp->data.type == SMP_T_IPV4) { |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1154 | ((struct sockaddr_in *)cli_conn->dst)->sin_family = AF_INET; |
| 1155 | ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = smp->data.u.ipv4.s_addr; |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1156 | } else if (smp->data.type == SMP_T_IPV6) { |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1157 | ((struct sockaddr_in6 *)cli_conn->dst)->sin6_family = AF_INET6; |
| 1158 | memcpy(&((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr)); |
| 1159 | ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = port; |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1160 | } |
| 1161 | cli_conn->flags |= CO_FL_ADDR_TO_SET; |
| 1162 | } |
| 1163 | } |
| 1164 | return ACT_RET_CONT; |
| 1165 | } |
| 1166 | |
| 1167 | /* |
Willy Tarreau | 00005ce | 2016-10-21 15:07:45 +0200 | [diff] [blame] | 1168 | * Execute the "set-src-port" action. May be called from {tcp,http}request. |
| 1169 | * We must test the sin_family before setting the port. If the address family |
| 1170 | * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0" |
| 1171 | * and the port is assigned. |
William Lallemand | 44be640 | 2016-05-25 01:51:35 +0200 | [diff] [blame] | 1172 | */ |
| 1173 | enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px, |
| 1174 | struct session *sess, struct stream *s, int flags) |
| 1175 | { |
| 1176 | struct connection *cli_conn; |
| 1177 | |
Willy Tarreau | dddd2b4 | 2019-07-17 11:37:29 +0200 | [diff] [blame] | 1178 | if ((cli_conn = objt_conn(sess->origin)) && conn_get_src(cli_conn)) { |
William Lallemand | 44be640 | 2016-05-25 01:51:35 +0200 | [diff] [blame] | 1179 | struct sample *smp; |
| 1180 | |
William Lallemand | 44be640 | 2016-05-25 01:51:35 +0200 | [diff] [blame] | 1181 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT); |
| 1182 | if (smp) { |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1183 | if (cli_conn->src->ss_family == AF_INET6) { |
| 1184 | ((struct sockaddr_in6 *)cli_conn->src)->sin6_port = htons(smp->data.u.sint); |
Willy Tarreau | 00005ce | 2016-10-21 15:07:45 +0200 | [diff] [blame] | 1185 | } else { |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1186 | if (cli_conn->src->ss_family != AF_INET) { |
| 1187 | cli_conn->src->ss_family = AF_INET; |
| 1188 | ((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr = 0; |
Willy Tarreau | 00005ce | 2016-10-21 15:07:45 +0200 | [diff] [blame] | 1189 | } |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1190 | ((struct sockaddr_in *)cli_conn->src)->sin_port = htons(smp->data.u.sint); |
William Lallemand | 44be640 | 2016-05-25 01:51:35 +0200 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | return ACT_RET_CONT; |
| 1195 | } |
| 1196 | |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1197 | /* |
Willy Tarreau | 00005ce | 2016-10-21 15:07:45 +0200 | [diff] [blame] | 1198 | * Execute the "set-dst-port" action. May be called from {tcp,http}request. |
| 1199 | * We must test the sin_family before setting the port. If the address family |
| 1200 | * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0" |
| 1201 | * and the port is assigned. |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1202 | */ |
| 1203 | enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px, |
| 1204 | struct session *sess, struct stream *s, int flags) |
| 1205 | { |
| 1206 | struct connection *cli_conn; |
| 1207 | |
Willy Tarreau | dddd2b4 | 2019-07-17 11:37:29 +0200 | [diff] [blame] | 1208 | if ((cli_conn = objt_conn(sess->origin)) && conn_get_dst(cli_conn)) { |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1209 | struct sample *smp; |
| 1210 | |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1211 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT); |
| 1212 | if (smp) { |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1213 | if (cli_conn->dst->ss_family == AF_INET6) { |
| 1214 | ((struct sockaddr_in6 *)cli_conn->dst)->sin6_port = htons(smp->data.u.sint); |
Willy Tarreau | 00005ce | 2016-10-21 15:07:45 +0200 | [diff] [blame] | 1215 | } else { |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1216 | if (cli_conn->dst->ss_family != AF_INET) { |
| 1217 | cli_conn->dst->ss_family = AF_INET; |
| 1218 | ((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr = 0; |
Willy Tarreau | 00005ce | 2016-10-21 15:07:45 +0200 | [diff] [blame] | 1219 | } |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1220 | ((struct sockaddr_in *)cli_conn->dst)->sin_port = htons(smp->data.u.sint); |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | return ACT_RET_CONT; |
| 1225 | } |
| 1226 | |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1227 | /* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */ |
| 1228 | static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags) |
| 1229 | { |
| 1230 | struct connection *conn = objt_conn(sess->origin); |
| 1231 | |
| 1232 | if (!conn) |
| 1233 | goto out; |
| 1234 | |
| 1235 | if (!conn_ctrl_ready(conn)) |
| 1236 | goto out; |
| 1237 | |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1238 | #ifdef TCP_QUICKACK |
Willy Tarreau | fc2a2d9 | 2015-09-29 18:15:01 +0200 | [diff] [blame] | 1239 | /* drain is needed only to send the quick ACK */ |
| 1240 | conn_sock_drain(conn); |
| 1241 | |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1242 | /* re-enable quickack if it was disabled to ack all data and avoid |
| 1243 | * retransmits from the client that might trigger a real reset. |
| 1244 | */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1245 | setsockopt(conn->handle.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1246 | #endif |
| 1247 | /* lingering must absolutely be disabled so that we don't send a |
| 1248 | * shutdown(), this is critical to the TCP_REPAIR trick. When no stream |
| 1249 | * is present, returning with ERR will cause lingering to be disabled. |
| 1250 | */ |
| 1251 | if (strm) |
| 1252 | strm->si[0].flags |= SI_FL_NOLINGER; |
| 1253 | |
Willy Tarreau | f50ec0f | 2015-09-29 18:11:32 +0200 | [diff] [blame] | 1254 | /* We're on the client-facing side, we must force to disable lingering to |
| 1255 | * ensure we will use an RST exclusively and kill any pending data. |
| 1256 | */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1257 | fdtab[conn->handle.fd].linger_risk = 1; |
Willy Tarreau | f50ec0f | 2015-09-29 18:11:32 +0200 | [diff] [blame] | 1258 | |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1259 | #ifdef TCP_REPAIR |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1260 | if (setsockopt(conn->handle.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) { |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1261 | /* socket will be quiet now */ |
| 1262 | goto out; |
| 1263 | } |
| 1264 | #endif |
| 1265 | /* either TCP_REPAIR is not defined or it failed (eg: permissions). |
| 1266 | * Let's fall back on the TTL trick, though it only works for routed |
| 1267 | * network and has no effect on local net. |
| 1268 | */ |
| 1269 | #ifdef IP_TTL |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1270 | setsockopt(conn->handle.fd, SOL_IP, IP_TTL, &one, sizeof(one)); |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1271 | #endif |
| 1272 | out: |
| 1273 | /* kill the stream if any */ |
| 1274 | if (strm) { |
| 1275 | channel_abort(&strm->req); |
| 1276 | channel_abort(&strm->res); |
Christopher Faulet | 177f480 | 2020-03-06 15:10:46 +0100 | [diff] [blame] | 1277 | strm->req.analysers &= AN_REQ_FLT_END; |
| 1278 | strm->res.analysers &= AN_RES_FLT_END; |
Christopher Faulet | ddc005a | 2020-03-06 15:23:18 +0100 | [diff] [blame] | 1279 | if (strm->flags & SF_BE_ASSIGNED) |
| 1280 | _HA_ATOMIC_ADD(&strm->be->be_counters.denied_req, 1); |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1281 | if (!(strm->flags & SF_ERR_MASK)) |
| 1282 | strm->flags |= SF_ERR_PRXCOND; |
| 1283 | if (!(strm->flags & SF_FINST_MASK)) |
| 1284 | strm->flags |= SF_FINST_R; |
| 1285 | } |
| 1286 | |
Olivier Houchard | 4051410 | 2019-03-08 18:54:04 +0100 | [diff] [blame] | 1287 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1288 | if (sess->listener->counters) |
Olivier Houchard | 4051410 | 2019-03-08 18:54:04 +0100 | [diff] [blame] | 1289 | _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1290 | |
Christopher Faulet | f573ba2 | 2020-03-06 15:15:49 +0100 | [diff] [blame] | 1291 | return ACT_RET_ABRT; |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1292 | } |
| 1293 | |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1294 | /* parse "set-{src,dst}[-port]" action */ |
| 1295 | 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] | 1296 | { |
| 1297 | int cur_arg; |
| 1298 | struct sample_expr *expr; |
| 1299 | unsigned int where; |
| 1300 | |
| 1301 | cur_arg = *orig_arg; |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 1302 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL); |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 1303 | if (!expr) |
| 1304 | return ACT_RET_PRS_ERR; |
| 1305 | |
| 1306 | where = 0; |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1307 | if (px->cap & PR_CAP_FE) |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 1308 | where |= SMP_VAL_FE_HRQ_HDR; |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1309 | if (px->cap & PR_CAP_BE) |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 1310 | where |= SMP_VAL_BE_HRQ_HDR; |
| 1311 | |
| 1312 | if (!(expr->fetch->val & where)) { |
| 1313 | memprintf(err, |
| 1314 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 1315 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 1316 | free(expr); |
| 1317 | return ACT_RET_PRS_ERR; |
| 1318 | } |
| 1319 | rule->arg.expr = expr; |
| 1320 | rule->action = ACT_CUSTOM; |
| 1321 | |
| 1322 | if (!strcmp(args[*orig_arg-1], "set-src")) { |
| 1323 | rule->action_ptr = tcp_action_req_set_src; |
William Lallemand | 44be640 | 2016-05-25 01:51:35 +0200 | [diff] [blame] | 1324 | } else if (!strcmp(args[*orig_arg-1], "set-src-port")) { |
| 1325 | rule->action_ptr = tcp_action_req_set_src_port; |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1326 | } else if (!strcmp(args[*orig_arg-1], "set-dst")) { |
| 1327 | rule->action_ptr = tcp_action_req_set_dst; |
| 1328 | } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) { |
| 1329 | rule->action_ptr = tcp_action_req_set_dst_port; |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 1330 | } else { |
| 1331 | return ACT_RET_PRS_ERR; |
| 1332 | } |
| 1333 | |
| 1334 | (*orig_arg)++; |
| 1335 | |
| 1336 | return ACT_RET_PRS_OK; |
| 1337 | } |
| 1338 | |
| 1339 | |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1340 | /* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on |
| 1341 | * success, ACT_RET_PRS_ERR on error. |
| 1342 | */ |
| 1343 | static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px, |
| 1344 | struct act_rule *rule, char **err) |
| 1345 | { |
| 1346 | rule->action = ACT_CUSTOM; |
| 1347 | rule->action_ptr = tcp_exec_action_silent_drop; |
| 1348 | return ACT_RET_PRS_OK; |
| 1349 | } |
| 1350 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1351 | |
| 1352 | /************************************************************************/ |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 1353 | /* All supported sample fetch functions must be declared here */ |
Willy Tarreau | 32389b7 | 2012-04-23 23:13:20 +0200 | [diff] [blame] | 1354 | /************************************************************************/ |
| 1355 | |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1356 | /* fetch the connection's source IPv4/IPv6 address */ |
Thierry FOURNIER | a123ad8 | 2015-07-24 09:12:15 +0200 | [diff] [blame] | 1357 | 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] | 1358 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1359 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1360 | |
| 1361 | if (!cli_conn) |
| 1362 | return 0; |
| 1363 | |
Willy Tarreau | dddd2b4 | 2019-07-17 11:37:29 +0200 | [diff] [blame] | 1364 | if (!conn_get_src(cli_conn)) |
| 1365 | return 0; |
| 1366 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1367 | switch (cli_conn->src->ss_family) { |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1368 | case AF_INET: |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1369 | smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->src)->sin_addr; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1370 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1371 | break; |
| 1372 | case AF_INET6: |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1373 | smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->src)->sin6_addr; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1374 | smp->data.type = SMP_T_IPV6; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1375 | break; |
| 1376 | default: |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1377 | return 0; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1378 | } |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1379 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1380 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1381 | return 1; |
| 1382 | } |
| 1383 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1384 | /* set temp integer to the connection's source port */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1385 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1386 | 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] | 1387 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1388 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1389 | |
| 1390 | if (!cli_conn) |
| 1391 | return 0; |
| 1392 | |
Willy Tarreau | dddd2b4 | 2019-07-17 11:37:29 +0200 | [diff] [blame] | 1393 | if (!conn_get_src(cli_conn)) |
| 1394 | return 0; |
| 1395 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1396 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1397 | if (!(smp->data.u.sint = get_host_port(cli_conn->src))) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1398 | return 0; |
| 1399 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1400 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1401 | return 1; |
| 1402 | } |
| 1403 | |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1404 | /* fetch the connection's destination IPv4/IPv6 address */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1405 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1406 | 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] | 1407 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1408 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1409 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1410 | if (!cli_conn) |
| 1411 | return 0; |
| 1412 | |
Willy Tarreau | dddd2b4 | 2019-07-17 11:37:29 +0200 | [diff] [blame] | 1413 | if (!conn_get_dst(cli_conn)) |
| 1414 | return 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1415 | |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1416 | switch (cli_conn->dst->ss_family) { |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1417 | case AF_INET: |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1418 | smp->data.u.ipv4 = ((struct sockaddr_in *)cli_conn->dst)->sin_addr; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1419 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1420 | break; |
| 1421 | case AF_INET6: |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1422 | smp->data.u.ipv6 = ((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1423 | smp->data.type = SMP_T_IPV6; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1424 | break; |
| 1425 | default: |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1426 | return 0; |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 1427 | } |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1428 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1429 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1430 | return 1; |
| 1431 | } |
| 1432 | |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 1433 | /* check if the destination address of the front connection is local to the |
| 1434 | * system or if it was intercepted. |
| 1435 | */ |
| 1436 | int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1437 | { |
| 1438 | struct connection *conn = objt_conn(smp->sess->origin); |
| 1439 | struct listener *li = smp->sess->listener; |
| 1440 | |
| 1441 | if (!conn) |
| 1442 | return 0; |
| 1443 | |
Willy Tarreau | dddd2b4 | 2019-07-17 11:37:29 +0200 | [diff] [blame] | 1444 | if (!conn_get_dst(conn)) |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 1445 | return 0; |
| 1446 | |
| 1447 | smp->data.type = SMP_T_BOOL; |
| 1448 | smp->flags = 0; |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1449 | smp->data.u.sint = addr_is_local(li->netns, conn->dst); |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 1450 | return smp->data.u.sint >= 0; |
| 1451 | } |
| 1452 | |
| 1453 | /* check if the source address of the front connection is local to the system |
| 1454 | * or not. |
| 1455 | */ |
| 1456 | int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1457 | { |
| 1458 | struct connection *conn = objt_conn(smp->sess->origin); |
| 1459 | struct listener *li = smp->sess->listener; |
| 1460 | |
| 1461 | if (!conn) |
| 1462 | return 0; |
| 1463 | |
Willy Tarreau | dddd2b4 | 2019-07-17 11:37:29 +0200 | [diff] [blame] | 1464 | if (!conn_get_src(conn)) |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 1465 | return 0; |
| 1466 | |
| 1467 | smp->data.type = SMP_T_BOOL; |
| 1468 | smp->flags = 0; |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1469 | smp->data.u.sint = addr_is_local(li->netns, conn->src); |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 1470 | return smp->data.u.sint >= 0; |
| 1471 | } |
| 1472 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1473 | /* set temp integer to the frontend connexion's destination port */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1474 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1475 | 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] | 1476 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1477 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1478 | |
| 1479 | if (!cli_conn) |
| 1480 | return 0; |
| 1481 | |
Willy Tarreau | dddd2b4 | 2019-07-17 11:37:29 +0200 | [diff] [blame] | 1482 | if (!conn_get_dst(cli_conn)) |
| 1483 | return 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1484 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1485 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 7bbc4a5 | 2019-07-17 15:41:35 +0200 | [diff] [blame] | 1486 | if (!(smp->data.u.sint = get_host_port(cli_conn->dst))) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1487 | return 0; |
| 1488 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1489 | smp->flags = 0; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1490 | return 1; |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1491 | } |
| 1492 | |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1493 | #ifdef TCP_INFO |
| 1494 | |
Christopher Faulet | ba0c53e | 2019-10-17 14:40:48 +0200 | [diff] [blame] | 1495 | |
| 1496 | /* Validates the arguments passed to "fc_*" fetch keywords returning a time |
| 1497 | * value. These keywords support an optional string representing the unit of the |
| 1498 | * result: "us" for microseconds and "ms" for milliseconds". Returns 0 on error |
| 1499 | * and non-zero if OK. |
| 1500 | */ |
| 1501 | static int val_fc_time_value(struct arg *args, char **err) |
| 1502 | { |
Christopher Faulet | ba0c53e | 2019-10-17 14:40:48 +0200 | [diff] [blame] | 1503 | if (args[0].type == ARGT_STR) { |
| 1504 | if (strcmp(args[0].data.str.area, "us") == 0) { |
| 1505 | free(args[0].data.str.area); |
| 1506 | args[0].type = ARGT_SINT; |
| 1507 | args[0].data.sint = TIME_UNIT_US; |
| 1508 | } |
| 1509 | else if (strcmp(args[0].data.str.area, "ms") == 0) { |
| 1510 | free(args[0].data.str.area); |
| 1511 | args[0].type = ARGT_SINT; |
| 1512 | args[0].data.sint = TIME_UNIT_MS; |
| 1513 | } |
| 1514 | else { |
| 1515 | memprintf(err, "expects 'us' or 'ms', got '%s'", |
| 1516 | args[0].data.str.area); |
| 1517 | return 0; |
| 1518 | } |
| 1519 | } |
| 1520 | else { |
| 1521 | memprintf(err, "Unexpected arg type"); |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | return 1; |
| 1526 | } |
| 1527 | |
| 1528 | /* Validates the arguments passed to "fc_*" fetch keywords returning a |
| 1529 | * counter. These keywords should be used without any keyword, but because of a |
| 1530 | * bug in previous versions, an optional string argument may be passed. In such |
| 1531 | * case, the argument is ignored and a warning is emitted. Returns 0 on error |
| 1532 | * and non-zero if OK. |
| 1533 | */ |
| 1534 | static int var_fc_counter(struct arg *args, char **err) |
| 1535 | { |
| 1536 | if (args[0].type != ARGT_STOP) { |
| 1537 | ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n"); |
| 1538 | if (args[0].type == ARGT_STR) |
| 1539 | free(args[0].data.str.area); |
| 1540 | args[0].type = ARGT_STOP; |
| 1541 | } |
| 1542 | |
| 1543 | return 1; |
| 1544 | } |
| 1545 | |
Joseph Herlant | a633147 | 2018-11-25 12:59:12 -0800 | [diff] [blame] | 1546 | /* Returns some tcp_info data if it's available. "dir" must be set to 0 if |
| 1547 | * the client connection is required, otherwise it is set to 1. "val" represents |
Christopher Faulet | ba0c53e | 2019-10-17 14:40:48 +0200 | [diff] [blame] | 1548 | * the required value. |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1549 | * If the function fails it returns 0, otherwise it returns 1 and "result" is filled. |
| 1550 | */ |
| 1551 | static inline int get_tcp_info(const struct arg *args, struct sample *smp, |
| 1552 | int dir, int val) |
| 1553 | { |
| 1554 | struct connection *conn; |
| 1555 | struct tcp_info info; |
| 1556 | socklen_t optlen; |
| 1557 | |
| 1558 | /* strm can be null. */ |
| 1559 | if (!smp->strm) |
| 1560 | return 0; |
| 1561 | |
| 1562 | /* get the object associated with the stream interface.The |
| 1563 | * object can be other thing than a connection. For example, |
| 1564 | * it be a appctx. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1565 | conn = cs_conn(objt_cs(smp->strm->si[dir].end)); |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1566 | if (!conn) |
| 1567 | return 0; |
| 1568 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1569 | /* The fd may not be available for the tcp_info struct, and the |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1570 | syscal can fail. */ |
| 1571 | optlen = sizeof(info); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1572 | if (getsockopt(conn->handle.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1) |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1573 | return 0; |
| 1574 | |
| 1575 | /* extract the value. */ |
| 1576 | smp->data.type = SMP_T_SINT; |
| 1577 | switch (val) { |
Joe Williams | 30fcd39 | 2016-08-10 07:06:44 -0700 | [diff] [blame] | 1578 | case 0: smp->data.u.sint = info.tcpi_rtt; break; |
| 1579 | case 1: smp->data.u.sint = info.tcpi_rttvar; break; |
| 1580 | #if defined(__linux__) |
| 1581 | /* these ones are common to all Linux versions */ |
| 1582 | case 2: smp->data.u.sint = info.tcpi_unacked; break; |
| 1583 | case 3: smp->data.u.sint = info.tcpi_sacked; break; |
| 1584 | case 4: smp->data.u.sint = info.tcpi_lost; break; |
| 1585 | case 5: smp->data.u.sint = info.tcpi_retrans; break; |
| 1586 | case 6: smp->data.u.sint = info.tcpi_fackets; break; |
| 1587 | case 7: smp->data.u.sint = info.tcpi_reordering; break; |
| 1588 | #elif defined(__FreeBSD__) || defined(__NetBSD__) |
| 1589 | /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */ |
| 1590 | case 2: smp->data.u.sint = info.__tcpi_unacked; break; |
| 1591 | case 3: smp->data.u.sint = info.__tcpi_sacked; break; |
| 1592 | case 4: smp->data.u.sint = info.__tcpi_lost; break; |
| 1593 | case 5: smp->data.u.sint = info.__tcpi_retrans; break; |
| 1594 | case 6: smp->data.u.sint = info.__tcpi_fackets; break; |
| 1595 | case 7: smp->data.u.sint = info.__tcpi_reordering; break; |
| 1596 | #endif |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1597 | default: return 0; |
| 1598 | } |
| 1599 | |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1600 | return 1; |
| 1601 | } |
| 1602 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 1603 | /* get the mean rtt of a client connection */ |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1604 | static int |
| 1605 | smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1606 | { |
| 1607 | if (!get_tcp_info(args, smp, 0, 0)) |
| 1608 | return 0; |
Christopher Faulet | ba0c53e | 2019-10-17 14:40:48 +0200 | [diff] [blame] | 1609 | |
| 1610 | /* By default or if explicitly specified, convert rtt to ms */ |
| 1611 | if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS) |
| 1612 | smp->data.u.sint = (smp->data.u.sint + 500) / 1000; |
| 1613 | |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1614 | return 1; |
| 1615 | } |
| 1616 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 1617 | /* get the variance of the mean rtt of a client connection */ |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1618 | static int |
| 1619 | smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1620 | { |
| 1621 | if (!get_tcp_info(args, smp, 0, 1)) |
| 1622 | return 0; |
Christopher Faulet | ba0c53e | 2019-10-17 14:40:48 +0200 | [diff] [blame] | 1623 | |
| 1624 | /* By default or if explicitly specified, convert rttvar to ms */ |
| 1625 | if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS) |
| 1626 | smp->data.u.sint = (smp->data.u.sint + 500) / 1000; |
| 1627 | |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1628 | return 1; |
| 1629 | } |
Joe Williams | 30fcd39 | 2016-08-10 07:06:44 -0700 | [diff] [blame] | 1630 | |
| 1631 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) |
| 1632 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 1633 | /* get the unacked counter on a client connection */ |
Joe Williams | 30fcd39 | 2016-08-10 07:06:44 -0700 | [diff] [blame] | 1634 | static int |
| 1635 | smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1636 | { |
| 1637 | if (!get_tcp_info(args, smp, 0, 2)) |
| 1638 | return 0; |
| 1639 | return 1; |
| 1640 | } |
| 1641 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 1642 | /* get the sacked counter on a client connection */ |
Joe Williams | 30fcd39 | 2016-08-10 07:06:44 -0700 | [diff] [blame] | 1643 | static int |
| 1644 | smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1645 | { |
| 1646 | if (!get_tcp_info(args, smp, 0, 3)) |
| 1647 | return 0; |
| 1648 | return 1; |
| 1649 | } |
| 1650 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 1651 | /* get the lost counter on a client connection */ |
Joe Williams | 30fcd39 | 2016-08-10 07:06:44 -0700 | [diff] [blame] | 1652 | static int |
| 1653 | smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1654 | { |
| 1655 | if (!get_tcp_info(args, smp, 0, 4)) |
| 1656 | return 0; |
| 1657 | return 1; |
| 1658 | } |
| 1659 | |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1660 | /* get the retrans counter on a client connection */ |
Joe Williams | 30fcd39 | 2016-08-10 07:06:44 -0700 | [diff] [blame] | 1661 | static int |
| 1662 | smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1663 | { |
| 1664 | if (!get_tcp_info(args, smp, 0, 5)) |
| 1665 | return 0; |
| 1666 | return 1; |
| 1667 | } |
| 1668 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 1669 | /* get the fackets counter on a client connection */ |
Joe Williams | 30fcd39 | 2016-08-10 07:06:44 -0700 | [diff] [blame] | 1670 | static int |
| 1671 | smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1672 | { |
| 1673 | if (!get_tcp_info(args, smp, 0, 6)) |
| 1674 | return 0; |
| 1675 | return 1; |
| 1676 | } |
| 1677 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 1678 | /* get the reordering counter on a client connection */ |
Joe Williams | 30fcd39 | 2016-08-10 07:06:44 -0700 | [diff] [blame] | 1679 | static int |
| 1680 | smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1681 | { |
| 1682 | if (!get_tcp_info(args, smp, 0, 7)) |
| 1683 | return 0; |
| 1684 | return 1; |
| 1685 | } |
| 1686 | #endif // linux || freebsd || netbsd |
| 1687 | #endif // TCP_INFO |
Thierry Fournier / OZON.IO | 6310bef | 2016-07-24 20:16:50 +0200 | [diff] [blame] | 1688 | |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1689 | #ifdef IPV6_V6ONLY |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 1690 | /* parse the "v4v6" bind keyword */ |
| 1691 | static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1692 | { |
| 1693 | struct listener *l; |
| 1694 | |
| 1695 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1696 | if (l->addr.ss_family == AF_INET6) |
| 1697 | l->options |= LI_O_V4V6; |
| 1698 | } |
| 1699 | |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1703 | /* parse the "v6only" bind keyword */ |
| 1704 | static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1705 | { |
| 1706 | struct listener *l; |
| 1707 | |
| 1708 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1709 | if (l->addr.ss_family == AF_INET6) |
| 1710 | l->options |= LI_O_V6ONLY; |
| 1711 | } |
| 1712 | |
| 1713 | return 0; |
| 1714 | } |
| 1715 | #endif |
| 1716 | |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 1717 | #ifdef CONFIG_HAP_TRANSPARENT |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1718 | /* parse the "transparent" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1719 | 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] | 1720 | { |
| 1721 | struct listener *l; |
| 1722 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1723 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1724 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1725 | l->options |= LI_O_FOREIGN; |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1726 | } |
| 1727 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1728 | return 0; |
| 1729 | } |
| 1730 | #endif |
| 1731 | |
| 1732 | #ifdef TCP_DEFER_ACCEPT |
| 1733 | /* parse the "defer-accept" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1734 | 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] | 1735 | { |
| 1736 | struct listener *l; |
| 1737 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1738 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1739 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1740 | l->options |= LI_O_DEF_ACCEPT; |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1741 | } |
| 1742 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1743 | return 0; |
| 1744 | } |
| 1745 | #endif |
| 1746 | |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 1747 | #ifdef TCP_FASTOPEN |
Lukas Tribus | 0defb90 | 2013-02-13 23:35:39 +0100 | [diff] [blame] | 1748 | /* parse the "tfo" bind keyword */ |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 1749 | static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1750 | { |
| 1751 | struct listener *l; |
| 1752 | |
| 1753 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1754 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1755 | l->options |= LI_O_TCP_FO; |
| 1756 | } |
| 1757 | |
| 1758 | return 0; |
| 1759 | } |
| 1760 | #endif |
| 1761 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1762 | #ifdef TCP_MAXSEG |
| 1763 | /* parse the "mss" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1764 | 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] | 1765 | { |
| 1766 | struct listener *l; |
| 1767 | int mss; |
| 1768 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1769 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1770 | memprintf(err, "'%s' : missing MSS value", args[cur_arg]); |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1771 | return ERR_ALERT | ERR_FATAL; |
| 1772 | } |
| 1773 | |
| 1774 | mss = atoi(args[cur_arg + 1]); |
| 1775 | if (!mss || abs(mss) > 65535) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1776 | 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] | 1777 | return ERR_ALERT | ERR_FATAL; |
| 1778 | } |
| 1779 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1780 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1781 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1782 | l->maxseg = mss; |
| 1783 | } |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1784 | |
| 1785 | return 0; |
| 1786 | } |
| 1787 | #endif |
| 1788 | |
Willy Tarreau | 2af207a | 2015-02-04 00:45:58 +0100 | [diff] [blame] | 1789 | #ifdef TCP_USER_TIMEOUT |
| 1790 | /* parse the "tcp-ut" bind keyword */ |
| 1791 | static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1792 | { |
| 1793 | const char *ptr = NULL; |
| 1794 | struct listener *l; |
| 1795 | unsigned int timeout; |
| 1796 | |
| 1797 | if (!*args[cur_arg + 1]) { |
| 1798 | memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]); |
| 1799 | return ERR_ALERT | ERR_FATAL; |
| 1800 | } |
| 1801 | |
| 1802 | ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 1803 | if (ptr == PARSE_TIME_OVER) { |
| 1804 | memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)", |
| 1805 | args[cur_arg+1], args[cur_arg]); |
| 1806 | return ERR_ALERT | ERR_FATAL; |
| 1807 | } |
| 1808 | else if (ptr == PARSE_TIME_UNDER) { |
| 1809 | memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)", |
| 1810 | args[cur_arg+1], args[cur_arg]); |
| 1811 | return ERR_ALERT | ERR_FATAL; |
| 1812 | } |
| 1813 | else if (ptr) { |
Willy Tarreau | 2af207a | 2015-02-04 00:45:58 +0100 | [diff] [blame] | 1814 | memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]); |
| 1815 | return ERR_ALERT | ERR_FATAL; |
| 1816 | } |
| 1817 | |
| 1818 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1819 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1820 | l->tcp_ut = timeout; |
| 1821 | } |
| 1822 | |
| 1823 | return 0; |
| 1824 | } |
| 1825 | #endif |
| 1826 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1827 | #ifdef SO_BINDTODEVICE |
Willy Tarreau | 2af207a | 2015-02-04 00:45:58 +0100 | [diff] [blame] | 1828 | /* parse the "interface" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1829 | 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] | 1830 | { |
| 1831 | struct listener *l; |
| 1832 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1833 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 1834 | memprintf(err, "'%s' : missing interface name", args[cur_arg]); |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1835 | return ERR_ALERT | ERR_FATAL; |
| 1836 | } |
| 1837 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1838 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1839 | if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6) |
| 1840 | l->interface = strdup(args[cur_arg + 1]); |
| 1841 | } |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1842 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1843 | return 0; |
| 1844 | } |
| 1845 | #endif |
| 1846 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1847 | #ifdef USE_NS |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1848 | /* parse the "namespace" bind keyword */ |
| 1849 | static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 1850 | { |
| 1851 | struct listener *l; |
| 1852 | char *namespace = NULL; |
| 1853 | |
| 1854 | if (!*args[cur_arg + 1]) { |
| 1855 | memprintf(err, "'%s' : missing namespace id", args[cur_arg]); |
| 1856 | return ERR_ALERT | ERR_FATAL; |
| 1857 | } |
| 1858 | namespace = args[cur_arg + 1]; |
| 1859 | |
| 1860 | list_for_each_entry(l, &conf->listeners, by_bind) { |
| 1861 | l->netns = netns_store_lookup(namespace, strlen(namespace)); |
| 1862 | |
| 1863 | if (l->netns == NULL) |
| 1864 | l->netns = netns_store_insert(namespace); |
| 1865 | |
| 1866 | if (l->netns == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1867 | ha_alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1868 | return ERR_ALERT | ERR_FATAL; |
| 1869 | } |
| 1870 | } |
| 1871 | return 0; |
| 1872 | } |
| 1873 | #endif |
| 1874 | |
Willy Tarreau | 163d462 | 2015-10-13 16:16:41 +0200 | [diff] [blame] | 1875 | #ifdef TCP_USER_TIMEOUT |
| 1876 | /* parse the "tcp-ut" server keyword */ |
| 1877 | static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1878 | { |
| 1879 | const char *ptr = NULL; |
| 1880 | unsigned int timeout; |
| 1881 | |
| 1882 | if (!*args[*cur_arg + 1]) { |
| 1883 | memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]); |
| 1884 | return ERR_ALERT | ERR_FATAL; |
| 1885 | } |
| 1886 | |
| 1887 | ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 1888 | if (ptr == PARSE_TIME_OVER) { |
| 1889 | memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)", |
| 1890 | args[*cur_arg+1], args[*cur_arg]); |
| 1891 | return ERR_ALERT | ERR_FATAL; |
| 1892 | } |
| 1893 | else if (ptr == PARSE_TIME_UNDER) { |
| 1894 | memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)", |
| 1895 | args[*cur_arg+1], args[*cur_arg]); |
| 1896 | return ERR_ALERT | ERR_FATAL; |
| 1897 | } |
| 1898 | else if (ptr) { |
Willy Tarreau | 163d462 | 2015-10-13 16:16:41 +0200 | [diff] [blame] | 1899 | memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]); |
| 1900 | return ERR_ALERT | ERR_FATAL; |
| 1901 | } |
| 1902 | |
| 1903 | if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6) |
| 1904 | newsrv->tcp_ut = timeout; |
| 1905 | |
| 1906 | return 0; |
| 1907 | } |
| 1908 | #endif |
| 1909 | |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 1910 | |
Willy Tarreau | 4a12981 | 2012-04-25 17:31:42 +0200 | [diff] [blame] | 1911 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1912 | * Note: fetches that may return multiple types must be declared as the lowest |
| 1913 | * common denominator, the type that can be casted into all other ones. For |
| 1914 | * instance v4/v6 must be declared v4. |
| 1915 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1916 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 1917 | { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI }, |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 1918 | { "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] | 1919 | { "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] | 1920 | { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI }, |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 1921 | { "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] | 1922 | { "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] | 1923 | #ifdef TCP_INFO |
Christopher Faulet | ba0c53e | 2019-10-17 14:40:48 +0200 | [diff] [blame] | 1924 | { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI }, |
| 1925 | { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI }, |
Joe Williams | 30fcd39 | 2016-08-10 07:06:44 -0700 | [diff] [blame] | 1926 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) |
Christopher Faulet | ba0c53e | 2019-10-17 14:40:48 +0200 | [diff] [blame] | 1927 | { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI }, |
| 1928 | { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI }, |
| 1929 | { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI }, |
| 1930 | { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI }, |
| 1931 | { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI }, |
| 1932 | { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI }, |
Joe Williams | 30fcd39 | 2016-08-10 07:06:44 -0700 | [diff] [blame] | 1933 | #endif // linux || freebsd || netbsd |
| 1934 | #endif // TCP_INFO |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 1935 | { /* END */ }, |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1936 | }}; |
| 1937 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1938 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 1939 | |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1940 | /************************************************************************/ |
| 1941 | /* All supported bind keywords must be declared here. */ |
| 1942 | /************************************************************************/ |
| 1943 | |
| 1944 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1945 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1946 | * all code contributors. |
| 1947 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1948 | * the config parser can report an appropriate error when a known keyword was |
| 1949 | * not enabled. |
| 1950 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 1951 | static struct bind_kw_list bind_kws = { "TCP", { }, { |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1952 | #ifdef TCP_DEFER_ACCEPT |
| 1953 | { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */ |
| 1954 | #endif |
| 1955 | #ifdef SO_BINDTODEVICE |
| 1956 | { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */ |
| 1957 | #endif |
| 1958 | #ifdef TCP_MAXSEG |
| 1959 | { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */ |
| 1960 | #endif |
Willy Tarreau | 2af207a | 2015-02-04 00:45:58 +0100 | [diff] [blame] | 1961 | #ifdef TCP_USER_TIMEOUT |
| 1962 | { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */ |
| 1963 | #endif |
Willy Tarreau | 1c862c5 | 2012-10-05 16:21:00 +0200 | [diff] [blame] | 1964 | #ifdef TCP_FASTOPEN |
| 1965 | { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */ |
| 1966 | #endif |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 1967 | #ifdef CONFIG_HAP_TRANSPARENT |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1968 | { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */ |
| 1969 | #endif |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1970 | #ifdef IPV6_V6ONLY |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 1971 | { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */ |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1972 | { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */ |
| 1973 | #endif |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1974 | #ifdef USE_NS |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1975 | { "namespace", bind_parse_namespace, 1 }, |
| 1976 | #endif |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1977 | /* the versions with the NULL parse function*/ |
| 1978 | { "defer-accept", NULL, 0 }, |
| 1979 | { "interface", NULL, 1 }, |
| 1980 | { "mss", NULL, 1 }, |
| 1981 | { "transparent", NULL, 0 }, |
Willy Tarreau | 77e3af9 | 2012-11-24 15:07:23 +0100 | [diff] [blame] | 1982 | { "v4v6", NULL, 0 }, |
Willy Tarreau | 9b6700f | 2012-11-24 11:55:28 +0100 | [diff] [blame] | 1983 | { "v6only", NULL, 0 }, |
Willy Tarreau | 4479124 | 2012-09-12 23:27:21 +0200 | [diff] [blame] | 1984 | { NULL, NULL, 0 }, |
| 1985 | }}; |
| 1986 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1987 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 1988 | |
Willy Tarreau | 163d462 | 2015-10-13 16:16:41 +0200 | [diff] [blame] | 1989 | static struct srv_kw_list srv_kws = { "TCP", { }, { |
| 1990 | #ifdef TCP_USER_TIMEOUT |
Frédéric Lécaille | 5c3cd97 | 2017-03-15 16:36:09 +0100 | [diff] [blame] | 1991 | { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */ |
Willy Tarreau | 163d462 | 2015-10-13 16:16:41 +0200 | [diff] [blame] | 1992 | #endif |
| 1993 | { NULL, NULL, 0 }, |
| 1994 | }}; |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1995 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1996 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 1997 | |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 1998 | static struct action_kw_list tcp_req_conn_actions = {ILH, { |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 1999 | { "set-src", tcp_parse_set_src_dst }, |
| 2000 | { "set-src-port", tcp_parse_set_src_dst }, |
| 2001 | { "set-dst" , tcp_parse_set_src_dst }, |
| 2002 | { "set-dst-port", tcp_parse_set_src_dst }, |
Baptiste Assmann | e1afd4f | 2019-04-18 16:21:13 +0200 | [diff] [blame] | 2003 | { "silent-drop", tcp_parse_silent_drop }, |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 2004 | { /* END */ } |
| 2005 | }}; |
| 2006 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2007 | INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions); |
| 2008 | |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 2009 | static struct action_kw_list tcp_req_sess_actions = {ILH, { |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 2010 | { "set-src", tcp_parse_set_src_dst }, |
| 2011 | { "set-src-port", tcp_parse_set_src_dst }, |
| 2012 | { "set-dst" , tcp_parse_set_src_dst }, |
| 2013 | { "set-dst-port", tcp_parse_set_src_dst }, |
Baptiste Assmann | e1afd4f | 2019-04-18 16:21:13 +0200 | [diff] [blame] | 2014 | { "silent-drop", tcp_parse_silent_drop }, |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 2015 | { /* END */ } |
| 2016 | }}; |
| 2017 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2018 | INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions); |
| 2019 | |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 2020 | static struct action_kw_list tcp_req_cont_actions = {ILH, { |
Baptiste Assmann | e1afd4f | 2019-04-18 16:21:13 +0200 | [diff] [blame] | 2021 | { "set-dst" , tcp_parse_set_src_dst }, |
| 2022 | { "set-dst-port", tcp_parse_set_src_dst }, |
| 2023 | { "silent-drop", tcp_parse_silent_drop }, |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 2024 | { /* END */ } |
| 2025 | }}; |
| 2026 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2027 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions); |
| 2028 | |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 2029 | static struct action_kw_list tcp_res_cont_actions = {ILH, { |
| 2030 | { "silent-drop", tcp_parse_silent_drop }, |
| 2031 | { /* END */ } |
| 2032 | }}; |
| 2033 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2034 | INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions); |
| 2035 | |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 2036 | static struct action_kw_list http_req_actions = {ILH, { |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 2037 | { "silent-drop", tcp_parse_silent_drop }, |
William Lallemand | 13e9b0c | 2016-05-25 02:34:07 +0200 | [diff] [blame] | 2038 | { "set-src", tcp_parse_set_src_dst }, |
| 2039 | { "set-src-port", tcp_parse_set_src_dst }, |
| 2040 | { "set-dst", tcp_parse_set_src_dst }, |
| 2041 | { "set-dst-port", tcp_parse_set_src_dst }, |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 2042 | { /* END */ } |
| 2043 | }}; |
| 2044 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2045 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 2046 | |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 2047 | static struct action_kw_list http_res_actions = {ILH, { |
| 2048 | { "silent-drop", tcp_parse_silent_drop }, |
| 2049 | { /* END */ } |
| 2050 | }}; |
| 2051 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2052 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
Willy Tarreau | 2d392c2 | 2015-08-24 01:43:45 +0200 | [diff] [blame] | 2053 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 2054 | REGISTER_BUILD_OPTS("Built with transparent proxy support using:" |
Willy Tarreau | ba96291 | 2016-12-21 18:55:02 +0100 | [diff] [blame] | 2055 | #if defined(IP_TRANSPARENT) |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 2056 | " IP_TRANSPARENT" |
Willy Tarreau | ba96291 | 2016-12-21 18:55:02 +0100 | [diff] [blame] | 2057 | #endif |
| 2058 | #if defined(IPV6_TRANSPARENT) |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 2059 | " IPV6_TRANSPARENT" |
Willy Tarreau | ba96291 | 2016-12-21 18:55:02 +0100 | [diff] [blame] | 2060 | #endif |
| 2061 | #if defined(IP_FREEBIND) |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 2062 | " IP_FREEBIND" |
Willy Tarreau | ba96291 | 2016-12-21 18:55:02 +0100 | [diff] [blame] | 2063 | #endif |
| 2064 | #if defined(IP_BINDANY) |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 2065 | " IP_BINDANY" |
Willy Tarreau | ba96291 | 2016-12-21 18:55:02 +0100 | [diff] [blame] | 2066 | #endif |
| 2067 | #if defined(IPV6_BINDANY) |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 2068 | " IPV6_BINDANY" |
Willy Tarreau | ba96291 | 2016-12-21 18:55:02 +0100 | [diff] [blame] | 2069 | #endif |
| 2070 | #if defined(SO_BINDANY) |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 2071 | " SO_BINDANY" |
Willy Tarreau | ba96291 | 2016-12-21 18:55:02 +0100 | [diff] [blame] | 2072 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 2073 | ""); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 2074 | |
| 2075 | |
| 2076 | /* |
| 2077 | * Local variables: |
| 2078 | * c-indent-level: 8 |
| 2079 | * c-basic-offset: 8 |
| 2080 | * End: |
| 2081 | */ |