blob: b664831baa6ce49de39c5fe3666c32fadb77bcd4 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
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 Assmann39a5f222016-08-08 14:12:08 +020013/* this is to have tcp_info defined on systems using musl
14 * library, such as Alpine Linux
15 */
16#define _GNU_SOURCE
17
Willy Tarreaue6b98942007-10-29 01:09:36 +010018#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 Sivachenkocaf58982009-08-24 15:11:06 +040032#include <netinet/tcp.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020033#include <netinet/in.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040034
Willy Tarreaue6b98942007-10-29 01:09:36 +010035#include <common/compat.h>
36#include <common/config.h>
37#include <common/debug.h>
38#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039#include <common/mini-clist.h>
40#include <common/standard.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010041#include <common/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010042
Willy Tarreau39713102016-11-25 15:49:32 +010043#include <types/action.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020044#include <types/connection.h>
Willy Tarreau39713102016-11-25 15:49:32 +010045#include <types/global.h>
46#include <types/stream.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010047
Willy Tarreau9fcb9842012-04-20 14:45:49 +020048#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020050#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020051#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020052#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020053#include <proto/log.h>
54#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020055#include <proto/protocol.h>
Willy Tarreau2d392c22015-08-24 01:43:45 +020056#include <proto/proto_http.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010057#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020059#include <proto/sample.h>
Willy Tarreau163d4622015-10-13 16:16:41 +020060#include <proto/server.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020061#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010062#include <proto/tcp_rules.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010063
Emeric Bruncf20bf12010-10-22 16:06:11 +020064static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
65static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010066
67/* Note: must not be declared <const> as its list will be overwritten */
68static 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 Tarreaubbebbbf2012-05-07 21:22:09 +020076 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020077 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020078 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010079 .bind_all = tcp_bind_listeners,
80 .unbind_all = unbind_all_listeners,
81 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020082 .get_src = tcp_get_src,
83 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020084 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +020085 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010086 .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 */
91static 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 Tarreaubbebbbf2012-05-07 21:22:09 +020099 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +0200100 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200101 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100102 .bind_all = tcp_bind_listeners,
103 .unbind_all = unbind_all_listeners,
104 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200105 .get_src = tcp_get_src,
106 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200107 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +0200108 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100109 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
110 .nb_listeners = 0,
111};
112
David du Colombier6f5ccb12011-03-10 22:26:24 +0100113/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100114 * 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 Tarreaue8c66af2008-01-13 18:40:14 +0100122 * 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 Colombier6f5ccb12011-03-10 22:26:24 +0100126int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100127{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100128 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100129 int foreign_ok = 0;
130 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100131 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200132 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200133
David du Colombier65c17962012-07-13 14:34:59 +0200134 switch (local->ss_family) {
135 case AF_INET:
136 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200137 /* 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 Baauwff30b662013-05-08 23:22:39 +0200148#if defined(IP_BINDANY)
149 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
150#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200151#if defined(SO_BINDANY)
152 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
153#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200154 )
David du Colombier65c17962012-07-13 14:34:59 +0200155 foreign_ok = 1;
156 else
157 ip_transp_working = 0;
158 }
159 break;
160 case AF_INET6:
161 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200162 if (0
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200163#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200164 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
165#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100166#if defined(IP_FREEBIND)
167 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
168#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200169#if defined(IPV6_BINDANY)
170 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
171#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200172#if defined(SO_BINDANY)
173 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
174#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200175 )
David du Colombier65c17962012-07-13 14:34:59 +0200176 foreign_ok = 1;
177 else
178 ip6_transp_working = 0;
179 }
180 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100181 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200182
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100183 if (flags) {
184 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200185 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100186 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 Tarreau5dc1e982011-12-16 21:25:11 +0100199 default:
200 /* we don't want to try to bind to an unknown address family */
201 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100202 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100203 }
204
Simon Hormande072bd2011-06-24 15:11:37 +0900205 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100206 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200207 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200208 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
209 if (ret < 0)
210 return 2;
211 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100212 }
213 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200214 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200215 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
216 if (ret < 0)
217 return 1;
218 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100219 }
220
221 if (!flags)
222 return 0;
223
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100224 if (!foreign_ok)
225 /* we could not bind to a foreign address */
226 return 2;
227
228 return 0;
229}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100230
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100231static int create_server_socket(struct connection *conn)
232{
Willy Tarreau529c1392014-12-24 13:47:55 +0100233 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100234
Willy Tarreau529c1392014-12-24 13:47:55 +0100235#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 Krisztianb3e54fe2014-11-17 15:11:45 +0100243 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
244}
Willy Tarreau9650f372009-08-16 14:02:45 +0200245
246/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200247 * 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 Tarreau3fdb3662012-11-12 00:42:33 +0100252 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100253 * 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 Tarreaub1d67742010-03-29 19:36:59 +0200260 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200261 * Note that a pending send_proxy message accounts for data.
262 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200263 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200264 * - 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üsterhus4896c442016-11-29 02:15:19 +0100270 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100271 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200272 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100273 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200274 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100275
Willy Tarreauf0837b22012-11-24 10:24:27 +0100276int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200277{
278 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100279 struct server *srv;
280 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100281 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100282
Willy Tarreau9ce70132014-01-24 16:08:19 +0100283 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
284
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100285 switch (obj_type(conn->target)) {
286 case OBJ_TYPE_PROXY:
287 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100288 srv = NULL;
289 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100290 case OBJ_TYPE_SERVER:
291 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100292 be = srv->proxy;
293 break;
294 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100295 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200296 return SF_ERR_INTERNAL;
Willy Tarreauac825402011-03-04 22:04:29 +0100297 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200298
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100299 fd = conn->t.sock.fd = create_server_socket(conn);
300
301 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200302 qfprintf(stderr, "Cannot get a server socket.\n");
303
Willy Tarreau9ce70132014-01-24 16:08:19 +0100304 if (errno == ENFILE) {
305 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200306 send_log(be, LOG_EMERG,
307 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
308 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100309 }
310 else if (errno == EMFILE) {
311 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200312 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 Tarreau9ce70132014-01-24 16:08:19 +0100315 }
316 else if (errno == ENOBUFS || errno == ENOMEM) {
317 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200318 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 Tarreau9ce70132014-01-24 16:08:19 +0100321 }
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 Tarreau9650f372009-08-16 14:02:45 +0200328 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100329 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200330 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200331 }
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 Tarreau9ce70132014-01-24 16:08:19 +0100339 conn->err_code = CO_ER_CONF_FDLIM;
340 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200341 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau9650f372009-08-16 14:02:45 +0200342 }
343
344 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900345 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200346 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
347 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100348 conn->err_code = CO_ER_SOCK_ERR;
349 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200350 return SF_ERR_INTERNAL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200351 }
352
353 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900354 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200355
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 /* allow specific binding :
357 * - server-specific at first
358 * - proxy-specific next
359 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100360 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 Tarreau9650f372009-08-16 14:02:45 +0200368 int ret, flags = 0;
369
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200370 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100371 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100372 case CO_SRC_TPROXY_CLI:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200373 conn->flags |= CO_FL_PRIVATE;
374 /* fall through */
375 case CO_SRC_TPROXY_ADDR:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200376 flags = 3;
377 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100378 case CO_SRC_TPROXY_CIP:
379 case CO_SRC_TPROXY_DYN:
Willy Tarreau387ebf82015-08-04 19:24:13 +0200380 conn->flags |= CO_FL_PRIVATE;
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200381 flags = 1;
382 break;
383 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200384 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200385
Willy Tarreau9650f372009-08-16 14:02:45 +0200386#ifdef SO_BINDTODEVICE
387 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100388 if (src->iface_name)
389 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200390#endif
391
Willy Tarreaua4380b42012-12-08 22:49:11 +0100392 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200393 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100394 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200395
396 ret = 1;
Vincent Bernat6e615892016-05-18 16:17:44 +0200397 memcpy(&sa, &src->source_addr, sizeof(sa));
Willy Tarreau9650f372009-08-16 14:02:45 +0200398
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 Tarreau8d5d77e2009-10-18 07:25:52 +0200403 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
404 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200405
406 if (!attempts)
407 break;
408 attempts--;
409
Willy Tarreaua4380b42012-12-08 22:49:11 +0100410 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100411 if (!fdinfo[fd].local_port) {
412 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200413 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100414 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200415
Willy Tarreaua4380b42012-12-08 22:49:11 +0100416 fdinfo[fd].port_range = src->sport_range;
417 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200418
Willy Tarreaua4380b42012-12-08 22:49:11 +0100419 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100420 if (ret != 0)
421 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200422 } while (ret != 0); /* binding NOK */
423 }
424 else {
Lukas Tribus7d56c6d2016-09-13 09:51:15 +0000425#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 Tarreaua4380b42012-12-08 22:49:11 +0100429 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100430 if (ret != 0)
431 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200432 }
433
Willy Tarreaua4380b42012-12-08 22:49:11 +0100434 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200435 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
436 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200437 close(fd);
438
Willy Tarreau9650f372009-08-16 14:02:45 +0200439 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100440 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200441 be->id);
442 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100443 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200444 be->id);
445 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100446 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200447 be->id);
448 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100449 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200450 be->id);
451 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100452 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200453 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 }
455 }
456
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400457#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 /* 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 Tarreauf0837b22012-11-24 10:24:27 +0100460 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200461 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200462 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900463 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200464#endif
465
Willy Tarreau163d4622015-10-13 16:16:41 +0200466#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 Tarreaue803de22010-01-21 17:43:04 +0100471 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 Tarreau819efbf2017-01-25 14:12:22 +0100477 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) {
478 if (errno == EINPROGRESS || errno == EALREADY) {
479 /* common case, let's wait for connect status */
480 conn->flags |= CO_FL_WAIT_L4_CONN;
481 }
482 else if (errno == EISCONN) {
483 /* should normally not happen but if so, indicates that it's OK */
484 conn->flags &= ~CO_FL_WAIT_L4_CONN;
485 }
486 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200487 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100488 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200489 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100490 conn->err_code = CO_ER_FREE_PORTS;
491 }
492 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200493 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100494 conn->err_code = CO_ER_ADDR_INUSE;
495 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200496
Willy Tarreaub1719512012-12-08 23:03:28 +0100497 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200498 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
499 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200500 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100501 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100502 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200503 return SF_ERR_RESOURCE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200504 } else if (errno == ETIMEDOUT) {
505 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200506 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
507 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200508 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100509 conn->err_code = CO_ER_SOCK_ERR;
510 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200511 return SF_ERR_SRVTO;
Willy Tarreau9650f372009-08-16 14:02:45 +0200512 } else {
513 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
514 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200515 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
516 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200517 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100518 conn->err_code = CO_ER_SOCK_ERR;
519 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200520 return SF_ERR_SRVCL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200521 }
522 }
Willy Tarreau819efbf2017-01-25 14:12:22 +0100523 else {
524 /* connect() == 0, this is great! */
525 conn->flags &= ~CO_FL_WAIT_L4_CONN;
526 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200527
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100528 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200529
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200530 /* Prepare to send a few handshakes related to the on-wire protocol. */
531 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200532 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200533
Willy Tarreauf79c8172013-10-21 16:30:56 +0200534 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100535 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200536
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200537 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200538 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100539 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200540 return SF_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200541 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200542
Willy Tarreau819efbf2017-01-25 14:12:22 +0100543 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN)) {
544 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
545 }
546 else {
547 /* If there's no more handshake, we need to notify the data
548 * layer when the connection is already OK otherwise we'll have
549 * no other opportunity to do it later (eg: health checks).
550 */
551 data = 1;
552 }
553
Willy Tarreau14f8e862012-08-30 22:23:13 +0200554 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200555 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200556
Willy Tarreaue7dff022015-04-03 01:14:29 +0200557 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau9650f372009-08-16 14:02:45 +0200558}
559
560
Willy Tarreau59b94792012-05-11 16:16:40 +0200561/*
562 * Retrieves the source address for the socket <fd>, with <dir> indicating
563 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
564 * success, -1 in case of error. The socket's source address is stored in
565 * <sa> for <salen> bytes.
566 */
567int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
568{
569 if (dir)
570 return getsockname(fd, sa, &salen);
571 else
572 return getpeername(fd, sa, &salen);
573}
574
575
576/*
577 * Retrieves the original destination address for the socket <fd>, with <dir>
578 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
579 * listener, if the original destination address was translated, the original
580 * address is retrieved. It returns 0 in case of success, -1 in case of error.
581 * The socket's source address is stored in <sa> for <salen> bytes.
582 */
583int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
584{
585 if (dir)
586 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100587 else {
588 int ret = getsockname(fd, sa, &salen);
589
590 if (ret < 0)
591 return ret;
592
Willy Tarreau59b94792012-05-11 16:16:40 +0200593#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100594 /* For TPROXY and Netfilter's NAT, we can retrieve the original
595 * IPv4 address before DNAT/REDIRECT. We must not do that with
596 * other families because v6-mapped IPv4 addresses are still
597 * reported as v4.
598 */
599 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
600 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
601 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200602#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100603 return ret;
604 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200605}
606
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200607/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100608 * receive shutdown. Returns positive if the shutdown was found, negative
609 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
610 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200611 */
612int tcp_drain(int fd)
613{
614 int turns = 2;
615 int len;
616
617 while (turns) {
618#ifdef MSG_TRUNC_CLEARS_INPUT
619 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
620 if (len == -1 && errno == EFAULT)
621#endif
622 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
623
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100624 if (len == 0) {
625 /* cool, shutdown received */
626 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200627 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100628 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200629
630 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100631 if (errno == EAGAIN) {
632 /* connection not closed yet */
633 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100634 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100635 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200636 if (errno == EINTR) /* oops, try again */
637 continue;
638 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100639 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200640 return 1;
641 }
642 /* OK we read some data, let's try again once */
643 turns--;
644 }
645 /* some data are still present, give up */
646 return 0;
647}
648
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200649/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100650 * and we have nothing to send. It updates the FD polling status. It returns 0
651 * if it fails in a fatal way or needs to poll to go further, otherwise it
652 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
653 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
654 * errno. The error checking is done in two passes in order to limit the number
655 * of syscalls in the normal case :
656 * - if POLL_ERR was reported by the poller, we check for a pending error on
657 * the socket before proceeding. If found, it's assigned to errno so that
658 * upper layers can see it.
659 * - otherwise connect() is used to check the connection state again, since
660 * the getsockopt return cannot reliably be used to know if the connection
661 * is still pending or ready. This one may often return an error as well,
662 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200663 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200664int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200665{
Willy Tarreau239d7182012-07-23 18:53:03 +0200666 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100667 socklen_t lskerr;
668 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200669
Willy Tarreau80184712012-07-06 14:54:49 +0200670 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200671 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200672
Willy Tarreau3c728722014-01-23 13:50:42 +0100673 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200674 return 0;
675
Willy Tarreau80184712012-07-06 14:54:49 +0200676 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200677 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200678
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100679 if (!fd_send_ready(fd))
680 return 0;
681
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100682 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
683 * without FD_POLL_IN also indicates a hangup without input data meaning
684 * there was no connection.
685 */
686 if (fdtab[fd].ev & FD_POLL_ERR ||
687 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
688 skerr = 0;
689 lskerr = sizeof(skerr);
690 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
691 errno = skerr;
692 if (errno == EAGAIN)
693 errno = 0;
694 if (errno)
695 goto out_error;
696 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200697
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100698 /* Use connect() to check the state of the socket. This has the
699 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200700 * - error
701 * - connecting (EALREADY, EINPROGRESS)
702 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200703 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200704 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200705 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100706 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100707 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200708 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200709 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200710
Willy Tarreau2c6be842012-07-06 17:12:34 +0200711 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200712 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200713
Willy Tarreau2c6be842012-07-06 17:12:34 +0200714 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200715 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200716
Willy Tarreau076be252012-07-06 16:02:29 +0200717 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200718 * forward the event to the transport layer which will notify the
719 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200720 */
Willy Tarreau80184712012-07-06 14:54:49 +0200721 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200722 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200723
724 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200725 /* Write error on the file descriptor. Report it to the connection
726 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200727 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100728 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100729 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100730 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200731 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200732}
733
Willy Tarreau59b94792012-05-11 16:16:40 +0200734
Willy Tarreaue6b98942007-10-29 01:09:36 +0100735/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100736 * an error message in <errmsg> if the message is at most <errlen> bytes long
737 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
738 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100739 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
740 * was alright and that no message was returned. ERR_RETRYABLE means that an
741 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700742 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100743 * the meaning of the error, but just indicate that a message is present which
744 * should be displayed with the respective level. Last, ERR_ABORT indicates
745 * that it's pointless to try to start other listeners. No error message is
746 * returned if errlen is NULL.
747 */
748int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
749{
750 __label__ tcp_return, tcp_close_return;
751 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100752 int ext, ready;
753 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100754 const char *msg = NULL;
755
756 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100757 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100758 *errmsg = 0;
759
760 if (listener->state != LI_ASSIGNED)
761 return ERR_NONE; /* already bound */
762
763 err = ERR_NONE;
764
Willy Tarreau40aa0702013-03-10 23:51:38 +0100765 /* if the listener already has an fd assigned, then we were offered the
766 * fd by an external process (most likely the parent), and we don't want
767 * to create a new socket. However we still want to set a few flags on
768 * the socket.
769 */
770 fd = listener->fd;
771 ext = (fd >= 0);
772
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100773 if (!ext) {
774 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
775
776 if (fd == -1) {
777 err |= ERR_RETRYABLE | ERR_ALERT;
778 msg = "cannot create listening socket";
779 goto tcp_return;
780 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100781 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100782
Willy Tarreaue6b98942007-10-29 01:09:36 +0100783 if (fd >= global.maxsock) {
784 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
785 msg = "not enough free sockets (raise '-n' parameter)";
786 goto tcp_close_return;
787 }
788
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200789 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100790 err |= ERR_FATAL | ERR_ALERT;
791 msg = "cannot make socket non-blocking";
792 goto tcp_close_return;
793 }
794
Willy Tarreau40aa0702013-03-10 23:51:38 +0100795 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100796 /* not fatal but should be reported */
797 msg = "cannot do so_reuseaddr";
798 err |= ERR_ALERT;
799 }
800
801 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900802 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100803
Willy Tarreaue6b98942007-10-29 01:09:36 +0100804#ifdef SO_REUSEPORT
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000805 /* OpenBSD and Linux 3.9 support this. As it's present in old libc versions of
806 * Linux, it might return an error that we will silently ignore.
Willy Tarreaue6b98942007-10-29 01:09:36 +0100807 */
Lukas Tribusa0bcbdc2016-09-12 21:42:20 +0000808 if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT))
Willy Tarreau40aa0702013-03-10 23:51:38 +0100809 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100810#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200811
Willy Tarreau40aa0702013-03-10 23:51:38 +0100812 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200813 switch (listener->addr.ss_family) {
814 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200815 if (1
816#if defined(IP_TRANSPARENT)
817 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
818#endif
819#if defined(IP_FREEBIND)
820 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
821#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200822#if defined(IP_BINDANY)
823 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
824#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200825#if defined(SO_BINDANY)
826 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
827#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200828 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200829 msg = "cannot make listening socket transparent";
830 err |= ERR_ALERT;
831 }
832 break;
833 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200834 if (1
Dinko Korunic7276f3a2016-09-09 09:41:15 +0200835#if defined(IPV6_TRANSPARENT) && defined(SOL_IPV6)
Pieter Baauwd551fb52013-05-08 22:49:23 +0200836 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
837#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100838#if defined(IP_FREEBIND)
839 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
840#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200841#if defined(IPV6_BINDANY)
842 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
843#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200844#if defined(SO_BINDANY)
845 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
846#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200847 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200848 msg = "cannot make listening socket transparent";
849 err |= ERR_ALERT;
850 }
851 break;
852 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100853 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200854
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100855#ifdef SO_BINDTODEVICE
856 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100857 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100858 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100859 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100860 msg = "cannot bind listener to device";
861 err |= ERR_WARN;
862 }
863 }
864#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400865#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100866 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400867 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200868 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
869 msg = "cannot set MSS";
870 err |= ERR_WARN;
871 }
872 }
873#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100874#if defined(TCP_USER_TIMEOUT)
875 if (listener->tcp_ut) {
876 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
877 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
878 msg = "cannot set TCP User Timeout";
879 err |= ERR_WARN;
880 }
881 }
882#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200883#if defined(TCP_DEFER_ACCEPT)
884 if (listener->options & LI_O_DEF_ACCEPT) {
885 /* defer accept by up to one second */
886 int accept_delay = 1;
887 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
888 msg = "cannot enable DEFER_ACCEPT";
889 err |= ERR_WARN;
890 }
891 }
892#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200893#if defined(TCP_FASTOPEN)
894 if (listener->options & LI_O_TCP_FO) {
895 /* TFO needs a queue length, let's use the configured backlog */
896 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
897 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
898 msg = "cannot enable TCP_FASTOPEN";
899 err |= ERR_WARN;
900 }
901 }
902#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100903#if defined(IPV6_V6ONLY)
904 if (listener->options & LI_O_V6ONLY)
905 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100906 else if (listener->options & LI_O_V4V6)
907 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100908#endif
909
Willy Tarreau40aa0702013-03-10 23:51:38 +0100910 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100911 err |= ERR_RETRYABLE | ERR_ALERT;
912 msg = "cannot bind socket";
913 goto tcp_close_return;
914 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100915
Willy Tarreau40aa0702013-03-10 23:51:38 +0100916 ready = 0;
917 ready_len = sizeof(ready);
918 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
919 ready = 0;
920
921 if (!(ext && ready) && /* only listen if not already done by external process */
922 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100923 err |= ERR_RETRYABLE | ERR_ALERT;
924 msg = "cannot listen to socket";
925 goto tcp_close_return;
926 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100927
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400928#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200929 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900930 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200931#endif
932
Willy Tarreaue6b98942007-10-29 01:09:36 +0100933 /* the socket is ready */
934 listener->fd = fd;
935 listener->state = LI_LISTEN;
936
Willy Tarreaueabf3132008-08-29 23:36:51 +0200937 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200938 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200939 fd_insert(fd);
940
Willy Tarreaue6b98942007-10-29 01:09:36 +0100941 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100942 if (msg && errlen) {
943 char pn[INET6_ADDRSTRLEN];
944
Willy Tarreau631f01c2011-09-05 00:36:48 +0200945 addr_to_str(&listener->addr, pn, sizeof(pn));
946 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100947 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100948 return err;
949
950 tcp_close_return:
951 close(fd);
952 goto tcp_return;
953}
954
955/* This function creates all TCP sockets bound to the protocol entry <proto>.
956 * It is intended to be used as the protocol's bind_all() function.
957 * The sockets will be registered but not added to any fd_set, in order not to
958 * loose them across the fork(). A call to enable_all_listeners() is needed
959 * to complete initialization. The return value is composed from ERR_*.
960 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200961static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100962{
963 struct listener *listener;
964 int err = ERR_NONE;
965
966 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200967 err |= tcp_bind_listener(listener, errmsg, errlen);
968 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100969 break;
970 }
971
972 return err;
973}
974
975/* Add listener to the list of tcpv4 listeners. The listener's state
976 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
977 * listeners is updated. This is the function to use to add a new listener.
978 */
979void tcpv4_add_listener(struct listener *listener)
980{
981 if (listener->state != LI_INIT)
982 return;
983 listener->state = LI_ASSIGNED;
984 listener->proto = &proto_tcpv4;
985 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
986 proto_tcpv4.nb_listeners++;
987}
988
989/* Add listener to the list of tcpv4 listeners. The listener's state
990 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
991 * listeners is updated. This is the function to use to add a new listener.
992 */
993void tcpv6_add_listener(struct listener *listener)
994{
995 if (listener->state != LI_INIT)
996 return;
997 listener->state = LI_ASSIGNED;
998 listener->proto = &proto_tcpv6;
999 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
1000 proto_tcpv6.nb_listeners++;
1001}
1002
Willy Tarreau092d8652014-07-07 20:22:12 +02001003/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
1004 * was totally stopped, or > 0 if correctly paused.
1005 */
1006int tcp_pause_listener(struct listener *l)
1007{
1008 if (shutdown(l->fd, SHUT_WR) != 0)
1009 return -1; /* Solaris dies here */
1010
1011 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1012 return -1; /* OpenBSD dies here */
1013
1014 if (shutdown(l->fd, SHUT_RD) != 0)
1015 return -1; /* should always be OK */
1016 return 1;
1017}
1018
William Lallemand2e785f22016-05-25 01:48:42 +02001019/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001020 * Execute the "set-src" action. May be called from {tcp,http}request.
1021 * It only changes the address and tries to preserve the original port. If the
1022 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand2e785f22016-05-25 01:48:42 +02001023 */
1024enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px,
1025 struct session *sess, struct stream *s, int flags)
1026{
1027 struct connection *cli_conn;
1028
1029 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1030 struct sample *smp;
1031
1032 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1033 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001034 int port = get_net_port(&cli_conn->addr.from);
1035
William Lallemand2e785f22016-05-25 01:48:42 +02001036 if (smp->data.type == SMP_T_IPV4) {
1037 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
1038 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Willy Tarreau00005ce2016-10-21 15:07:45 +02001039 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001040 } else if (smp->data.type == SMP_T_IPV6) {
1041 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
1042 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Willy Tarreau00005ce2016-10-21 15:07:45 +02001043 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = port;
William Lallemand2e785f22016-05-25 01:48:42 +02001044 }
1045 }
William Lallemand01252ed2016-05-25 02:33:16 +02001046 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
William Lallemand2e785f22016-05-25 01:48:42 +02001047 }
1048 return ACT_RET_CONT;
1049}
1050
William Lallemand44be6402016-05-25 01:51:35 +02001051/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001052 * Execute the "set-dst" action. May be called from {tcp,http}request.
1053 * It only changes the address and tries to preserve the original port. If the
1054 * previous family was neither AF_INET nor AF_INET6, the port is set to zero.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001055 */
1056enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px,
1057 struct session *sess, struct stream *s, int flags)
1058{
1059 struct connection *cli_conn;
1060
1061 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1062 struct sample *smp;
1063
1064 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
1065 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001066 int port = get_net_port(&cli_conn->addr.to);
1067
William Lallemand13e9b0c2016-05-25 02:34:07 +02001068 if (smp->data.type == SMP_T_IPV4) {
1069 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_family = AF_INET;
1070 ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
1071 } else if (smp->data.type == SMP_T_IPV6) {
1072 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_family = AF_INET6;
1073 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Willy Tarreau00005ce2016-10-21 15:07:45 +02001074 ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_port = port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001075 }
1076 cli_conn->flags |= CO_FL_ADDR_TO_SET;
1077 }
1078 }
1079 return ACT_RET_CONT;
1080}
1081
1082/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001083 * Execute the "set-src-port" action. May be called from {tcp,http}request.
1084 * We must test the sin_family before setting the port. If the address family
1085 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1086 * and the port is assigned.
William Lallemand44be6402016-05-25 01:51:35 +02001087 */
1088enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px,
1089 struct session *sess, struct stream *s, int flags)
1090{
1091 struct connection *cli_conn;
1092
1093 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1094 struct sample *smp;
1095
1096 conn_get_from_addr(cli_conn);
1097
1098 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1099 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001100 if (cli_conn->addr.from.ss_family == AF_INET6) {
William Lallemand44be6402016-05-25 01:51:35 +02001101 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
Willy Tarreau00005ce2016-10-21 15:07:45 +02001102 } else {
1103 if (cli_conn->addr.from.ss_family != AF_INET) {
1104 cli_conn->addr.from.ss_family = AF_INET;
1105 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1106 }
1107 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand44be6402016-05-25 01:51:35 +02001108 }
1109 }
1110 }
1111 return ACT_RET_CONT;
1112}
1113
William Lallemand13e9b0c2016-05-25 02:34:07 +02001114/*
Willy Tarreau00005ce2016-10-21 15:07:45 +02001115 * Execute the "set-dst-port" action. May be called from {tcp,http}request.
1116 * We must test the sin_family before setting the port. If the address family
1117 * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0"
1118 * and the port is assigned.
William Lallemand13e9b0c2016-05-25 02:34:07 +02001119 */
1120enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px,
1121 struct session *sess, struct stream *s, int flags)
1122{
1123 struct connection *cli_conn;
1124
1125 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
1126 struct sample *smp;
1127
1128 conn_get_to_addr(cli_conn);
1129
1130 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
1131 if (smp) {
Willy Tarreau00005ce2016-10-21 15:07:45 +02001132 if (cli_conn->addr.from.ss_family == AF_INET6) {
1133 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = htons(smp->data.u.sint);
1134 } else {
1135 if (cli_conn->addr.from.ss_family != AF_INET) {
1136 cli_conn->addr.from.ss_family = AF_INET;
1137 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = 0;
1138 }
1139 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = htons(smp->data.u.sint);
William Lallemand13e9b0c2016-05-25 02:34:07 +02001140 }
1141 }
1142 }
1143 return ACT_RET_CONT;
1144}
1145
Willy Tarreau2d392c22015-08-24 01:43:45 +02001146/* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */
1147static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *strm, int flags)
1148{
1149 struct connection *conn = objt_conn(sess->origin);
1150
1151 if (!conn)
1152 goto out;
1153
1154 if (!conn_ctrl_ready(conn))
1155 goto out;
1156
Willy Tarreau2d392c22015-08-24 01:43:45 +02001157#ifdef TCP_QUICKACK
Willy Tarreaufc2a2d92015-09-29 18:15:01 +02001158 /* drain is needed only to send the quick ACK */
1159 conn_sock_drain(conn);
1160
Willy Tarreau2d392c22015-08-24 01:43:45 +02001161 /* re-enable quickack if it was disabled to ack all data and avoid
1162 * retransmits from the client that might trigger a real reset.
1163 */
1164 setsockopt(conn->t.sock.fd, SOL_TCP, TCP_QUICKACK, &one, sizeof(one));
1165#endif
1166 /* lingering must absolutely be disabled so that we don't send a
1167 * shutdown(), this is critical to the TCP_REPAIR trick. When no stream
1168 * is present, returning with ERR will cause lingering to be disabled.
1169 */
1170 if (strm)
1171 strm->si[0].flags |= SI_FL_NOLINGER;
1172
Willy Tarreauf50ec0f2015-09-29 18:11:32 +02001173 /* We're on the client-facing side, we must force to disable lingering to
1174 * ensure we will use an RST exclusively and kill any pending data.
1175 */
1176 fdtab[conn->t.sock.fd].linger_risk = 1;
1177
Willy Tarreau2d392c22015-08-24 01:43:45 +02001178#ifdef TCP_REPAIR
1179 if (setsockopt(conn->t.sock.fd, SOL_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) {
1180 /* socket will be quiet now */
1181 goto out;
1182 }
1183#endif
1184 /* either TCP_REPAIR is not defined or it failed (eg: permissions).
1185 * Let's fall back on the TTL trick, though it only works for routed
1186 * network and has no effect on local net.
1187 */
1188#ifdef IP_TTL
1189 setsockopt(conn->t.sock.fd, SOL_IP, IP_TTL, &one, sizeof(one));
1190#endif
1191 out:
1192 /* kill the stream if any */
1193 if (strm) {
1194 channel_abort(&strm->req);
1195 channel_abort(&strm->res);
1196 strm->req.analysers = 0;
1197 strm->res.analysers = 0;
1198 strm->be->be_counters.denied_req++;
1199 if (!(strm->flags & SF_ERR_MASK))
1200 strm->flags |= SF_ERR_PRXCOND;
1201 if (!(strm->flags & SF_FINST_MASK))
1202 strm->flags |= SF_FINST_R;
1203 }
1204
1205 sess->fe->fe_counters.denied_req++;
1206 if (sess->listener->counters)
1207 sess->listener->counters->denied_req++;
1208
1209 return ACT_RET_STOP;
1210}
1211
William Lallemand13e9b0c2016-05-25 02:34:07 +02001212/* parse "set-{src,dst}[-port]" action */
1213enum act_parse_ret tcp_parse_set_src_dst(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
William Lallemand2e785f22016-05-25 01:48:42 +02001214{
1215 int cur_arg;
1216 struct sample_expr *expr;
1217 unsigned int where;
1218
1219 cur_arg = *orig_arg;
1220 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
1221 if (!expr)
1222 return ACT_RET_PRS_ERR;
1223
1224 where = 0;
1225 if (proxy->cap & PR_CAP_FE)
1226 where |= SMP_VAL_FE_HRQ_HDR;
1227 if (proxy->cap & PR_CAP_BE)
1228 where |= SMP_VAL_BE_HRQ_HDR;
1229
1230 if (!(expr->fetch->val & where)) {
1231 memprintf(err,
1232 "fetch method '%s' extracts information from '%s', none of which is available here",
1233 args[cur_arg-1], sample_src_names(expr->fetch->use));
1234 free(expr);
1235 return ACT_RET_PRS_ERR;
1236 }
1237 rule->arg.expr = expr;
1238 rule->action = ACT_CUSTOM;
1239
1240 if (!strcmp(args[*orig_arg-1], "set-src")) {
1241 rule->action_ptr = tcp_action_req_set_src;
William Lallemand44be6402016-05-25 01:51:35 +02001242 } else if (!strcmp(args[*orig_arg-1], "set-src-port")) {
1243 rule->action_ptr = tcp_action_req_set_src_port;
William Lallemand13e9b0c2016-05-25 02:34:07 +02001244 } else if (!strcmp(args[*orig_arg-1], "set-dst")) {
1245 rule->action_ptr = tcp_action_req_set_dst;
1246 } else if (!strcmp(args[*orig_arg-1], "set-dst-port")) {
1247 rule->action_ptr = tcp_action_req_set_dst_port;
William Lallemand2e785f22016-05-25 01:48:42 +02001248 } else {
1249 return ACT_RET_PRS_ERR;
1250 }
1251
1252 (*orig_arg)++;
1253
1254 return ACT_RET_PRS_OK;
1255}
1256
1257
Willy Tarreau2d392c22015-08-24 01:43:45 +02001258/* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on
1259 * success, ACT_RET_PRS_ERR on error.
1260 */
1261static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px,
1262 struct act_rule *rule, char **err)
1263{
1264 rule->action = ACT_CUSTOM;
1265 rule->action_ptr = tcp_exec_action_silent_drop;
1266 return ACT_RET_PRS_OK;
1267}
1268
Willy Tarreau645513a2010-05-24 20:55:15 +02001269
1270/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001271/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001272/************************************************************************/
1273
Willy Tarreau4a129812012-04-25 17:31:42 +02001274/* fetch the connection's source IPv4/IPv6 address */
Thierry FOURNIERa123ad82015-07-24 09:12:15 +02001275int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001276{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001277 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001278
1279 if (!cli_conn)
1280 return 0;
1281
1282 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001283 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001284 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001285 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001286 break;
1287 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001288 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001289 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001290 break;
1291 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001292 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001293 }
Emeric Brunf769f512010-10-22 17:14:01 +02001294
Willy Tarreau37406352012-04-23 16:16:37 +02001295 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001296 return 1;
1297}
1298
Willy Tarreaua5e37562011-12-16 17:06:15 +01001299/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001300static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001301smp_fetch_sport(const struct arg *args, struct sample *smp, const char *k, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001302{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001303 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001304
1305 if (!cli_conn)
1306 return 0;
1307
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001308 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001309 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001310 return 0;
1311
Willy Tarreau37406352012-04-23 16:16:37 +02001312 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001313 return 1;
1314}
1315
Willy Tarreau4a129812012-04-25 17:31:42 +02001316/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001317static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001318smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001319{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001320 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreau645513a2010-05-24 20:55:15 +02001321
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001322 if (!cli_conn)
1323 return 0;
1324
1325 conn_get_to_addr(cli_conn);
1326
1327 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001328 case AF_INET:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001329 smp->data.u.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001330 smp->data.type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001331 break;
1332 case AF_INET6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001333 smp->data.u.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001334 smp->data.type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001335 break;
1336 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001337 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001338 }
Emeric Brunf769f512010-10-22 17:14:01 +02001339
Willy Tarreau37406352012-04-23 16:16:37 +02001340 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001341 return 1;
1342}
1343
Willy Tarreau16e01562016-08-09 16:46:18 +02001344/* check if the destination address of the front connection is local to the
1345 * system or if it was intercepted.
1346 */
1347int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1348{
1349 struct connection *conn = objt_conn(smp->sess->origin);
1350 struct listener *li = smp->sess->listener;
1351
1352 if (!conn)
1353 return 0;
1354
1355 conn_get_to_addr(conn);
1356 if (!(conn->flags & CO_FL_ADDR_TO_SET))
1357 return 0;
1358
1359 smp->data.type = SMP_T_BOOL;
1360 smp->flags = 0;
1361 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.to);
1362 return smp->data.u.sint >= 0;
1363}
1364
1365/* check if the source address of the front connection is local to the system
1366 * or not.
1367 */
1368int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
1369{
1370 struct connection *conn = objt_conn(smp->sess->origin);
1371 struct listener *li = smp->sess->listener;
1372
1373 if (!conn)
1374 return 0;
1375
1376 conn_get_from_addr(conn);
1377 if (!(conn->flags & CO_FL_ADDR_FROM_SET))
1378 return 0;
1379
1380 smp->data.type = SMP_T_BOOL;
1381 smp->flags = 0;
1382 smp->data.u.sint = addr_is_local(li->netns, &conn->addr.from);
1383 return smp->data.u.sint >= 0;
1384}
1385
Willy Tarreaua5e37562011-12-16 17:06:15 +01001386/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001387static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001388smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau645513a2010-05-24 20:55:15 +02001389{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001390 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001391
1392 if (!cli_conn)
1393 return 0;
1394
1395 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001396
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001397 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001398 if (!(smp->data.u.sint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001399 return 0;
1400
Willy Tarreau37406352012-04-23 16:16:37 +02001401 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001402 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001403}
1404
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001405#ifdef TCP_INFO
1406
1407/* Returns some tcp_info data is its avalaible. "dir" must be set to 0 if
1408 * the client connection is require, otherwise it is set to 1. "val" represents
1409 * the required value. Use 0 for rtt and 1 for rttavg. "unit" is the expected unit
1410 * by default, the rtt is in us. Id "unit" is set to 0, the unit is us, if it is
1411 * set to 1, the untis are milliseconds.
1412 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
1413 */
1414static inline int get_tcp_info(const struct arg *args, struct sample *smp,
1415 int dir, int val)
1416{
1417 struct connection *conn;
1418 struct tcp_info info;
1419 socklen_t optlen;
1420
1421 /* strm can be null. */
1422 if (!smp->strm)
1423 return 0;
1424
1425 /* get the object associated with the stream interface.The
1426 * object can be other thing than a connection. For example,
1427 * it be a appctx. */
1428 conn = objt_conn(smp->strm->si[dir].end);
1429 if (!conn)
1430 return 0;
1431
1432 /* The fd may not be avalaible for the tcp_info struct, and the
1433 syscal can fail. */
1434 optlen = sizeof(info);
1435 if (getsockopt(conn->t.sock.fd, SOL_TCP, TCP_INFO, &info, &optlen) == -1)
1436 return 0;
1437
1438 /* extract the value. */
1439 smp->data.type = SMP_T_SINT;
1440 switch (val) {
Joe Williams30fcd392016-08-10 07:06:44 -07001441 case 0: smp->data.u.sint = info.tcpi_rtt; break;
1442 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
1443#if defined(__linux__)
1444 /* these ones are common to all Linux versions */
1445 case 2: smp->data.u.sint = info.tcpi_unacked; break;
1446 case 3: smp->data.u.sint = info.tcpi_sacked; break;
1447 case 4: smp->data.u.sint = info.tcpi_lost; break;
1448 case 5: smp->data.u.sint = info.tcpi_retrans; break;
1449 case 6: smp->data.u.sint = info.tcpi_fackets; break;
1450 case 7: smp->data.u.sint = info.tcpi_reordering; break;
1451#elif defined(__FreeBSD__) || defined(__NetBSD__)
1452 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
1453 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
1454 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
1455 case 4: smp->data.u.sint = info.__tcpi_lost; break;
1456 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
1457 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
1458 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
1459#endif
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001460 default: return 0;
1461 }
1462
1463 /* Convert the value as expected. */
1464 if (args) {
1465 if (args[0].type == ARGT_STR) {
1466 if (strcmp(args[0].data.str.str, "us") == 0) {
1467 /* Do nothing. */
1468 } else if (strcmp(args[0].data.str.str, "ms") == 0) {
1469 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1470 } else
1471 return 0;
1472 } else if (args[0].type == ARGT_STOP) {
1473 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
1474 } else
1475 return 0;
1476 }
1477
1478 return 1;
1479}
1480
1481/* get the mean rtt of a client connexion */
1482static int
1483smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1484{
1485 if (!get_tcp_info(args, smp, 0, 0))
1486 return 0;
1487 return 1;
1488}
1489
1490/* get the variance of the mean rtt of a client connexion */
1491static int
1492smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
1493{
1494 if (!get_tcp_info(args, smp, 0, 1))
1495 return 0;
1496 return 1;
1497}
Joe Williams30fcd392016-08-10 07:06:44 -07001498
1499#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1500
1501/* get the unacked counter on a client connexion */
1502static int
1503smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1504{
1505 if (!get_tcp_info(args, smp, 0, 2))
1506 return 0;
1507 return 1;
1508}
1509
1510/* get the sacked counter on a client connexion */
1511static int
1512smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1513{
1514 if (!get_tcp_info(args, smp, 0, 3))
1515 return 0;
1516 return 1;
1517}
1518
1519/* get the lost counter on a client connexion */
1520static int
1521smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
1522{
1523 if (!get_tcp_info(args, smp, 0, 4))
1524 return 0;
1525 return 1;
1526}
1527
1528/* get the retrans counter on a client connexion */
1529static int
1530smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
1531{
1532 if (!get_tcp_info(args, smp, 0, 5))
1533 return 0;
1534 return 1;
1535}
1536
1537/* get the fackets counter on a client connexion */
1538static int
1539smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
1540{
1541 if (!get_tcp_info(args, smp, 0, 6))
1542 return 0;
1543 return 1;
1544}
1545
1546/* get the reordering counter on a client connexion */
1547static int
1548smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
1549{
1550 if (!get_tcp_info(args, smp, 0, 7))
1551 return 0;
1552 return 1;
1553}
1554#endif // linux || freebsd || netbsd
1555#endif // TCP_INFO
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001556
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001557#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001558/* parse the "v4v6" bind keyword */
1559static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1560{
1561 struct listener *l;
1562
1563 list_for_each_entry(l, &conf->listeners, by_bind) {
1564 if (l->addr.ss_family == AF_INET6)
1565 l->options |= LI_O_V4V6;
1566 }
1567
1568 return 0;
1569}
1570
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001571/* parse the "v6only" bind keyword */
1572static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1573{
1574 struct listener *l;
1575
1576 list_for_each_entry(l, &conf->listeners, by_bind) {
1577 if (l->addr.ss_family == AF_INET6)
1578 l->options |= LI_O_V6ONLY;
1579 }
1580
1581 return 0;
1582}
1583#endif
1584
Pieter Baauwd551fb52013-05-08 22:49:23 +02001585#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001586/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001587static int bind_parse_transparent(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001588{
1589 struct listener *l;
1590
Willy Tarreau4348fad2012-09-20 16:48:07 +02001591 list_for_each_entry(l, &conf->listeners, by_bind) {
1592 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1593 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001594 }
1595
Willy Tarreau44791242012-09-12 23:27:21 +02001596 return 0;
1597}
1598#endif
1599
1600#ifdef TCP_DEFER_ACCEPT
1601/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001602static int bind_parse_defer_accept(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001603{
1604 struct listener *l;
1605
Willy Tarreau4348fad2012-09-20 16:48:07 +02001606 list_for_each_entry(l, &conf->listeners, by_bind) {
1607 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1608 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001609 }
1610
Willy Tarreau44791242012-09-12 23:27:21 +02001611 return 0;
1612}
1613#endif
1614
Willy Tarreau1c862c52012-10-05 16:21:00 +02001615#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001616/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001617static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1618{
1619 struct listener *l;
1620
1621 list_for_each_entry(l, &conf->listeners, by_bind) {
1622 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1623 l->options |= LI_O_TCP_FO;
1624 }
1625
1626 return 0;
1627}
1628#endif
1629
Willy Tarreau44791242012-09-12 23:27:21 +02001630#ifdef TCP_MAXSEG
1631/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001632static int bind_parse_mss(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001633{
1634 struct listener *l;
1635 int mss;
1636
Willy Tarreau44791242012-09-12 23:27:21 +02001637 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001638 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001639 return ERR_ALERT | ERR_FATAL;
1640 }
1641
1642 mss = atoi(args[cur_arg + 1]);
1643 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001644 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001645 return ERR_ALERT | ERR_FATAL;
1646 }
1647
Willy Tarreau4348fad2012-09-20 16:48:07 +02001648 list_for_each_entry(l, &conf->listeners, by_bind) {
1649 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1650 l->maxseg = mss;
1651 }
Willy Tarreau44791242012-09-12 23:27:21 +02001652
1653 return 0;
1654}
1655#endif
1656
Willy Tarreau2af207a2015-02-04 00:45:58 +01001657#ifdef TCP_USER_TIMEOUT
1658/* parse the "tcp-ut" bind keyword */
1659static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1660{
1661 const char *ptr = NULL;
1662 struct listener *l;
1663 unsigned int timeout;
1664
1665 if (!*args[cur_arg + 1]) {
1666 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
1667 return ERR_ALERT | ERR_FATAL;
1668 }
1669
1670 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
1671 if (ptr) {
1672 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
1673 return ERR_ALERT | ERR_FATAL;
1674 }
1675
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->tcp_ut = timeout;
1679 }
1680
1681 return 0;
1682}
1683#endif
1684
Willy Tarreau44791242012-09-12 23:27:21 +02001685#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01001686/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001687static int bind_parse_interface(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001688{
1689 struct listener *l;
1690
Willy Tarreau44791242012-09-12 23:27:21 +02001691 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001692 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001693 return ERR_ALERT | ERR_FATAL;
1694 }
1695
Willy Tarreau4348fad2012-09-20 16:48:07 +02001696 list_for_each_entry(l, &conf->listeners, by_bind) {
1697 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1698 l->interface = strdup(args[cur_arg + 1]);
1699 }
Willy Tarreau44791242012-09-12 23:27:21 +02001700
1701 global.last_checks |= LSTCHK_NETADM;
1702 return 0;
1703}
1704#endif
1705
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001706#ifdef CONFIG_HAP_NS
1707/* parse the "namespace" bind keyword */
1708static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1709{
1710 struct listener *l;
1711 char *namespace = NULL;
1712
1713 if (!*args[cur_arg + 1]) {
1714 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
1715 return ERR_ALERT | ERR_FATAL;
1716 }
1717 namespace = args[cur_arg + 1];
1718
1719 list_for_each_entry(l, &conf->listeners, by_bind) {
1720 l->netns = netns_store_lookup(namespace, strlen(namespace));
1721
1722 if (l->netns == NULL)
1723 l->netns = netns_store_insert(namespace);
1724
1725 if (l->netns == NULL) {
1726 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
1727 return ERR_ALERT | ERR_FATAL;
1728 }
1729 }
1730 return 0;
1731}
1732#endif
1733
Willy Tarreau163d4622015-10-13 16:16:41 +02001734#ifdef TCP_USER_TIMEOUT
1735/* parse the "tcp-ut" server keyword */
1736static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1737{
1738 const char *ptr = NULL;
1739 unsigned int timeout;
1740
1741 if (!*args[*cur_arg + 1]) {
1742 memprintf(err, "'%s' : missing TCP User Timeout value", args[*cur_arg]);
1743 return ERR_ALERT | ERR_FATAL;
1744 }
1745
1746 ptr = parse_time_err(args[*cur_arg + 1], &timeout, TIME_UNIT_MS);
1747 if (ptr) {
1748 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[*cur_arg]);
1749 return ERR_ALERT | ERR_FATAL;
1750 }
1751
1752 if (newsrv->addr.ss_family == AF_INET || newsrv->addr.ss_family == AF_INET6)
1753 newsrv->tcp_ut = timeout;
1754
1755 return 0;
1756}
1757#endif
1758
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001759
Willy Tarreau4a129812012-04-25 17:31:42 +02001760/* Note: must not be declared <const> as its list will be overwritten.
1761 * Note: fetches that may return multiple types must be declared as the lowest
1762 * common denominator, the type that can be casted into all other ones. For
1763 * instance v4/v6 must be declared v4.
1764 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001765static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001766 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001767 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001768 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001769 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
Willy Tarreau16e01562016-08-09 16:46:18 +02001770 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001771 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Thierry Fournier / OZON.IO6310bef2016-07-24 20:16:50 +02001772#ifdef TCP_INFO
Joe Williams30fcd392016-08-10 07:06:44 -07001773 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1774 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1775#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
1776 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1777 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1778 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1779 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1780 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1781 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_L4CLI },
1782#endif // linux || freebsd || netbsd
1783#endif // TCP_INFO
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001784 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001785}};
1786
Willy Tarreau44791242012-09-12 23:27:21 +02001787/************************************************************************/
1788/* All supported bind keywords must be declared here. */
1789/************************************************************************/
1790
1791/* Note: must not be declared <const> as its list will be overwritten.
1792 * Please take care of keeping this list alphabetically sorted, doing so helps
1793 * all code contributors.
1794 * Optional keywords are also declared with a NULL ->parse() function so that
1795 * the config parser can report an appropriate error when a known keyword was
1796 * not enabled.
1797 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001798static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001799#ifdef TCP_DEFER_ACCEPT
1800 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1801#endif
1802#ifdef SO_BINDTODEVICE
1803 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1804#endif
1805#ifdef TCP_MAXSEG
1806 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1807#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01001808#ifdef TCP_USER_TIMEOUT
1809 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
1810#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001811#ifdef TCP_FASTOPEN
1812 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1813#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001814#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001815 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1816#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001817#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001818 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001819 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1820#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001821#ifdef CONFIG_HAP_NS
1822 { "namespace", bind_parse_namespace, 1 },
1823#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001824 /* the versions with the NULL parse function*/
1825 { "defer-accept", NULL, 0 },
1826 { "interface", NULL, 1 },
1827 { "mss", NULL, 1 },
1828 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001829 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001830 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001831 { NULL, NULL, 0 },
1832}};
1833
Willy Tarreau163d4622015-10-13 16:16:41 +02001834static struct srv_kw_list srv_kws = { "TCP", { }, {
1835#ifdef TCP_USER_TIMEOUT
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001836 { "tcp-ut", srv_parse_tcp_ut, 1, 1 }, /* set TCP user timeout on server */
Willy Tarreau163d4622015-10-13 16:16:41 +02001837#endif
1838 { NULL, NULL, 0 },
1839}};
Willy Tarreau2d392c22015-08-24 01:43:45 +02001840
1841static struct action_kw_list tcp_req_conn_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02001842 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02001843 { "set-src", tcp_parse_set_src_dst },
1844 { "set-src-port", tcp_parse_set_src_dst },
1845 { "set-dst" , tcp_parse_set_src_dst },
1846 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02001847 { /* END */ }
1848}};
1849
Willy Tarreau620408f2016-10-21 16:37:51 +02001850static struct action_kw_list tcp_req_sess_actions = {ILH, {
1851 { "silent-drop", tcp_parse_silent_drop },
1852 { "set-src", tcp_parse_set_src_dst },
1853 { "set-src-port", tcp_parse_set_src_dst },
1854 { "set-dst" , tcp_parse_set_src_dst },
1855 { "set-dst-port", tcp_parse_set_src_dst },
1856 { /* END */ }
1857}};
1858
Willy Tarreau2d392c22015-08-24 01:43:45 +02001859static struct action_kw_list tcp_req_cont_actions = {ILH, {
1860 { "silent-drop", tcp_parse_silent_drop },
1861 { /* END */ }
1862}};
1863
1864static struct action_kw_list tcp_res_cont_actions = {ILH, {
1865 { "silent-drop", tcp_parse_silent_drop },
1866 { /* END */ }
1867}};
1868
1869static struct action_kw_list http_req_actions = {ILH, {
William Lallemand2e785f22016-05-25 01:48:42 +02001870 { "silent-drop", tcp_parse_silent_drop },
William Lallemand13e9b0c2016-05-25 02:34:07 +02001871 { "set-src", tcp_parse_set_src_dst },
1872 { "set-src-port", tcp_parse_set_src_dst },
1873 { "set-dst", tcp_parse_set_src_dst },
1874 { "set-dst-port", tcp_parse_set_src_dst },
Willy Tarreau2d392c22015-08-24 01:43:45 +02001875 { /* END */ }
1876}};
1877
1878static struct action_kw_list http_res_actions = {ILH, {
1879 { "silent-drop", tcp_parse_silent_drop },
1880 { /* END */ }
1881}};
1882
1883
Willy Tarreaue6b98942007-10-29 01:09:36 +01001884__attribute__((constructor))
1885static void __tcp_protocol_init(void)
1886{
1887 protocol_register(&proto_tcpv4);
1888 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001889 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau44791242012-09-12 23:27:21 +02001890 bind_register_keywords(&bind_kws);
Willy Tarreau163d4622015-10-13 16:16:41 +02001891 srv_register_keywords(&srv_kws);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001892 tcp_req_conn_keywords_register(&tcp_req_conn_actions);
Willy Tarreau620408f2016-10-21 16:37:51 +02001893 tcp_req_sess_keywords_register(&tcp_req_sess_actions);
Willy Tarreau2d392c22015-08-24 01:43:45 +02001894 tcp_req_cont_keywords_register(&tcp_req_cont_actions);
1895 tcp_res_cont_keywords_register(&tcp_res_cont_actions);
1896 http_req_keywords_register(&http_req_actions);
1897 http_res_keywords_register(&http_res_actions);
Willy Tarreauba962912016-12-21 18:55:02 +01001898
1899
1900 hap_register_build_opts("Built with transparent proxy support using:"
1901#if defined(IP_TRANSPARENT)
1902 " IP_TRANSPARENT"
1903#endif
1904#if defined(IPV6_TRANSPARENT)
1905 " IPV6_TRANSPARENT"
1906#endif
1907#if defined(IP_FREEBIND)
1908 " IP_FREEBIND"
1909#endif
1910#if defined(IP_BINDANY)
1911 " IP_BINDANY"
1912#endif
1913#if defined(IPV6_BINDANY)
1914 " IPV6_BINDANY"
1915#endif
1916#if defined(SO_BINDANY)
1917 " SO_BINDANY"
1918#endif
1919 "", 0);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001920}
1921
1922
1923/*
1924 * Local variables:
1925 * c-indent-level: 8
1926 * c-basic-offset: 8
1927 * End:
1928 */