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