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