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