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