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