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