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