blob: 87ac849ce631acdab1a3da364eeeb78f8054142b [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 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 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{
Willy Tarreau80184712012-07-06 14:54:49 +0200539 struct connection *conn = fdtab[fd].owner;
540 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200541 struct buffer *b = si->ob;
Willy Tarreaua190d592012-05-20 18:35:19 +0200542 int retval = 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200543
Willy Tarreau80184712012-07-06 14:54:49 +0200544 if (conn->flags & CO_FL_ERROR)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200545 goto out_error;
546
Willy Tarreau80184712012-07-06 14:54:49 +0200547 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200548 goto out_ignore; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200549
550 /* we might have been called just after an asynchronous shutw */
551 if (b->flags & BF_SHUTW)
552 goto out_wakeup;
553
Willy Tarreaua190d592012-05-20 18:35:19 +0200554 /* If we have a PROXY line to send, we'll use this to validate the
555 * connection, in which case the connection is validated only once
556 * we've sent the whole proxy line. Otherwise we use connect().
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200557 */
Willy Tarreaua190d592012-05-20 18:35:19 +0200558 if (si->send_proxy_ofs) {
559 int ret;
560
561 /* The target server expects a PROXY line to be sent first.
562 * If the send_proxy_ofs is negative, it corresponds to the
563 * offset to start sending from then end of the proxy string
564 * (which is recomputed every time since it's constant). If
565 * it is positive, it means we have to send from the start.
566 */
567 ret = make_proxy_line(trash, trashlen, &b->prod->addr.from, &b->prod->addr.to);
568 if (!ret)
569 goto out_error;
570
571 if (si->send_proxy_ofs > 0)
572 si->send_proxy_ofs = -ret; /* first call */
573
574 /* we have to send trash from (ret+sp for -sp bytes) */
Willy Tarreaufb7508a2012-05-21 16:47:54 +0200575 ret = send(fd, trash + ret + si->send_proxy_ofs, -si->send_proxy_ofs,
Willy Tarreaua190d592012-05-20 18:35:19 +0200576 (b->flags & BF_OUT_EMPTY) ? 0 : MSG_MORE);
577
578 if (ret == 0)
579 goto out_ignore;
580
581 if (ret < 0) {
582 if (errno == EAGAIN)
583 goto out_ignore;
584 goto out_error;
585 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200586
Willy Tarreaua190d592012-05-20 18:35:19 +0200587 si->send_proxy_ofs += ret; /* becomes zero once complete */
588 if (si->send_proxy_ofs != 0)
589 goto out_ignore;
590
591 /* OK we've sent the whole line, we're connected */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200592 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200593 else {
594 /* We have no data to send to check the connection, and
595 * getsockopt() will not inform us whether the connection
596 * is still pending. So we'll reuse connect() to check the
597 * state of the socket. This has the advantage of giving us
598 * the following info :
599 * - error
600 * - connecting (EALREADY, EINPROGRESS)
601 * - connected (EISCONN, 0)
602 */
Willy Tarreau80184712012-07-06 14:54:49 +0200603 if ((connect(fd, conn->peeraddr, conn->peerlen) < 0)) {
Willy Tarreaua190d592012-05-20 18:35:19 +0200604 if (errno == EALREADY || errno == EINPROGRESS)
605 goto out_ignore;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200606
Willy Tarreaua190d592012-05-20 18:35:19 +0200607 if (errno && errno != EISCONN)
608 goto out_error;
609
610 /* otherwise we're connected */
611 }
612 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200613
614 /* OK we just need to indicate that we got a connection
615 * and that we wrote nothing.
616 */
617 b->flags |= BF_WRITE_NULL;
618
Willy Tarreaua190d592012-05-20 18:35:19 +0200619 /* The FD is ready now, we can hand the handlers to the socket layer
620 * and forward the event there to start working on the socket.
621 */
Willy Tarreaud2274c62012-07-06 14:29:45 +0200622 fdtab[fd].cb[DIR_RD].f = NULL;
623 fdtab[fd].cb[DIR_WR].f = NULL;
Willy Tarreau80184712012-07-06 14:54:49 +0200624 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau8ae52cb2012-05-20 10:38:46 +0200625 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200626 return si_data(si)->write(fd);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200627
628 out_wakeup:
629 task_wakeup(si->owner, TASK_WOKEN_IO);
630
631 out_ignore:
632 fdtab[fd].ev &= ~FD_POLL_OUT;
633 return retval;
634
635 out_error:
636 /* Write error on the file descriptor. We mark the FD as STERROR so
637 * that we don't use it anymore. The error is reported to the stream
638 * interface which will take proper action. We must not perturbate the
639 * buffer because the stream interface wants to ensure transparent
640 * connection retries.
641 */
642
Willy Tarreau80184712012-07-06 14:54:49 +0200643 conn->flags |= CO_FL_ERROR;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200644 fdtab[fd].ev &= ~FD_POLL_STICKY;
645 EV_FD_REM(fd);
646 si->flags |= SI_FL_ERR;
Willy Tarreaua190d592012-05-20 18:35:19 +0200647 retval = 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200648 goto out_wakeup;
649}
650
651
652/* might be used on connect error */
653static int tcp_connect_read(int fd)
654{
Willy Tarreau80184712012-07-06 14:54:49 +0200655 struct connection *conn = fdtab[fd].owner;
656 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200657 int retval;
658
659 retval = 1;
660
Willy Tarreau80184712012-07-06 14:54:49 +0200661 if (conn->flags & CO_FL_ERROR)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200662 goto out_error;
663
Willy Tarreau80184712012-07-06 14:54:49 +0200664 if (!(conn->flags & CO_FL_WAIT_L4_CONN)) {
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200665 retval = 0;
666 goto out_ignore; /* strange we were called while ready */
667 }
668
669 /* stop here if we reached the end of data */
670 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
671 goto out_error;
672
673 out_wakeup:
674 task_wakeup(si->owner, TASK_WOKEN_IO);
675 out_ignore:
676 fdtab[fd].ev &= ~FD_POLL_IN;
677 return retval;
678
679 out_error:
680 /* Read error on the file descriptor. We mark the FD as STERROR so
681 * that we don't use it anymore. The error is reported to the stream
682 * interface which will take proper action. We must not perturbate the
683 * buffer because the stream interface wants to ensure transparent
684 * connection retries.
685 */
686
Willy Tarreau80184712012-07-06 14:54:49 +0200687 conn->flags |= CO_FL_ERROR;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200688 fdtab[fd].ev &= ~FD_POLL_STICKY;
689 EV_FD_REM(fd);
690 si->flags |= SI_FL_ERR;
691 goto out_wakeup;
692}
693
Willy Tarreau59b94792012-05-11 16:16:40 +0200694
Willy Tarreaue6b98942007-10-29 01:09:36 +0100695/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
696 * an error message in <err> if the message is at most <errlen> bytes long
697 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
698 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
699 * was alright and that no message was returned. ERR_RETRYABLE means that an
700 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700701 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100702 * the meaning of the error, but just indicate that a message is present which
703 * should be displayed with the respective level. Last, ERR_ABORT indicates
704 * that it's pointless to try to start other listeners. No error message is
705 * returned if errlen is NULL.
706 */
707int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
708{
709 __label__ tcp_return, tcp_close_return;
710 int fd, err;
711 const char *msg = NULL;
712
713 /* ensure we never return garbage */
714 if (errmsg && errlen)
715 *errmsg = 0;
716
717 if (listener->state != LI_ASSIGNED)
718 return ERR_NONE; /* already bound */
719
720 err = ERR_NONE;
721
722 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
723 err |= ERR_RETRYABLE | ERR_ALERT;
724 msg = "cannot create listening socket";
725 goto tcp_return;
726 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100727
Willy Tarreaue6b98942007-10-29 01:09:36 +0100728 if (fd >= global.maxsock) {
729 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
730 msg = "not enough free sockets (raise '-n' parameter)";
731 goto tcp_close_return;
732 }
733
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200734 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100735 err |= ERR_FATAL | ERR_ALERT;
736 msg = "cannot make socket non-blocking";
737 goto tcp_close_return;
738 }
739
Simon Hormande072bd2011-06-24 15:11:37 +0900740 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100741 /* not fatal but should be reported */
742 msg = "cannot do so_reuseaddr";
743 err |= ERR_ALERT;
744 }
745
746 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900747 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100748
Willy Tarreaue6b98942007-10-29 01:09:36 +0100749#ifdef SO_REUSEPORT
750 /* OpenBSD supports this. As it's present in old libc versions of Linux,
751 * it might return an error that we will silently ignore.
752 */
Simon Hormande072bd2011-06-24 15:11:37 +0900753 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100754#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100755#ifdef CONFIG_HAP_LINUX_TPROXY
David du Colombier65c17962012-07-13 14:34:59 +0200756 if (listener->options & LI_O_FOREIGN) {
757 switch (listener->addr.ss_family) {
758 case AF_INET:
759 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
760 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
761 msg = "cannot make listening socket transparent";
762 err |= ERR_ALERT;
763 }
764 break;
765 case AF_INET6:
766 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
767 msg = "cannot make listening socket transparent";
768 err |= ERR_ALERT;
769 }
770 break;
771 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100772 }
773#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100774#ifdef SO_BINDTODEVICE
775 /* Note: this might fail if not CAP_NET_RAW */
776 if (listener->interface) {
777 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100778 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100779 msg = "cannot bind listener to device";
780 err |= ERR_WARN;
781 }
782 }
783#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400784#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100785 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400786 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200787 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
788 msg = "cannot set MSS";
789 err |= ERR_WARN;
790 }
791 }
792#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200793#if defined(TCP_DEFER_ACCEPT)
794 if (listener->options & LI_O_DEF_ACCEPT) {
795 /* defer accept by up to one second */
796 int accept_delay = 1;
797 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
798 msg = "cannot enable DEFER_ACCEPT";
799 err |= ERR_WARN;
800 }
801 }
802#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100803 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
804 err |= ERR_RETRYABLE | ERR_ALERT;
805 msg = "cannot bind socket";
806 goto tcp_close_return;
807 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100808
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100809 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100810 err |= ERR_RETRYABLE | ERR_ALERT;
811 msg = "cannot listen to socket";
812 goto tcp_close_return;
813 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100814
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400815#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200816 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900817 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200818#endif
819
Willy Tarreaue6b98942007-10-29 01:09:36 +0100820 /* the socket is ready */
821 listener->fd = fd;
822 listener->state = LI_LISTEN;
823
Willy Tarreaueabf3132008-08-29 23:36:51 +0200824 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaueb472682010-05-28 18:46:57 +0200825 fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200826 fdtab[fd].iocb = listener->proto->accept;
827 fdtab[fd].cb[DIR_RD].f = NULL; /* never called */
Willy Tarreaueb472682010-05-28 18:46:57 +0200828 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
Willy Tarreaueb472682010-05-28 18:46:57 +0200829 fd_insert(fd);
830
Willy Tarreaue6b98942007-10-29 01:09:36 +0100831 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100832 if (msg && errlen) {
833 char pn[INET6_ADDRSTRLEN];
834
Willy Tarreau631f01c2011-09-05 00:36:48 +0200835 addr_to_str(&listener->addr, pn, sizeof(pn));
836 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100837 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100838 return err;
839
840 tcp_close_return:
841 close(fd);
842 goto tcp_return;
843}
844
845/* This function creates all TCP sockets bound to the protocol entry <proto>.
846 * It is intended to be used as the protocol's bind_all() function.
847 * The sockets will be registered but not added to any fd_set, in order not to
848 * loose them across the fork(). A call to enable_all_listeners() is needed
849 * to complete initialization. The return value is composed from ERR_*.
850 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200851static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100852{
853 struct listener *listener;
854 int err = ERR_NONE;
855
856 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200857 err |= tcp_bind_listener(listener, errmsg, errlen);
858 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100859 break;
860 }
861
862 return err;
863}
864
865/* Add listener to the list of tcpv4 listeners. The listener's state
866 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
867 * listeners is updated. This is the function to use to add a new listener.
868 */
869void tcpv4_add_listener(struct listener *listener)
870{
871 if (listener->state != LI_INIT)
872 return;
873 listener->state = LI_ASSIGNED;
874 listener->proto = &proto_tcpv4;
875 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
876 proto_tcpv4.nb_listeners++;
877}
878
879/* Add listener to the list of tcpv4 listeners. The listener's state
880 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
881 * listeners is updated. This is the function to use to add a new listener.
882 */
883void tcpv6_add_listener(struct listener *listener)
884{
885 if (listener->state != LI_INIT)
886 return;
887 listener->state = LI_ASSIGNED;
888 listener->proto = &proto_tcpv6;
889 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
890 proto_tcpv6.nb_listeners++;
891}
892
Willy Tarreauedcf6682008-11-30 23:15:34 +0100893/* This function performs the TCP request analysis on the current request. It
894 * returns 1 if the processing can continue on next analysers, or zero if it
895 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200896 * request. It relies on buffers flags, and updates s->req->analysers. The
897 * function may be called for frontend rules and backend rules. It only relies
898 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100899 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200900int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100901{
902 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200903 struct stksess *ts;
904 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100905 int partial;
906
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100907 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 +0100908 now_ms, __FUNCTION__,
909 s,
910 req,
911 req->rex, req->wex,
912 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100913 req->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100914 req->analysers);
915
Willy Tarreauedcf6682008-11-30 23:15:34 +0100916 /* We don't know whether we have enough data, so must proceed
917 * this way :
918 * - iterate through all rules in their declaration order
919 * - if one rule returns MISS, it means the inspect delay is
920 * not over yet, then return immediately, otherwise consider
921 * it as a non-match.
922 * - if one rule returns OK, then return OK
923 * - if one rule returns KO, then return KO
924 */
925
Willy Tarreaub824b002010-09-29 16:36:16 +0200926 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 +0200927 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100928 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200929 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100930
Willy Tarreaufb356202010-08-03 14:02:05 +0200931 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100932 int ret = ACL_PAT_PASS;
933
934 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200935 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100936 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200937 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100938 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200939 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
940 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100941 return 0;
942 }
943
944 ret = acl_pass(ret);
945 if (rule->cond->pol == ACL_COND_UNLESS)
946 ret = !ret;
947 }
948
949 if (ret) {
950 /* we have a matching rule. */
951 if (rule->action == TCP_ACT_REJECT) {
952 buffer_abort(req);
953 buffer_abort(s->rep);
954 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200955
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100956 s->be->be_counters.denied_req++;
957 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200958 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200959 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200960
Willy Tarreauedcf6682008-11-30 23:15:34 +0100961 if (!(s->flags & SN_ERR_MASK))
962 s->flags |= SN_ERR_PRXCOND;
963 if (!(s->flags & SN_FINST_MASK))
964 s->flags |= SN_FINST_R;
965 return 0;
966 }
Willy Tarreau56123282010-08-06 19:06:56 +0200967 else if (rule->action == TCP_ACT_TRK_SC1) {
968 if (!s->stkctr1_entry) {
969 /* only the first valid track-sc1 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200970 * Also, note that right now we can only track SRC so we
971 * don't check how to get the key, but later we may need
972 * to consider rule->act_prm->trk_ctr.type.
973 */
974 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100975 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200976 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200977 session_track_stkctr1(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200978 if (s->fe != s->be)
979 s->flags |= SN_BE_TRACK_SC1;
980 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200981 }
982 }
Willy Tarreau56123282010-08-06 19:06:56 +0200983 else if (rule->action == TCP_ACT_TRK_SC2) {
984 if (!s->stkctr2_entry) {
985 /* only the first valid track-sc2 directive applies.
Willy Tarreaud1f96522010-08-03 19:34:32 +0200986 * Also, note that right now we can only track SRC so we
987 * don't check how to get the key, but later we may need
988 * to consider rule->act_prm->trk_ctr.type.
989 */
990 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +0100991 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200992 if (ts) {
Willy Tarreau56123282010-08-06 19:06:56 +0200993 session_track_stkctr2(s, t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200994 if (s->fe != s->be)
995 s->flags |= SN_BE_TRACK_SC2;
996 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200997 }
998 }
999 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +01001000 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001001 break;
1002 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001003 }
1004 }
1005
1006 /* if we get there, it means we have no rule which matches, or
1007 * we have an explicit accept, so we apply the default accept.
1008 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001009 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001010 req->analyse_exp = TICK_ETERNITY;
1011 return 1;
1012}
1013
Emeric Brun97679e72010-09-23 17:56:44 +02001014/* This function performs the TCP response analysis on the current response. It
1015 * returns 1 if the processing can continue on next analysers, or zero if it
1016 * needs more data, encounters an error, or wants to immediately abort the
1017 * response. It relies on buffers flags, and updates s->rep->analysers. The
1018 * function may be called for backend rules.
1019 */
1020int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit)
1021{
1022 struct tcp_rule *rule;
1023 int partial;
1024
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001025 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 +02001026 now_ms, __FUNCTION__,
1027 s,
1028 rep,
1029 rep->rex, rep->wex,
1030 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001031 rep->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001032 rep->analysers);
1033
1034 /* We don't know whether we have enough data, so must proceed
1035 * this way :
1036 * - iterate through all rules in their declaration order
1037 * - if one rule returns MISS, it means the inspect delay is
1038 * not over yet, then return immediately, otherwise consider
1039 * it as a non-match.
1040 * - if one rule returns OK, then return OK
1041 * - if one rule returns KO, then return KO
1042 */
1043
1044 if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001045 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001046 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001047 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001048
1049 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
1050 int ret = ACL_PAT_PASS;
1051
1052 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001053 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +02001054 if (ret == ACL_PAT_MISS) {
1055 /* just set the analyser timeout once at the beginning of the response */
1056 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
1057 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
1058 return 0;
1059 }
1060
1061 ret = acl_pass(ret);
1062 if (rule->cond->pol == ACL_COND_UNLESS)
1063 ret = !ret;
1064 }
1065
1066 if (ret) {
1067 /* we have a matching rule. */
1068 if (rule->action == TCP_ACT_REJECT) {
1069 buffer_abort(rep);
1070 buffer_abort(s->req);
1071 rep->analysers = 0;
1072
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001073 s->be->be_counters.denied_resp++;
1074 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001075 if (s->listener->counters)
1076 s->listener->counters->denied_resp++;
1077
1078 if (!(s->flags & SN_ERR_MASK))
1079 s->flags |= SN_ERR_PRXCOND;
1080 if (!(s->flags & SN_FINST_MASK))
1081 s->flags |= SN_FINST_D;
1082 return 0;
1083 }
1084 else {
1085 /* otherwise accept */
1086 break;
1087 }
1088 }
1089 }
1090
1091 /* if we get there, it means we have no rule which matches, or
1092 * we have an explicit accept, so we apply the default accept.
1093 */
1094 rep->analysers &= ~an_bit;
1095 rep->analyse_exp = TICK_ETERNITY;
1096 return 1;
1097}
1098
1099
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001100/* This function performs the TCP layer4 analysis on the current request. It
1101 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1102 * matches or if no more rule matches. It can only use rules which don't need
1103 * any data.
1104 */
1105int tcp_exec_req_rules(struct session *s)
1106{
1107 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001108 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001109 struct stktable *t = NULL;
1110 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001111 int ret;
1112
1113 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1114 ret = ACL_PAT_PASS;
1115
1116 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001117 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001118 ret = acl_pass(ret);
1119 if (rule->cond->pol == ACL_COND_UNLESS)
1120 ret = !ret;
1121 }
1122
1123 if (ret) {
1124 /* we have a matching rule. */
1125 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001126 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001127 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001128 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001129
1130 if (!(s->flags & SN_ERR_MASK))
1131 s->flags |= SN_ERR_PRXCOND;
1132 if (!(s->flags & SN_FINST_MASK))
1133 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001134 result = 0;
1135 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001136 }
Willy Tarreau56123282010-08-06 19:06:56 +02001137 else if (rule->action == TCP_ACT_TRK_SC1) {
1138 if (!s->stkctr1_entry) {
1139 /* only the first valid track-sc1 directive applies.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001140 * Also, note that right now we can only track SRC so we
1141 * don't check how to get the key, but later we may need
1142 * to consider rule->act_prm->trk_ctr.type.
1143 */
1144 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001145 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001146 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001147 session_track_stkctr1(s, t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001148 }
1149 }
Willy Tarreau56123282010-08-06 19:06:56 +02001150 else if (rule->action == TCP_ACT_TRK_SC2) {
1151 if (!s->stkctr2_entry) {
1152 /* only the first valid track-sc2 directive applies.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001153 * Also, note that right now we can only track SRC so we
1154 * don't check how to get the key, but later we may need
1155 * to consider rule->act_prm->trk_ctr.type.
1156 */
1157 t = rule->act_prm.trk_ctr.table.t;
David du Colombier4f92d322011-03-24 11:09:31 +01001158 ts = stktable_get_entry(t, tcp_src_to_stktable_key(s));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001159 if (ts)
Willy Tarreau56123282010-08-06 19:06:56 +02001160 session_track_stkctr2(s, t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001161 }
1162 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001163 else {
1164 /* otherwise it's an accept */
1165 break;
1166 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001167 }
1168 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001169 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001170}
1171
Emeric Brun97679e72010-09-23 17:56:44 +02001172/* Parse a tcp-response rule. Return a negative value in case of failure */
1173static int tcp_parse_response_rule(char **args, int arg, int section_type,
1174 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001175 struct tcp_rule *rule, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001176{
1177 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001178 memprintf(err, "%s %s is only allowed in 'backend' sections",
1179 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001180 return -1;
1181 }
1182
1183 if (strcmp(args[arg], "accept") == 0) {
1184 arg++;
1185 rule->action = TCP_ACT_ACCEPT;
1186 }
1187 else if (strcmp(args[arg], "reject") == 0) {
1188 arg++;
1189 rule->action = TCP_ACT_REJECT;
1190 }
1191 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001192 memprintf(err,
1193 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1194 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001195 return -1;
1196 }
1197
1198 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001199 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1200 memprintf(err,
1201 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1202 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001203 return -1;
1204 }
1205 }
1206 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001207 memprintf(err,
1208 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001209 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1210 return -1;
1211 }
1212 return 0;
1213}
1214
1215
1216
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001217/* Parse a tcp-request rule. Return a negative value in case of failure */
1218static int tcp_parse_request_rule(char **args, int arg, int section_type,
1219 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001220 struct tcp_rule *rule, char **err)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001221{
1222 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001223 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1224 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001225 return -1;
1226 }
1227
1228 if (!strcmp(args[arg], "accept")) {
1229 arg++;
1230 rule->action = TCP_ACT_ACCEPT;
1231 }
1232 else if (!strcmp(args[arg], "reject")) {
1233 arg++;
1234 rule->action = TCP_ACT_REJECT;
1235 }
Willy Tarreau56123282010-08-06 19:06:56 +02001236 else if (strcmp(args[arg], "track-sc1") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001237 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001238 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001239
1240 arg++;
1241 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001242 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001243
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001244 if (ret < 0) { /* nb: warnings are not handled yet */
1245 memprintf(err,
1246 "'%s %s %s' : %s in %s '%s'",
1247 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1248 return ret;
1249 }
Willy Tarreau56123282010-08-06 19:06:56 +02001250 rule->action = TCP_ACT_TRK_SC1;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001251 }
Willy Tarreau56123282010-08-06 19:06:56 +02001252 else if (strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001253 int ret;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001254 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001255
1256 arg++;
1257 ret = parse_track_counters(args, &arg, section_type, curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001258 &rule->act_prm.trk_ctr, defpx, err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001259
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001260 if (ret < 0) { /* nb: warnings are not handled yet */
1261 memprintf(err,
1262 "'%s %s %s' : %s in %s '%s'",
1263 args[0], args[1], args[kw], *err, proxy_type_str(curpx), curpx->id);
1264 return ret;
1265 }
Willy Tarreau56123282010-08-06 19:06:56 +02001266 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001267 }
1268 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001269 memprintf(err,
1270 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1271 "or 'track-sc2' in %s '%s' (got '%s')",
1272 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001273 return -1;
1274 }
1275
1276 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001277 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1278 memprintf(err,
1279 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1280 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001281 return -1;
1282 }
1283 }
1284 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001285 memprintf(err,
1286 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001287 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1288 return -1;
1289 }
1290 return 0;
1291}
1292
Emeric Brun97679e72010-09-23 17:56:44 +02001293/* This function should be called to parse a line starting with the "tcp-response"
1294 * keyword.
1295 */
1296static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001297 struct proxy *defpx, char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001298{
1299 const char *ptr = NULL;
1300 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001301 int warn = 0;
1302 int arg;
1303 struct tcp_rule *rule;
1304
1305 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001306 memprintf(err, "missing argument for '%s' in %s '%s'",
1307 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001308 return -1;
1309 }
1310
1311 if (strcmp(args[1], "inspect-delay") == 0) {
1312 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001313 memprintf(err, "%s %s is only allowed in 'backend' sections",
1314 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001315 return -1;
1316 }
1317
1318 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001319 memprintf(err,
1320 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1321 args[0], args[1], proxy_type_str(curpx), curpx->id);
1322 if (ptr)
1323 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001324 return -1;
1325 }
1326
1327 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001328 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1329 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001330 return 1;
1331 }
1332 curpx->tcp_rep.inspect_delay = val;
1333 return 0;
1334 }
1335
Simon Hormande072bd2011-06-24 15:11:37 +09001336 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001337 LIST_INIT(&rule->list);
1338 arg = 1;
1339
1340 if (strcmp(args[1], "content") == 0) {
1341 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001342 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001343 goto error;
1344
1345 if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) {
1346 struct acl *acl;
1347 const char *name;
1348
1349 acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE);
1350 name = acl ? acl->name : "(unknown)";
1351
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001352 memprintf(err,
1353 "acl '%s' involves some request-only criteria which will be ignored in '%s %s'",
1354 name, args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001355 warn++;
1356 }
1357
1358 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1359 }
1360 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001361 memprintf(err,
1362 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1363 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001364 goto error;
1365 }
1366
1367 return warn;
1368 error:
1369 free(rule);
1370 return -1;
1371}
1372
1373
Willy Tarreaub6866442008-07-14 23:54:42 +02001374/* This function should be called to parse a line starting with the "tcp-request"
1375 * keyword.
1376 */
1377static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001378 struct proxy *defpx, char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001379{
1380 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001381 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001382 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001383 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001384 struct tcp_rule *rule;
Willy Tarreaub6866442008-07-14 23:54:42 +02001385
1386 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001387 if (curpx == defpx)
1388 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1389 else
1390 memprintf(err, "missing argument for '%s' in %s '%s'",
1391 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001392 return -1;
1393 }
1394
1395 if (!strcmp(args[1], "inspect-delay")) {
1396 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001397 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1398 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001399 return -1;
1400 }
1401
Willy Tarreaub6866442008-07-14 23:54:42 +02001402 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001403 memprintf(err,
1404 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1405 args[0], args[1], proxy_type_str(curpx), curpx->id);
1406 if (ptr)
1407 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001408 return -1;
1409 }
1410
1411 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001412 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1413 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001414 return 1;
1415 }
1416 curpx->tcp_req.inspect_delay = val;
1417 return 0;
1418 }
1419
Simon Hormande072bd2011-06-24 15:11:37 +09001420 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001421 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001422 arg = 1;
1423
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001424 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001425 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001426 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001427 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001428
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001429 if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001430 struct acl *acl;
1431 const char *name;
1432
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001433 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001434 name = acl ? acl->name : "(unknown)";
1435
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001436 memprintf(err,
1437 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1438 name, args[0], args[1]);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001439 warn++;
1440 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001441 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001442 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001443 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001444 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001445
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001446 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001447 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1448 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001449 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001450 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001451
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001452 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001453 goto error;
1454
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001455 if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) {
1456 struct acl *acl;
1457 const char *name;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001458
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001459 acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY);
1460 name = acl ? acl->name : "(unknown)";
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001461
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001462 if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001463 memprintf(err,
1464 "'%s %s' may not reference acl '%s' which makes use of "
1465 "payload in %s '%s'. Please use '%s content' for this.",
1466 args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001467 goto error;
1468 }
1469 if (acl->requires & ACL_USE_RTR_ANY)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001470 memprintf(err,
1471 "acl '%s' involves some response-only criteria which will be ignored in '%s %s'",
1472 name, args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001473 warn++;
1474 }
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001475 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001476 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001477 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001478 if (curpx == defpx)
1479 memprintf(err,
1480 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1481 args[0], args[1]);
1482 else
1483 memprintf(err,
1484 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1485 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001486 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001487 }
1488
Willy Tarreau1a687942010-05-23 22:40:30 +02001489 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001490 error:
1491 free(rule);
1492 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001493}
1494
Willy Tarreau645513a2010-05-24 20:55:15 +02001495
1496/************************************************************************/
Willy Tarreau32389b72012-04-23 23:13:20 +02001497/* All supported sample fetch functios must be declared here */
1498/************************************************************************/
1499
1500/* Fetch the request RDP cookie identified in the args, or any cookie if no arg
Willy Tarreau12785782012-04-27 21:37:17 +02001501 * is passed. It is usable both for ACL and for samples. Note: this decoder
Willy Tarreau32389b72012-04-23 23:13:20 +02001502 * only works with non-wrapping data. Accepts either 0 or 1 argument. Argument
1503 * is a string (cookie name), other types will lead to undefined behaviour.
1504 */
1505int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001506smp_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau32389b72012-04-23 23:13:20 +02001507 const struct arg *args, struct sample *smp)
1508{
1509 int bleft;
1510 const unsigned char *data;
1511
1512 if (!l4 || !l4->req)
1513 return 0;
1514
1515 smp->flags = 0;
1516 smp->type = SMP_T_CSTR;
1517
1518 bleft = l4->req->i;
1519 if (bleft <= 11)
1520 goto too_short;
1521
1522 data = (const unsigned char *)l4->req->p + 11;
1523 bleft -= 11;
1524
1525 if (bleft <= 7)
1526 goto too_short;
1527
1528 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
1529 goto not_cookie;
1530
1531 data += 7;
1532 bleft -= 7;
1533
1534 while (bleft > 0 && *data == ' ') {
1535 data++;
1536 bleft--;
1537 }
1538
1539 if (args) {
1540
1541 if (bleft <= args->data.str.len)
1542 goto too_short;
1543
1544 if ((data[args->data.str.len] != '=') ||
1545 strncasecmp(args->data.str.str, (const char *)data, args->data.str.len) != 0)
1546 goto not_cookie;
1547
1548 data += args->data.str.len + 1;
1549 bleft -= args->data.str.len + 1;
1550 } else {
1551 while (bleft > 0 && *data != '=') {
1552 if (*data == '\r' || *data == '\n')
1553 goto not_cookie;
1554 data++;
1555 bleft--;
1556 }
1557
1558 if (bleft < 1)
1559 goto too_short;
1560
1561 if (*data != '=')
1562 goto not_cookie;
1563
1564 data++;
1565 bleft--;
1566 }
1567
1568 /* data points to cookie value */
1569 smp->data.str.str = (char *)data;
1570 smp->data.str.len = 0;
1571
1572 while (bleft > 0 && *data != '\r') {
1573 data++;
1574 bleft--;
1575 }
1576
1577 if (bleft < 2)
1578 goto too_short;
1579
1580 if (data[0] != '\r' || data[1] != '\n')
1581 goto not_cookie;
1582
1583 smp->data.str.len = (char *)data - smp->data.str.str;
1584 smp->flags = SMP_F_VOLATILE;
1585 return 1;
1586
1587 too_short:
1588 smp->flags = SMP_F_MAY_CHANGE;
1589 not_cookie:
1590 return 0;
1591}
1592
Willy Tarreau32389b72012-04-23 23:13:20 +02001593/************************************************************************/
Willy Tarreau645513a2010-05-24 20:55:15 +02001594/* All supported ACL keywords must be declared here. */
1595/************************************************************************/
1596
Willy Tarreau32389b72012-04-23 23:13:20 +02001597/* returns either 1 or 0 depending on whether an RDP cookie is found or not */
1598static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001599acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001600 const struct arg *args, struct sample *smp)
Willy Tarreau32389b72012-04-23 23:13:20 +02001601{
1602 int ret;
1603
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001604 ret = smp_fetch_rdp_cookie(px, l4, l7, opt, args, smp);
Willy Tarreau32389b72012-04-23 23:13:20 +02001605
1606 if (smp->flags & SMP_F_MAY_CHANGE)
1607 return 0;
1608
1609 smp->flags = SMP_F_VOLATILE;
1610 smp->type = SMP_T_UINT;
1611 smp->data.uint = ret;
1612 return 1;
1613}
1614
1615
Willy Tarreau4a129812012-04-25 17:31:42 +02001616/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001617static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001618smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001619 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001620{
Willy Tarreauf4362b32011-12-16 17:49:52 +01001621 switch (l4->si[0].addr.from.ss_family) {
1622 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001623 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.from)->sin_addr;
1624 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001625 break;
1626 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001627 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.from))->sin6_addr;
1628 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001629 break;
1630 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001631 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001632 }
Emeric Brunf769f512010-10-22 17:14:01 +02001633
Willy Tarreau37406352012-04-23 16:16:37 +02001634 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001635 return 1;
1636}
1637
Willy Tarreaua5e37562011-12-16 17:06:15 +01001638/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001639static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001640smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001641 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001642{
Willy Tarreauf853c462012-04-23 18:53:56 +02001643 smp->type = SMP_T_UINT;
1644 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001645 return 0;
1646
Willy Tarreau37406352012-04-23 16:16:37 +02001647 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001648 return 1;
1649}
1650
Willy Tarreau4a129812012-04-25 17:31:42 +02001651/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001652static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001653smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001654 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001655{
Willy Tarreau59b94792012-05-11 16:16:40 +02001656 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001657
Willy Tarreauf4362b32011-12-16 17:49:52 +01001658 switch (l4->si[0].addr.to.ss_family) {
1659 case AF_INET:
Willy Tarreauf853c462012-04-23 18:53:56 +02001660 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].addr.to)->sin_addr;
1661 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001662 break;
1663 case AF_INET6:
Willy Tarreauf853c462012-04-23 18:53:56 +02001664 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].addr.to))->sin6_addr;
1665 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001666 break;
1667 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001668 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001669 }
Emeric Brunf769f512010-10-22 17:14:01 +02001670
Willy Tarreau37406352012-04-23 16:16:37 +02001671 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001672 return 1;
1673}
1674
Willy Tarreaua5e37562011-12-16 17:06:15 +01001675/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001676static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001677smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001678 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001679{
Willy Tarreau59b94792012-05-11 16:16:40 +02001680 si_get_to_addr(&l4->si[0]);
Willy Tarreau645513a2010-05-24 20:55:15 +02001681
Willy Tarreauf853c462012-04-23 18:53:56 +02001682 smp->type = SMP_T_UINT;
1683 if (!(smp->data.uint = get_host_port(&l4->si[0].addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001684 return 0;
1685
Willy Tarreau37406352012-04-23 16:16:37 +02001686 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001687 return 1;
1688}
1689
1690static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001691smp_fetch_payload_lv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1692 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001693{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001694 unsigned int len_offset = arg_p[0].data.uint;
1695 unsigned int len_size = arg_p[1].data.uint;
1696 unsigned int buf_offset;
1697 unsigned int buf_size = 0;
Emericf2d7cae2010-11-05 18:13:50 +01001698 struct buffer *b;
1699 int i;
1700
1701 /* Format is (len offset, len size, buf offset) or (len offset, len size) */
1702 /* by default buf offset == len offset + len size */
1703 /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
1704
1705 if (!l4)
1706 return 0;
1707
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001708 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001709
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001710 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001711 return 0;
1712
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001713 if (len_offset + len_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001714 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001715
1716 for (i = 0; i < len_size; i++) {
Willy Tarreau89fa7062012-03-02 16:13:16 +01001717 buf_size = (buf_size << 8) + ((unsigned char *)b->p)[i + len_offset];
Emericf2d7cae2010-11-05 18:13:50 +01001718 }
1719
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001720 /* buf offset may be implicit, absolute or relative */
1721 buf_offset = len_offset + len_size;
1722 if (arg_p[2].type == ARGT_UINT)
1723 buf_offset = arg_p[2].data.uint;
1724 else if (arg_p[2].type == ARGT_SINT)
1725 buf_offset += arg_p[2].data.sint;
1726
Willy Tarreau82ea8002012-04-25 19:19:43 +02001727 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1728 /* will never match */
1729 smp->flags = 0;
1730 return 0;
1731 }
1732
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001733 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001734 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001735
1736 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001737 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001738 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001739 smp->flags = SMP_F_VOLATILE;
Emericf2d7cae2010-11-05 18:13:50 +01001740 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001741
1742 too_short:
1743 smp->flags = SMP_F_MAY_CHANGE;
1744 return 0;
Emericf2d7cae2010-11-05 18:13:50 +01001745}
1746
1747static int
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001748smp_fetch_payload(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
1749 const struct arg *arg_p, struct sample *smp)
Emericf2d7cae2010-11-05 18:13:50 +01001750{
Willy Tarreau82ea8002012-04-25 19:19:43 +02001751 unsigned int buf_offset = arg_p[0].data.uint;
1752 unsigned int buf_size = arg_p[1].data.uint;
Emericf2d7cae2010-11-05 18:13:50 +01001753 struct buffer *b;
1754
1755 if (!l4)
1756 return 0;
1757
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001758 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emericf2d7cae2010-11-05 18:13:50 +01001759
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001760 if (!b)
Emericf2d7cae2010-11-05 18:13:50 +01001761 return 0;
Willy Tarreau82ea8002012-04-25 19:19:43 +02001762
1763 if (!buf_size || buf_size > b->size || buf_offset + buf_size > b->size) {
1764 /* will never match */
1765 smp->flags = 0;
1766 return 0;
1767 }
Emericf2d7cae2010-11-05 18:13:50 +01001768
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001769 if (buf_offset + buf_size > b->i)
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001770 goto too_short;
Emericf2d7cae2010-11-05 18:13:50 +01001771
1772 /* init chunk as read only */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001773 smp->type = SMP_T_CBIN;
Willy Tarreau342acb42012-04-23 22:03:39 +02001774 chunk_initlen(&smp->data.str, b->p + buf_offset, 0, buf_size);
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001775 smp->flags = SMP_F_VOLATILE;
Simon Hormanab814e02011-06-24 14:50:20 +09001776 return 1;
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001777
1778 too_short:
1779 smp->flags = SMP_F_MAY_CHANGE;
1780 return 0;
Simon Hormanab814e02011-06-24 14:50:20 +09001781}
1782
Willy Tarreau21d68a62012-04-20 15:52:36 +02001783/* This function is used to validate the arguments passed to a "payload" fetch
1784 * keyword. This keyword expects two positive integers, with the second one
1785 * being strictly positive. It is assumed that the types are already the correct
1786 * ones. Returns 0 on error, non-zero if OK. If <err_msg> is not NULL, it will be
1787 * filled with a pointer to an error message in case of error, that the caller
1788 * is responsible for freeing. The initial location must either be freeable or
1789 * NULL.
1790 */
1791static int val_payload(struct arg *arg, char **err_msg)
1792{
1793 if (!arg[1].data.uint) {
1794 if (err_msg)
1795 memprintf(err_msg, "payload length must be > 0");
1796 return 0;
1797 }
1798 return 1;
1799}
1800
1801/* This function is used to validate the arguments passed to a "payload_lv" fetch
1802 * keyword. This keyword allows two positive integers and an optional signed one,
1803 * with the second one being strictly positive and the third one being greater than
1804 * the opposite of the two others if negative. It is assumed that the types are
1805 * already the correct ones. Returns 0 on error, non-zero if OK. If <err_msg> is
1806 * not NULL, it will be filled with a pointer to an error message in case of
1807 * error, that the caller is responsible for freeing. The initial location must
1808 * either be freeable or NULL.
1809 */
1810static int val_payload_lv(struct arg *arg, char **err_msg)
1811{
1812 if (!arg[1].data.uint) {
1813 if (err_msg)
1814 memprintf(err_msg, "payload length must be > 0");
1815 return 0;
1816 }
1817
1818 if (arg[2].type == ARGT_SINT &&
1819 (int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
1820 if (err_msg)
1821 memprintf(err_msg, "payload offset too negative");
1822 return 0;
1823 }
1824 return 1;
1825}
1826
Willy Tarreaub6866442008-07-14 23:54:42 +02001827static struct cfg_kw_list cfg_kws = {{ },{
1828 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001829 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001830 { 0, NULL, NULL },
1831}};
1832
Willy Tarreau61612d42012-04-19 18:42:05 +02001833/* Note: must not be declared <const> as its list will be overwritten.
1834 * Please take care of keeping this list alphabetically sorted.
1835 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001836static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001837 { "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 +02001838 { "dst_port", acl_parse_int, smp_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreau0d5fe142012-04-26 12:24:45 +02001839 { "payload", acl_parse_str, smp_fetch_payload, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(2,UINT,UINT), val_payload },
1840 { "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 +02001841 { "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 +02001842 { "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 +02001843 { "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 +02001844 { "src_port", acl_parse_int, smp_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT, 0 },
Willy Tarreaub6866442008-07-14 23:54:42 +02001845 { NULL, NULL, NULL, NULL },
1846}};
1847
Willy Tarreau4a129812012-04-25 17:31:42 +02001848/* Note: must not be declared <const> as its list will be overwritten.
1849 * Note: fetches that may return multiple types must be declared as the lowest
1850 * common denominator, the type that can be casted into all other ones. For
1851 * instance v4/v6 must be declared v4.
1852 */
Willy Tarreau12785782012-04-27 21:37:17 +02001853static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau4a129812012-04-25 17:31:42 +02001854 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau4a129812012-04-25 17:31:42 +02001855 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001856 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau0ce3aa02012-04-25 18:46:33 +02001857 { "payload", smp_fetch_payload, ARG2(2,UINT,UINT), val_payload, SMP_T_CBIN, SMP_CAP_REQ|SMP_CAP_RES },
1858 { "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 +02001859 { "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 +02001860 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_CAP_REQ|SMP_CAP_RES },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001861 { NULL, NULL, 0, 0, 0 },
Willy Tarreau645513a2010-05-24 20:55:15 +02001862}};
1863
Willy Tarreaue6b98942007-10-29 01:09:36 +01001864__attribute__((constructor))
1865static void __tcp_protocol_init(void)
1866{
1867 protocol_register(&proto_tcpv4);
1868 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001869 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001870 cfg_register_keywords(&cfg_kws);
1871 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001872}
1873
1874
1875/*
1876 * Local variables:
1877 * c-indent-level: 8
1878 * c-basic-offset: 8
1879 * End:
1880 */