blob: 4357b0407f1c3da833984aa08601a5dcd85c7c6a [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 Tarreau2b57cb82013-06-10 19:56:38 +020080 .drain = tcp_drain,
Willy Tarreaue6b98942007-10-29 01:09:36 +010081 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
82 .nb_listeners = 0,
83};
84
85/* Note: must not be declared <const> as its list will be overwritten */
86static struct protocol proto_tcpv6 = {
87 .name = "tcpv6",
88 .sock_domain = AF_INET6,
89 .sock_type = SOCK_STREAM,
90 .sock_prot = IPPROTO_TCP,
91 .sock_family = AF_INET6,
92 .sock_addrlen = sizeof(struct sockaddr_in6),
93 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020094 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020095 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020096 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010097 .bind_all = tcp_bind_listeners,
98 .unbind_all = unbind_all_listeners,
99 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200100 .get_src = tcp_get_src,
101 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200102 .drain = tcp_drain,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100103 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
104 .nb_listeners = 0,
105};
106
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100107
David du Colombier6f5ccb12011-03-10 22:26:24 +0100108/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100109 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
110 * - 0 : ignore remote address (may even be a NULL pointer)
111 * - 1 : use provided address
112 * - 2 : use provided port
113 * - 3 : use both
114 *
115 * The function supports multiple foreign binding methods :
116 * - linux_tproxy: we directly bind to the foreign address
117 * - cttproxy: we bind to a local address then nat.
118 * The second one can be used as a fallback for the first one.
119 * This function returns 0 when everything's OK, 1 if it could not bind, to the
120 * local address, 2 if it could not bind to the foreign address.
121 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100122int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100123{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100124 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100125 int foreign_ok = 0;
126 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100127 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200128 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200129
David du Colombier65c17962012-07-13 14:34:59 +0200130 switch (local->ss_family) {
131 case AF_INET:
132 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200133 /* This deserves some explanation. Some platforms will support
134 * multiple combinations of certain methods, so we try the
135 * supported ones until one succeeds.
136 */
137 if (0
138#if defined(IP_TRANSPARENT)
139 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
140#endif
141#if defined(IP_FREEBIND)
142 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
143#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200144#if defined(IP_BINDANY)
145 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
146#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200147#if defined(SO_BINDANY)
148 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
149#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200150 )
David du Colombier65c17962012-07-13 14:34:59 +0200151 foreign_ok = 1;
152 else
153 ip_transp_working = 0;
154 }
155 break;
156 case AF_INET6:
157 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200158 if (0
159#if defined(IPV6_TRANSPARENT)
160 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
161#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200162#if defined(IPV6_BINDANY)
163 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
164#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200165#if defined(SO_BINDANY)
166 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
167#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200168 )
David du Colombier65c17962012-07-13 14:34:59 +0200169 foreign_ok = 1;
170 else
171 ip6_transp_working = 0;
172 }
173 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100174 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200175
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100176 if (flags) {
177 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200178 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100179 switch (remote->ss_family) {
180 case AF_INET:
181 if (flags & 1)
182 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
183 if (flags & 2)
184 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
185 break;
186 case AF_INET6:
187 if (flags & 1)
188 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
189 if (flags & 2)
190 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
191 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100192 default:
193 /* we don't want to try to bind to an unknown address family */
194 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100195 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100196 }
197
Simon Hormande072bd2011-06-24 15:11:37 +0900198 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100199 if (foreign_ok) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200200 if (is_addr(&bind_addr)) {
201 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
202 if (ret < 0)
203 return 2;
204 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100205 }
206 else {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200207 if (is_addr(local)) {
208 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
209 if (ret < 0)
210 return 1;
211 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100212 }
213
214 if (!flags)
215 return 0;
216
217#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100218 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100219 struct in_tproxy itp1, itp2;
220 memset(&itp1, 0, sizeof(itp1));
221
222 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100223 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
224 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100225
226 /* set connect flag on socket */
227 itp2.op = TPROXY_FLAGS;
228 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
229
230 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
231 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
232 foreign_ok = 1;
233 }
234 }
235#endif
236 if (!foreign_ok)
237 /* we could not bind to a foreign address */
238 return 2;
239
240 return 0;
241}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100242
Willy Tarreau9650f372009-08-16 14:02:45 +0200243
244/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200245 * This function initiates a TCP connection establishment to the target assigned
246 * to connection <conn> using (si->{target,addr.to}). A source address may be
247 * pointed to by conn->addr.from in case of transparent proxying. Normal source
248 * bind addresses are still determined locally (due to the possible need of a
249 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100250 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100251 * supported. The <data> parameter is a boolean indicating whether there are data
252 * waiting for being sent or not, in order to adjust data write polling and on
253 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
254 * allows the caller to force using a delayed ACK when establishing the connection :
255 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
256 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
257 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200258 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200259 * It can return one of :
260 * - SN_ERR_NONE if everything's OK
261 * - SN_ERR_SRVTO if there are no more servers
262 * - SN_ERR_SRVCL if the connection was refused by the server
263 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
264 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
265 * - SN_ERR_INTERNAL for any other purely internal errors
266 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100267 *
268 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
269 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200270 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100271
Willy Tarreauf0837b22012-11-24 10:24:27 +0100272int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200273{
274 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100275 struct server *srv;
276 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100277 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100278
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100279 switch (obj_type(conn->target)) {
280 case OBJ_TYPE_PROXY:
281 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100282 srv = NULL;
283 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100284 case OBJ_TYPE_SERVER:
285 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100286 be = srv->proxy;
287 break;
288 default:
289 return SN_ERR_INTERNAL;
290 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200291
Willy Tarreau986a9d22012-08-30 21:11:38 +0200292 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200293 qfprintf(stderr, "Cannot get a server socket.\n");
294
295 if (errno == ENFILE)
296 send_log(be, LOG_EMERG,
297 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
298 be->id, maxfd);
299 else if (errno == EMFILE)
300 send_log(be, LOG_EMERG,
301 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
302 be->id, maxfd);
303 else if (errno == ENOBUFS || errno == ENOMEM)
304 send_log(be, LOG_EMERG,
305 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
306 be->id, maxfd);
307 /* this is a resource error */
308 return SN_ERR_RESOURCE;
309 }
310
311 if (fd >= global.maxsock) {
312 /* do not log anything there, it's a normal condition when this option
313 * is used to serialize connections to a server !
314 */
315 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
316 close(fd);
317 return SN_ERR_PRXCOND; /* it is a configuration limit */
318 }
319
320 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900321 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200322 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
323 close(fd);
324 return SN_ERR_INTERNAL;
325 }
326
327 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900328 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200329
Willy Tarreau9650f372009-08-16 14:02:45 +0200330 /* allow specific binding :
331 * - server-specific at first
332 * - proxy-specific next
333 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100334 if (srv && srv->conn_src.opts & CO_SRC_BIND)
335 src = &srv->conn_src;
336 else if (be->conn_src.opts & CO_SRC_BIND)
337 src = &be->conn_src;
338 else
339 src = NULL;
340
341 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200342 int ret, flags = 0;
343
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200344 if (is_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100345 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100346 case CO_SRC_TPROXY_ADDR:
347 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200348 flags = 3;
349 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100350 case CO_SRC_TPROXY_CIP:
351 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200352 flags = 1;
353 break;
354 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200355 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200356
Willy Tarreau9650f372009-08-16 14:02:45 +0200357#ifdef SO_BINDTODEVICE
358 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100359 if (src->iface_name)
360 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200361#endif
362
Willy Tarreaua4380b42012-12-08 22:49:11 +0100363 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200364 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100365 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200366
367 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100368 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200369
370 do {
371 /* note: in case of retry, we may have to release a previously
372 * allocated port, hence this loop's construct.
373 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200374 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
375 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200376
377 if (!attempts)
378 break;
379 attempts--;
380
Willy Tarreaua4380b42012-12-08 22:49:11 +0100381 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200382 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200383 break;
384
Willy Tarreaua4380b42012-12-08 22:49:11 +0100385 fdinfo[fd].port_range = src->sport_range;
386 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200387
Willy Tarreaua4380b42012-12-08 22:49:11 +0100388 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200389 } while (ret != 0); /* binding NOK */
390 }
391 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100392 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200393 }
394
Willy Tarreaua4380b42012-12-08 22:49:11 +0100395 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200396 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
397 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200398 close(fd);
399
Willy Tarreau9650f372009-08-16 14:02:45 +0200400 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100401 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200402 be->id);
403 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100404 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200405 be->id);
406 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100407 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200408 be->id);
409 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100410 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200411 be->id);
412 }
413 return SN_ERR_RESOURCE;
414 }
415 }
416
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400417#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200418 /* disabling tcp quick ack now allows the first request to leave the
419 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100420 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200421 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100422 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900423 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200424#endif
425
Willy Tarreaue803de22010-01-21 17:43:04 +0100426 if (global.tune.server_sndbuf)
427 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
428
429 if (global.tune.server_rcvbuf)
430 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
431
Willy Tarreau986a9d22012-08-30 21:11:38 +0200432 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200433 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
434
Willy Tarreaub1719512012-12-08 23:03:28 +0100435 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200436 char *msg;
Willy Tarreaub1719512012-12-08 23:03:28 +0100437 if (errno == EAGAIN || errno == EADDRNOTAVAIL)
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 msg = "no free ports";
439 else
440 msg = "local address already in use";
441
Willy Tarreaub1719512012-12-08 23:03:28 +0100442 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200443 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
444 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100446 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9650f372009-08-16 14:02:45 +0200447 return SN_ERR_RESOURCE;
448 } else if (errno == ETIMEDOUT) {
449 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200450 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
451 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 close(fd);
453 return SN_ERR_SRVTO;
454 } else {
455 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
456 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200457 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
458 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200459 close(fd);
460 return SN_ERR_SRVCL;
461 }
462 }
463
Willy Tarreau986a9d22012-08-30 21:11:38 +0200464 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200465 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100466 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200467
Willy Tarreaud2274c62012-07-06 14:29:45 +0200468 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200470 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200471
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200472 if (conn_xprt_init(conn) < 0) {
Willy Tarreau184636e2012-09-06 14:04:41 +0200473 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200474 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200475 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200476
Willy Tarreau14f8e862012-08-30 22:23:13 +0200477 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200478 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200479
Willy Tarreau9650f372009-08-16 14:02:45 +0200480 return SN_ERR_NONE; /* connection is OK */
481}
482
483
Willy Tarreau59b94792012-05-11 16:16:40 +0200484/*
485 * Retrieves the source address for the socket <fd>, with <dir> indicating
486 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
487 * success, -1 in case of error. The socket's source address is stored in
488 * <sa> for <salen> bytes.
489 */
490int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
491{
492 if (dir)
493 return getsockname(fd, sa, &salen);
494 else
495 return getpeername(fd, sa, &salen);
496}
497
498
499/*
500 * Retrieves the original destination address for the socket <fd>, with <dir>
501 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
502 * listener, if the original destination address was translated, the original
503 * address is retrieved. It returns 0 in case of success, -1 in case of error.
504 * The socket's source address is stored in <sa> for <salen> bytes.
505 */
506int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
507{
508 if (dir)
509 return getpeername(fd, sa, &salen);
510#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
511 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
512 return 0;
513#endif
514 else
515 return getsockname(fd, sa, &salen);
516}
517
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200518/* Tries to drain any pending incoming data from the socket to reach the
519 * receive shutdown. Returns non-zero if the shutdown was found, otherwise
520 * zero. This is useful to decide whether we can close a connection cleanly
521 * are we must kill it hard.
522 */
523int tcp_drain(int fd)
524{
525 int turns = 2;
526 int len;
527
528 while (turns) {
529#ifdef MSG_TRUNC_CLEARS_INPUT
530 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
531 if (len == -1 && errno == EFAULT)
532#endif
533 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
534
535 if (len == 0) /* cool, shutdown received */
536 return 1;
537
538 if (len < 0) {
539 if (errno == EAGAIN) /* connection not closed yet */
540 return 0;
541 if (errno == EINTR) /* oops, try again */
542 continue;
543 /* other errors indicate a dead connection, fine. */
544 return 1;
545 }
546 /* OK we read some data, let's try again once */
547 turns--;
548 }
549 /* some data are still present, give up */
550 return 0;
551}
552
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200553/* This is the callback which is set when a connection establishment is pending
554 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200555 * once the connection is established. It updates the FD polling status. It
556 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
557 * it returns non-zero and removes itself from the connection's flags (the bit is
558 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200559 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200560int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200561{
Willy Tarreau239d7182012-07-23 18:53:03 +0200562 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200563
Willy Tarreau80184712012-07-06 14:54:49 +0200564 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200565 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200566
Willy Tarreau80184712012-07-06 14:54:49 +0200567 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200568 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200569
Willy Tarreau2da156f2012-07-23 15:07:23 +0200570 /* stop here if we reached the end of data */
571 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
572 goto out_error;
573
Willy Tarreau2c6be842012-07-06 17:12:34 +0200574 /* We have no data to send to check the connection, and
575 * getsockopt() will not inform us whether the connection
576 * is still pending. So we'll reuse connect() to check the
577 * state of the socket. This has the advantage of giving us
578 * the following info :
579 * - error
580 * - connecting (EALREADY, EINPROGRESS)
581 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200582 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200583 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200584 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100585 __conn_sock_stop_recv(conn);
586 __conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200587 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200588 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200589
Willy Tarreau2c6be842012-07-06 17:12:34 +0200590 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200591 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200592
Willy Tarreau2c6be842012-07-06 17:12:34 +0200593 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200594 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200595
Willy Tarreau076be252012-07-06 16:02:29 +0200596 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200597 * forward the event to the transport layer which will notify the
598 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200599 */
Willy Tarreau80184712012-07-06 14:54:49 +0200600 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200601 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200602
603 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200604 /* Write error on the file descriptor. Report it to the connection
605 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200606 */
607
Willy Tarreau80184712012-07-06 14:54:49 +0200608 conn->flags |= CO_FL_ERROR;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100609 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200610 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200611}
612
Willy Tarreau59b94792012-05-11 16:16:40 +0200613
Willy Tarreaue6b98942007-10-29 01:09:36 +0100614/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100615 * an error message in <errmsg> if the message is at most <errlen> bytes long
616 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
617 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100618 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
619 * was alright and that no message was returned. ERR_RETRYABLE means that an
620 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700621 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100622 * the meaning of the error, but just indicate that a message is present which
623 * should be displayed with the respective level. Last, ERR_ABORT indicates
624 * that it's pointless to try to start other listeners. No error message is
625 * returned if errlen is NULL.
626 */
627int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
628{
629 __label__ tcp_return, tcp_close_return;
630 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100631 int ext, ready;
632 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100633 const char *msg = NULL;
634
635 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100636 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100637 *errmsg = 0;
638
639 if (listener->state != LI_ASSIGNED)
640 return ERR_NONE; /* already bound */
641
642 err = ERR_NONE;
643
Willy Tarreau40aa0702013-03-10 23:51:38 +0100644 /* if the listener already has an fd assigned, then we were offered the
645 * fd by an external process (most likely the parent), and we don't want
646 * to create a new socket. However we still want to set a few flags on
647 * the socket.
648 */
649 fd = listener->fd;
650 ext = (fd >= 0);
651
652 if (!ext && (fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100653 err |= ERR_RETRYABLE | ERR_ALERT;
654 msg = "cannot create listening socket";
655 goto tcp_return;
656 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100657
Willy Tarreaue6b98942007-10-29 01:09:36 +0100658 if (fd >= global.maxsock) {
659 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
660 msg = "not enough free sockets (raise '-n' parameter)";
661 goto tcp_close_return;
662 }
663
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200664 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100665 err |= ERR_FATAL | ERR_ALERT;
666 msg = "cannot make socket non-blocking";
667 goto tcp_close_return;
668 }
669
Willy Tarreau40aa0702013-03-10 23:51:38 +0100670 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100671 /* not fatal but should be reported */
672 msg = "cannot do so_reuseaddr";
673 err |= ERR_ALERT;
674 }
675
676 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900677 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100678
Willy Tarreaue6b98942007-10-29 01:09:36 +0100679#ifdef SO_REUSEPORT
680 /* OpenBSD supports this. As it's present in old libc versions of Linux,
681 * it might return an error that we will silently ignore.
682 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100683 if (!ext)
684 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100685#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200686
Willy Tarreau40aa0702013-03-10 23:51:38 +0100687 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200688 switch (listener->addr.ss_family) {
689 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200690 if (1
691#if defined(IP_TRANSPARENT)
692 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
693#endif
694#if defined(IP_FREEBIND)
695 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
696#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200697#if defined(IP_BINDANY)
698 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
699#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200700#if defined(SO_BINDANY)
701 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
702#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200703 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200704 msg = "cannot make listening socket transparent";
705 err |= ERR_ALERT;
706 }
707 break;
708 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200709 if (1
710#if defined(IPV6_TRANSPARENT)
711 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
712#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200713#if defined(IPV6_BINDANY)
714 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
715#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200716#if defined(SO_BINDANY)
717 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
718#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200719 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200720 msg = "cannot make listening socket transparent";
721 err |= ERR_ALERT;
722 }
723 break;
724 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100725 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200726
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100727#ifdef SO_BINDTODEVICE
728 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100729 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100730 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100731 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100732 msg = "cannot bind listener to device";
733 err |= ERR_WARN;
734 }
735 }
736#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400737#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100738 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400739 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200740 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
741 msg = "cannot set MSS";
742 err |= ERR_WARN;
743 }
744 }
745#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200746#if defined(TCP_DEFER_ACCEPT)
747 if (listener->options & LI_O_DEF_ACCEPT) {
748 /* defer accept by up to one second */
749 int accept_delay = 1;
750 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
751 msg = "cannot enable DEFER_ACCEPT";
752 err |= ERR_WARN;
753 }
754 }
755#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200756#if defined(TCP_FASTOPEN)
757 if (listener->options & LI_O_TCP_FO) {
758 /* TFO needs a queue length, let's use the configured backlog */
759 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
760 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
761 msg = "cannot enable TCP_FASTOPEN";
762 err |= ERR_WARN;
763 }
764 }
765#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100766#if defined(IPV6_V6ONLY)
767 if (listener->options & LI_O_V6ONLY)
768 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100769 else if (listener->options & LI_O_V4V6)
770 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100771#endif
772
Willy Tarreau40aa0702013-03-10 23:51:38 +0100773 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100774 err |= ERR_RETRYABLE | ERR_ALERT;
775 msg = "cannot bind socket";
776 goto tcp_close_return;
777 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100778
Willy Tarreau40aa0702013-03-10 23:51:38 +0100779 ready = 0;
780 ready_len = sizeof(ready);
781 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
782 ready = 0;
783
784 if (!(ext && ready) && /* only listen if not already done by external process */
785 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100786 err |= ERR_RETRYABLE | ERR_ALERT;
787 msg = "cannot listen to socket";
788 goto tcp_close_return;
789 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100790
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400791#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200792 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900793 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200794#endif
795
Willy Tarreaue6b98942007-10-29 01:09:36 +0100796 /* the socket is ready */
797 listener->fd = fd;
798 listener->state = LI_LISTEN;
799
Willy Tarreaueabf3132008-08-29 23:36:51 +0200800 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200801 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200802 fd_insert(fd);
803
Willy Tarreaue6b98942007-10-29 01:09:36 +0100804 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100805 if (msg && errlen) {
806 char pn[INET6_ADDRSTRLEN];
807
Willy Tarreau631f01c2011-09-05 00:36:48 +0200808 addr_to_str(&listener->addr, pn, sizeof(pn));
809 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100810 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100811 return err;
812
813 tcp_close_return:
814 close(fd);
815 goto tcp_return;
816}
817
818/* This function creates all TCP sockets bound to the protocol entry <proto>.
819 * It is intended to be used as the protocol's bind_all() function.
820 * The sockets will be registered but not added to any fd_set, in order not to
821 * loose them across the fork(). A call to enable_all_listeners() is needed
822 * to complete initialization. The return value is composed from ERR_*.
823 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200824static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100825{
826 struct listener *listener;
827 int err = ERR_NONE;
828
829 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200830 err |= tcp_bind_listener(listener, errmsg, errlen);
831 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100832 break;
833 }
834
835 return err;
836}
837
838/* Add listener to the list of tcpv4 listeners. The listener's state
839 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
840 * listeners is updated. This is the function to use to add a new listener.
841 */
842void tcpv4_add_listener(struct listener *listener)
843{
844 if (listener->state != LI_INIT)
845 return;
846 listener->state = LI_ASSIGNED;
847 listener->proto = &proto_tcpv4;
848 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
849 proto_tcpv4.nb_listeners++;
850}
851
852/* Add listener to the list of tcpv4 listeners. The listener's state
853 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
854 * listeners is updated. This is the function to use to add a new listener.
855 */
856void tcpv6_add_listener(struct listener *listener)
857{
858 if (listener->state != LI_INIT)
859 return;
860 listener->state = LI_ASSIGNED;
861 listener->proto = &proto_tcpv6;
862 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
863 proto_tcpv6.nb_listeners++;
864}
865
Willy Tarreauedcf6682008-11-30 23:15:34 +0100866/* This function performs the TCP request analysis on the current request. It
867 * returns 1 if the processing can continue on next analysers, or zero if it
868 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200869 * request. It relies on buffers flags, and updates s->req->analysers. The
870 * function may be called for frontend rules and backend rules. It only relies
871 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100872 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200873int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100874{
875 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200876 struct stksess *ts;
877 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100878 int partial;
879
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100880 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 +0100881 now_ms, __FUNCTION__,
882 s,
883 req,
884 req->rex, req->wex,
885 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200886 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100887 req->analysers);
888
Willy Tarreauedcf6682008-11-30 23:15:34 +0100889 /* We don't know whether we have enough data, so must proceed
890 * this way :
891 * - iterate through all rules in their declaration order
892 * - if one rule returns MISS, it means the inspect delay is
893 * not over yet, then return immediately, otherwise consider
894 * it as a non-match.
895 * - if one rule returns OK, then return OK
896 * - if one rule returns KO, then return KO
897 */
898
Willy Tarreau9b28e032012-10-12 23:49:43 +0200899 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200900 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200901 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100902 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200903 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100904
Willy Tarreaufb356202010-08-03 14:02:05 +0200905 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100906 int ret = ACL_PAT_PASS;
907
908 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200909 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100910 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200911 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100912 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200913 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
914 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100915 return 0;
916 }
917
918 ret = acl_pass(ret);
919 if (rule->cond->pol == ACL_COND_UNLESS)
920 ret = !ret;
921 }
922
923 if (ret) {
924 /* we have a matching rule. */
925 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200926 channel_abort(req);
927 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100928 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200929
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100930 s->be->be_counters.denied_req++;
931 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200932 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200933 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200934
Willy Tarreauedcf6682008-11-30 23:15:34 +0100935 if (!(s->flags & SN_ERR_MASK))
936 s->flags |= SN_ERR_PRXCOND;
937 if (!(s->flags & SN_FINST_MASK))
938 s->flags |= SN_FINST_R;
939 return 0;
940 }
Willy Tarreaube4a3ef2013-06-17 15:04:07 +0200941 else if ((rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SC2) &&
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200942 !s->stkctr[tcp_trk_idx(rule->action)].entry) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100943 /* Note: only the first valid tracking parameter of each
944 * applies.
945 */
946 struct stktable_key *key;
947
948 t = rule->act_prm.trk_ctr.table.t;
949 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
950
951 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200952 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
953 if (s->fe != s->be)
Willy Tarreaube4a3ef2013-06-17 15:04:07 +0200954 s->flags |= SN_BE_TRACK_SC0 << tcp_trk_idx(rule->action);
Willy Tarreaud1f96522010-08-03 19:34:32 +0200955 }
956 }
957 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100958 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200959 break;
960 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100961 }
962 }
963
964 /* if we get there, it means we have no rule which matches, or
965 * we have an explicit accept, so we apply the default accept.
966 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200967 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100968 req->analyse_exp = TICK_ETERNITY;
969 return 1;
970}
971
Emeric Brun97679e72010-09-23 17:56:44 +0200972/* This function performs the TCP response analysis on the current response. It
973 * returns 1 if the processing can continue on next analysers, or zero if it
974 * needs more data, encounters an error, or wants to immediately abort the
975 * response. It relies on buffers flags, and updates s->rep->analysers. The
976 * function may be called for backend rules.
977 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200978int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200979{
980 struct tcp_rule *rule;
981 int partial;
982
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100983 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 +0200984 now_ms, __FUNCTION__,
985 s,
986 rep,
987 rep->rex, rep->wex,
988 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200989 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200990 rep->analysers);
991
992 /* We don't know whether we have enough data, so must proceed
993 * this way :
994 * - iterate through all rules in their declaration order
995 * - if one rule returns MISS, it means the inspect delay is
996 * not over yet, then return immediately, otherwise consider
997 * it as a non-match.
998 * - if one rule returns OK, then return OK
999 * - if one rule returns KO, then return KO
1000 */
1001
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001002 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001003 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001004 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001005 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001006
1007 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
1008 int ret = ACL_PAT_PASS;
1009
1010 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001011 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +02001012 if (ret == ACL_PAT_MISS) {
1013 /* just set the analyser timeout once at the beginning of the response */
1014 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
1015 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
1016 return 0;
1017 }
1018
1019 ret = acl_pass(ret);
1020 if (rule->cond->pol == ACL_COND_UNLESS)
1021 ret = !ret;
1022 }
1023
1024 if (ret) {
1025 /* we have a matching rule. */
1026 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001027 channel_abort(rep);
1028 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001029 rep->analysers = 0;
1030
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001031 s->be->be_counters.denied_resp++;
1032 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001033 if (s->listener->counters)
1034 s->listener->counters->denied_resp++;
1035
1036 if (!(s->flags & SN_ERR_MASK))
1037 s->flags |= SN_ERR_PRXCOND;
1038 if (!(s->flags & SN_FINST_MASK))
1039 s->flags |= SN_FINST_D;
1040 return 0;
1041 }
1042 else {
1043 /* otherwise accept */
1044 break;
1045 }
1046 }
1047 }
1048
1049 /* if we get there, it means we have no rule which matches, or
1050 * we have an explicit accept, so we apply the default accept.
1051 */
1052 rep->analysers &= ~an_bit;
1053 rep->analyse_exp = TICK_ETERNITY;
1054 return 1;
1055}
1056
1057
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001058/* This function performs the TCP layer4 analysis on the current request. It
1059 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1060 * matches or if no more rule matches. It can only use rules which don't need
1061 * any data.
1062 */
1063int tcp_exec_req_rules(struct session *s)
1064{
1065 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001066 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001067 struct stktable *t = NULL;
1068 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001069 int ret;
1070
1071 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1072 ret = ACL_PAT_PASS;
1073
1074 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001075 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001076 ret = acl_pass(ret);
1077 if (rule->cond->pol == ACL_COND_UNLESS)
1078 ret = !ret;
1079 }
1080
1081 if (ret) {
1082 /* we have a matching rule. */
1083 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001084 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001085 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001086 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001087
1088 if (!(s->flags & SN_ERR_MASK))
1089 s->flags |= SN_ERR_PRXCOND;
1090 if (!(s->flags & SN_FINST_MASK))
1091 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001092 result = 0;
1093 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001094 }
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001095 else if ((rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SC2) &&
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001096 !s->stkctr[tcp_trk_idx(rule->action)].entry) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001097 /* Note: only the first valid tracking parameter of each
1098 * applies.
1099 */
1100 struct stktable_key *key;
1101
1102 t = rule->act_prm.trk_ctr.table.t;
1103 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
1104
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001105 if (key && (ts = stktable_get_entry(t, key)))
1106 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001107 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001108 else if (rule->action == TCP_ACT_EXPECT_PX) {
1109 s->si[0].conn->flags |= CO_FL_ACCEPT_PROXY;
1110 conn_sock_want_recv(s->si[0].conn);
1111 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001112 else {
1113 /* otherwise it's an accept */
1114 break;
1115 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001116 }
1117 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001118 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001119}
1120
Emeric Brun97679e72010-09-23 17:56:44 +02001121/* Parse a tcp-response rule. Return a negative value in case of failure */
1122static int tcp_parse_response_rule(char **args, int arg, int section_type,
1123 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau80aca902013-01-07 15:42:20 +01001124 struct tcp_rule *rule, char **err,
1125 unsigned int where)
Emeric Brun97679e72010-09-23 17:56:44 +02001126{
1127 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001128 memprintf(err, "%s %s is only allowed in 'backend' sections",
1129 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001130 return -1;
1131 }
1132
1133 if (strcmp(args[arg], "accept") == 0) {
1134 arg++;
1135 rule->action = TCP_ACT_ACCEPT;
1136 }
1137 else if (strcmp(args[arg], "reject") == 0) {
1138 arg++;
1139 rule->action = TCP_ACT_REJECT;
1140 }
1141 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001142 memprintf(err,
1143 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1144 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001145 return -1;
1146 }
1147
1148 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001149 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1150 memprintf(err,
1151 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1152 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001153 return -1;
1154 }
1155 }
1156 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001157 memprintf(err,
1158 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001159 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1160 return -1;
1161 }
1162 return 0;
1163}
1164
1165
1166
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001167/* Parse a tcp-request rule. Return a negative value in case of failure */
1168static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001169 struct proxy *curpx, struct proxy *defpx,
1170 struct tcp_rule *rule, char **err,
1171 unsigned int where)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001172{
1173 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001174 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1175 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001176 return -1;
1177 }
1178
1179 if (!strcmp(args[arg], "accept")) {
1180 arg++;
1181 rule->action = TCP_ACT_ACCEPT;
1182 }
1183 else if (!strcmp(args[arg], "reject")) {
1184 arg++;
1185 rule->action = TCP_ACT_REJECT;
1186 }
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001187 else if (strcmp(args[arg], "track-sc0") == 0 || strcmp(args[arg], "track-sc1") == 0 || strcmp(args[arg], "track-sc2") == 0) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001188 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001189 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001190
1191 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001192
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001193 curpx->conf.args.ctx = ARGC_TRK;
1194 expr = sample_parse_expr(args, &arg, trash.str, trash.size, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001195 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001196 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001197 "'%s %s %s' : %s",
1198 args[0], args[1], args[kw], trash.str);
1199 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001200 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001201
Willy Tarreau80aca902013-01-07 15:42:20 +01001202 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001203 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001204 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001205 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001206 free(expr);
1207 return -1;
1208 }
1209
1210 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001211 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001212
1213 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001214 arg++;
1215 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001216 memprintf(err,
1217 "'%s %s %s' : missing table name",
1218 args[0], args[1], args[kw]);
1219 free(expr);
1220 return -1;
1221 }
1222 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001223 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001224 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001225 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001226 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001227 rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001228 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001229 else if (strcmp(args[arg], "expect-proxy") == 0) {
1230 if (strcmp(args[arg+1], "layer4") != 0) {
1231 memprintf(err,
1232 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1233 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1234 return -1;
1235 }
1236
1237 if (!(where & SMP_VAL_FE_CON_ACC)) {
1238 memprintf(err,
1239 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1240 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1241 return -1;
1242 }
1243
1244 arg += 2;
1245 rule->action = TCP_ACT_EXPECT_PX;
1246 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001247 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001248 memprintf(err,
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001249 "'%s %s' expects 'accept', 'reject', 'track-sc0', 'track-sc1' "
1250 " or 'track-sc2' in %s '%s' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001251 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001252 return -1;
1253 }
1254
1255 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001256 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1257 memprintf(err,
1258 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1259 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001260 return -1;
1261 }
1262 }
1263 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001264 memprintf(err,
1265 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001266 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1267 return -1;
1268 }
1269 return 0;
1270}
1271
Emeric Brun97679e72010-09-23 17:56:44 +02001272/* This function should be called to parse a line starting with the "tcp-response"
1273 * keyword.
1274 */
1275static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001276 struct proxy *defpx, const char *file, int line,
1277 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001278{
1279 const char *ptr = NULL;
1280 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001281 int warn = 0;
1282 int arg;
1283 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001284 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001285 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001286 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001287
1288 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001289 memprintf(err, "missing argument for '%s' in %s '%s'",
1290 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001291 return -1;
1292 }
1293
1294 if (strcmp(args[1], "inspect-delay") == 0) {
1295 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001296 memprintf(err, "%s %s is only allowed in 'backend' sections",
1297 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001298 return -1;
1299 }
1300
1301 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001302 memprintf(err,
1303 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1304 args[0], args[1], proxy_type_str(curpx), curpx->id);
1305 if (ptr)
1306 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001307 return -1;
1308 }
1309
1310 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001311 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1312 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001313 return 1;
1314 }
1315 curpx->tcp_rep.inspect_delay = val;
1316 return 0;
1317 }
1318
Simon Hormande072bd2011-06-24 15:11:37 +09001319 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001320 LIST_INIT(&rule->list);
1321 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001322 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001323
1324 if (strcmp(args[1], "content") == 0) {
1325 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001326
1327 if (curpx->cap & PR_CAP_FE)
1328 where |= SMP_VAL_FE_RES_CNT;
1329 if (curpx->cap & PR_CAP_BE)
1330 where |= SMP_VAL_BE_RES_CNT;
1331
1332 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001333 goto error;
1334
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001335 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1336 if (acl) {
1337 if (acl->name && *acl->name)
1338 memprintf(err,
1339 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1340 acl->name, args[0], args[1], sample_ckp_names(where));
1341 else
1342 memprintf(err,
1343 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1344 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001345 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001346 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001347
Emeric Brun97679e72010-09-23 17:56:44 +02001348 warn++;
1349 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001350 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1351 if (acl->name && *acl->name)
1352 memprintf(err,
1353 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001354 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001355 else
1356 memprintf(err,
1357 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001358 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001359 warn++;
1360 }
Emeric Brun97679e72010-09-23 17:56:44 +02001361
1362 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1363 }
1364 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001365 memprintf(err,
1366 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1367 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001368 goto error;
1369 }
1370
1371 return warn;
1372 error:
1373 free(rule);
1374 return -1;
1375}
1376
1377
Willy Tarreaub6866442008-07-14 23:54:42 +02001378/* This function should be called to parse a line starting with the "tcp-request"
1379 * keyword.
1380 */
1381static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001382 struct proxy *defpx, const char *file, int line,
1383 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001384{
1385 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001386 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001387 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001388 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001389 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001390 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001391 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001392 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001393
1394 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001395 if (curpx == defpx)
1396 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1397 else
1398 memprintf(err, "missing argument for '%s' in %s '%s'",
1399 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001400 return -1;
1401 }
1402
1403 if (!strcmp(args[1], "inspect-delay")) {
1404 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001405 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1406 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001407 return -1;
1408 }
1409
Willy Tarreaub6866442008-07-14 23:54:42 +02001410 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001411 memprintf(err,
1412 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1413 args[0], args[1], proxy_type_str(curpx), curpx->id);
1414 if (ptr)
1415 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001416 return -1;
1417 }
1418
1419 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001420 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1421 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001422 return 1;
1423 }
1424 curpx->tcp_req.inspect_delay = val;
1425 return 0;
1426 }
1427
Simon Hormande072bd2011-06-24 15:11:37 +09001428 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001429 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001430 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001431 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001432
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001433 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001434 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001435
1436 if (curpx->cap & PR_CAP_FE)
1437 where |= SMP_VAL_FE_REQ_CNT;
1438 if (curpx->cap & PR_CAP_BE)
1439 where |= SMP_VAL_BE_REQ_CNT;
1440
1441 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001442 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001443
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001444 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1445 if (acl) {
1446 if (acl->name && *acl->name)
1447 memprintf(err,
1448 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1449 acl->name, args[0], args[1], sample_ckp_names(where));
1450 else
1451 memprintf(err,
1452 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1453 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001454 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001455 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001456
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001457 warn++;
1458 }
1459 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1460 if (acl->name && *acl->name)
1461 memprintf(err,
1462 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001463 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001464 else
1465 memprintf(err,
1466 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001467 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001468 warn++;
1469 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001470
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001471 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001472 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001473 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001474 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001475
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001476 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001477 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1478 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001479 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001480 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001481
Willy Tarreau80aca902013-01-07 15:42:20 +01001482 where |= SMP_VAL_FE_CON_ACC;
1483
1484 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001485 goto error;
1486
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001487 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1488 if (acl) {
1489 if (acl->name && *acl->name)
1490 memprintf(err,
1491 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1492 acl->name, args[0], args[1], sample_ckp_names(where));
1493 else
1494 memprintf(err,
1495 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1496 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001497 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001498 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001499
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001500 warn++;
1501 }
1502 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1503 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001504 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001505 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001506 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001507 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001508 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001509 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001510 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001511 warn++;
1512 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001513
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001514 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001515 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001516 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001517 if (curpx == defpx)
1518 memprintf(err,
1519 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1520 args[0], args[1]);
1521 else
1522 memprintf(err,
1523 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001524 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001525 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001526 }
1527
Willy Tarreau1a687942010-05-23 22:40:30 +02001528 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001529 error:
1530 free(rule);
1531 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001532}
1533
Willy Tarreau645513a2010-05-24 20:55:15 +02001534
1535/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001536/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001537/************************************************************************/
1538
Willy Tarreau4a129812012-04-25 17:31:42 +02001539/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001540static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001541smp_fetch_src(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 switch (l4->si[0].conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001545 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001546 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001547 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001548 break;
1549 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001550 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001551 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001552 break;
1553 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001554 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001555 }
Emeric Brunf769f512010-10-22 17:14:01 +02001556
Willy Tarreau37406352012-04-23 16:16:37 +02001557 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001558 return 1;
1559}
1560
Willy Tarreaua5e37562011-12-16 17:06:15 +01001561/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001562static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001563smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001564 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001565{
Willy Tarreauf853c462012-04-23 18:53:56 +02001566 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001567 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001568 return 0;
1569
Willy Tarreau37406352012-04-23 16:16:37 +02001570 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001571 return 1;
1572}
1573
Willy Tarreau4a129812012-04-25 17:31:42 +02001574/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001575static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001576smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001577 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001578{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001579 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001580
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001581 switch (l4->si[0].conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001582 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001583 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001584 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001585 break;
1586 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001587 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001588 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001589 break;
1590 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001591 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001592 }
Emeric Brunf769f512010-10-22 17:14:01 +02001593
Willy Tarreau37406352012-04-23 16:16:37 +02001594 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001595 return 1;
1596}
1597
Willy Tarreaua5e37562011-12-16 17:06:15 +01001598/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001599static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001600smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001601 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001602{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001603 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001604
Willy Tarreauf853c462012-04-23 18:53:56 +02001605 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001606 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001607 return 0;
1608
Willy Tarreau37406352012-04-23 16:16:37 +02001609 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001610 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001611}
1612
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001613#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001614/* parse the "v4v6" bind keyword */
1615static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1616{
1617 struct listener *l;
1618
1619 list_for_each_entry(l, &conf->listeners, by_bind) {
1620 if (l->addr.ss_family == AF_INET6)
1621 l->options |= LI_O_V4V6;
1622 }
1623
1624 return 0;
1625}
1626
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001627/* parse the "v6only" bind keyword */
1628static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1629{
1630 struct listener *l;
1631
1632 list_for_each_entry(l, &conf->listeners, by_bind) {
1633 if (l->addr.ss_family == AF_INET6)
1634 l->options |= LI_O_V6ONLY;
1635 }
1636
1637 return 0;
1638}
1639#endif
1640
Pieter Baauwd551fb52013-05-08 22:49:23 +02001641#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001642/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001643static 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 +02001644{
1645 struct listener *l;
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->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001650 }
1651
Willy Tarreau44791242012-09-12 23:27:21 +02001652 return 0;
1653}
1654#endif
1655
1656#ifdef TCP_DEFER_ACCEPT
1657/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001658static 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 +02001659{
1660 struct listener *l;
1661
Willy Tarreau4348fad2012-09-20 16:48:07 +02001662 list_for_each_entry(l, &conf->listeners, by_bind) {
1663 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1664 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001665 }
1666
Willy Tarreau44791242012-09-12 23:27:21 +02001667 return 0;
1668}
1669#endif
1670
Willy Tarreau1c862c52012-10-05 16:21:00 +02001671#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001672/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001673static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1674{
1675 struct listener *l;
1676
1677 list_for_each_entry(l, &conf->listeners, by_bind) {
1678 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1679 l->options |= LI_O_TCP_FO;
1680 }
1681
1682 return 0;
1683}
1684#endif
1685
Willy Tarreau44791242012-09-12 23:27:21 +02001686#ifdef TCP_MAXSEG
1687/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001688static 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 +02001689{
1690 struct listener *l;
1691 int mss;
1692
Willy Tarreau44791242012-09-12 23:27:21 +02001693 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001694 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001695 return ERR_ALERT | ERR_FATAL;
1696 }
1697
1698 mss = atoi(args[cur_arg + 1]);
1699 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001700 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001701 return ERR_ALERT | ERR_FATAL;
1702 }
1703
Willy Tarreau4348fad2012-09-20 16:48:07 +02001704 list_for_each_entry(l, &conf->listeners, by_bind) {
1705 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1706 l->maxseg = mss;
1707 }
Willy Tarreau44791242012-09-12 23:27:21 +02001708
1709 return 0;
1710}
1711#endif
1712
1713#ifdef SO_BINDTODEVICE
1714/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001715static 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 +02001716{
1717 struct listener *l;
1718
Willy Tarreau44791242012-09-12 23:27:21 +02001719 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001720 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001721 return ERR_ALERT | ERR_FATAL;
1722 }
1723
Willy Tarreau4348fad2012-09-20 16:48:07 +02001724 list_for_each_entry(l, &conf->listeners, by_bind) {
1725 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1726 l->interface = strdup(args[cur_arg + 1]);
1727 }
Willy Tarreau44791242012-09-12 23:27:21 +02001728
1729 global.last_checks |= LSTCHK_NETADM;
1730 return 0;
1731}
1732#endif
1733
Willy Tarreaudc13c112013-06-21 23:16:39 +02001734static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001735 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001736 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001737 { 0, NULL, NULL },
1738}};
1739
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001740
Willy Tarreau61612d42012-04-19 18:42:05 +02001741/* Note: must not be declared <const> as its list will be overwritten.
1742 * Please take care of keeping this list alphabetically sorted.
1743 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001744static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001745 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02001746}};
1747
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001748
Willy Tarreau4a129812012-04-25 17:31:42 +02001749/* Note: must not be declared <const> as its list will be overwritten.
1750 * Note: fetches that may return multiple types must be declared as the lowest
1751 * common denominator, the type that can be casted into all other ones. For
1752 * instance v4/v6 must be declared v4.
1753 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001754static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001755 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1756 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1757 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1758 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1759 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001760}};
1761
Willy Tarreau44791242012-09-12 23:27:21 +02001762/************************************************************************/
1763/* All supported bind keywords must be declared here. */
1764/************************************************************************/
1765
1766/* Note: must not be declared <const> as its list will be overwritten.
1767 * Please take care of keeping this list alphabetically sorted, doing so helps
1768 * all code contributors.
1769 * Optional keywords are also declared with a NULL ->parse() function so that
1770 * the config parser can report an appropriate error when a known keyword was
1771 * not enabled.
1772 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001773static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001774#ifdef TCP_DEFER_ACCEPT
1775 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1776#endif
1777#ifdef SO_BINDTODEVICE
1778 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1779#endif
1780#ifdef TCP_MAXSEG
1781 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1782#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001783#ifdef TCP_FASTOPEN
1784 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1785#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001786#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001787 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1788#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001789#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001790 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001791 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1792#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001793 /* the versions with the NULL parse function*/
1794 { "defer-accept", NULL, 0 },
1795 { "interface", NULL, 1 },
1796 { "mss", NULL, 1 },
1797 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001798 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001799 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001800 { NULL, NULL, 0 },
1801}};
1802
Willy Tarreaue6b98942007-10-29 01:09:36 +01001803__attribute__((constructor))
1804static void __tcp_protocol_init(void)
1805{
1806 protocol_register(&proto_tcpv4);
1807 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001808 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001809 cfg_register_keywords(&cfg_kws);
1810 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001811 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001812}
1813
1814
1815/*
1816 * Local variables:
1817 * c-indent-level: 8
1818 * c-basic-offset: 8
1819 * End:
1820 */