blob: 1d38b3caa610299734aee7deb5f234e1189bf855 [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;
David du Colombier65c17962012-07-13 14:34:59 +0200131 static int ip6_transp_working = 1;
132 switch (local->ss_family) {
133 case AF_INET:
134 if (flags && ip_transp_working) {
135 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
136 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
137 foreign_ok = 1;
138 else
139 ip_transp_working = 0;
140 }
141 break;
142 case AF_INET6:
143 if (flags && ip6_transp_working) {
144 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
145 foreign_ok = 1;
146 else
147 ip6_transp_working = 0;
148 }
149 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100150 }
151#endif
152 if (flags) {
153 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200154 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100155 switch (remote->ss_family) {
156 case AF_INET:
157 if (flags & 1)
158 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
159 if (flags & 2)
160 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
161 break;
162 case AF_INET6:
163 if (flags & 1)
164 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
165 if (flags & 2)
166 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
167 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100168 default:
169 /* we don't want to try to bind to an unknown address family */
170 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100171 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100172 }
173
Simon Hormande072bd2011-06-24 15:11:37 +0900174 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100175 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200176 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100177 if (ret < 0)
178 return 2;
179 }
180 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200181 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100182 if (ret < 0)
183 return 1;
184 }
185
186 if (!flags)
187 return 0;
188
189#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100190 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100191 struct in_tproxy itp1, itp2;
192 memset(&itp1, 0, sizeof(itp1));
193
194 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100195 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
196 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100197
198 /* set connect flag on socket */
199 itp2.op = TPROXY_FLAGS;
200 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
201
202 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
203 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
204 foreign_ok = 1;
205 }
206 }
207#endif
208 if (!foreign_ok)
209 /* we could not bind to a foreign address */
210 return 2;
211
212 return 0;
213}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100214
Willy Tarreau9650f372009-08-16 14:02:45 +0200215
216/*
Willy Tarreauac825402011-03-04 22:04:29 +0100217 * This function initiates a connection to the target assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200218 * (si->{target,addr.to}). A source address may be pointed to by si->addr.from
Willy Tarreauac825402011-03-04 22:04:29 +0100219 * in case of transparent proxying. Normal source bind addresses are still
220 * determined locally (due to the possible need of a source port).
221 * si->target may point either to a valid server or to a backend, depending
222 * on si->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are supported.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200223 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200224 * It can return one of :
225 * - SN_ERR_NONE if everything's OK
226 * - SN_ERR_SRVTO if there are no more servers
227 * - SN_ERR_SRVCL if the connection was refused by the server
228 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
229 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
230 * - SN_ERR_INTERNAL for any other purely internal errors
231 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
232 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100233
David du Colombier6f5ccb12011-03-10 22:26:24 +0100234int tcp_connect_server(struct stream_interface *si)
Willy Tarreau9650f372009-08-16 14:02:45 +0200235{
236 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100237 struct server *srv;
238 struct proxy *be;
239
240 switch (si->target.type) {
241 case TARG_TYPE_PROXY:
242 be = si->target.ptr.p;
243 srv = NULL;
244 break;
245 case TARG_TYPE_SERVER:
246 srv = si->target.ptr.s;
247 be = srv->proxy;
248 break;
249 default:
250 return SN_ERR_INTERNAL;
251 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200252
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200253 if ((fd = si->conn.t.sock.fd = socket(si->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200254 qfprintf(stderr, "Cannot get a server socket.\n");
255
256 if (errno == ENFILE)
257 send_log(be, LOG_EMERG,
258 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
259 be->id, maxfd);
260 else if (errno == EMFILE)
261 send_log(be, LOG_EMERG,
262 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
263 be->id, maxfd);
264 else if (errno == ENOBUFS || errno == ENOMEM)
265 send_log(be, LOG_EMERG,
266 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
267 be->id, maxfd);
268 /* this is a resource error */
269 return SN_ERR_RESOURCE;
270 }
271
272 if (fd >= global.maxsock) {
273 /* do not log anything there, it's a normal condition when this option
274 * is used to serialize connections to a server !
275 */
276 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
277 close(fd);
278 return SN_ERR_PRXCOND; /* it is a configuration limit */
279 }
280
281 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900282 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200283 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
284 close(fd);
285 return SN_ERR_INTERNAL;
286 }
287
288 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900289 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200290
291 if (be->options & PR_O_TCP_NOLING)
Willy Tarreauf6f82252011-11-30 18:02:24 +0100292 si->flags |= SI_FL_NOLINGER;
Willy Tarreau9650f372009-08-16 14:02:45 +0200293
294 /* allow specific binding :
295 * - server-specific at first
296 * - proxy-specific next
297 */
298 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200299 int ret, flags = 0;
300
Willy Tarreau9650f372009-08-16 14:02:45 +0200301 switch (srv->state & SRV_TPROXY_MASK) {
302 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200303 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200304 flags = 3;
305 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200306 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200307 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200308 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200309 break;
310 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200311
Willy Tarreau9650f372009-08-16 14:02:45 +0200312#ifdef SO_BINDTODEVICE
313 /* Note: this might fail if not CAP_NET_RAW */
314 if (srv->iface_name)
315 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
316#endif
317
318 if (srv->sport_range) {
319 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100320 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200321
322 ret = 1;
323 src = srv->source_addr;
324
325 do {
326 /* note: in case of retry, we may have to release a previously
327 * allocated port, hence this loop's construct.
328 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200329 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
330 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200331
332 if (!attempts)
333 break;
334 attempts--;
335
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200336 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
337 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200338 break;
339
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200340 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200341 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200342
Willy Tarreau6471afb2011-09-23 10:54:59 +0200343 ret = tcp_bind_socket(fd, flags, &src, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200344 } while (ret != 0); /* binding NOK */
345 }
346 else {
Willy Tarreau6471afb2011-09-23 10:54:59 +0200347 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200348 }
349
350 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200351 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
352 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200353 close(fd);
354
355 if (ret == 1) {
356 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
357 be->id, srv->id);
358 send_log(be, LOG_EMERG,
359 "Cannot bind to source address before connect() for server %s/%s.\n",
360 be->id, srv->id);
361 } else {
362 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
363 be->id, srv->id);
364 send_log(be, LOG_EMERG,
365 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
366 be->id, srv->id);
367 }
368 return SN_ERR_RESOURCE;
369 }
370 }
371 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200372 int ret, flags = 0;
373
Willy Tarreau9650f372009-08-16 14:02:45 +0200374 switch (be->options & PR_O_TPXY_MASK) {
375 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200376 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200377 flags = 3;
378 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200379 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200380 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200381 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200382 break;
383 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200384
Willy Tarreau9650f372009-08-16 14:02:45 +0200385#ifdef SO_BINDTODEVICE
386 /* Note: this might fail if not CAP_NET_RAW */
387 if (be->iface_name)
388 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
389#endif
Willy Tarreau6471afb2011-09-23 10:54:59 +0200390 ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200391 if (ret) {
392 close(fd);
393 if (ret == 1) {
394 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
395 be->id);
396 send_log(be, LOG_EMERG,
397 "Cannot bind to source address before connect() for proxy %s.\n",
398 be->id);
399 } else {
400 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
401 be->id);
402 send_log(be, LOG_EMERG,
403 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
404 be->id);
405 }
406 return SN_ERR_RESOURCE;
407 }
408 }
409
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400410#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200411 /* disabling tcp quick ack now allows the first request to leave the
412 * machine with the first ACK. We only do this if there are pending
413 * data in the buffer.
414 */
Willy Tarreau2e046c62012-03-01 16:08:30 +0100415 if ((be->options2 & PR_O2_SMARTCON) && si->ob->o)
Simon Hormande072bd2011-06-24 15:11:37 +0900416 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200417#endif
418
Willy Tarreaue803de22010-01-21 17:43:04 +0100419 if (global.tune.server_sndbuf)
420 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
421
422 if (global.tune.server_rcvbuf)
423 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
424
Willy Tarreau9b061e32012-04-07 18:03:52 +0200425 si->flags &= ~SI_FL_FROM_SET;
Willy Tarreau96596ae2012-06-08 22:57:36 +0200426
427 si->conn.peeraddr = (struct sockaddr *)&si->addr.to;
428 si->conn.peerlen = get_addr_len(&si->addr.to);
429
430 if ((connect(fd, si->conn.peeraddr, si->conn.peerlen) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200431 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
432
433 if (errno == EAGAIN || errno == EADDRINUSE) {
434 char *msg;
435 if (errno == EAGAIN) /* no free ports left, try again later */
436 msg = "no free ports";
437 else
438 msg = "local address already in use";
439
440 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200441 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
442 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200443 close(fd);
444 send_log(be, LOG_EMERG,
445 "Connect() failed for server %s/%s: %s.\n",
446 be->id, srv->id, msg);
447 return SN_ERR_RESOURCE;
448 } else if (errno == ETIMEDOUT) {
449 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200450 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
451 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 close(fd);
453 return SN_ERR_SRVTO;
454 } else {
455 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
456 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200457 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
458 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200459 close(fd);
460 return SN_ERR_SRVCL;
461 }
462 }
463
William Lallemandb7ff6a32012-03-02 14:35:21 +0100464 /* needs src ip/port for logging */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200465 if (si->flags & SI_FL_SRC_ADDR)
Willy Tarreau59b94792012-05-11 16:16:40 +0200466 si_get_from_addr(si);
William Lallemandb7ff6a32012-03-02 14:35:21 +0100467
Willy Tarreau9650f372009-08-16 14:02:45 +0200468 fdtab[fd].owner = si;
469 fdtab[fd].state = FD_STCONN; /* connection in progress */
470 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau9650f372009-08-16 14:02:45 +0200471
Willy Tarreaube0688c2012-05-18 15:15:26 +0200472 /* If we have nothing to send, we want to confirm that the TCP
473 * connection is established before doing so, so we use our own write
474 * callback then switch to the sock layer.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200475 */
Willy Tarreaua190d592012-05-20 18:35:19 +0200476 if ((si->ob->flags & BF_OUT_EMPTY) || si->send_proxy_ofs) {
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200477 fdtab[fd].cb[DIR_RD].f = tcp_connect_read;
478 fdtab[fd].cb[DIR_WR].f = tcp_connect_write;
479 }
480 else {
Willy Tarreau73b013b2012-05-21 16:31:45 +0200481 fdtab[fd].cb[DIR_RD].f = si_data(si)->read;
482 fdtab[fd].cb[DIR_WR].f = si_data(si)->write;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200483 }
484
Willy Tarreau9650f372009-08-16 14:02:45 +0200485 fd_insert(fd);
486 EV_FD_SET(fd, DIR_WR); /* for connect status */
487
488 si->state = SI_ST_CON;
489 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
490 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
491
492 return SN_ERR_NONE; /* connection is OK */
493}
494
495
Willy Tarreau59b94792012-05-11 16:16:40 +0200496/*
497 * Retrieves the source address for the socket <fd>, with <dir> indicating
498 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
499 * success, -1 in case of error. The socket's source address is stored in
500 * <sa> for <salen> bytes.
501 */
502int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
503{
504 if (dir)
505 return getsockname(fd, sa, &salen);
506 else
507 return getpeername(fd, sa, &salen);
508}
509
510
511/*
512 * Retrieves the original destination address for the socket <fd>, with <dir>
513 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
514 * listener, if the original destination address was translated, the original
515 * address is retrieved. It returns 0 in case of success, -1 in case of error.
516 * The socket's source address is stored in <sa> for <salen> bytes.
517 */
518int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
519{
520 if (dir)
521 return getpeername(fd, sa, &salen);
522#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
523 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
524 return 0;
525#endif
526 else
527 return getsockname(fd, sa, &salen);
528}
529
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200530/* This is the callback which is set when a connection establishment is pending
531 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreaua190d592012-05-20 18:35:19 +0200532 * once the connection is established. It returns zero if it needs some polling
533 * before being called again.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200534 */
535static int tcp_connect_write(int fd)
536{
537 struct stream_interface *si = fdtab[fd].owner;
538 struct buffer *b = si->ob;
Willy Tarreaua190d592012-05-20 18:35:19 +0200539 int retval = 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200540
541 if (fdtab[fd].state == FD_STERROR)
542 goto out_error;
543
Willy Tarreaua190d592012-05-20 18:35:19 +0200544 if (fdtab[fd].state != FD_STCONN)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200545 goto out_ignore; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200546
547 /* we might have been called just after an asynchronous shutw */
548 if (b->flags & BF_SHUTW)
549 goto out_wakeup;
550
Willy Tarreaua190d592012-05-20 18:35:19 +0200551 /* If we have a PROXY line to send, we'll use this to validate the
552 * connection, in which case the connection is validated only once
553 * we've sent the whole proxy line. Otherwise we use connect().
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200554 */
Willy Tarreaua190d592012-05-20 18:35:19 +0200555 if (si->send_proxy_ofs) {
556 int ret;
557
558 /* The target server expects a PROXY line to be sent first.
559 * If the send_proxy_ofs is negative, it corresponds to the
560 * offset to start sending from then end of the proxy string
561 * (which is recomputed every time since it's constant). If
562 * it is positive, it means we have to send from the start.
563 */
564 ret = make_proxy_line(trash, trashlen, &b->prod->addr.from, &b->prod->addr.to);
565 if (!ret)
566 goto out_error;
567
568 if (si->send_proxy_ofs > 0)
569 si->send_proxy_ofs = -ret; /* first call */
570
571 /* we have to send trash from (ret+sp for -sp bytes) */
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200572 ret = send(fd, trash + ret + si->send_proxy_ofs, -si->send_proxy_ofs,
Willy Tarreaua190d592012-05-20 18:35:19 +0200573 (b->flags & BF_OUT_EMPTY) ? 0 : MSG_MORE);
574
575 if (ret == 0)
576 goto out_ignore;
577
578 if (ret < 0) {
579 if (errno == EAGAIN)
580 goto out_ignore;
581 goto out_error;
582 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200583
Willy Tarreaua190d592012-05-20 18:35:19 +0200584 si->send_proxy_ofs += ret; /* becomes zero once complete */
585 if (si->send_proxy_ofs != 0)
586 goto out_ignore;
587
588 /* OK we've sent the whole line, we're connected */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200589 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200590 else {
591 /* We have no data to send to check the connection, and
592 * getsockopt() will not inform us whether the connection
593 * is still pending. So we'll reuse connect() to check the
594 * state of the socket. This has the advantage of giving us
595 * the following info :
596 * - error
597 * - connecting (EALREADY, EINPROGRESS)
598 * - connected (EISCONN, 0)
599 */
Willy Tarreau96596ae2012-06-08 22:57:36 +0200600 if ((connect(fd, si->conn.peeraddr, si->conn.peerlen) < 0)) {
Willy Tarreaua190d592012-05-20 18:35:19 +0200601 if (errno == EALREADY || errno == EINPROGRESS)
602 goto out_ignore;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200603
Willy Tarreaua190d592012-05-20 18:35:19 +0200604 if (errno && errno != EISCONN)
605 goto out_error;
606
607 /* otherwise we're connected */
608 }
609 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200610
611 /* OK we just need to indicate that we got a connection
612 * and that we wrote nothing.
613 */
614 b->flags |= BF_WRITE_NULL;
615
Willy Tarreaua190d592012-05-20 18:35:19 +0200616 /* The FD is ready now, we can hand the handlers to the socket layer
617 * and forward the event there to start working on the socket.
618 */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200619 fdtab[fd].cb[DIR_RD].f = si_data(si)->read;
620 fdtab[fd].cb[DIR_WR].f = si_data(si)->write;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200621 fdtab[fd].state = FD_STREADY;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200622 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200623 return si_data(si)->write(fd);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200624
625 out_wakeup:
626 task_wakeup(si->owner, TASK_WOKEN_IO);
627
628 out_ignore:
629 fdtab[fd].ev &= ~FD_POLL_OUT;
630 return retval;
631
632 out_error:
633 /* Write error on the file descriptor. We mark the FD as STERROR so
634 * that we don't use it anymore. The error is reported to the stream
635 * interface which will take proper action. We must not perturbate the
636 * buffer because the stream interface wants to ensure transparent
637 * connection retries.
638 */
639
640 fdtab[fd].state = FD_STERROR;
641 fdtab[fd].ev &= ~FD_POLL_STICKY;
642 EV_FD_REM(fd);
643 si->flags |= SI_FL_ERR;
Willy Tarreaua190d592012-05-20 18:35:19 +0200644 retval = 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200645 goto out_wakeup;
646}
647
648
649/* might be used on connect error */
650static int tcp_connect_read(int fd)
651{
652 struct stream_interface *si = fdtab[fd].owner;
653 int retval;
654
655 retval = 1;
656
657 if (fdtab[fd].state == FD_STERROR)
658 goto out_error;
659
660 if (fdtab[fd].state != FD_STCONN) {
661 retval = 0;
662 goto out_ignore; /* strange we were called while ready */
663 }
664
665 /* stop here if we reached the end of data */
666 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
667 goto out_error;
668
669 out_wakeup:
670 task_wakeup(si->owner, TASK_WOKEN_IO);
671 out_ignore:
672 fdtab[fd].ev &= ~FD_POLL_IN;
673 return retval;
674
675 out_error:
676 /* Read error on the file descriptor. We mark the FD as STERROR so
677 * that we don't use it anymore. The error is reported to the stream
678 * interface which will take proper action. We must not perturbate the
679 * buffer because the stream interface wants to ensure transparent
680 * connection retries.
681 */
682
683 fdtab[fd].state = FD_STERROR;
684 fdtab[fd].ev &= ~FD_POLL_STICKY;
685 EV_FD_REM(fd);
686 si->flags |= SI_FL_ERR;
687 goto out_wakeup;
688}
689
Willy Tarreau59b94792012-05-11 16:16:40 +0200690
Willy Tarreaue6b98942007-10-29 01:09:36 +0100691/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
692 * an error message in <err> if the message is at most <errlen> bytes long
693 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
694 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
695 * was alright and that no message was returned. ERR_RETRYABLE means that an
696 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700697 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100698 * the meaning of the error, but just indicate that a message is present which
699 * should be displayed with the respective level. Last, ERR_ABORT indicates
700 * that it's pointless to try to start other listeners. No error message is
701 * returned if errlen is NULL.
702 */
703int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
704{
705 __label__ tcp_return, tcp_close_return;
706 int fd, err;
707 const char *msg = NULL;
708
709 /* ensure we never return garbage */
710 if (errmsg && errlen)
711 *errmsg = 0;
712
713 if (listener->state != LI_ASSIGNED)
714 return ERR_NONE; /* already bound */
715
716 err = ERR_NONE;
717
718 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
719 err |= ERR_RETRYABLE | ERR_ALERT;
720 msg = "cannot create listening socket";
721 goto tcp_return;
722 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100723
Willy Tarreaue6b98942007-10-29 01:09:36 +0100724 if (fd >= global.maxsock) {
725 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
726 msg = "not enough free sockets (raise '-n' parameter)";
727 goto tcp_close_return;
728 }
729
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200730 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100731 err |= ERR_FATAL | ERR_ALERT;
732 msg = "cannot make socket non-blocking";
733 goto tcp_close_return;
734 }
735
Simon Hormande072bd2011-06-24 15:11:37 +0900736 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100737 /* not fatal but should be reported */
738 msg = "cannot do so_reuseaddr";
739 err |= ERR_ALERT;
740 }
741
742 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900743 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100744
Willy Tarreaue6b98942007-10-29 01:09:36 +0100745#ifdef SO_REUSEPORT
746 /* OpenBSD supports this. As it's present in old libc versions of Linux,
747 * it might return an error that we will silently ignore.
748 */
Simon Hormande072bd2011-06-24 15:11:37 +0900749 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100750#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100751#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200752 if (listener->options & LI_O_FOREIGN) {
753 switch (listener->addr.ss_family) {
754 case AF_INET:
755 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
756 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
757 msg = "cannot make listening socket transparent";
758 err |= ERR_ALERT;
759 }
760 break;
761 case AF_INET6:
762 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
763 msg = "cannot make listening socket transparent";
764 err |= ERR_ALERT;
765 }
766 break;
767 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100768 }
769#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100770#ifdef SO_BINDTODEVICE
771 /* Note: this might fail if not CAP_NET_RAW */
772 if (listener->interface) {
773 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100774 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100775 msg = "cannot bind listener to device";
776 err |= ERR_WARN;
777 }
778 }
779#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400780#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100781 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400782 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200783 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
784 msg = "cannot set MSS";
785 err |= ERR_WARN;
786 }
787 }
788#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200789#if defined(TCP_DEFER_ACCEPT)
790 if (listener->options & LI_O_DEF_ACCEPT) {
791 /* defer accept by up to one second */
792 int accept_delay = 1;
793 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
794 msg = "cannot enable DEFER_ACCEPT";
795 err |= ERR_WARN;
796 }
797 }
798#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100799 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
800 err |= ERR_RETRYABLE | ERR_ALERT;
801 msg = "cannot bind socket";
802 goto tcp_close_return;
803 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100804
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100805 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100806 err |= ERR_RETRYABLE | ERR_ALERT;
807 msg = "cannot listen to socket";
808 goto tcp_close_return;
809 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100810
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400811#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200812 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900813 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200814#endif
815
Willy Tarreaue6b98942007-10-29 01:09:36 +0100816 /* the socket is ready */
817 listener->fd = fd;
818 listener->state = LI_LISTEN;
819
Willy Tarreaueabf3132008-08-29 23:36:51 +0200820 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100821 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaueb472682010-05-28 18:46:57 +0200822 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
823 fdtab[fd].cb[DIR_RD].f = listener->proto->accept;
824 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
Willy Tarreaueb472682010-05-28 18:46:57 +0200825 fd_insert(fd);
826
Willy Tarreaue6b98942007-10-29 01:09:36 +0100827 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100828 if (msg && errlen) {
829 char pn[INET6_ADDRSTRLEN];
830
Willy Tarreau631f01c2011-09-05 00:36:48 +0200831 addr_to_str(&listener->addr, pn, sizeof(pn));
832 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100833 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100834 return err;
835
836 tcp_close_return:
837 close(fd);
838 goto tcp_return;
839}
840
841/* This function creates all TCP sockets bound to the protocol entry <proto>.
842 * It is intended to be used as the protocol's bind_all() function.
843 * The sockets will be registered but not added to any fd_set, in order not to
844 * loose them across the fork(). A call to enable_all_listeners() is needed
845 * to complete initialization. The return value is composed from ERR_*.
846 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200847static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100848{
849 struct listener *listener;
850 int err = ERR_NONE;
851
852 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200853 err |= tcp_bind_listener(listener, errmsg, errlen);
854 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100855 break;
856 }
857
858 return err;
859}
860
861/* Add listener to the list of tcpv4 listeners. The listener's state
862 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
863 * listeners is updated. This is the function to use to add a new listener.
864 */
865void tcpv4_add_listener(struct listener *listener)
866{
867 if (listener->state != LI_INIT)
868 return;
869 listener->state = LI_ASSIGNED;
870 listener->proto = &proto_tcpv4;
871 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
872 proto_tcpv4.nb_listeners++;
873}
874
875/* Add listener to the list of tcpv4 listeners. The listener's state
876 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
877 * listeners is updated. This is the function to use to add a new listener.
878 */
879void tcpv6_add_listener(struct listener *listener)
880{
881 if (listener->state != LI_INIT)
882 return;
883 listener->state = LI_ASSIGNED;
884 listener->proto = &proto_tcpv6;
885 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
886 proto_tcpv6.nb_listeners++;
887}
888
Willy Tarreauedcf6682008-11-30 23:15:34 +0100889/* This function performs the TCP request analysis on the current request. It
890 * returns 1 if the processing can continue on next analysers, or zero if it
891 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200892 * request. It relies on buffers flags, and updates s->req->analysers. The
893 * function may be called for frontend rules and backend rules. It only relies
894 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100895 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200896int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100897{
898 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200899 struct stksess *ts;
900 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100901 int partial;
902
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100903 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 +0100904 now_ms, __FUNCTION__,
905 s,
906 req,
907 req->rex, req->wex,
908 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100909 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100910 req->analysers);
911
Willy Tarreauedcf6682008-11-30 23:15:34 +0100912 /* We don't know whether we have enough data, so must proceed
913 * this way :
914 * - iterate through all rules in their declaration order
915 * - if one rule returns MISS, it means the inspect delay is
916 * not over yet, then return immediately, otherwise consider
917 * it as a non-match.
918 * - if one rule returns OK, then return OK
919 * - if one rule returns KO, then return KO
920 */
921
Willy Tarreaub824b002010-09-29 16:36:16 +0200922 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 +0200923 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100924 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200925 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100926
Willy Tarreaufb356202010-08-03 14:02:05 +0200927 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100928 int ret = ACL_PAT_PASS;
929
930 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200931 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100932 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200933 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100934 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200935 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
936 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100937 return 0;
938 }
939
940 ret = acl_pass(ret);
941 if (rule->cond->pol == ACL_COND_UNLESS)
942 ret = !ret;
943 }
944
945 if (ret) {
946 /* we have a matching rule. */
947 if (rule->action == TCP_ACT_REJECT) {
948 buffer_abort(req);
949 buffer_abort(s->rep);
950 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200951
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100952 s->be->be_counters.denied_req++;
953 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200954 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200955 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200956
Willy Tarreauedcf6682008-11-30 23:15:34 +0100957 if (!(s->flags & SN_ERR_MASK))
958 s->flags |= SN_ERR_PRXCOND;
959 if (!(s->flags & SN_FINST_MASK))
960 s->flags |= SN_FINST_R;
961 return 0;
962 }
Willy Tarreau56123282010-08-06 19:06:56 +0200963 else if (rule->action == TCP_ACT_TRK_SC1) {
964 if (!s->stkctr1_entry) {
965 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200966 * Also, note that right now we can only track SRC so we
967 * don't check how to get the key, but later we may need
968 * to consider rule->act_prm->trk_ctr.type.
969 */
970 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100971 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200972 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200973 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200974 if (s->fe != s->be)
975 s->flags |= SN_BE_TRACK_SC1;
976 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200977 }
978 }
Willy Tarreau56123282010-08-06 19:06:56 +0200979 else if (rule->action == TCP_ACT_TRK_SC2) {
980 if (!s->stkctr2_entry) {
981 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200982 * Also, note that right now we can only track SRC so we
983 * don't check how to get the key, but later we may need
984 * to consider rule->act_prm->trk_ctr.type.
985 */
986 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100987 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200988 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200989 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200990 if (s->fe != s->be)
991 s->flags |= SN_BE_TRACK_SC2;
992 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200993 }
994 }
995 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100996 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200997 break;
998 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100999 }
1000 }
1001
1002 /* if we get there, it means we have no rule which matches, or
1003 * we have an explicit accept, so we apply the default accept.
1004 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001005 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001006 req->analyse_exp = TICK_ETERNITY;
1007 return 1;
1008}
1009
Emeric Brun97679e72010-09-23 17:56:44 +02001010/* This function performs the TCP response analysis on the current response. It
1011 * returns 1 if the processing can continue on next analysers, or zero if it
1012 * needs more data, encounters an error, or wants to immediately abort the
1013 * response. It relies on buffers flags, and updates s->rep->analysers. The
1014 * function may be called for backend rules.
1015 */
1016int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit)
1017{
1018 struct tcp_rule *rule;
1019 int partial;
1020
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001021 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 +02001022 now_ms, __FUNCTION__,
1023 s,
1024 rep,
1025 rep->rex, rep->wex,
1026 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001027 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001028 rep->analysers);
1029
1030 /* We don't know whether we have enough data, so must proceed
1031 * this way :
1032 * - iterate through all rules in their declaration order
1033 * - if one rule returns MISS, it means the inspect delay is
1034 * not over yet, then return immediately, otherwise consider
1035 * it as a non-match.
1036 * - if one rule returns OK, then return OK
1037 * - if one rule returns KO, then return KO
1038 */
1039
1040 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001041 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001042 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001043 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001044
1045 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
1046 int ret = ACL_PAT_PASS;
1047
1048 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001049 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +02001050 if (ret == ACL_PAT_MISS) {
1051 /* just set the analyser timeout once at the beginning of the response */
1052 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
1053 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
1054 return 0;
1055 }
1056
1057 ret = acl_pass(ret);
1058 if (rule->cond->pol == ACL_COND_UNLESS)
1059 ret = !ret;
1060 }
1061
1062 if (ret) {
1063 /* we have a matching rule. */
1064 if (rule->action == TCP_ACT_REJECT) {
1065 buffer_abort(rep);
1066 buffer_abort(s->req);
1067 rep->analysers = 0;
1068
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001069 s->be->be_counters.denied_resp++;
1070 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001071 if (s->listener->counters)
1072 s->listener->counters->denied_resp++;
1073
1074 if (!(s->flags & SN_ERR_MASK))
1075 s->flags |= SN_ERR_PRXCOND;
1076 if (!(s->flags & SN_FINST_MASK))
1077 s->flags |= SN_FINST_D;
1078 return 0;
1079 }
1080 else {
1081 /* otherwise accept */
1082 break;
1083 }
1084 }
1085 }
1086
1087 /* if we get there, it means we have no rule which matches, or
1088 * we have an explicit accept, so we apply the default accept.
1089 */
1090 rep->analysers &= ~an_bit;
1091 rep->analyse_exp = TICK_ETERNITY;
1092 return 1;
1093}
1094
1095
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001096/* This function performs the TCP layer4 analysis on the current request. It
1097 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1098 * matches or if no more rule matches. It can only use rules which don't need
1099 * any data.
1100 */
1101int tcp_exec_req_rules(struct session *s)
1102{
1103 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001104 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001105 struct stktable *t = NULL;
1106 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001107 int ret;
1108
1109 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1110 ret = ACL_PAT_PASS;
1111
1112 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001113 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001114 ret = acl_pass(ret);
1115 if (rule->cond->pol == ACL_COND_UNLESS)
1116 ret = !ret;
1117 }
1118
1119 if (ret) {
1120 /* we have a matching rule. */
1121 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001122 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001123 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001124 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001125
1126 if (!(s->flags & SN_ERR_MASK))
1127 s->flags |= SN_ERR_PRXCOND;
1128 if (!(s->flags & SN_FINST_MASK))
1129 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001130 result = 0;
1131 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001132 }
Willy Tarreau56123282010-08-06 19:06:56 +02001133 else if (rule->action == TCP_ACT_TRK_SC1) {
1134 if (!s->stkctr1_entry) {
1135 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001136 * Also, note that right now we can only track SRC so we
1137 * don't check how to get the key, but later we may need
1138 * to consider rule->act_prm->trk_ctr.type.
1139 */
1140 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001141 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001142 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001143 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001144 }
1145 }
Willy Tarreau56123282010-08-06 19:06:56 +02001146 else if (rule->action == TCP_ACT_TRK_SC2) {
1147 if (!s->stkctr2_entry) {
1148 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001149 * Also, note that right now we can only track SRC so we
1150 * don't check how to get the key, but later we may need
1151 * to consider rule->act_prm->trk_ctr.type.
1152 */
1153 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001154 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001155 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001156 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001157 }
1158 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001159 else {
1160 /* otherwise it's an accept */
1161 break;
1162 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001163 }
1164 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001165 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001166}
1167
Emeric Brun97679e72010-09-23 17:56:44 +02001168/* Parse a tcp-response rule. Return a negative value in case of failure */
1169static int tcp_parse_response_rule(char **args, int arg, int section_type,
1170 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001171 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001172{
1173 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001174 memprintf(err, "%s %s is only allowed in 'backend' sections",
1175 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001176 return -1;
1177 }
1178
1179 if (strcmp(args[arg], "accept") == 0) {
1180 arg++;
1181 rule->action = TCP_ACT_ACCEPT;
1182 }
1183 else if (strcmp(args[arg], "reject") == 0) {
1184 arg++;
1185 rule->action = TCP_ACT_REJECT;
1186 }
1187 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001188 memprintf(err,
1189 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1190 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001191 return -1;
1192 }
1193
1194 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001195 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1196 memprintf(err,
1197 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1198 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001199 return -1;
1200 }
1201 }
1202 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001203 memprintf(err,
1204 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001205 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1206 return -1;
1207 }
1208 return 0;
1209}
1210
1211
1212
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001213/* Parse a tcp-request rule. Return a negative value in case of failure */
1214static int tcp_parse_request_rule(char **args, int arg, int section_type,
1215 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001216 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001217{
1218 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001219 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1220 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001221 return -1;
1222 }
1223
1224 if (!strcmp(args[arg], "accept")) {
1225 arg++;
1226 rule->action = TCP_ACT_ACCEPT;
1227 }
1228 else if (!strcmp(args[arg], "reject")) {
1229 arg++;
1230 rule->action = TCP_ACT_REJECT;
1231 }
Willy Tarreau56123282010-08-06 19:06:56 +02001232 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001233 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001234 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001235
1236 arg++;
1237 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001238 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001239
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001240 if (ret < 0) { /* nb: warnings are not handled yet */
1241 memprintf(err,
1242 "'%s %s %s' : %s in %s '%s'",
1243 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1244 return ret;
1245 }
Willy Tarreau56123282010-08-06 19:06:56 +02001246 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001247 }
Willy Tarreau56123282010-08-06 19:06:56 +02001248 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001249 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001250 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001251
1252 arg++;
1253 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001254 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001255
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001256 if (ret < 0) { /* nb: warnings are not handled yet */
1257 memprintf(err,
1258 "'%s %s %s' : %s in %s '%s'",
1259 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1260 return ret;
1261 }
Willy Tarreau56123282010-08-06 19:06:56 +02001262 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001263 }
1264 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001265 memprintf(err,
1266 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1267 "or 'track-sc2' in %s '%s' (got '%s')",
1268 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001269 return -1;
1270 }
1271
1272 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001273 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1274 memprintf(err,
1275 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1276 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001277 return -1;
1278 }
1279 }
1280 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001281 memprintf(err,
1282 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001283 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1284 return -1;
1285 }
1286 return 0;
1287}
1288
Emeric Brun97679e72010-09-23 17:56:44 +02001289/* This function should be called to parse a line starting with the "tcp-response"
1290 * keyword.
1291 */
1292static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001293 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001294{
1295 const char *ptr = NULL;
1296 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001297 int warn = 0;
1298 int arg;
1299 struct tcp_rule *rule;
1300
1301 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001302 memprintf(err, "missing argument for '%s' in %s '%s'",
1303 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001304 return -1;
1305 }
1306
1307 if (strcmp(args[1], "inspect-delay") == 0) {
1308 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001309 memprintf(err, "%s %s is only allowed in 'backend' sections",
1310 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001311 return -1;
1312 }
1313
1314 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001315 memprintf(err,
1316 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1317 args[0], args[1], proxy_type_str(curpx), curpx->id);
1318 if (ptr)
1319 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001320 return -1;
1321 }
1322
1323 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001324 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1325 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001326 return 1;
1327 }
1328 curpx->tcp_rep.inspect_delay = val;
1329 return 0;
1330 }
1331
Simon Hormande072bd2011-06-24 15:11:37 +09001332 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001333 LIST_INIT(&rule->list);
1334 arg = 1;
1335
1336 if (strcmp(args[1], "content") == 0) {
1337 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001338 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001339 goto error;
1340
1341 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1342 struct acl *acl;
1343 const char *name;
1344
1345 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1346 name = acl ? acl->name : "(unknown)";
1347
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001348 memprintf(err,
1349 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1350 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001351 warn++;
1352 }
1353
1354 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1355 }
1356 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001357 memprintf(err,
1358 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1359 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001360 goto error;
1361 }
1362
1363 return warn;
1364 error:
1365 free(rule);
1366 return -1;
1367}
1368
1369
Willy Tarreaub6866442008-07-14 23:54:42 +02001370/* This function should be called to parse a line starting with the "tcp-request"
1371 * keyword.
1372 */
1373static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001374 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001375{
1376 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001377 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001378 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001379 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001380 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001381
1382 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001383 if (curpx == defpx)
1384 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1385 else
1386 memprintf(err, "missing argument for '%s' in %s '%s'",
1387 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001388 return -1;
1389 }
1390
1391 if (!strcmp(args[1], "inspect-delay")) {
1392 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001393 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1394 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001395 return -1;
1396 }
1397
Willy Tarreaub6866442008-07-14 23:54:42 +02001398 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001399 memprintf(err,
1400 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1401 args[0], args[1], proxy_type_str(curpx), curpx->id);
1402 if (ptr)
1403 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001404 return -1;
1405 }
1406
1407 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001408 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1409 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001410 return 1;
1411 }
1412 curpx->tcp_req.inspect_delay = val;
1413 return 0;
1414 }
1415
Simon Hormande072bd2011-06-24 15:11:37 +09001416 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001417 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001418 arg = 1;
1419
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001420 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001421 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001422 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001423 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001424
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001425 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001426 struct acl *acl;
1427 const char *name;
1428
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001429 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001430 name = acl ? acl->name : "(unknown)";
1431
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001432 memprintf(err,
1433 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1434 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001435 warn++;
1436 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001437 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001438 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001439 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001440 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001441
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001442 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001443 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1444 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001445 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001446 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001447
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001448 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001449 goto error;
1450
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001451 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1452 struct acl *acl;
1453 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001454
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001455 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1456 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001457
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001458 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001459 memprintf(err,
1460 "'%s %s' may not reference acl '%s' which makes use of "
1461 "payload in %s '%s'. Please use '%s content' for this.",
1462 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001463 goto error;
1464 }
1465 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001466 memprintf(err,
1467 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1468 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001469 warn++;
1470 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001471 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001472 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001473 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001474 if (curpx == defpx)
1475 memprintf(err,
1476 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1477 args[0], args[1]);
1478 else
1479 memprintf(err,
1480 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1481 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001482 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001483 }
1484
Willy Tarreau1a687942010-05-23 22:40:30 +02001485 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001486 error:
1487 free(rule);
1488 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001489}
1490
Willy Tarreau645513a2010-05-24 20:55:15 +02001491
1492/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001493/* All supported sample fetch functios must be declared here */
1494/************************************************************************/
1495
1496/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001497 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001498 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1499 * is a string (cookie name), other types will lead to undefined behaviour.
1500 */
1501int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001502smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001503 const struct arg *args, struct sample *smp)
1504{
1505 int bleft;
1506 const unsigned char *data;
1507
1508 if (!l4 || !l4->req)
1509 return 0;
1510
1511 smp->flags = 0;
1512 smp->type = SMP_T_CSTR;
1513
1514 bleft = l4->req->i;
1515 if (bleft <= 11)
1516 goto too_short;
1517
1518 data = (const unsigned char *)l4->req->p + 11;
1519 bleft -= 11;
1520
1521 if (bleft <= 7)
1522 goto too_short;
1523
1524 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1525 goto not_cookie;
1526
1527 data += 7;
1528 bleft -= 7;
1529
1530 while (bleft > 0 && *data == ' ') {
1531 data++;
1532 bleft--;
1533 }
1534
1535 if (args) {
1536
1537 if (bleft <= args->data.str.len)
1538 goto too_short;
1539
1540 if ((data[args->data.str.len] != '=') ||
1541 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1542 goto not_cookie;
1543
1544 data += args->data.str.len + 1;
1545 bleft -= args->data.str.len + 1;
1546 } else {
1547 while (bleft > 0 && *data != '=') {
1548 if (*data == '\r' || *data == '\n')
1549 goto not_cookie;
1550 data++;
1551 bleft--;
1552 }
1553
1554 if (bleft < 1)
1555 goto too_short;
1556
1557 if (*data != '=')
1558 goto not_cookie;
1559
1560 data++;
1561 bleft--;
1562 }
1563
1564 /* data points to cookie value */
1565 smp->data.str.str = (char *)data;
1566 smp->data.str.len = 0;
1567
1568 while (bleft > 0 && *data != '\r') {
1569 data++;
1570 bleft--;
1571 }
1572
1573 if (bleft < 2)
1574 goto too_short;
1575
1576 if (data[0] != '\r' || data[1] != '\n')
1577 goto not_cookie;
1578
1579 smp->data.str.len = (char *)data - smp->data.str.str;
1580 smp->flags = SMP_F_VOLATILE;
1581 return 1;
1582
1583 too_short:
1584 smp->flags = SMP_F_MAY_CHANGE;
1585 not_cookie:
1586 return 0;
1587}
1588
Willy Tarreau32389b72012-04-23 23:13:20 +02001589/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001590/* All supported ACL keywords must be declared here. */
1591/************************************************************************/
1592
Willy Tarreau32389b72012-04-23 23:13:20 +02001593/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1594static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001595acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001596 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001597{
1598 int ret;
1599
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001600 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001601
1602 if (smp->flags & SMP_F_MAY_CHANGE)
1603 return 0;
1604
1605 smp->flags = SMP_F_VOLATILE;
1606 smp->type = SMP_T_UINT;
1607 smp->data.uint = ret;
1608 return 1;
1609}
1610
1611
Willy Tarreau4a129812012-04-25 17:31:42 +02001612/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001613static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001614smp_fetch_src(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 Tarreauf4362b32011-12-16 17:49:52 +01001617 switch (l4->si[0].addr.from.ss_family) {
1618 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001619 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.from)->sin_addr;
1620 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001621 break;
1622 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001623 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.from))->sin6_addr;
1624 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001625 break;
1626 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001627 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001628 }
Emeric Brunf769f512010-10-22 17:14:01 +02001629
Willy Tarreau37406352012-04-23 16:16:37 +02001630 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001631 return 1;
1632}
1633
Willy Tarreaua5e37562011-12-16 17:06:15 +01001634/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001635static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001636smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001637 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001638{
Willy Tarreauf853c462012-04-23 18:53:56 +02001639 smp->type = SMP_T_UINT;
1640 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001641 return 0;
1642
Willy Tarreau37406352012-04-23 16:16:37 +02001643 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001644 return 1;
1645}
1646
Willy Tarreau4a129812012-04-25 17:31:42 +02001647/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001648static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001649smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001650 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001651{
Willy Tarreau59b94792012-05-11 16:16:40 +02001652 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001653
Willy Tarreauf4362b32011-12-16 17:49:52 +01001654 switch (l4->si[0].addr.to.ss_family) {
1655 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001656 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.to)->sin_addr;
1657 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001658 break;
1659 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001660 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.to))->sin6_addr;
1661 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001662 break;
1663 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001664 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001665 }
Emeric Brunf769f512010-10-22 17:14:01 +02001666
Willy Tarreau37406352012-04-23 16:16:37 +02001667 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001668 return 1;
1669}
1670
Willy Tarreaua5e37562011-12-16 17:06:15 +01001671/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001672static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001673smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001674 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001675{
Willy Tarreau59b94792012-05-11 16:16:40 +02001676 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001677
Willy Tarreauf853c462012-04-23 18:53:56 +02001678 smp->type = SMP_T_UINT;
1679 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001680 return 0;
1681
Willy Tarreau37406352012-04-23 16:16:37 +02001682 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001683 return 1;
1684}
1685
1686static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001687smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1688 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001689{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001690 unsigned int len_offset = arg_p[0].data.uint;
1691 unsigned int len_size = arg_p[1].data.uint;
1692 unsigned int buf_offset;
1693 unsigned int buf_size = 0;
Emericf2d7cae2010-11-05 18:13:50 +01001694 struct buffer *b;
1695 int i;
1696
1697 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1698 /* by default buf offset == len offset + len size */
1699 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1700
1701 if (!l4)
1702 return 0;
1703
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001704 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001705
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001706 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001707 return 0;
1708
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001709 if (len_offset + len_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001710 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001711
1712 for (i = 0; i < len_size; i++) {
Willy Tarreau89fa7062012-03-02 16:13:16 +01001713 buf_size = (buf_size << 8) + ((unsigned char *)b->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001714 }
1715
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001716 /* buf offset may be implicit, absolute or relative */
1717 buf_offset = len_offset + len_size;
1718 if (arg_p[2].type == ARGT_UINT)
1719 buf_offset = arg_p[2].data.uint;
1720 else if (arg_p[2].type == ARGT_SINT)
1721 buf_offset += arg_p[2].data.sint;
1722
Willy Tarreau82ea8002012-04-25 19:19:43 +02001723 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1724 /* will never match */
1725 smp->flags = 0;
1726 return 0;
1727 }
1728
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001729 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001730 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001731
1732 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001733 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001734 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001735 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001736 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001737
1738 too_short:
1739 smp->flags = SMP_F_MAY_CHANGE;
1740 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001741}
1742
1743static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001744smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1745 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001746{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001747 unsigned int buf_offset = arg_p[0].data.uint;
1748 unsigned int buf_size = arg_p[1].data.uint;
Emericf2d7cae2010-11-05 18:13:50 +01001749 struct buffer *b;
1750
1751 if (!l4)
1752 return 0;
1753
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001754 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001755
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001756 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001757 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001758
1759 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1760 /* will never match */
1761 smp->flags = 0;
1762 return 0;
1763 }
Emericf2d7cae2010-11-05 18:13:50 +01001764
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001765 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001766 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001767
1768 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001769 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001770 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001771 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001772 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001773
1774 too_short:
1775 smp->flags = SMP_F_MAY_CHANGE;
1776 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001777}
1778
Willy Tarreau21d68a62012-04-20 15:52:36 +02001779/* This function is used to validate the arguments passed to a "payload" fetch
1780 * keyword. This keyword expects two positive integers, with the second one
1781 * being strictly positive. It is assumed that the types are already the correct
1782 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1783 * filled with a pointer to an error message in case of error, that the caller
1784 * is responsible for freeing. The initial location must either be freeable or
1785 * NULL.
1786 */
1787static int val_payload(struct arg *arg, char **err_msg)
1788{
1789 if (!arg[1].data.uint) {
1790 if (err_msg)
1791 memprintf(err_msg, "payload length must be > 0");
1792 return 0;
1793 }
1794 return 1;
1795}
1796
1797/* This function is used to validate the arguments passed to a "payload_lv" fetch
1798 * keyword. This keyword allows two positive integers and an optional signed one,
1799 * with the second one being strictly positive and the third one being greater than
1800 * the opposite of the two others if negative. It is assumed that the types are
1801 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1802 * not NULL, it will be filled with a pointer to an error message in case of
1803 * error, that the caller is responsible for freeing. The initial location must
1804 * either be freeable or NULL.
1805 */
1806static int val_payload_lv(struct arg *arg, char **err_msg)
1807{
1808 if (!arg[1].data.uint) {
1809 if (err_msg)
1810 memprintf(err_msg, "payload length must be > 0");
1811 return 0;
1812 }
1813
1814 if (arg[2].type == ARGT_SINT &&
1815 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1816 if (err_msg)
1817 memprintf(err_msg, "payload offset too negative");
1818 return 0;
1819 }
1820 return 1;
1821}
1822
Willy Tarreaub6866442008-07-14 23:54:42 +02001823static struct cfg_kw_list cfg_kws = {{ },{
1824 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001825 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001826 { 0, NULL, NULL },
1827}};
1828
Willy Tarreau61612d42012-04-19 18:42:05 +02001829/* Note: must not be declared <const> as its list will be overwritten.
1830 * Please take care of keeping this list alphabetically sorted.
1831 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001832static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001833 { "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 +02001834 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001835 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1836 { "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 +02001837 { "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 +02001838 { "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 +02001839 { "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 +02001840 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001841 { NULL, NULL, NULL, NULL },
1842}};
1843
Willy Tarreau4a129812012-04-25 17:31:42 +02001844/* Note: must not be declared <const> as its list will be overwritten.
1845 * Note: fetches that may return multiple types must be declared as the lowest
1846 * common denominator, the type that can be casted into all other ones. For
1847 * instance v4/v6 must be declared v4.
1848 */
Willy Tarreau12785782012-04-27 21:37:17 +02001849static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001850 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001851 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001852 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001853 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1854 { "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 +02001855 { "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 +02001856 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001857 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001858}};
1859
Willy Tarreaue6b98942007-10-29 01:09:36 +01001860__attribute__((constructor))
1861static void __tcp_protocol_init(void)
1862{
1863 protocol_register(&proto_tcpv4);
1864 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001865 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001866 cfg_register_keywords(&cfg_kws);
1867 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001868}
1869
1870
1871/*
1872 * Local variables:
1873 * c-indent-level: 8
1874 * c-basic-offset: 8
1875 * End:
1876 */