blob: 60b111c71fd83cae2562547cde5fd2d0dc950947 [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;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100250 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100251
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100252 switch (obj_type(conn->target)) {
253 case OBJ_TYPE_PROXY:
254 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100255 srv = NULL;
256 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100257 case OBJ_TYPE_SERVER:
258 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100259 be = srv->proxy;
260 break;
261 default:
262 return SN_ERR_INTERNAL;
263 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200264
Willy Tarreau986a9d22012-08-30 21:11:38 +0200265 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200266 qfprintf(stderr, "Cannot get a server socket.\n");
267
268 if (errno == ENFILE)
269 send_log(be, LOG_EMERG,
270 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
271 be->id, maxfd);
272 else if (errno == EMFILE)
273 send_log(be, LOG_EMERG,
274 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
275 be->id, maxfd);
276 else if (errno == ENOBUFS || errno == ENOMEM)
277 send_log(be, LOG_EMERG,
278 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
279 be->id, maxfd);
280 /* this is a resource error */
281 return SN_ERR_RESOURCE;
282 }
283
284 if (fd >= global.maxsock) {
285 /* do not log anything there, it's a normal condition when this option
286 * is used to serialize connections to a server !
287 */
288 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
289 close(fd);
290 return SN_ERR_PRXCOND; /* it is a configuration limit */
291 }
292
293 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900294 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200295 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
296 close(fd);
297 return SN_ERR_INTERNAL;
298 }
299
300 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900301 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200302
Willy Tarreau9650f372009-08-16 14:02:45 +0200303 /* allow specific binding :
304 * - server-specific at first
305 * - proxy-specific next
306 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100307 if (srv && srv->conn_src.opts & CO_SRC_BIND)
308 src = &srv->conn_src;
309 else if (be->conn_src.opts & CO_SRC_BIND)
310 src = &be->conn_src;
311 else
312 src = NULL;
313
314 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200315 int ret, flags = 0;
316
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200317 if (is_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100318 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100319 case CO_SRC_TPROXY_ADDR:
320 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200321 flags = 3;
322 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100323 case CO_SRC_TPROXY_CIP:
324 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200325 flags = 1;
326 break;
327 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200328 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200329
Willy Tarreau9650f372009-08-16 14:02:45 +0200330#ifdef SO_BINDTODEVICE
331 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100332 if (src->iface_name)
333 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200334#endif
335
Willy Tarreaua4380b42012-12-08 22:49:11 +0100336 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200337 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100338 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200339
340 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100341 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200342
343 do {
344 /* note: in case of retry, we may have to release a previously
345 * allocated port, hence this loop's construct.
346 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200347 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
348 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200349
350 if (!attempts)
351 break;
352 attempts--;
353
Willy Tarreaua4380b42012-12-08 22:49:11 +0100354 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200355 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 break;
357
Willy Tarreaua4380b42012-12-08 22:49:11 +0100358 fdinfo[fd].port_range = src->sport_range;
359 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200360
Willy Tarreaua4380b42012-12-08 22:49:11 +0100361 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200362 } while (ret != 0); /* binding NOK */
363 }
364 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100365 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200366 }
367
Willy Tarreaua4380b42012-12-08 22:49:11 +0100368 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200369 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
370 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200371 close(fd);
372
Willy Tarreau9650f372009-08-16 14:02:45 +0200373 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100374 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 be->id);
376 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100377 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200378 be->id);
379 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100380 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200381 be->id);
382 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100383 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200384 be->id);
385 }
386 return SN_ERR_RESOURCE;
387 }
388 }
389
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400390#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200391 /* disabling tcp quick ack now allows the first request to leave the
392 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100393 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200394 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100395 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900396 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200397#endif
398
Willy Tarreaue803de22010-01-21 17:43:04 +0100399 if (global.tune.server_sndbuf)
400 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
401
402 if (global.tune.server_rcvbuf)
403 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
404
Willy Tarreau986a9d22012-08-30 21:11:38 +0200405 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200406 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
407
Willy Tarreaub1719512012-12-08 23:03:28 +0100408 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200409 char *msg;
Willy Tarreaub1719512012-12-08 23:03:28 +0100410 if (errno == EAGAIN || errno == EADDRNOTAVAIL)
Willy Tarreau9650f372009-08-16 14:02:45 +0200411 msg = "no free ports";
412 else
413 msg = "local address already in use";
414
Willy Tarreaub1719512012-12-08 23:03:28 +0100415 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200416 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
417 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200418 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100419 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9650f372009-08-16 14:02:45 +0200420 return SN_ERR_RESOURCE;
421 } else if (errno == ETIMEDOUT) {
422 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200423 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
424 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200425 close(fd);
426 return SN_ERR_SRVTO;
427 } else {
428 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
429 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200430 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
431 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200432 close(fd);
433 return SN_ERR_SRVCL;
434 }
435 }
436
Willy Tarreau986a9d22012-08-30 21:11:38 +0200437 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200438 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100439 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200440
Willy Tarreaud2274c62012-07-06 14:29:45 +0200441 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200443 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200444
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200445 if (conn_xprt_init(conn) < 0) {
Willy Tarreau184636e2012-09-06 14:04:41 +0200446 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200447 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200448 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200449
Willy Tarreau14f8e862012-08-30 22:23:13 +0200450 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200451 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200452
Willy Tarreau9650f372009-08-16 14:02:45 +0200453 return SN_ERR_NONE; /* connection is OK */
454}
455
456
Willy Tarreau59b94792012-05-11 16:16:40 +0200457/*
458 * Retrieves the source address for the socket <fd>, with <dir> indicating
459 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
460 * success, -1 in case of error. The socket's source address is stored in
461 * <sa> for <salen> bytes.
462 */
463int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
464{
465 if (dir)
466 return getsockname(fd, sa, &salen);
467 else
468 return getpeername(fd, sa, &salen);
469}
470
471
472/*
473 * Retrieves the original destination address for the socket <fd>, with <dir>
474 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
475 * listener, if the original destination address was translated, the original
476 * address is retrieved. It returns 0 in case of success, -1 in case of error.
477 * The socket's source address is stored in <sa> for <salen> bytes.
478 */
479int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
480{
481 if (dir)
482 return getpeername(fd, sa, &salen);
483#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
484 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
485 return 0;
486#endif
487 else
488 return getsockname(fd, sa, &salen);
489}
490
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200491/* This is the callback which is set when a connection establishment is pending
492 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200493 * once the connection is established. It updates the FD polling status. It
494 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
495 * it returns non-zero and removes itself from the connection's flags (the bit is
496 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200497 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200498int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200499{
Willy Tarreau239d7182012-07-23 18:53:03 +0200500 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200501
Willy Tarreau80184712012-07-06 14:54:49 +0200502 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200503 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200504
Willy Tarreau80184712012-07-06 14:54:49 +0200505 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200506 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200507
Willy Tarreau2da156f2012-07-23 15:07:23 +0200508 /* stop here if we reached the end of data */
509 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
510 goto out_error;
511
Willy Tarreau2c6be842012-07-06 17:12:34 +0200512 /* We have no data to send to check the connection, and
513 * getsockopt() will not inform us whether the connection
514 * is still pending. So we'll reuse connect() to check the
515 * state of the socket. This has the advantage of giving us
516 * the following info :
517 * - error
518 * - connecting (EALREADY, EINPROGRESS)
519 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200520 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200521 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200522 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100523 __conn_sock_stop_recv(conn);
524 __conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200525 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200526 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200527
Willy Tarreau2c6be842012-07-06 17:12:34 +0200528 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200529 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200530
Willy Tarreau2c6be842012-07-06 17:12:34 +0200531 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200532 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200533
Willy Tarreau076be252012-07-06 16:02:29 +0200534 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200535 * forward the event to the transport layer which will notify the
536 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200537 */
Willy Tarreau80184712012-07-06 14:54:49 +0200538 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200539 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200540
541 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200542 /* Write error on the file descriptor. Report it to the connection
543 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200544 */
545
Willy Tarreau80184712012-07-06 14:54:49 +0200546 conn->flags |= CO_FL_ERROR;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100547 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200548 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200549}
550
Willy Tarreau59b94792012-05-11 16:16:40 +0200551
Willy Tarreaue6b98942007-10-29 01:09:36 +0100552/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100553 * an error message in <errmsg> if the message is at most <errlen> bytes long
554 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
555 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100556 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
557 * was alright and that no message was returned. ERR_RETRYABLE means that an
558 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700559 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100560 * the meaning of the error, but just indicate that a message is present which
561 * should be displayed with the respective level. Last, ERR_ABORT indicates
562 * that it's pointless to try to start other listeners. No error message is
563 * returned if errlen is NULL.
564 */
565int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
566{
567 __label__ tcp_return, tcp_close_return;
568 int fd, err;
569 const char *msg = NULL;
570
571 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100572 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100573 *errmsg = 0;
574
575 if (listener->state != LI_ASSIGNED)
576 return ERR_NONE; /* already bound */
577
578 err = ERR_NONE;
579
580 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
581 err |= ERR_RETRYABLE | ERR_ALERT;
582 msg = "cannot create listening socket";
583 goto tcp_return;
584 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100585
Willy Tarreaue6b98942007-10-29 01:09:36 +0100586 if (fd >= global.maxsock) {
587 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
588 msg = "not enough free sockets (raise '-n' parameter)";
589 goto tcp_close_return;
590 }
591
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200592 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100593 err |= ERR_FATAL | ERR_ALERT;
594 msg = "cannot make socket non-blocking";
595 goto tcp_close_return;
596 }
597
Simon Hormande072bd2011-06-24 15:11:37 +0900598 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100599 /* not fatal but should be reported */
600 msg = "cannot do so_reuseaddr";
601 err |= ERR_ALERT;
602 }
603
604 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900605 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100606
Willy Tarreaue6b98942007-10-29 01:09:36 +0100607#ifdef SO_REUSEPORT
608 /* OpenBSD supports this. As it's present in old libc versions of Linux,
609 * it might return an error that we will silently ignore.
610 */
Simon Hormande072bd2011-06-24 15:11:37 +0900611 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100612#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100613#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200614 if (listener->options & LI_O_FOREIGN) {
615 switch (listener->addr.ss_family) {
616 case AF_INET:
617 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
618 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
619 msg = "cannot make listening socket transparent";
620 err |= ERR_ALERT;
621 }
622 break;
623 case AF_INET6:
624 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
625 msg = "cannot make listening socket transparent";
626 err |= ERR_ALERT;
627 }
628 break;
629 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100630 }
631#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100632#ifdef SO_BINDTODEVICE
633 /* Note: this might fail if not CAP_NET_RAW */
634 if (listener->interface) {
635 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100636 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100637 msg = "cannot bind listener to device";
638 err |= ERR_WARN;
639 }
640 }
641#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400642#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100643 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400644 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200645 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
646 msg = "cannot set MSS";
647 err |= ERR_WARN;
648 }
649 }
650#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200651#if defined(TCP_DEFER_ACCEPT)
652 if (listener->options & LI_O_DEF_ACCEPT) {
653 /* defer accept by up to one second */
654 int accept_delay = 1;
655 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
656 msg = "cannot enable DEFER_ACCEPT";
657 err |= ERR_WARN;
658 }
659 }
660#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200661#if defined(TCP_FASTOPEN)
662 if (listener->options & LI_O_TCP_FO) {
663 /* TFO needs a queue length, let's use the configured backlog */
664 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
665 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
666 msg = "cannot enable TCP_FASTOPEN";
667 err |= ERR_WARN;
668 }
669 }
670#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100671#if defined(IPV6_V6ONLY)
672 if (listener->options & LI_O_V6ONLY)
673 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100674 else if (listener->options & LI_O_V4V6)
675 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100676#endif
677
Willy Tarreaue6b98942007-10-29 01:09:36 +0100678 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
679 err |= ERR_RETRYABLE | ERR_ALERT;
680 msg = "cannot bind socket";
681 goto tcp_close_return;
682 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100683
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100684 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100685 err |= ERR_RETRYABLE | ERR_ALERT;
686 msg = "cannot listen to socket";
687 goto tcp_close_return;
688 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100689
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400690#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200691 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900692 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200693#endif
694
Willy Tarreaue6b98942007-10-29 01:09:36 +0100695 /* the socket is ready */
696 listener->fd = fd;
697 listener->state = LI_LISTEN;
698
Willy Tarreaueabf3132008-08-29 23:36:51 +0200699 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200700 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200701 fd_insert(fd);
702
Willy Tarreaue6b98942007-10-29 01:09:36 +0100703 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100704 if (msg && errlen) {
705 char pn[INET6_ADDRSTRLEN];
706
Willy Tarreau631f01c2011-09-05 00:36:48 +0200707 addr_to_str(&listener->addr, pn, sizeof(pn));
708 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100709 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100710 return err;
711
712 tcp_close_return:
713 close(fd);
714 goto tcp_return;
715}
716
717/* This function creates all TCP sockets bound to the protocol entry <proto>.
718 * It is intended to be used as the protocol's bind_all() function.
719 * The sockets will be registered but not added to any fd_set, in order not to
720 * loose them across the fork(). A call to enable_all_listeners() is needed
721 * to complete initialization. The return value is composed from ERR_*.
722 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200723static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100724{
725 struct listener *listener;
726 int err = ERR_NONE;
727
728 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200729 err |= tcp_bind_listener(listener, errmsg, errlen);
730 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100731 break;
732 }
733
734 return err;
735}
736
737/* Add listener to the list of tcpv4 listeners. The listener's state
738 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
739 * listeners is updated. This is the function to use to add a new listener.
740 */
741void tcpv4_add_listener(struct listener *listener)
742{
743 if (listener->state != LI_INIT)
744 return;
745 listener->state = LI_ASSIGNED;
746 listener->proto = &proto_tcpv4;
747 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
748 proto_tcpv4.nb_listeners++;
749}
750
751/* Add listener to the list of tcpv4 listeners. The listener's state
752 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
753 * listeners is updated. This is the function to use to add a new listener.
754 */
755void tcpv6_add_listener(struct listener *listener)
756{
757 if (listener->state != LI_INIT)
758 return;
759 listener->state = LI_ASSIGNED;
760 listener->proto = &proto_tcpv6;
761 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
762 proto_tcpv6.nb_listeners++;
763}
764
Willy Tarreauedcf6682008-11-30 23:15:34 +0100765/* This function performs the TCP request analysis on the current request. It
766 * returns 1 if the processing can continue on next analysers, or zero if it
767 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200768 * request. It relies on buffers flags, and updates s->req->analysers. The
769 * function may be called for frontend rules and backend rules. It only relies
770 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100771 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200772int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100773{
774 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200775 struct stksess *ts;
776 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100777 int partial;
778
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100779 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 +0100780 now_ms, __FUNCTION__,
781 s,
782 req,
783 req->rex, req->wex,
784 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200785 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100786 req->analysers);
787
Willy Tarreauedcf6682008-11-30 23:15:34 +0100788 /* We don't know whether we have enough data, so must proceed
789 * this way :
790 * - iterate through all rules in their declaration order
791 * - if one rule returns MISS, it means the inspect delay is
792 * not over yet, then return immediately, otherwise consider
793 * it as a non-match.
794 * - if one rule returns OK, then return OK
795 * - if one rule returns KO, then return KO
796 */
797
Willy Tarreau9b28e032012-10-12 23:49:43 +0200798 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200799 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200800 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100801 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200802 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100803
Willy Tarreaufb356202010-08-03 14:02:05 +0200804 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100805 int ret = ACL_PAT_PASS;
806
807 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200808 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100809 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200810 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100811 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200812 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
813 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100814 return 0;
815 }
816
817 ret = acl_pass(ret);
818 if (rule->cond->pol == ACL_COND_UNLESS)
819 ret = !ret;
820 }
821
822 if (ret) {
823 /* we have a matching rule. */
824 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200825 channel_abort(req);
826 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100827 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200828
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100829 s->be->be_counters.denied_req++;
830 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200831 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200832 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200833
Willy Tarreauedcf6682008-11-30 23:15:34 +0100834 if (!(s->flags & SN_ERR_MASK))
835 s->flags |= SN_ERR_PRXCOND;
836 if (!(s->flags & SN_FINST_MASK))
837 s->flags |= SN_FINST_R;
838 return 0;
839 }
Willy Tarreau20d46a52012-12-09 15:55:40 +0100840 else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) ||
841 (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100842 /* Note: only the first valid tracking parameter of each
843 * applies.
844 */
845 struct stktable_key *key;
846
847 t = rule->act_prm.trk_ctr.table.t;
848 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
849
850 if (key && (ts = stktable_get_entry(t, key))) {
851 if (rule->action == TCP_ACT_TRK_SC1) {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100852 session_track_stkctr(&s->stkctr[0], t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200853 if (s->fe != s->be)
854 s->flags |= SN_BE_TRACK_SC1;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100855 } else {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100856 session_track_stkctr(&s->stkctr[1], t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200857 if (s->fe != s->be)
858 s->flags |= SN_BE_TRACK_SC2;
859 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200860 }
861 }
862 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100863 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200864 break;
865 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100866 }
867 }
868
869 /* if we get there, it means we have no rule which matches, or
870 * we have an explicit accept, so we apply the default accept.
871 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200872 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100873 req->analyse_exp = TICK_ETERNITY;
874 return 1;
875}
876
Emeric Brun97679e72010-09-23 17:56:44 +0200877/* This function performs the TCP response analysis on the current response. It
878 * returns 1 if the processing can continue on next analysers, or zero if it
879 * needs more data, encounters an error, or wants to immediately abort the
880 * response. It relies on buffers flags, and updates s->rep->analysers. The
881 * function may be called for backend rules.
882 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200883int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200884{
885 struct tcp_rule *rule;
886 int partial;
887
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100888 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 +0200889 now_ms, __FUNCTION__,
890 s,
891 rep,
892 rep->rex, rep->wex,
893 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200894 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200895 rep->analysers);
896
897 /* We don't know whether we have enough data, so must proceed
898 * this way :
899 * - iterate through all rules in their declaration order
900 * - if one rule returns MISS, it means the inspect delay is
901 * not over yet, then return immediately, otherwise consider
902 * it as a non-match.
903 * - if one rule returns OK, then return OK
904 * - if one rule returns KO, then return KO
905 */
906
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200907 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200908 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200909 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200910 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200911
912 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
913 int ret = ACL_PAT_PASS;
914
915 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200916 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200917 if (ret == ACL_PAT_MISS) {
918 /* just set the analyser timeout once at the beginning of the response */
919 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
920 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
921 return 0;
922 }
923
924 ret = acl_pass(ret);
925 if (rule->cond->pol == ACL_COND_UNLESS)
926 ret = !ret;
927 }
928
929 if (ret) {
930 /* we have a matching rule. */
931 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200932 channel_abort(rep);
933 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200934 rep->analysers = 0;
935
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100936 s->be->be_counters.denied_resp++;
937 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200938 if (s->listener->counters)
939 s->listener->counters->denied_resp++;
940
941 if (!(s->flags & SN_ERR_MASK))
942 s->flags |= SN_ERR_PRXCOND;
943 if (!(s->flags & SN_FINST_MASK))
944 s->flags |= SN_FINST_D;
945 return 0;
946 }
947 else {
948 /* otherwise accept */
949 break;
950 }
951 }
952 }
953
954 /* if we get there, it means we have no rule which matches, or
955 * we have an explicit accept, so we apply the default accept.
956 */
957 rep->analysers &= ~an_bit;
958 rep->analyse_exp = TICK_ETERNITY;
959 return 1;
960}
961
962
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200963/* This function performs the TCP layer4 analysis on the current request. It
964 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
965 * matches or if no more rule matches. It can only use rules which don't need
966 * any data.
967 */
968int tcp_exec_req_rules(struct session *s)
969{
970 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200971 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200972 struct stktable *t = NULL;
973 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200974 int ret;
975
976 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
977 ret = ACL_PAT_PASS;
978
979 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200980 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200981 ret = acl_pass(ret);
982 if (rule->cond->pol == ACL_COND_UNLESS)
983 ret = !ret;
984 }
985
986 if (ret) {
987 /* we have a matching rule. */
988 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100989 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200990 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +0200991 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200992
993 if (!(s->flags & SN_ERR_MASK))
994 s->flags |= SN_ERR_PRXCOND;
995 if (!(s->flags & SN_FINST_MASK))
996 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200997 result = 0;
998 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200999 }
Willy Tarreau20d46a52012-12-09 15:55:40 +01001000 else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) ||
1001 (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001002 /* Note: only the first valid tracking parameter of each
1003 * applies.
1004 */
1005 struct stktable_key *key;
1006
1007 t = rule->act_prm.trk_ctr.table.t;
1008 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
1009
1010 if (key && (ts = stktable_get_entry(t, key))) {
1011 if (rule->action == TCP_ACT_TRK_SC1)
Willy Tarreau20d46a52012-12-09 15:55:40 +01001012 session_track_stkctr(&s->stkctr[0], t, ts);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001013 else
Willy Tarreau20d46a52012-12-09 15:55:40 +01001014 session_track_stkctr(&s->stkctr[1], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001015 }
1016 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001017 else {
1018 /* otherwise it's an accept */
1019 break;
1020 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001021 }
1022 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001023 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001024}
1025
Emeric Brun97679e72010-09-23 17:56:44 +02001026/* Parse a tcp-response rule. Return a negative value in case of failure */
1027static int tcp_parse_response_rule(char **args, int arg, int section_type,
1028 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001029 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001030{
1031 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001032 memprintf(err, "%s %s is only allowed in 'backend' sections",
1033 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001034 return -1;
1035 }
1036
1037 if (strcmp(args[arg], "accept") == 0) {
1038 arg++;
1039 rule->action = TCP_ACT_ACCEPT;
1040 }
1041 else if (strcmp(args[arg], "reject") == 0) {
1042 arg++;
1043 rule->action = TCP_ACT_REJECT;
1044 }
1045 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001046 memprintf(err,
1047 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1048 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001049 return -1;
1050 }
1051
1052 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001053 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1054 memprintf(err,
1055 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1056 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001057 return -1;
1058 }
1059 }
1060 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001061 memprintf(err,
1062 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001063 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1064 return -1;
1065 }
1066 return 0;
1067}
1068
1069
1070
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001071/* Parse a tcp-request rule. Return a negative value in case of failure */
1072static int tcp_parse_request_rule(char **args, int arg, int section_type,
1073 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001074 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001075{
1076 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001077 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1078 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001079 return -1;
1080 }
1081
1082 if (!strcmp(args[arg], "accept")) {
1083 arg++;
1084 rule->action = TCP_ACT_ACCEPT;
1085 }
1086 else if (!strcmp(args[arg], "reject")) {
1087 arg++;
1088 rule->action = TCP_ACT_REJECT;
1089 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001090 else if (strcmp(args[arg], "track-sc1") == 0 || strcmp(args[arg], "track-sc2") == 0) {
1091 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001092 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001093
1094 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001095
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001096 expr = sample_parse_expr(args, &arg, trash.str, trash.size);
1097 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001098 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001099 "'%s %s %s' : %s",
1100 args[0], args[1], args[kw], trash.str);
1101 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001102 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001103
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001104 if (!(expr->fetch->cap & SMP_CAP_REQ)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001105 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001106 "'%s %s %s' : fetch method '%s' cannot be used on request",
1107 args[0], args[1], args[kw], trash.str);
1108 free(expr);
1109 return -1;
1110 }
1111
1112 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
1113 if (expr->fetch->cap & SMP_CAP_L7)
1114 curpx->acl_requires |= ACL_USE_L7_ANY;
1115
1116 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001117 arg++;
1118 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001119 memprintf(err,
1120 "'%s %s %s' : missing table name",
1121 args[0], args[1], args[kw]);
1122 free(expr);
1123 return -1;
1124 }
1125 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001126 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001127 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001128 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001129 rule->act_prm.trk_ctr.expr = expr;
1130
1131 if (args[kw][8] == '1')
1132 rule->action = TCP_ACT_TRK_SC1;
1133 else
1134 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001135 }
1136 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001137 memprintf(err,
1138 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1139 "or 'track-sc2' in %s '%s' (got '%s')",
1140 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001141 return -1;
1142 }
1143
1144 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001145 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1146 memprintf(err,
1147 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1148 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001149 return -1;
1150 }
1151 }
1152 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001153 memprintf(err,
1154 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001155 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1156 return -1;
1157 }
1158 return 0;
1159}
1160
Emeric Brun97679e72010-09-23 17:56:44 +02001161/* This function should be called to parse a line starting with the "tcp-response"
1162 * keyword.
1163 */
1164static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001165 struct proxy *defpx, const char *file, int line,
1166 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001167{
1168 const char *ptr = NULL;
1169 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001170 int warn = 0;
1171 int arg;
1172 struct tcp_rule *rule;
1173
1174 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001175 memprintf(err, "missing argument for '%s' in %s '%s'",
1176 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001177 return -1;
1178 }
1179
1180 if (strcmp(args[1], "inspect-delay") == 0) {
1181 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001182 memprintf(err, "%s %s is only allowed in 'backend' sections",
1183 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001184 return -1;
1185 }
1186
1187 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001188 memprintf(err,
1189 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1190 args[0], args[1], proxy_type_str(curpx), curpx->id);
1191 if (ptr)
1192 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001193 return -1;
1194 }
1195
1196 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001197 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1198 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001199 return 1;
1200 }
1201 curpx->tcp_rep.inspect_delay = val;
1202 return 0;
1203 }
1204
Simon Hormande072bd2011-06-24 15:11:37 +09001205 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001206 LIST_INIT(&rule->list);
1207 arg = 1;
1208
1209 if (strcmp(args[1], "content") == 0) {
1210 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001211 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001212 goto error;
1213
1214 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1215 struct acl *acl;
1216 const char *name;
1217
1218 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1219 name = acl ? acl->name : "(unknown)";
1220
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001221 memprintf(err,
1222 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1223 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001224 warn++;
1225 }
1226
1227 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1228 }
1229 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001230 memprintf(err,
1231 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1232 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001233 goto error;
1234 }
1235
1236 return warn;
1237 error:
1238 free(rule);
1239 return -1;
1240}
1241
1242
Willy Tarreaub6866442008-07-14 23:54:42 +02001243/* This function should be called to parse a line starting with the "tcp-request"
1244 * keyword.
1245 */
1246static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001247 struct proxy *defpx, const char *file, int line,
1248 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001249{
1250 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001251 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001252 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001253 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001254 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001255
1256 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001257 if (curpx == defpx)
1258 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1259 else
1260 memprintf(err, "missing argument for '%s' in %s '%s'",
1261 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001262 return -1;
1263 }
1264
1265 if (!strcmp(args[1], "inspect-delay")) {
1266 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001267 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1268 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001269 return -1;
1270 }
1271
Willy Tarreaub6866442008-07-14 23:54:42 +02001272 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001273 memprintf(err,
1274 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1275 args[0], args[1], proxy_type_str(curpx), curpx->id);
1276 if (ptr)
1277 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001278 return -1;
1279 }
1280
1281 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001282 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1283 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001284 return 1;
1285 }
1286 curpx->tcp_req.inspect_delay = val;
1287 return 0;
1288 }
1289
Simon Hormande072bd2011-06-24 15:11:37 +09001290 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001291 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001292 arg = 1;
1293
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001294 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001295 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001296 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001297 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001298
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001299 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001300 struct acl *acl;
1301 const char *name;
1302
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001303 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001304 name = acl ? acl->name : "(unknown)";
1305
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001306 memprintf(err,
1307 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1308 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001309 warn++;
1310 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001311
1312 if ((rule->action == TCP_ACT_TRK_SC1 || rule->action == TCP_ACT_TRK_SC2) &&
Willy Tarreaub54b6ca2012-12-09 17:04:41 +01001313 !(rule->act_prm.trk_ctr.expr->fetch->cap & SMP_CAP_REQ)) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001314 memprintf(err,
Willy Tarreaub54b6ca2012-12-09 17:04:41 +01001315 "fetch '%s' cannot be used on requests and will be ignored in '%s %s'",
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001316 rule->act_prm.trk_ctr.expr->fetch->kw, args[0], args[1]);
1317 warn++;
1318 }
1319
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001320 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001321 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001322 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001323 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001324
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001325 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001326 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1327 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001328 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001329 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001330
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001331 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001332 goto error;
1333
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001334 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1335 struct acl *acl;
1336 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001337
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001338 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1339 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001340
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001341 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001342 memprintf(err,
1343 "'%s %s' may not reference acl '%s' which makes use of "
1344 "payload in %s '%s'. Please use '%s content' for this.",
1345 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001346 goto error;
1347 }
1348 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001349 memprintf(err,
1350 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1351 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001352 warn++;
1353 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001354
1355 if ((rule->action == TCP_ACT_TRK_SC1 || rule->action == TCP_ACT_TRK_SC2) &&
Willy Tarreaub54b6ca2012-12-09 17:04:41 +01001356 !(rule->act_prm.trk_ctr.expr->fetch->cap & SMP_CAP_REQ)) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001357 memprintf(err,
Willy Tarreaub54b6ca2012-12-09 17:04:41 +01001358 "fetch '%s' cannot be used on requests and will be ignored in '%s %s'",
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001359 rule->act_prm.trk_ctr.expr->fetch->kw, args[0], args[1]);
1360 warn++;
1361 }
1362
1363 if ((rule->action == TCP_ACT_TRK_SC1 || rule->action == TCP_ACT_TRK_SC2) &&
1364 (rule->act_prm.trk_ctr.expr->fetch->cap & SMP_CAP_L7)) {
1365 memprintf(err,
1366 "fetch '%s' involves some layer7-only criteria which will be ignored in '%s %s'",
1367 rule->act_prm.trk_ctr.expr->fetch->kw, args[0], args[1]);
1368 warn++;
1369 }
1370
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001371 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001372 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001373 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001374 if (curpx == defpx)
1375 memprintf(err,
1376 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1377 args[0], args[1]);
1378 else
1379 memprintf(err,
1380 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1381 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001382 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001383 }
1384
Willy Tarreau1a687942010-05-23 22:40:30 +02001385 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001386 error:
1387 free(rule);
1388 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001389}
1390
Willy Tarreau645513a2010-05-24 20:55:15 +02001391
1392/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001393/* All supported sample fetch functios must be declared here */
1394/************************************************************************/
1395
1396/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001397 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001398 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1399 * is a string (cookie name), other types will lead to undefined behaviour.
1400 */
1401int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001402smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001403 const struct arg *args, struct sample *smp)
1404{
1405 int bleft;
1406 const unsigned char *data;
1407
1408 if (!l4 || !l4->req)
1409 return 0;
1410
1411 smp->flags = 0;
1412 smp->type = SMP_T_CSTR;
1413
Willy Tarreau9b28e032012-10-12 23:49:43 +02001414 bleft = l4->req->buf->i;
Willy Tarreau32389b72012-04-23 23:13:20 +02001415 if (bleft <= 11)
1416 goto too_short;
1417
Willy Tarreau9b28e032012-10-12 23:49:43 +02001418 data = (const unsigned char *)l4->req->buf->p + 11;
Willy Tarreau32389b72012-04-23 23:13:20 +02001419 bleft -= 11;
1420
1421 if (bleft <= 7)
1422 goto too_short;
1423
1424 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1425 goto not_cookie;
1426
1427 data += 7;
1428 bleft -= 7;
1429
1430 while (bleft > 0 && *data == ' ') {
1431 data++;
1432 bleft--;
1433 }
1434
1435 if (args) {
1436
1437 if (bleft <= args->data.str.len)
1438 goto too_short;
1439
1440 if ((data[args->data.str.len] != '=') ||
1441 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1442 goto not_cookie;
1443
1444 data += args->data.str.len + 1;
1445 bleft -= args->data.str.len + 1;
1446 } else {
1447 while (bleft > 0 && *data != '=') {
1448 if (*data == '\r' || *data == '\n')
1449 goto not_cookie;
1450 data++;
1451 bleft--;
1452 }
1453
1454 if (bleft < 1)
1455 goto too_short;
1456
1457 if (*data != '=')
1458 goto not_cookie;
1459
1460 data++;
1461 bleft--;
1462 }
1463
1464 /* data points to cookie value */
1465 smp->data.str.str = (char *)data;
1466 smp->data.str.len = 0;
1467
1468 while (bleft > 0 && *data != '\r') {
1469 data++;
1470 bleft--;
1471 }
1472
1473 if (bleft < 2)
1474 goto too_short;
1475
1476 if (data[0] != '\r' || data[1] != '\n')
1477 goto not_cookie;
1478
1479 smp->data.str.len = (char *)data - smp->data.str.str;
1480 smp->flags = SMP_F_VOLATILE;
1481 return 1;
1482
1483 too_short:
1484 smp->flags = SMP_F_MAY_CHANGE;
1485 not_cookie:
1486 return 0;
1487}
1488
Willy Tarreau32389b72012-04-23 23:13:20 +02001489/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001490/* All supported ACL keywords must be declared here. */
1491/************************************************************************/
1492
Willy Tarreau32389b72012-04-23 23:13:20 +02001493/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1494static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001495acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001496 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001497{
1498 int ret;
1499
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001500 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001501
1502 if (smp->flags & SMP_F_MAY_CHANGE)
1503 return 0;
1504
1505 smp->flags = SMP_F_VOLATILE;
1506 smp->type = SMP_T_UINT;
1507 smp->data.uint = ret;
1508 return 1;
1509}
1510
1511
Willy Tarreau4a129812012-04-25 17:31:42 +02001512/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001513static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001514smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001515 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001516{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001517 switch (l4->si[0].conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001518 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001519 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001520 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001521 break;
1522 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001523 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001524 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001525 break;
1526 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001527 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001528 }
Emeric Brunf769f512010-10-22 17:14:01 +02001529
Willy Tarreau37406352012-04-23 16:16:37 +02001530 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001531 return 1;
1532}
1533
Willy Tarreaua5e37562011-12-16 17:06:15 +01001534/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001535static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001536smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001537 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001538{
Willy Tarreauf853c462012-04-23 18:53:56 +02001539 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001540 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001541 return 0;
1542
Willy Tarreau37406352012-04-23 16:16:37 +02001543 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001544 return 1;
1545}
1546
Willy Tarreau4a129812012-04-25 17:31:42 +02001547/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001548static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001549smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001550 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001551{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001552 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001553
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001554 switch (l4->si[0].conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001555 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001556 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001557 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001558 break;
1559 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001560 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001561 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001562 break;
1563 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001564 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001565 }
Emeric Brunf769f512010-10-22 17:14:01 +02001566
Willy Tarreau37406352012-04-23 16:16:37 +02001567 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001568 return 1;
1569}
1570
Willy Tarreaua5e37562011-12-16 17:06:15 +01001571/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001572static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001573smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001574 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001575{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001576 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001577
Willy Tarreauf853c462012-04-23 18:53:56 +02001578 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001579 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001580 return 0;
1581
Willy Tarreau37406352012-04-23 16:16:37 +02001582 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001583 return 1;
1584}
1585
1586static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001587smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1588 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001589{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001590 unsigned int len_offset = arg_p[0].data.uint;
1591 unsigned int len_size = arg_p[1].data.uint;
1592 unsigned int buf_offset;
1593 unsigned int buf_size = 0;
Willy Tarreau697d8502012-10-12 23:53:39 +02001594 struct channel *chn;
Emericf2d7cae2010-11-05 18:13:50 +01001595 int i;
1596
1597 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1598 /* by default buf offset == len offset + len size */
1599 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1600
1601 if (!l4)
1602 return 0;
1603
Willy Tarreau697d8502012-10-12 23:53:39 +02001604 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001605
Willy Tarreau697d8502012-10-12 23:53:39 +02001606 if (!chn)
Emericf2d7cae2010-11-05 18:13:50 +01001607 return 0;
1608
Willy Tarreau9b28e032012-10-12 23:49:43 +02001609 if (len_offset + len_size > chn->buf->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001610 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001611
1612 for (i = 0; i < len_size; i++) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02001613 buf_size = (buf_size << 8) + ((unsigned char *)chn->buf->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001614 }
1615
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001616 /* buf offset may be implicit, absolute or relative */
1617 buf_offset = len_offset + len_size;
1618 if (arg_p[2].type == ARGT_UINT)
1619 buf_offset = arg_p[2].data.uint;
1620 else if (arg_p[2].type == ARGT_SINT)
1621 buf_offset += arg_p[2].data.sint;
1622
Willy Tarreau9b28e032012-10-12 23:49:43 +02001623 if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001624 /* will never match */
1625 smp->flags = 0;
1626 return 0;
1627 }
1628
Willy Tarreau9b28e032012-10-12 23:49:43 +02001629 if (buf_offset + buf_size > chn->buf->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001630 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001631
1632 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001633 smp->type = SMP_T_CBIN;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001634 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001635 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001636 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001637
1638 too_short:
1639 smp->flags = SMP_F_MAY_CHANGE;
1640 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001641}
1642
1643static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001644smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1645 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001646{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001647 unsigned int buf_offset = arg_p[0].data.uint;
1648 unsigned int buf_size = arg_p[1].data.uint;
Willy Tarreau697d8502012-10-12 23:53:39 +02001649 struct channel *chn;
Emericf2d7cae2010-11-05 18:13:50 +01001650
1651 if (!l4)
1652 return 0;
1653
Willy Tarreau697d8502012-10-12 23:53:39 +02001654 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001655
Willy Tarreau697d8502012-10-12 23:53:39 +02001656 if (!chn)
Emericf2d7cae2010-11-05 18:13:50 +01001657 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001658
Willy Tarreau9b28e032012-10-12 23:49:43 +02001659 if (!buf_size || buf_size > chn->buf->size || buf_offset + buf_size > chn->buf->size) {
Willy Tarreau82ea8002012-04-25 19:19:43 +02001660 /* will never match */
1661 smp->flags = 0;
1662 return 0;
1663 }
Emericf2d7cae2010-11-05 18:13:50 +01001664
Willy Tarreau9b28e032012-10-12 23:49:43 +02001665 if (buf_offset + buf_size > chn->buf->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001666 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001667
1668 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001669 smp->type = SMP_T_CBIN;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001670 chunk_initlen(&smp->data.str, chn->buf->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001671 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001672 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001673
1674 too_short:
1675 smp->flags = SMP_F_MAY_CHANGE;
1676 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001677}
1678
Willy Tarreau21d68a62012-04-20 15:52:36 +02001679/* This function is used to validate the arguments passed to a "payload" fetch
1680 * keyword. This keyword expects two positive integers, with the second one
1681 * being strictly positive. It is assumed that the types are already the correct
1682 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1683 * filled with a pointer to an error message in case of error, that the caller
1684 * is responsible for freeing. The initial location must either be freeable or
1685 * NULL.
1686 */
1687static int val_payload(struct arg *arg, char **err_msg)
1688{
1689 if (!arg[1].data.uint) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001690 memprintf(err_msg, "payload length must be > 0");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001691 return 0;
1692 }
1693 return 1;
1694}
1695
1696/* This function is used to validate the arguments passed to a "payload_lv" fetch
1697 * keyword. This keyword allows two positive integers and an optional signed one,
1698 * with the second one being strictly positive and the third one being greater than
1699 * the opposite of the two others if negative. It is assumed that the types are
1700 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1701 * not NULL, it will be filled with a pointer to an error message in case of
1702 * error, that the caller is responsible for freeing. The initial location must
1703 * either be freeable or NULL.
1704 */
1705static int val_payload_lv(struct arg *arg, char **err_msg)
1706{
1707 if (!arg[1].data.uint) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001708 memprintf(err_msg, "payload length must be > 0");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001709 return 0;
1710 }
1711
1712 if (arg[2].type == ARGT_SINT &&
1713 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001714 memprintf(err_msg, "payload offset too negative");
Willy Tarreau21d68a62012-04-20 15:52:36 +02001715 return 0;
1716 }
1717 return 1;
1718}
1719
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001720#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001721/* parse the "v4v6" bind keyword */
1722static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1723{
1724 struct listener *l;
1725
1726 list_for_each_entry(l, &conf->listeners, by_bind) {
1727 if (l->addr.ss_family == AF_INET6)
1728 l->options |= LI_O_V4V6;
1729 }
1730
1731 return 0;
1732}
1733
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001734/* parse the "v6only" bind keyword */
1735static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1736{
1737 struct listener *l;
1738
1739 list_for_each_entry(l, &conf->listeners, by_bind) {
1740 if (l->addr.ss_family == AF_INET6)
1741 l->options |= LI_O_V6ONLY;
1742 }
1743
1744 return 0;
1745}
1746#endif
1747
Willy Tarreau44791242012-09-12 23:27:21 +02001748#ifdef CONFIG_HAP_LINUX_TPROXY
1749/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001750static 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 +02001751{
1752 struct listener *l;
1753
Willy Tarreau4348fad2012-09-20 16:48:07 +02001754 list_for_each_entry(l, &conf->listeners, by_bind) {
1755 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1756 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001757 }
1758
Willy Tarreau44791242012-09-12 23:27:21 +02001759 return 0;
1760}
1761#endif
1762
1763#ifdef TCP_DEFER_ACCEPT
1764/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001765static 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 +02001766{
1767 struct listener *l;
1768
Willy Tarreau4348fad2012-09-20 16:48:07 +02001769 list_for_each_entry(l, &conf->listeners, by_bind) {
1770 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1771 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001772 }
1773
Willy Tarreau44791242012-09-12 23:27:21 +02001774 return 0;
1775}
1776#endif
1777
Willy Tarreau1c862c52012-10-05 16:21:00 +02001778#ifdef TCP_FASTOPEN
1779/* parse the "defer-accept" bind keyword */
1780static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1781{
1782 struct listener *l;
1783
1784 list_for_each_entry(l, &conf->listeners, by_bind) {
1785 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1786 l->options |= LI_O_TCP_FO;
1787 }
1788
1789 return 0;
1790}
1791#endif
1792
Willy Tarreau44791242012-09-12 23:27:21 +02001793#ifdef TCP_MAXSEG
1794/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001795static 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 +02001796{
1797 struct listener *l;
1798 int mss;
1799
Willy Tarreau44791242012-09-12 23:27:21 +02001800 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001801 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001802 return ERR_ALERT | ERR_FATAL;
1803 }
1804
1805 mss = atoi(args[cur_arg + 1]);
1806 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001807 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001808 return ERR_ALERT | ERR_FATAL;
1809 }
1810
Willy Tarreau4348fad2012-09-20 16:48:07 +02001811 list_for_each_entry(l, &conf->listeners, by_bind) {
1812 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1813 l->maxseg = mss;
1814 }
Willy Tarreau44791242012-09-12 23:27:21 +02001815
1816 return 0;
1817}
1818#endif
1819
1820#ifdef SO_BINDTODEVICE
1821/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001822static 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 +02001823{
1824 struct listener *l;
1825
Willy Tarreau44791242012-09-12 23:27:21 +02001826 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001827 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001828 return ERR_ALERT | ERR_FATAL;
1829 }
1830
Willy Tarreau4348fad2012-09-20 16:48:07 +02001831 list_for_each_entry(l, &conf->listeners, by_bind) {
1832 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1833 l->interface = strdup(args[cur_arg + 1]);
1834 }
Willy Tarreau44791242012-09-12 23:27:21 +02001835
1836 global.last_checks |= LSTCHK_NETADM;
1837 return 0;
1838}
1839#endif
1840
Willy Tarreaub6866442008-07-14 23:54:42 +02001841static struct cfg_kw_list cfg_kws = {{ },{
1842 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001843 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001844 { 0, NULL, NULL },
1845}};
1846
Willy Tarreau61612d42012-04-19 18:42:05 +02001847/* Note: must not be declared <const> as its list will be overwritten.
1848 * Please take care of keeping this list alphabetically sorted.
1849 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001850static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001851 { "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 +02001852 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001853 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1854 { "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 +02001855 { "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 +02001856 { "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 +02001857 { "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 +02001858 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001859 { NULL, NULL, NULL, NULL },
1860}};
1861
Willy Tarreau4a129812012-04-25 17:31:42 +02001862/* Note: must not be declared <const> as its list will be overwritten.
1863 * Note: fetches that may return multiple types must be declared as the lowest
1864 * common denominator, the type that can be casted into all other ones. For
1865 * instance v4/v6 must be declared v4.
1866 */
Willy Tarreau12785782012-04-27 21:37:17 +02001867static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001868 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001869 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001870 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001871 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1872 { "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 +02001873 { "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 +02001874 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001875 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001876}};
1877
Willy Tarreau44791242012-09-12 23:27:21 +02001878/************************************************************************/
1879/* All supported bind keywords must be declared here. */
1880/************************************************************************/
1881
1882/* Note: must not be declared <const> as its list will be overwritten.
1883 * Please take care of keeping this list alphabetically sorted, doing so helps
1884 * all code contributors.
1885 * Optional keywords are also declared with a NULL ->parse() function so that
1886 * the config parser can report an appropriate error when a known keyword was
1887 * not enabled.
1888 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001889static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001890#ifdef TCP_DEFER_ACCEPT
1891 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1892#endif
1893#ifdef SO_BINDTODEVICE
1894 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1895#endif
1896#ifdef TCP_MAXSEG
1897 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1898#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001899#ifdef TCP_FASTOPEN
1900 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1901#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001902#ifdef CONFIG_HAP_LINUX_TPROXY
1903 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1904#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001905#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001906 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001907 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1908#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001909 /* the versions with the NULL parse function*/
1910 { "defer-accept", NULL, 0 },
1911 { "interface", NULL, 1 },
1912 { "mss", NULL, 1 },
1913 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001914 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001915 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001916 { NULL, NULL, 0 },
1917}};
1918
Willy Tarreaue6b98942007-10-29 01:09:36 +01001919__attribute__((constructor))
1920static void __tcp_protocol_init(void)
1921{
1922 protocol_register(&proto_tcpv4);
1923 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001924 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001925 cfg_register_keywords(&cfg_kws);
1926 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001927 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001928}
1929
1930
1931/*
1932 * Local variables:
1933 * c-indent-level: 8
1934 * c-basic-offset: 8
1935 * End:
1936 */