blob: 7b69aadc4093b21c4f41fc6c9a888eb5aab72c0c [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 Tarreau03fa5df2010-05-24 21:02:37 +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 Tarreau9650f372009-08-16 14:02:45 +0200469 fdtab[fd].owner = si;
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 Tarreaube0688c2012-05-18 15:15:26 +0200473 /* If we have nothing to send, we want to confirm that the TCP
474 * connection is established before doing so, so we use our own write
475 * callback then switch to the sock layer.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200476 */
Willy Tarreaua190d592012-05-20 18:35:19 +0200477 if ((si->ob->flags & BF_OUT_EMPTY) || si->send_proxy_ofs) {
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200478 fdtab[fd].cb[DIR_RD].f = tcp_connect_read;
479 fdtab[fd].cb[DIR_WR].f = tcp_connect_write;
480 }
481 else {
Willy Tarreaud2274c62012-07-06 14:29:45 +0200482 fdtab[fd].cb[DIR_RD].f = NULL;
483 fdtab[fd].cb[DIR_WR].f = NULL;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200484 }
485
Willy Tarreaud2274c62012-07-06 14:29:45 +0200486 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200487 fd_insert(fd);
488 EV_FD_SET(fd, DIR_WR); /* for connect status */
489
490 si->state = SI_ST_CON;
491 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
492 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
493
494 return SN_ERR_NONE; /* connection is OK */
495}
496
497
Willy Tarreau59b94792012-05-11 16:16:40 +0200498/*
499 * Retrieves the source address for the socket <fd>, with <dir> indicating
500 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
501 * success, -1 in case of error. The socket's source address is stored in
502 * <sa> for <salen> bytes.
503 */
504int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
505{
506 if (dir)
507 return getsockname(fd, sa, &salen);
508 else
509 return getpeername(fd, sa, &salen);
510}
511
512
513/*
514 * Retrieves the original destination address for the socket <fd>, with <dir>
515 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
516 * listener, if the original destination address was translated, the original
517 * address is retrieved. It returns 0 in case of success, -1 in case of error.
518 * The socket's source address is stored in <sa> for <salen> bytes.
519 */
520int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
521{
522 if (dir)
523 return getpeername(fd, sa, &salen);
524#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
525 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
526 return 0;
527#endif
528 else
529 return getsockname(fd, sa, &salen);
530}
531
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200532/* This is the callback which is set when a connection establishment is pending
533 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreaua190d592012-05-20 18:35:19 +0200534 * once the connection is established. It returns zero if it needs some polling
535 * before being called again.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200536 */
537static int tcp_connect_write(int fd)
538{
539 struct stream_interface *si = fdtab[fd].owner;
540 struct buffer *b = si->ob;
Willy Tarreaua190d592012-05-20 18:35:19 +0200541 int retval = 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200542
Willy Tarreau505e34a2012-07-06 10:17:53 +0200543 if (si->conn.flags & CO_FL_ERROR)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200544 goto out_error;
545
Willy Tarreau505e34a2012-07-06 10:17:53 +0200546 if (!(si->conn.flags & CO_FL_WAIT_L4_CONN))
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200547 goto out_ignore; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200548
549 /* we might have been called just after an asynchronous shutw */
550 if (b->flags & BF_SHUTW)
551 goto out_wakeup;
552
Willy Tarreaua190d592012-05-20 18:35:19 +0200553 /* If we have a PROXY line to send, we'll use this to validate the
554 * connection, in which case the connection is validated only once
555 * we've sent the whole proxy line. Otherwise we use connect().
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200556 */
Willy Tarreaua190d592012-05-20 18:35:19 +0200557 if (si->send_proxy_ofs) {
558 int ret;
559
560 /* The target server expects a PROXY line to be sent first.
561 * If the send_proxy_ofs is negative, it corresponds to the
562 * offset to start sending from then end of the proxy string
563 * (which is recomputed every time since it's constant). If
564 * it is positive, it means we have to send from the start.
565 */
566 ret = make_proxy_line(trash, trashlen, &b->prod->addr.from, &b->prod->addr.to);
567 if (!ret)
568 goto out_error;
569
570 if (si->send_proxy_ofs > 0)
571 si->send_proxy_ofs = -ret; /* first call */
572
573 /* we have to send trash from (ret+sp for -sp bytes) */
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200574 ret = send(fd, trash + ret + si->send_proxy_ofs, -si->send_proxy_ofs,
Willy Tarreaua190d592012-05-20 18:35:19 +0200575 (b->flags & BF_OUT_EMPTY) ? 0 : MSG_MORE);
576
577 if (ret == 0)
578 goto out_ignore;
579
580 if (ret < 0) {
581 if (errno == EAGAIN)
582 goto out_ignore;
583 goto out_error;
584 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200585
Willy Tarreaua190d592012-05-20 18:35:19 +0200586 si->send_proxy_ofs += ret; /* becomes zero once complete */
587 if (si->send_proxy_ofs != 0)
588 goto out_ignore;
589
590 /* OK we've sent the whole line, we're connected */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200591 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200592 else {
593 /* We have no data to send to check the connection, and
594 * getsockopt() will not inform us whether the connection
595 * is still pending. So we'll reuse connect() to check the
596 * state of the socket. This has the advantage of giving us
597 * the following info :
598 * - error
599 * - connecting (EALREADY, EINPROGRESS)
600 * - connected (EISCONN, 0)
601 */
Willy Tarreau96596ae2012-06-08 22:57:36 +0200602 if ((connect(fd, si->conn.peeraddr, si->conn.peerlen) < 0)) {
Willy Tarreaua190d592012-05-20 18:35:19 +0200603 if (errno == EALREADY || errno == EINPROGRESS)
604 goto out_ignore;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200605
Willy Tarreaua190d592012-05-20 18:35:19 +0200606 if (errno && errno != EISCONN)
607 goto out_error;
608
609 /* otherwise we're connected */
610 }
611 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200612
613 /* OK we just need to indicate that we got a connection
614 * and that we wrote nothing.
615 */
616 b->flags |= BF_WRITE_NULL;
617
Willy Tarreaua190d592012-05-20 18:35:19 +0200618 /* The FD is ready now, we can hand the handlers to the socket layer
619 * and forward the event there to start working on the socket.
620 */
Willy Tarreaud2274c62012-07-06 14:29:45 +0200621 fdtab[fd].cb[DIR_RD].f = NULL;
622 fdtab[fd].cb[DIR_WR].f = NULL;
Willy Tarreau505e34a2012-07-06 10:17:53 +0200623 si->conn.flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200624 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200625 return si_data(si)->write(fd);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200626
627 out_wakeup:
628 task_wakeup(si->owner, TASK_WOKEN_IO);
629
630 out_ignore:
631 fdtab[fd].ev &= ~FD_POLL_OUT;
632 return retval;
633
634 out_error:
635 /* Write error on the file descriptor. We mark the FD as STERROR so
636 * that we don't use it anymore. The error is reported to the stream
637 * interface which will take proper action. We must not perturbate the
638 * buffer because the stream interface wants to ensure transparent
639 * connection retries.
640 */
641
Willy Tarreau505e34a2012-07-06 10:17:53 +0200642 si->conn.flags |= CO_FL_ERROR;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200643 fdtab[fd].ev &= ~FD_POLL_STICKY;
644 EV_FD_REM(fd);
645 si->flags |= SI_FL_ERR;
Willy Tarreaua190d592012-05-20 18:35:19 +0200646 retval = 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200647 goto out_wakeup;
648}
649
650
651/* might be used on connect error */
652static int tcp_connect_read(int fd)
653{
654 struct stream_interface *si = fdtab[fd].owner;
655 int retval;
656
657 retval = 1;
658
Willy Tarreau505e34a2012-07-06 10:17:53 +0200659 if (si->conn.flags & CO_FL_ERROR)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200660 goto out_error;
661
Willy Tarreau505e34a2012-07-06 10:17:53 +0200662 if (!(si->conn.flags & CO_FL_WAIT_L4_CONN)) {
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200663 retval = 0;
664 goto out_ignore; /* strange we were called while ready */
665 }
666
667 /* stop here if we reached the end of data */
668 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
669 goto out_error;
670
671 out_wakeup:
672 task_wakeup(si->owner, TASK_WOKEN_IO);
673 out_ignore:
674 fdtab[fd].ev &= ~FD_POLL_IN;
675 return retval;
676
677 out_error:
678 /* Read error on the file descriptor. We mark the FD as STERROR so
679 * that we don't use it anymore. The error is reported to the stream
680 * interface which will take proper action. We must not perturbate the
681 * buffer because the stream interface wants to ensure transparent
682 * connection retries.
683 */
684
Willy Tarreau505e34a2012-07-06 10:17:53 +0200685 si->conn.flags |= CO_FL_ERROR;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200686 fdtab[fd].ev &= ~FD_POLL_STICKY;
687 EV_FD_REM(fd);
688 si->flags |= SI_FL_ERR;
689 goto out_wakeup;
690}
691
Willy Tarreau59b94792012-05-11 16:16:40 +0200692
Willy Tarreaue6b98942007-10-29 01:09:36 +0100693/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
694 * an error message in <err> if the message is at most <errlen> bytes long
695 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
696 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
697 * was alright and that no message was returned. ERR_RETRYABLE means that an
698 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700699 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100700 * the meaning of the error, but just indicate that a message is present which
701 * should be displayed with the respective level. Last, ERR_ABORT indicates
702 * that it's pointless to try to start other listeners. No error message is
703 * returned if errlen is NULL.
704 */
705int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
706{
707 __label__ tcp_return, tcp_close_return;
708 int fd, err;
709 const char *msg = NULL;
710
711 /* ensure we never return garbage */
712 if (errmsg && errlen)
713 *errmsg = 0;
714
715 if (listener->state != LI_ASSIGNED)
716 return ERR_NONE; /* already bound */
717
718 err = ERR_NONE;
719
720 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
721 err |= ERR_RETRYABLE | ERR_ALERT;
722 msg = "cannot create listening socket";
723 goto tcp_return;
724 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100725
Willy Tarreaue6b98942007-10-29 01:09:36 +0100726 if (fd >= global.maxsock) {
727 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
728 msg = "not enough free sockets (raise '-n' parameter)";
729 goto tcp_close_return;
730 }
731
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200732 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100733 err |= ERR_FATAL | ERR_ALERT;
734 msg = "cannot make socket non-blocking";
735 goto tcp_close_return;
736 }
737
Simon Hormande072bd2011-06-24 15:11:37 +0900738 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100739 /* not fatal but should be reported */
740 msg = "cannot do so_reuseaddr";
741 err |= ERR_ALERT;
742 }
743
744 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900745 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100746
Willy Tarreaue6b98942007-10-29 01:09:36 +0100747#ifdef SO_REUSEPORT
748 /* OpenBSD supports this. As it's present in old libc versions of Linux,
749 * it might return an error that we will silently ignore.
750 */
Simon Hormande072bd2011-06-24 15:11:37 +0900751 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100752#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100753#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200754 if (listener->options & LI_O_FOREIGN) {
755 switch (listener->addr.ss_family) {
756 case AF_INET:
757 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
758 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
759 msg = "cannot make listening socket transparent";
760 err |= ERR_ALERT;
761 }
762 break;
763 case AF_INET6:
764 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
765 msg = "cannot make listening socket transparent";
766 err |= ERR_ALERT;
767 }
768 break;
769 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100770 }
771#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100772#ifdef SO_BINDTODEVICE
773 /* Note: this might fail if not CAP_NET_RAW */
774 if (listener->interface) {
775 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100776 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100777 msg = "cannot bind listener to device";
778 err |= ERR_WARN;
779 }
780 }
781#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400782#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100783 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400784 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200785 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
786 msg = "cannot set MSS";
787 err |= ERR_WARN;
788 }
789 }
790#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200791#if defined(TCP_DEFER_ACCEPT)
792 if (listener->options & LI_O_DEF_ACCEPT) {
793 /* defer accept by up to one second */
794 int accept_delay = 1;
795 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
796 msg = "cannot enable DEFER_ACCEPT";
797 err |= ERR_WARN;
798 }
799 }
800#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100801 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
802 err |= ERR_RETRYABLE | ERR_ALERT;
803 msg = "cannot bind socket";
804 goto tcp_close_return;
805 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100806
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100807 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100808 err |= ERR_RETRYABLE | ERR_ALERT;
809 msg = "cannot listen to socket";
810 goto tcp_close_return;
811 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100812
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400813#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200814 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900815 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200816#endif
817
Willy Tarreaue6b98942007-10-29 01:09:36 +0100818 /* the socket is ready */
819 listener->fd = fd;
820 listener->state = LI_LISTEN;
821
Willy Tarreaueabf3132008-08-29 23:36:51 +0200822 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaueb472682010-05-28 18:46:57 +0200823 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200824 fdtab[fd].iocb = listener->proto->accept;
825 fdtab[fd].cb[DIR_RD].f = NULL; /* never called */
Willy Tarreaueb472682010-05-28 18:46:57 +0200826 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
Willy Tarreaueb472682010-05-28 18:46:57 +0200827 fd_insert(fd);
828
Willy Tarreaue6b98942007-10-29 01:09:36 +0100829 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100830 if (msg && errlen) {
831 char pn[INET6_ADDRSTRLEN];
832
Willy Tarreau631f01c2011-09-05 00:36:48 +0200833 addr_to_str(&listener->addr, pn, sizeof(pn));
834 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100835 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100836 return err;
837
838 tcp_close_return:
839 close(fd);
840 goto tcp_return;
841}
842
843/* This function creates all TCP sockets bound to the protocol entry <proto>.
844 * It is intended to be used as the protocol's bind_all() function.
845 * The sockets will be registered but not added to any fd_set, in order not to
846 * loose them across the fork(). A call to enable_all_listeners() is needed
847 * to complete initialization. The return value is composed from ERR_*.
848 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200849static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100850{
851 struct listener *listener;
852 int err = ERR_NONE;
853
854 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200855 err |= tcp_bind_listener(listener, errmsg, errlen);
856 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100857 break;
858 }
859
860 return err;
861}
862
863/* Add listener to the list of tcpv4 listeners. The listener's state
864 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
865 * listeners is updated. This is the function to use to add a new listener.
866 */
867void tcpv4_add_listener(struct listener *listener)
868{
869 if (listener->state != LI_INIT)
870 return;
871 listener->state = LI_ASSIGNED;
872 listener->proto = &proto_tcpv4;
873 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
874 proto_tcpv4.nb_listeners++;
875}
876
877/* Add listener to the list of tcpv4 listeners. The listener's state
878 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
879 * listeners is updated. This is the function to use to add a new listener.
880 */
881void tcpv6_add_listener(struct listener *listener)
882{
883 if (listener->state != LI_INIT)
884 return;
885 listener->state = LI_ASSIGNED;
886 listener->proto = &proto_tcpv6;
887 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
888 proto_tcpv6.nb_listeners++;
889}
890
Willy Tarreauedcf6682008-11-30 23:15:34 +0100891/* This function performs the TCP request analysis on the current request. It
892 * returns 1 if the processing can continue on next analysers, or zero if it
893 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200894 * request. It relies on buffers flags, and updates s->req->analysers. The
895 * function may be called for frontend rules and backend rules. It only relies
896 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100897 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200898int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100899{
900 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200901 struct stksess *ts;
902 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100903 int partial;
904
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100905 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 +0100906 now_ms, __FUNCTION__,
907 s,
908 req,
909 req->rex, req->wex,
910 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100911 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100912 req->analysers);
913
Willy Tarreauedcf6682008-11-30 23:15:34 +0100914 /* We don't know whether we have enough data, so must proceed
915 * this way :
916 * - iterate through all rules in their declaration order
917 * - if one rule returns MISS, it means the inspect delay is
918 * not over yet, then return immediately, otherwise consider
919 * it as a non-match.
920 * - if one rule returns OK, then return OK
921 * - if one rule returns KO, then return KO
922 */
923
Willy Tarreaub824b002010-09-29 16:36:16 +0200924 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 +0200925 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100926 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200927 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100928
Willy Tarreaufb356202010-08-03 14:02:05 +0200929 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100930 int ret = ACL_PAT_PASS;
931
932 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200933 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100934 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200935 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100936 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200937 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
938 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100939 return 0;
940 }
941
942 ret = acl_pass(ret);
943 if (rule->cond->pol == ACL_COND_UNLESS)
944 ret = !ret;
945 }
946
947 if (ret) {
948 /* we have a matching rule. */
949 if (rule->action == TCP_ACT_REJECT) {
950 buffer_abort(req);
951 buffer_abort(s->rep);
952 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200953
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100954 s->be->be_counters.denied_req++;
955 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200956 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200957 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200958
Willy Tarreauedcf6682008-11-30 23:15:34 +0100959 if (!(s->flags & SN_ERR_MASK))
960 s->flags |= SN_ERR_PRXCOND;
961 if (!(s->flags & SN_FINST_MASK))
962 s->flags |= SN_FINST_R;
963 return 0;
964 }
Willy Tarreau56123282010-08-06 19:06:56 +0200965 else if (rule->action == TCP_ACT_TRK_SC1) {
966 if (!s->stkctr1_entry) {
967 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200968 * Also, note that right now we can only track SRC so we
969 * don't check how to get the key, but later we may need
970 * to consider rule->act_prm->trk_ctr.type.
971 */
972 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100973 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200974 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200975 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200976 if (s->fe != s->be)
977 s->flags |= SN_BE_TRACK_SC1;
978 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200979 }
980 }
Willy Tarreau56123282010-08-06 19:06:56 +0200981 else if (rule->action == TCP_ACT_TRK_SC2) {
982 if (!s->stkctr2_entry) {
983 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200984 * Also, note that right now we can only track SRC so we
985 * don't check how to get the key, but later we may need
986 * to consider rule->act_prm->trk_ctr.type.
987 */
988 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100989 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200990 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200991 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200992 if (s->fe != s->be)
993 s->flags |= SN_BE_TRACK_SC2;
994 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200995 }
996 }
997 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100998 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200999 break;
1000 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001001 }
1002 }
1003
1004 /* if we get there, it means we have no rule which matches, or
1005 * we have an explicit accept, so we apply the default accept.
1006 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001007 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001008 req->analyse_exp = TICK_ETERNITY;
1009 return 1;
1010}
1011
Emeric Brun97679e72010-09-23 17:56:44 +02001012/* This function performs the TCP response analysis on the current response. It
1013 * returns 1 if the processing can continue on next analysers, or zero if it
1014 * needs more data, encounters an error, or wants to immediately abort the
1015 * response. It relies on buffers flags, and updates s->rep->analysers. The
1016 * function may be called for backend rules.
1017 */
1018int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit)
1019{
1020 struct tcp_rule *rule;
1021 int partial;
1022
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001023 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 +02001024 now_ms, __FUNCTION__,
1025 s,
1026 rep,
1027 rep->rex, rep->wex,
1028 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001029 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001030 rep->analysers);
1031
1032 /* We don't know whether we have enough data, so must proceed
1033 * this way :
1034 * - iterate through all rules in their declaration order
1035 * - if one rule returns MISS, it means the inspect delay is
1036 * not over yet, then return immediately, otherwise consider
1037 * it as a non-match.
1038 * - if one rule returns OK, then return OK
1039 * - if one rule returns KO, then return KO
1040 */
1041
1042 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001043 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001044 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001045 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001046
1047 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
1048 int ret = ACL_PAT_PASS;
1049
1050 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001051 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +02001052 if (ret == ACL_PAT_MISS) {
1053 /* just set the analyser timeout once at the beginning of the response */
1054 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
1055 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
1056 return 0;
1057 }
1058
1059 ret = acl_pass(ret);
1060 if (rule->cond->pol == ACL_COND_UNLESS)
1061 ret = !ret;
1062 }
1063
1064 if (ret) {
1065 /* we have a matching rule. */
1066 if (rule->action == TCP_ACT_REJECT) {
1067 buffer_abort(rep);
1068 buffer_abort(s->req);
1069 rep->analysers = 0;
1070
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001071 s->be->be_counters.denied_resp++;
1072 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001073 if (s->listener->counters)
1074 s->listener->counters->denied_resp++;
1075
1076 if (!(s->flags & SN_ERR_MASK))
1077 s->flags |= SN_ERR_PRXCOND;
1078 if (!(s->flags & SN_FINST_MASK))
1079 s->flags |= SN_FINST_D;
1080 return 0;
1081 }
1082 else {
1083 /* otherwise accept */
1084 break;
1085 }
1086 }
1087 }
1088
1089 /* if we get there, it means we have no rule which matches, or
1090 * we have an explicit accept, so we apply the default accept.
1091 */
1092 rep->analysers &= ~an_bit;
1093 rep->analyse_exp = TICK_ETERNITY;
1094 return 1;
1095}
1096
1097
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001098/* This function performs the TCP layer4 analysis on the current request. It
1099 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1100 * matches or if no more rule matches. It can only use rules which don't need
1101 * any data.
1102 */
1103int tcp_exec_req_rules(struct session *s)
1104{
1105 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001106 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001107 struct stktable *t = NULL;
1108 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001109 int ret;
1110
1111 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1112 ret = ACL_PAT_PASS;
1113
1114 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001115 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001116 ret = acl_pass(ret);
1117 if (rule->cond->pol == ACL_COND_UNLESS)
1118 ret = !ret;
1119 }
1120
1121 if (ret) {
1122 /* we have a matching rule. */
1123 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001124 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001125 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001126 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001127
1128 if (!(s->flags & SN_ERR_MASK))
1129 s->flags |= SN_ERR_PRXCOND;
1130 if (!(s->flags & SN_FINST_MASK))
1131 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001132 result = 0;
1133 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001134 }
Willy Tarreau56123282010-08-06 19:06:56 +02001135 else if (rule->action == TCP_ACT_TRK_SC1) {
1136 if (!s->stkctr1_entry) {
1137 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001138 * Also, note that right now we can only track SRC so we
1139 * don't check how to get the key, but later we may need
1140 * to consider rule->act_prm->trk_ctr.type.
1141 */
1142 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001143 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001144 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001145 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001146 }
1147 }
Willy Tarreau56123282010-08-06 19:06:56 +02001148 else if (rule->action == TCP_ACT_TRK_SC2) {
1149 if (!s->stkctr2_entry) {
1150 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001151 * Also, note that right now we can only track SRC so we
1152 * don't check how to get the key, but later we may need
1153 * to consider rule->act_prm->trk_ctr.type.
1154 */
1155 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001156 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001157 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001158 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001159 }
1160 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001161 else {
1162 /* otherwise it's an accept */
1163 break;
1164 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001165 }
1166 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001167 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001168}
1169
Emeric Brun97679e72010-09-23 17:56:44 +02001170/* Parse a tcp-response rule. Return a negative value in case of failure */
1171static int tcp_parse_response_rule(char **args, int arg, int section_type,
1172 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001173 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001174{
1175 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001176 memprintf(err, "%s %s is only allowed in 'backend' sections",
1177 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001178 return -1;
1179 }
1180
1181 if (strcmp(args[arg], "accept") == 0) {
1182 arg++;
1183 rule->action = TCP_ACT_ACCEPT;
1184 }
1185 else if (strcmp(args[arg], "reject") == 0) {
1186 arg++;
1187 rule->action = TCP_ACT_REJECT;
1188 }
1189 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001190 memprintf(err,
1191 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1192 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001193 return -1;
1194 }
1195
1196 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001197 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1198 memprintf(err,
1199 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1200 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001201 return -1;
1202 }
1203 }
1204 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001205 memprintf(err,
1206 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001207 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1208 return -1;
1209 }
1210 return 0;
1211}
1212
1213
1214
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001215/* Parse a tcp-request rule. Return a negative value in case of failure */
1216static int tcp_parse_request_rule(char **args, int arg, int section_type,
1217 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001218 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001219{
1220 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001221 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1222 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001223 return -1;
1224 }
1225
1226 if (!strcmp(args[arg], "accept")) {
1227 arg++;
1228 rule->action = TCP_ACT_ACCEPT;
1229 }
1230 else if (!strcmp(args[arg], "reject")) {
1231 arg++;
1232 rule->action = TCP_ACT_REJECT;
1233 }
Willy Tarreau56123282010-08-06 19:06:56 +02001234 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001235 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001236 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001237
1238 arg++;
1239 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001240 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001241
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001242 if (ret < 0) { /* nb: warnings are not handled yet */
1243 memprintf(err,
1244 "'%s %s %s' : %s in %s '%s'",
1245 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1246 return ret;
1247 }
Willy Tarreau56123282010-08-06 19:06:56 +02001248 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001249 }
Willy Tarreau56123282010-08-06 19:06:56 +02001250 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001251 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001252 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001253
1254 arg++;
1255 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001256 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001257
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001258 if (ret < 0) { /* nb: warnings are not handled yet */
1259 memprintf(err,
1260 "'%s %s %s' : %s in %s '%s'",
1261 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1262 return ret;
1263 }
Willy Tarreau56123282010-08-06 19:06:56 +02001264 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001265 }
1266 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001267 memprintf(err,
1268 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1269 "or 'track-sc2' in %s '%s' (got '%s')",
1270 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001271 return -1;
1272 }
1273
1274 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001275 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1276 memprintf(err,
1277 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1278 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001279 return -1;
1280 }
1281 }
1282 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001283 memprintf(err,
1284 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001285 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1286 return -1;
1287 }
1288 return 0;
1289}
1290
Emeric Brun97679e72010-09-23 17:56:44 +02001291/* This function should be called to parse a line starting with the "tcp-response"
1292 * keyword.
1293 */
1294static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001295 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001296{
1297 const char *ptr = NULL;
1298 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001299 int warn = 0;
1300 int arg;
1301 struct tcp_rule *rule;
1302
1303 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001304 memprintf(err, "missing argument for '%s' in %s '%s'",
1305 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001306 return -1;
1307 }
1308
1309 if (strcmp(args[1], "inspect-delay") == 0) {
1310 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001311 memprintf(err, "%s %s is only allowed in 'backend' sections",
1312 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001313 return -1;
1314 }
1315
1316 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001317 memprintf(err,
1318 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1319 args[0], args[1], proxy_type_str(curpx), curpx->id);
1320 if (ptr)
1321 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001322 return -1;
1323 }
1324
1325 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001326 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1327 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001328 return 1;
1329 }
1330 curpx->tcp_rep.inspect_delay = val;
1331 return 0;
1332 }
1333
Simon Hormande072bd2011-06-24 15:11:37 +09001334 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001335 LIST_INIT(&rule->list);
1336 arg = 1;
1337
1338 if (strcmp(args[1], "content") == 0) {
1339 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001340 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001341 goto error;
1342
1343 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1344 struct acl *acl;
1345 const char *name;
1346
1347 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1348 name = acl ? acl->name : "(unknown)";
1349
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001350 memprintf(err,
1351 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1352 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001353 warn++;
1354 }
1355
1356 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1357 }
1358 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001359 memprintf(err,
1360 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1361 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001362 goto error;
1363 }
1364
1365 return warn;
1366 error:
1367 free(rule);
1368 return -1;
1369}
1370
1371
Willy Tarreaub6866442008-07-14 23:54:42 +02001372/* This function should be called to parse a line starting with the "tcp-request"
1373 * keyword.
1374 */
1375static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001376 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001377{
1378 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001379 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001380 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001381 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001382 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001383
1384 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001385 if (curpx == defpx)
1386 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1387 else
1388 memprintf(err, "missing argument for '%s' in %s '%s'",
1389 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001390 return -1;
1391 }
1392
1393 if (!strcmp(args[1], "inspect-delay")) {
1394 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001395 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1396 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001397 return -1;
1398 }
1399
Willy Tarreaub6866442008-07-14 23:54:42 +02001400 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001401 memprintf(err,
1402 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1403 args[0], args[1], proxy_type_str(curpx), curpx->id);
1404 if (ptr)
1405 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001406 return -1;
1407 }
1408
1409 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001410 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1411 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001412 return 1;
1413 }
1414 curpx->tcp_req.inspect_delay = val;
1415 return 0;
1416 }
1417
Simon Hormande072bd2011-06-24 15:11:37 +09001418 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001419 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001420 arg = 1;
1421
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001422 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001423 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001424 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001425 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001426
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001427 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001428 struct acl *acl;
1429 const char *name;
1430
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001431 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001432 name = acl ? acl->name : "(unknown)";
1433
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001434 memprintf(err,
1435 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1436 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001437 warn++;
1438 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001439 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001440 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001441 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001442 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001443
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001444 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001445 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1446 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001447 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001448 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001449
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001450 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001451 goto error;
1452
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001453 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1454 struct acl *acl;
1455 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001456
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001457 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1458 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001459
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001460 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001461 memprintf(err,
1462 "'%s %s' may not reference acl '%s' which makes use of "
1463 "payload in %s '%s'. Please use '%s content' for this.",
1464 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001465 goto error;
1466 }
1467 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001468 memprintf(err,
1469 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1470 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001471 warn++;
1472 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001473 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001474 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001475 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001476 if (curpx == defpx)
1477 memprintf(err,
1478 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1479 args[0], args[1]);
1480 else
1481 memprintf(err,
1482 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1483 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001484 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001485 }
1486
Willy Tarreau1a687942010-05-23 22:40:30 +02001487 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001488 error:
1489 free(rule);
1490 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001491}
1492
Willy Tarreau645513a2010-05-24 20:55:15 +02001493
1494/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001495/* All supported sample fetch functios must be declared here */
1496/************************************************************************/
1497
1498/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001499 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001500 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1501 * is a string (cookie name), other types will lead to undefined behaviour.
1502 */
1503int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001504smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001505 const struct arg *args, struct sample *smp)
1506{
1507 int bleft;
1508 const unsigned char *data;
1509
1510 if (!l4 || !l4->req)
1511 return 0;
1512
1513 smp->flags = 0;
1514 smp->type = SMP_T_CSTR;
1515
1516 bleft = l4->req->i;
1517 if (bleft <= 11)
1518 goto too_short;
1519
1520 data = (const unsigned char *)l4->req->p + 11;
1521 bleft -= 11;
1522
1523 if (bleft <= 7)
1524 goto too_short;
1525
1526 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1527 goto not_cookie;
1528
1529 data += 7;
1530 bleft -= 7;
1531
1532 while (bleft > 0 && *data == ' ') {
1533 data++;
1534 bleft--;
1535 }
1536
1537 if (args) {
1538
1539 if (bleft <= args->data.str.len)
1540 goto too_short;
1541
1542 if ((data[args->data.str.len] != '=') ||
1543 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1544 goto not_cookie;
1545
1546 data += args->data.str.len + 1;
1547 bleft -= args->data.str.len + 1;
1548 } else {
1549 while (bleft > 0 && *data != '=') {
1550 if (*data == '\r' || *data == '\n')
1551 goto not_cookie;
1552 data++;
1553 bleft--;
1554 }
1555
1556 if (bleft < 1)
1557 goto too_short;
1558
1559 if (*data != '=')
1560 goto not_cookie;
1561
1562 data++;
1563 bleft--;
1564 }
1565
1566 /* data points to cookie value */
1567 smp->data.str.str = (char *)data;
1568 smp->data.str.len = 0;
1569
1570 while (bleft > 0 && *data != '\r') {
1571 data++;
1572 bleft--;
1573 }
1574
1575 if (bleft < 2)
1576 goto too_short;
1577
1578 if (data[0] != '\r' || data[1] != '\n')
1579 goto not_cookie;
1580
1581 smp->data.str.len = (char *)data - smp->data.str.str;
1582 smp->flags = SMP_F_VOLATILE;
1583 return 1;
1584
1585 too_short:
1586 smp->flags = SMP_F_MAY_CHANGE;
1587 not_cookie:
1588 return 0;
1589}
1590
Willy Tarreau32389b72012-04-23 23:13:20 +02001591/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001592/* All supported ACL keywords must be declared here. */
1593/************************************************************************/
1594
Willy Tarreau32389b72012-04-23 23:13:20 +02001595/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1596static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001597acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001598 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001599{
1600 int ret;
1601
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001602 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001603
1604 if (smp->flags & SMP_F_MAY_CHANGE)
1605 return 0;
1606
1607 smp->flags = SMP_F_VOLATILE;
1608 smp->type = SMP_T_UINT;
1609 smp->data.uint = ret;
1610 return 1;
1611}
1612
1613
Willy Tarreau4a129812012-04-25 17:31:42 +02001614/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001615static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001616smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001617 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001618{
Willy Tarreauf4362b32011-12-16 17:49:52 +01001619 switch (l4->si[0].addr.from.ss_family) {
1620 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001621 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.from)->sin_addr;
1622 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001623 break;
1624 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001625 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.from))->sin6_addr;
1626 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001627 break;
1628 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001629 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001630 }
Emeric Brunf769f512010-10-22 17:14:01 +02001631
Willy Tarreau37406352012-04-23 16:16:37 +02001632 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001633 return 1;
1634}
1635
Willy Tarreaua5e37562011-12-16 17:06:15 +01001636/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001637static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001638smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001639 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001640{
Willy Tarreauf853c462012-04-23 18:53:56 +02001641 smp->type = SMP_T_UINT;
1642 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001643 return 0;
1644
Willy Tarreau37406352012-04-23 16:16:37 +02001645 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001646 return 1;
1647}
1648
Willy Tarreau4a129812012-04-25 17:31:42 +02001649/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001650static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001651smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001652 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001653{
Willy Tarreau59b94792012-05-11 16:16:40 +02001654 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001655
Willy Tarreauf4362b32011-12-16 17:49:52 +01001656 switch (l4->si[0].addr.to.ss_family) {
1657 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001658 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.to)->sin_addr;
1659 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001660 break;
1661 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001662 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.to))->sin6_addr;
1663 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001664 break;
1665 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001666 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001667 }
Emeric Brunf769f512010-10-22 17:14:01 +02001668
Willy Tarreau37406352012-04-23 16:16:37 +02001669 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001670 return 1;
1671}
1672
Willy Tarreaua5e37562011-12-16 17:06:15 +01001673/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001674static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001675smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001676 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001677{
Willy Tarreau59b94792012-05-11 16:16:40 +02001678 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001679
Willy Tarreauf853c462012-04-23 18:53:56 +02001680 smp->type = SMP_T_UINT;
1681 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001682 return 0;
1683
Willy Tarreau37406352012-04-23 16:16:37 +02001684 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001685 return 1;
1686}
1687
1688static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001689smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1690 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001691{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001692 unsigned int len_offset = arg_p[0].data.uint;
1693 unsigned int len_size = arg_p[1].data.uint;
1694 unsigned int buf_offset;
1695 unsigned int buf_size = 0;
Emericf2d7cae2010-11-05 18:13:50 +01001696 struct buffer *b;
1697 int i;
1698
1699 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1700 /* by default buf offset == len offset + len size */
1701 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1702
1703 if (!l4)
1704 return 0;
1705
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001706 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001707
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001708 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001709 return 0;
1710
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001711 if (len_offset + len_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001712 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001713
1714 for (i = 0; i < len_size; i++) {
Willy Tarreau89fa7062012-03-02 16:13:16 +01001715 buf_size = (buf_size << 8) + ((unsigned char *)b->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001716 }
1717
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001718 /* buf offset may be implicit, absolute or relative */
1719 buf_offset = len_offset + len_size;
1720 if (arg_p[2].type == ARGT_UINT)
1721 buf_offset = arg_p[2].data.uint;
1722 else if (arg_p[2].type == ARGT_SINT)
1723 buf_offset += arg_p[2].data.sint;
1724
Willy Tarreau82ea8002012-04-25 19:19:43 +02001725 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1726 /* will never match */
1727 smp->flags = 0;
1728 return 0;
1729 }
1730
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001731 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001732 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001733
1734 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001735 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001736 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001737 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001738 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001739
1740 too_short:
1741 smp->flags = SMP_F_MAY_CHANGE;
1742 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001743}
1744
1745static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001746smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1747 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001748{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001749 unsigned int buf_offset = arg_p[0].data.uint;
1750 unsigned int buf_size = arg_p[1].data.uint;
Emericf2d7cae2010-11-05 18:13:50 +01001751 struct buffer *b;
1752
1753 if (!l4)
1754 return 0;
1755
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001756 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001757
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001758 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001759 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001760
1761 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1762 /* will never match */
1763 smp->flags = 0;
1764 return 0;
1765 }
Emericf2d7cae2010-11-05 18:13:50 +01001766
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001767 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001768 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001769
1770 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001771 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001772 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001773 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001774 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001775
1776 too_short:
1777 smp->flags = SMP_F_MAY_CHANGE;
1778 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001779}
1780
Willy Tarreau21d68a62012-04-20 15:52:36 +02001781/* This function is used to validate the arguments passed to a "payload" fetch
1782 * keyword. This keyword expects two positive integers, with the second one
1783 * being strictly positive. It is assumed that the types are already the correct
1784 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1785 * filled with a pointer to an error message in case of error, that the caller
1786 * is responsible for freeing. The initial location must either be freeable or
1787 * NULL.
1788 */
1789static int val_payload(struct arg *arg, char **err_msg)
1790{
1791 if (!arg[1].data.uint) {
1792 if (err_msg)
1793 memprintf(err_msg, "payload length must be > 0");
1794 return 0;
1795 }
1796 return 1;
1797}
1798
1799/* This function is used to validate the arguments passed to a "payload_lv" fetch
1800 * keyword. This keyword allows two positive integers and an optional signed one,
1801 * with the second one being strictly positive and the third one being greater than
1802 * the opposite of the two others if negative. It is assumed that the types are
1803 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1804 * not NULL, it will be filled with a pointer to an error message in case of
1805 * error, that the caller is responsible for freeing. The initial location must
1806 * either be freeable or NULL.
1807 */
1808static int val_payload_lv(struct arg *arg, char **err_msg)
1809{
1810 if (!arg[1].data.uint) {
1811 if (err_msg)
1812 memprintf(err_msg, "payload length must be > 0");
1813 return 0;
1814 }
1815
1816 if (arg[2].type == ARGT_SINT &&
1817 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1818 if (err_msg)
1819 memprintf(err_msg, "payload offset too negative");
1820 return 0;
1821 }
1822 return 1;
1823}
1824
Willy Tarreaub6866442008-07-14 23:54:42 +02001825static struct cfg_kw_list cfg_kws = {{ },{
1826 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001827 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001828 { 0, NULL, NULL },
1829}};
1830
Willy Tarreau61612d42012-04-19 18:42:05 +02001831/* Note: must not be declared <const> as its list will be overwritten.
1832 * Please take care of keeping this list alphabetically sorted.
1833 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001834static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001835 { "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 +02001836 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001837 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1838 { "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 +02001839 { "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 +02001840 { "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 +02001841 { "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 +02001842 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001843 { NULL, NULL, NULL, NULL },
1844}};
1845
Willy Tarreau4a129812012-04-25 17:31:42 +02001846/* Note: must not be declared <const> as its list will be overwritten.
1847 * Note: fetches that may return multiple types must be declared as the lowest
1848 * common denominator, the type that can be casted into all other ones. For
1849 * instance v4/v6 must be declared v4.
1850 */
Willy Tarreau12785782012-04-27 21:37:17 +02001851static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001852 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001853 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001854 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001855 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1856 { "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 +02001857 { "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 +02001858 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001859 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001860}};
1861
Willy Tarreaue6b98942007-10-29 01:09:36 +01001862__attribute__((constructor))
1863static void __tcp_protocol_init(void)
1864{
1865 protocol_register(&proto_tcpv4);
1866 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001867 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001868 cfg_register_keywords(&cfg_kws);
1869 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001870}
1871
1872
1873/*
1874 * Local variables:
1875 * c-indent-level: 8
1876 * c-basic-offset: 8
1877 * End:
1878 */