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