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