blob: 86ef47b7cfd4479d082859bff3e8c776aab1d6de [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 Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020044//#include <proto/frontend.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020045#include <proto/log.h>
46#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010047#include <proto/protocols.h>
48#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020049#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020050#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020051#include <proto/session.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020052#include <proto/sock_raw.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/stick_table.h>
Willy Tarreau59b94792012-05-11 16:16:40 +020054#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020055#include <proto/task.h>
Emeric Brun97679e72010-09-23 17:56:44 +020056#include <proto/buffers.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010057
Willy Tarreaue8c66af2008-01-13 18:40:14 +010058#ifdef CONFIG_HAP_CTTPROXY
59#include <import/ip_tproxy.h>
60#endif
61
Emeric Bruncf20bf12010-10-22 16:06:11 +020062static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
63static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaueeda90e2012-05-11 19:53:32 +020064static int tcp_connect_write(int fd);
65static int tcp_connect_read(int fd);
Willy Tarreaue6b98942007-10-29 01:09:36 +010066
67/* Note: must not be declared <const> as its list will be overwritten */
68static struct protocol proto_tcpv4 = {
69 .name = "tcpv4",
70 .sock_domain = AF_INET,
71 .sock_type = SOCK_STREAM,
72 .sock_prot = IPPROTO_TCP,
73 .sock_family = AF_INET,
74 .sock_addrlen = sizeof(struct sockaddr_in),
75 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020076 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020077 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020078 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010079 .bind_all = tcp_bind_listeners,
80 .unbind_all = unbind_all_listeners,
81 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020082 .get_src = tcp_get_src,
83 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010084 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
85 .nb_listeners = 0,
86};
87
88/* Note: must not be declared <const> as its list will be overwritten */
89static struct protocol proto_tcpv6 = {
90 .name = "tcpv6",
91 .sock_domain = AF_INET6,
92 .sock_type = SOCK_STREAM,
93 .sock_prot = IPPROTO_TCP,
94 .sock_family = AF_INET6,
95 .sock_addrlen = sizeof(struct sockaddr_in6),
96 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020097 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020098 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020099 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100100 .bind_all = tcp_bind_listeners,
101 .unbind_all = unbind_all_listeners,
102 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200103 .get_src = tcp_get_src,
104 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100105 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
106 .nb_listeners = 0,
107};
108
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100109
David du Colombier6f5ccb12011-03-10 22:26:24 +0100110/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100111 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
112 * - 0 : ignore remote address (may even be a NULL pointer)
113 * - 1 : use provided address
114 * - 2 : use provided port
115 * - 3 : use both
116 *
117 * The function supports multiple foreign binding methods :
118 * - linux_tproxy: we directly bind to the foreign address
119 * - cttproxy: we bind to a local address then nat.
120 * The second one can be used as a fallback for the first one.
121 * This function returns 0 when everything's OK, 1 if it could not bind, to the
122 * local address, 2 if it could not bind to the foreign address.
123 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100124int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100125{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100126 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100127 int foreign_ok = 0;
128 int ret;
129
130#ifdef CONFIG_HAP_LINUX_TPROXY
131 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200132 static int ip6_transp_working = 1;
133 switch (local->ss_family) {
134 case AF_INET:
135 if (flags && ip_transp_working) {
136 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
137 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
138 foreign_ok = 1;
139 else
140 ip_transp_working = 0;
141 }
142 break;
143 case AF_INET6:
144 if (flags && ip6_transp_working) {
145 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
146 foreign_ok = 1;
147 else
148 ip6_transp_working = 0;
149 }
150 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100151 }
152#endif
153 if (flags) {
154 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200155 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100156 switch (remote->ss_family) {
157 case AF_INET:
158 if (flags & 1)
159 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
160 if (flags & 2)
161 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
162 break;
163 case AF_INET6:
164 if (flags & 1)
165 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
166 if (flags & 2)
167 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
168 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100169 default:
170 /* we don't want to try to bind to an unknown address family */
171 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100172 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100173 }
174
Simon Hormande072bd2011-06-24 15:11:37 +0900175 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100176 if (foreign_ok) {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200177 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100178 if (ret < 0)
179 return 2;
180 }
181 else {
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200182 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100183 if (ret < 0)
184 return 1;
185 }
186
187 if (!flags)
188 return 0;
189
190#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100191 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100192 struct in_tproxy itp1, itp2;
193 memset(&itp1, 0, sizeof(itp1));
194
195 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100196 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
197 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100198
199 /* set connect flag on socket */
200 itp2.op = TPROXY_FLAGS;
201 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
202
203 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
204 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
205 foreign_ok = 1;
206 }
207 }
208#endif
209 if (!foreign_ok)
210 /* we could not bind to a foreign address */
211 return 2;
212
213 return 0;
214}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100215
Willy Tarreau9650f372009-08-16 14:02:45 +0200216
217/*
Willy Tarreauac825402011-03-04 22:04:29 +0100218 * This function initiates a connection to the target assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200219 * (si->{target,addr.to}). A source address may be pointed to by si->addr.from
Willy Tarreauac825402011-03-04 22:04:29 +0100220 * in case of transparent proxying. Normal source bind addresses are still
221 * determined locally (due to the possible need of a source port).
222 * si->target may point either to a valid server or to a backend, depending
223 * on si->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are supported.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200224 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200225 * It can return one of :
226 * - SN_ERR_NONE if everything's OK
227 * - SN_ERR_SRVTO if there are no more servers
228 * - SN_ERR_SRVCL if the connection was refused by the server
229 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
230 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
231 * - SN_ERR_INTERNAL for any other purely internal errors
232 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
233 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100234
David du Colombier6f5ccb12011-03-10 22:26:24 +0100235int tcp_connect_server(struct stream_interface *si)
Willy Tarreau9650f372009-08-16 14:02:45 +0200236{
237 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100238 struct server *srv;
239 struct proxy *be;
240
241 switch (si->target.type) {
242 case TARG_TYPE_PROXY:
243 be = si->target.ptr.p;
244 srv = NULL;
245 break;
246 case TARG_TYPE_SERVER:
247 srv = si->target.ptr.s;
248 be = srv->proxy;
249 break;
250 default:
251 return SN_ERR_INTERNAL;
252 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200253
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200254 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 +0200255 qfprintf(stderr, "Cannot get a server socket.\n");
256
257 if (errno == ENFILE)
258 send_log(be, LOG_EMERG,
259 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
260 be->id, maxfd);
261 else if (errno == EMFILE)
262 send_log(be, LOG_EMERG,
263 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
264 be->id, maxfd);
265 else if (errno == ENOBUFS || errno == ENOMEM)
266 send_log(be, LOG_EMERG,
267 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
268 be->id, maxfd);
269 /* this is a resource error */
270 return SN_ERR_RESOURCE;
271 }
272
273 if (fd >= global.maxsock) {
274 /* do not log anything there, it's a normal condition when this option
275 * is used to serialize connections to a server !
276 */
277 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
278 close(fd);
279 return SN_ERR_PRXCOND; /* it is a configuration limit */
280 }
281
282 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900283 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200284 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
285 close(fd);
286 return SN_ERR_INTERNAL;
287 }
288
289 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900290 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200291
292 if (be->options & PR_O_TCP_NOLING)
Willy Tarreauf6f82252011-11-30 18:02:24 +0100293 si->flags |= SI_FL_NOLINGER;
Willy Tarreau9650f372009-08-16 14:02:45 +0200294
295 /* allow specific binding :
296 * - server-specific at first
297 * - proxy-specific next
298 */
299 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200300 int ret, flags = 0;
301
Willy Tarreau9650f372009-08-16 14:02:45 +0200302 switch (srv->state & SRV_TPROXY_MASK) {
303 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200304 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200305 flags = 3;
306 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200307 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200308 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200309 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200310 break;
311 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200312
Willy Tarreau9650f372009-08-16 14:02:45 +0200313#ifdef SO_BINDTODEVICE
314 /* Note: this might fail if not CAP_NET_RAW */
315 if (srv->iface_name)
316 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
317#endif
318
319 if (srv->sport_range) {
320 int attempts = 10; /* should be more than enough to find a spare port */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100321 struct sockaddr_storage src;
Willy Tarreau9650f372009-08-16 14:02:45 +0200322
323 ret = 1;
324 src = srv->source_addr;
325
326 do {
327 /* note: in case of retry, we may have to release a previously
328 * allocated port, hence this loop's construct.
329 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200330 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
331 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200332
333 if (!attempts)
334 break;
335 attempts--;
336
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200337 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
338 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200339 break;
340
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200341 fdinfo[fd].port_range = srv->sport_range;
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200342 set_host_port(&src, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200343
Willy Tarreau6471afb2011-09-23 10:54:59 +0200344 ret = tcp_bind_socket(fd, flags, &src, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200345 } while (ret != 0); /* binding NOK */
346 }
347 else {
Willy Tarreau6471afb2011-09-23 10:54:59 +0200348 ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200349 }
350
351 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200352 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
353 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200354 close(fd);
355
356 if (ret == 1) {
357 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
358 be->id, srv->id);
359 send_log(be, LOG_EMERG,
360 "Cannot bind to source address before connect() for server %s/%s.\n",
361 be->id, srv->id);
362 } else {
363 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
364 be->id, srv->id);
365 send_log(be, LOG_EMERG,
366 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
367 be->id, srv->id);
368 }
369 return SN_ERR_RESOURCE;
370 }
371 }
372 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200373 int ret, flags = 0;
374
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 switch (be->options & PR_O_TPXY_MASK) {
376 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200377 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200378 flags = 3;
379 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200380 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200381 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200382 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200383 break;
384 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200385
Willy Tarreau9650f372009-08-16 14:02:45 +0200386#ifdef SO_BINDTODEVICE
387 /* Note: this might fail if not CAP_NET_RAW */
388 if (be->iface_name)
389 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
390#endif
Willy Tarreau6471afb2011-09-23 10:54:59 +0200391 ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200392 if (ret) {
393 close(fd);
394 if (ret == 1) {
395 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
396 be->id);
397 send_log(be, LOG_EMERG,
398 "Cannot bind to source address before connect() for proxy %s.\n",
399 be->id);
400 } else {
401 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
402 be->id);
403 send_log(be, LOG_EMERG,
404 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
405 be->id);
406 }
407 return SN_ERR_RESOURCE;
408 }
409 }
410
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400411#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200412 /* disabling tcp quick ack now allows the first request to leave the
413 * machine with the first ACK. We only do this if there are pending
414 * data in the buffer.
415 */
Willy Tarreau2e046c62012-03-01 16:08:30 +0100416 if ((be->options2 & PR_O2_SMARTCON) && si->ob->o)
Simon Hormande072bd2011-06-24 15:11:37 +0900417 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200418#endif
419
Willy Tarreaue803de22010-01-21 17:43:04 +0100420 if (global.tune.server_sndbuf)
421 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
422
423 if (global.tune.server_rcvbuf)
424 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
425
Willy Tarreau9b061e32012-04-07 18:03:52 +0200426 si->flags &= ~SI_FL_FROM_SET;
Willy Tarreau96596ae2012-06-08 22:57:36 +0200427
428 si->conn.peeraddr = (struct sockaddr *)&si->addr.to;
429 si->conn.peerlen = get_addr_len(&si->addr.to);
430
431 if ((connect(fd, si->conn.peeraddr, si->conn.peerlen) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200432 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
433
434 if (errno == EAGAIN || errno == EADDRINUSE) {
435 char *msg;
436 if (errno == EAGAIN) /* no free ports left, try again later */
437 msg = "no free ports";
438 else
439 msg = "local address already in use";
440
441 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200442 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
443 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200444 close(fd);
445 send_log(be, LOG_EMERG,
446 "Connect() failed for server %s/%s: %s.\n",
447 be->id, srv->id, msg);
448 return SN_ERR_RESOURCE;
449 } else if (errno == ETIMEDOUT) {
450 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200451 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
452 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200453 close(fd);
454 return SN_ERR_SRVTO;
455 } else {
456 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
457 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200458 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
459 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200460 close(fd);
461 return SN_ERR_SRVCL;
462 }
463 }
464
William Lallemandb7ff6a32012-03-02 14:35:21 +0100465 /* needs src ip/port for logging */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200466 if (si->flags & SI_FL_SRC_ADDR)
Willy Tarreau59b94792012-05-11 16:16:40 +0200467 si_get_from_addr(si);
William Lallemandb7ff6a32012-03-02 14:35:21 +0100468
Willy Tarreau80184712012-07-06 14:54:49 +0200469 fdtab[fd].owner = &si->conn;
Willy Tarreau9650f372009-08-16 14:02:45 +0200470 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau505e34a2012-07-06 10:17:53 +0200471 si->conn.flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreau9650f372009-08-16 14:02:45 +0200472
Willy Tarreau2c6be842012-07-06 17:12:34 +0200473 /* Prepare to send a few handshakes related to the on-wire protocol.
474 * If we have nothing to send, we want to confirm that the TCP
Willy Tarreaube0688c2012-05-18 15:15:26 +0200475 * connection is established before doing so, so we use our own write
476 * callback then switch to the sock layer.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200477 */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200478 fdtab[fd].cb[DIR_RD].f = NULL;
479 fdtab[fd].cb[DIR_WR].f = NULL;
480
481 if (si->send_proxy_ofs)
482 si->conn.flags |= CO_FL_SI_SEND_PROXY;
483 else if (si->ob->flags & BF_OUT_EMPTY) {
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200484 fdtab[fd].cb[DIR_RD].f = tcp_connect_read;
485 fdtab[fd].cb[DIR_WR].f = tcp_connect_write;
486 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200487
Willy Tarreaud2274c62012-07-06 14:29:45 +0200488 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200489 fd_insert(fd);
490 EV_FD_SET(fd, DIR_WR); /* for connect status */
491
492 si->state = SI_ST_CON;
493 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
494 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
495
496 return SN_ERR_NONE; /* connection is OK */
497}
498
499
Willy Tarreau59b94792012-05-11 16:16:40 +0200500/*
501 * Retrieves the source address for the socket <fd>, with <dir> indicating
502 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
503 * success, -1 in case of error. The socket's source address is stored in
504 * <sa> for <salen> bytes.
505 */
506int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
507{
508 if (dir)
509 return getsockname(fd, sa, &salen);
510 else
511 return getpeername(fd, sa, &salen);
512}
513
514
515/*
516 * Retrieves the original destination address for the socket <fd>, with <dir>
517 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
518 * listener, if the original destination address was translated, the original
519 * address is retrieved. It returns 0 in case of success, -1 in case of error.
520 * The socket's source address is stored in <sa> for <salen> bytes.
521 */
522int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
523{
524 if (dir)
525 return getpeername(fd, sa, &salen);
526#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
527 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
528 return 0;
529#endif
530 else
531 return getsockname(fd, sa, &salen);
532}
533
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200534/* This is the callback which is set when a connection establishment is pending
535 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreaua190d592012-05-20 18:35:19 +0200536 * once the connection is established. It returns zero if it needs some polling
537 * before being called again.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200538 */
539static int tcp_connect_write(int fd)
540{
Willy Tarreau80184712012-07-06 14:54:49 +0200541 struct connection *conn = fdtab[fd].owner;
542 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200543 struct buffer *b = si->ob;
Willy Tarreaua190d592012-05-20 18:35:19 +0200544 int retval = 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200545
Willy Tarreau80184712012-07-06 14:54:49 +0200546 if (conn->flags & CO_FL_ERROR)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200547 goto out_error;
548
Willy Tarreau80184712012-07-06 14:54:49 +0200549 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200550 goto out_ignore; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200551
552 /* we might have been called just after an asynchronous shutw */
553 if (b->flags & BF_SHUTW)
554 goto out_wakeup;
555
Willy Tarreau2c6be842012-07-06 17:12:34 +0200556 /* We have no data to send to check the connection, and
557 * getsockopt() will not inform us whether the connection
558 * is still pending. So we'll reuse connect() to check the
559 * state of the socket. This has the advantage of giving us
560 * the following info :
561 * - error
562 * - connecting (EALREADY, EINPROGRESS)
563 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200564 */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200565 if ((connect(fd, conn->peeraddr, conn->peerlen) < 0)) {
566 if (errno == EALREADY || errno == EINPROGRESS)
Willy Tarreaua190d592012-05-20 18:35:19 +0200567 goto out_ignore;
568
Willy Tarreau2c6be842012-07-06 17:12:34 +0200569 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200570 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200571
Willy Tarreau2c6be842012-07-06 17:12:34 +0200572 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200573 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200574
575 /* OK we just need to indicate that we got a connection
576 * and that we wrote nothing.
577 */
578 b->flags |= BF_WRITE_NULL;
579
Willy Tarreaua190d592012-05-20 18:35:19 +0200580 /* The FD is ready now, we can hand the handlers to the socket layer
581 * and forward the event there to start working on the socket.
582 */
Willy Tarreaud2274c62012-07-06 14:29:45 +0200583 fdtab[fd].cb[DIR_RD].f = NULL;
584 fdtab[fd].cb[DIR_WR].f = NULL;
Willy Tarreau80184712012-07-06 14:54:49 +0200585 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200586 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200587 return si_data(si)->write(fd);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200588
589 out_wakeup:
590 task_wakeup(si->owner, TASK_WOKEN_IO);
591
592 out_ignore:
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200593 return retval;
594
595 out_error:
596 /* Write error on the file descriptor. We mark the FD as STERROR so
597 * that we don't use it anymore. The error is reported to the stream
598 * interface which will take proper action. We must not perturbate the
599 * buffer because the stream interface wants to ensure transparent
600 * connection retries.
601 */
602
Willy Tarreau80184712012-07-06 14:54:49 +0200603 conn->flags |= CO_FL_ERROR;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200604 fdtab[fd].ev &= ~FD_POLL_STICKY;
605 EV_FD_REM(fd);
606 si->flags |= SI_FL_ERR;
Willy Tarreaua190d592012-05-20 18:35:19 +0200607 retval = 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200608 goto out_wakeup;
609}
610
611
612/* might be used on connect error */
613static int tcp_connect_read(int fd)
614{
Willy Tarreau80184712012-07-06 14:54:49 +0200615 struct connection *conn = fdtab[fd].owner;
616 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200617 int retval;
618
619 retval = 1;
620
Willy Tarreau80184712012-07-06 14:54:49 +0200621 if (conn->flags & CO_FL_ERROR)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200622 goto out_error;
623
Willy Tarreau80184712012-07-06 14:54:49 +0200624 if (!(conn->flags & CO_FL_WAIT_L4_CONN)) {
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200625 retval = 0;
626 goto out_ignore; /* strange we were called while ready */
627 }
628
629 /* stop here if we reached the end of data */
630 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
631 goto out_error;
632
633 out_wakeup:
634 task_wakeup(si->owner, TASK_WOKEN_IO);
635 out_ignore:
636 fdtab[fd].ev &= ~FD_POLL_IN;
637 return retval;
638
639 out_error:
640 /* Read error on the file descriptor. We mark the FD as STERROR so
641 * that we don't use it anymore. The error is reported to the stream
642 * interface which will take proper action. We must not perturbate the
643 * buffer because the stream interface wants to ensure transparent
644 * connection retries.
645 */
646
Willy Tarreau80184712012-07-06 14:54:49 +0200647 conn->flags |= CO_FL_ERROR;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200648 fdtab[fd].ev &= ~FD_POLL_STICKY;
649 EV_FD_REM(fd);
650 si->flags |= SI_FL_ERR;
651 goto out_wakeup;
652}
653
Willy Tarreau59b94792012-05-11 16:16:40 +0200654
Willy Tarreaue6b98942007-10-29 01:09:36 +0100655/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
656 * an error message in <err> if the message is at most <errlen> bytes long
657 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
658 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
659 * was alright and that no message was returned. ERR_RETRYABLE means that an
660 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700661 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100662 * the meaning of the error, but just indicate that a message is present which
663 * should be displayed with the respective level. Last, ERR_ABORT indicates
664 * that it's pointless to try to start other listeners. No error message is
665 * returned if errlen is NULL.
666 */
667int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
668{
669 __label__ tcp_return, tcp_close_return;
670 int fd, err;
671 const char *msg = NULL;
672
673 /* ensure we never return garbage */
674 if (errmsg && errlen)
675 *errmsg = 0;
676
677 if (listener->state != LI_ASSIGNED)
678 return ERR_NONE; /* already bound */
679
680 err = ERR_NONE;
681
682 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
683 err |= ERR_RETRYABLE | ERR_ALERT;
684 msg = "cannot create listening socket";
685 goto tcp_return;
686 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100687
Willy Tarreaue6b98942007-10-29 01:09:36 +0100688 if (fd >= global.maxsock) {
689 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
690 msg = "not enough free sockets (raise '-n' parameter)";
691 goto tcp_close_return;
692 }
693
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200694 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100695 err |= ERR_FATAL | ERR_ALERT;
696 msg = "cannot make socket non-blocking";
697 goto tcp_close_return;
698 }
699
Simon Hormande072bd2011-06-24 15:11:37 +0900700 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100701 /* not fatal but should be reported */
702 msg = "cannot do so_reuseaddr";
703 err |= ERR_ALERT;
704 }
705
706 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900707 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100708
Willy Tarreaue6b98942007-10-29 01:09:36 +0100709#ifdef SO_REUSEPORT
710 /* OpenBSD supports this. As it's present in old libc versions of Linux,
711 * it might return an error that we will silently ignore.
712 */
Simon Hormande072bd2011-06-24 15:11:37 +0900713 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100714#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100715#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200716 if (listener->options & LI_O_FOREIGN) {
717 switch (listener->addr.ss_family) {
718 case AF_INET:
719 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
720 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
721 msg = "cannot make listening socket transparent";
722 err |= ERR_ALERT;
723 }
724 break;
725 case AF_INET6:
726 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
727 msg = "cannot make listening socket transparent";
728 err |= ERR_ALERT;
729 }
730 break;
731 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100732 }
733#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100734#ifdef SO_BINDTODEVICE
735 /* Note: this might fail if not CAP_NET_RAW */
736 if (listener->interface) {
737 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100738 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100739 msg = "cannot bind listener to device";
740 err |= ERR_WARN;
741 }
742 }
743#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400744#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100745 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400746 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200747 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
748 msg = "cannot set MSS";
749 err |= ERR_WARN;
750 }
751 }
752#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200753#if defined(TCP_DEFER_ACCEPT)
754 if (listener->options & LI_O_DEF_ACCEPT) {
755 /* defer accept by up to one second */
756 int accept_delay = 1;
757 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
758 msg = "cannot enable DEFER_ACCEPT";
759 err |= ERR_WARN;
760 }
761 }
762#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100763 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
764 err |= ERR_RETRYABLE | ERR_ALERT;
765 msg = "cannot bind socket";
766 goto tcp_close_return;
767 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100768
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100769 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100770 err |= ERR_RETRYABLE | ERR_ALERT;
771 msg = "cannot listen to socket";
772 goto tcp_close_return;
773 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100774
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400775#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200776 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900777 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200778#endif
779
Willy Tarreaue6b98942007-10-29 01:09:36 +0100780 /* the socket is ready */
781 listener->fd = fd;
782 listener->state = LI_LISTEN;
783
Willy Tarreaueabf3132008-08-29 23:36:51 +0200784 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaueb472682010-05-28 18:46:57 +0200785 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200786 fdtab[fd].iocb = listener->proto->accept;
787 fdtab[fd].cb[DIR_RD].f = NULL; /* never called */
Willy Tarreaueb472682010-05-28 18:46:57 +0200788 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
Willy Tarreaueb472682010-05-28 18:46:57 +0200789 fd_insert(fd);
790
Willy Tarreaue6b98942007-10-29 01:09:36 +0100791 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100792 if (msg && errlen) {
793 char pn[INET6_ADDRSTRLEN];
794
Willy Tarreau631f01c2011-09-05 00:36:48 +0200795 addr_to_str(&listener->addr, pn, sizeof(pn));
796 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100797 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100798 return err;
799
800 tcp_close_return:
801 close(fd);
802 goto tcp_return;
803}
804
805/* This function creates all TCP sockets bound to the protocol entry <proto>.
806 * It is intended to be used as the protocol's bind_all() function.
807 * The sockets will be registered but not added to any fd_set, in order not to
808 * loose them across the fork(). A call to enable_all_listeners() is needed
809 * to complete initialization. The return value is composed from ERR_*.
810 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200811static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100812{
813 struct listener *listener;
814 int err = ERR_NONE;
815
816 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200817 err |= tcp_bind_listener(listener, errmsg, errlen);
818 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100819 break;
820 }
821
822 return err;
823}
824
825/* Add listener to the list of tcpv4 listeners. The listener's state
826 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
827 * listeners is updated. This is the function to use to add a new listener.
828 */
829void tcpv4_add_listener(struct listener *listener)
830{
831 if (listener->state != LI_INIT)
832 return;
833 listener->state = LI_ASSIGNED;
834 listener->proto = &proto_tcpv4;
835 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
836 proto_tcpv4.nb_listeners++;
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 tcpv6_add_listener(struct listener *listener)
844{
845 if (listener->state != LI_INIT)
846 return;
847 listener->state = LI_ASSIGNED;
848 listener->proto = &proto_tcpv6;
849 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
850 proto_tcpv6.nb_listeners++;
851}
852
Willy Tarreauedcf6682008-11-30 23:15:34 +0100853/* This function performs the TCP request analysis on the current request. It
854 * returns 1 if the processing can continue on next analysers, or zero if it
855 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200856 * request. It relies on buffers flags, and updates s->req->analysers. The
857 * function may be called for frontend rules and backend rules. It only relies
858 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100859 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200860int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100861{
862 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200863 struct stksess *ts;
864 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100865 int partial;
866
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100867 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 +0100868 now_ms, __FUNCTION__,
869 s,
870 req,
871 req->rex, req->wex,
872 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100873 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100874 req->analysers);
875
Willy Tarreauedcf6682008-11-30 23:15:34 +0100876 /* We don't know whether we have enough data, so must proceed
877 * this way :
878 * - iterate through all rules in their declaration order
879 * - if one rule returns MISS, it means the inspect delay is
880 * not over yet, then return immediately, otherwise consider
881 * it as a non-match.
882 * - if one rule returns OK, then return OK
883 * - if one rule returns KO, then return KO
884 */
885
Willy Tarreaub824b002010-09-29 16:36:16 +0200886 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 +0200887 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100888 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200889 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100890
Willy Tarreaufb356202010-08-03 14:02:05 +0200891 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100892 int ret = ACL_PAT_PASS;
893
894 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200895 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100896 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200897 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100898 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200899 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
900 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100901 return 0;
902 }
903
904 ret = acl_pass(ret);
905 if (rule->cond->pol == ACL_COND_UNLESS)
906 ret = !ret;
907 }
908
909 if (ret) {
910 /* we have a matching rule. */
911 if (rule->action == TCP_ACT_REJECT) {
912 buffer_abort(req);
913 buffer_abort(s->rep);
914 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200915
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100916 s->be->be_counters.denied_req++;
917 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200918 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200919 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200920
Willy Tarreauedcf6682008-11-30 23:15:34 +0100921 if (!(s->flags & SN_ERR_MASK))
922 s->flags |= SN_ERR_PRXCOND;
923 if (!(s->flags & SN_FINST_MASK))
924 s->flags |= SN_FINST_R;
925 return 0;
926 }
Willy Tarreau56123282010-08-06 19:06:56 +0200927 else if (rule->action == TCP_ACT_TRK_SC1) {
928 if (!s->stkctr1_entry) {
929 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200930 * Also, note that right now we can only track SRC so we
931 * don't check how to get the key, but later we may need
932 * to consider rule->act_prm->trk_ctr.type.
933 */
934 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100935 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200936 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200937 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200938 if (s->fe != s->be)
939 s->flags |= SN_BE_TRACK_SC1;
940 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200941 }
942 }
Willy Tarreau56123282010-08-06 19:06:56 +0200943 else if (rule->action == TCP_ACT_TRK_SC2) {
944 if (!s->stkctr2_entry) {
945 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200946 * Also, note that right now we can only track SRC so we
947 * don't check how to get the key, but later we may need
948 * to consider rule->act_prm->trk_ctr.type.
949 */
950 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100951 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200952 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200953 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200954 if (s->fe != s->be)
955 s->flags |= SN_BE_TRACK_SC2;
956 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200957 }
958 }
959 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100960 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200961 break;
962 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100963 }
964 }
965
966 /* if we get there, it means we have no rule which matches, or
967 * we have an explicit accept, so we apply the default accept.
968 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200969 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100970 req->analyse_exp = TICK_ETERNITY;
971 return 1;
972}
973
Emeric Brun97679e72010-09-23 17:56:44 +0200974/* This function performs the TCP response analysis on the current response. It
975 * returns 1 if the processing can continue on next analysers, or zero if it
976 * needs more data, encounters an error, or wants to immediately abort the
977 * response. It relies on buffers flags, and updates s->rep->analysers. The
978 * function may be called for backend rules.
979 */
980int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit)
981{
982 struct tcp_rule *rule;
983 int partial;
984
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100985 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 +0200986 now_ms, __FUNCTION__,
987 s,
988 rep,
989 rep->rex, rep->wex,
990 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100991 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200992 rep->analysers);
993
994 /* We don't know whether we have enough data, so must proceed
995 * this way :
996 * - iterate through all rules in their declaration order
997 * - if one rule returns MISS, it means the inspect delay is
998 * not over yet, then return immediately, otherwise consider
999 * it as a non-match.
1000 * - if one rule returns OK, then return OK
1001 * - if one rule returns KO, then return KO
1002 */
1003
1004 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001005 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001006 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001007 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001008
1009 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
1010 int ret = ACL_PAT_PASS;
1011
1012 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001013 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +02001014 if (ret == ACL_PAT_MISS) {
1015 /* just set the analyser timeout once at the beginning of the response */
1016 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
1017 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
1018 return 0;
1019 }
1020
1021 ret = acl_pass(ret);
1022 if (rule->cond->pol == ACL_COND_UNLESS)
1023 ret = !ret;
1024 }
1025
1026 if (ret) {
1027 /* we have a matching rule. */
1028 if (rule->action == TCP_ACT_REJECT) {
1029 buffer_abort(rep);
1030 buffer_abort(s->req);
1031 rep->analysers = 0;
1032
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001033 s->be->be_counters.denied_resp++;
1034 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001035 if (s->listener->counters)
1036 s->listener->counters->denied_resp++;
1037
1038 if (!(s->flags & SN_ERR_MASK))
1039 s->flags |= SN_ERR_PRXCOND;
1040 if (!(s->flags & SN_FINST_MASK))
1041 s->flags |= SN_FINST_D;
1042 return 0;
1043 }
1044 else {
1045 /* otherwise accept */
1046 break;
1047 }
1048 }
1049 }
1050
1051 /* if we get there, it means we have no rule which matches, or
1052 * we have an explicit accept, so we apply the default accept.
1053 */
1054 rep->analysers &= ~an_bit;
1055 rep->analyse_exp = TICK_ETERNITY;
1056 return 1;
1057}
1058
1059
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001060/* This function performs the TCP layer4 analysis on the current request. It
1061 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1062 * matches or if no more rule matches. It can only use rules which don't need
1063 * any data.
1064 */
1065int tcp_exec_req_rules(struct session *s)
1066{
1067 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001068 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001069 struct stktable *t = NULL;
1070 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001071 int ret;
1072
1073 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1074 ret = ACL_PAT_PASS;
1075
1076 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001077 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001078 ret = acl_pass(ret);
1079 if (rule->cond->pol == ACL_COND_UNLESS)
1080 ret = !ret;
1081 }
1082
1083 if (ret) {
1084 /* we have a matching rule. */
1085 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001086 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001087 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001088 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001089
1090 if (!(s->flags & SN_ERR_MASK))
1091 s->flags |= SN_ERR_PRXCOND;
1092 if (!(s->flags & SN_FINST_MASK))
1093 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001094 result = 0;
1095 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001096 }
Willy Tarreau56123282010-08-06 19:06:56 +02001097 else if (rule->action == TCP_ACT_TRK_SC1) {
1098 if (!s->stkctr1_entry) {
1099 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001100 * Also, note that right now we can only track SRC so we
1101 * don't check how to get the key, but later we may need
1102 * to consider rule->act_prm->trk_ctr.type.
1103 */
1104 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001105 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001106 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001107 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001108 }
1109 }
Willy Tarreau56123282010-08-06 19:06:56 +02001110 else if (rule->action == TCP_ACT_TRK_SC2) {
1111 if (!s->stkctr2_entry) {
1112 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001113 * Also, note that right now we can only track SRC so we
1114 * don't check how to get the key, but later we may need
1115 * to consider rule->act_prm->trk_ctr.type.
1116 */
1117 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001118 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001119 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001120 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001121 }
1122 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001123 else {
1124 /* otherwise it's an accept */
1125 break;
1126 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001127 }
1128 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001129 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001130}
1131
Emeric Brun97679e72010-09-23 17:56:44 +02001132/* Parse a tcp-response rule. Return a negative value in case of failure */
1133static int tcp_parse_response_rule(char **args, int arg, int section_type,
1134 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001135 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001136{
1137 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001138 memprintf(err, "%s %s is only allowed in 'backend' sections",
1139 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001140 return -1;
1141 }
1142
1143 if (strcmp(args[arg], "accept") == 0) {
1144 arg++;
1145 rule->action = TCP_ACT_ACCEPT;
1146 }
1147 else if (strcmp(args[arg], "reject") == 0) {
1148 arg++;
1149 rule->action = TCP_ACT_REJECT;
1150 }
1151 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001152 memprintf(err,
1153 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1154 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001155 return -1;
1156 }
1157
1158 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001159 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1160 memprintf(err,
1161 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1162 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001163 return -1;
1164 }
1165 }
1166 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001167 memprintf(err,
1168 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001169 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1170 return -1;
1171 }
1172 return 0;
1173}
1174
1175
1176
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001177/* Parse a tcp-request rule. Return a negative value in case of failure */
1178static int tcp_parse_request_rule(char **args, int arg, int section_type,
1179 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001180 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001181{
1182 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001183 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1184 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001185 return -1;
1186 }
1187
1188 if (!strcmp(args[arg], "accept")) {
1189 arg++;
1190 rule->action = TCP_ACT_ACCEPT;
1191 }
1192 else if (!strcmp(args[arg], "reject")) {
1193 arg++;
1194 rule->action = TCP_ACT_REJECT;
1195 }
Willy Tarreau56123282010-08-06 19:06:56 +02001196 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001197 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001198 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001199
1200 arg++;
1201 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001202 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001203
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001204 if (ret < 0) { /* nb: warnings are not handled yet */
1205 memprintf(err,
1206 "'%s %s %s' : %s in %s '%s'",
1207 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1208 return ret;
1209 }
Willy Tarreau56123282010-08-06 19:06:56 +02001210 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001211 }
Willy Tarreau56123282010-08-06 19:06:56 +02001212 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001213 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001214 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001215
1216 arg++;
1217 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001218 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001219
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001220 if (ret < 0) { /* nb: warnings are not handled yet */
1221 memprintf(err,
1222 "'%s %s %s' : %s in %s '%s'",
1223 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1224 return ret;
1225 }
Willy Tarreau56123282010-08-06 19:06:56 +02001226 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001227 }
1228 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001229 memprintf(err,
1230 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1231 "or 'track-sc2' in %s '%s' (got '%s')",
1232 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001233 return -1;
1234 }
1235
1236 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001237 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1238 memprintf(err,
1239 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1240 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001241 return -1;
1242 }
1243 }
1244 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001245 memprintf(err,
1246 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001247 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1248 return -1;
1249 }
1250 return 0;
1251}
1252
Emeric Brun97679e72010-09-23 17:56:44 +02001253/* This function should be called to parse a line starting with the "tcp-response"
1254 * keyword.
1255 */
1256static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001257 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001258{
1259 const char *ptr = NULL;
1260 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001261 int warn = 0;
1262 int arg;
1263 struct tcp_rule *rule;
1264
1265 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001266 memprintf(err, "missing argument for '%s' in %s '%s'",
1267 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001268 return -1;
1269 }
1270
1271 if (strcmp(args[1], "inspect-delay") == 0) {
1272 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001273 memprintf(err, "%s %s is only allowed in 'backend' sections",
1274 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001275 return -1;
1276 }
1277
1278 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001279 memprintf(err,
1280 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1281 args[0], args[1], proxy_type_str(curpx), curpx->id);
1282 if (ptr)
1283 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001284 return -1;
1285 }
1286
1287 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001288 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1289 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001290 return 1;
1291 }
1292 curpx->tcp_rep.inspect_delay = val;
1293 return 0;
1294 }
1295
Simon Hormande072bd2011-06-24 15:11:37 +09001296 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001297 LIST_INIT(&rule->list);
1298 arg = 1;
1299
1300 if (strcmp(args[1], "content") == 0) {
1301 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001302 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001303 goto error;
1304
1305 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1306 struct acl *acl;
1307 const char *name;
1308
1309 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1310 name = acl ? acl->name : "(unknown)";
1311
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001312 memprintf(err,
1313 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1314 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001315 warn++;
1316 }
1317
1318 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1319 }
1320 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001321 memprintf(err,
1322 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1323 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001324 goto error;
1325 }
1326
1327 return warn;
1328 error:
1329 free(rule);
1330 return -1;
1331}
1332
1333
Willy Tarreaub6866442008-07-14 23:54:42 +02001334/* This function should be called to parse a line starting with the "tcp-request"
1335 * keyword.
1336 */
1337static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001338 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001339{
1340 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001341 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001342 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001343 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001344 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001345
1346 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001347 if (curpx == defpx)
1348 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1349 else
1350 memprintf(err, "missing argument for '%s' in %s '%s'",
1351 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001352 return -1;
1353 }
1354
1355 if (!strcmp(args[1], "inspect-delay")) {
1356 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001357 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1358 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001359 return -1;
1360 }
1361
Willy Tarreaub6866442008-07-14 23:54:42 +02001362 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001363 memprintf(err,
1364 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1365 args[0], args[1], proxy_type_str(curpx), curpx->id);
1366 if (ptr)
1367 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001368 return -1;
1369 }
1370
1371 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001372 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1373 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001374 return 1;
1375 }
1376 curpx->tcp_req.inspect_delay = val;
1377 return 0;
1378 }
1379
Simon Hormande072bd2011-06-24 15:11:37 +09001380 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001381 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001382 arg = 1;
1383
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001384 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001385 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001386 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001387 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001388
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001389 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001390 struct acl *acl;
1391 const char *name;
1392
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001393 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001394 name = acl ? acl->name : "(unknown)";
1395
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001396 memprintf(err,
1397 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1398 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001399 warn++;
1400 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001401 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001402 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001403 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001404 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001405
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001406 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001407 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1408 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001409 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001410 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001411
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001412 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001413 goto error;
1414
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001415 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1416 struct acl *acl;
1417 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001418
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001419 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1420 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001421
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001422 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001423 memprintf(err,
1424 "'%s %s' may not reference acl '%s' which makes use of "
1425 "payload in %s '%s'. Please use '%s content' for this.",
1426 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001427 goto error;
1428 }
1429 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001430 memprintf(err,
1431 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1432 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001433 warn++;
1434 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001435 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001436 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001437 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001438 if (curpx == defpx)
1439 memprintf(err,
1440 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1441 args[0], args[1]);
1442 else
1443 memprintf(err,
1444 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1445 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001446 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001447 }
1448
Willy Tarreau1a687942010-05-23 22:40:30 +02001449 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001450 error:
1451 free(rule);
1452 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001453}
1454
Willy Tarreau645513a2010-05-24 20:55:15 +02001455
1456/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001457/* All supported sample fetch functios must be declared here */
1458/************************************************************************/
1459
1460/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001461 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001462 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1463 * is a string (cookie name), other types will lead to undefined behaviour.
1464 */
1465int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001466smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001467 const struct arg *args, struct sample *smp)
1468{
1469 int bleft;
1470 const unsigned char *data;
1471
1472 if (!l4 || !l4->req)
1473 return 0;
1474
1475 smp->flags = 0;
1476 smp->type = SMP_T_CSTR;
1477
1478 bleft = l4->req->i;
1479 if (bleft <= 11)
1480 goto too_short;
1481
1482 data = (const unsigned char *)l4->req->p + 11;
1483 bleft -= 11;
1484
1485 if (bleft <= 7)
1486 goto too_short;
1487
1488 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1489 goto not_cookie;
1490
1491 data += 7;
1492 bleft -= 7;
1493
1494 while (bleft > 0 && *data == ' ') {
1495 data++;
1496 bleft--;
1497 }
1498
1499 if (args) {
1500
1501 if (bleft <= args->data.str.len)
1502 goto too_short;
1503
1504 if ((data[args->data.str.len] != '=') ||
1505 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1506 goto not_cookie;
1507
1508 data += args->data.str.len + 1;
1509 bleft -= args->data.str.len + 1;
1510 } else {
1511 while (bleft > 0 && *data != '=') {
1512 if (*data == '\r' || *data == '\n')
1513 goto not_cookie;
1514 data++;
1515 bleft--;
1516 }
1517
1518 if (bleft < 1)
1519 goto too_short;
1520
1521 if (*data != '=')
1522 goto not_cookie;
1523
1524 data++;
1525 bleft--;
1526 }
1527
1528 /* data points to cookie value */
1529 smp->data.str.str = (char *)data;
1530 smp->data.str.len = 0;
1531
1532 while (bleft > 0 && *data != '\r') {
1533 data++;
1534 bleft--;
1535 }
1536
1537 if (bleft < 2)
1538 goto too_short;
1539
1540 if (data[0] != '\r' || data[1] != '\n')
1541 goto not_cookie;
1542
1543 smp->data.str.len = (char *)data - smp->data.str.str;
1544 smp->flags = SMP_F_VOLATILE;
1545 return 1;
1546
1547 too_short:
1548 smp->flags = SMP_F_MAY_CHANGE;
1549 not_cookie:
1550 return 0;
1551}
1552
Willy Tarreau32389b72012-04-23 23:13:20 +02001553/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001554/* All supported ACL keywords must be declared here. */
1555/************************************************************************/
1556
Willy Tarreau32389b72012-04-23 23:13:20 +02001557/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1558static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001559acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001560 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001561{
1562 int ret;
1563
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001564 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001565
1566 if (smp->flags & SMP_F_MAY_CHANGE)
1567 return 0;
1568
1569 smp->flags = SMP_F_VOLATILE;
1570 smp->type = SMP_T_UINT;
1571 smp->data.uint = ret;
1572 return 1;
1573}
1574
1575
Willy Tarreau4a129812012-04-25 17:31:42 +02001576/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001577static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001578smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001579 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001580{
Willy Tarreauf4362b32011-12-16 17:49:52 +01001581 switch (l4->si[0].addr.from.ss_family) {
1582 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001583 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.from)->sin_addr;
1584 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001585 break;
1586 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001587 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.from))->sin6_addr;
1588 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001589 break;
1590 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001591 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001592 }
Emeric Brunf769f512010-10-22 17:14:01 +02001593
Willy Tarreau37406352012-04-23 16:16:37 +02001594 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001595 return 1;
1596}
1597
Willy Tarreaua5e37562011-12-16 17:06:15 +01001598/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001599static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001600smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001601 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001602{
Willy Tarreauf853c462012-04-23 18:53:56 +02001603 smp->type = SMP_T_UINT;
1604 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001605 return 0;
1606
Willy Tarreau37406352012-04-23 16:16:37 +02001607 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001608 return 1;
1609}
1610
Willy Tarreau4a129812012-04-25 17:31:42 +02001611/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001612static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001613smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001614 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001615{
Willy Tarreau59b94792012-05-11 16:16:40 +02001616 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001617
Willy Tarreauf4362b32011-12-16 17:49:52 +01001618 switch (l4->si[0].addr.to.ss_family) {
1619 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001620 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.to)->sin_addr;
1621 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001622 break;
1623 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001624 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.to))->sin6_addr;
1625 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001626 break;
1627 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001628 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001629 }
Emeric Brunf769f512010-10-22 17:14:01 +02001630
Willy Tarreau37406352012-04-23 16:16:37 +02001631 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001632 return 1;
1633}
1634
Willy Tarreaua5e37562011-12-16 17:06:15 +01001635/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001636static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001637smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001638 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001639{
Willy Tarreau59b94792012-05-11 16:16:40 +02001640 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001641
Willy Tarreauf853c462012-04-23 18:53:56 +02001642 smp->type = SMP_T_UINT;
1643 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001644 return 0;
1645
Willy Tarreau37406352012-04-23 16:16:37 +02001646 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001647 return 1;
1648}
1649
1650static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001651smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1652 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001653{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001654 unsigned int len_offset = arg_p[0].data.uint;
1655 unsigned int len_size = arg_p[1].data.uint;
1656 unsigned int buf_offset;
1657 unsigned int buf_size = 0;
Emericf2d7cae2010-11-05 18:13:50 +01001658 struct buffer *b;
1659 int i;
1660
1661 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1662 /* by default buf offset == len offset + len size */
1663 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1664
1665 if (!l4)
1666 return 0;
1667
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001668 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001669
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001670 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001671 return 0;
1672
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001673 if (len_offset + len_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001674 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001675
1676 for (i = 0; i < len_size; i++) {
Willy Tarreau89fa7062012-03-02 16:13:16 +01001677 buf_size = (buf_size << 8) + ((unsigned char *)b->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001678 }
1679
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001680 /* buf offset may be implicit, absolute or relative */
1681 buf_offset = len_offset + len_size;
1682 if (arg_p[2].type == ARGT_UINT)
1683 buf_offset = arg_p[2].data.uint;
1684 else if (arg_p[2].type == ARGT_SINT)
1685 buf_offset += arg_p[2].data.sint;
1686
Willy Tarreau82ea8002012-04-25 19:19:43 +02001687 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1688 /* will never match */
1689 smp->flags = 0;
1690 return 0;
1691 }
1692
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001693 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001694 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001695
1696 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001697 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001698 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001699 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001700 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001701
1702 too_short:
1703 smp->flags = SMP_F_MAY_CHANGE;
1704 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001705}
1706
1707static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001708smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1709 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001710{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001711 unsigned int buf_offset = arg_p[0].data.uint;
1712 unsigned int buf_size = arg_p[1].data.uint;
Emericf2d7cae2010-11-05 18:13:50 +01001713 struct buffer *b;
1714
1715 if (!l4)
1716 return 0;
1717
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001718 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001719
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001720 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001721 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001722
1723 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 }
Emericf2d7cae2010-11-05 18:13:50 +01001728
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;
Simon Hormanab814e02011-06-24 14:50:20 +09001736 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001737
1738 too_short:
1739 smp->flags = SMP_F_MAY_CHANGE;
1740 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001741}
1742
Willy Tarreau21d68a62012-04-20 15:52:36 +02001743/* This function is used to validate the arguments passed to a "payload" fetch
1744 * keyword. This keyword expects two positive integers, with the second one
1745 * being strictly positive. It is assumed that the types are already the correct
1746 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1747 * filled with a pointer to an error message in case of error, that the caller
1748 * is responsible for freeing. The initial location must either be freeable or
1749 * NULL.
1750 */
1751static int val_payload(struct arg *arg, char **err_msg)
1752{
1753 if (!arg[1].data.uint) {
1754 if (err_msg)
1755 memprintf(err_msg, "payload length must be > 0");
1756 return 0;
1757 }
1758 return 1;
1759}
1760
1761/* This function is used to validate the arguments passed to a "payload_lv" fetch
1762 * keyword. This keyword allows two positive integers and an optional signed one,
1763 * with the second one being strictly positive and the third one being greater than
1764 * the opposite of the two others if negative. It is assumed that the types are
1765 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1766 * not NULL, it will be filled with a pointer to an error message in case of
1767 * error, that the caller is responsible for freeing. The initial location must
1768 * either be freeable or NULL.
1769 */
1770static int val_payload_lv(struct arg *arg, char **err_msg)
1771{
1772 if (!arg[1].data.uint) {
1773 if (err_msg)
1774 memprintf(err_msg, "payload length must be > 0");
1775 return 0;
1776 }
1777
1778 if (arg[2].type == ARGT_SINT &&
1779 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1780 if (err_msg)
1781 memprintf(err_msg, "payload offset too negative");
1782 return 0;
1783 }
1784 return 1;
1785}
1786
Willy Tarreaub6866442008-07-14 23:54:42 +02001787static struct cfg_kw_list cfg_kws = {{ },{
1788 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001789 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001790 { 0, NULL, NULL },
1791}};
1792
Willy Tarreau61612d42012-04-19 18:42:05 +02001793/* Note: must not be declared <const> as its list will be overwritten.
1794 * Please take care of keeping this list alphabetically sorted.
1795 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001796static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001797 { "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 +02001798 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001799 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1800 { "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 +02001801 { "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 +02001802 { "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 +02001803 { "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 +02001804 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001805 { NULL, NULL, NULL, NULL },
1806}};
1807
Willy Tarreau4a129812012-04-25 17:31:42 +02001808/* Note: must not be declared <const> as its list will be overwritten.
1809 * Note: fetches that may return multiple types must be declared as the lowest
1810 * common denominator, the type that can be casted into all other ones. For
1811 * instance v4/v6 must be declared v4.
1812 */
Willy Tarreau12785782012-04-27 21:37:17 +02001813static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001814 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001815 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001816 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001817 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1818 { "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 +02001819 { "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 +02001820 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001821 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001822}};
1823
Willy Tarreaue6b98942007-10-29 01:09:36 +01001824__attribute__((constructor))
1825static void __tcp_protocol_init(void)
1826{
1827 protocol_register(&proto_tcpv4);
1828 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001829 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001830 cfg_register_keywords(&cfg_kws);
1831 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001832}
1833
1834
1835/*
1836 * Local variables:
1837 * c-indent-level: 8
1838 * c-basic-offset: 8
1839 * End:
1840 */