blob: 9931d28d7d615fb6f0f0e0fe906478c5958c5d08 [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 Tarreaue6b98942007-10-29 01:09:36 +010042#include <proto/buffers.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020043#include <proto/frontend.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020044#include <proto/log.h>
45#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010046#include <proto/protocols.h>
47#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020049#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020050#include <proto/session.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020051#include <proto/sock_raw.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020052#include <proto/stick_table.h>
Willy Tarreau59b94792012-05-11 16:16:40 +020053#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020054#include <proto/task.h>
Emeric Brun97679e72010-09-23 17:56:44 +020055#include <proto/buffers.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010056
Willy Tarreaue8c66af2008-01-13 18:40:14 +010057#ifdef CONFIG_HAP_CTTPROXY
58#include <import/ip_tproxy.h>
59#endif
60
Emeric Bruncf20bf12010-10-22 16:06:11 +020061static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
62static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaueeda90e2012-05-11 19:53:32 +020063static int tcp_connect_write(int fd);
64static int tcp_connect_read(int fd);
Willy Tarreaue6b98942007-10-29 01:09:36 +010065
66/* Note: must not be declared <const> as its list will be overwritten */
67static struct protocol proto_tcpv4 = {
68 .name = "tcpv4",
69 .sock_domain = AF_INET,
70 .sock_type = SOCK_STREAM,
71 .sock_prot = IPPROTO_TCP,
72 .sock_family = AF_INET,
73 .sock_addrlen = sizeof(struct sockaddr_in),
74 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020075 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020076 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020077 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010078 .bind_all = tcp_bind_listeners,
79 .unbind_all = unbind_all_listeners,
80 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020081 .get_src = tcp_get_src,
82 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010083 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
84 .nb_listeners = 0,
85};
86
87/* Note: must not be declared <const> as its list will be overwritten */
88static struct protocol proto_tcpv6 = {
89 .name = "tcpv6",
90 .sock_domain = AF_INET6,
91 .sock_type = SOCK_STREAM,
92 .sock_prot = IPPROTO_TCP,
93 .sock_family = AF_INET6,
94 .sock_addrlen = sizeof(struct sockaddr_in6),
95 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020096 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020097 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020098 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010099 .bind_all = tcp_bind_listeners,
100 .unbind_all = unbind_all_listeners,
101 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200102 .get_src = tcp_get_src,
103 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100104 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
105 .nb_listeners = 0,
106};
107
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100108
David du Colombier6f5ccb12011-03-10 22:26:24 +0100109/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100110 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
111 * - 0 : ignore remote address (may even be a NULL pointer)
112 * - 1 : use provided address
113 * - 2 : use provided port
114 * - 3 : use both
115 *
116 * The function supports multiple foreign binding methods :
117 * - linux_tproxy: we directly bind to the foreign address
118 * - cttproxy: we bind to a local address then nat.
119 * The second one can be used as a fallback for the first one.
120 * This function returns 0 when everything's OK, 1 if it could not bind, to the
121 * local address, 2 if it could not bind to the foreign address.
122 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100123int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100124{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100125 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100126 int foreign_ok = 0;
127 int ret;
128
129#ifdef CONFIG_HAP_LINUX_TPROXY
130 static int ip_transp_working = 1;
131 if (flags && ip_transp_working) {
Simon Hormande072bd2011-06-24 15:11:37 +0900132 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
133 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100134 foreign_ok = 1;
135 else
136 ip_transp_working = 0;
137 }
138#endif
139 if (flags) {
140 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200141 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100142 switch (remote->ss_family) {
143 case AF_INET:
144 if (flags & 1)
145 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
146 if (flags & 2)
147 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
148 break;
149 case AF_INET6:
150 if (flags & 1)
151 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
152 if (flags & 2)
153 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
154 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100155 default:
156 /* we don't want to try to bind to an unknown address family */
157 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100158 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100159 }
160
Simon Hormande072bd2011-06-24 15:11:37 +0900161 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100162 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200163 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100164 if (ret < 0)
165 return 2;
166 }
167 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200168 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100169 if (ret < 0)
170 return 1;
171 }
172
173 if (!flags)
174 return 0;
175
176#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100177 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100178 struct in_tproxy itp1, itp2;
179 memset(&itp1, 0, sizeof(itp1));
180
181 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100182 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
183 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100184
185 /* set connect flag on socket */
186 itp2.op = TPROXY_FLAGS;
187 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
188
189 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
190 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
191 foreign_ok = 1;
192 }
193 }
194#endif
195 if (!foreign_ok)
196 /* we could not bind to a foreign address */
197 return 2;
198
199 return 0;
200}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100201
Willy Tarreau9650f372009-08-16 14:02:45 +0200202
203/*
Willy Tarreauac825402011-03-04 22:04:29 +0100204 * This function initiates a connection to the target assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200205 * (si->{target,addr.to}). A source address may be pointed to by si->addr.from
Willy Tarreauac825402011-03-04 22:04:29 +0100206 * in case of transparent proxying. Normal source bind addresses are still
207 * determined locally (due to the possible need of a source port).
208 * si->target may point either to a valid server or to a backend, depending
209 * on si->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are supported.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200210 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200211 * It can return one of :
212 * - SN_ERR_NONE if everything's OK
213 * - SN_ERR_SRVTO if there are no more servers
214 * - SN_ERR_SRVCL if the connection was refused by the server
215 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
216 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
217 * - SN_ERR_INTERNAL for any other purely internal errors
218 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
219 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100220
David du Colombier6f5ccb12011-03-10 22:26:24 +0100221int tcp_connect_server(struct stream_interface *si)
Willy Tarreau9650f372009-08-16 14:02:45 +0200222{
223 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100224 struct server *srv;
225 struct proxy *be;
226
227 switch (si->target.type) {
228 case TARG_TYPE_PROXY:
229 be = si->target.ptr.p;
230 srv = NULL;
231 break;
232 case TARG_TYPE_SERVER:
233 srv = si->target.ptr.s;
234 be = srv->proxy;
235 break;
236 default:
237 return SN_ERR_INTERNAL;
238 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200239
Willy Tarreau6471afb2011-09-23 10:54:59 +0200240 if ((fd = si->fd = socket(si->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200241 qfprintf(stderr, "Cannot get a server socket.\n");
242
243 if (errno == ENFILE)
244 send_log(be, LOG_EMERG,
245 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
246 be->id, maxfd);
247 else if (errno == EMFILE)
248 send_log(be, LOG_EMERG,
249 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
250 be->id, maxfd);
251 else if (errno == ENOBUFS || errno == ENOMEM)
252 send_log(be, LOG_EMERG,
253 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
254 be->id, maxfd);
255 /* this is a resource error */
256 return SN_ERR_RESOURCE;
257 }
258
259 if (fd >= global.maxsock) {
260 /* do not log anything there, it's a normal condition when this option
261 * is used to serialize connections to a server !
262 */
263 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
264 close(fd);
265 return SN_ERR_PRXCOND; /* it is a configuration limit */
266 }
267
268 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900269 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200270 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
271 close(fd);
272 return SN_ERR_INTERNAL;
273 }
274
275 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900276 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200277
278 if (be->options & PR_O_TCP_NOLING)
Willy Tarreauf6f82252011-11-30 18:02:24 +0100279 si->flags |= SI_FL_NOLINGER;
Willy Tarreau9650f372009-08-16 14:02:45 +0200280
281 /* allow specific binding :
282 * - server-specific at first
283 * - proxy-specific next
284 */
285 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200286 int ret, flags = 0;
287
Willy Tarreau9650f372009-08-16 14:02:45 +0200288 switch (srv->state & SRV_TPROXY_MASK) {
289 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200290 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200291 flags = 3;
292 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200293 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200294 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200295 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200296 break;
297 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200298
Willy Tarreau9650f372009-08-16 14:02:45 +0200299#ifdef SO_BINDTODEVICE
300 /* Note: this might fail if not CAP_NET_RAW */
301 if (srv->iface_name)
302 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
303#endif
304
305 if (srv->sport_range) {
306 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100307 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200308
309 ret = 1;
310 src = srv->source_addr;
311
312 do {
313 /* note: in case of retry, we may have to release a previously
314 * allocated port, hence this loop's construct.
315 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200316 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
317 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200318
319 if (!attempts)
320 break;
321 attempts--;
322
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200323 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
324 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200325 break;
326
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200327 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200328 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200329
Willy Tarreau6471afb2011-09-23 10:54:59 +0200330 ret = tcp_bind_socket(fd, flags, &src, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200331 } while (ret != 0); /* binding NOK */
332 }
333 else {
Willy Tarreau6471afb2011-09-23 10:54:59 +0200334 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200335 }
336
337 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200338 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
339 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200340 close(fd);
341
342 if (ret == 1) {
343 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
344 be->id, srv->id);
345 send_log(be, LOG_EMERG,
346 "Cannot bind to source address before connect() for server %s/%s.\n",
347 be->id, srv->id);
348 } else {
349 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
350 be->id, srv->id);
351 send_log(be, LOG_EMERG,
352 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
353 be->id, srv->id);
354 }
355 return SN_ERR_RESOURCE;
356 }
357 }
358 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200359 int ret, flags = 0;
360
Willy Tarreau9650f372009-08-16 14:02:45 +0200361 switch (be->options & PR_O_TPXY_MASK) {
362 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200363 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200364 flags = 3;
365 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200366 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200367 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200368 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200369 break;
370 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200371
Willy Tarreau9650f372009-08-16 14:02:45 +0200372#ifdef SO_BINDTODEVICE
373 /* Note: this might fail if not CAP_NET_RAW */
374 if (be->iface_name)
375 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
376#endif
Willy Tarreau6471afb2011-09-23 10:54:59 +0200377 ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200378 if (ret) {
379 close(fd);
380 if (ret == 1) {
381 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
382 be->id);
383 send_log(be, LOG_EMERG,
384 "Cannot bind to source address before connect() for proxy %s.\n",
385 be->id);
386 } else {
387 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
388 be->id);
389 send_log(be, LOG_EMERG,
390 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
391 be->id);
392 }
393 return SN_ERR_RESOURCE;
394 }
395 }
396
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400397#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200398 /* disabling tcp quick ack now allows the first request to leave the
399 * machine with the first ACK. We only do this if there are pending
400 * data in the buffer.
401 */
Willy Tarreau2e046c62012-03-01 16:08:30 +0100402 if ((be->options2 & PR_O2_SMARTCON) && si->ob->o)
Simon Hormande072bd2011-06-24 15:11:37 +0900403 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200404#endif
405
Willy Tarreaue803de22010-01-21 17:43:04 +0100406 if (global.tune.server_sndbuf)
407 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
408
409 if (global.tune.server_rcvbuf)
410 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
411
Willy Tarreau9b061e32012-04-07 18:03:52 +0200412 si->flags &= ~SI_FL_FROM_SET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200413 if ((connect(fd, (struct sockaddr *)&si->addr.to, get_addr_len(&si->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200414 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
415
416 if (errno == EAGAIN || errno == EADDRINUSE) {
417 char *msg;
418 if (errno == EAGAIN) /* no free ports left, try again later */
419 msg = "no free ports";
420 else
421 msg = "local address already in use";
422
423 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200424 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
425 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200426 close(fd);
427 send_log(be, LOG_EMERG,
428 "Connect() failed for server %s/%s: %s.\n",
429 be->id, srv->id, msg);
430 return SN_ERR_RESOURCE;
431 } else if (errno == ETIMEDOUT) {
432 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200433 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
434 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200435 close(fd);
436 return SN_ERR_SRVTO;
437 } else {
438 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
439 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200440 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
441 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 close(fd);
443 return SN_ERR_SRVCL;
444 }
445 }
446
William Lallemandb7ff6a32012-03-02 14:35:21 +0100447 /* needs src ip/port for logging */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200448 if (si->flags & SI_FL_SRC_ADDR)
Willy Tarreau59b94792012-05-11 16:16:40 +0200449 si_get_from_addr(si);
William Lallemandb7ff6a32012-03-02 14:35:21 +0100450
Willy Tarreau9650f372009-08-16 14:02:45 +0200451 fdtab[fd].owner = si;
452 fdtab[fd].state = FD_STCONN; /* connection in progress */
453 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau9650f372009-08-16 14:02:45 +0200454
Willy Tarreaube0688c2012-05-18 15:15:26 +0200455 /* If we have nothing to send, we want to confirm that the TCP
456 * connection is established before doing so, so we use our own write
457 * callback then switch to the sock layer.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200458 */
Willy Tarreaua190d592012-05-20 18:35:19 +0200459 if ((si->ob->flags & BF_OUT_EMPTY) || si->send_proxy_ofs) {
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200460 fdtab[fd].cb[DIR_RD].f = tcp_connect_read;
461 fdtab[fd].cb[DIR_WR].f = tcp_connect_write;
462 }
463 else {
464 fdtab[fd].cb[DIR_RD].f = si->sock.read;
465 fdtab[fd].cb[DIR_WR].f = si->sock.write;
466 }
467
Willy Tarreau6471afb2011-09-23 10:54:59 +0200468 fdinfo[fd].peeraddr = (struct sockaddr *)&si->addr.to;
469 fdinfo[fd].peerlen = get_addr_len(&si->addr.to);
Willy Tarreau9650f372009-08-16 14:02:45 +0200470
471 fd_insert(fd);
472 EV_FD_SET(fd, DIR_WR); /* for connect status */
473
474 si->state = SI_ST_CON;
475 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
476 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
477
478 return SN_ERR_NONE; /* connection is OK */
479}
480
481
Willy Tarreau59b94792012-05-11 16:16:40 +0200482/*
483 * Retrieves the source address for the socket <fd>, with <dir> indicating
484 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
485 * success, -1 in case of error. The socket's source address is stored in
486 * <sa> for <salen> bytes.
487 */
488int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
489{
490 if (dir)
491 return getsockname(fd, sa, &salen);
492 else
493 return getpeername(fd, sa, &salen);
494}
495
496
497/*
498 * Retrieves the original destination address for the socket <fd>, with <dir>
499 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
500 * listener, if the original destination address was translated, the original
501 * address is retrieved. It returns 0 in case of success, -1 in case of error.
502 * The socket's source address is stored in <sa> for <salen> bytes.
503 */
504int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
505{
506 if (dir)
507 return getpeername(fd, sa, &salen);
508#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
509 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
510 return 0;
511#endif
512 else
513 return getsockname(fd, sa, &salen);
514}
515
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200516/* This is the callback which is set when a connection establishment is pending
517 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreaua190d592012-05-20 18:35:19 +0200518 * once the connection is established. It returns zero if it needs some polling
519 * before being called again.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200520 */
521static int tcp_connect_write(int fd)
522{
523 struct stream_interface *si = fdtab[fd].owner;
524 struct buffer *b = si->ob;
Willy Tarreaua190d592012-05-20 18:35:19 +0200525 int retval = 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200526
527 if (fdtab[fd].state == FD_STERROR)
528 goto out_error;
529
Willy Tarreaua190d592012-05-20 18:35:19 +0200530 if (fdtab[fd].state != FD_STCONN)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200531 goto out_ignore; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200532
533 /* we might have been called just after an asynchronous shutw */
534 if (b->flags & BF_SHUTW)
535 goto out_wakeup;
536
Willy Tarreaua190d592012-05-20 18:35:19 +0200537 /* If we have a PROXY line to send, we'll use this to validate the
538 * connection, in which case the connection is validated only once
539 * we've sent the whole proxy line. Otherwise we use connect().
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200540 */
Willy Tarreaua190d592012-05-20 18:35:19 +0200541 if (si->send_proxy_ofs) {
542 int ret;
543
544 /* The target server expects a PROXY line to be sent first.
545 * If the send_proxy_ofs is negative, it corresponds to the
546 * offset to start sending from then end of the proxy string
547 * (which is recomputed every time since it's constant). If
548 * it is positive, it means we have to send from the start.
549 */
550 ret = make_proxy_line(trash, trashlen, &b->prod->addr.from, &b->prod->addr.to);
551 if (!ret)
552 goto out_error;
553
554 if (si->send_proxy_ofs > 0)
555 si->send_proxy_ofs = -ret; /* first call */
556
557 /* we have to send trash from (ret+sp for -sp bytes) */
558 ret = send(si->fd, trash + ret + si->send_proxy_ofs, -si->send_proxy_ofs,
559 (b->flags & BF_OUT_EMPTY) ? 0 : MSG_MORE);
560
561 if (ret == 0)
562 goto out_ignore;
563
564 if (ret < 0) {
565 if (errno == EAGAIN)
566 goto out_ignore;
567 goto out_error;
568 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200569
Willy Tarreaua190d592012-05-20 18:35:19 +0200570 si->send_proxy_ofs += ret; /* becomes zero once complete */
571 if (si->send_proxy_ofs != 0)
572 goto out_ignore;
573
574 /* OK we've sent the whole line, we're connected */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200575 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200576 else {
577 /* We have no data to send to check the connection, and
578 * getsockopt() will not inform us whether the connection
579 * is still pending. So we'll reuse connect() to check the
580 * state of the socket. This has the advantage of giving us
581 * the following info :
582 * - error
583 * - connecting (EALREADY, EINPROGRESS)
584 * - connected (EISCONN, 0)
585 */
586 if ((connect(fd, fdinfo[fd].peeraddr, fdinfo[fd].peerlen) < 0)) {
587 if (errno == EALREADY || errno == EINPROGRESS)
588 goto out_ignore;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200589
Willy Tarreaua190d592012-05-20 18:35:19 +0200590 if (errno && errno != EISCONN)
591 goto out_error;
592
593 /* otherwise we're connected */
594 }
595 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200596
597 /* OK we just need to indicate that we got a connection
598 * and that we wrote nothing.
599 */
600 b->flags |= BF_WRITE_NULL;
601
Willy Tarreaua190d592012-05-20 18:35:19 +0200602 /* The FD is ready now, we can hand the handlers to the socket layer
603 * and forward the event there to start working on the socket.
604 */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200605 fdtab[fd].cb[DIR_RD].f = si->sock.read;
606 fdtab[fd].cb[DIR_WR].f = si->sock.write;
607 fdtab[fd].state = FD_STREADY;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200608 si->exp = TICK_ETERNITY;
Willy Tarreaua190d592012-05-20 18:35:19 +0200609 return si->sock.write(fd);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200610
611 out_wakeup:
612 task_wakeup(si->owner, TASK_WOKEN_IO);
613
614 out_ignore:
615 fdtab[fd].ev &= ~FD_POLL_OUT;
616 return retval;
617
618 out_error:
619 /* Write error on the file descriptor. We mark the FD as STERROR so
620 * that we don't use it anymore. The error is reported to the stream
621 * interface which will take proper action. We must not perturbate the
622 * buffer because the stream interface wants to ensure transparent
623 * connection retries.
624 */
625
626 fdtab[fd].state = FD_STERROR;
627 fdtab[fd].ev &= ~FD_POLL_STICKY;
628 EV_FD_REM(fd);
629 si->flags |= SI_FL_ERR;
Willy Tarreaua190d592012-05-20 18:35:19 +0200630 retval = 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200631 goto out_wakeup;
632}
633
634
635/* might be used on connect error */
636static int tcp_connect_read(int fd)
637{
638 struct stream_interface *si = fdtab[fd].owner;
639 int retval;
640
641 retval = 1;
642
643 if (fdtab[fd].state == FD_STERROR)
644 goto out_error;
645
646 if (fdtab[fd].state != FD_STCONN) {
647 retval = 0;
648 goto out_ignore; /* strange we were called while ready */
649 }
650
651 /* stop here if we reached the end of data */
652 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
653 goto out_error;
654
655 out_wakeup:
656 task_wakeup(si->owner, TASK_WOKEN_IO);
657 out_ignore:
658 fdtab[fd].ev &= ~FD_POLL_IN;
659 return retval;
660
661 out_error:
662 /* Read error on the file descriptor. We mark the FD as STERROR so
663 * that we don't use it anymore. The error is reported to the stream
664 * interface which will take proper action. We must not perturbate the
665 * buffer because the stream interface wants to ensure transparent
666 * connection retries.
667 */
668
669 fdtab[fd].state = FD_STERROR;
670 fdtab[fd].ev &= ~FD_POLL_STICKY;
671 EV_FD_REM(fd);
672 si->flags |= SI_FL_ERR;
673 goto out_wakeup;
674}
675
Willy Tarreau59b94792012-05-11 16:16:40 +0200676
Willy Tarreaue6b98942007-10-29 01:09:36 +0100677/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
678 * an error message in <err> if the message is at most <errlen> bytes long
679 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
680 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
681 * was alright and that no message was returned. ERR_RETRYABLE means that an
682 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700683 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100684 * the meaning of the error, but just indicate that a message is present which
685 * should be displayed with the respective level. Last, ERR_ABORT indicates
686 * that it's pointless to try to start other listeners. No error message is
687 * returned if errlen is NULL.
688 */
689int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
690{
691 __label__ tcp_return, tcp_close_return;
692 int fd, err;
693 const char *msg = NULL;
694
695 /* ensure we never return garbage */
696 if (errmsg && errlen)
697 *errmsg = 0;
698
699 if (listener->state != LI_ASSIGNED)
700 return ERR_NONE; /* already bound */
701
702 err = ERR_NONE;
703
704 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
705 err |= ERR_RETRYABLE | ERR_ALERT;
706 msg = "cannot create listening socket";
707 goto tcp_return;
708 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100709
Willy Tarreaue6b98942007-10-29 01:09:36 +0100710 if (fd >= global.maxsock) {
711 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
712 msg = "not enough free sockets (raise '-n' parameter)";
713 goto tcp_close_return;
714 }
715
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200716 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100717 err |= ERR_FATAL | ERR_ALERT;
718 msg = "cannot make socket non-blocking";
719 goto tcp_close_return;
720 }
721
Simon Hormande072bd2011-06-24 15:11:37 +0900722 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100723 /* not fatal but should be reported */
724 msg = "cannot do so_reuseaddr";
725 err |= ERR_ALERT;
726 }
727
728 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900729 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100730
Willy Tarreaue6b98942007-10-29 01:09:36 +0100731#ifdef SO_REUSEPORT
732 /* OpenBSD supports this. As it's present in old libc versions of Linux,
733 * it might return an error that we will silently ignore.
734 */
Simon Hormande072bd2011-06-24 15:11:37 +0900735 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100736#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100737#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100738 if ((listener->options & LI_O_FOREIGN)
Simon Hormande072bd2011-06-24 15:11:37 +0900739 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
740 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100741 msg = "cannot make listening socket transparent";
742 err |= ERR_ALERT;
743 }
744#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100745#ifdef SO_BINDTODEVICE
746 /* Note: this might fail if not CAP_NET_RAW */
747 if (listener->interface) {
748 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100749 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100750 msg = "cannot bind listener to device";
751 err |= ERR_WARN;
752 }
753 }
754#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400755#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100756 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400757 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200758 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
759 msg = "cannot set MSS";
760 err |= ERR_WARN;
761 }
762 }
763#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200764#if defined(TCP_DEFER_ACCEPT)
765 if (listener->options & LI_O_DEF_ACCEPT) {
766 /* defer accept by up to one second */
767 int accept_delay = 1;
768 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
769 msg = "cannot enable DEFER_ACCEPT";
770 err |= ERR_WARN;
771 }
772 }
773#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100774 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
775 err |= ERR_RETRYABLE | ERR_ALERT;
776 msg = "cannot bind socket";
777 goto tcp_close_return;
778 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100779
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100780 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100781 err |= ERR_RETRYABLE | ERR_ALERT;
782 msg = "cannot listen to socket";
783 goto tcp_close_return;
784 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100785
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400786#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200787 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900788 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200789#endif
790
Willy Tarreaue6b98942007-10-29 01:09:36 +0100791 /* the socket is ready */
792 listener->fd = fd;
793 listener->state = LI_LISTEN;
794
Willy Tarreaueabf3132008-08-29 23:36:51 +0200795 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100796 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaueb472682010-05-28 18:46:57 +0200797 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
798 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
799 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
Willy Tarreau5d707e12009-06-28 11:09:07 +0200800
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200801 fdinfo[fd].peeraddr = NULL;
802 fdinfo[fd].peerlen = 0;
Willy Tarreaueb472682010-05-28 18:46:57 +0200803 fd_insert(fd);
804
Willy Tarreaue6b98942007-10-29 01:09:36 +0100805 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100806 if (msg && errlen) {
807 char pn[INET6_ADDRSTRLEN];
808
Willy Tarreau631f01c2011-09-05 00:36:48 +0200809 addr_to_str(&listener->addr, pn, sizeof(pn));
810 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100811 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100812 return err;
813
814 tcp_close_return:
815 close(fd);
816 goto tcp_return;
817}
818
819/* This function creates all TCP sockets bound to the protocol entry <proto>.
820 * It is intended to be used as the protocol's bind_all() function.
821 * The sockets will be registered but not added to any fd_set, in order not to
822 * loose them across the fork(). A call to enable_all_listeners() is needed
823 * to complete initialization. The return value is composed from ERR_*.
824 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200825static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100826{
827 struct listener *listener;
828 int err = ERR_NONE;
829
830 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200831 err |= tcp_bind_listener(listener, errmsg, errlen);
832 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100833 break;
834 }
835
836 return err;
837}
838
839/* Add listener to the list of tcpv4 listeners. The listener's state
840 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
841 * listeners is updated. This is the function to use to add a new listener.
842 */
843void tcpv4_add_listener(struct listener *listener)
844{
845 if (listener->state != LI_INIT)
846 return;
847 listener->state = LI_ASSIGNED;
848 listener->proto = &proto_tcpv4;
849 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
850 proto_tcpv4.nb_listeners++;
851}
852
853/* Add listener to the list of tcpv4 listeners. The listener's state
854 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
855 * listeners is updated. This is the function to use to add a new listener.
856 */
857void tcpv6_add_listener(struct listener *listener)
858{
859 if (listener->state != LI_INIT)
860 return;
861 listener->state = LI_ASSIGNED;
862 listener->proto = &proto_tcpv6;
863 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
864 proto_tcpv6.nb_listeners++;
865}
866
Willy Tarreauedcf6682008-11-30 23:15:34 +0100867/* This function performs the TCP request analysis on the current request. It
868 * returns 1 if the processing can continue on next analysers, or zero if it
869 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200870 * request. It relies on buffers flags, and updates s->req->analysers. The
871 * function may be called for frontend rules and backend rules. It only relies
872 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100873 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200874int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100875{
876 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200877 struct stksess *ts;
878 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100879 int partial;
880
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100881 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 +0100882 now_ms, __FUNCTION__,
883 s,
884 req,
885 req->rex, req->wex,
886 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100887 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100888 req->analysers);
889
Willy Tarreauedcf6682008-11-30 23:15:34 +0100890 /* We don't know whether we have enough data, so must proceed
891 * this way :
892 * - iterate through all rules in their declaration order
893 * - if one rule returns MISS, it means the inspect delay is
894 * not over yet, then return immediately, otherwise consider
895 * it as a non-match.
896 * - if one rule returns OK, then return OK
897 * - if one rule returns KO, then return KO
898 */
899
Willy Tarreaub824b002010-09-29 16:36:16 +0200900 if (req->flags & (BF_SHUTR|BF_FULL) || !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200901 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100902 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200903 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100904
Willy Tarreaufb356202010-08-03 14:02:05 +0200905 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100906 int ret = ACL_PAT_PASS;
907
908 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200909 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100910 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200911 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100912 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200913 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
914 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100915 return 0;
916 }
917
918 ret = acl_pass(ret);
919 if (rule->cond->pol == ACL_COND_UNLESS)
920 ret = !ret;
921 }
922
923 if (ret) {
924 /* we have a matching rule. */
925 if (rule->action == TCP_ACT_REJECT) {
926 buffer_abort(req);
927 buffer_abort(s->rep);
928 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200929
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100930 s->be->be_counters.denied_req++;
931 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200932 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200933 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200934
Willy Tarreauedcf6682008-11-30 23:15:34 +0100935 if (!(s->flags & SN_ERR_MASK))
936 s->flags |= SN_ERR_PRXCOND;
937 if (!(s->flags & SN_FINST_MASK))
938 s->flags |= SN_FINST_R;
939 return 0;
940 }
Willy Tarreau56123282010-08-06 19:06:56 +0200941 else if (rule->action == TCP_ACT_TRK_SC1) {
942 if (!s->stkctr1_entry) {
943 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200944 * Also, note that right now we can only track SRC so we
945 * don't check how to get the key, but later we may need
946 * to consider rule->act_prm->trk_ctr.type.
947 */
948 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100949 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200950 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200951 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200952 if (s->fe != s->be)
953 s->flags |= SN_BE_TRACK_SC1;
954 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200955 }
956 }
Willy Tarreau56123282010-08-06 19:06:56 +0200957 else if (rule->action == TCP_ACT_TRK_SC2) {
958 if (!s->stkctr2_entry) {
959 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200960 * Also, note that right now we can only track SRC so we
961 * don't check how to get the key, but later we may need
962 * to consider rule->act_prm->trk_ctr.type.
963 */
964 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100965 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200966 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200967 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200968 if (s->fe != s->be)
969 s->flags |= SN_BE_TRACK_SC2;
970 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200971 }
972 }
973 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100974 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200975 break;
976 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100977 }
978 }
979
980 /* if we get there, it means we have no rule which matches, or
981 * we have an explicit accept, so we apply the default accept.
982 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200983 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100984 req->analyse_exp = TICK_ETERNITY;
985 return 1;
986}
987
Emeric Brun97679e72010-09-23 17:56:44 +0200988/* This function performs the TCP response analysis on the current response. It
989 * returns 1 if the processing can continue on next analysers, or zero if it
990 * needs more data, encounters an error, or wants to immediately abort the
991 * response. It relies on buffers flags, and updates s->rep->analysers. The
992 * function may be called for backend rules.
993 */
994int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit)
995{
996 struct tcp_rule *rule;
997 int partial;
998
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100999 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 +02001000 now_ms, __FUNCTION__,
1001 s,
1002 rep,
1003 rep->rex, rep->wex,
1004 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001005 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001006 rep->analysers);
1007
1008 /* We don't know whether we have enough data, so must proceed
1009 * this way :
1010 * - iterate through all rules in their declaration order
1011 * - if one rule returns MISS, it means the inspect delay is
1012 * not over yet, then return immediately, otherwise consider
1013 * it as a non-match.
1014 * - if one rule returns OK, then return OK
1015 * - if one rule returns KO, then return KO
1016 */
1017
1018 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001019 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001020 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001021 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001022
1023 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
1024 int ret = ACL_PAT_PASS;
1025
1026 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001027 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +02001028 if (ret == ACL_PAT_MISS) {
1029 /* just set the analyser timeout once at the beginning of the response */
1030 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
1031 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
1032 return 0;
1033 }
1034
1035 ret = acl_pass(ret);
1036 if (rule->cond->pol == ACL_COND_UNLESS)
1037 ret = !ret;
1038 }
1039
1040 if (ret) {
1041 /* we have a matching rule. */
1042 if (rule->action == TCP_ACT_REJECT) {
1043 buffer_abort(rep);
1044 buffer_abort(s->req);
1045 rep->analysers = 0;
1046
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001047 s->be->be_counters.denied_resp++;
1048 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001049 if (s->listener->counters)
1050 s->listener->counters->denied_resp++;
1051
1052 if (!(s->flags & SN_ERR_MASK))
1053 s->flags |= SN_ERR_PRXCOND;
1054 if (!(s->flags & SN_FINST_MASK))
1055 s->flags |= SN_FINST_D;
1056 return 0;
1057 }
1058 else {
1059 /* otherwise accept */
1060 break;
1061 }
1062 }
1063 }
1064
1065 /* if we get there, it means we have no rule which matches, or
1066 * we have an explicit accept, so we apply the default accept.
1067 */
1068 rep->analysers &= ~an_bit;
1069 rep->analyse_exp = TICK_ETERNITY;
1070 return 1;
1071}
1072
1073
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001074/* This function performs the TCP layer4 analysis on the current request. It
1075 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1076 * matches or if no more rule matches. It can only use rules which don't need
1077 * any data.
1078 */
1079int tcp_exec_req_rules(struct session *s)
1080{
1081 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001082 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001083 struct stktable *t = NULL;
1084 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001085 int ret;
1086
1087 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1088 ret = ACL_PAT_PASS;
1089
1090 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001091 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001092 ret = acl_pass(ret);
1093 if (rule->cond->pol == ACL_COND_UNLESS)
1094 ret = !ret;
1095 }
1096
1097 if (ret) {
1098 /* we have a matching rule. */
1099 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001100 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001101 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001102 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001103
1104 if (!(s->flags & SN_ERR_MASK))
1105 s->flags |= SN_ERR_PRXCOND;
1106 if (!(s->flags & SN_FINST_MASK))
1107 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001108 result = 0;
1109 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001110 }
Willy Tarreau56123282010-08-06 19:06:56 +02001111 else if (rule->action == TCP_ACT_TRK_SC1) {
1112 if (!s->stkctr1_entry) {
1113 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001114 * Also, note that right now we can only track SRC so we
1115 * don't check how to get the key, but later we may need
1116 * to consider rule->act_prm->trk_ctr.type.
1117 */
1118 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001119 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001120 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001121 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001122 }
1123 }
Willy Tarreau56123282010-08-06 19:06:56 +02001124 else if (rule->action == TCP_ACT_TRK_SC2) {
1125 if (!s->stkctr2_entry) {
1126 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001127 * Also, note that right now we can only track SRC so we
1128 * don't check how to get the key, but later we may need
1129 * to consider rule->act_prm->trk_ctr.type.
1130 */
1131 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001132 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001133 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001134 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001135 }
1136 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001137 else {
1138 /* otherwise it's an accept */
1139 break;
1140 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001141 }
1142 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001143 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001144}
1145
Emeric Brun97679e72010-09-23 17:56:44 +02001146/* Parse a tcp-response rule. Return a negative value in case of failure */
1147static int tcp_parse_response_rule(char **args, int arg, int section_type,
1148 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001149 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001150{
1151 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001152 memprintf(err, "%s %s is only allowed in 'backend' sections",
1153 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001154 return -1;
1155 }
1156
1157 if (strcmp(args[arg], "accept") == 0) {
1158 arg++;
1159 rule->action = TCP_ACT_ACCEPT;
1160 }
1161 else if (strcmp(args[arg], "reject") == 0) {
1162 arg++;
1163 rule->action = TCP_ACT_REJECT;
1164 }
1165 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001166 memprintf(err,
1167 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1168 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001169 return -1;
1170 }
1171
1172 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001173 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1174 memprintf(err,
1175 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1176 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001177 return -1;
1178 }
1179 }
1180 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001181 memprintf(err,
1182 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001183 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1184 return -1;
1185 }
1186 return 0;
1187}
1188
1189
1190
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001191/* Parse a tcp-request rule. Return a negative value in case of failure */
1192static int tcp_parse_request_rule(char **args, int arg, int section_type,
1193 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001194 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001195{
1196 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001197 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1198 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001199 return -1;
1200 }
1201
1202 if (!strcmp(args[arg], "accept")) {
1203 arg++;
1204 rule->action = TCP_ACT_ACCEPT;
1205 }
1206 else if (!strcmp(args[arg], "reject")) {
1207 arg++;
1208 rule->action = TCP_ACT_REJECT;
1209 }
Willy Tarreau56123282010-08-06 19:06:56 +02001210 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001211 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001212 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001213
1214 arg++;
1215 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001216 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001217
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001218 if (ret < 0) { /* nb: warnings are not handled yet */
1219 memprintf(err,
1220 "'%s %s %s' : %s in %s '%s'",
1221 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1222 return ret;
1223 }
Willy Tarreau56123282010-08-06 19:06:56 +02001224 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001225 }
Willy Tarreau56123282010-08-06 19:06:56 +02001226 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001227 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001228 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001229
1230 arg++;
1231 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001232 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001233
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001234 if (ret < 0) { /* nb: warnings are not handled yet */
1235 memprintf(err,
1236 "'%s %s %s' : %s in %s '%s'",
1237 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1238 return ret;
1239 }
Willy Tarreau56123282010-08-06 19:06:56 +02001240 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001241 }
1242 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001243 memprintf(err,
1244 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1245 "or 'track-sc2' in %s '%s' (got '%s')",
1246 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001247 return -1;
1248 }
1249
1250 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001251 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1252 memprintf(err,
1253 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1254 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001255 return -1;
1256 }
1257 }
1258 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001259 memprintf(err,
1260 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001261 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1262 return -1;
1263 }
1264 return 0;
1265}
1266
Emeric Brun97679e72010-09-23 17:56:44 +02001267/* This function should be called to parse a line starting with the "tcp-response"
1268 * keyword.
1269 */
1270static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001271 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001272{
1273 const char *ptr = NULL;
1274 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001275 int warn = 0;
1276 int arg;
1277 struct tcp_rule *rule;
1278
1279 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001280 memprintf(err, "missing argument for '%s' in %s '%s'",
1281 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001282 return -1;
1283 }
1284
1285 if (strcmp(args[1], "inspect-delay") == 0) {
1286 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001287 memprintf(err, "%s %s is only allowed in 'backend' sections",
1288 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001289 return -1;
1290 }
1291
1292 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001293 memprintf(err,
1294 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1295 args[0], args[1], proxy_type_str(curpx), curpx->id);
1296 if (ptr)
1297 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001298 return -1;
1299 }
1300
1301 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001302 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1303 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001304 return 1;
1305 }
1306 curpx->tcp_rep.inspect_delay = val;
1307 return 0;
1308 }
1309
Simon Hormande072bd2011-06-24 15:11:37 +09001310 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001311 LIST_INIT(&rule->list);
1312 arg = 1;
1313
1314 if (strcmp(args[1], "content") == 0) {
1315 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001316 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001317 goto error;
1318
1319 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1320 struct acl *acl;
1321 const char *name;
1322
1323 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1324 name = acl ? acl->name : "(unknown)";
1325
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001326 memprintf(err,
1327 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1328 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001329 warn++;
1330 }
1331
1332 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1333 }
1334 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001335 memprintf(err,
1336 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1337 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001338 goto error;
1339 }
1340
1341 return warn;
1342 error:
1343 free(rule);
1344 return -1;
1345}
1346
1347
Willy Tarreaub6866442008-07-14 23:54:42 +02001348/* This function should be called to parse a line starting with the "tcp-request"
1349 * keyword.
1350 */
1351static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001352 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001353{
1354 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001355 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001356 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001357 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001358 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001359
1360 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001361 if (curpx == defpx)
1362 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1363 else
1364 memprintf(err, "missing argument for '%s' in %s '%s'",
1365 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001366 return -1;
1367 }
1368
1369 if (!strcmp(args[1], "inspect-delay")) {
1370 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001371 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1372 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001373 return -1;
1374 }
1375
Willy Tarreaub6866442008-07-14 23:54:42 +02001376 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001377 memprintf(err,
1378 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1379 args[0], args[1], proxy_type_str(curpx), curpx->id);
1380 if (ptr)
1381 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001382 return -1;
1383 }
1384
1385 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001386 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1387 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001388 return 1;
1389 }
1390 curpx->tcp_req.inspect_delay = val;
1391 return 0;
1392 }
1393
Simon Hormande072bd2011-06-24 15:11:37 +09001394 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001395 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001396 arg = 1;
1397
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001398 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001399 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001400 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001401 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001402
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001403 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001404 struct acl *acl;
1405 const char *name;
1406
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001407 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001408 name = acl ? acl->name : "(unknown)";
1409
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001410 memprintf(err,
1411 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1412 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001413 warn++;
1414 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001415 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001416 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001417 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001418 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001419
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001420 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001421 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1422 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001423 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001424 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001425
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001426 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001427 goto error;
1428
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001429 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1430 struct acl *acl;
1431 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001432
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001433 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1434 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001435
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001436 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001437 memprintf(err,
1438 "'%s %s' may not reference acl '%s' which makes use of "
1439 "payload in %s '%s'. Please use '%s content' for this.",
1440 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001441 goto error;
1442 }
1443 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001444 memprintf(err,
1445 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1446 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001447 warn++;
1448 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001449 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001450 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001451 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001452 if (curpx == defpx)
1453 memprintf(err,
1454 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1455 args[0], args[1]);
1456 else
1457 memprintf(err,
1458 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1459 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001460 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001461 }
1462
Willy Tarreau1a687942010-05-23 22:40:30 +02001463 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001464 error:
1465 free(rule);
1466 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001467}
1468
Willy Tarreau645513a2010-05-24 20:55:15 +02001469
1470/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001471/* All supported sample fetch functios must be declared here */
1472/************************************************************************/
1473
1474/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001475 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001476 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1477 * is a string (cookie name), other types will lead to undefined behaviour.
1478 */
1479int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001480smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001481 const struct arg *args, struct sample *smp)
1482{
1483 int bleft;
1484 const unsigned char *data;
1485
1486 if (!l4 || !l4->req)
1487 return 0;
1488
1489 smp->flags = 0;
1490 smp->type = SMP_T_CSTR;
1491
1492 bleft = l4->req->i;
1493 if (bleft <= 11)
1494 goto too_short;
1495
1496 data = (const unsigned char *)l4->req->p + 11;
1497 bleft -= 11;
1498
1499 if (bleft <= 7)
1500 goto too_short;
1501
1502 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1503 goto not_cookie;
1504
1505 data += 7;
1506 bleft -= 7;
1507
1508 while (bleft > 0 && *data == ' ') {
1509 data++;
1510 bleft--;
1511 }
1512
1513 if (args) {
1514
1515 if (bleft <= args->data.str.len)
1516 goto too_short;
1517
1518 if ((data[args->data.str.len] != '=') ||
1519 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1520 goto not_cookie;
1521
1522 data += args->data.str.len + 1;
1523 bleft -= args->data.str.len + 1;
1524 } else {
1525 while (bleft > 0 && *data != '=') {
1526 if (*data == '\r' || *data == '\n')
1527 goto not_cookie;
1528 data++;
1529 bleft--;
1530 }
1531
1532 if (bleft < 1)
1533 goto too_short;
1534
1535 if (*data != '=')
1536 goto not_cookie;
1537
1538 data++;
1539 bleft--;
1540 }
1541
1542 /* data points to cookie value */
1543 smp->data.str.str = (char *)data;
1544 smp->data.str.len = 0;
1545
1546 while (bleft > 0 && *data != '\r') {
1547 data++;
1548 bleft--;
1549 }
1550
1551 if (bleft < 2)
1552 goto too_short;
1553
1554 if (data[0] != '\r' || data[1] != '\n')
1555 goto not_cookie;
1556
1557 smp->data.str.len = (char *)data - smp->data.str.str;
1558 smp->flags = SMP_F_VOLATILE;
1559 return 1;
1560
1561 too_short:
1562 smp->flags = SMP_F_MAY_CHANGE;
1563 not_cookie:
1564 return 0;
1565}
1566
Willy Tarreau32389b72012-04-23 23:13:20 +02001567/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001568/* All supported ACL keywords must be declared here. */
1569/************************************************************************/
1570
Willy Tarreau32389b72012-04-23 23:13:20 +02001571/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1572static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001573acl_fetch_rdp_cookie_cnt(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 Tarreau32389b72012-04-23 23:13:20 +02001575{
1576 int ret;
1577
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001578 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001579
1580 if (smp->flags & SMP_F_MAY_CHANGE)
1581 return 0;
1582
1583 smp->flags = SMP_F_VOLATILE;
1584 smp->type = SMP_T_UINT;
1585 smp->data.uint = ret;
1586 return 1;
1587}
1588
1589
Willy Tarreau4a129812012-04-25 17:31:42 +02001590/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001591static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001592smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001593 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001594{
Willy Tarreauf4362b32011-12-16 17:49:52 +01001595 switch (l4->si[0].addr.from.ss_family) {
1596 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001597 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.from)->sin_addr;
1598 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001599 break;
1600 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001601 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.from))->sin6_addr;
1602 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001603 break;
1604 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001605 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001606 }
Emeric Brunf769f512010-10-22 17:14:01 +02001607
Willy Tarreau37406352012-04-23 16:16:37 +02001608 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001609 return 1;
1610}
1611
Willy Tarreaua5e37562011-12-16 17:06:15 +01001612/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001613static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001614smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001615 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001616{
Willy Tarreauf853c462012-04-23 18:53:56 +02001617 smp->type = SMP_T_UINT;
1618 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001619 return 0;
1620
Willy Tarreau37406352012-04-23 16:16:37 +02001621 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001622 return 1;
1623}
1624
Willy Tarreau4a129812012-04-25 17:31:42 +02001625/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001626static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001627smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001628 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001629{
Willy Tarreau59b94792012-05-11 16:16:40 +02001630 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001631
Willy Tarreauf4362b32011-12-16 17:49:52 +01001632 switch (l4->si[0].addr.to.ss_family) {
1633 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001634 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.to)->sin_addr;
1635 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001636 break;
1637 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001638 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.to))->sin6_addr;
1639 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001640 break;
1641 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001642 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001643 }
Emeric Brunf769f512010-10-22 17:14:01 +02001644
Willy Tarreau37406352012-04-23 16:16:37 +02001645 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001646 return 1;
1647}
1648
Willy Tarreaua5e37562011-12-16 17:06:15 +01001649/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001650static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001651smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001652 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001653{
Willy Tarreau59b94792012-05-11 16:16:40 +02001654 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001655
Willy Tarreauf853c462012-04-23 18:53:56 +02001656 smp->type = SMP_T_UINT;
1657 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001658 return 0;
1659
Willy Tarreau37406352012-04-23 16:16:37 +02001660 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001661 return 1;
1662}
1663
1664static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001665smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1666 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001667{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001668 unsigned int len_offset = arg_p[0].data.uint;
1669 unsigned int len_size = arg_p[1].data.uint;
1670 unsigned int buf_offset;
1671 unsigned int buf_size = 0;
Emericf2d7cae2010-11-05 18:13:50 +01001672 struct buffer *b;
1673 int i;
1674
1675 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1676 /* by default buf offset == len offset + len size */
1677 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1678
1679 if (!l4)
1680 return 0;
1681
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001682 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001683
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001684 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001685 return 0;
1686
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001687 if (len_offset + len_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001688 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001689
1690 for (i = 0; i < len_size; i++) {
Willy Tarreau89fa7062012-03-02 16:13:16 +01001691 buf_size = (buf_size << 8) + ((unsigned char *)b->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001692 }
1693
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001694 /* buf offset may be implicit, absolute or relative */
1695 buf_offset = len_offset + len_size;
1696 if (arg_p[2].type == ARGT_UINT)
1697 buf_offset = arg_p[2].data.uint;
1698 else if (arg_p[2].type == ARGT_SINT)
1699 buf_offset += arg_p[2].data.sint;
1700
Willy Tarreau82ea8002012-04-25 19:19:43 +02001701 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1702 /* will never match */
1703 smp->flags = 0;
1704 return 0;
1705 }
1706
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001707 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001708 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001709
1710 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001711 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001712 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001713 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001714 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001715
1716 too_short:
1717 smp->flags = SMP_F_MAY_CHANGE;
1718 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001719}
1720
1721static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001722smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1723 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001724{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001725 unsigned int buf_offset = arg_p[0].data.uint;
1726 unsigned int buf_size = arg_p[1].data.uint;
Emericf2d7cae2010-11-05 18:13:50 +01001727 struct buffer *b;
1728
1729 if (!l4)
1730 return 0;
1731
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001732 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001733
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001734 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001735 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001736
1737 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1738 /* will never match */
1739 smp->flags = 0;
1740 return 0;
1741 }
Emericf2d7cae2010-11-05 18:13:50 +01001742
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001743 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001744 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001745
1746 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001747 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001748 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001749 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001750 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001751
1752 too_short:
1753 smp->flags = SMP_F_MAY_CHANGE;
1754 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001755}
1756
Willy Tarreau21d68a62012-04-20 15:52:36 +02001757/* This function is used to validate the arguments passed to a "payload" fetch
1758 * keyword. This keyword expects two positive integers, with the second one
1759 * being strictly positive. It is assumed that the types are already the correct
1760 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1761 * filled with a pointer to an error message in case of error, that the caller
1762 * is responsible for freeing. The initial location must either be freeable or
1763 * NULL.
1764 */
1765static int val_payload(struct arg *arg, char **err_msg)
1766{
1767 if (!arg[1].data.uint) {
1768 if (err_msg)
1769 memprintf(err_msg, "payload length must be > 0");
1770 return 0;
1771 }
1772 return 1;
1773}
1774
1775/* This function is used to validate the arguments passed to a "payload_lv" fetch
1776 * keyword. This keyword allows two positive integers and an optional signed one,
1777 * with the second one being strictly positive and the third one being greater than
1778 * the opposite of the two others if negative. It is assumed that the types are
1779 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1780 * not NULL, it will be filled with a pointer to an error message in case of
1781 * error, that the caller is responsible for freeing. The initial location must
1782 * either be freeable or NULL.
1783 */
1784static int val_payload_lv(struct arg *arg, char **err_msg)
1785{
1786 if (!arg[1].data.uint) {
1787 if (err_msg)
1788 memprintf(err_msg, "payload length must be > 0");
1789 return 0;
1790 }
1791
1792 if (arg[2].type == ARGT_SINT &&
1793 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1794 if (err_msg)
1795 memprintf(err_msg, "payload offset too negative");
1796 return 0;
1797 }
1798 return 1;
1799}
1800
Willy Tarreaub6866442008-07-14 23:54:42 +02001801static struct cfg_kw_list cfg_kws = {{ },{
1802 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001803 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001804 { 0, NULL, NULL },
1805}};
1806
Willy Tarreau61612d42012-04-19 18:42:05 +02001807/* Note: must not be declared <const> as its list will be overwritten.
1808 * Please take care of keeping this list alphabetically sorted.
1809 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001810static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001811 { "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 +02001812 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001813 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1814 { "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 +02001815 { "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 +02001816 { "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 +02001817 { "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 +02001818 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001819 { NULL, NULL, NULL, NULL },
1820}};
1821
Willy Tarreau4a129812012-04-25 17:31:42 +02001822/* Note: must not be declared <const> as its list will be overwritten.
1823 * Note: fetches that may return multiple types must be declared as the lowest
1824 * common denominator, the type that can be casted into all other ones. For
1825 * instance v4/v6 must be declared v4.
1826 */
Willy Tarreau12785782012-04-27 21:37:17 +02001827static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001828 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001829 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001830 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001831 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1832 { "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 +02001833 { "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 +02001834 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001835 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001836}};
1837
Willy Tarreaue6b98942007-10-29 01:09:36 +01001838__attribute__((constructor))
1839static void __tcp_protocol_init(void)
1840{
1841 protocol_register(&proto_tcpv4);
1842 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001843 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001844 cfg_register_keywords(&cfg_kws);
1845 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001846}
1847
1848
1849/*
1850 * Local variables:
1851 * c-indent-level: 8
1852 * c-basic-offset: 8
1853 * End:
1854 */