blob: 83d568c1948c18b9378aec9f0c32d4dd524253f7 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreau1a687942010-05-23 22:40:30 +02004 * Copyright 2000-2010 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>
Willy Tarreaue6b98942007-10-29 01:09:36 +010036
Willy Tarreaue6b98942007-10-29 01:09:36 +010037#include <types/global.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020038#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039
40#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020041#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020044#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020046#include <proto/log.h>
47#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020048#include <proto/protocol.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010049#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020051#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020052#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/stick_table.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020054#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010055
Willy Tarreaue8c66af2008-01-13 18:40:14 +010056#ifdef CONFIG_HAP_CTTPROXY
57#include <import/ip_tproxy.h>
58#endif
59
Emeric Bruncf20bf12010-10-22 16:06:11 +020060static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
61static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010062
63/* Note: must not be declared <const> as its list will be overwritten */
64static struct protocol proto_tcpv4 = {
65 .name = "tcpv4",
66 .sock_domain = AF_INET,
67 .sock_type = SOCK_STREAM,
68 .sock_prot = IPPROTO_TCP,
69 .sock_family = AF_INET,
70 .sock_addrlen = sizeof(struct sockaddr_in),
71 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020072 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020073 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020074 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010075 .bind_all = tcp_bind_listeners,
76 .unbind_all = unbind_all_listeners,
77 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020078 .get_src = tcp_get_src,
79 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010080 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
81 .nb_listeners = 0,
82};
83
84/* Note: must not be declared <const> as its list will be overwritten */
85static struct protocol proto_tcpv6 = {
86 .name = "tcpv6",
87 .sock_domain = AF_INET6,
88 .sock_type = SOCK_STREAM,
89 .sock_prot = IPPROTO_TCP,
90 .sock_family = AF_INET6,
91 .sock_addrlen = sizeof(struct sockaddr_in6),
92 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020093 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020094 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020095 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010096 .bind_all = tcp_bind_listeners,
97 .unbind_all = unbind_all_listeners,
98 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020099 .get_src = tcp_get_src,
100 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100101 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
102 .nb_listeners = 0,
103};
104
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100105
David du Colombier6f5ccb12011-03-10 22:26:24 +0100106/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100107 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
108 * - 0 : ignore remote address (may even be a NULL pointer)
109 * - 1 : use provided address
110 * - 2 : use provided port
111 * - 3 : use both
112 *
113 * The function supports multiple foreign binding methods :
114 * - linux_tproxy: we directly bind to the foreign address
115 * - cttproxy: we bind to a local address then nat.
116 * The second one can be used as a fallback for the first one.
117 * This function returns 0 when everything's OK, 1 if it could not bind, to the
118 * local address, 2 if it could not bind to the foreign address.
119 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100120int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100121{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100122 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100123 int foreign_ok = 0;
124 int ret;
125
126#ifdef CONFIG_HAP_LINUX_TPROXY
127 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200128 static int ip6_transp_working = 1;
129 switch (local->ss_family) {
130 case AF_INET:
131 if (flags && ip_transp_working) {
132 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
133 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
134 foreign_ok = 1;
135 else
136 ip_transp_working = 0;
137 }
138 break;
139 case AF_INET6:
140 if (flags && ip6_transp_working) {
141 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
142 foreign_ok = 1;
143 else
144 ip6_transp_working = 0;
145 }
146 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100147 }
148#endif
149 if (flags) {
150 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200151 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100152 switch (remote->ss_family) {
153 case AF_INET:
154 if (flags & 1)
155 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
156 if (flags & 2)
157 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
158 break;
159 case AF_INET6:
160 if (flags & 1)
161 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
162 if (flags & 2)
163 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
164 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100165 default:
166 /* we don't want to try to bind to an unknown address family */
167 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100168 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100169 }
170
Simon Hormande072bd2011-06-24 15:11:37 +0900171 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100172 if (foreign_ok) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200173 if (is_addr(&bind_addr)) {
174 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
175 if (ret < 0)
176 return 2;
177 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100178 }
179 else {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200180 if (is_addr(local)) {
181 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
182 if (ret < 0)
183 return 1;
184 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100185 }
186
187 if (!flags)
188 return 0;
189
190#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100191 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100192 struct in_tproxy itp1, itp2;
193 memset(&itp1, 0, sizeof(itp1));
194
195 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100196 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
197 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100198
199 /* set connect flag on socket */
200 itp2.op = TPROXY_FLAGS;
201 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
202
203 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
204 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
205 foreign_ok = 1;
206 }
207 }
208#endif
209 if (!foreign_ok)
210 /* we could not bind to a foreign address */
211 return 2;
212
213 return 0;
214}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100215
Willy Tarreau9650f372009-08-16 14:02:45 +0200216
217/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200218 * This function initiates a TCP connection establishment to the target assigned
219 * to connection <conn> using (si->{target,addr.to}). A source address may be
220 * pointed to by conn->addr.from in case of transparent proxying. Normal source
221 * bind addresses are still determined locally (due to the possible need of a
222 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100223 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100224 * supported. The <data> parameter is a boolean indicating whether there are data
225 * waiting for being sent or not, in order to adjust data write polling and on
226 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
227 * allows the caller to force using a delayed ACK when establishing the connection :
228 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
229 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
230 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200231 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200232 * It can return one of :
233 * - SN_ERR_NONE if everything's OK
234 * - SN_ERR_SRVTO if there are no more servers
235 * - SN_ERR_SRVCL if the connection was refused by the server
236 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
237 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
238 * - SN_ERR_INTERNAL for any other purely internal errors
239 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100240 *
241 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
242 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200243 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100244
Willy Tarreauf0837b22012-11-24 10:24:27 +0100245int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200246{
247 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100248 struct server *srv;
249 struct proxy *be;
250
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100251 switch (obj_type(conn->target)) {
252 case OBJ_TYPE_PROXY:
253 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100254 srv = NULL;
255 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100256 case OBJ_TYPE_SERVER:
257 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100258 be = srv->proxy;
259 break;
260 default:
261 return SN_ERR_INTERNAL;
262 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200263
Willy Tarreau986a9d22012-08-30 21:11:38 +0200264 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200265 qfprintf(stderr, "Cannot get a server socket.\n");
266
267 if (errno == ENFILE)
268 send_log(be, LOG_EMERG,
269 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
270 be->id, maxfd);
271 else if (errno == EMFILE)
272 send_log(be, LOG_EMERG,
273 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
274 be->id, maxfd);
275 else if (errno == ENOBUFS || errno == ENOMEM)
276 send_log(be, LOG_EMERG,
277 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
278 be->id, maxfd);
279 /* this is a resource error */
280 return SN_ERR_RESOURCE;
281 }
282
283 if (fd >= global.maxsock) {
284 /* do not log anything there, it's a normal condition when this option
285 * is used to serialize connections to a server !
286 */
287 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
288 close(fd);
289 return SN_ERR_PRXCOND; /* it is a configuration limit */
290 }
291
292 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900293 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200294 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
295 close(fd);
296 return SN_ERR_INTERNAL;
297 }
298
299 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900300 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200301
Willy Tarreau9650f372009-08-16 14:02:45 +0200302 /* allow specific binding :
303 * - server-specific at first
304 * - proxy-specific next
305 */
Willy Tarreauef9a3602012-12-08 22:29:20 +0100306 if (srv != NULL && srv->conn_src.opts & CO_SRC_BIND) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200307 int ret, flags = 0;
308
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200309 if (is_addr(&conn->addr.from)) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100310 switch (srv->conn_src.opts & CO_SRC_TPROXY_MASK) {
311 case CO_SRC_TPROXY_ADDR:
312 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200313 flags = 3;
314 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100315 case CO_SRC_TPROXY_CIP:
316 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200317 flags = 1;
318 break;
319 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200320 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200321
Willy Tarreau9650f372009-08-16 14:02:45 +0200322#ifdef SO_BINDTODEVICE
323 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreauef9a3602012-12-08 22:29:20 +0100324 if (srv->conn_src.iface_name)
325 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->conn_src.iface_name, srv->conn_src.iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200326#endif
327
Willy Tarreauef9a3602012-12-08 22:29:20 +0100328 if (srv->conn_src.sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200329 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100330 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200331
332 ret = 1;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100333 src = srv->conn_src.source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200334
335 do {
336 /* note: in case of retry, we may have to release a previously
337 * allocated port, hence this loop's construct.
338 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200339 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
340 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200341
342 if (!attempts)
343 break;
344 attempts--;
345
Willy Tarreauef9a3602012-12-08 22:29:20 +0100346 fdinfo[fd].local_port = port_range_alloc_port(srv->conn_src.sport_range);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200347 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200348 break;
349
Willy Tarreauef9a3602012-12-08 22:29:20 +0100350 fdinfo[fd].port_range = srv->conn_src.sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200351 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200352
Willy Tarreau986a9d22012-08-30 21:11:38 +0200353 ret = tcp_bind_socket(fd, flags, &src, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200354 } while (ret != 0); /* binding NOK */
355 }
356 else {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100357 ret = tcp_bind_socket(fd, flags, &srv->conn_src.source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200358 }
359
360 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200361 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
362 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200363 close(fd);
364
365 if (ret == 1) {
366 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
367 be->id, srv->id);
368 send_log(be, LOG_EMERG,
369 "Cannot bind to source address before connect() for server %s/%s.\n",
370 be->id, srv->id);
371 } else {
372 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
373 be->id, srv->id);
374 send_log(be, LOG_EMERG,
375 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
376 be->id, srv->id);
377 }
378 return SN_ERR_RESOURCE;
379 }
380 }
Willy Tarreauef9a3602012-12-08 22:29:20 +0100381 else if (be->conn_src.opts & CO_SRC_BIND) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200382 int ret, flags = 0;
383
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200384 if (is_addr(&conn->addr.from)) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100385 switch (be->conn_src.opts & CO_SRC_BIND) {
386 case CO_SRC_TPROXY_ADDR:
387 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200388 flags = 3;
389 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100390 case CO_SRC_TPROXY_CIP:
391 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200392 flags = 1;
393 break;
394 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200395 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200396
Willy Tarreau9650f372009-08-16 14:02:45 +0200397#ifdef SO_BINDTODEVICE
398 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreauef9a3602012-12-08 22:29:20 +0100399 if (be->conn_src.iface_name)
400 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->conn_src.iface_name, be->conn_src.iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200401#endif
Willy Tarreauef9a3602012-12-08 22:29:20 +0100402 ret = tcp_bind_socket(fd, flags, &be->conn_src.source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200403 if (ret) {
404 close(fd);
405 if (ret == 1) {
406 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
407 be->id);
408 send_log(be, LOG_EMERG,
409 "Cannot bind to source address before connect() for proxy %s.\n",
410 be->id);
411 } else {
412 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
413 be->id);
414 send_log(be, LOG_EMERG,
415 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
416 be->id);
417 }
418 return SN_ERR_RESOURCE;
419 }
420 }
421
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400422#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200423 /* disabling tcp quick ack now allows the first request to leave the
424 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100425 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200426 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100427 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900428 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200429#endif
430
Willy Tarreaue803de22010-01-21 17:43:04 +0100431 if (global.tune.server_sndbuf)
432 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
433
434 if (global.tune.server_rcvbuf)
435 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
436
Willy Tarreau986a9d22012-08-30 21:11:38 +0200437 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
439
Willy Tarreaub1719512012-12-08 23:03:28 +0100440 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200441 char *msg;
Willy Tarreaub1719512012-12-08 23:03:28 +0100442 if (errno == EAGAIN || errno == EADDRNOTAVAIL)
Willy Tarreau9650f372009-08-16 14:02:45 +0200443 msg = "no free ports";
444 else
445 msg = "local address already in use";
446
Willy Tarreaub1719512012-12-08 23:03:28 +0100447 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200448 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
449 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200450 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100451 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 return SN_ERR_RESOURCE;
453 } else if (errno == ETIMEDOUT) {
454 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200455 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
456 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200457 close(fd);
458 return SN_ERR_SRVTO;
459 } else {
460 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
461 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200462 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
463 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200464 close(fd);
465 return SN_ERR_SRVCL;
466 }
467 }
468
Willy Tarreau986a9d22012-08-30 21:11:38 +0200469 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200470 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100471 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200472
Willy Tarreaud2274c62012-07-06 14:29:45 +0200473 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200474 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200475 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200476
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200477 if (conn_xprt_init(conn) < 0) {
Willy Tarreau184636e2012-09-06 14:04:41 +0200478 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200479 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200480 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200481
Willy Tarreau14f8e862012-08-30 22:23:13 +0200482 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200483 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200484
Willy Tarreau9650f372009-08-16 14:02:45 +0200485 return SN_ERR_NONE; /* connection is OK */
486}
487
488
Willy Tarreau59b94792012-05-11 16:16:40 +0200489/*
490 * Retrieves the source address for the socket <fd>, with <dir> indicating
491 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
492 * success, -1 in case of error. The socket's source address is stored in
493 * <sa> for <salen> bytes.
494 */
495int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
496{
497 if (dir)
498 return getsockname(fd, sa, &salen);
499 else
500 return getpeername(fd, sa, &salen);
501}
502
503
504/*
505 * Retrieves the original destination address for the socket <fd>, with <dir>
506 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
507 * listener, if the original destination address was translated, the original
508 * address is retrieved. It returns 0 in case of success, -1 in case of error.
509 * The socket's source address is stored in <sa> for <salen> bytes.
510 */
511int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
512{
513 if (dir)
514 return getpeername(fd, sa, &salen);
515#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
516 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
517 return 0;
518#endif
519 else
520 return getsockname(fd, sa, &salen);
521}
522
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200523/* This is the callback which is set when a connection establishment is pending
524 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200525 * once the connection is established. It updates the FD polling status. It
526 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
527 * it returns non-zero and removes itself from the connection's flags (the bit is
528 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200529 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200530int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200531{
Willy Tarreau239d7182012-07-23 18:53:03 +0200532 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200533
Willy Tarreau80184712012-07-06 14:54:49 +0200534 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200535 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200536
Willy Tarreau80184712012-07-06 14:54:49 +0200537 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200538 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200539
Willy Tarreau2da156f2012-07-23 15:07:23 +0200540 /* stop here if we reached the end of data */
541 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
542 goto out_error;
543
Willy Tarreau2c6be842012-07-06 17:12:34 +0200544 /* We have no data to send to check the connection, and
545 * getsockopt() will not inform us whether the connection
546 * is still pending. So we'll reuse connect() to check the
547 * state of the socket. This has the advantage of giving us
548 * the following info :
549 * - error
550 * - connecting (EALREADY, EINPROGRESS)
551 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200552 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200553 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200554 if (errno == EALREADY || errno == EINPROGRESS) {
555 conn_sock_stop_recv(conn);
556 conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200557 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200558 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200559
Willy Tarreau2c6be842012-07-06 17:12:34 +0200560 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200561 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200562
Willy Tarreau2c6be842012-07-06 17:12:34 +0200563 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200564 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200565
Willy Tarreau076be252012-07-06 16:02:29 +0200566 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200567 * forward the event to the transport layer which will notify the
568 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200569 */
Willy Tarreau80184712012-07-06 14:54:49 +0200570 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200571 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200572
573 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200574 /* Write error on the file descriptor. Report it to the connection
575 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200576 */
577
Willy Tarreau80184712012-07-06 14:54:49 +0200578 conn->flags |= CO_FL_ERROR;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200579 conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200580 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200581}
582
Willy Tarreau59b94792012-05-11 16:16:40 +0200583
Willy Tarreaue6b98942007-10-29 01:09:36 +0100584/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
585 * an error message in <err> if the message is at most <errlen> bytes long
586 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
587 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
588 * was alright and that no message was returned. ERR_RETRYABLE means that an
589 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700590 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100591 * the meaning of the error, but just indicate that a message is present which
592 * should be displayed with the respective level. Last, ERR_ABORT indicates
593 * that it's pointless to try to start other listeners. No error message is
594 * returned if errlen is NULL.
595 */
596int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
597{
598 __label__ tcp_return, tcp_close_return;
599 int fd, err;
600 const char *msg = NULL;
601
602 /* ensure we never return garbage */
603 if (errmsg && errlen)
604 *errmsg = 0;
605
606 if (listener->state != LI_ASSIGNED)
607 return ERR_NONE; /* already bound */
608
609 err = ERR_NONE;
610
611 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
612 err |= ERR_RETRYABLE | ERR_ALERT;
613 msg = "cannot create listening socket";
614 goto tcp_return;
615 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100616
Willy Tarreaue6b98942007-10-29 01:09:36 +0100617 if (fd >= global.maxsock) {
618 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
619 msg = "not enough free sockets (raise '-n' parameter)";
620 goto tcp_close_return;
621 }
622
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200623 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100624 err |= ERR_FATAL | ERR_ALERT;
625 msg = "cannot make socket non-blocking";
626 goto tcp_close_return;
627 }
628
Simon Hormande072bd2011-06-24 15:11:37 +0900629 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100630 /* not fatal but should be reported */
631 msg = "cannot do so_reuseaddr";
632 err |= ERR_ALERT;
633 }
634
635 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900636 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100637
Willy Tarreaue6b98942007-10-29 01:09:36 +0100638#ifdef SO_REUSEPORT
639 /* OpenBSD supports this. As it's present in old libc versions of Linux,
640 * it might return an error that we will silently ignore.
641 */
Simon Hormande072bd2011-06-24 15:11:37 +0900642 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100643#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100644#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200645 if (listener->options & LI_O_FOREIGN) {
646 switch (listener->addr.ss_family) {
647 case AF_INET:
648 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
649 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
650 msg = "cannot make listening socket transparent";
651 err |= ERR_ALERT;
652 }
653 break;
654 case AF_INET6:
655 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
656 msg = "cannot make listening socket transparent";
657 err |= ERR_ALERT;
658 }
659 break;
660 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100661 }
662#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100663#ifdef SO_BINDTODEVICE
664 /* Note: this might fail if not CAP_NET_RAW */
665 if (listener->interface) {
666 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100667 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100668 msg = "cannot bind listener to device";
669 err |= ERR_WARN;
670 }
671 }
672#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400673#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100674 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400675 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200676 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
677 msg = "cannot set MSS";
678 err |= ERR_WARN;
679 }
680 }
681#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200682#if defined(TCP_DEFER_ACCEPT)
683 if (listener->options & LI_O_DEF_ACCEPT) {
684 /* defer accept by up to one second */
685 int accept_delay = 1;
686 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
687 msg = "cannot enable DEFER_ACCEPT";
688 err |= ERR_WARN;
689 }
690 }
691#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200692#if defined(TCP_FASTOPEN)
693 if (listener->options & LI_O_TCP_FO) {
694 /* TFO needs a queue length, let's use the configured backlog */
695 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
696 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
697 msg = "cannot enable TCP_FASTOPEN";
698 err |= ERR_WARN;
699 }
700 }
701#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100702#if defined(IPV6_V6ONLY)
703 if (listener->options & LI_O_V6ONLY)
704 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100705 else if (listener->options & LI_O_V4V6)
706 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100707#endif
708
Willy Tarreaue6b98942007-10-29 01:09:36 +0100709 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
710 err |= ERR_RETRYABLE | ERR_ALERT;
711 msg = "cannot bind socket";
712 goto tcp_close_return;
713 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100714
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100715 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100716 err |= ERR_RETRYABLE | ERR_ALERT;
717 msg = "cannot listen to socket";
718 goto tcp_close_return;
719 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100720
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400721#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200722 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900723 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200724#endif
725
Willy Tarreaue6b98942007-10-29 01:09:36 +0100726 /* the socket is ready */
727 listener->fd = fd;
728 listener->state = LI_LISTEN;
729
Willy Tarreaueabf3132008-08-29 23:36:51 +0200730 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200731 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200732 fd_insert(fd);
733
Willy Tarreaue6b98942007-10-29 01:09:36 +0100734 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100735 if (msg && errlen) {
736 char pn[INET6_ADDRSTRLEN];
737
Willy Tarreau631f01c2011-09-05 00:36:48 +0200738 addr_to_str(&listener->addr, pn, sizeof(pn));
739 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100740 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100741 return err;
742
743 tcp_close_return:
744 close(fd);
745 goto tcp_return;
746}
747
748/* This function creates all TCP sockets bound to the protocol entry <proto>.
749 * It is intended to be used as the protocol's bind_all() function.
750 * The sockets will be registered but not added to any fd_set, in order not to
751 * loose them across the fork(). A call to enable_all_listeners() is needed
752 * to complete initialization. The return value is composed from ERR_*.
753 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200754static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100755{
756 struct listener *listener;
757 int err = ERR_NONE;
758
759 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200760 err |= tcp_bind_listener(listener, errmsg, errlen);
761 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100762 break;
763 }
764
765 return err;
766}
767
768/* Add listener to the list of tcpv4 listeners. The listener's state
769 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
770 * listeners is updated. This is the function to use to add a new listener.
771 */
772void tcpv4_add_listener(struct listener *listener)
773{
774 if (listener->state != LI_INIT)
775 return;
776 listener->state = LI_ASSIGNED;
777 listener->proto = &proto_tcpv4;
778 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
779 proto_tcpv4.nb_listeners++;
780}
781
782/* Add listener to the list of tcpv4 listeners. The listener's state
783 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
784 * listeners is updated. This is the function to use to add a new listener.
785 */
786void tcpv6_add_listener(struct listener *listener)
787{
788 if (listener->state != LI_INIT)
789 return;
790 listener->state = LI_ASSIGNED;
791 listener->proto = &proto_tcpv6;
792 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
793 proto_tcpv6.nb_listeners++;
794}
795
Willy Tarreauedcf6682008-11-30 23:15:34 +0100796/* This function performs the TCP request analysis on the current request. It
797 * returns 1 if the processing can continue on next analysers, or zero if it
798 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200799 * request. It relies on buffers flags, and updates s->req->analysers. The
800 * function may be called for frontend rules and backend rules. It only relies
801 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100802 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200803int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100804{
805 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200806 struct stksess *ts;
807 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100808 int partial;
809
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100810 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 +0100811 now_ms, __FUNCTION__,
812 s,
813 req,
814 req->rex, req->wex,
815 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200816 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100817 req->analysers);
818
Willy Tarreauedcf6682008-11-30 23:15:34 +0100819 /* We don't know whether we have enough data, so must proceed
820 * this way :
821 * - iterate through all rules in their declaration order
822 * - if one rule returns MISS, it means the inspect delay is
823 * not over yet, then return immediately, otherwise consider
824 * it as a non-match.
825 * - if one rule returns OK, then return OK
826 * - if one rule returns KO, then return KO
827 */
828
Willy Tarreau9b28e032012-10-12 23:49:43 +0200829 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200830 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200831 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100832 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200833 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100834
Willy Tarreaufb356202010-08-03 14:02:05 +0200835 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100836 int ret = ACL_PAT_PASS;
837
838 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200839 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100840 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200841 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100842 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200843 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
844 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100845 return 0;
846 }
847
848 ret = acl_pass(ret);
849 if (rule->cond->pol == ACL_COND_UNLESS)
850 ret = !ret;
851 }
852
853 if (ret) {
854 /* we have a matching rule. */
855 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200856 channel_abort(req);
857 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100858 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200859
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100860 s->be->be_counters.denied_req++;
861 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200862 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200863 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200864
Willy Tarreauedcf6682008-11-30 23:15:34 +0100865 if (!(s->flags & SN_ERR_MASK))
866 s->flags |= SN_ERR_PRXCOND;
867 if (!(s->flags & SN_FINST_MASK))
868 s->flags |= SN_FINST_R;
869 return 0;
870 }
Willy Tarreau56123282010-08-06 19:06:56 +0200871 else if (rule->action == TCP_ACT_TRK_SC1) {
872 if (!s->stkctr1_entry) {
873 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200874 * Also, note that right now we can only track SRC so we
875 * don't check how to get the key, but later we may need
876 * to consider rule->act_prm->trk_ctr.type.
877 */
878 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200879 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn->addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200880 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200881 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200882 if (s->fe != s->be)
883 s->flags |= SN_BE_TRACK_SC1;
884 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200885 }
886 }
Willy Tarreau56123282010-08-06 19:06:56 +0200887 else if (rule->action == TCP_ACT_TRK_SC2) {
888 if (!s->stkctr2_entry) {
889 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200890 * Also, note that right now we can only track SRC so we
891 * don't check how to get the key, but later we may need
892 * to consider rule->act_prm->trk_ctr.type.
893 */
894 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200895 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn->addr.from));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200896 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200897 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200898 if (s->fe != s->be)
899 s->flags |= SN_BE_TRACK_SC2;
900 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200901 }
902 }
903 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100904 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200905 break;
906 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100907 }
908 }
909
910 /* if we get there, it means we have no rule which matches, or
911 * we have an explicit accept, so we apply the default accept.
912 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200913 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100914 req->analyse_exp = TICK_ETERNITY;
915 return 1;
916}
917
Emeric Brun97679e72010-09-23 17:56:44 +0200918/* This function performs the TCP response analysis on the current response. It
919 * returns 1 if the processing can continue on next analysers, or zero if it
920 * needs more data, encounters an error, or wants to immediately abort the
921 * response. It relies on buffers flags, and updates s->rep->analysers. The
922 * function may be called for backend rules.
923 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200924int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200925{
926 struct tcp_rule *rule;
927 int partial;
928
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100929 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 +0200930 now_ms, __FUNCTION__,
931 s,
932 rep,
933 rep->rex, rep->wex,
934 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200935 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200936 rep->analysers);
937
938 /* We don't know whether we have enough data, so must proceed
939 * this way :
940 * - iterate through all rules in their declaration order
941 * - if one rule returns MISS, it means the inspect delay is
942 * not over yet, then return immediately, otherwise consider
943 * it as a non-match.
944 * - if one rule returns OK, then return OK
945 * - if one rule returns KO, then return KO
946 */
947
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200948 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200949 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200950 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200951 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200952
953 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
954 int ret = ACL_PAT_PASS;
955
956 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200957 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200958 if (ret == ACL_PAT_MISS) {
959 /* just set the analyser timeout once at the beginning of the response */
960 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
961 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
962 return 0;
963 }
964
965 ret = acl_pass(ret);
966 if (rule->cond->pol == ACL_COND_UNLESS)
967 ret = !ret;
968 }
969
970 if (ret) {
971 /* we have a matching rule. */
972 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200973 channel_abort(rep);
974 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200975 rep->analysers = 0;
976
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100977 s->be->be_counters.denied_resp++;
978 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200979 if (s->listener->counters)
980 s->listener->counters->denied_resp++;
981
982 if (!(s->flags & SN_ERR_MASK))
983 s->flags |= SN_ERR_PRXCOND;
984 if (!(s->flags & SN_FINST_MASK))
985 s->flags |= SN_FINST_D;
986 return 0;
987 }
988 else {
989 /* otherwise accept */
990 break;
991 }
992 }
993 }
994
995 /* if we get there, it means we have no rule which matches, or
996 * we have an explicit accept, so we apply the default accept.
997 */
998 rep->analysers &= ~an_bit;
999 rep->analyse_exp = TICK_ETERNITY;
1000 return 1;
1001}
1002
1003
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001004/* This function performs the TCP layer4 analysis on the current request. It
1005 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1006 * matches or if no more rule matches. It can only use rules which don't need
1007 * any data.
1008 */
1009int tcp_exec_req_rules(struct session *s)
1010{
1011 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001012 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001013 struct stktable *t = NULL;
1014 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001015 int ret;
1016
1017 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1018 ret = ACL_PAT_PASS;
1019
1020 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001021 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001022 ret = acl_pass(ret);
1023 if (rule->cond->pol == ACL_COND_UNLESS)
1024 ret = !ret;
1025 }
1026
1027 if (ret) {
1028 /* we have a matching rule. */
1029 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001030 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001031 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001032 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001033
1034 if (!(s->flags & SN_ERR_MASK))
1035 s->flags |= SN_ERR_PRXCOND;
1036 if (!(s->flags & SN_FINST_MASK))
1037 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001038 result = 0;
1039 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001040 }
Willy Tarreau56123282010-08-06 19:06:56 +02001041 else if (rule->action == TCP_ACT_TRK_SC1) {
1042 if (!s->stkctr1_entry) {
1043 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001044 * Also, note that right now we can only track SRC so we
1045 * don't check how to get the key, but later we may need
1046 * to consider rule->act_prm->trk_ctr.type.
1047 */
1048 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001049 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn->addr.from));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001050 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001051 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001052 }
1053 }
Willy Tarreau56123282010-08-06 19:06:56 +02001054 else if (rule->action == TCP_ACT_TRK_SC2) {
1055 if (!s->stkctr2_entry) {
1056 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001057 * Also, note that right now we can only track SRC so we
1058 * don't check how to get the key, but later we may need
1059 * to consider rule->act_prm->trk_ctr.type.
1060 */
1061 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001062 ts = stktable_get_entry(t, addr_to_stktable_key(&s->si[0].conn->addr.from));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001063 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001064 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001065 }
1066 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001067 else {
1068 /* otherwise it's an accept */
1069 break;
1070 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001071 }
1072 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001073 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001074}
1075
Emeric Brun97679e72010-09-23 17:56:44 +02001076/* Parse a tcp-response rule. Return a negative value in case of failure */
1077static int tcp_parse_response_rule(char **args, int arg, int section_type,
1078 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001079 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001080{
1081 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001082 memprintf(err, "%s %s is only allowed in 'backend' sections",
1083 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001084 return -1;
1085 }
1086
1087 if (strcmp(args[arg], "accept") == 0) {
1088 arg++;
1089 rule->action = TCP_ACT_ACCEPT;
1090 }
1091 else if (strcmp(args[arg], "reject") == 0) {
1092 arg++;
1093 rule->action = TCP_ACT_REJECT;
1094 }
1095 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001096 memprintf(err,
1097 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1098 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001099 return -1;
1100 }
1101
1102 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001103 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1104 memprintf(err,
1105 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1106 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001107 return -1;
1108 }
1109 }
1110 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001111 memprintf(err,
1112 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001113 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1114 return -1;
1115 }
1116 return 0;
1117}
1118
1119
1120
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001121/* Parse a tcp-request rule. Return a negative value in case of failure */
1122static int tcp_parse_request_rule(char **args, int arg, int section_type,
1123 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001124 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001125{
1126 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001127 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1128 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001129 return -1;
1130 }
1131
1132 if (!strcmp(args[arg], "accept")) {
1133 arg++;
1134 rule->action = TCP_ACT_ACCEPT;
1135 }
1136 else if (!strcmp(args[arg], "reject")) {
1137 arg++;
1138 rule->action = TCP_ACT_REJECT;
1139 }
Willy Tarreau56123282010-08-06 19:06:56 +02001140 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001141 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001142 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001143
1144 arg++;
1145 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001146 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001147
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001148 if (ret < 0) { /* nb: warnings are not handled yet */
1149 memprintf(err,
1150 "'%s %s %s' : %s in %s '%s'",
1151 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1152 return ret;
1153 }
Willy Tarreau56123282010-08-06 19:06:56 +02001154 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001155 }
Willy Tarreau56123282010-08-06 19:06:56 +02001156 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001157 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001158 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001159
1160 arg++;
1161 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001162 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001163
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001164 if (ret < 0) { /* nb: warnings are not handled yet */
1165 memprintf(err,
1166 "'%s %s %s' : %s in %s '%s'",
1167 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1168 return ret;
1169 }
Willy Tarreau56123282010-08-06 19:06:56 +02001170 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001171 }
1172 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001173 memprintf(err,
1174 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1175 "or 'track-sc2' in %s '%s' (got '%s')",
1176 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001177 return -1;
1178 }
1179
1180 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001181 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1182 memprintf(err,
1183 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1184 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001185 return -1;
1186 }
1187 }
1188 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001189 memprintf(err,
1190 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001191 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1192 return -1;
1193 }
1194 return 0;
1195}
1196
Emeric Brun97679e72010-09-23 17:56:44 +02001197/* This function should be called to parse a line starting with the "tcp-response"
1198 * keyword.
1199 */
1200static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001201 struct proxy *defpx, const char *file, int line,
1202 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001203{
1204 const char *ptr = NULL;
1205 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001206 int warn = 0;
1207 int arg;
1208 struct tcp_rule *rule;
1209
1210 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001211 memprintf(err, "missing argument for '%s' in %s '%s'",
1212 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001213 return -1;
1214 }
1215
1216 if (strcmp(args[1], "inspect-delay") == 0) {
1217 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001218 memprintf(err, "%s %s is only allowed in 'backend' sections",
1219 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001220 return -1;
1221 }
1222
1223 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001224 memprintf(err,
1225 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1226 args[0], args[1], proxy_type_str(curpx), curpx->id);
1227 if (ptr)
1228 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001229 return -1;
1230 }
1231
1232 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001233 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1234 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001235 return 1;
1236 }
1237 curpx->tcp_rep.inspect_delay = val;
1238 return 0;
1239 }
1240
Simon Hormande072bd2011-06-24 15:11:37 +09001241 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001242 LIST_INIT(&rule->list);
1243 arg = 1;
1244
1245 if (strcmp(args[1], "content") == 0) {
1246 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001247 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001248 goto error;
1249
1250 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1251 struct acl *acl;
1252 const char *name;
1253
1254 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1255 name = acl ? acl->name : "(unknown)";
1256
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001257 memprintf(err,
1258 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1259 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001260 warn++;
1261 }
1262
1263 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1264 }
1265 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001266 memprintf(err,
1267 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1268 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001269 goto error;
1270 }
1271
1272 return warn;
1273 error:
1274 free(rule);
1275 return -1;
1276}
1277
1278
Willy Tarreaub6866442008-07-14 23:54:42 +02001279/* This function should be called to parse a line starting with the "tcp-request"
1280 * keyword.
1281 */
1282static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001283 struct proxy *defpx, const char *file, int line,
1284 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001285{
1286 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001287 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001288 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001289 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001290 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001291
1292 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001293 if (curpx == defpx)
1294 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1295 else
1296 memprintf(err, "missing argument for '%s' in %s '%s'",
1297 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001298 return -1;
1299 }
1300
1301 if (!strcmp(args[1], "inspect-delay")) {
1302 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001303 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1304 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001305 return -1;
1306 }
1307
Willy Tarreaub6866442008-07-14 23:54:42 +02001308 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001309 memprintf(err,
1310 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1311 args[0], args[1], proxy_type_str(curpx), curpx->id);
1312 if (ptr)
1313 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001314 return -1;
1315 }
1316
1317 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001318 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1319 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001320 return 1;
1321 }
1322 curpx->tcp_req.inspect_delay = val;
1323 return 0;
1324 }
1325
Simon Hormande072bd2011-06-24 15:11:37 +09001326 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001327 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001328 arg = 1;
1329
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001330 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001331 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001332 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001333 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001334
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001335 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001336 struct acl *acl;
1337 const char *name;
1338
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001339 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001340 name = acl ? acl->name : "(unknown)";
1341
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001342 memprintf(err,
1343 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1344 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001345 warn++;
1346 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001347 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001348 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001349 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001350 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001351
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001352 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001353 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1354 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001355 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001356 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001357
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001358 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001359 goto error;
1360
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001361 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1362 struct acl *acl;
1363 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001364
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001365 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1366 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001367
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001368 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001369 memprintf(err,
1370 "'%s %s' may not reference acl '%s' which makes use of "
1371 "payload in %s '%s'. Please use '%s content' for this.",
1372 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001373 goto error;
1374 }
1375 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001376 memprintf(err,
1377 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1378 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001379 warn++;
1380 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001381 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001382 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001383 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001384 if (curpx == defpx)
1385 memprintf(err,
1386 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1387 args[0], args[1]);
1388 else
1389 memprintf(err,
1390 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1391 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001392 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001393 }
1394
Willy Tarreau1a687942010-05-23 22:40:30 +02001395 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001396 error:
1397 free(rule);
1398 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001399}
1400
Willy Tarreau645513a2010-05-24 20:55:15 +02001401
1402/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001403/* All supported sample fetch functios must be declared here */
1404/************************************************************************/
1405
1406/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001407 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001408 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1409 * is a string (cookie name), other types will lead to undefined behaviour.
1410 */
1411int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001412smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001413 const struct arg *args, struct sample *smp)
1414{
1415 int bleft;
1416 const unsigned char *data;
1417
1418 if (!l4 || !l4->req)
1419 return 0;
1420
1421 smp->flags = 0;
1422 smp->type = SMP_T_CSTR;
1423
Willy Tarreau9b28e032012-10-12 23:49:43 +02001424 bleft = l4->req->buf->i;
Willy Tarreau32389b72012-04-23 23:13:20 +02001425 if (bleft <= 11)
1426 goto too_short;
1427
Willy Tarreau9b28e032012-10-12 23:49:43 +02001428 data = (const unsigned char *)l4->req->buf->p + 11;
Willy Tarreau32389b72012-04-23 23:13:20 +02001429 bleft -= 11;
1430
1431 if (bleft <= 7)
1432 goto too_short;
1433
1434 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1435 goto not_cookie;
1436
1437 data += 7;
1438 bleft -= 7;
1439
1440 while (bleft > 0 && *data == ' ') {
1441 data++;
1442 bleft--;
1443 }
1444
1445 if (args) {
1446
1447 if (bleft <= args->data.str.len)
1448 goto too_short;
1449
1450 if ((data[args->data.str.len] != '=') ||
1451 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1452 goto not_cookie;
1453
1454 data += args->data.str.len + 1;
1455 bleft -= args->data.str.len + 1;
1456 } else {
1457 while (bleft > 0 && *data != '=') {
1458 if (*data == '\r' || *data == '\n')
1459 goto not_cookie;
1460 data++;
1461 bleft--;
1462 }
1463
1464 if (bleft < 1)
1465 goto too_short;
1466
1467 if (*data != '=')
1468 goto not_cookie;
1469
1470 data++;
1471 bleft--;
1472 }
1473
1474 /* data points to cookie value */
1475 smp->data.str.str = (char *)data;
1476 smp->data.str.len = 0;
1477
1478 while (bleft > 0 && *data != '\r') {
1479 data++;
1480 bleft--;
1481 }
1482
1483 if (bleft < 2)
1484 goto too_short;
1485
1486 if (data[0] != '\r' || data[1] != '\n')
1487 goto not_cookie;
1488
1489 smp->data.str.len = (char *)data - smp->data.str.str;
1490 smp->flags = SMP_F_VOLATILE;
1491 return 1;
1492
1493 too_short:
1494 smp->flags = SMP_F_MAY_CHANGE;
1495 not_cookie:
1496 return 0;
1497}
1498
Willy Tarreau32389b72012-04-23 23:13:20 +02001499/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001500/* All supported ACL keywords must be declared here. */
1501/************************************************************************/
1502
Willy Tarreau32389b72012-04-23 23:13:20 +02001503/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1504static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001505acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001506 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001507{
1508 int ret;
1509
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001510 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001511
1512 if (smp->flags & SMP_F_MAY_CHANGE)
1513 return 0;
1514
1515 smp->flags = SMP_F_VOLATILE;
1516 smp->type = SMP_T_UINT;
1517 smp->data.uint = ret;
1518 return 1;
1519}
1520
1521
Willy Tarreau4a129812012-04-25 17:31:42 +02001522/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001523static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001524smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001525 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001526{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001527 switch (l4->si[0].conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001528 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001529 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001530 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001531 break;
1532 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001533 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001534 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001535 break;
1536 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001537 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001538 }
Emeric Brunf769f512010-10-22 17:14:01 +02001539
Willy Tarreau37406352012-04-23 16:16:37 +02001540 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001541 return 1;
1542}
1543
Willy Tarreaua5e37562011-12-16 17:06:15 +01001544/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001545static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001546smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001547 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001548{
Willy Tarreauf853c462012-04-23 18:53:56 +02001549 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001550 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001551 return 0;
1552
Willy Tarreau37406352012-04-23 16:16:37 +02001553 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001554 return 1;
1555}
1556
Willy Tarreau4a129812012-04-25 17:31:42 +02001557/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001558static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001559smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001560 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001561{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001562 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001563
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001564 switch (l4->si[0].conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001565 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001566 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001567 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001568 break;
1569 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001570 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001571 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001572 break;
1573 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001574 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001575 }
Emeric Brunf769f512010-10-22 17:14:01 +02001576
Willy Tarreau37406352012-04-23 16:16:37 +02001577 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001578 return 1;
1579}
1580
Willy Tarreaua5e37562011-12-16 17:06:15 +01001581/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001582static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001583smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001584 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001585{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001586 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001587
Willy Tarreauf853c462012-04-23 18:53:56 +02001588 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001589 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001590 return 0;
1591
Willy Tarreau37406352012-04-23 16:16:37 +02001592 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001593 return 1;
1594}
1595
1596static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001597smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1598 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001599{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001600 unsigned int len_offset = arg_p[0].data.uint;
1601 unsigned int len_size = arg_p[1].data.uint;
1602 unsigned int buf_offset;
1603 unsigned int buf_size = 0;
Willy Tarreau697d8502012-10-12 23:53:39 +02001604 struct channel *chn;
Emericf2d7cae2010-11-05 18:13:50 +01001605 int i;
1606
1607 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1608 /* by default buf offset == len offset + len size */
1609 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1610
1611 if (!l4)
1612 return 0;
1613
Willy Tarreau697d8502012-10-12 23:53:39 +02001614 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001615
Willy Tarreau697d8502012-10-12 23:53:39 +02001616 if (!chn)
Emericf2d7cae2010-11-05 18:13:50 +01001617 return 0;
1618
Willy Tarreau9b28e032012-10-12 23:49:43 +02001619 if (len_offset + len_size > chn->buf->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001620 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001621
1622 for (i = 0; i < len_size; i++) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02001623 buf_size = (buf_size << 8) + ((unsigned char *)chn->buf->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001624 }
1625
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001626 /* buf offset may be implicit, absolute or relative */
1627 buf_offset = len_offset + len_size;
1628 if (arg_p[2].type == ARGT_UINT)
1629 buf_offset = arg_p[2].data.uint;
1630 else if (arg_p[2].type == ARGT_SINT)
1631 buf_offset += arg_p[2].data.sint;
1632
Willy Tarreau9b28e032012-10-12 23:49:43 +02001633 if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001634 /* will never match */
1635 smp->flags = 0;
1636 return 0;
1637 }
1638
Willy Tarreau9b28e032012-10-12 23:49:43 +02001639 if (buf_offset + buf_size > chn->buf->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001640 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001641
1642 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001643 smp->type = SMP_T_CBIN;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001644 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001645 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001646 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001647
1648 too_short:
1649 smp->flags = SMP_F_MAY_CHANGE;
1650 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001651}
1652
1653static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001654smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1655 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001656{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001657 unsigned int buf_offset = arg_p[0].data.uint;
1658 unsigned int buf_size = arg_p[1].data.uint;
Willy Tarreau697d8502012-10-12 23:53:39 +02001659 struct channel *chn;
Emericf2d7cae2010-11-05 18:13:50 +01001660
1661 if (!l4)
1662 return 0;
1663
Willy Tarreau697d8502012-10-12 23:53:39 +02001664 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001665
Willy Tarreau697d8502012-10-12 23:53:39 +02001666 if (!chn)
Emericf2d7cae2010-11-05 18:13:50 +01001667 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001668
Willy Tarreau9b28e032012-10-12 23:49:43 +02001669 if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001670 /* will never match */
1671 smp->flags = 0;
1672 return 0;
1673 }
Emericf2d7cae2010-11-05 18:13:50 +01001674
Willy Tarreau9b28e032012-10-12 23:49:43 +02001675 if (buf_offset + buf_size > chn->buf->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001676 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001677
1678 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001679 smp->type = SMP_T_CBIN;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001680 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001681 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001682 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001683
1684 too_short:
1685 smp->flags = SMP_F_MAY_CHANGE;
1686 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001687}
1688
Willy Tarreau21d68a62012-04-20 15:52:36 +02001689/* This function is used to validate the arguments passed to a "payload" fetch
1690 * keyword. This keyword expects two positive integers, with the second one
1691 * being strictly positive. It is assumed that the types are already the correct
1692 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1693 * filled with a pointer to an error message in case of error, that the caller
1694 * is responsible for freeing. The initial location must either be freeable or
1695 * NULL.
1696 */
1697static int val_payload(struct arg *arg, char **err_msg)
1698{
1699 if (!arg[1].data.uint) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001700 memprintf(err_msg, "payload length must be > 0");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001701 return 0;
1702 }
1703 return 1;
1704}
1705
1706/* This function is used to validate the arguments passed to a "payload_lv" fetch
1707 * keyword. This keyword allows two positive integers and an optional signed one,
1708 * with the second one being strictly positive and the third one being greater than
1709 * the opposite of the two others if negative. It is assumed that the types are
1710 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1711 * not NULL, it will be filled with a pointer to an error message in case of
1712 * error, that the caller is responsible for freeing. The initial location must
1713 * either be freeable or NULL.
1714 */
1715static int val_payload_lv(struct arg *arg, char **err_msg)
1716{
1717 if (!arg[1].data.uint) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001718 memprintf(err_msg, "payload length must be > 0");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001719 return 0;
1720 }
1721
1722 if (arg[2].type == ARGT_SINT &&
1723 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001724 memprintf(err_msg, "payload offset too negative");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001725 return 0;
1726 }
1727 return 1;
1728}
1729
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001730#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001731/* parse the "v4v6" bind keyword */
1732static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1733{
1734 struct listener *l;
1735
1736 list_for_each_entry(l, &conf->listeners, by_bind) {
1737 if (l->addr.ss_family == AF_INET6)
1738 l->options |= LI_O_V4V6;
1739 }
1740
1741 return 0;
1742}
1743
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001744/* parse the "v6only" bind keyword */
1745static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1746{
1747 struct listener *l;
1748
1749 list_for_each_entry(l, &conf->listeners, by_bind) {
1750 if (l->addr.ss_family == AF_INET6)
1751 l->options |= LI_O_V6ONLY;
1752 }
1753
1754 return 0;
1755}
1756#endif
1757
Willy Tarreau44791242012-09-12 23:27:21 +02001758#ifdef CONFIG_HAP_LINUX_TPROXY
1759/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001760static 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 +02001761{
1762 struct listener *l;
1763
Willy Tarreau4348fad2012-09-20 16:48:07 +02001764 list_for_each_entry(l, &conf->listeners, by_bind) {
1765 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1766 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001767 }
1768
Willy Tarreau44791242012-09-12 23:27:21 +02001769 return 0;
1770}
1771#endif
1772
1773#ifdef TCP_DEFER_ACCEPT
1774/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001775static 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 +02001776{
1777 struct listener *l;
1778
Willy Tarreau4348fad2012-09-20 16:48:07 +02001779 list_for_each_entry(l, &conf->listeners, by_bind) {
1780 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1781 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001782 }
1783
Willy Tarreau44791242012-09-12 23:27:21 +02001784 return 0;
1785}
1786#endif
1787
Willy Tarreau1c862c52012-10-05 16:21:00 +02001788#ifdef TCP_FASTOPEN
1789/* parse the "defer-accept" bind keyword */
1790static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1791{
1792 struct listener *l;
1793
1794 list_for_each_entry(l, &conf->listeners, by_bind) {
1795 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1796 l->options |= LI_O_TCP_FO;
1797 }
1798
1799 return 0;
1800}
1801#endif
1802
Willy Tarreau44791242012-09-12 23:27:21 +02001803#ifdef TCP_MAXSEG
1804/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001805static 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 +02001806{
1807 struct listener *l;
1808 int mss;
1809
Willy Tarreau44791242012-09-12 23:27:21 +02001810 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001811 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001812 return ERR_ALERT | ERR_FATAL;
1813 }
1814
1815 mss = atoi(args[cur_arg + 1]);
1816 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001817 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001818 return ERR_ALERT | ERR_FATAL;
1819 }
1820
Willy Tarreau4348fad2012-09-20 16:48:07 +02001821 list_for_each_entry(l, &conf->listeners, by_bind) {
1822 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1823 l->maxseg = mss;
1824 }
Willy Tarreau44791242012-09-12 23:27:21 +02001825
1826 return 0;
1827}
1828#endif
1829
1830#ifdef SO_BINDTODEVICE
1831/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001832static 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 +02001833{
1834 struct listener *l;
1835
Willy Tarreau44791242012-09-12 23:27:21 +02001836 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001837 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001838 return ERR_ALERT | ERR_FATAL;
1839 }
1840
Willy Tarreau4348fad2012-09-20 16:48:07 +02001841 list_for_each_entry(l, &conf->listeners, by_bind) {
1842 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1843 l->interface = strdup(args[cur_arg + 1]);
1844 }
Willy Tarreau44791242012-09-12 23:27:21 +02001845
1846 global.last_checks |= LSTCHK_NETADM;
1847 return 0;
1848}
1849#endif
1850
Willy Tarreaub6866442008-07-14 23:54:42 +02001851static struct cfg_kw_list cfg_kws = {{ },{
1852 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001853 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001854 { 0, NULL, NULL },
1855}};
1856
Willy Tarreau61612d42012-04-19 18:42:05 +02001857/* Note: must not be declared <const> as its list will be overwritten.
1858 * Please take care of keeping this list alphabetically sorted.
1859 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001860static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001861 { "dst", acl_parse_ip, smp_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001862 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001863 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1864 { "payload_lv", acl_parse_str, smp_fetch_payload_lv, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG3(2,UINT,UINT,SINT), val_payload_lv },
Willy Tarreau9fb4bc72012-04-24 00:09:26 +02001865 { "req_rdp_cookie", acl_parse_str, smp_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
Willy Tarreau32389b72012-04-23 23:13:20 +02001866 { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau4a129812012-04-25 17:31:42 +02001867 { "src", acl_parse_ip, smp_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001868 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001869 { NULL, NULL, NULL, NULL },
1870}};
1871
Willy Tarreau4a129812012-04-25 17:31:42 +02001872/* Note: must not be declared <const> as its list will be overwritten.
1873 * Note: fetches that may return multiple types must be declared as the lowest
1874 * common denominator, the type that can be casted into all other ones. For
1875 * instance v4/v6 must be declared v4.
1876 */
Willy Tarreau12785782012-04-27 21:37:17 +02001877static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001878 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001879 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001880 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001881 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1882 { "payload_lv", smp_fetch_payload_lv, ARG3(2,UINT,UINT,SINT), val_payload_lv, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreaud6281ae2012-04-26 11:23:39 +02001883 { "rdp_cookie", smp_fetch_rdp_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001884 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001885 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001886}};
1887
Willy Tarreau44791242012-09-12 23:27:21 +02001888/************************************************************************/
1889/* All supported bind keywords must be declared here. */
1890/************************************************************************/
1891
1892/* Note: must not be declared <const> as its list will be overwritten.
1893 * Please take care of keeping this list alphabetically sorted, doing so helps
1894 * all code contributors.
1895 * Optional keywords are also declared with a NULL ->parse() function so that
1896 * the config parser can report an appropriate error when a known keyword was
1897 * not enabled.
1898 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001899static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001900#ifdef TCP_DEFER_ACCEPT
1901 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1902#endif
1903#ifdef SO_BINDTODEVICE
1904 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1905#endif
1906#ifdef TCP_MAXSEG
1907 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1908#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001909#ifdef TCP_FASTOPEN
1910 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1911#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001912#ifdef CONFIG_HAP_LINUX_TPROXY
1913 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1914#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001915#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001916 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001917 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1918#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001919 /* the versions with the NULL parse function*/
1920 { "defer-accept", NULL, 0 },
1921 { "interface", NULL, 1 },
1922 { "mss", NULL, 1 },
1923 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001924 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001925 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001926 { NULL, NULL, 0 },
1927}};
1928
Willy Tarreaue6b98942007-10-29 01:09:36 +01001929__attribute__((constructor))
1930static void __tcp_protocol_init(void)
1931{
1932 protocol_register(&proto_tcpv4);
1933 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001934 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001935 cfg_register_keywords(&cfg_kws);
1936 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001937 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001938}
1939
1940
1941/*
1942 * Local variables:
1943 * c-indent-level: 8
1944 * c-basic-offset: 8
1945 * End:
1946 */