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