blob: 86a67c4479dbd2b5382cdcca67c89b43e5fc76a9 [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 Tarreaua975b8f2010-06-05 19:13:27 +020054#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010055
Willy Tarreaue8c66af2008-01-13 18:40:14 +010056#ifdef CONFIG_HAP_CTTPROXY
57#include <import/ip_tproxy.h>
58#endif
59
Emeric Bruncf20bf12010-10-22 16:06:11 +020060static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
61static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010062
63/* Note: must not be declared <const> as its list will be overwritten */
64static struct protocol proto_tcpv4 = {
65 .name = "tcpv4",
66 .sock_domain = AF_INET,
67 .sock_type = SOCK_STREAM,
68 .sock_prot = IPPROTO_TCP,
69 .sock_family = AF_INET,
70 .sock_addrlen = sizeof(struct sockaddr_in),
71 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020072 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020073 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020074 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010075 .bind_all = tcp_bind_listeners,
76 .unbind_all = unbind_all_listeners,
77 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020078 .get_src = tcp_get_src,
79 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010080 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
81 .nb_listeners = 0,
82};
83
84/* Note: must not be declared <const> as its list will be overwritten */
85static struct protocol proto_tcpv6 = {
86 .name = "tcpv6",
87 .sock_domain = AF_INET6,
88 .sock_type = SOCK_STREAM,
89 .sock_prot = IPPROTO_TCP,
90 .sock_family = AF_INET6,
91 .sock_addrlen = sizeof(struct sockaddr_in6),
92 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020093 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020094 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020095 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010096 .bind_all = tcp_bind_listeners,
97 .unbind_all = unbind_all_listeners,
98 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020099 .get_src = tcp_get_src,
100 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100101 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
102 .nb_listeners = 0,
103};
104
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100105
David du Colombier6f5ccb12011-03-10 22:26:24 +0100106/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100107 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
108 * - 0 : ignore remote address (may even be a NULL pointer)
109 * - 1 : use provided address
110 * - 2 : use provided port
111 * - 3 : use both
112 *
113 * The function supports multiple foreign binding methods :
114 * - linux_tproxy: we directly bind to the foreign address
115 * - cttproxy: we bind to a local address then nat.
116 * The second one can be used as a fallback for the first one.
117 * This function returns 0 when everything's OK, 1 if it could not bind, to the
118 * local address, 2 if it could not bind to the foreign address.
119 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100120int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100121{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100122 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100123 int foreign_ok = 0;
124 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100125 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200126 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200127
David du Colombier65c17962012-07-13 14:34:59 +0200128 switch (local->ss_family) {
129 case AF_INET:
130 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200131 /* This deserves some explanation. Some platforms will support
132 * multiple combinations of certain methods, so we try the
133 * supported ones until one succeeds.
134 */
135 if (0
136#if defined(IP_TRANSPARENT)
137 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
138#endif
139#if defined(IP_FREEBIND)
140 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
141#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200142#if defined(IP_BINDANY)
143 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
144#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200145#if defined(SO_BINDANY)
146 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
147#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200148 )
David du Colombier65c17962012-07-13 14:34:59 +0200149 foreign_ok = 1;
150 else
151 ip_transp_working = 0;
152 }
153 break;
154 case AF_INET6:
155 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200156 if (0
157#if defined(IPV6_TRANSPARENT)
158 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
159#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200160#if defined(IPV6_BINDANY)
161 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
162#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200163#if defined(SO_BINDANY)
164 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
165#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200166 )
David du Colombier65c17962012-07-13 14:34:59 +0200167 foreign_ok = 1;
168 else
169 ip6_transp_working = 0;
170 }
171 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100172 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200173
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100174 if (flags) {
175 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200176 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100177 switch (remote->ss_family) {
178 case AF_INET:
179 if (flags & 1)
180 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
181 if (flags & 2)
182 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
183 break;
184 case AF_INET6:
185 if (flags & 1)
186 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
187 if (flags & 2)
188 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
189 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100190 default:
191 /* we don't want to try to bind to an unknown address family */
192 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100193 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100194 }
195
Simon Hormande072bd2011-06-24 15:11:37 +0900196 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100197 if (foreign_ok) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200198 if (is_addr(&bind_addr)) {
199 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
200 if (ret < 0)
201 return 2;
202 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100203 }
204 else {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200205 if (is_addr(local)) {
206 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
207 if (ret < 0)
208 return 1;
209 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100210 }
211
212 if (!flags)
213 return 0;
214
215#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100216 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100217 struct in_tproxy itp1, itp2;
218 memset(&itp1, 0, sizeof(itp1));
219
220 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100221 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
222 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100223
224 /* set connect flag on socket */
225 itp2.op = TPROXY_FLAGS;
226 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
227
228 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
229 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
230 foreign_ok = 1;
231 }
232 }
233#endif
234 if (!foreign_ok)
235 /* we could not bind to a foreign address */
236 return 2;
237
238 return 0;
239}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100240
Willy Tarreau9650f372009-08-16 14:02:45 +0200241
242/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200243 * This function initiates a TCP connection establishment to the target assigned
244 * to connection <conn> using (si->{target,addr.to}). A source address may be
245 * pointed to by conn->addr.from in case of transparent proxying. Normal source
246 * bind addresses are still determined locally (due to the possible need of a
247 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100248 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100249 * supported. The <data> parameter is a boolean indicating whether there are data
250 * waiting for being sent or not, in order to adjust data write polling and on
251 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
252 * allows the caller to force using a delayed ACK when establishing the connection :
253 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
254 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
255 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200256 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200257 * It can return one of :
258 * - SN_ERR_NONE if everything's OK
259 * - SN_ERR_SRVTO if there are no more servers
260 * - SN_ERR_SRVCL if the connection was refused by the server
261 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
262 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
263 * - SN_ERR_INTERNAL for any other purely internal errors
264 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100265 *
266 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
267 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200268 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100269
Willy Tarreauf0837b22012-11-24 10:24:27 +0100270int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200271{
272 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100273 struct server *srv;
274 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100275 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100276
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100277 switch (obj_type(conn->target)) {
278 case OBJ_TYPE_PROXY:
279 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100280 srv = NULL;
281 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100282 case OBJ_TYPE_SERVER:
283 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100284 be = srv->proxy;
285 break;
286 default:
287 return SN_ERR_INTERNAL;
288 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200289
Willy Tarreau986a9d22012-08-30 21:11:38 +0200290 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200291 qfprintf(stderr, "Cannot get a server socket.\n");
292
293 if (errno == ENFILE)
294 send_log(be, LOG_EMERG,
295 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
296 be->id, maxfd);
297 else if (errno == EMFILE)
298 send_log(be, LOG_EMERG,
299 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
300 be->id, maxfd);
301 else if (errno == ENOBUFS || errno == ENOMEM)
302 send_log(be, LOG_EMERG,
303 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
304 be->id, maxfd);
305 /* this is a resource error */
306 return SN_ERR_RESOURCE;
307 }
308
309 if (fd >= global.maxsock) {
310 /* do not log anything there, it's a normal condition when this option
311 * is used to serialize connections to a server !
312 */
313 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
314 close(fd);
315 return SN_ERR_PRXCOND; /* it is a configuration limit */
316 }
317
318 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900319 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200320 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
321 close(fd);
322 return SN_ERR_INTERNAL;
323 }
324
325 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900326 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200327
Willy Tarreau9650f372009-08-16 14:02:45 +0200328 /* allow specific binding :
329 * - server-specific at first
330 * - proxy-specific next
331 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100332 if (srv && srv->conn_src.opts & CO_SRC_BIND)
333 src = &srv->conn_src;
334 else if (be->conn_src.opts & CO_SRC_BIND)
335 src = &be->conn_src;
336 else
337 src = NULL;
338
339 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200340 int ret, flags = 0;
341
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200342 if (is_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100343 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100344 case CO_SRC_TPROXY_ADDR:
345 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200346 flags = 3;
347 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100348 case CO_SRC_TPROXY_CIP:
349 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200350 flags = 1;
351 break;
352 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200353 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200354
Willy Tarreau9650f372009-08-16 14:02:45 +0200355#ifdef SO_BINDTODEVICE
356 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100357 if (src->iface_name)
358 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200359#endif
360
Willy Tarreaua4380b42012-12-08 22:49:11 +0100361 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200362 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100363 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200364
365 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100366 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200367
368 do {
369 /* note: in case of retry, we may have to release a previously
370 * allocated port, hence this loop's construct.
371 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200372 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
373 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200374
375 if (!attempts)
376 break;
377 attempts--;
378
Willy Tarreaua4380b42012-12-08 22:49:11 +0100379 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200380 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200381 break;
382
Willy Tarreaua4380b42012-12-08 22:49:11 +0100383 fdinfo[fd].port_range = src->sport_range;
384 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200385
Willy Tarreaua4380b42012-12-08 22:49:11 +0100386 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200387 } while (ret != 0); /* binding NOK */
388 }
389 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100390 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200391 }
392
Willy Tarreaua4380b42012-12-08 22:49:11 +0100393 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200394 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
395 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200396 close(fd);
397
Willy Tarreau9650f372009-08-16 14:02:45 +0200398 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100399 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200400 be->id);
401 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100402 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200403 be->id);
404 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100405 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200406 be->id);
407 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100408 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200409 be->id);
410 }
411 return SN_ERR_RESOURCE;
412 }
413 }
414
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400415#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200416 /* disabling tcp quick ack now allows the first request to leave the
417 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100418 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200419 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100420 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900421 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200422#endif
423
Willy Tarreaue803de22010-01-21 17:43:04 +0100424 if (global.tune.server_sndbuf)
425 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
426
427 if (global.tune.server_rcvbuf)
428 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
429
Willy Tarreau986a9d22012-08-30 21:11:38 +0200430 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200431 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
432
Willy Tarreaub1719512012-12-08 23:03:28 +0100433 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200434 char *msg;
Willy Tarreaub1719512012-12-08 23:03:28 +0100435 if (errno == EAGAIN || errno == EADDRNOTAVAIL)
Willy Tarreau9650f372009-08-16 14:02:45 +0200436 msg = "no free ports";
437 else
438 msg = "local address already in use";
439
Willy Tarreaub1719512012-12-08 23:03:28 +0100440 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200441 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
442 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200443 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100444 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 return SN_ERR_RESOURCE;
446 } else if (errno == ETIMEDOUT) {
447 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200448 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
449 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200450 close(fd);
451 return SN_ERR_SRVTO;
452 } else {
453 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
454 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200455 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
456 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200457 close(fd);
458 return SN_ERR_SRVCL;
459 }
460 }
461
Willy Tarreau986a9d22012-08-30 21:11:38 +0200462 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200463 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100464 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200465
Willy Tarreaud2274c62012-07-06 14:29:45 +0200466 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200467 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200468 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200469
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200470 if (conn_xprt_init(conn) < 0) {
Willy Tarreau184636e2012-09-06 14:04:41 +0200471 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200472 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200473 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200474
Willy Tarreau14f8e862012-08-30 22:23:13 +0200475 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200476 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200477
Willy Tarreau9650f372009-08-16 14:02:45 +0200478 return SN_ERR_NONE; /* connection is OK */
479}
480
481
Willy Tarreau59b94792012-05-11 16:16:40 +0200482/*
483 * Retrieves the source address for the socket <fd>, with <dir> indicating
484 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
485 * success, -1 in case of error. The socket's source address is stored in
486 * <sa> for <salen> bytes.
487 */
488int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
489{
490 if (dir)
491 return getsockname(fd, sa, &salen);
492 else
493 return getpeername(fd, sa, &salen);
494}
495
496
497/*
498 * Retrieves the original destination address for the socket <fd>, with <dir>
499 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
500 * listener, if the original destination address was translated, the original
501 * address is retrieved. It returns 0 in case of success, -1 in case of error.
502 * The socket's source address is stored in <sa> for <salen> bytes.
503 */
504int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
505{
506 if (dir)
507 return getpeername(fd, sa, &salen);
508#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
509 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
510 return 0;
511#endif
512 else
513 return getsockname(fd, sa, &salen);
514}
515
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200516/* This is the callback which is set when a connection establishment is pending
517 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200518 * once the connection is established. It updates the FD polling status. It
519 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
520 * it returns non-zero and removes itself from the connection's flags (the bit is
521 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200522 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200523int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200524{
Willy Tarreau239d7182012-07-23 18:53:03 +0200525 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200526
Willy Tarreau80184712012-07-06 14:54:49 +0200527 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200528 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200529
Willy Tarreau80184712012-07-06 14:54:49 +0200530 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200531 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200532
Willy Tarreau2da156f2012-07-23 15:07:23 +0200533 /* stop here if we reached the end of data */
534 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
535 goto out_error;
536
Willy Tarreau2c6be842012-07-06 17:12:34 +0200537 /* We have no data to send to check the connection, and
538 * getsockopt() will not inform us whether the connection
539 * is still pending. So we'll reuse connect() to check the
540 * state of the socket. This has the advantage of giving us
541 * the following info :
542 * - error
543 * - connecting (EALREADY, EINPROGRESS)
544 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200545 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200546 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200547 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100548 __conn_sock_stop_recv(conn);
549 __conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200550 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200551 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200552
Willy Tarreau2c6be842012-07-06 17:12:34 +0200553 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200554 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200555
Willy Tarreau2c6be842012-07-06 17:12:34 +0200556 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200557 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200558
Willy Tarreau076be252012-07-06 16:02:29 +0200559 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200560 * forward the event to the transport layer which will notify the
561 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200562 */
Willy Tarreau80184712012-07-06 14:54:49 +0200563 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200564 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200565
566 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200567 /* Write error on the file descriptor. Report it to the connection
568 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200569 */
570
Willy Tarreau80184712012-07-06 14:54:49 +0200571 conn->flags |= CO_FL_ERROR;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100572 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200573 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200574}
575
Willy Tarreau59b94792012-05-11 16:16:40 +0200576
Willy Tarreaue6b98942007-10-29 01:09:36 +0100577/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100578 * an error message in <errmsg> if the message is at most <errlen> bytes long
579 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
580 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100581 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
582 * was alright and that no message was returned. ERR_RETRYABLE means that an
583 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700584 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100585 * the meaning of the error, but just indicate that a message is present which
586 * should be displayed with the respective level. Last, ERR_ABORT indicates
587 * that it's pointless to try to start other listeners. No error message is
588 * returned if errlen is NULL.
589 */
590int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
591{
592 __label__ tcp_return, tcp_close_return;
593 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100594 int ext, ready;
595 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100596 const char *msg = NULL;
597
598 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100599 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100600 *errmsg = 0;
601
602 if (listener->state != LI_ASSIGNED)
603 return ERR_NONE; /* already bound */
604
605 err = ERR_NONE;
606
Willy Tarreau40aa0702013-03-10 23:51:38 +0100607 /* if the listener already has an fd assigned, then we were offered the
608 * fd by an external process (most likely the parent), and we don't want
609 * to create a new socket. However we still want to set a few flags on
610 * the socket.
611 */
612 fd = listener->fd;
613 ext = (fd >= 0);
614
615 if (!ext && (fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100616 err |= ERR_RETRYABLE | ERR_ALERT;
617 msg = "cannot create listening socket";
618 goto tcp_return;
619 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100620
Willy Tarreaue6b98942007-10-29 01:09:36 +0100621 if (fd >= global.maxsock) {
622 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
623 msg = "not enough free sockets (raise '-n' parameter)";
624 goto tcp_close_return;
625 }
626
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200627 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100628 err |= ERR_FATAL | ERR_ALERT;
629 msg = "cannot make socket non-blocking";
630 goto tcp_close_return;
631 }
632
Willy Tarreau40aa0702013-03-10 23:51:38 +0100633 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100634 /* not fatal but should be reported */
635 msg = "cannot do so_reuseaddr";
636 err |= ERR_ALERT;
637 }
638
639 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900640 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100641
Willy Tarreaue6b98942007-10-29 01:09:36 +0100642#ifdef SO_REUSEPORT
643 /* OpenBSD supports this. As it's present in old libc versions of Linux,
644 * it might return an error that we will silently ignore.
645 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100646 if (!ext)
647 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100648#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200649
Willy Tarreau40aa0702013-03-10 23:51:38 +0100650 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200651 switch (listener->addr.ss_family) {
652 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200653 if (1
654#if defined(IP_TRANSPARENT)
655 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
656#endif
657#if defined(IP_FREEBIND)
658 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
659#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200660#if defined(IP_BINDANY)
661 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
662#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200663#if defined(SO_BINDANY)
664 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
665#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200666 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200667 msg = "cannot make listening socket transparent";
668 err |= ERR_ALERT;
669 }
670 break;
671 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200672 if (1
673#if defined(IPV6_TRANSPARENT)
674 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
675#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200676#if defined(IPV6_BINDANY)
677 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
678#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200679#if defined(SO_BINDANY)
680 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
681#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200682 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200683 msg = "cannot make listening socket transparent";
684 err |= ERR_ALERT;
685 }
686 break;
687 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100688 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200689
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100690#ifdef SO_BINDTODEVICE
691 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100692 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100693 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100694 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100695 msg = "cannot bind listener to device";
696 err |= ERR_WARN;
697 }
698 }
699#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400700#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100701 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400702 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200703 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
704 msg = "cannot set MSS";
705 err |= ERR_WARN;
706 }
707 }
708#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200709#if defined(TCP_DEFER_ACCEPT)
710 if (listener->options & LI_O_DEF_ACCEPT) {
711 /* defer accept by up to one second */
712 int accept_delay = 1;
713 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
714 msg = "cannot enable DEFER_ACCEPT";
715 err |= ERR_WARN;
716 }
717 }
718#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200719#if defined(TCP_FASTOPEN)
720 if (listener->options & LI_O_TCP_FO) {
721 /* TFO needs a queue length, let's use the configured backlog */
722 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
723 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
724 msg = "cannot enable TCP_FASTOPEN";
725 err |= ERR_WARN;
726 }
727 }
728#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100729#if defined(IPV6_V6ONLY)
730 if (listener->options & LI_O_V6ONLY)
731 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100732 else if (listener->options & LI_O_V4V6)
733 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100734#endif
735
Willy Tarreau40aa0702013-03-10 23:51:38 +0100736 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100737 err |= ERR_RETRYABLE | ERR_ALERT;
738 msg = "cannot bind socket";
739 goto tcp_close_return;
740 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100741
Willy Tarreau40aa0702013-03-10 23:51:38 +0100742 ready = 0;
743 ready_len = sizeof(ready);
744 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
745 ready = 0;
746
747 if (!(ext && ready) && /* only listen if not already done by external process */
748 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100749 err |= ERR_RETRYABLE | ERR_ALERT;
750 msg = "cannot listen to socket";
751 goto tcp_close_return;
752 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100753
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400754#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200755 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900756 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200757#endif
758
Willy Tarreaue6b98942007-10-29 01:09:36 +0100759 /* the socket is ready */
760 listener->fd = fd;
761 listener->state = LI_LISTEN;
762
Willy Tarreaueabf3132008-08-29 23:36:51 +0200763 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200764 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200765 fd_insert(fd);
766
Willy Tarreaue6b98942007-10-29 01:09:36 +0100767 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100768 if (msg && errlen) {
769 char pn[INET6_ADDRSTRLEN];
770
Willy Tarreau631f01c2011-09-05 00:36:48 +0200771 addr_to_str(&listener->addr, pn, sizeof(pn));
772 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100773 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100774 return err;
775
776 tcp_close_return:
777 close(fd);
778 goto tcp_return;
779}
780
781/* This function creates all TCP sockets bound to the protocol entry <proto>.
782 * It is intended to be used as the protocol's bind_all() function.
783 * The sockets will be registered but not added to any fd_set, in order not to
784 * loose them across the fork(). A call to enable_all_listeners() is needed
785 * to complete initialization. The return value is composed from ERR_*.
786 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200787static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100788{
789 struct listener *listener;
790 int err = ERR_NONE;
791
792 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200793 err |= tcp_bind_listener(listener, errmsg, errlen);
794 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100795 break;
796 }
797
798 return err;
799}
800
801/* Add listener to the list of tcpv4 listeners. The listener's state
802 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
803 * listeners is updated. This is the function to use to add a new listener.
804 */
805void tcpv4_add_listener(struct listener *listener)
806{
807 if (listener->state != LI_INIT)
808 return;
809 listener->state = LI_ASSIGNED;
810 listener->proto = &proto_tcpv4;
811 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
812 proto_tcpv4.nb_listeners++;
813}
814
815/* Add listener to the list of tcpv4 listeners. The listener's state
816 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
817 * listeners is updated. This is the function to use to add a new listener.
818 */
819void tcpv6_add_listener(struct listener *listener)
820{
821 if (listener->state != LI_INIT)
822 return;
823 listener->state = LI_ASSIGNED;
824 listener->proto = &proto_tcpv6;
825 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
826 proto_tcpv6.nb_listeners++;
827}
828
Willy Tarreauedcf6682008-11-30 23:15:34 +0100829/* This function performs the TCP request analysis on the current request. It
830 * returns 1 if the processing can continue on next analysers, or zero if it
831 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200832 * request. It relies on buffers flags, and updates s->req->analysers. The
833 * function may be called for frontend rules and backend rules. It only relies
834 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100835 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200836int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100837{
838 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200839 struct stksess *ts;
840 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100841 int partial;
842
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100843 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 +0100844 now_ms, __FUNCTION__,
845 s,
846 req,
847 req->rex, req->wex,
848 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200849 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100850 req->analysers);
851
Willy Tarreauedcf6682008-11-30 23:15:34 +0100852 /* We don't know whether we have enough data, so must proceed
853 * this way :
854 * - iterate through all rules in their declaration order
855 * - if one rule returns MISS, it means the inspect delay is
856 * not over yet, then return immediately, otherwise consider
857 * it as a non-match.
858 * - if one rule returns OK, then return OK
859 * - if one rule returns KO, then return KO
860 */
861
Willy Tarreau9b28e032012-10-12 23:49:43 +0200862 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200863 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200864 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100865 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200866 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100867
Willy Tarreaufb356202010-08-03 14:02:05 +0200868 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100869 int ret = ACL_PAT_PASS;
870
871 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200872 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100873 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200874 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100875 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200876 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
877 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100878 return 0;
879 }
880
881 ret = acl_pass(ret);
882 if (rule->cond->pol == ACL_COND_UNLESS)
883 ret = !ret;
884 }
885
886 if (ret) {
887 /* we have a matching rule. */
888 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200889 channel_abort(req);
890 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100891 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200892
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100893 s->be->be_counters.denied_req++;
894 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200895 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200896 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200897
Willy Tarreauedcf6682008-11-30 23:15:34 +0100898 if (!(s->flags & SN_ERR_MASK))
899 s->flags |= SN_ERR_PRXCOND;
900 if (!(s->flags & SN_FINST_MASK))
901 s->flags |= SN_FINST_R;
902 return 0;
903 }
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200904 else if ((rule->action >= TCP_ACT_TRK_SC1 && rule->action <= TCP_ACT_TRK_SC2) &&
905 !s->stkctr[tcp_trk_idx(rule->action)].entry) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100906 /* Note: only the first valid tracking parameter of each
907 * applies.
908 */
909 struct stktable_key *key;
910
911 t = rule->act_prm.trk_ctr.table.t;
912 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
913
914 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200915 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
916 if (s->fe != s->be)
917 s->flags |= SN_BE_TRACK_SC1 << tcp_trk_idx(rule->action);
Willy Tarreaud1f96522010-08-03 19:34:32 +0200918 }
919 }
920 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100921 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200922 break;
923 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100924 }
925 }
926
927 /* if we get there, it means we have no rule which matches, or
928 * we have an explicit accept, so we apply the default accept.
929 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200930 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100931 req->analyse_exp = TICK_ETERNITY;
932 return 1;
933}
934
Emeric Brun97679e72010-09-23 17:56:44 +0200935/* This function performs the TCP response analysis on the current response. It
936 * returns 1 if the processing can continue on next analysers, or zero if it
937 * needs more data, encounters an error, or wants to immediately abort the
938 * response. It relies on buffers flags, and updates s->rep->analysers. The
939 * function may be called for backend rules.
940 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200941int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200942{
943 struct tcp_rule *rule;
944 int partial;
945
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100946 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 +0200947 now_ms, __FUNCTION__,
948 s,
949 rep,
950 rep->rex, rep->wex,
951 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200952 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200953 rep->analysers);
954
955 /* We don't know whether we have enough data, so must proceed
956 * this way :
957 * - iterate through all rules in their declaration order
958 * - if one rule returns MISS, it means the inspect delay is
959 * not over yet, then return immediately, otherwise consider
960 * it as a non-match.
961 * - if one rule returns OK, then return OK
962 * - if one rule returns KO, then return KO
963 */
964
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200965 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200966 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200967 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200968 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200969
970 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
971 int ret = ACL_PAT_PASS;
972
973 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200974 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200975 if (ret == ACL_PAT_MISS) {
976 /* just set the analyser timeout once at the beginning of the response */
977 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
978 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
979 return 0;
980 }
981
982 ret = acl_pass(ret);
983 if (rule->cond->pol == ACL_COND_UNLESS)
984 ret = !ret;
985 }
986
987 if (ret) {
988 /* we have a matching rule. */
989 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200990 channel_abort(rep);
991 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200992 rep->analysers = 0;
993
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100994 s->be->be_counters.denied_resp++;
995 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200996 if (s->listener->counters)
997 s->listener->counters->denied_resp++;
998
999 if (!(s->flags & SN_ERR_MASK))
1000 s->flags |= SN_ERR_PRXCOND;
1001 if (!(s->flags & SN_FINST_MASK))
1002 s->flags |= SN_FINST_D;
1003 return 0;
1004 }
1005 else {
1006 /* otherwise accept */
1007 break;
1008 }
1009 }
1010 }
1011
1012 /* if we get there, it means we have no rule which matches, or
1013 * we have an explicit accept, so we apply the default accept.
1014 */
1015 rep->analysers &= ~an_bit;
1016 rep->analyse_exp = TICK_ETERNITY;
1017 return 1;
1018}
1019
1020
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001021/* This function performs the TCP layer4 analysis on the current request. It
1022 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1023 * matches or if no more rule matches. It can only use rules which don't need
1024 * any data.
1025 */
1026int tcp_exec_req_rules(struct session *s)
1027{
1028 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001029 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001030 struct stktable *t = NULL;
1031 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001032 int ret;
1033
1034 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1035 ret = ACL_PAT_PASS;
1036
1037 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001038 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001039 ret = acl_pass(ret);
1040 if (rule->cond->pol == ACL_COND_UNLESS)
1041 ret = !ret;
1042 }
1043
1044 if (ret) {
1045 /* we have a matching rule. */
1046 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001047 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001048 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001049 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001050
1051 if (!(s->flags & SN_ERR_MASK))
1052 s->flags |= SN_ERR_PRXCOND;
1053 if (!(s->flags & SN_FINST_MASK))
1054 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001055 result = 0;
1056 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001057 }
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001058 else if ((rule->action >= TCP_ACT_TRK_SC1 && rule->action <= TCP_ACT_TRK_SC2) &&
1059 !s->stkctr[tcp_trk_idx(rule->action)].entry) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001060 /* Note: only the first valid tracking parameter of each
1061 * applies.
1062 */
1063 struct stktable_key *key;
1064
1065 t = rule->act_prm.trk_ctr.table.t;
1066 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
1067
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001068 if (key && (ts = stktable_get_entry(t, key)))
1069 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001070 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001071 else {
1072 /* otherwise it's an accept */
1073 break;
1074 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001075 }
1076 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001077 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001078}
1079
Emeric Brun97679e72010-09-23 17:56:44 +02001080/* Parse a tcp-response rule. Return a negative value in case of failure */
1081static int tcp_parse_response_rule(char **args, int arg, int section_type,
1082 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau80aca902013-01-07 15:42:20 +01001083 struct tcp_rule *rule, char **err,
1084 unsigned int where)
Emeric Brun97679e72010-09-23 17:56:44 +02001085{
1086 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001087 memprintf(err, "%s %s is only allowed in 'backend' sections",
1088 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001089 return -1;
1090 }
1091
1092 if (strcmp(args[arg], "accept") == 0) {
1093 arg++;
1094 rule->action = TCP_ACT_ACCEPT;
1095 }
1096 else if (strcmp(args[arg], "reject") == 0) {
1097 arg++;
1098 rule->action = TCP_ACT_REJECT;
1099 }
1100 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001101 memprintf(err,
1102 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1103 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001104 return -1;
1105 }
1106
1107 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001108 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1109 memprintf(err,
1110 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1111 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001112 return -1;
1113 }
1114 }
1115 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001116 memprintf(err,
1117 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001118 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1119 return -1;
1120 }
1121 return 0;
1122}
1123
1124
1125
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001126/* Parse a tcp-request rule. Return a negative value in case of failure */
1127static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001128 struct proxy *curpx, struct proxy *defpx,
1129 struct tcp_rule *rule, char **err,
1130 unsigned int where)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001131{
1132 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001133 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1134 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001135 return -1;
1136 }
1137
1138 if (!strcmp(args[arg], "accept")) {
1139 arg++;
1140 rule->action = TCP_ACT_ACCEPT;
1141 }
1142 else if (!strcmp(args[arg], "reject")) {
1143 arg++;
1144 rule->action = TCP_ACT_REJECT;
1145 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001146 else if (strcmp(args[arg], "track-sc1") == 0 || strcmp(args[arg], "track-sc2") == 0) {
1147 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001148 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001149
1150 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001151
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001152 curpx->conf.args.ctx = ARGC_TRK;
1153 expr = sample_parse_expr(args, &arg, trash.str, trash.size, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001154 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001155 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001156 "'%s %s %s' : %s",
1157 args[0], args[1], args[kw], trash.str);
1158 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001159 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001160
Willy Tarreau80aca902013-01-07 15:42:20 +01001161 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001162 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001163 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001164 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001165 free(expr);
1166 return -1;
1167 }
1168
1169 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001170 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001171
1172 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001173 arg++;
1174 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001175 memprintf(err,
1176 "'%s %s %s' : missing table name",
1177 args[0], args[1], args[kw]);
1178 free(expr);
1179 return -1;
1180 }
1181 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001182 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001183 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001184 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001185 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001186 rule->action = TCP_ACT_TRK_SC1 + args[kw][8] - '1';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001187 }
1188 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001189 memprintf(err,
1190 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1191 "or 'track-sc2' in %s '%s' (got '%s')",
1192 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001193 return -1;
1194 }
1195
1196 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001197 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1198 memprintf(err,
1199 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1200 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001201 return -1;
1202 }
1203 }
1204 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001205 memprintf(err,
1206 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001207 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1208 return -1;
1209 }
1210 return 0;
1211}
1212
Emeric Brun97679e72010-09-23 17:56:44 +02001213/* This function should be called to parse a line starting with the "tcp-response"
1214 * keyword.
1215 */
1216static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001217 struct proxy *defpx, const char *file, int line,
1218 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001219{
1220 const char *ptr = NULL;
1221 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001222 int warn = 0;
1223 int arg;
1224 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001225 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001226 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001227 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001228
1229 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001230 memprintf(err, "missing argument for '%s' in %s '%s'",
1231 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001232 return -1;
1233 }
1234
1235 if (strcmp(args[1], "inspect-delay") == 0) {
1236 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001237 memprintf(err, "%s %s is only allowed in 'backend' sections",
1238 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001239 return -1;
1240 }
1241
1242 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001243 memprintf(err,
1244 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1245 args[0], args[1], proxy_type_str(curpx), curpx->id);
1246 if (ptr)
1247 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001248 return -1;
1249 }
1250
1251 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001252 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1253 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001254 return 1;
1255 }
1256 curpx->tcp_rep.inspect_delay = val;
1257 return 0;
1258 }
1259
Simon Hormande072bd2011-06-24 15:11:37 +09001260 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001261 LIST_INIT(&rule->list);
1262 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001263 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001264
1265 if (strcmp(args[1], "content") == 0) {
1266 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001267
1268 if (curpx->cap & PR_CAP_FE)
1269 where |= SMP_VAL_FE_RES_CNT;
1270 if (curpx->cap & PR_CAP_BE)
1271 where |= SMP_VAL_BE_RES_CNT;
1272
1273 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001274 goto error;
1275
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001276 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1277 if (acl) {
1278 if (acl->name && *acl->name)
1279 memprintf(err,
1280 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1281 acl->name, args[0], args[1], sample_ckp_names(where));
1282 else
1283 memprintf(err,
1284 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1285 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001286 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001287 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001288
Emeric Brun97679e72010-09-23 17:56:44 +02001289 warn++;
1290 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001291 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1292 if (acl->name && *acl->name)
1293 memprintf(err,
1294 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001295 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001296 else
1297 memprintf(err,
1298 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001299 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001300 warn++;
1301 }
Emeric Brun97679e72010-09-23 17:56:44 +02001302
1303 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1304 }
1305 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001306 memprintf(err,
1307 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1308 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001309 goto error;
1310 }
1311
1312 return warn;
1313 error:
1314 free(rule);
1315 return -1;
1316}
1317
1318
Willy Tarreaub6866442008-07-14 23:54:42 +02001319/* This function should be called to parse a line starting with the "tcp-request"
1320 * keyword.
1321 */
1322static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001323 struct proxy *defpx, const char *file, int line,
1324 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001325{
1326 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001327 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001328 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001329 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001330 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001331 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001332 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001333 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001334
1335 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001336 if (curpx == defpx)
1337 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1338 else
1339 memprintf(err, "missing argument for '%s' in %s '%s'",
1340 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001341 return -1;
1342 }
1343
1344 if (!strcmp(args[1], "inspect-delay")) {
1345 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001346 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1347 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001348 return -1;
1349 }
1350
Willy Tarreaub6866442008-07-14 23:54:42 +02001351 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001352 memprintf(err,
1353 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1354 args[0], args[1], proxy_type_str(curpx), curpx->id);
1355 if (ptr)
1356 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001357 return -1;
1358 }
1359
1360 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001361 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1362 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001363 return 1;
1364 }
1365 curpx->tcp_req.inspect_delay = val;
1366 return 0;
1367 }
1368
Simon Hormande072bd2011-06-24 15:11:37 +09001369 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001370 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001371 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001372 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001373
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001374 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001375 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001376
1377 if (curpx->cap & PR_CAP_FE)
1378 where |= SMP_VAL_FE_REQ_CNT;
1379 if (curpx->cap & PR_CAP_BE)
1380 where |= SMP_VAL_BE_REQ_CNT;
1381
1382 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001383 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001384
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001385 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1386 if (acl) {
1387 if (acl->name && *acl->name)
1388 memprintf(err,
1389 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1390 acl->name, args[0], args[1], sample_ckp_names(where));
1391 else
1392 memprintf(err,
1393 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1394 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001395 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001396 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001397
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001398 warn++;
1399 }
1400 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1401 if (acl->name && *acl->name)
1402 memprintf(err,
1403 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001404 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001405 else
1406 memprintf(err,
1407 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001408 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001409 warn++;
1410 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001411
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001412 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001413 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001414 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001415 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001416
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001417 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001418 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1419 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001420 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001421 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001422
Willy Tarreau80aca902013-01-07 15:42:20 +01001423 where |= SMP_VAL_FE_CON_ACC;
1424
1425 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001426 goto error;
1427
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001428 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1429 if (acl) {
1430 if (acl->name && *acl->name)
1431 memprintf(err,
1432 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1433 acl->name, args[0], args[1], sample_ckp_names(where));
1434 else
1435 memprintf(err,
1436 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1437 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001438 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001439 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001440
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001441 warn++;
1442 }
1443 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1444 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001445 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001446 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001447 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001448 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001449 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001450 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001451 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001452 warn++;
1453 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001454
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001455 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001456 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001457 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001458 if (curpx == defpx)
1459 memprintf(err,
1460 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1461 args[0], args[1]);
1462 else
1463 memprintf(err,
1464 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001465 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001466 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001467 }
1468
Willy Tarreau1a687942010-05-23 22:40:30 +02001469 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001470 error:
1471 free(rule);
1472 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001473}
1474
Willy Tarreau645513a2010-05-24 20:55:15 +02001475
1476/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001477/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001478/************************************************************************/
1479
Willy Tarreau4a129812012-04-25 17:31:42 +02001480/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001481static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001482smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001483 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001484{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001485 switch (l4->si[0].conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001486 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001487 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001488 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001489 break;
1490 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001491 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001492 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001493 break;
1494 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001495 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001496 }
Emeric Brunf769f512010-10-22 17:14:01 +02001497
Willy Tarreau37406352012-04-23 16:16:37 +02001498 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001499 return 1;
1500}
1501
Willy Tarreaua5e37562011-12-16 17:06:15 +01001502/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001503static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001504smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001505 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001506{
Willy Tarreauf853c462012-04-23 18:53:56 +02001507 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001508 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001509 return 0;
1510
Willy Tarreau37406352012-04-23 16:16:37 +02001511 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001512 return 1;
1513}
1514
Willy Tarreau4a129812012-04-25 17:31:42 +02001515/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001516static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001517smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001518 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001519{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001520 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001521
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001522 switch (l4->si[0].conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001523 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001524 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001525 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001526 break;
1527 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001528 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001529 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001530 break;
1531 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001532 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001533 }
Emeric Brunf769f512010-10-22 17:14:01 +02001534
Willy Tarreau37406352012-04-23 16:16:37 +02001535 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001536 return 1;
1537}
1538
Willy Tarreaua5e37562011-12-16 17:06:15 +01001539/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001540static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001541smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001542 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001543{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001544 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001545
Willy Tarreauf853c462012-04-23 18:53:56 +02001546 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001547 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001548 return 0;
1549
Willy Tarreau37406352012-04-23 16:16:37 +02001550 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001551 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001552}
1553
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001554#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001555/* parse the "v4v6" bind keyword */
1556static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1557{
1558 struct listener *l;
1559
1560 list_for_each_entry(l, &conf->listeners, by_bind) {
1561 if (l->addr.ss_family == AF_INET6)
1562 l->options |= LI_O_V4V6;
1563 }
1564
1565 return 0;
1566}
1567
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001568/* parse the "v6only" bind keyword */
1569static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1570{
1571 struct listener *l;
1572
1573 list_for_each_entry(l, &conf->listeners, by_bind) {
1574 if (l->addr.ss_family == AF_INET6)
1575 l->options |= LI_O_V6ONLY;
1576 }
1577
1578 return 0;
1579}
1580#endif
1581
Pieter Baauwd551fb52013-05-08 22:49:23 +02001582#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001583/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001584static 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 +02001585{
1586 struct listener *l;
1587
Willy Tarreau4348fad2012-09-20 16:48:07 +02001588 list_for_each_entry(l, &conf->listeners, by_bind) {
1589 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1590 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001591 }
1592
Willy Tarreau44791242012-09-12 23:27:21 +02001593 return 0;
1594}
1595#endif
1596
1597#ifdef TCP_DEFER_ACCEPT
1598/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001599static 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 +02001600{
1601 struct listener *l;
1602
Willy Tarreau4348fad2012-09-20 16:48:07 +02001603 list_for_each_entry(l, &conf->listeners, by_bind) {
1604 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1605 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001606 }
1607
Willy Tarreau44791242012-09-12 23:27:21 +02001608 return 0;
1609}
1610#endif
1611
Willy Tarreau1c862c52012-10-05 16:21:00 +02001612#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001613/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001614static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1615{
1616 struct listener *l;
1617
1618 list_for_each_entry(l, &conf->listeners, by_bind) {
1619 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1620 l->options |= LI_O_TCP_FO;
1621 }
1622
1623 return 0;
1624}
1625#endif
1626
Willy Tarreau44791242012-09-12 23:27:21 +02001627#ifdef TCP_MAXSEG
1628/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001629static 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 +02001630{
1631 struct listener *l;
1632 int mss;
1633
Willy Tarreau44791242012-09-12 23:27:21 +02001634 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001635 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001636 return ERR_ALERT | ERR_FATAL;
1637 }
1638
1639 mss = atoi(args[cur_arg + 1]);
1640 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001641 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001642 return ERR_ALERT | ERR_FATAL;
1643 }
1644
Willy Tarreau4348fad2012-09-20 16:48:07 +02001645 list_for_each_entry(l, &conf->listeners, by_bind) {
1646 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1647 l->maxseg = mss;
1648 }
Willy Tarreau44791242012-09-12 23:27:21 +02001649
1650 return 0;
1651}
1652#endif
1653
1654#ifdef SO_BINDTODEVICE
1655/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001656static 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 +02001657{
1658 struct listener *l;
1659
Willy Tarreau44791242012-09-12 23:27:21 +02001660 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001661 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001662 return ERR_ALERT | ERR_FATAL;
1663 }
1664
Willy Tarreau4348fad2012-09-20 16:48:07 +02001665 list_for_each_entry(l, &conf->listeners, by_bind) {
1666 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1667 l->interface = strdup(args[cur_arg + 1]);
1668 }
Willy Tarreau44791242012-09-12 23:27:21 +02001669
1670 global.last_checks |= LSTCHK_NETADM;
1671 return 0;
1672}
1673#endif
1674
Willy Tarreaub6866442008-07-14 23:54:42 +02001675static struct cfg_kw_list cfg_kws = {{ },{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001676 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001677 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001678 { 0, NULL, NULL },
1679}};
1680
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001681
Willy Tarreau61612d42012-04-19 18:42:05 +02001682/* Note: must not be declared <const> as its list will be overwritten.
1683 * Please take care of keeping this list alphabetically sorted.
1684 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001685static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaud86e29d2013-03-25 08:21:05 +01001686 { "dst", NULL, acl_parse_ip, acl_match_ip },
1687 { "dst_port", NULL, acl_parse_int, acl_match_int },
1688 { "src", NULL, acl_parse_ip, acl_match_ip },
1689 { "src_port", NULL, acl_parse_int, acl_match_int },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001690 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02001691}};
1692
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001693
Willy Tarreau4a129812012-04-25 17:31:42 +02001694/* Note: must not be declared <const> as its list will be overwritten.
1695 * Note: fetches that may return multiple types must be declared as the lowest
1696 * common denominator, the type that can be casted into all other ones. For
1697 * instance v4/v6 must be declared v4.
1698 */
Willy Tarreau12785782012-04-27 21:37:17 +02001699static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001700 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1701 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1702 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1703 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1704 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001705}};
1706
Willy Tarreau44791242012-09-12 23:27:21 +02001707/************************************************************************/
1708/* All supported bind keywords must be declared here. */
1709/************************************************************************/
1710
1711/* Note: must not be declared <const> as its list will be overwritten.
1712 * Please take care of keeping this list alphabetically sorted, doing so helps
1713 * all code contributors.
1714 * Optional keywords are also declared with a NULL ->parse() function so that
1715 * the config parser can report an appropriate error when a known keyword was
1716 * not enabled.
1717 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001718static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001719#ifdef TCP_DEFER_ACCEPT
1720 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1721#endif
1722#ifdef SO_BINDTODEVICE
1723 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1724#endif
1725#ifdef TCP_MAXSEG
1726 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1727#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001728#ifdef TCP_FASTOPEN
1729 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1730#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001731#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001732 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1733#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001734#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001735 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001736 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1737#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001738 /* the versions with the NULL parse function*/
1739 { "defer-accept", NULL, 0 },
1740 { "interface", NULL, 1 },
1741 { "mss", NULL, 1 },
1742 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001743 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001744 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001745 { NULL, NULL, 0 },
1746}};
1747
Willy Tarreaue6b98942007-10-29 01:09:36 +01001748__attribute__((constructor))
1749static void __tcp_protocol_init(void)
1750{
1751 protocol_register(&proto_tcpv4);
1752 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001753 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001754 cfg_register_keywords(&cfg_kws);
1755 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001756 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001757}
1758
1759
1760/*
1761 * Local variables:
1762 * c-indent-level: 8
1763 * c-basic-offset: 8
1764 * End:
1765 */