blob: d01be31bd93054d4f1fa57a9b4d56645c3351a71 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 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 Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020044#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020046#include <proto/log.h>
47#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020048#include <proto/protocol.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010049#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020051#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020052#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/stick_table.h>
Willy Tarreaucc1e04b2013-09-11 23:20:29 +020054#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020055#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010056
Willy Tarreaue8c66af2008-01-13 18:40:14 +010057#ifdef CONFIG_HAP_CTTPROXY
58#include <import/ip_tproxy.h>
59#endif
60
Emeric Bruncf20bf12010-10-22 16:06:11 +020061static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
62static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010063
64/* Note: must not be declared <const> as its list will be overwritten */
65static struct protocol proto_tcpv4 = {
66 .name = "tcpv4",
67 .sock_domain = AF_INET,
68 .sock_type = SOCK_STREAM,
69 .sock_prot = IPPROTO_TCP,
70 .sock_family = AF_INET,
71 .sock_addrlen = sizeof(struct sockaddr_in),
72 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020073 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020074 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020075 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010076 .bind_all = tcp_bind_listeners,
77 .unbind_all = unbind_all_listeners,
78 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020079 .get_src = tcp_get_src,
80 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020081 .drain = tcp_drain,
Willy Tarreaue6b98942007-10-29 01:09:36 +010082 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
83 .nb_listeners = 0,
84};
85
86/* Note: must not be declared <const> as its list will be overwritten */
87static struct protocol proto_tcpv6 = {
88 .name = "tcpv6",
89 .sock_domain = AF_INET6,
90 .sock_type = SOCK_STREAM,
91 .sock_prot = IPPROTO_TCP,
92 .sock_family = AF_INET6,
93 .sock_addrlen = sizeof(struct sockaddr_in6),
94 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020095 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020096 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020097 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010098 .bind_all = tcp_bind_listeners,
99 .unbind_all = unbind_all_listeners,
100 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200101 .get_src = tcp_get_src,
102 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200103 .drain = tcp_drain,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100104 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
105 .nb_listeners = 0,
106};
107
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100108
David du Colombier6f5ccb12011-03-10 22:26:24 +0100109/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100110 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
111 * - 0 : ignore remote address (may even be a NULL pointer)
112 * - 1 : use provided address
113 * - 2 : use provided port
114 * - 3 : use both
115 *
116 * The function supports multiple foreign binding methods :
117 * - linux_tproxy: we directly bind to the foreign address
118 * - cttproxy: we bind to a local address then nat.
119 * The second one can be used as a fallback for the first one.
120 * This function returns 0 when everything's OK, 1 if it could not bind, to the
121 * local address, 2 if it could not bind to the foreign address.
122 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100123int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100124{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100125 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100126 int foreign_ok = 0;
127 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100128 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200129 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200130
David du Colombier65c17962012-07-13 14:34:59 +0200131 switch (local->ss_family) {
132 case AF_INET:
133 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200134 /* This deserves some explanation. Some platforms will support
135 * multiple combinations of certain methods, so we try the
136 * supported ones until one succeeds.
137 */
138 if (0
139#if defined(IP_TRANSPARENT)
140 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
141#endif
142#if defined(IP_FREEBIND)
143 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
144#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200145#if defined(IP_BINDANY)
146 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
147#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200148#if defined(SO_BINDANY)
149 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
150#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200151 )
David du Colombier65c17962012-07-13 14:34:59 +0200152 foreign_ok = 1;
153 else
154 ip_transp_working = 0;
155 }
156 break;
157 case AF_INET6:
158 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200159 if (0
160#if defined(IPV6_TRANSPARENT)
161 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
162#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200163#if defined(IPV6_BINDANY)
164 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
165#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200166#if defined(SO_BINDANY)
167 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
168#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200169 )
David du Colombier65c17962012-07-13 14:34:59 +0200170 foreign_ok = 1;
171 else
172 ip6_transp_working = 0;
173 }
174 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100175 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200176
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100177 if (flags) {
178 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200179 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100180 switch (remote->ss_family) {
181 case AF_INET:
182 if (flags & 1)
183 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
184 if (flags & 2)
185 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
186 break;
187 case AF_INET6:
188 if (flags & 1)
189 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
190 if (flags & 2)
191 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
192 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100193 default:
194 /* we don't want to try to bind to an unknown address family */
195 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100196 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100197 }
198
Simon Hormande072bd2011-06-24 15:11:37 +0900199 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100200 if (foreign_ok) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200201 if (is_addr(&bind_addr)) {
202 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
203 if (ret < 0)
204 return 2;
205 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100206 }
207 else {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200208 if (is_addr(local)) {
209 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
210 if (ret < 0)
211 return 1;
212 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100213 }
214
215 if (!flags)
216 return 0;
217
218#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100219 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100220 struct in_tproxy itp1, itp2;
221 memset(&itp1, 0, sizeof(itp1));
222
223 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100224 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
225 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100226
227 /* set connect flag on socket */
228 itp2.op = TPROXY_FLAGS;
229 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
230
231 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
232 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
233 foreign_ok = 1;
234 }
235 }
236#endif
237 if (!foreign_ok)
238 /* we could not bind to a foreign address */
239 return 2;
240
241 return 0;
242}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100243
Willy Tarreau9650f372009-08-16 14:02:45 +0200244
245/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200246 * This function initiates a TCP connection establishment to the target assigned
247 * to connection <conn> using (si->{target,addr.to}). A source address may be
248 * pointed to by conn->addr.from in case of transparent proxying. Normal source
249 * bind addresses are still determined locally (due to the possible need of a
250 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100251 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100252 * supported. The <data> parameter is a boolean indicating whether there are data
253 * waiting for being sent or not, in order to adjust data write polling and on
254 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
255 * allows the caller to force using a delayed ACK when establishing the connection :
256 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
257 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
258 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200259 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200260 * Note that a pending send_proxy message accounts for data.
261 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200262 * It can return one of :
263 * - SN_ERR_NONE if everything's OK
264 * - SN_ERR_SRVTO if there are no more servers
265 * - SN_ERR_SRVCL if the connection was refused by the server
266 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
267 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
268 * - SN_ERR_INTERNAL for any other purely internal errors
269 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100270 *
271 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
272 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200273 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100274
Willy Tarreauf0837b22012-11-24 10:24:27 +0100275int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200276{
277 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100278 struct server *srv;
279 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100280 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100281
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100282 switch (obj_type(conn->target)) {
283 case OBJ_TYPE_PROXY:
284 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100285 srv = NULL;
286 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100287 case OBJ_TYPE_SERVER:
288 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100289 be = srv->proxy;
290 break;
291 default:
292 return SN_ERR_INTERNAL;
293 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200294
Willy Tarreau986a9d22012-08-30 21:11:38 +0200295 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200296 qfprintf(stderr, "Cannot get a server socket.\n");
297
298 if (errno == ENFILE)
299 send_log(be, LOG_EMERG,
300 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
301 be->id, maxfd);
302 else if (errno == EMFILE)
303 send_log(be, LOG_EMERG,
304 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
305 be->id, maxfd);
306 else if (errno == ENOBUFS || errno == ENOMEM)
307 send_log(be, LOG_EMERG,
308 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
309 be->id, maxfd);
310 /* this is a resource error */
311 return SN_ERR_RESOURCE;
312 }
313
314 if (fd >= global.maxsock) {
315 /* do not log anything there, it's a normal condition when this option
316 * is used to serialize connections to a server !
317 */
318 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
319 close(fd);
320 return SN_ERR_PRXCOND; /* it is a configuration limit */
321 }
322
323 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900324 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200325 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
326 close(fd);
327 return SN_ERR_INTERNAL;
328 }
329
330 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900331 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200332
Willy Tarreau9650f372009-08-16 14:02:45 +0200333 /* allow specific binding :
334 * - server-specific at first
335 * - proxy-specific next
336 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100337 if (srv && srv->conn_src.opts & CO_SRC_BIND)
338 src = &srv->conn_src;
339 else if (be->conn_src.opts & CO_SRC_BIND)
340 src = &be->conn_src;
341 else
342 src = NULL;
343
344 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200345 int ret, flags = 0;
346
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200347 if (is_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100348 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100349 case CO_SRC_TPROXY_ADDR:
350 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200351 flags = 3;
352 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100353 case CO_SRC_TPROXY_CIP:
354 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200355 flags = 1;
356 break;
357 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200358 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200359
Willy Tarreau9650f372009-08-16 14:02:45 +0200360#ifdef SO_BINDTODEVICE
361 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100362 if (src->iface_name)
363 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200364#endif
365
Willy Tarreaua4380b42012-12-08 22:49:11 +0100366 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200367 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100368 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200369
370 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100371 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200372
373 do {
374 /* note: in case of retry, we may have to release a previously
375 * allocated port, hence this loop's construct.
376 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200377 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
378 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200379
380 if (!attempts)
381 break;
382 attempts--;
383
Willy Tarreaua4380b42012-12-08 22:49:11 +0100384 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200385 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200386 break;
387
Willy Tarreaua4380b42012-12-08 22:49:11 +0100388 fdinfo[fd].port_range = src->sport_range;
389 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200390
Willy Tarreaua4380b42012-12-08 22:49:11 +0100391 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200392 } while (ret != 0); /* binding NOK */
393 }
394 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100395 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200396 }
397
Willy Tarreaua4380b42012-12-08 22:49:11 +0100398 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200399 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
400 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200401 close(fd);
402
Willy Tarreau9650f372009-08-16 14:02:45 +0200403 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100404 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200405 be->id);
406 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100407 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200408 be->id);
409 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100410 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200411 be->id);
412 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100413 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200414 be->id);
415 }
416 return SN_ERR_RESOURCE;
417 }
418 }
419
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200420 /* if a send_proxy is there, there are data */
421 data |= conn->send_proxy_ofs;
422
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400423#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200424 /* disabling tcp quick ack now allows the first request to leave the
425 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100426 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200427 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100428 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900429 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200430#endif
431
Willy Tarreaue803de22010-01-21 17:43:04 +0100432 if (global.tune.server_sndbuf)
433 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
434
435 if (global.tune.server_rcvbuf)
436 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
437
Willy Tarreau986a9d22012-08-30 21:11:38 +0200438 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200439 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
440
Willy Tarreaub1719512012-12-08 23:03:28 +0100441 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 char *msg;
Willy Tarreaub1719512012-12-08 23:03:28 +0100443 if (errno == EAGAIN || errno == EADDRNOTAVAIL)
Willy Tarreau9650f372009-08-16 14:02:45 +0200444 msg = "no free ports";
445 else
446 msg = "local address already in use";
447
Willy Tarreaub1719512012-12-08 23:03:28 +0100448 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200449 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
450 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200451 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100452 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9650f372009-08-16 14:02:45 +0200453 return SN_ERR_RESOURCE;
454 } else if (errno == ETIMEDOUT) {
455 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200456 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
457 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200458 close(fd);
459 return SN_ERR_SRVTO;
460 } else {
461 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
462 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200463 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
464 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200465 close(fd);
466 return SN_ERR_SRVCL;
467 }
468 }
469
Willy Tarreau986a9d22012-08-30 21:11:38 +0200470 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100471 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200472
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200473 /* Prepare to send a few handshakes related to the on-wire protocol. */
474 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200475 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200476
Willy Tarreauf79c8172013-10-21 16:30:56 +0200477 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100478 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200479 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200480
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200481 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200482 conn_force_close(conn);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200483 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200484 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200485
Willy Tarreau14f8e862012-08-30 22:23:13 +0200486 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200487 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200488
Willy Tarreau9650f372009-08-16 14:02:45 +0200489 return SN_ERR_NONE; /* connection is OK */
490}
491
492
Willy Tarreau59b94792012-05-11 16:16:40 +0200493/*
494 * Retrieves the source address for the socket <fd>, with <dir> indicating
495 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
496 * success, -1 in case of error. The socket's source address is stored in
497 * <sa> for <salen> bytes.
498 */
499int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
500{
501 if (dir)
502 return getsockname(fd, sa, &salen);
503 else
504 return getpeername(fd, sa, &salen);
505}
506
507
508/*
509 * Retrieves the original destination address for the socket <fd>, with <dir>
510 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
511 * listener, if the original destination address was translated, the original
512 * address is retrieved. It returns 0 in case of success, -1 in case of error.
513 * The socket's source address is stored in <sa> for <salen> bytes.
514 */
515int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
516{
517 if (dir)
518 return getpeername(fd, sa, &salen);
519#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
520 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
521 return 0;
522#endif
523 else
524 return getsockname(fd, sa, &salen);
525}
526
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200527/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100528 * receive shutdown. Returns positive if the shutdown was found, negative
529 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
530 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200531 */
532int tcp_drain(int fd)
533{
534 int turns = 2;
535 int len;
536
537 while (turns) {
538#ifdef MSG_TRUNC_CLEARS_INPUT
539 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
540 if (len == -1 && errno == EFAULT)
541#endif
542 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
543
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100544 if (len == 0) {
545 /* cool, shutdown received */
546 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200547 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100548 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200549
550 if (len < 0) {
551 if (errno == EAGAIN) /* connection not closed yet */
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100552 return -1;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200553 if (errno == EINTR) /* oops, try again */
554 continue;
555 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100556 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200557 return 1;
558 }
559 /* OK we read some data, let's try again once */
560 turns--;
561 }
562 /* some data are still present, give up */
563 return 0;
564}
565
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200566/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100567 * and we have nothing to send. It updates the FD polling status. It returns 0
568 * if it fails in a fatal way or needs to poll to go further, otherwise it
569 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
570 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
571 * errno. The error checking is done in two passes in order to limit the number
572 * of syscalls in the normal case :
573 * - if POLL_ERR was reported by the poller, we check for a pending error on
574 * the socket before proceeding. If found, it's assigned to errno so that
575 * upper layers can see it.
576 * - otherwise connect() is used to check the connection state again, since
577 * the getsockopt return cannot reliably be used to know if the connection
578 * is still pending or ready. This one may often return an error as well,
579 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200580 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200581int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200582{
Willy Tarreau239d7182012-07-23 18:53:03 +0200583 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100584 socklen_t lskerr;
585 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200586
Willy Tarreau80184712012-07-06 14:54:49 +0200587 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200588 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200589
Willy Tarreauf79c8172013-10-21 16:30:56 +0200590 if (!(conn->flags & CO_FL_CTRL_READY))
591 return 0;
592
Willy Tarreau80184712012-07-06 14:54:49 +0200593 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200594 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200595
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100596 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
597 * without FD_POLL_IN also indicates a hangup without input data meaning
598 * there was no connection.
599 */
600 if (fdtab[fd].ev & FD_POLL_ERR ||
601 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
602 skerr = 0;
603 lskerr = sizeof(skerr);
604 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
605 errno = skerr;
606 if (errno == EAGAIN)
607 errno = 0;
608 if (errno)
609 goto out_error;
610 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200611
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100612 /* Use connect() to check the state of the socket. This has the
613 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200614 * - error
615 * - connecting (EALREADY, EINPROGRESS)
616 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200617 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200618 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200619 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100620 __conn_sock_stop_recv(conn);
621 __conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200622 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200623 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200624
Willy Tarreau2c6be842012-07-06 17:12:34 +0200625 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200626 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200627
Willy Tarreau2c6be842012-07-06 17:12:34 +0200628 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200629 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200630
Willy Tarreau076be252012-07-06 16:02:29 +0200631 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200632 * forward the event to the transport layer which will notify the
633 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200634 */
Willy Tarreau80184712012-07-06 14:54:49 +0200635 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200636 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200637
638 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200639 /* Write error on the file descriptor. Report it to the connection
640 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200641 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100642 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100643 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100644 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200645 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200646}
647
Willy Tarreau59b94792012-05-11 16:16:40 +0200648
Willy Tarreaue6b98942007-10-29 01:09:36 +0100649/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100650 * an error message in <errmsg> if the message is at most <errlen> bytes long
651 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
652 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100653 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
654 * was alright and that no message was returned. ERR_RETRYABLE means that an
655 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700656 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100657 * the meaning of the error, but just indicate that a message is present which
658 * should be displayed with the respective level. Last, ERR_ABORT indicates
659 * that it's pointless to try to start other listeners. No error message is
660 * returned if errlen is NULL.
661 */
662int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
663{
664 __label__ tcp_return, tcp_close_return;
665 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100666 int ext, ready;
667 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100668 const char *msg = NULL;
669
670 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100671 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100672 *errmsg = 0;
673
674 if (listener->state != LI_ASSIGNED)
675 return ERR_NONE; /* already bound */
676
677 err = ERR_NONE;
678
Willy Tarreau40aa0702013-03-10 23:51:38 +0100679 /* if the listener already has an fd assigned, then we were offered the
680 * fd by an external process (most likely the parent), and we don't want
681 * to create a new socket. However we still want to set a few flags on
682 * the socket.
683 */
684 fd = listener->fd;
685 ext = (fd >= 0);
686
687 if (!ext && (fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100688 err |= ERR_RETRYABLE | ERR_ALERT;
689 msg = "cannot create listening socket";
690 goto tcp_return;
691 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100692
Willy Tarreaue6b98942007-10-29 01:09:36 +0100693 if (fd >= global.maxsock) {
694 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
695 msg = "not enough free sockets (raise '-n' parameter)";
696 goto tcp_close_return;
697 }
698
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200699 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100700 err |= ERR_FATAL | ERR_ALERT;
701 msg = "cannot make socket non-blocking";
702 goto tcp_close_return;
703 }
704
Willy Tarreau40aa0702013-03-10 23:51:38 +0100705 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100706 /* not fatal but should be reported */
707 msg = "cannot do so_reuseaddr";
708 err |= ERR_ALERT;
709 }
710
711 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900712 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100713
Willy Tarreaue6b98942007-10-29 01:09:36 +0100714#ifdef SO_REUSEPORT
715 /* OpenBSD supports this. As it's present in old libc versions of Linux,
716 * it might return an error that we will silently ignore.
717 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100718 if (!ext)
719 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100720#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200721
Willy Tarreau40aa0702013-03-10 23:51:38 +0100722 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200723 switch (listener->addr.ss_family) {
724 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200725 if (1
726#if defined(IP_TRANSPARENT)
727 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
728#endif
729#if defined(IP_FREEBIND)
730 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
731#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200732#if defined(IP_BINDANY)
733 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
734#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200735#if defined(SO_BINDANY)
736 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
737#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200738 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200739 msg = "cannot make listening socket transparent";
740 err |= ERR_ALERT;
741 }
742 break;
743 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200744 if (1
745#if defined(IPV6_TRANSPARENT)
746 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
747#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200748#if defined(IPV6_BINDANY)
749 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
750#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200751#if defined(SO_BINDANY)
752 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
753#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200754 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200755 msg = "cannot make listening socket transparent";
756 err |= ERR_ALERT;
757 }
758 break;
759 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100760 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200761
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100762#ifdef SO_BINDTODEVICE
763 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100764 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100765 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100766 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100767 msg = "cannot bind listener to device";
768 err |= ERR_WARN;
769 }
770 }
771#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400772#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100773 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400774 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200775 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
776 msg = "cannot set MSS";
777 err |= ERR_WARN;
778 }
779 }
780#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200781#if defined(TCP_DEFER_ACCEPT)
782 if (listener->options & LI_O_DEF_ACCEPT) {
783 /* defer accept by up to one second */
784 int accept_delay = 1;
785 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
786 msg = "cannot enable DEFER_ACCEPT";
787 err |= ERR_WARN;
788 }
789 }
790#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200791#if defined(TCP_FASTOPEN)
792 if (listener->options & LI_O_TCP_FO) {
793 /* TFO needs a queue length, let's use the configured backlog */
794 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
795 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
796 msg = "cannot enable TCP_FASTOPEN";
797 err |= ERR_WARN;
798 }
799 }
800#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100801#if defined(IPV6_V6ONLY)
802 if (listener->options & LI_O_V6ONLY)
803 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100804 else if (listener->options & LI_O_V4V6)
805 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100806#endif
807
Willy Tarreau40aa0702013-03-10 23:51:38 +0100808 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100809 err |= ERR_RETRYABLE | ERR_ALERT;
810 msg = "cannot bind socket";
811 goto tcp_close_return;
812 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100813
Willy Tarreau40aa0702013-03-10 23:51:38 +0100814 ready = 0;
815 ready_len = sizeof(ready);
816 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
817 ready = 0;
818
819 if (!(ext && ready) && /* only listen if not already done by external process */
820 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100821 err |= ERR_RETRYABLE | ERR_ALERT;
822 msg = "cannot listen to socket";
823 goto tcp_close_return;
824 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100825
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400826#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200827 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900828 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200829#endif
830
Willy Tarreaue6b98942007-10-29 01:09:36 +0100831 /* the socket is ready */
832 listener->fd = fd;
833 listener->state = LI_LISTEN;
834
Willy Tarreaueabf3132008-08-29 23:36:51 +0200835 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200836 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200837 fd_insert(fd);
838
Willy Tarreaue6b98942007-10-29 01:09:36 +0100839 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100840 if (msg && errlen) {
841 char pn[INET6_ADDRSTRLEN];
842
Willy Tarreau631f01c2011-09-05 00:36:48 +0200843 addr_to_str(&listener->addr, pn, sizeof(pn));
844 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100845 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100846 return err;
847
848 tcp_close_return:
849 close(fd);
850 goto tcp_return;
851}
852
853/* This function creates all TCP sockets bound to the protocol entry <proto>.
854 * It is intended to be used as the protocol's bind_all() function.
855 * The sockets will be registered but not added to any fd_set, in order not to
856 * loose them across the fork(). A call to enable_all_listeners() is needed
857 * to complete initialization. The return value is composed from ERR_*.
858 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200859static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100860{
861 struct listener *listener;
862 int err = ERR_NONE;
863
864 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200865 err |= tcp_bind_listener(listener, errmsg, errlen);
866 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100867 break;
868 }
869
870 return err;
871}
872
873/* Add listener to the list of tcpv4 listeners. The listener's state
874 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
875 * listeners is updated. This is the function to use to add a new listener.
876 */
877void tcpv4_add_listener(struct listener *listener)
878{
879 if (listener->state != LI_INIT)
880 return;
881 listener->state = LI_ASSIGNED;
882 listener->proto = &proto_tcpv4;
883 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
884 proto_tcpv4.nb_listeners++;
885}
886
887/* Add listener to the list of tcpv4 listeners. The listener's state
888 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
889 * listeners is updated. This is the function to use to add a new listener.
890 */
891void tcpv6_add_listener(struct listener *listener)
892{
893 if (listener->state != LI_INIT)
894 return;
895 listener->state = LI_ASSIGNED;
896 listener->proto = &proto_tcpv6;
897 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
898 proto_tcpv6.nb_listeners++;
899}
900
Willy Tarreauedcf6682008-11-30 23:15:34 +0100901/* This function performs the TCP request analysis on the current request. It
902 * returns 1 if the processing can continue on next analysers, or zero if it
903 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200904 * request. It relies on buffers flags, and updates s->req->analysers. The
905 * function may be called for frontend rules and backend rules. It only relies
906 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100907 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200908int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100909{
910 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200911 struct stksess *ts;
912 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100913 int partial;
914
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100915 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 +0100916 now_ms, __FUNCTION__,
917 s,
918 req,
919 req->rex, req->wex,
920 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200921 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100922 req->analysers);
923
Willy Tarreauedcf6682008-11-30 23:15:34 +0100924 /* We don't know whether we have enough data, so must proceed
925 * this way :
926 * - iterate through all rules in their declaration order
927 * - if one rule returns MISS, it means the inspect delay is
928 * not over yet, then return immediately, otherwise consider
929 * it as a non-match.
930 * - if one rule returns OK, then return OK
931 * - if one rule returns KO, then return KO
932 */
933
Willy Tarreau9b28e032012-10-12 23:49:43 +0200934 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200935 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200936 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100937 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200938 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100939
Willy Tarreaufb356202010-08-03 14:02:05 +0200940 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +0100941 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100942
943 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200944 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100945 if (ret == ACL_TEST_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200946 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100947 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200948 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +0100949 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100950 return 0;
951 }
952
953 ret = acl_pass(ret);
954 if (rule->cond->pol == ACL_COND_UNLESS)
955 ret = !ret;
956 }
957
958 if (ret) {
959 /* we have a matching rule. */
960 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200961 channel_abort(req);
962 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100963 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200964
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100965 s->be->be_counters.denied_req++;
966 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200967 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200968 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200969
Willy Tarreauedcf6682008-11-30 23:15:34 +0100970 if (!(s->flags & SN_ERR_MASK))
971 s->flags |= SN_ERR_PRXCOND;
972 if (!(s->flags & SN_FINST_MASK))
973 s->flags |= SN_FINST_R;
974 return 0;
975 }
Willy Tarreau44778ad2013-10-30 19:24:00 +0100976 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100977 /* Note: only the first valid tracking parameter of each
978 * applies.
979 */
980 struct stktable_key *key;
981
Willy Tarreau44778ad2013-10-30 19:24:00 +0100982 if (s->stkctr[tcp_trk_idx(rule->action)].entry)
983 continue;
984
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100985 t = rule->act_prm.trk_ctr.table.t;
986 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
987
988 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200989 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
990 if (s->fe != s->be)
Willy Tarreaube4a3ef2013-06-17 15:04:07 +0200991 s->flags |= SN_BE_TRACK_SC0 << tcp_trk_idx(rule->action);
Willy Tarreaud1f96522010-08-03 19:34:32 +0200992 }
993 }
994 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100995 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200996 break;
997 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100998 }
999 }
1000
1001 /* if we get there, it means we have no rule which matches, or
1002 * we have an explicit accept, so we apply the default accept.
1003 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001004 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001005 req->analyse_exp = TICK_ETERNITY;
1006 return 1;
1007}
1008
Emeric Brun97679e72010-09-23 17:56:44 +02001009/* This function performs the TCP response analysis on the current response. It
1010 * returns 1 if the processing can continue on next analysers, or zero if it
1011 * needs more data, encounters an error, or wants to immediately abort the
1012 * response. It relies on buffers flags, and updates s->rep->analysers. The
1013 * function may be called for backend rules.
1014 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001015int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001016{
1017 struct tcp_rule *rule;
1018 int partial;
1019
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001020 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 +02001021 now_ms, __FUNCTION__,
1022 s,
1023 rep,
1024 rep->rex, rep->wex,
1025 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001026 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001027 rep->analysers);
1028
1029 /* We don't know whether we have enough data, so must proceed
1030 * this way :
1031 * - iterate through all rules in their declaration order
1032 * - if one rule returns MISS, it means the inspect delay is
1033 * not over yet, then return immediately, otherwise consider
1034 * it as a non-match.
1035 * - if one rule returns OK, then return OK
1036 * - if one rule returns KO, then return KO
1037 */
1038
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001039 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001040 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001041 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001042 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001043
1044 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001045 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001046
1047 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001048 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001049 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001050 /* just set the analyser timeout once at the beginning of the response */
1051 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001052 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001053 return 0;
1054 }
1055
1056 ret = acl_pass(ret);
1057 if (rule->cond->pol == ACL_COND_UNLESS)
1058 ret = !ret;
1059 }
1060
1061 if (ret) {
1062 /* we have a matching rule. */
1063 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001064 channel_abort(rep);
1065 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001066 rep->analysers = 0;
1067
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001068 s->be->be_counters.denied_resp++;
1069 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001070 if (s->listener->counters)
1071 s->listener->counters->denied_resp++;
1072
1073 if (!(s->flags & SN_ERR_MASK))
1074 s->flags |= SN_ERR_PRXCOND;
1075 if (!(s->flags & SN_FINST_MASK))
1076 s->flags |= SN_FINST_D;
1077 return 0;
1078 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001079 else if (rule->action == TCP_ACT_CLOSE) {
1080 rep->prod->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1081 si_shutr(rep->prod);
1082 si_shutw(rep->prod);
1083 break;
1084 }
Emeric Brun97679e72010-09-23 17:56:44 +02001085 else {
1086 /* otherwise accept */
1087 break;
1088 }
1089 }
1090 }
1091
1092 /* if we get there, it means we have no rule which matches, or
1093 * we have an explicit accept, so we apply the default accept.
1094 */
1095 rep->analysers &= ~an_bit;
1096 rep->analyse_exp = TICK_ETERNITY;
1097 return 1;
1098}
1099
1100
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001101/* This function performs the TCP layer4 analysis on the current request. It
1102 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1103 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001104 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001105 */
1106int tcp_exec_req_rules(struct session *s)
1107{
1108 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001109 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001110 struct stktable *t = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001111 struct connection *conn = objt_conn(s->si[0].end);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001112 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001113 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001114
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001115 if (!conn)
1116 return result;
1117
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001118 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001119 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001120
1121 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001122 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001123 ret = acl_pass(ret);
1124 if (rule->cond->pol == ACL_COND_UNLESS)
1125 ret = !ret;
1126 }
1127
1128 if (ret) {
1129 /* we have a matching rule. */
1130 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001131 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001132 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001133 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001134
1135 if (!(s->flags & SN_ERR_MASK))
1136 s->flags |= SN_ERR_PRXCOND;
1137 if (!(s->flags & SN_FINST_MASK))
1138 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001139 result = 0;
1140 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001141 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001142 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001143 /* Note: only the first valid tracking parameter of each
1144 * applies.
1145 */
1146 struct stktable_key *key;
1147
Willy Tarreau44778ad2013-10-30 19:24:00 +01001148 if (s->stkctr[tcp_trk_idx(rule->action)].entry)
1149 continue;
1150
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001151 t = rule->act_prm.trk_ctr.table.t;
1152 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
1153
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001154 if (key && (ts = stktable_get_entry(t, key)))
1155 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001156 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001157 else if (rule->action == TCP_ACT_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001158 conn->flags |= CO_FL_ACCEPT_PROXY;
1159 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001160 }
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 Tarreau80aca902013-01-07 15:42:20 +01001173 struct tcp_rule *rule, char **err,
1174 unsigned int where)
Emeric Brun97679e72010-09-23 17:56:44 +02001175{
1176 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001177 memprintf(err, "%s %s is only allowed in 'backend' sections",
1178 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001179 return -1;
1180 }
1181
1182 if (strcmp(args[arg], "accept") == 0) {
1183 arg++;
1184 rule->action = TCP_ACT_ACCEPT;
1185 }
1186 else if (strcmp(args[arg], "reject") == 0) {
1187 arg++;
1188 rule->action = TCP_ACT_REJECT;
1189 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001190 else if (strcmp(args[arg], "close") == 0) {
1191 arg++;
1192 rule->action = TCP_ACT_CLOSE;
1193 }
Emeric Brun97679e72010-09-23 17:56:44 +02001194 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001195 memprintf(err,
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001196 "'%s %s' expects 'accept', 'close' or 'reject' in %s '%s' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001197 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001198 return -1;
1199 }
1200
1201 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001202 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1203 memprintf(err,
1204 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1205 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001206 return -1;
1207 }
1208 }
1209 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001210 memprintf(err,
1211 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001212 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1213 return -1;
1214 }
1215 return 0;
1216}
1217
1218
1219
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001220/* Parse a tcp-request rule. Return a negative value in case of failure */
1221static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001222 struct proxy *curpx, struct proxy *defpx,
1223 struct tcp_rule *rule, char **err,
1224 unsigned int where)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001225{
1226 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001227 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1228 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001229 return -1;
1230 }
1231
1232 if (!strcmp(args[arg], "accept")) {
1233 arg++;
1234 rule->action = TCP_ACT_ACCEPT;
1235 }
1236 else if (!strcmp(args[arg], "reject")) {
1237 arg++;
1238 rule->action = TCP_ACT_REJECT;
1239 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001240 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1241 args[arg][9] == '\0' && args[arg][8] >= '0' &&
1242 args[arg][8] <= '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001243 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001244 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001245
1246 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001247
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001248 curpx->conf.args.ctx = ARGC_TRK;
Willy Tarreau975c1782013-12-12 23:16:54 +01001249 expr = sample_parse_expr(args, &arg, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001250 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001251 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001252 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001253 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001254 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001255 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001256
Willy Tarreau80aca902013-01-07 15:42:20 +01001257 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001258 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001259 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001260 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001261 free(expr);
1262 return -1;
1263 }
1264
1265 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001266 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001267
1268 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001269 arg++;
1270 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001271 memprintf(err,
1272 "'%s %s %s' : missing table name",
1273 args[0], args[1], args[kw]);
1274 free(expr);
1275 return -1;
1276 }
1277 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001278 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001279 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001280 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001281 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001282 rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001283 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001284 else if (strcmp(args[arg], "expect-proxy") == 0) {
1285 if (strcmp(args[arg+1], "layer4") != 0) {
1286 memprintf(err,
1287 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1288 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1289 return -1;
1290 }
1291
1292 if (!(where & SMP_VAL_FE_CON_ACC)) {
1293 memprintf(err,
1294 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1295 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1296 return -1;
1297 }
1298
1299 arg += 2;
1300 rule->action = TCP_ACT_EXPECT_PX;
1301 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001302 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001303 memprintf(err,
Willy Tarreaub4c84932013-07-23 19:15:30 +02001304 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d' "
1305 " in %s '%s' (got '%s')",
1306 args[0], args[1], MAX_SESS_STKCTR, proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001307 return -1;
1308 }
1309
1310 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001311 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1312 memprintf(err,
1313 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1314 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001315 return -1;
1316 }
1317 }
1318 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001319 memprintf(err,
1320 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001321 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1322 return -1;
1323 }
1324 return 0;
1325}
1326
Emeric Brun97679e72010-09-23 17:56:44 +02001327/* This function should be called to parse a line starting with the "tcp-response"
1328 * keyword.
1329 */
1330static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001331 struct proxy *defpx, const char *file, int line,
1332 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001333{
1334 const char *ptr = NULL;
1335 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001336 int warn = 0;
1337 int arg;
1338 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001339 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001340 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001341 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001342
1343 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001344 memprintf(err, "missing argument for '%s' in %s '%s'",
1345 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001346 return -1;
1347 }
1348
1349 if (strcmp(args[1], "inspect-delay") == 0) {
1350 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001351 memprintf(err, "%s %s is only allowed in 'backend' sections",
1352 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001353 return -1;
1354 }
1355
1356 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001357 memprintf(err,
1358 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1359 args[0], args[1], proxy_type_str(curpx), curpx->id);
1360 if (ptr)
1361 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001362 return -1;
1363 }
1364
1365 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001366 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1367 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001368 return 1;
1369 }
1370 curpx->tcp_rep.inspect_delay = val;
1371 return 0;
1372 }
1373
Simon Hormande072bd2011-06-24 15:11:37 +09001374 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001375 LIST_INIT(&rule->list);
1376 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001377 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001378
1379 if (strcmp(args[1], "content") == 0) {
1380 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001381
1382 if (curpx->cap & PR_CAP_FE)
1383 where |= SMP_VAL_FE_RES_CNT;
1384 if (curpx->cap & PR_CAP_BE)
1385 where |= SMP_VAL_BE_RES_CNT;
1386
1387 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001388 goto error;
1389
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001390 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1391 if (acl) {
1392 if (acl->name && *acl->name)
1393 memprintf(err,
1394 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1395 acl->name, args[0], args[1], sample_ckp_names(where));
1396 else
1397 memprintf(err,
1398 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1399 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001400 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001401 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001402
Emeric Brun97679e72010-09-23 17:56:44 +02001403 warn++;
1404 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001405 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1406 if (acl->name && *acl->name)
1407 memprintf(err,
1408 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001409 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001410 else
1411 memprintf(err,
1412 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001413 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001414 warn++;
1415 }
Emeric Brun97679e72010-09-23 17:56:44 +02001416
1417 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1418 }
1419 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001420 memprintf(err,
1421 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1422 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001423 goto error;
1424 }
1425
1426 return warn;
1427 error:
1428 free(rule);
1429 return -1;
1430}
1431
1432
Willy Tarreaub6866442008-07-14 23:54:42 +02001433/* This function should be called to parse a line starting with the "tcp-request"
1434 * keyword.
1435 */
1436static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001437 struct proxy *defpx, const char *file, int line,
1438 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001439{
1440 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001441 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001442 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001443 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001444 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001445 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001446 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001447 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001448
1449 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001450 if (curpx == defpx)
1451 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1452 else
1453 memprintf(err, "missing argument for '%s' in %s '%s'",
1454 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001455 return -1;
1456 }
1457
1458 if (!strcmp(args[1], "inspect-delay")) {
1459 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001460 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1461 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001462 return -1;
1463 }
1464
Willy Tarreaub6866442008-07-14 23:54:42 +02001465 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001466 memprintf(err,
1467 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1468 args[0], args[1], proxy_type_str(curpx), curpx->id);
1469 if (ptr)
1470 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001471 return -1;
1472 }
1473
1474 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001475 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1476 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001477 return 1;
1478 }
1479 curpx->tcp_req.inspect_delay = val;
1480 return 0;
1481 }
1482
Simon Hormande072bd2011-06-24 15:11:37 +09001483 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001484 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001485 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001486 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001487
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001488 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001489 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001490
1491 if (curpx->cap & PR_CAP_FE)
1492 where |= SMP_VAL_FE_REQ_CNT;
1493 if (curpx->cap & PR_CAP_BE)
1494 where |= SMP_VAL_BE_REQ_CNT;
1495
1496 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001497 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001498
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001499 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1500 if (acl) {
1501 if (acl->name && *acl->name)
1502 memprintf(err,
1503 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1504 acl->name, args[0], args[1], sample_ckp_names(where));
1505 else
1506 memprintf(err,
1507 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1508 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001509 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001510 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001511
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001512 warn++;
1513 }
1514 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1515 if (acl->name && *acl->name)
1516 memprintf(err,
1517 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001518 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001519 else
1520 memprintf(err,
1521 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001522 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001523 warn++;
1524 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001525
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001526 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001527 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001528 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001529 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001530
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001531 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001532 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1533 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001534 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001535 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001536
Willy Tarreau80aca902013-01-07 15:42:20 +01001537 where |= SMP_VAL_FE_CON_ACC;
1538
1539 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001540 goto error;
1541
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001542 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1543 if (acl) {
1544 if (acl->name && *acl->name)
1545 memprintf(err,
1546 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1547 acl->name, args[0], args[1], sample_ckp_names(where));
1548 else
1549 memprintf(err,
1550 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1551 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001552 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001553 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001554
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001555 warn++;
1556 }
1557 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1558 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001559 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001560 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001561 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001562 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001563 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001564 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001565 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001566 warn++;
1567 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001568
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001569 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001570 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001571 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001572 if (curpx == defpx)
1573 memprintf(err,
1574 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1575 args[0], args[1]);
1576 else
1577 memprintf(err,
1578 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001579 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001580 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001581 }
1582
Willy Tarreau1a687942010-05-23 22:40:30 +02001583 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001584 error:
1585 free(rule);
1586 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001587}
1588
Willy Tarreau645513a2010-05-24 20:55:15 +02001589
1590/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001591/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001592/************************************************************************/
1593
Willy Tarreau4a129812012-04-25 17:31:42 +02001594/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001595static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001596smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001597 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001598{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001599 struct connection *cli_conn = objt_conn(l4->si[0].end);
1600
1601 if (!cli_conn)
1602 return 0;
1603
1604 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001605 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001606 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001607 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001608 break;
1609 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001610 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001611 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001612 break;
1613 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001614 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001615 }
Emeric Brunf769f512010-10-22 17:14:01 +02001616
Willy Tarreau37406352012-04-23 16:16:37 +02001617 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001618 return 1;
1619}
1620
Willy Tarreaua5e37562011-12-16 17:06:15 +01001621/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001622static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001623smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001624 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001625{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001626 struct connection *cli_conn = objt_conn(l4->si[0].end);
1627
1628 if (!cli_conn)
1629 return 0;
1630
Willy Tarreauf853c462012-04-23 18:53:56 +02001631 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001632 if (!(smp->data.uint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001633 return 0;
1634
Willy Tarreau37406352012-04-23 16:16:37 +02001635 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001636 return 1;
1637}
1638
Willy Tarreau4a129812012-04-25 17:31:42 +02001639/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001640static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001641smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001642 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001643{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001644 struct connection *cli_conn = objt_conn(l4->si[0].end);
Willy Tarreau645513a2010-05-24 20:55:15 +02001645
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001646 if (!cli_conn)
1647 return 0;
1648
1649 conn_get_to_addr(cli_conn);
1650
1651 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001652 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001653 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001654 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001655 break;
1656 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001657 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001658 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001659 break;
1660 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001661 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001662 }
Emeric Brunf769f512010-10-22 17:14:01 +02001663
Willy Tarreau37406352012-04-23 16:16:37 +02001664 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001665 return 1;
1666}
1667
Willy Tarreaua5e37562011-12-16 17:06:15 +01001668/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001669static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001670smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001671 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001672{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001673 struct connection *cli_conn = objt_conn(l4->si[0].end);
1674
1675 if (!cli_conn)
1676 return 0;
1677
1678 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001679
Willy Tarreauf853c462012-04-23 18:53:56 +02001680 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001681 if (!(smp->data.uint = get_host_port(&cli_conn->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;
Emericf2d7cae2010-11-05 18:13:50 +01001686}
1687
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001688#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001689/* parse the "v4v6" bind keyword */
1690static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1691{
1692 struct listener *l;
1693
1694 list_for_each_entry(l, &conf->listeners, by_bind) {
1695 if (l->addr.ss_family == AF_INET6)
1696 l->options |= LI_O_V4V6;
1697 }
1698
1699 return 0;
1700}
1701
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001702/* parse the "v6only" bind keyword */
1703static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1704{
1705 struct listener *l;
1706
1707 list_for_each_entry(l, &conf->listeners, by_bind) {
1708 if (l->addr.ss_family == AF_INET6)
1709 l->options |= LI_O_V6ONLY;
1710 }
1711
1712 return 0;
1713}
1714#endif
1715
Pieter Baauwd551fb52013-05-08 22:49:23 +02001716#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001717/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001718static int bind_parse_transparent(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001719{
1720 struct listener *l;
1721
Willy Tarreau4348fad2012-09-20 16:48:07 +02001722 list_for_each_entry(l, &conf->listeners, by_bind) {
1723 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1724 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001725 }
1726
Willy Tarreau44791242012-09-12 23:27:21 +02001727 return 0;
1728}
1729#endif
1730
1731#ifdef TCP_DEFER_ACCEPT
1732/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001733static int bind_parse_defer_accept(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001734{
1735 struct listener *l;
1736
Willy Tarreau4348fad2012-09-20 16:48:07 +02001737 list_for_each_entry(l, &conf->listeners, by_bind) {
1738 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1739 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001740 }
1741
Willy Tarreau44791242012-09-12 23:27:21 +02001742 return 0;
1743}
1744#endif
1745
Willy Tarreau1c862c52012-10-05 16:21:00 +02001746#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001747/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001748static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1749{
1750 struct listener *l;
1751
1752 list_for_each_entry(l, &conf->listeners, by_bind) {
1753 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1754 l->options |= LI_O_TCP_FO;
1755 }
1756
1757 return 0;
1758}
1759#endif
1760
Willy Tarreau44791242012-09-12 23:27:21 +02001761#ifdef TCP_MAXSEG
1762/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001763static int bind_parse_mss(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001764{
1765 struct listener *l;
1766 int mss;
1767
Willy Tarreau44791242012-09-12 23:27:21 +02001768 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001769 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001770 return ERR_ALERT | ERR_FATAL;
1771 }
1772
1773 mss = atoi(args[cur_arg + 1]);
1774 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001775 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001776 return ERR_ALERT | ERR_FATAL;
1777 }
1778
Willy Tarreau4348fad2012-09-20 16:48:07 +02001779 list_for_each_entry(l, &conf->listeners, by_bind) {
1780 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1781 l->maxseg = mss;
1782 }
Willy Tarreau44791242012-09-12 23:27:21 +02001783
1784 return 0;
1785}
1786#endif
1787
1788#ifdef SO_BINDTODEVICE
1789/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001790static int bind_parse_interface(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau44791242012-09-12 23:27:21 +02001791{
1792 struct listener *l;
1793
Willy Tarreau44791242012-09-12 23:27:21 +02001794 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001795 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001796 return ERR_ALERT | ERR_FATAL;
1797 }
1798
Willy Tarreau4348fad2012-09-20 16:48:07 +02001799 list_for_each_entry(l, &conf->listeners, by_bind) {
1800 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1801 l->interface = strdup(args[cur_arg + 1]);
1802 }
Willy Tarreau44791242012-09-12 23:27:21 +02001803
1804 global.last_checks |= LSTCHK_NETADM;
1805 return 0;
1806}
1807#endif
1808
Willy Tarreaudc13c112013-06-21 23:16:39 +02001809static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001810 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001811 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001812 { 0, NULL, NULL },
1813}};
1814
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001815
Willy Tarreau61612d42012-04-19 18:42:05 +02001816/* Note: must not be declared <const> as its list will be overwritten.
1817 * Please take care of keeping this list alphabetically sorted.
1818 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001819static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001820 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02001821}};
1822
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001823
Willy Tarreau4a129812012-04-25 17:31:42 +02001824/* Note: must not be declared <const> as its list will be overwritten.
1825 * Note: fetches that may return multiple types must be declared as the lowest
1826 * common denominator, the type that can be casted into all other ones. For
1827 * instance v4/v6 must be declared v4.
1828 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001829static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001830 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1831 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1832 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1833 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1834 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001835}};
1836
Willy Tarreau44791242012-09-12 23:27:21 +02001837/************************************************************************/
1838/* All supported bind keywords must be declared here. */
1839/************************************************************************/
1840
1841/* Note: must not be declared <const> as its list will be overwritten.
1842 * Please take care of keeping this list alphabetically sorted, doing so helps
1843 * all code contributors.
1844 * Optional keywords are also declared with a NULL ->parse() function so that
1845 * the config parser can report an appropriate error when a known keyword was
1846 * not enabled.
1847 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001848static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001849#ifdef TCP_DEFER_ACCEPT
1850 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1851#endif
1852#ifdef SO_BINDTODEVICE
1853 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1854#endif
1855#ifdef TCP_MAXSEG
1856 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1857#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001858#ifdef TCP_FASTOPEN
1859 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1860#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001861#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001862 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1863#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001864#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001865 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001866 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1867#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001868 /* the versions with the NULL parse function*/
1869 { "defer-accept", NULL, 0 },
1870 { "interface", NULL, 1 },
1871 { "mss", NULL, 1 },
1872 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001873 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001874 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001875 { NULL, NULL, 0 },
1876}};
1877
Willy Tarreaue6b98942007-10-29 01:09:36 +01001878__attribute__((constructor))
1879static void __tcp_protocol_init(void)
1880{
1881 protocol_register(&proto_tcpv4);
1882 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001883 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001884 cfg_register_keywords(&cfg_kws);
1885 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001886 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001887}
1888
1889
1890/*
1891 * Local variables:
1892 * c-indent-level: 8
1893 * c-basic-offset: 8
1894 * End:
1895 */