blob: 0ae359a767dbb69baad8c6760a58fa193819d0c9 [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 Baauwd551fb52013-05-08 22:49:23 +0200145 )
David du Colombier65c17962012-07-13 14:34:59 +0200146 foreign_ok = 1;
147 else
148 ip_transp_working = 0;
149 }
150 break;
151 case AF_INET6:
152 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200153 if (0
154#if defined(IPV6_TRANSPARENT)
155 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
156#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200157#if defined(IPV6_BINDANY)
158 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
159#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200160 )
David du Colombier65c17962012-07-13 14:34:59 +0200161 foreign_ok = 1;
162 else
163 ip6_transp_working = 0;
164 }
165 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100166 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200167
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100168 if (flags) {
169 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200170 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100171 switch (remote->ss_family) {
172 case AF_INET:
173 if (flags & 1)
174 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
175 if (flags & 2)
176 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
177 break;
178 case AF_INET6:
179 if (flags & 1)
180 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
181 if (flags & 2)
182 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
183 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100184 default:
185 /* we don't want to try to bind to an unknown address family */
186 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100187 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100188 }
189
Simon Hormande072bd2011-06-24 15:11:37 +0900190 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100191 if (foreign_ok) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200192 if (is_addr(&bind_addr)) {
193 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
194 if (ret < 0)
195 return 2;
196 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100197 }
198 else {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200199 if (is_addr(local)) {
200 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
201 if (ret < 0)
202 return 1;
203 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100204 }
205
206 if (!flags)
207 return 0;
208
209#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100210 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100211 struct in_tproxy itp1, itp2;
212 memset(&itp1, 0, sizeof(itp1));
213
214 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100215 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
216 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100217
218 /* set connect flag on socket */
219 itp2.op = TPROXY_FLAGS;
220 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
221
222 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
223 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
224 foreign_ok = 1;
225 }
226 }
227#endif
228 if (!foreign_ok)
229 /* we could not bind to a foreign address */
230 return 2;
231
232 return 0;
233}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100234
Willy Tarreau9650f372009-08-16 14:02:45 +0200235
236/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200237 * This function initiates a TCP connection establishment to the target assigned
238 * to connection <conn> using (si->{target,addr.to}). A source address may be
239 * pointed to by conn->addr.from in case of transparent proxying. Normal source
240 * bind addresses are still determined locally (due to the possible need of a
241 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100242 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100243 * supported. The <data> parameter is a boolean indicating whether there are data
244 * waiting for being sent or not, in order to adjust data write polling and on
245 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
246 * allows the caller to force using a delayed ACK when establishing the connection :
247 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
248 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
249 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200250 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200251 * It can return one of :
252 * - SN_ERR_NONE if everything's OK
253 * - SN_ERR_SRVTO if there are no more servers
254 * - SN_ERR_SRVCL if the connection was refused by the server
255 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
256 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
257 * - SN_ERR_INTERNAL for any other purely internal errors
258 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100259 *
260 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
261 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200262 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100263
Willy Tarreauf0837b22012-11-24 10:24:27 +0100264int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200265{
266 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100267 struct server *srv;
268 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100269 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100270
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100271 switch (obj_type(conn->target)) {
272 case OBJ_TYPE_PROXY:
273 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100274 srv = NULL;
275 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100276 case OBJ_TYPE_SERVER:
277 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100278 be = srv->proxy;
279 break;
280 default:
281 return SN_ERR_INTERNAL;
282 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200283
Willy Tarreau986a9d22012-08-30 21:11:38 +0200284 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200285 qfprintf(stderr, "Cannot get a server socket.\n");
286
287 if (errno == ENFILE)
288 send_log(be, LOG_EMERG,
289 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
290 be->id, maxfd);
291 else if (errno == EMFILE)
292 send_log(be, LOG_EMERG,
293 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
294 be->id, maxfd);
295 else if (errno == ENOBUFS || errno == ENOMEM)
296 send_log(be, LOG_EMERG,
297 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
298 be->id, maxfd);
299 /* this is a resource error */
300 return SN_ERR_RESOURCE;
301 }
302
303 if (fd >= global.maxsock) {
304 /* do not log anything there, it's a normal condition when this option
305 * is used to serialize connections to a server !
306 */
307 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
308 close(fd);
309 return SN_ERR_PRXCOND; /* it is a configuration limit */
310 }
311
312 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900313 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200314 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
315 close(fd);
316 return SN_ERR_INTERNAL;
317 }
318
319 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900320 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200321
Willy Tarreau9650f372009-08-16 14:02:45 +0200322 /* allow specific binding :
323 * - server-specific at first
324 * - proxy-specific next
325 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100326 if (srv && srv->conn_src.opts & CO_SRC_BIND)
327 src = &srv->conn_src;
328 else if (be->conn_src.opts & CO_SRC_BIND)
329 src = &be->conn_src;
330 else
331 src = NULL;
332
333 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200334 int ret, flags = 0;
335
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200336 if (is_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100337 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100338 case CO_SRC_TPROXY_ADDR:
339 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200340 flags = 3;
341 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100342 case CO_SRC_TPROXY_CIP:
343 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200344 flags = 1;
345 break;
346 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200347 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200348
Willy Tarreau9650f372009-08-16 14:02:45 +0200349#ifdef SO_BINDTODEVICE
350 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100351 if (src->iface_name)
352 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200353#endif
354
Willy Tarreaua4380b42012-12-08 22:49:11 +0100355 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100357 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200358
359 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100360 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200361
362 do {
363 /* note: in case of retry, we may have to release a previously
364 * allocated port, hence this loop's construct.
365 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200366 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
367 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200368
369 if (!attempts)
370 break;
371 attempts--;
372
Willy Tarreaua4380b42012-12-08 22:49:11 +0100373 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200374 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 break;
376
Willy Tarreaua4380b42012-12-08 22:49:11 +0100377 fdinfo[fd].port_range = src->sport_range;
378 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200379
Willy Tarreaua4380b42012-12-08 22:49:11 +0100380 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200381 } while (ret != 0); /* binding NOK */
382 }
383 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100384 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200385 }
386
Willy Tarreaua4380b42012-12-08 22:49:11 +0100387 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200388 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
389 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200390 close(fd);
391
Willy Tarreau9650f372009-08-16 14:02:45 +0200392 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100393 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200394 be->id);
395 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100396 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200397 be->id);
398 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100399 Alert("Cannot bind to tproxy 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 tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200403 be->id);
404 }
405 return SN_ERR_RESOURCE;
406 }
407 }
408
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400409#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200410 /* disabling tcp quick ack now allows the first request to leave the
411 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100412 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200413 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100414 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900415 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200416#endif
417
Willy Tarreaue803de22010-01-21 17:43:04 +0100418 if (global.tune.server_sndbuf)
419 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
420
421 if (global.tune.server_rcvbuf)
422 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
423
Willy Tarreau986a9d22012-08-30 21:11:38 +0200424 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200425 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
426
Willy Tarreaub1719512012-12-08 23:03:28 +0100427 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200428 char *msg;
Willy Tarreaub1719512012-12-08 23:03:28 +0100429 if (errno == EAGAIN || errno == EADDRNOTAVAIL)
Willy Tarreau9650f372009-08-16 14:02:45 +0200430 msg = "no free ports";
431 else
432 msg = "local address already in use";
433
Willy Tarreaub1719512012-12-08 23:03:28 +0100434 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200435 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
436 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200437 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100438 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9650f372009-08-16 14:02:45 +0200439 return SN_ERR_RESOURCE;
440 } else if (errno == ETIMEDOUT) {
441 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200442 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
443 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200444 close(fd);
445 return SN_ERR_SRVTO;
446 } else {
447 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
448 //qfprintf(stderr,"Connect(): %d", errno);
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);
452 return SN_ERR_SRVCL;
453 }
454 }
455
Willy Tarreau986a9d22012-08-30 21:11:38 +0200456 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200457 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100458 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200459
Willy Tarreaud2274c62012-07-06 14:29:45 +0200460 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200461 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200462 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200463
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200464 if (conn_xprt_init(conn) < 0) {
Willy Tarreau184636e2012-09-06 14:04:41 +0200465 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200466 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200467 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200468
Willy Tarreau14f8e862012-08-30 22:23:13 +0200469 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200470 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200471
Willy Tarreau9650f372009-08-16 14:02:45 +0200472 return SN_ERR_NONE; /* connection is OK */
473}
474
475
Willy Tarreau59b94792012-05-11 16:16:40 +0200476/*
477 * Retrieves the source address for the socket <fd>, with <dir> indicating
478 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
479 * success, -1 in case of error. The socket's source address is stored in
480 * <sa> for <salen> bytes.
481 */
482int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
483{
484 if (dir)
485 return getsockname(fd, sa, &salen);
486 else
487 return getpeername(fd, sa, &salen);
488}
489
490
491/*
492 * Retrieves the original destination address for the socket <fd>, with <dir>
493 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
494 * listener, if the original destination address was translated, the original
495 * address is retrieved. It returns 0 in case of success, -1 in case of error.
496 * The socket's source address is stored in <sa> for <salen> bytes.
497 */
498int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
499{
500 if (dir)
501 return getpeername(fd, sa, &salen);
502#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
503 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
504 return 0;
505#endif
506 else
507 return getsockname(fd, sa, &salen);
508}
509
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200510/* This is the callback which is set when a connection establishment is pending
511 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200512 * once the connection is established. It updates the FD polling status. It
513 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
514 * it returns non-zero and removes itself from the connection's flags (the bit is
515 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200516 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200517int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200518{
Willy Tarreau239d7182012-07-23 18:53:03 +0200519 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200520
Willy Tarreau80184712012-07-06 14:54:49 +0200521 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200522 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200523
Willy Tarreau80184712012-07-06 14:54:49 +0200524 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200525 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200526
Willy Tarreau2da156f2012-07-23 15:07:23 +0200527 /* stop here if we reached the end of data */
528 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
529 goto out_error;
530
Willy Tarreau2c6be842012-07-06 17:12:34 +0200531 /* We have no data to send to check the connection, and
532 * getsockopt() will not inform us whether the connection
533 * is still pending. So we'll reuse connect() to check the
534 * state of the socket. This has the advantage of giving us
535 * the following info :
536 * - error
537 * - connecting (EALREADY, EINPROGRESS)
538 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200539 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200540 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200541 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100542 __conn_sock_stop_recv(conn);
543 __conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200544 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200545 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200546
Willy Tarreau2c6be842012-07-06 17:12:34 +0200547 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200548 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200549
Willy Tarreau2c6be842012-07-06 17:12:34 +0200550 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200551 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200552
Willy Tarreau076be252012-07-06 16:02:29 +0200553 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200554 * forward the event to the transport layer which will notify the
555 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200556 */
Willy Tarreau80184712012-07-06 14:54:49 +0200557 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200558 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200559
560 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200561 /* Write error on the file descriptor. Report it to the connection
562 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200563 */
564
Willy Tarreau80184712012-07-06 14:54:49 +0200565 conn->flags |= CO_FL_ERROR;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100566 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200567 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200568}
569
Willy Tarreau59b94792012-05-11 16:16:40 +0200570
Willy Tarreaue6b98942007-10-29 01:09:36 +0100571/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100572 * an error message in <errmsg> if the message is at most <errlen> bytes long
573 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
574 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100575 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
576 * was alright and that no message was returned. ERR_RETRYABLE means that an
577 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700578 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100579 * the meaning of the error, but just indicate that a message is present which
580 * should be displayed with the respective level. Last, ERR_ABORT indicates
581 * that it's pointless to try to start other listeners. No error message is
582 * returned if errlen is NULL.
583 */
584int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
585{
586 __label__ tcp_return, tcp_close_return;
587 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100588 int ext, ready;
589 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100590 const char *msg = NULL;
591
592 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100593 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100594 *errmsg = 0;
595
596 if (listener->state != LI_ASSIGNED)
597 return ERR_NONE; /* already bound */
598
599 err = ERR_NONE;
600
Willy Tarreau40aa0702013-03-10 23:51:38 +0100601 /* if the listener already has an fd assigned, then we were offered the
602 * fd by an external process (most likely the parent), and we don't want
603 * to create a new socket. However we still want to set a few flags on
604 * the socket.
605 */
606 fd = listener->fd;
607 ext = (fd >= 0);
608
609 if (!ext && (fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100610 err |= ERR_RETRYABLE | ERR_ALERT;
611 msg = "cannot create listening socket";
612 goto tcp_return;
613 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100614
Willy Tarreaue6b98942007-10-29 01:09:36 +0100615 if (fd >= global.maxsock) {
616 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
617 msg = "not enough free sockets (raise '-n' parameter)";
618 goto tcp_close_return;
619 }
620
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200621 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100622 err |= ERR_FATAL | ERR_ALERT;
623 msg = "cannot make socket non-blocking";
624 goto tcp_close_return;
625 }
626
Willy Tarreau40aa0702013-03-10 23:51:38 +0100627 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100628 /* not fatal but should be reported */
629 msg = "cannot do so_reuseaddr";
630 err |= ERR_ALERT;
631 }
632
633 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900634 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100635
Willy Tarreaue6b98942007-10-29 01:09:36 +0100636#ifdef SO_REUSEPORT
637 /* OpenBSD supports this. As it's present in old libc versions of Linux,
638 * it might return an error that we will silently ignore.
639 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100640 if (!ext)
641 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100642#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200643
Willy Tarreau40aa0702013-03-10 23:51:38 +0100644 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200645 switch (listener->addr.ss_family) {
646 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200647 if (1
648#if defined(IP_TRANSPARENT)
649 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
650#endif
651#if defined(IP_FREEBIND)
652 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
653#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200654#if defined(IP_BINDANY)
655 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
656#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200657 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200658 msg = "cannot make listening socket transparent";
659 err |= ERR_ALERT;
660 }
661 break;
662 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200663 if (1
664#if defined(IPV6_TRANSPARENT)
665 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
666#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200667#if defined(IPV6_BINDANY)
668 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
669#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200670 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200671 msg = "cannot make listening socket transparent";
672 err |= ERR_ALERT;
673 }
674 break;
675 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100676 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200677
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100678#ifdef SO_BINDTODEVICE
679 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100680 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100681 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100682 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100683 msg = "cannot bind listener to device";
684 err |= ERR_WARN;
685 }
686 }
687#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400688#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100689 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400690 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200691 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
692 msg = "cannot set MSS";
693 err |= ERR_WARN;
694 }
695 }
696#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200697#if defined(TCP_DEFER_ACCEPT)
698 if (listener->options & LI_O_DEF_ACCEPT) {
699 /* defer accept by up to one second */
700 int accept_delay = 1;
701 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
702 msg = "cannot enable DEFER_ACCEPT";
703 err |= ERR_WARN;
704 }
705 }
706#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200707#if defined(TCP_FASTOPEN)
708 if (listener->options & LI_O_TCP_FO) {
709 /* TFO needs a queue length, let's use the configured backlog */
710 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
711 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
712 msg = "cannot enable TCP_FASTOPEN";
713 err |= ERR_WARN;
714 }
715 }
716#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100717#if defined(IPV6_V6ONLY)
718 if (listener->options & LI_O_V6ONLY)
719 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100720 else if (listener->options & LI_O_V4V6)
721 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100722#endif
723
Willy Tarreau40aa0702013-03-10 23:51:38 +0100724 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100725 err |= ERR_RETRYABLE | ERR_ALERT;
726 msg = "cannot bind socket";
727 goto tcp_close_return;
728 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100729
Willy Tarreau40aa0702013-03-10 23:51:38 +0100730 ready = 0;
731 ready_len = sizeof(ready);
732 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
733 ready = 0;
734
735 if (!(ext && ready) && /* only listen if not already done by external process */
736 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100737 err |= ERR_RETRYABLE | ERR_ALERT;
738 msg = "cannot listen to socket";
739 goto tcp_close_return;
740 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100741
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400742#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200743 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900744 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200745#endif
746
Willy Tarreaue6b98942007-10-29 01:09:36 +0100747 /* the socket is ready */
748 listener->fd = fd;
749 listener->state = LI_LISTEN;
750
Willy Tarreaueabf3132008-08-29 23:36:51 +0200751 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200752 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200753 fd_insert(fd);
754
Willy Tarreaue6b98942007-10-29 01:09:36 +0100755 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100756 if (msg && errlen) {
757 char pn[INET6_ADDRSTRLEN];
758
Willy Tarreau631f01c2011-09-05 00:36:48 +0200759 addr_to_str(&listener->addr, pn, sizeof(pn));
760 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100761 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100762 return err;
763
764 tcp_close_return:
765 close(fd);
766 goto tcp_return;
767}
768
769/* This function creates all TCP sockets bound to the protocol entry <proto>.
770 * It is intended to be used as the protocol's bind_all() function.
771 * The sockets will be registered but not added to any fd_set, in order not to
772 * loose them across the fork(). A call to enable_all_listeners() is needed
773 * to complete initialization. The return value is composed from ERR_*.
774 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200775static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100776{
777 struct listener *listener;
778 int err = ERR_NONE;
779
780 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200781 err |= tcp_bind_listener(listener, errmsg, errlen);
782 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100783 break;
784 }
785
786 return err;
787}
788
789/* Add listener to the list of tcpv4 listeners. The listener's state
790 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
791 * listeners is updated. This is the function to use to add a new listener.
792 */
793void tcpv4_add_listener(struct listener *listener)
794{
795 if (listener->state != LI_INIT)
796 return;
797 listener->state = LI_ASSIGNED;
798 listener->proto = &proto_tcpv4;
799 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
800 proto_tcpv4.nb_listeners++;
801}
802
803/* Add listener to the list of tcpv4 listeners. The listener's state
804 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
805 * listeners is updated. This is the function to use to add a new listener.
806 */
807void tcpv6_add_listener(struct listener *listener)
808{
809 if (listener->state != LI_INIT)
810 return;
811 listener->state = LI_ASSIGNED;
812 listener->proto = &proto_tcpv6;
813 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
814 proto_tcpv6.nb_listeners++;
815}
816
Willy Tarreauedcf6682008-11-30 23:15:34 +0100817/* This function performs the TCP request analysis on the current request. It
818 * returns 1 if the processing can continue on next analysers, or zero if it
819 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200820 * request. It relies on buffers flags, and updates s->req->analysers. The
821 * function may be called for frontend rules and backend rules. It only relies
822 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100823 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200824int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100825{
826 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200827 struct stksess *ts;
828 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100829 int partial;
830
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100831 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 +0100832 now_ms, __FUNCTION__,
833 s,
834 req,
835 req->rex, req->wex,
836 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200837 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100838 req->analysers);
839
Willy Tarreauedcf6682008-11-30 23:15:34 +0100840 /* We don't know whether we have enough data, so must proceed
841 * this way :
842 * - iterate through all rules in their declaration order
843 * - if one rule returns MISS, it means the inspect delay is
844 * not over yet, then return immediately, otherwise consider
845 * it as a non-match.
846 * - if one rule returns OK, then return OK
847 * - if one rule returns KO, then return KO
848 */
849
Willy Tarreau9b28e032012-10-12 23:49:43 +0200850 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200851 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200852 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100853 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200854 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100855
Willy Tarreaufb356202010-08-03 14:02:05 +0200856 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100857 int ret = ACL_PAT_PASS;
858
859 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200860 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100861 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200862 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100863 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200864 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
865 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100866 return 0;
867 }
868
869 ret = acl_pass(ret);
870 if (rule->cond->pol == ACL_COND_UNLESS)
871 ret = !ret;
872 }
873
874 if (ret) {
875 /* we have a matching rule. */
876 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200877 channel_abort(req);
878 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100879 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200880
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100881 s->be->be_counters.denied_req++;
882 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200883 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200884 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200885
Willy Tarreauedcf6682008-11-30 23:15:34 +0100886 if (!(s->flags & SN_ERR_MASK))
887 s->flags |= SN_ERR_PRXCOND;
888 if (!(s->flags & SN_FINST_MASK))
889 s->flags |= SN_FINST_R;
890 return 0;
891 }
Willy Tarreau20d46a52012-12-09 15:55:40 +0100892 else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) ||
893 (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100894 /* Note: only the first valid tracking parameter of each
895 * applies.
896 */
897 struct stktable_key *key;
898
899 t = rule->act_prm.trk_ctr.table.t;
900 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
901
902 if (key && (ts = stktable_get_entry(t, key))) {
903 if (rule->action == TCP_ACT_TRK_SC1) {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100904 session_track_stkctr(&s->stkctr[0], t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200905 if (s->fe != s->be)
906 s->flags |= SN_BE_TRACK_SC1;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100907 } else {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100908 session_track_stkctr(&s->stkctr[1], t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200909 if (s->fe != s->be)
910 s->flags |= SN_BE_TRACK_SC2;
911 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200912 }
913 }
914 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100915 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200916 break;
917 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100918 }
919 }
920
921 /* if we get there, it means we have no rule which matches, or
922 * we have an explicit accept, so we apply the default accept.
923 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200924 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100925 req->analyse_exp = TICK_ETERNITY;
926 return 1;
927}
928
Emeric Brun97679e72010-09-23 17:56:44 +0200929/* This function performs the TCP response analysis on the current response. It
930 * returns 1 if the processing can continue on next analysers, or zero if it
931 * needs more data, encounters an error, or wants to immediately abort the
932 * response. It relies on buffers flags, and updates s->rep->analysers. The
933 * function may be called for backend rules.
934 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200935int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200936{
937 struct tcp_rule *rule;
938 int partial;
939
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100940 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 +0200941 now_ms, __FUNCTION__,
942 s,
943 rep,
944 rep->rex, rep->wex,
945 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200946 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200947 rep->analysers);
948
949 /* We don't know whether we have enough data, so must proceed
950 * this way :
951 * - iterate through all rules in their declaration order
952 * - if one rule returns MISS, it means the inspect delay is
953 * not over yet, then return immediately, otherwise consider
954 * it as a non-match.
955 * - if one rule returns OK, then return OK
956 * - if one rule returns KO, then return KO
957 */
958
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200959 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200960 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200961 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200962 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200963
964 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
965 int ret = ACL_PAT_PASS;
966
967 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200968 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200969 if (ret == ACL_PAT_MISS) {
970 /* just set the analyser timeout once at the beginning of the response */
971 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
972 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
973 return 0;
974 }
975
976 ret = acl_pass(ret);
977 if (rule->cond->pol == ACL_COND_UNLESS)
978 ret = !ret;
979 }
980
981 if (ret) {
982 /* we have a matching rule. */
983 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200984 channel_abort(rep);
985 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200986 rep->analysers = 0;
987
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100988 s->be->be_counters.denied_resp++;
989 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200990 if (s->listener->counters)
991 s->listener->counters->denied_resp++;
992
993 if (!(s->flags & SN_ERR_MASK))
994 s->flags |= SN_ERR_PRXCOND;
995 if (!(s->flags & SN_FINST_MASK))
996 s->flags |= SN_FINST_D;
997 return 0;
998 }
999 else {
1000 /* otherwise accept */
1001 break;
1002 }
1003 }
1004 }
1005
1006 /* if we get there, it means we have no rule which matches, or
1007 * we have an explicit accept, so we apply the default accept.
1008 */
1009 rep->analysers &= ~an_bit;
1010 rep->analyse_exp = TICK_ETERNITY;
1011 return 1;
1012}
1013
1014
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001015/* This function performs the TCP layer4 analysis on the current request. It
1016 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1017 * matches or if no more rule matches. It can only use rules which don't need
1018 * any data.
1019 */
1020int tcp_exec_req_rules(struct session *s)
1021{
1022 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001023 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001024 struct stktable *t = NULL;
1025 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001026 int ret;
1027
1028 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1029 ret = ACL_PAT_PASS;
1030
1031 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001032 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001033 ret = acl_pass(ret);
1034 if (rule->cond->pol == ACL_COND_UNLESS)
1035 ret = !ret;
1036 }
1037
1038 if (ret) {
1039 /* we have a matching rule. */
1040 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001041 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001042 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001043 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001044
1045 if (!(s->flags & SN_ERR_MASK))
1046 s->flags |= SN_ERR_PRXCOND;
1047 if (!(s->flags & SN_FINST_MASK))
1048 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001049 result = 0;
1050 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001051 }
Willy Tarreau20d46a52012-12-09 15:55:40 +01001052 else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) ||
1053 (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001054 /* Note: only the first valid tracking parameter of each
1055 * applies.
1056 */
1057 struct stktable_key *key;
1058
1059 t = rule->act_prm.trk_ctr.table.t;
1060 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
1061
1062 if (key && (ts = stktable_get_entry(t, key))) {
1063 if (rule->action == TCP_ACT_TRK_SC1)
Willy Tarreau20d46a52012-12-09 15:55:40 +01001064 session_track_stkctr(&s->stkctr[0], t, ts);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001065 else
Willy Tarreau20d46a52012-12-09 15:55:40 +01001066 session_track_stkctr(&s->stkctr[1], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001067 }
1068 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001069 else {
1070 /* otherwise it's an accept */
1071 break;
1072 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001073 }
1074 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001075 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001076}
1077
Emeric Brun97679e72010-09-23 17:56:44 +02001078/* Parse a tcp-response rule. Return a negative value in case of failure */
1079static int tcp_parse_response_rule(char **args, int arg, int section_type,
1080 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau80aca902013-01-07 15:42:20 +01001081 struct tcp_rule *rule, char **err,
1082 unsigned int where)
Emeric Brun97679e72010-09-23 17:56:44 +02001083{
1084 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001085 memprintf(err, "%s %s is only allowed in 'backend' sections",
1086 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001087 return -1;
1088 }
1089
1090 if (strcmp(args[arg], "accept") == 0) {
1091 arg++;
1092 rule->action = TCP_ACT_ACCEPT;
1093 }
1094 else if (strcmp(args[arg], "reject") == 0) {
1095 arg++;
1096 rule->action = TCP_ACT_REJECT;
1097 }
1098 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001099 memprintf(err,
1100 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1101 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001102 return -1;
1103 }
1104
1105 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001106 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1107 memprintf(err,
1108 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1109 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001110 return -1;
1111 }
1112 }
1113 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001114 memprintf(err,
1115 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001116 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1117 return -1;
1118 }
1119 return 0;
1120}
1121
1122
1123
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001124/* Parse a tcp-request rule. Return a negative value in case of failure */
1125static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001126 struct proxy *curpx, struct proxy *defpx,
1127 struct tcp_rule *rule, char **err,
1128 unsigned int where)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001129{
1130 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001131 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1132 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001133 return -1;
1134 }
1135
1136 if (!strcmp(args[arg], "accept")) {
1137 arg++;
1138 rule->action = TCP_ACT_ACCEPT;
1139 }
1140 else if (!strcmp(args[arg], "reject")) {
1141 arg++;
1142 rule->action = TCP_ACT_REJECT;
1143 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001144 else if (strcmp(args[arg], "track-sc1") == 0 || strcmp(args[arg], "track-sc2") == 0) {
1145 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001146 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001147
1148 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001149
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001150 curpx->conf.args.ctx = ARGC_TRK;
1151 expr = sample_parse_expr(args, &arg, trash.str, trash.size, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001152 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001153 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001154 "'%s %s %s' : %s",
1155 args[0], args[1], args[kw], trash.str);
1156 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001157 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001158
Willy Tarreau80aca902013-01-07 15:42:20 +01001159 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001160 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001161 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001162 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001163 free(expr);
1164 return -1;
1165 }
1166
1167 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001168 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001169
1170 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001171 arg++;
1172 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001173 memprintf(err,
1174 "'%s %s %s' : missing table name",
1175 args[0], args[1], args[kw]);
1176 free(expr);
1177 return -1;
1178 }
1179 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001180 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001181 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001182 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001183 rule->act_prm.trk_ctr.expr = expr;
1184
1185 if (args[kw][8] == '1')
1186 rule->action = TCP_ACT_TRK_SC1;
1187 else
1188 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001189 }
1190 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001191 memprintf(err,
1192 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1193 "or 'track-sc2' in %s '%s' (got '%s')",
1194 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001195 return -1;
1196 }
1197
1198 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001199 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1200 memprintf(err,
1201 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1202 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001203 return -1;
1204 }
1205 }
1206 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001207 memprintf(err,
1208 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001209 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1210 return -1;
1211 }
1212 return 0;
1213}
1214
Emeric Brun97679e72010-09-23 17:56:44 +02001215/* This function should be called to parse a line starting with the "tcp-response"
1216 * keyword.
1217 */
1218static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001219 struct proxy *defpx, const char *file, int line,
1220 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001221{
1222 const char *ptr = NULL;
1223 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001224 int warn = 0;
1225 int arg;
1226 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001227 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001228 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001229 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001230
1231 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001232 memprintf(err, "missing argument for '%s' in %s '%s'",
1233 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001234 return -1;
1235 }
1236
1237 if (strcmp(args[1], "inspect-delay") == 0) {
1238 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001239 memprintf(err, "%s %s is only allowed in 'backend' sections",
1240 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001241 return -1;
1242 }
1243
1244 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001245 memprintf(err,
1246 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1247 args[0], args[1], proxy_type_str(curpx), curpx->id);
1248 if (ptr)
1249 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001250 return -1;
1251 }
1252
1253 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001254 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1255 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001256 return 1;
1257 }
1258 curpx->tcp_rep.inspect_delay = val;
1259 return 0;
1260 }
1261
Simon Hormande072bd2011-06-24 15:11:37 +09001262 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001263 LIST_INIT(&rule->list);
1264 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001265 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001266
1267 if (strcmp(args[1], "content") == 0) {
1268 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001269
1270 if (curpx->cap & PR_CAP_FE)
1271 where |= SMP_VAL_FE_RES_CNT;
1272 if (curpx->cap & PR_CAP_BE)
1273 where |= SMP_VAL_BE_RES_CNT;
1274
1275 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001276 goto error;
1277
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001278 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1279 if (acl) {
1280 if (acl->name && *acl->name)
1281 memprintf(err,
1282 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1283 acl->name, args[0], args[1], sample_ckp_names(where));
1284 else
1285 memprintf(err,
1286 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1287 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001288 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001289 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001290
Emeric Brun97679e72010-09-23 17:56:44 +02001291 warn++;
1292 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001293 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1294 if (acl->name && *acl->name)
1295 memprintf(err,
1296 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001297 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001298 else
1299 memprintf(err,
1300 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001301 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001302 warn++;
1303 }
Emeric Brun97679e72010-09-23 17:56:44 +02001304
1305 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1306 }
1307 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001308 memprintf(err,
1309 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1310 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001311 goto error;
1312 }
1313
1314 return warn;
1315 error:
1316 free(rule);
1317 return -1;
1318}
1319
1320
Willy Tarreaub6866442008-07-14 23:54:42 +02001321/* This function should be called to parse a line starting with the "tcp-request"
1322 * keyword.
1323 */
1324static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001325 struct proxy *defpx, const char *file, int line,
1326 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001327{
1328 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001329 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001330 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001331 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001332 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001333 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001334 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001335 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001336
1337 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001338 if (curpx == defpx)
1339 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1340 else
1341 memprintf(err, "missing argument for '%s' in %s '%s'",
1342 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001343 return -1;
1344 }
1345
1346 if (!strcmp(args[1], "inspect-delay")) {
1347 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001348 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1349 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001350 return -1;
1351 }
1352
Willy Tarreaub6866442008-07-14 23:54:42 +02001353 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001354 memprintf(err,
1355 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1356 args[0], args[1], proxy_type_str(curpx), curpx->id);
1357 if (ptr)
1358 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001359 return -1;
1360 }
1361
1362 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001363 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1364 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001365 return 1;
1366 }
1367 curpx->tcp_req.inspect_delay = val;
1368 return 0;
1369 }
1370
Simon Hormande072bd2011-06-24 15:11:37 +09001371 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001372 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001373 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001374 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001375
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001376 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001377 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001378
1379 if (curpx->cap & PR_CAP_FE)
1380 where |= SMP_VAL_FE_REQ_CNT;
1381 if (curpx->cap & PR_CAP_BE)
1382 where |= SMP_VAL_BE_REQ_CNT;
1383
1384 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001385 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001386
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001387 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1388 if (acl) {
1389 if (acl->name && *acl->name)
1390 memprintf(err,
1391 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1392 acl->name, args[0], args[1], sample_ckp_names(where));
1393 else
1394 memprintf(err,
1395 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1396 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001397 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001398 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001399
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001400 warn++;
1401 }
1402 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1403 if (acl->name && *acl->name)
1404 memprintf(err,
1405 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001406 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001407 else
1408 memprintf(err,
1409 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001410 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001411 warn++;
1412 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001413
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001414 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001415 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001416 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001417 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001418
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001419 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001420 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1421 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001422 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001423 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001424
Willy Tarreau80aca902013-01-07 15:42:20 +01001425 where |= SMP_VAL_FE_CON_ACC;
1426
1427 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001428 goto error;
1429
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001430 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1431 if (acl) {
1432 if (acl->name && *acl->name)
1433 memprintf(err,
1434 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1435 acl->name, args[0], args[1], sample_ckp_names(where));
1436 else
1437 memprintf(err,
1438 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1439 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001440 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001441 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001442
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001443 warn++;
1444 }
1445 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1446 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001447 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001448 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001449 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001450 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001451 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001452 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001453 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001454 warn++;
1455 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001456
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001457 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001458 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001459 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001460 if (curpx == defpx)
1461 memprintf(err,
1462 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1463 args[0], args[1]);
1464 else
1465 memprintf(err,
1466 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001467 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001468 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001469 }
1470
Willy Tarreau1a687942010-05-23 22:40:30 +02001471 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001472 error:
1473 free(rule);
1474 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001475}
1476
Willy Tarreau645513a2010-05-24 20:55:15 +02001477
1478/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001479/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001480/************************************************************************/
1481
Willy Tarreau4a129812012-04-25 17:31:42 +02001482/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001483static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001484smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001485 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001486{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001487 switch (l4->si[0].conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001488 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001489 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001490 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001491 break;
1492 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001493 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001494 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001495 break;
1496 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001497 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001498 }
Emeric Brunf769f512010-10-22 17:14:01 +02001499
Willy Tarreau37406352012-04-23 16:16:37 +02001500 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001501 return 1;
1502}
1503
Willy Tarreaua5e37562011-12-16 17:06:15 +01001504/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001505static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001506smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001507 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001508{
Willy Tarreauf853c462012-04-23 18:53:56 +02001509 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001510 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001511 return 0;
1512
Willy Tarreau37406352012-04-23 16:16:37 +02001513 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001514 return 1;
1515}
1516
Willy Tarreau4a129812012-04-25 17:31:42 +02001517/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001518static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001519smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001520 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001521{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001522 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001523
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001524 switch (l4->si[0].conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001525 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001526 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001527 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001528 break;
1529 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001530 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001531 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001532 break;
1533 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001534 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001535 }
Emeric Brunf769f512010-10-22 17:14:01 +02001536
Willy Tarreau37406352012-04-23 16:16:37 +02001537 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001538 return 1;
1539}
1540
Willy Tarreaua5e37562011-12-16 17:06:15 +01001541/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001542static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001543smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001544 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001545{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001546 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001547
Willy Tarreauf853c462012-04-23 18:53:56 +02001548 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001549 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001550 return 0;
1551
Willy Tarreau37406352012-04-23 16:16:37 +02001552 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001553 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001554}
1555
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001556#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001557/* parse the "v4v6" bind keyword */
1558static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1559{
1560 struct listener *l;
1561
1562 list_for_each_entry(l, &conf->listeners, by_bind) {
1563 if (l->addr.ss_family == AF_INET6)
1564 l->options |= LI_O_V4V6;
1565 }
1566
1567 return 0;
1568}
1569
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001570/* parse the "v6only" bind keyword */
1571static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1572{
1573 struct listener *l;
1574
1575 list_for_each_entry(l, &conf->listeners, by_bind) {
1576 if (l->addr.ss_family == AF_INET6)
1577 l->options |= LI_O_V6ONLY;
1578 }
1579
1580 return 0;
1581}
1582#endif
1583
Pieter Baauwd551fb52013-05-08 22:49:23 +02001584#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001585/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001586static 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 +02001587{
1588 struct listener *l;
1589
Willy Tarreau4348fad2012-09-20 16:48:07 +02001590 list_for_each_entry(l, &conf->listeners, by_bind) {
1591 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1592 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001593 }
1594
Willy Tarreau44791242012-09-12 23:27:21 +02001595 return 0;
1596}
1597#endif
1598
1599#ifdef TCP_DEFER_ACCEPT
1600/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001601static 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 +02001602{
1603 struct listener *l;
1604
Willy Tarreau4348fad2012-09-20 16:48:07 +02001605 list_for_each_entry(l, &conf->listeners, by_bind) {
1606 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1607 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001608 }
1609
Willy Tarreau44791242012-09-12 23:27:21 +02001610 return 0;
1611}
1612#endif
1613
Willy Tarreau1c862c52012-10-05 16:21:00 +02001614#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001615/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001616static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1617{
1618 struct listener *l;
1619
1620 list_for_each_entry(l, &conf->listeners, by_bind) {
1621 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1622 l->options |= LI_O_TCP_FO;
1623 }
1624
1625 return 0;
1626}
1627#endif
1628
Willy Tarreau44791242012-09-12 23:27:21 +02001629#ifdef TCP_MAXSEG
1630/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001631static 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 +02001632{
1633 struct listener *l;
1634 int mss;
1635
Willy Tarreau44791242012-09-12 23:27:21 +02001636 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001637 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001638 return ERR_ALERT | ERR_FATAL;
1639 }
1640
1641 mss = atoi(args[cur_arg + 1]);
1642 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001643 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001644 return ERR_ALERT | ERR_FATAL;
1645 }
1646
Willy Tarreau4348fad2012-09-20 16:48:07 +02001647 list_for_each_entry(l, &conf->listeners, by_bind) {
1648 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1649 l->maxseg = mss;
1650 }
Willy Tarreau44791242012-09-12 23:27:21 +02001651
1652 return 0;
1653}
1654#endif
1655
1656#ifdef SO_BINDTODEVICE
1657/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001658static 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 +02001659{
1660 struct listener *l;
1661
Willy Tarreau44791242012-09-12 23:27:21 +02001662 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001663 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001664 return ERR_ALERT | ERR_FATAL;
1665 }
1666
Willy Tarreau4348fad2012-09-20 16:48:07 +02001667 list_for_each_entry(l, &conf->listeners, by_bind) {
1668 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1669 l->interface = strdup(args[cur_arg + 1]);
1670 }
Willy Tarreau44791242012-09-12 23:27:21 +02001671
1672 global.last_checks |= LSTCHK_NETADM;
1673 return 0;
1674}
1675#endif
1676
Willy Tarreaub6866442008-07-14 23:54:42 +02001677static struct cfg_kw_list cfg_kws = {{ },{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001678 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001679 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001680 { 0, NULL, NULL },
1681}};
1682
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001683
Willy Tarreau61612d42012-04-19 18:42:05 +02001684/* Note: must not be declared <const> as its list will be overwritten.
1685 * Please take care of keeping this list alphabetically sorted.
1686 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001687static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaud86e29d2013-03-25 08:21:05 +01001688 { "dst", NULL, acl_parse_ip, acl_match_ip },
1689 { "dst_port", NULL, acl_parse_int, acl_match_int },
1690 { "src", NULL, acl_parse_ip, acl_match_ip },
1691 { "src_port", NULL, acl_parse_int, acl_match_int },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001692 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02001693}};
1694
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001695
Willy Tarreau4a129812012-04-25 17:31:42 +02001696/* Note: must not be declared <const> as its list will be overwritten.
1697 * Note: fetches that may return multiple types must be declared as the lowest
1698 * common denominator, the type that can be casted into all other ones. For
1699 * instance v4/v6 must be declared v4.
1700 */
Willy Tarreau12785782012-04-27 21:37:17 +02001701static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001702 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1703 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1704 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1705 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1706 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001707}};
1708
Willy Tarreau44791242012-09-12 23:27:21 +02001709/************************************************************************/
1710/* All supported bind keywords must be declared here. */
1711/************************************************************************/
1712
1713/* Note: must not be declared <const> as its list will be overwritten.
1714 * Please take care of keeping this list alphabetically sorted, doing so helps
1715 * all code contributors.
1716 * Optional keywords are also declared with a NULL ->parse() function so that
1717 * the config parser can report an appropriate error when a known keyword was
1718 * not enabled.
1719 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001720static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001721#ifdef TCP_DEFER_ACCEPT
1722 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1723#endif
1724#ifdef SO_BINDTODEVICE
1725 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1726#endif
1727#ifdef TCP_MAXSEG
1728 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1729#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001730#ifdef TCP_FASTOPEN
1731 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1732#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001733#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001734 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1735#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001736#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001737 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001738 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1739#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001740 /* the versions with the NULL parse function*/
1741 { "defer-accept", NULL, 0 },
1742 { "interface", NULL, 1 },
1743 { "mss", NULL, 1 },
1744 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001745 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001746 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001747 { NULL, NULL, 0 },
1748}};
1749
Willy Tarreaue6b98942007-10-29 01:09:36 +01001750__attribute__((constructor))
1751static void __tcp_protocol_init(void)
1752{
1753 protocol_register(&proto_tcpv4);
1754 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001755 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001756 cfg_register_keywords(&cfg_kws);
1757 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001758 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001759}
1760
1761
1762/*
1763 * Local variables:
1764 * c-indent-level: 8
1765 * c-basic-offset: 8
1766 * End:
1767 */