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