blob: afb9e155a2c03466e27e6b74009658885f7bf5de [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
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040027#include <netinet/tcp.h>
28
Willy Tarreaub6866442008-07-14 23:54:42 +020029#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <common/mini-clist.h>
35#include <common/standard.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010036#include <common/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010037
Willy Tarreaue6b98942007-10-29 01:09:36 +010038#include <types/global.h>
Willy Tarreau18bf01e2014-06-13 16:18:52 +020039#include <types/capture.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020040#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010041
42#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020043#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020044#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020045#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020046#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020047#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020048#include <proto/log.h>
49#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/protocol.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010051#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020052#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020053#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020054#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020055#include <proto/stick_table.h>
Willy Tarreaucc1e04b2013-09-11 23:20:29 +020056#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020057#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010058
Willy Tarreaue8c66af2008-01-13 18:40:14 +010059#ifdef CONFIG_HAP_CTTPROXY
60#include <import/ip_tproxy.h>
61#endif
62
Emeric Bruncf20bf12010-10-22 16:06:11 +020063static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
64static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010065
66/* Note: must not be declared <const> as its list will be overwritten */
67static struct protocol proto_tcpv4 = {
68 .name = "tcpv4",
69 .sock_domain = AF_INET,
70 .sock_type = SOCK_STREAM,
71 .sock_prot = IPPROTO_TCP,
72 .sock_family = AF_INET,
73 .sock_addrlen = sizeof(struct sockaddr_in),
74 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020075 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020076 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020077 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010078 .bind_all = tcp_bind_listeners,
79 .unbind_all = unbind_all_listeners,
80 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020081 .get_src = tcp_get_src,
82 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020083 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +020084 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010085 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
86 .nb_listeners = 0,
87};
88
89/* Note: must not be declared <const> as its list will be overwritten */
90static struct protocol proto_tcpv6 = {
91 .name = "tcpv6",
92 .sock_domain = AF_INET6,
93 .sock_type = SOCK_STREAM,
94 .sock_prot = IPPROTO_TCP,
95 .sock_family = AF_INET6,
96 .sock_addrlen = sizeof(struct sockaddr_in6),
97 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020098 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020099 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200100 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100101 .bind_all = tcp_bind_listeners,
102 .unbind_all = unbind_all_listeners,
103 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200104 .get_src = tcp_get_src,
105 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200106 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +0200107 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100108 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
109 .nb_listeners = 0,
110};
111
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100112
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
122 * - cttproxy: we bind to a local address then nat.
123 * The second one can be used as a fallback for the first one.
124 * This function returns 0 when everything's OK, 1 if it could not bind, to the
125 * local address, 2 if it could not bind to the foreign address.
126 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100127int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100128{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100129 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100130 int foreign_ok = 0;
131 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100132 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200133 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200134
David du Colombier65c17962012-07-13 14:34:59 +0200135 switch (local->ss_family) {
136 case AF_INET:
137 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200138 /* This deserves some explanation. Some platforms will support
139 * multiple combinations of certain methods, so we try the
140 * supported ones until one succeeds.
141 */
142 if (0
143#if defined(IP_TRANSPARENT)
144 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
145#endif
146#if defined(IP_FREEBIND)
147 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
148#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200149#if defined(IP_BINDANY)
150 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
151#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200152#if defined(SO_BINDANY)
153 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
154#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200155 )
David du Colombier65c17962012-07-13 14:34:59 +0200156 foreign_ok = 1;
157 else
158 ip_transp_working = 0;
159 }
160 break;
161 case AF_INET6:
162 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200163 if (0
164#if defined(IPV6_TRANSPARENT)
165 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
166#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100167#if defined(IP_FREEBIND)
168 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
169#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200170#if defined(IPV6_BINDANY)
171 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
172#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200173#if defined(SO_BINDANY)
174 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
175#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200176 )
David du Colombier65c17962012-07-13 14:34:59 +0200177 foreign_ok = 1;
178 else
179 ip6_transp_working = 0;
180 }
181 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100182 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200183
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100184 if (flags) {
185 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200186 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100187 switch (remote->ss_family) {
188 case AF_INET:
189 if (flags & 1)
190 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
191 if (flags & 2)
192 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
193 break;
194 case AF_INET6:
195 if (flags & 1)
196 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
197 if (flags & 2)
198 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
199 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100200 default:
201 /* we don't want to try to bind to an unknown address family */
202 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100203 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100204 }
205
Simon Hormande072bd2011-06-24 15:11:37 +0900206 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100207 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200208 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200209 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
210 if (ret < 0)
211 return 2;
212 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100213 }
214 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200215 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200216 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
217 if (ret < 0)
218 return 1;
219 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100220 }
221
222 if (!flags)
223 return 0;
224
225#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100226 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100227 struct in_tproxy itp1, itp2;
228 memset(&itp1, 0, sizeof(itp1));
229
230 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100231 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
232 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100233
234 /* set connect flag on socket */
235 itp2.op = TPROXY_FLAGS;
236 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
237
238 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
239 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
240 foreign_ok = 1;
241 }
242 }
243#endif
244 if (!foreign_ok)
245 /* we could not bind to a foreign address */
246 return 2;
247
248 return 0;
249}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100250
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100251static int create_server_socket(struct connection *conn)
252{
253 const struct netns_entry *ns = objt_server(conn->target)->netns;
254
255 if (objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
256 ns = conn->proxy_netns;
257
258 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
259}
Willy Tarreau9650f372009-08-16 14:02:45 +0200260
261/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200262 * This function initiates a TCP connection establishment to the target assigned
263 * to connection <conn> using (si->{target,addr.to}). A source address may be
264 * pointed to by conn->addr.from in case of transparent proxying. Normal source
265 * bind addresses are still determined locally (due to the possible need of a
266 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100267 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100268 * supported. The <data> parameter is a boolean indicating whether there are data
269 * waiting for being sent or not, in order to adjust data write polling and on
270 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
271 * allows the caller to force using a delayed ACK when establishing the connection :
272 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
273 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
274 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200275 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200276 * Note that a pending send_proxy message accounts for data.
277 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200278 * It can return one of :
279 * - SN_ERR_NONE if everything's OK
280 * - SN_ERR_SRVTO if there are no more servers
281 * - SN_ERR_SRVCL if the connection was refused by the server
282 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
283 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
284 * - SN_ERR_INTERNAL for any other purely internal errors
285 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100286 *
287 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
288 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200289 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100290
Willy Tarreauf0837b22012-11-24 10:24:27 +0100291int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200292{
293 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100294 struct server *srv;
295 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100296 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100297
Willy Tarreau9ce70132014-01-24 16:08:19 +0100298 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
299
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100300 switch (obj_type(conn->target)) {
301 case OBJ_TYPE_PROXY:
302 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100303 srv = NULL;
304 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100305 case OBJ_TYPE_SERVER:
306 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100307 be = srv->proxy;
308 break;
309 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100310 conn->flags |= CO_FL_ERROR;
Willy Tarreauac825402011-03-04 22:04:29 +0100311 return SN_ERR_INTERNAL;
312 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200313
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100314 fd = conn->t.sock.fd = create_server_socket(conn);
315
316 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200317 qfprintf(stderr, "Cannot get a server socket.\n");
318
Willy Tarreau9ce70132014-01-24 16:08:19 +0100319 if (errno == ENFILE) {
320 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200321 send_log(be, LOG_EMERG,
322 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
323 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100324 }
325 else if (errno == EMFILE) {
326 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200327 send_log(be, LOG_EMERG,
328 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
329 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100330 }
331 else if (errno == ENOBUFS || errno == ENOMEM) {
332 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200333 send_log(be, LOG_EMERG,
334 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
335 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100336 }
337 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
338 conn->err_code = CO_ER_NOPROTO;
339 }
340 else
341 conn->err_code = CO_ER_SOCK_ERR;
342
Willy Tarreau9650f372009-08-16 14:02:45 +0200343 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100344 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200345 return SN_ERR_RESOURCE;
346 }
347
348 if (fd >= global.maxsock) {
349 /* do not log anything there, it's a normal condition when this option
350 * is used to serialize connections to a server !
351 */
352 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
353 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100354 conn->err_code = CO_ER_CONF_FDLIM;
355 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 return SN_ERR_PRXCOND; /* it is a configuration limit */
357 }
358
359 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900360 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200361 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
362 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100363 conn->err_code = CO_ER_SOCK_ERR;
364 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200365 return SN_ERR_INTERNAL;
366 }
367
368 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900369 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200370
Willy Tarreau9650f372009-08-16 14:02:45 +0200371 /* allow specific binding :
372 * - server-specific at first
373 * - proxy-specific next
374 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100375 if (srv && srv->conn_src.opts & CO_SRC_BIND)
376 src = &srv->conn_src;
377 else if (be->conn_src.opts & CO_SRC_BIND)
378 src = &be->conn_src;
379 else
380 src = NULL;
381
382 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200383 int ret, flags = 0;
384
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200385 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100386 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100387 case CO_SRC_TPROXY_ADDR:
388 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200389 flags = 3;
390 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100391 case CO_SRC_TPROXY_CIP:
392 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200393 flags = 1;
394 break;
395 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200396 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200397
Willy Tarreau9650f372009-08-16 14:02:45 +0200398#ifdef SO_BINDTODEVICE
399 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100400 if (src->iface_name)
401 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200402#endif
403
Willy Tarreaua4380b42012-12-08 22:49:11 +0100404 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200405 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100406 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200407
408 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100409 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200410
411 do {
412 /* note: in case of retry, we may have to release a previously
413 * allocated port, hence this loop's construct.
414 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200415 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
416 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200417
418 if (!attempts)
419 break;
420 attempts--;
421
Willy Tarreaua4380b42012-12-08 22:49:11 +0100422 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100423 if (!fdinfo[fd].local_port) {
424 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200425 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100426 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200427
Willy Tarreaua4380b42012-12-08 22:49:11 +0100428 fdinfo[fd].port_range = src->sport_range;
429 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200430
Willy Tarreaua4380b42012-12-08 22:49:11 +0100431 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100432 if (ret != 0)
433 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200434 } while (ret != 0); /* binding NOK */
435 }
436 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100437 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100438 if (ret != 0)
439 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200440 }
441
Willy Tarreaua4380b42012-12-08 22:49:11 +0100442 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200443 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
444 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 close(fd);
446
Willy Tarreau9650f372009-08-16 14:02:45 +0200447 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100448 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200449 be->id);
450 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100451 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 be->id);
453 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100454 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200455 be->id);
456 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100457 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 be->id);
459 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100460 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200461 return SN_ERR_RESOURCE;
462 }
463 }
464
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400465#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200466 /* disabling tcp quick ack now allows the first request to leave the
467 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100468 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200470 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900471 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200472#endif
473
Willy Tarreaue803de22010-01-21 17:43:04 +0100474 if (global.tune.server_sndbuf)
475 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
476
477 if (global.tune.server_rcvbuf)
478 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
479
Willy Tarreau986a9d22012-08-30 21:11:38 +0200480 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200481 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
482
Willy Tarreaub1719512012-12-08 23:03:28 +0100483 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200484 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100485 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200486 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100487 conn->err_code = CO_ER_FREE_PORTS;
488 }
489 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200490 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100491 conn->err_code = CO_ER_ADDR_INUSE;
492 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200493
Willy Tarreaub1719512012-12-08 23:03:28 +0100494 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200495 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
496 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200497 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100498 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100499 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200500 return SN_ERR_RESOURCE;
501 } else if (errno == ETIMEDOUT) {
502 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200503 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
504 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200505 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100506 conn->err_code = CO_ER_SOCK_ERR;
507 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200508 return SN_ERR_SRVTO;
509 } else {
510 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
511 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200512 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
513 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200514 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100515 conn->err_code = CO_ER_SOCK_ERR;
516 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200517 return SN_ERR_SRVCL;
518 }
519 }
520
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100521 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200522
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200523 /* Prepare to send a few handshakes related to the on-wire protocol. */
524 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200525 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200526
Willy Tarreauf79c8172013-10-21 16:30:56 +0200527 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100528 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200529 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200530
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200531 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200532 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100533 conn->flags |= CO_FL_ERROR;
Willy Tarreau15678ef2012-08-31 13:54:11 +0200534 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200535 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200536
Willy Tarreau14f8e862012-08-30 22:23:13 +0200537 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200538 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200539
Willy Tarreau9650f372009-08-16 14:02:45 +0200540 return SN_ERR_NONE; /* connection is OK */
541}
542
543
Willy Tarreau59b94792012-05-11 16:16:40 +0200544/*
545 * Retrieves the source address for the socket <fd>, with <dir> indicating
546 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
547 * success, -1 in case of error. The socket's source address is stored in
548 * <sa> for <salen> bytes.
549 */
550int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
551{
552 if (dir)
553 return getsockname(fd, sa, &salen);
554 else
555 return getpeername(fd, sa, &salen);
556}
557
558
559/*
560 * Retrieves the original destination address for the socket <fd>, with <dir>
561 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
562 * listener, if the original destination address was translated, the original
563 * address is retrieved. It returns 0 in case of success, -1 in case of error.
564 * The socket's source address is stored in <sa> for <salen> bytes.
565 */
566int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
567{
568 if (dir)
569 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100570 else {
571 int ret = getsockname(fd, sa, &salen);
572
573 if (ret < 0)
574 return ret;
575
Willy Tarreau59b94792012-05-11 16:16:40 +0200576#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100577 /* For TPROXY and Netfilter's NAT, we can retrieve the original
578 * IPv4 address before DNAT/REDIRECT. We must not do that with
579 * other families because v6-mapped IPv4 addresses are still
580 * reported as v4.
581 */
582 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
583 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
584 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200585#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100586 return ret;
587 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200588}
589
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200590/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100591 * receive shutdown. Returns positive if the shutdown was found, negative
592 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
593 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200594 */
595int tcp_drain(int fd)
596{
597 int turns = 2;
598 int len;
599
600 while (turns) {
601#ifdef MSG_TRUNC_CLEARS_INPUT
602 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
603 if (len == -1 && errno == EFAULT)
604#endif
605 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
606
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100607 if (len == 0) {
608 /* cool, shutdown received */
609 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200610 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100611 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200612
613 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100614 if (errno == EAGAIN) {
615 /* connection not closed yet */
616 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100617 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100618 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200619 if (errno == EINTR) /* oops, try again */
620 continue;
621 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100622 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200623 return 1;
624 }
625 /* OK we read some data, let's try again once */
626 turns--;
627 }
628 /* some data are still present, give up */
629 return 0;
630}
631
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200632/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100633 * and we have nothing to send. It updates the FD polling status. It returns 0
634 * if it fails in a fatal way or needs to poll to go further, otherwise it
635 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
636 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
637 * errno. The error checking is done in two passes in order to limit the number
638 * of syscalls in the normal case :
639 * - if POLL_ERR was reported by the poller, we check for a pending error on
640 * the socket before proceeding. If found, it's assigned to errno so that
641 * upper layers can see it.
642 * - otherwise connect() is used to check the connection state again, since
643 * the getsockopt return cannot reliably be used to know if the connection
644 * is still pending or ready. This one may often return an error as well,
645 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200646 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200647int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200648{
Willy Tarreau239d7182012-07-23 18:53:03 +0200649 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100650 socklen_t lskerr;
651 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200652
Willy Tarreau80184712012-07-06 14:54:49 +0200653 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200654 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200655
Willy Tarreau3c728722014-01-23 13:50:42 +0100656 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200657 return 0;
658
Willy Tarreau80184712012-07-06 14:54:49 +0200659 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200660 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200661
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100662 if (!fd_send_ready(fd))
663 return 0;
664
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100665 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
666 * without FD_POLL_IN also indicates a hangup without input data meaning
667 * there was no connection.
668 */
669 if (fdtab[fd].ev & FD_POLL_ERR ||
670 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
671 skerr = 0;
672 lskerr = sizeof(skerr);
673 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
674 errno = skerr;
675 if (errno == EAGAIN)
676 errno = 0;
677 if (errno)
678 goto out_error;
679 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200680
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100681 /* Use connect() to check the state of the socket. This has the
682 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200683 * - error
684 * - connecting (EALREADY, EINPROGRESS)
685 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200686 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200687 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200688 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100689 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100690 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200691 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200692 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200693
Willy Tarreau2c6be842012-07-06 17:12:34 +0200694 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200695 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200696
Willy Tarreau2c6be842012-07-06 17:12:34 +0200697 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200698 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200699
Willy Tarreau076be252012-07-06 16:02:29 +0200700 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200701 * forward the event to the transport layer which will notify the
702 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200703 */
Willy Tarreau80184712012-07-06 14:54:49 +0200704 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200705 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200706
707 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200708 /* Write error on the file descriptor. Report it to the connection
709 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200710 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100711 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100712 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100713 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200714 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200715}
716
Willy Tarreau59b94792012-05-11 16:16:40 +0200717
Willy Tarreaue6b98942007-10-29 01:09:36 +0100718/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100719 * an error message in <errmsg> if the message is at most <errlen> bytes long
720 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
721 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100722 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
723 * was alright and that no message was returned. ERR_RETRYABLE means that an
724 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700725 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100726 * the meaning of the error, but just indicate that a message is present which
727 * should be displayed with the respective level. Last, ERR_ABORT indicates
728 * that it's pointless to try to start other listeners. No error message is
729 * returned if errlen is NULL.
730 */
731int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
732{
733 __label__ tcp_return, tcp_close_return;
734 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100735 int ext, ready;
736 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100737 const char *msg = NULL;
738
739 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100740 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100741 *errmsg = 0;
742
743 if (listener->state != LI_ASSIGNED)
744 return ERR_NONE; /* already bound */
745
746 err = ERR_NONE;
747
Willy Tarreau40aa0702013-03-10 23:51:38 +0100748 /* if the listener already has an fd assigned, then we were offered the
749 * fd by an external process (most likely the parent), and we don't want
750 * to create a new socket. However we still want to set a few flags on
751 * the socket.
752 */
753 fd = listener->fd;
754 ext = (fd >= 0);
755
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100756 if (!ext) {
757 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
758
759 if (fd == -1) {
760 err |= ERR_RETRYABLE | ERR_ALERT;
761 msg = "cannot create listening socket";
762 goto tcp_return;
763 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100764 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100765
Willy Tarreaue6b98942007-10-29 01:09:36 +0100766 if (fd >= global.maxsock) {
767 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
768 msg = "not enough free sockets (raise '-n' parameter)";
769 goto tcp_close_return;
770 }
771
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200772 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100773 err |= ERR_FATAL | ERR_ALERT;
774 msg = "cannot make socket non-blocking";
775 goto tcp_close_return;
776 }
777
Willy Tarreau40aa0702013-03-10 23:51:38 +0100778 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100779 /* not fatal but should be reported */
780 msg = "cannot do so_reuseaddr";
781 err |= ERR_ALERT;
782 }
783
784 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900785 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100786
Willy Tarreaue6b98942007-10-29 01:09:36 +0100787#ifdef SO_REUSEPORT
788 /* OpenBSD supports this. As it's present in old libc versions of Linux,
789 * it might return an error that we will silently ignore.
790 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100791 if (!ext)
792 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100793#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200794
Willy Tarreau40aa0702013-03-10 23:51:38 +0100795 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200796 switch (listener->addr.ss_family) {
797 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200798 if (1
799#if defined(IP_TRANSPARENT)
800 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
801#endif
802#if defined(IP_FREEBIND)
803 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
804#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200805#if defined(IP_BINDANY)
806 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
807#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200808#if defined(SO_BINDANY)
809 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
810#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200811 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200812 msg = "cannot make listening socket transparent";
813 err |= ERR_ALERT;
814 }
815 break;
816 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200817 if (1
818#if defined(IPV6_TRANSPARENT)
819 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
820#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100821#if defined(IP_FREEBIND)
822 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
823#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200824#if defined(IPV6_BINDANY)
825 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
826#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200827#if defined(SO_BINDANY)
828 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
829#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200830 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200831 msg = "cannot make listening socket transparent";
832 err |= ERR_ALERT;
833 }
834 break;
835 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100836 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200837
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100838#ifdef SO_BINDTODEVICE
839 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100840 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100841 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100842 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100843 msg = "cannot bind listener to device";
844 err |= ERR_WARN;
845 }
846 }
847#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400848#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100849 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400850 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200851 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
852 msg = "cannot set MSS";
853 err |= ERR_WARN;
854 }
855 }
856#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200857#if defined(TCP_DEFER_ACCEPT)
858 if (listener->options & LI_O_DEF_ACCEPT) {
859 /* defer accept by up to one second */
860 int accept_delay = 1;
861 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
862 msg = "cannot enable DEFER_ACCEPT";
863 err |= ERR_WARN;
864 }
865 }
866#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200867#if defined(TCP_FASTOPEN)
868 if (listener->options & LI_O_TCP_FO) {
869 /* TFO needs a queue length, let's use the configured backlog */
870 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
871 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
872 msg = "cannot enable TCP_FASTOPEN";
873 err |= ERR_WARN;
874 }
875 }
876#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100877#if defined(IPV6_V6ONLY)
878 if (listener->options & LI_O_V6ONLY)
879 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100880 else if (listener->options & LI_O_V4V6)
881 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100882#endif
883
Willy Tarreau40aa0702013-03-10 23:51:38 +0100884 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100885 err |= ERR_RETRYABLE | ERR_ALERT;
886 msg = "cannot bind socket";
887 goto tcp_close_return;
888 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100889
Willy Tarreau40aa0702013-03-10 23:51:38 +0100890 ready = 0;
891 ready_len = sizeof(ready);
892 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
893 ready = 0;
894
895 if (!(ext && ready) && /* only listen if not already done by external process */
896 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100897 err |= ERR_RETRYABLE | ERR_ALERT;
898 msg = "cannot listen to socket";
899 goto tcp_close_return;
900 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100901
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400902#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200903 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900904 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200905#endif
906
Willy Tarreaue6b98942007-10-29 01:09:36 +0100907 /* the socket is ready */
908 listener->fd = fd;
909 listener->state = LI_LISTEN;
910
Willy Tarreaueabf3132008-08-29 23:36:51 +0200911 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200912 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200913 fd_insert(fd);
914
Willy Tarreaue6b98942007-10-29 01:09:36 +0100915 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100916 if (msg && errlen) {
917 char pn[INET6_ADDRSTRLEN];
918
Willy Tarreau631f01c2011-09-05 00:36:48 +0200919 addr_to_str(&listener->addr, pn, sizeof(pn));
920 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100921 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100922 return err;
923
924 tcp_close_return:
925 close(fd);
926 goto tcp_return;
927}
928
929/* This function creates all TCP sockets bound to the protocol entry <proto>.
930 * It is intended to be used as the protocol's bind_all() function.
931 * The sockets will be registered but not added to any fd_set, in order not to
932 * loose them across the fork(). A call to enable_all_listeners() is needed
933 * to complete initialization. The return value is composed from ERR_*.
934 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200935static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100936{
937 struct listener *listener;
938 int err = ERR_NONE;
939
940 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200941 err |= tcp_bind_listener(listener, errmsg, errlen);
942 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100943 break;
944 }
945
946 return err;
947}
948
949/* Add listener to the list of tcpv4 listeners. The listener's state
950 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
951 * listeners is updated. This is the function to use to add a new listener.
952 */
953void tcpv4_add_listener(struct listener *listener)
954{
955 if (listener->state != LI_INIT)
956 return;
957 listener->state = LI_ASSIGNED;
958 listener->proto = &proto_tcpv4;
959 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
960 proto_tcpv4.nb_listeners++;
961}
962
963/* Add listener to the list of tcpv4 listeners. The listener's state
964 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
965 * listeners is updated. This is the function to use to add a new listener.
966 */
967void tcpv6_add_listener(struct listener *listener)
968{
969 if (listener->state != LI_INIT)
970 return;
971 listener->state = LI_ASSIGNED;
972 listener->proto = &proto_tcpv6;
973 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
974 proto_tcpv6.nb_listeners++;
975}
976
Willy Tarreau092d8652014-07-07 20:22:12 +0200977/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
978 * was totally stopped, or > 0 if correctly paused.
979 */
980int tcp_pause_listener(struct listener *l)
981{
982 if (shutdown(l->fd, SHUT_WR) != 0)
983 return -1; /* Solaris dies here */
984
985 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
986 return -1; /* OpenBSD dies here */
987
988 if (shutdown(l->fd, SHUT_RD) != 0)
989 return -1; /* should always be OK */
990 return 1;
991}
992
Willy Tarreauedcf6682008-11-30 23:15:34 +0100993/* This function performs the TCP request analysis on the current request. It
994 * returns 1 if the processing can continue on next analysers, or zero if it
995 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200996 * request. It relies on buffers flags, and updates s->req->analysers. The
997 * function may be called for frontend rules and backend rules. It only relies
998 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100999 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001000int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +01001001{
1002 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +02001003 struct stksess *ts;
1004 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001005 int partial;
1006
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001007 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreauedcf6682008-11-30 23:15:34 +01001008 now_ms, __FUNCTION__,
1009 s,
1010 req,
1011 req->rex, req->wex,
1012 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001013 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +01001014 req->analysers);
1015
Willy Tarreauedcf6682008-11-30 23:15:34 +01001016 /* We don't know whether we have enough data, so must proceed
1017 * this way :
1018 * - iterate through all rules in their declaration order
1019 * - if one rule returns MISS, it means the inspect delay is
1020 * not over yet, then return immediately, otherwise consider
1021 * it as a non-match.
1022 * - if one rule returns OK, then return OK
1023 * - if one rule returns KO, then return KO
1024 */
1025
Willy Tarreau9b28e032012-10-12 23:49:43 +02001026 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001027 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001028 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001029 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001030 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001031
Willy Tarreaufb356202010-08-03 14:02:05 +02001032 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001033 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001034
1035 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001036 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001037 if (ret == ACL_TEST_MISS)
1038 goto missing_data;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001039
1040 ret = acl_pass(ret);
1041 if (rule->cond->pol == ACL_COND_UNLESS)
1042 ret = !ret;
1043 }
1044
1045 if (ret) {
1046 /* we have a matching rule. */
1047 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001048 channel_abort(req);
1049 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001050 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001051
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001052 s->be->be_counters.denied_req++;
1053 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001054 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +02001055 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001056
Willy Tarreauedcf6682008-11-30 23:15:34 +01001057 if (!(s->flags & SN_ERR_MASK))
1058 s->flags |= SN_ERR_PRXCOND;
1059 if (!(s->flags & SN_FINST_MASK))
1060 s->flags |= SN_FINST_R;
1061 return 0;
1062 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001063 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001064 /* Note: only the first valid tracking parameter of each
1065 * applies.
1066 */
1067 struct stktable_key *key;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001068 struct sample smp;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001069
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001070 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001071 continue;
1072
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001073 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001074 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial, rule->act_prm.trk_ctr.expr, &smp);
1075
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001076 if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL))
1077 goto missing_data; /* key might appear later */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001078
1079 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001080 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001081 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001082 if (s->fe != s->be)
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001083 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreaud1f96522010-08-03 19:34:32 +02001084 }
1085 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001086 else if (rule->action == TCP_ACT_CAPTURE) {
1087 struct sample *key;
1088 struct cap_hdr *h = rule->act_prm.cap.hdr;
1089 char **cap = s->txn.req.cap;
1090 int len;
1091
1092 key = sample_fetch_string(s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial, rule->act_prm.cap.expr);
1093 if (!key)
1094 continue;
1095
1096 if (key->flags & SMP_F_MAY_CHANGE)
1097 goto missing_data;
1098
1099 if (cap[h->index] == NULL)
1100 cap[h->index] = pool_alloc2(h->pool);
1101
1102 if (cap[h->index] == NULL) /* no more capture memory */
1103 continue;
1104
1105 len = key->data.str.len;
1106 if (len > h->len)
1107 len = h->len;
1108
1109 memcpy(cap[h->index], key->data.str.str, len);
1110 cap[h->index][len] = 0;
1111 }
Willy Tarreaud1f96522010-08-03 19:34:32 +02001112 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +01001113 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001114 break;
1115 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001116 }
1117 }
1118
1119 /* if we get there, it means we have no rule which matches, or
1120 * we have an explicit accept, so we apply the default accept.
1121 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001122 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001123 req->analyse_exp = TICK_ETERNITY;
1124 return 1;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001125
1126 missing_data:
1127 channel_dont_connect(req);
1128 /* just set the request timeout once at the beginning of the request */
1129 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
1130 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
1131 return 0;
1132
Willy Tarreauedcf6682008-11-30 23:15:34 +01001133}
1134
Emeric Brun97679e72010-09-23 17:56:44 +02001135/* This function performs the TCP response analysis on the current response. It
1136 * returns 1 if the processing can continue on next analysers, or zero if it
1137 * needs more data, encounters an error, or wants to immediately abort the
1138 * response. It relies on buffers flags, and updates s->rep->analysers. The
1139 * function may be called for backend rules.
1140 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001141int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001142{
1143 struct tcp_rule *rule;
1144 int partial;
1145
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001146 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun97679e72010-09-23 17:56:44 +02001147 now_ms, __FUNCTION__,
1148 s,
1149 rep,
1150 rep->rex, rep->wex,
1151 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001152 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001153 rep->analysers);
1154
1155 /* We don't know whether we have enough data, so must proceed
1156 * this way :
1157 * - iterate through all rules in their declaration order
1158 * - if one rule returns MISS, it means the inspect delay is
1159 * not over yet, then return immediately, otherwise consider
1160 * it as a non-match.
1161 * - if one rule returns OK, then return OK
1162 * - if one rule returns KO, then return KO
1163 */
1164
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001165 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001166 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001167 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001168 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001169
1170 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001171 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001172
1173 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001174 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001175 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001176 /* just set the analyser timeout once at the beginning of the response */
1177 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001178 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001179 return 0;
1180 }
1181
1182 ret = acl_pass(ret);
1183 if (rule->cond->pol == ACL_COND_UNLESS)
1184 ret = !ret;
1185 }
1186
1187 if (ret) {
1188 /* we have a matching rule. */
1189 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001190 channel_abort(rep);
1191 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001192 rep->analysers = 0;
1193
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001194 s->be->be_counters.denied_resp++;
1195 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001196 if (s->listener->counters)
1197 s->listener->counters->denied_resp++;
1198
1199 if (!(s->flags & SN_ERR_MASK))
1200 s->flags |= SN_ERR_PRXCOND;
1201 if (!(s->flags & SN_FINST_MASK))
1202 s->flags |= SN_FINST_D;
1203 return 0;
1204 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001205 else if (rule->action == TCP_ACT_CLOSE) {
1206 rep->prod->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1207 si_shutr(rep->prod);
1208 si_shutw(rep->prod);
1209 break;
1210 }
Emeric Brun97679e72010-09-23 17:56:44 +02001211 else {
1212 /* otherwise accept */
1213 break;
1214 }
1215 }
1216 }
1217
1218 /* if we get there, it means we have no rule which matches, or
1219 * we have an explicit accept, so we apply the default accept.
1220 */
1221 rep->analysers &= ~an_bit;
1222 rep->analyse_exp = TICK_ETERNITY;
1223 return 1;
1224}
1225
1226
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001227/* This function performs the TCP layer4 analysis on the current request. It
1228 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1229 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001230 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001231 */
1232int tcp_exec_req_rules(struct session *s)
1233{
1234 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001235 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001236 struct stktable *t = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001237 struct connection *conn = objt_conn(s->si[0].end);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001238 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001239 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001240
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001241 if (!conn)
1242 return result;
1243
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001244 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001245 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001246
1247 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001248 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001249 ret = acl_pass(ret);
1250 if (rule->cond->pol == ACL_COND_UNLESS)
1251 ret = !ret;
1252 }
1253
1254 if (ret) {
1255 /* we have a matching rule. */
1256 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001257 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001258 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001259 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001260
1261 if (!(s->flags & SN_ERR_MASK))
1262 s->flags |= SN_ERR_PRXCOND;
1263 if (!(s->flags & SN_FINST_MASK))
1264 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001265 result = 0;
1266 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001267 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001268 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001269 /* Note: only the first valid tracking parameter of each
1270 * applies.
1271 */
1272 struct stktable_key *key;
1273
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001274 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001275 continue;
1276
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001277 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreaub5975de2014-06-25 16:20:53 +02001278 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001279
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001280 if (key && (ts = stktable_get_entry(t, key)))
1281 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001282 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001283 else if (rule->action == TCP_ACT_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001284 conn->flags |= CO_FL_ACCEPT_PROXY;
1285 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001286 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001287 else {
1288 /* otherwise it's an accept */
1289 break;
1290 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001291 }
1292 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001293 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001294}
1295
Emeric Brun97679e72010-09-23 17:56:44 +02001296/* Parse a tcp-response rule. Return a negative value in case of failure */
1297static int tcp_parse_response_rule(char **args, int arg, int section_type,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001298 struct proxy *curpx, struct proxy *defpx,
1299 struct tcp_rule *rule, char **err,
1300 unsigned int where,
1301 const char *file, int line)
Emeric Brun97679e72010-09-23 17:56:44 +02001302{
1303 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001304 memprintf(err, "%s %s is only allowed in 'backend' sections",
1305 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001306 return -1;
1307 }
1308
1309 if (strcmp(args[arg], "accept") == 0) {
1310 arg++;
1311 rule->action = TCP_ACT_ACCEPT;
1312 }
1313 else if (strcmp(args[arg], "reject") == 0) {
1314 arg++;
1315 rule->action = TCP_ACT_REJECT;
1316 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001317 else if (strcmp(args[arg], "close") == 0) {
1318 arg++;
1319 rule->action = TCP_ACT_CLOSE;
1320 }
Emeric Brun97679e72010-09-23 17:56:44 +02001321 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001322 memprintf(err,
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001323 "'%s %s' expects 'accept', 'close' or 'reject' in %s '%s' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001324 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001325 return -1;
1326 }
1327
1328 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001329 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001330 memprintf(err,
1331 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1332 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001333 return -1;
1334 }
1335 }
1336 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001337 memprintf(err,
1338 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001339 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1340 return -1;
1341 }
1342 return 0;
1343}
1344
1345
1346
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001347/* Parse a tcp-request rule. Return a negative value in case of failure */
1348static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001349 struct proxy *curpx, struct proxy *defpx,
1350 struct tcp_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001351 unsigned int where, const char *file, int line)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001352{
1353 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001354 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1355 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001356 return -1;
1357 }
1358
1359 if (!strcmp(args[arg], "accept")) {
1360 arg++;
1361 rule->action = TCP_ACT_ACCEPT;
1362 }
1363 else if (!strcmp(args[arg], "reject")) {
1364 arg++;
1365 rule->action = TCP_ACT_REJECT;
1366 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001367 else if (strcmp(args[arg], "capture") == 0) {
1368 struct sample_expr *expr;
1369 struct cap_hdr *hdr;
1370 int kw = arg;
1371 int len = 0;
1372
1373 if (!(curpx->cap & PR_CAP_FE)) {
1374 memprintf(err,
1375 "'%s %s %s' : proxy '%s' has no frontend capability",
1376 args[0], args[1], args[kw], curpx->id);
1377 return -1;
1378 }
1379
1380 if (!(where & SMP_VAL_FE_REQ_CNT)) {
1381 memprintf(err,
1382 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1383 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1384 return -1;
1385 }
1386
1387 arg++;
1388
1389 curpx->conf.args.ctx = ARGC_CAP;
1390 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
1391 if (!expr) {
1392 memprintf(err,
1393 "'%s %s %s' : %s",
1394 args[0], args[1], args[kw], *err);
1395 return -1;
1396 }
1397
1398 if (!(expr->fetch->val & where)) {
1399 memprintf(err,
1400 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
1401 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
1402 free(expr);
1403 return -1;
1404 }
1405
1406 if (strcmp(args[arg], "len") == 0) {
1407 arg++;
1408 if (!args[arg]) {
1409 memprintf(err,
1410 "'%s %s %s' : missing length value",
1411 args[0], args[1], args[kw]);
1412 free(expr);
1413 return -1;
1414 }
1415 /* we copy the table name for now, it will be resolved later */
1416 len = atoi(args[arg]);
1417 if (len <= 0) {
1418 memprintf(err,
1419 "'%s %s %s' : length must be > 0",
1420 args[0], args[1], args[kw]);
1421 free(expr);
1422 return -1;
1423 }
1424 arg++;
1425 }
1426
1427 if (!len) {
1428 memprintf(err,
1429 "'%s %s %s' : a positive 'len' argument is mandatory",
1430 args[0], args[1], args[kw]);
1431 free(expr);
1432 return -1;
1433 }
1434
1435 hdr = calloc(sizeof(struct cap_hdr), 1);
1436 hdr->next = curpx->req_cap;
1437 hdr->name = NULL; /* not a header capture */
1438 hdr->namelen = 0;
1439 hdr->len = len;
1440 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
1441 hdr->index = curpx->nb_req_cap++;
1442
1443 curpx->req_cap = hdr;
1444 curpx->to_log |= LW_REQHDR;
1445
1446 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
1447 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1448
1449 rule->act_prm.cap.expr = expr;
1450 rule->act_prm.cap.hdr = hdr;
1451 rule->action = TCP_ACT_CAPTURE;
1452 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001453 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1454 args[arg][9] == '\0' && args[arg][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02001455 args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001456 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001457 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001458
1459 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001460
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001461 curpx->conf.args.ctx = ARGC_TRK;
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001462 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001463 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001464 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001465 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001466 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001467 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001468 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001469
Willy Tarreau80aca902013-01-07 15:42:20 +01001470 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001471 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001472 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001473 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001474 free(expr);
1475 return -1;
1476 }
1477
1478 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001479 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001480
1481 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001482 arg++;
1483 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001484 memprintf(err,
1485 "'%s %s %s' : missing table name",
1486 args[0], args[1], args[kw]);
1487 free(expr);
1488 return -1;
1489 }
1490 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001491 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001492 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001493 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001494 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001495 rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001496 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001497 else if (strcmp(args[arg], "expect-proxy") == 0) {
1498 if (strcmp(args[arg+1], "layer4") != 0) {
1499 memprintf(err,
1500 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1501 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1502 return -1;
1503 }
1504
1505 if (!(where & SMP_VAL_FE_CON_ACC)) {
1506 memprintf(err,
1507 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1508 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1509 return -1;
1510 }
1511
1512 arg += 2;
1513 rule->action = TCP_ACT_EXPECT_PX;
1514 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001515 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001516 memprintf(err,
Willy Tarreaub4c84932013-07-23 19:15:30 +02001517 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d' "
1518 " in %s '%s' (got '%s')",
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02001519 args[0], args[1], MAX_SESS_STKCTR-1, proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001520 return -1;
1521 }
1522
1523 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001524 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001525 memprintf(err,
1526 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1527 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001528 return -1;
1529 }
1530 }
1531 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001532 memprintf(err,
1533 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001534 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1535 return -1;
1536 }
1537 return 0;
1538}
1539
Emeric Brun97679e72010-09-23 17:56:44 +02001540/* This function should be called to parse a line starting with the "tcp-response"
1541 * keyword.
1542 */
1543static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001544 struct proxy *defpx, const char *file, int line,
1545 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001546{
1547 const char *ptr = NULL;
1548 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001549 int warn = 0;
1550 int arg;
1551 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001552 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001553 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001554 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001555
1556 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001557 memprintf(err, "missing argument for '%s' in %s '%s'",
1558 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001559 return -1;
1560 }
1561
1562 if (strcmp(args[1], "inspect-delay") == 0) {
1563 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001564 memprintf(err, "%s %s is only allowed in 'backend' sections",
1565 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001566 return -1;
1567 }
1568
1569 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001570 memprintf(err,
1571 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1572 args[0], args[1], proxy_type_str(curpx), curpx->id);
1573 if (ptr)
1574 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001575 return -1;
1576 }
1577
1578 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001579 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1580 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001581 return 1;
1582 }
1583 curpx->tcp_rep.inspect_delay = val;
1584 return 0;
1585 }
1586
Simon Hormande072bd2011-06-24 15:11:37 +09001587 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001588 LIST_INIT(&rule->list);
1589 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001590 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001591
1592 if (strcmp(args[1], "content") == 0) {
1593 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001594
1595 if (curpx->cap & PR_CAP_FE)
1596 where |= SMP_VAL_FE_RES_CNT;
1597 if (curpx->cap & PR_CAP_BE)
1598 where |= SMP_VAL_BE_RES_CNT;
1599
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001600 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001601 goto error;
1602
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001603 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1604 if (acl) {
1605 if (acl->name && *acl->name)
1606 memprintf(err,
1607 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1608 acl->name, args[0], args[1], sample_ckp_names(where));
1609 else
1610 memprintf(err,
1611 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1612 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001613 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001614 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001615
Emeric Brun97679e72010-09-23 17:56:44 +02001616 warn++;
1617 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001618 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1619 if (acl->name && *acl->name)
1620 memprintf(err,
1621 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001622 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001623 else
1624 memprintf(err,
1625 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001626 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001627 warn++;
1628 }
Emeric Brun97679e72010-09-23 17:56:44 +02001629
1630 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1631 }
1632 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001633 memprintf(err,
1634 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1635 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001636 goto error;
1637 }
1638
1639 return warn;
1640 error:
1641 free(rule);
1642 return -1;
1643}
1644
1645
Willy Tarreaub6866442008-07-14 23:54:42 +02001646/* This function should be called to parse a line starting with the "tcp-request"
1647 * keyword.
1648 */
1649static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001650 struct proxy *defpx, const char *file, int line,
1651 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001652{
1653 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001654 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001655 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001656 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001657 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001658 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001659 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001660 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001661
1662 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001663 if (curpx == defpx)
1664 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1665 else
1666 memprintf(err, "missing argument for '%s' in %s '%s'",
1667 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001668 return -1;
1669 }
1670
1671 if (!strcmp(args[1], "inspect-delay")) {
1672 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001673 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1674 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001675 return -1;
1676 }
1677
Willy Tarreaub6866442008-07-14 23:54:42 +02001678 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001679 memprintf(err,
1680 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1681 args[0], args[1], proxy_type_str(curpx), curpx->id);
1682 if (ptr)
1683 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001684 return -1;
1685 }
1686
1687 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001688 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1689 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001690 return 1;
1691 }
1692 curpx->tcp_req.inspect_delay = val;
1693 return 0;
1694 }
1695
Simon Hormande072bd2011-06-24 15:11:37 +09001696 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001697 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001698 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001699 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001700
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001701 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001702 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001703
1704 if (curpx->cap & PR_CAP_FE)
1705 where |= SMP_VAL_FE_REQ_CNT;
1706 if (curpx->cap & PR_CAP_BE)
1707 where |= SMP_VAL_BE_REQ_CNT;
1708
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001709 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001710 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001711
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001712 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1713 if (acl) {
1714 if (acl->name && *acl->name)
1715 memprintf(err,
1716 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1717 acl->name, args[0], args[1], sample_ckp_names(where));
1718 else
1719 memprintf(err,
1720 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1721 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001722 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001723 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001724
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001725 warn++;
1726 }
1727 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1728 if (acl->name && *acl->name)
1729 memprintf(err,
1730 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001731 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001732 else
1733 memprintf(err,
1734 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001735 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001736 warn++;
1737 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001738
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001739 /* the following function directly emits the warning */
1740 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001741 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001742 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001743 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001744 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001745
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001746 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001747 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1748 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001749 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001750 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001751
Willy Tarreau80aca902013-01-07 15:42:20 +01001752 where |= SMP_VAL_FE_CON_ACC;
1753
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001754 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001755 goto error;
1756
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001757 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1758 if (acl) {
1759 if (acl->name && *acl->name)
1760 memprintf(err,
1761 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1762 acl->name, args[0], args[1], sample_ckp_names(where));
1763 else
1764 memprintf(err,
1765 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1766 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001767 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001768 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001769
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001770 warn++;
1771 }
1772 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1773 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001774 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001775 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001776 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001777 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001778 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001779 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001780 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001781 warn++;
1782 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001783
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001784 /* the following function directly emits the warning */
1785 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001786 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001787 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001788 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001789 if (curpx == defpx)
1790 memprintf(err,
1791 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1792 args[0], args[1]);
1793 else
1794 memprintf(err,
1795 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001796 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001797 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001798 }
1799
Willy Tarreau1a687942010-05-23 22:40:30 +02001800 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001801 error:
1802 free(rule);
1803 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001804}
1805
Willy Tarreau645513a2010-05-24 20:55:15 +02001806
1807/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001808/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001809/************************************************************************/
1810
Willy Tarreau4a129812012-04-25 17:31:42 +02001811/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001812static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001813smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001814 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001815{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001816 struct connection *cli_conn = objt_conn(l4->si[0].end);
1817
1818 if (!cli_conn)
1819 return 0;
1820
1821 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001822 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001823 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001824 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001825 break;
1826 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001827 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001828 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001829 break;
1830 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001831 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001832 }
Emeric Brunf769f512010-10-22 17:14:01 +02001833
Willy Tarreau37406352012-04-23 16:16:37 +02001834 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001835 return 1;
1836}
1837
Willy Tarreaua5e37562011-12-16 17:06:15 +01001838/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001839static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001840smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001841 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001842{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001843 struct connection *cli_conn = objt_conn(l4->si[0].end);
1844
1845 if (!cli_conn)
1846 return 0;
1847
Willy Tarreauf853c462012-04-23 18:53:56 +02001848 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001849 if (!(smp->data.uint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001850 return 0;
1851
Willy Tarreau37406352012-04-23 16:16:37 +02001852 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001853 return 1;
1854}
1855
Willy Tarreau4a129812012-04-25 17:31:42 +02001856/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001857static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001858smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001859 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001860{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001861 struct connection *cli_conn = objt_conn(l4->si[0].end);
Willy Tarreau645513a2010-05-24 20:55:15 +02001862
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001863 if (!cli_conn)
1864 return 0;
1865
1866 conn_get_to_addr(cli_conn);
1867
1868 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001869 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001870 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001871 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001872 break;
1873 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001874 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001875 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001876 break;
1877 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001878 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001879 }
Emeric Brunf769f512010-10-22 17:14:01 +02001880
Willy Tarreau37406352012-04-23 16:16:37 +02001881 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001882 return 1;
1883}
1884
Willy Tarreaua5e37562011-12-16 17:06:15 +01001885/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001886static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001887smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001888 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001889{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001890 struct connection *cli_conn = objt_conn(l4->si[0].end);
1891
1892 if (!cli_conn)
1893 return 0;
1894
1895 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001896
Willy Tarreauf853c462012-04-23 18:53:56 +02001897 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001898 if (!(smp->data.uint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001899 return 0;
1900
Willy Tarreau37406352012-04-23 16:16:37 +02001901 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001902 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001903}
1904
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001905#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001906/* parse the "v4v6" bind keyword */
1907static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1908{
1909 struct listener *l;
1910
1911 list_for_each_entry(l, &conf->listeners, by_bind) {
1912 if (l->addr.ss_family == AF_INET6)
1913 l->options |= LI_O_V4V6;
1914 }
1915
1916 return 0;
1917}
1918
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001919/* parse the "v6only" bind keyword */
1920static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1921{
1922 struct listener *l;
1923
1924 list_for_each_entry(l, &conf->listeners, by_bind) {
1925 if (l->addr.ss_family == AF_INET6)
1926 l->options |= LI_O_V6ONLY;
1927 }
1928
1929 return 0;
1930}
1931#endif
1932
Pieter Baauwd551fb52013-05-08 22:49:23 +02001933#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001934/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001935static 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 +02001936{
1937 struct listener *l;
1938
Willy Tarreau4348fad2012-09-20 16:48:07 +02001939 list_for_each_entry(l, &conf->listeners, by_bind) {
1940 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1941 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001942 }
1943
Willy Tarreau44791242012-09-12 23:27:21 +02001944 return 0;
1945}
1946#endif
1947
1948#ifdef TCP_DEFER_ACCEPT
1949/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001950static 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 +02001951{
1952 struct listener *l;
1953
Willy Tarreau4348fad2012-09-20 16:48:07 +02001954 list_for_each_entry(l, &conf->listeners, by_bind) {
1955 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1956 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001957 }
1958
Willy Tarreau44791242012-09-12 23:27:21 +02001959 return 0;
1960}
1961#endif
1962
Willy Tarreau1c862c52012-10-05 16:21:00 +02001963#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001964/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001965static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1966{
1967 struct listener *l;
1968
1969 list_for_each_entry(l, &conf->listeners, by_bind) {
1970 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1971 l->options |= LI_O_TCP_FO;
1972 }
1973
1974 return 0;
1975}
1976#endif
1977
Willy Tarreau44791242012-09-12 23:27:21 +02001978#ifdef TCP_MAXSEG
1979/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001980static 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 +02001981{
1982 struct listener *l;
1983 int mss;
1984
Willy Tarreau44791242012-09-12 23:27:21 +02001985 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001986 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001987 return ERR_ALERT | ERR_FATAL;
1988 }
1989
1990 mss = atoi(args[cur_arg + 1]);
1991 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001992 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001993 return ERR_ALERT | ERR_FATAL;
1994 }
1995
Willy Tarreau4348fad2012-09-20 16:48:07 +02001996 list_for_each_entry(l, &conf->listeners, by_bind) {
1997 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1998 l->maxseg = mss;
1999 }
Willy Tarreau44791242012-09-12 23:27:21 +02002000
2001 return 0;
2002}
2003#endif
2004
2005#ifdef SO_BINDTODEVICE
2006/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002007static 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 +02002008{
2009 struct listener *l;
2010
Willy Tarreau44791242012-09-12 23:27:21 +02002011 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002012 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002013 return ERR_ALERT | ERR_FATAL;
2014 }
2015
Willy Tarreau4348fad2012-09-20 16:48:07 +02002016 list_for_each_entry(l, &conf->listeners, by_bind) {
2017 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2018 l->interface = strdup(args[cur_arg + 1]);
2019 }
Willy Tarreau44791242012-09-12 23:27:21 +02002020
2021 global.last_checks |= LSTCHK_NETADM;
2022 return 0;
2023}
2024#endif
2025
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002026#ifdef CONFIG_HAP_NS
2027/* parse the "namespace" bind keyword */
2028static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2029{
2030 struct listener *l;
2031 char *namespace = NULL;
2032
2033 if (!*args[cur_arg + 1]) {
2034 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
2035 return ERR_ALERT | ERR_FATAL;
2036 }
2037 namespace = args[cur_arg + 1];
2038
2039 list_for_each_entry(l, &conf->listeners, by_bind) {
2040 l->netns = netns_store_lookup(namespace, strlen(namespace));
2041
2042 if (l->netns == NULL)
2043 l->netns = netns_store_insert(namespace);
2044
2045 if (l->netns == NULL) {
2046 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
2047 return ERR_ALERT | ERR_FATAL;
2048 }
2049 }
2050 return 0;
2051}
2052#endif
2053
Willy Tarreaudc13c112013-06-21 23:16:39 +02002054static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002055 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02002056 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02002057 { 0, NULL, NULL },
2058}};
2059
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002060
Willy Tarreau61612d42012-04-19 18:42:05 +02002061/* Note: must not be declared <const> as its list will be overwritten.
2062 * Please take care of keeping this list alphabetically sorted.
2063 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002064static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002065 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02002066}};
2067
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002068
Willy Tarreau4a129812012-04-25 17:31:42 +02002069/* Note: must not be declared <const> as its list will be overwritten.
2070 * Note: fetches that may return multiple types must be declared as the lowest
2071 * common denominator, the type that can be casted into all other ones. For
2072 * instance v4/v6 must be declared v4.
2073 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002074static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002075 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
2076 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
2077 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
2078 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
2079 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002080}};
2081
Willy Tarreau44791242012-09-12 23:27:21 +02002082/************************************************************************/
2083/* All supported bind keywords must be declared here. */
2084/************************************************************************/
2085
2086/* Note: must not be declared <const> as its list will be overwritten.
2087 * Please take care of keeping this list alphabetically sorted, doing so helps
2088 * all code contributors.
2089 * Optional keywords are also declared with a NULL ->parse() function so that
2090 * the config parser can report an appropriate error when a known keyword was
2091 * not enabled.
2092 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002093static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02002094#ifdef TCP_DEFER_ACCEPT
2095 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
2096#endif
2097#ifdef SO_BINDTODEVICE
2098 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
2099#endif
2100#ifdef TCP_MAXSEG
2101 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
2102#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02002103#ifdef TCP_FASTOPEN
2104 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
2105#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02002106#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002107 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
2108#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002109#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002110 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002111 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
2112#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002113#ifdef CONFIG_HAP_NS
2114 { "namespace", bind_parse_namespace, 1 },
2115#endif
Willy Tarreau44791242012-09-12 23:27:21 +02002116 /* the versions with the NULL parse function*/
2117 { "defer-accept", NULL, 0 },
2118 { "interface", NULL, 1 },
2119 { "mss", NULL, 1 },
2120 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002121 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002122 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002123 { NULL, NULL, 0 },
2124}};
2125
Willy Tarreaue6b98942007-10-29 01:09:36 +01002126__attribute__((constructor))
2127static void __tcp_protocol_init(void)
2128{
2129 protocol_register(&proto_tcpv4);
2130 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002131 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02002132 cfg_register_keywords(&cfg_kws);
2133 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02002134 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002135}
2136
2137
2138/*
2139 * Local variables:
2140 * c-indent-level: 8
2141 * c-basic-offset: 8
2142 * End:
2143 */