blob: 56fa2a392d85ed2035d824244cd2d0463fc9532b [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040027#include <netinet/tcp.h>
28
Willy Tarreaub6866442008-07-14 23:54:42 +020029#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010036
Willy Tarreaue6b98942007-10-29 01:09:36 +010037#include <types/global.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020038#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039
40#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020041#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020044#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020046#include <proto/log.h>
47#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020048#include <proto/protocol.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010049#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020051#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020052#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/stick_table.h>
Willy Tarreaucc1e04b2013-09-11 23:20:29 +020054#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020055#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010056
Willy Tarreaue8c66af2008-01-13 18:40:14 +010057#ifdef CONFIG_HAP_CTTPROXY
58#include <import/ip_tproxy.h>
59#endif
60
Emeric Bruncf20bf12010-10-22 16:06:11 +020061static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
62static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010063
64/* Note: must not be declared <const> as its list will be overwritten */
65static struct protocol proto_tcpv4 = {
66 .name = "tcpv4",
67 .sock_domain = AF_INET,
68 .sock_type = SOCK_STREAM,
69 .sock_prot = IPPROTO_TCP,
70 .sock_family = AF_INET,
71 .sock_addrlen = sizeof(struct sockaddr_in),
72 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020073 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020074 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020075 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010076 .bind_all = tcp_bind_listeners,
77 .unbind_all = unbind_all_listeners,
78 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020079 .get_src = tcp_get_src,
80 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020081 .drain = tcp_drain,
Willy Tarreaue6b98942007-10-29 01:09:36 +010082 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
83 .nb_listeners = 0,
84};
85
86/* Note: must not be declared <const> as its list will be overwritten */
87static struct protocol proto_tcpv6 = {
88 .name = "tcpv6",
89 .sock_domain = AF_INET6,
90 .sock_type = SOCK_STREAM,
91 .sock_prot = IPPROTO_TCP,
92 .sock_family = AF_INET6,
93 .sock_addrlen = sizeof(struct sockaddr_in6),
94 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020095 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020096 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020097 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010098 .bind_all = tcp_bind_listeners,
99 .unbind_all = unbind_all_listeners,
100 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200101 .get_src = tcp_get_src,
102 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200103 .drain = tcp_drain,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100104 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
105 .nb_listeners = 0,
106};
107
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100108
David du Colombier6f5ccb12011-03-10 22:26:24 +0100109/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100110 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
111 * - 0 : ignore remote address (may even be a NULL pointer)
112 * - 1 : use provided address
113 * - 2 : use provided port
114 * - 3 : use both
115 *
116 * The function supports multiple foreign binding methods :
117 * - linux_tproxy: we directly bind to the foreign address
118 * - cttproxy: we bind to a local address then nat.
119 * The second one can be used as a fallback for the first one.
120 * This function returns 0 when everything's OK, 1 if it could not bind, to the
121 * local address, 2 if it could not bind to the foreign address.
122 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100123int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100124{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100125 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100126 int foreign_ok = 0;
127 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100128 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200129 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200130
David du Colombier65c17962012-07-13 14:34:59 +0200131 switch (local->ss_family) {
132 case AF_INET:
133 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200134 /* This deserves some explanation. Some platforms will support
135 * multiple combinations of certain methods, so we try the
136 * supported ones until one succeeds.
137 */
138 if (0
139#if defined(IP_TRANSPARENT)
140 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
141#endif
142#if defined(IP_FREEBIND)
143 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
144#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200145#if defined(IP_BINDANY)
146 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
147#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200148#if defined(SO_BINDANY)
149 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
150#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200151 )
David du Colombier65c17962012-07-13 14:34:59 +0200152 foreign_ok = 1;
153 else
154 ip_transp_working = 0;
155 }
156 break;
157 case AF_INET6:
158 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200159 if (0
160#if defined(IPV6_TRANSPARENT)
161 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
162#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200163#if defined(IPV6_BINDANY)
164 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
165#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200166#if defined(SO_BINDANY)
167 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
168#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200169 )
David du Colombier65c17962012-07-13 14:34:59 +0200170 foreign_ok = 1;
171 else
172 ip6_transp_working = 0;
173 }
174 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100175 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200176
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100177 if (flags) {
178 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200179 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100180 switch (remote->ss_family) {
181 case AF_INET:
182 if (flags & 1)
183 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
184 if (flags & 2)
185 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
186 break;
187 case AF_INET6:
188 if (flags & 1)
189 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
190 if (flags & 2)
191 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
192 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100193 default:
194 /* we don't want to try to bind to an unknown address family */
195 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100196 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100197 }
198
Simon Hormande072bd2011-06-24 15:11:37 +0900199 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100200 if (foreign_ok) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200201 if (is_addr(&bind_addr)) {
202 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
203 if (ret < 0)
204 return 2;
205 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100206 }
207 else {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200208 if (is_addr(local)) {
209 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
210 if (ret < 0)
211 return 1;
212 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100213 }
214
215 if (!flags)
216 return 0;
217
218#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100219 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100220 struct in_tproxy itp1, itp2;
221 memset(&itp1, 0, sizeof(itp1));
222
223 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100224 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
225 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100226
227 /* set connect flag on socket */
228 itp2.op = TPROXY_FLAGS;
229 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
230
231 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
232 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
233 foreign_ok = 1;
234 }
235 }
236#endif
237 if (!foreign_ok)
238 /* we could not bind to a foreign address */
239 return 2;
240
241 return 0;
242}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100243
Willy Tarreau9650f372009-08-16 14:02:45 +0200244
245/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200246 * This function initiates a TCP connection establishment to the target assigned
247 * to connection <conn> using (si->{target,addr.to}). A source address may be
248 * pointed to by conn->addr.from in case of transparent proxying. Normal source
249 * bind addresses are still determined locally (due to the possible need of a
250 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100251 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100252 * supported. The <data> parameter is a boolean indicating whether there are data
253 * waiting for being sent or not, in order to adjust data write polling and on
254 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
255 * allows the caller to force using a delayed ACK when establishing the connection :
256 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
257 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
258 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200259 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200260 * It can return one of :
261 * - SN_ERR_NONE if everything's OK
262 * - SN_ERR_SRVTO if there are no more servers
263 * - SN_ERR_SRVCL if the connection was refused by the server
264 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
265 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
266 * - SN_ERR_INTERNAL for any other purely internal errors
267 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100268 *
269 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
270 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200271 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100272
Willy Tarreauf0837b22012-11-24 10:24:27 +0100273int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200274{
275 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100276 struct server *srv;
277 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100278 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100279
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100280 switch (obj_type(conn->target)) {
281 case OBJ_TYPE_PROXY:
282 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100283 srv = NULL;
284 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100285 case OBJ_TYPE_SERVER:
286 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100287 be = srv->proxy;
288 break;
289 default:
290 return SN_ERR_INTERNAL;
291 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200292
Willy Tarreau986a9d22012-08-30 21:11:38 +0200293 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200294 qfprintf(stderr, "Cannot get a server socket.\n");
295
296 if (errno == ENFILE)
297 send_log(be, LOG_EMERG,
298 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
299 be->id, maxfd);
300 else if (errno == EMFILE)
301 send_log(be, LOG_EMERG,
302 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
303 be->id, maxfd);
304 else if (errno == ENOBUFS || errno == ENOMEM)
305 send_log(be, LOG_EMERG,
306 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
307 be->id, maxfd);
308 /* this is a resource error */
309 return SN_ERR_RESOURCE;
310 }
311
312 if (fd >= global.maxsock) {
313 /* do not log anything there, it's a normal condition when this option
314 * is used to serialize connections to a server !
315 */
316 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
317 close(fd);
318 return SN_ERR_PRXCOND; /* it is a configuration limit */
319 }
320
321 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900322 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200323 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
324 close(fd);
325 return SN_ERR_INTERNAL;
326 }
327
328 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900329 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200330
Willy Tarreau9650f372009-08-16 14:02:45 +0200331 /* allow specific binding :
332 * - server-specific at first
333 * - proxy-specific next
334 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100335 if (srv && srv->conn_src.opts & CO_SRC_BIND)
336 src = &srv->conn_src;
337 else if (be->conn_src.opts & CO_SRC_BIND)
338 src = &be->conn_src;
339 else
340 src = NULL;
341
342 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200343 int ret, flags = 0;
344
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200345 if (is_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100346 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100347 case CO_SRC_TPROXY_ADDR:
348 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200349 flags = 3;
350 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100351 case CO_SRC_TPROXY_CIP:
352 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200353 flags = 1;
354 break;
355 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200357
Willy Tarreau9650f372009-08-16 14:02:45 +0200358#ifdef SO_BINDTODEVICE
359 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100360 if (src->iface_name)
361 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200362#endif
363
Willy Tarreaua4380b42012-12-08 22:49:11 +0100364 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200365 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100366 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200367
368 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100369 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200370
371 do {
372 /* note: in case of retry, we may have to release a previously
373 * allocated port, hence this loop's construct.
374 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200375 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
376 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200377
378 if (!attempts)
379 break;
380 attempts--;
381
Willy Tarreaua4380b42012-12-08 22:49:11 +0100382 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200383 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200384 break;
385
Willy Tarreaua4380b42012-12-08 22:49:11 +0100386 fdinfo[fd].port_range = src->sport_range;
387 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200388
Willy Tarreaua4380b42012-12-08 22:49:11 +0100389 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200390 } while (ret != 0); /* binding NOK */
391 }
392 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100393 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200394 }
395
Willy Tarreaua4380b42012-12-08 22:49:11 +0100396 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200397 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
398 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200399 close(fd);
400
Willy Tarreau9650f372009-08-16 14:02:45 +0200401 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100402 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200403 be->id);
404 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100405 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200406 be->id);
407 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100408 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200409 be->id);
410 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100411 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200412 be->id);
413 }
414 return SN_ERR_RESOURCE;
415 }
416 }
417
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400418#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200419 /* disabling tcp quick ack now allows the first request to leave the
420 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100421 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200422 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100423 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900424 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200425#endif
426
Willy Tarreaue803de22010-01-21 17:43:04 +0100427 if (global.tune.server_sndbuf)
428 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
429
430 if (global.tune.server_rcvbuf)
431 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
432
Willy Tarreau986a9d22012-08-30 21:11:38 +0200433 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200434 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
435
Willy Tarreaub1719512012-12-08 23:03:28 +0100436 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200437 char *msg;
Willy Tarreaub1719512012-12-08 23:03:28 +0100438 if (errno == EAGAIN || errno == EADDRNOTAVAIL)
Willy Tarreau9650f372009-08-16 14:02:45 +0200439 msg = "no free ports";
440 else
441 msg = "local address already in use";
442
Willy Tarreaub1719512012-12-08 23:03:28 +0100443 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200444 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
445 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200446 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100447 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9650f372009-08-16 14:02:45 +0200448 return SN_ERR_RESOURCE;
449 } else if (errno == ETIMEDOUT) {
450 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200451 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
452 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200453 close(fd);
454 return SN_ERR_SRVTO;
455 } else {
456 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
457 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200458 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
459 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200460 close(fd);
461 return SN_ERR_SRVCL;
462 }
463 }
464
Willy Tarreau986a9d22012-08-30 21:11:38 +0200465 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200466 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100467 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200468
Willy Tarreaud2274c62012-07-06 14:29:45 +0200469 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200470 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200471 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200472
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200473 if (conn_xprt_init(conn) < 0) {
Willy Tarreau184636e2012-09-06 14:04:41 +0200474 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200475 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200476 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200477
Willy Tarreau14f8e862012-08-30 22:23:13 +0200478 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200479 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200480
Willy Tarreau9650f372009-08-16 14:02:45 +0200481 return SN_ERR_NONE; /* connection is OK */
482}
483
484
Willy Tarreau59b94792012-05-11 16:16:40 +0200485/*
486 * Retrieves the source address for the socket <fd>, with <dir> indicating
487 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
488 * success, -1 in case of error. The socket's source address is stored in
489 * <sa> for <salen> bytes.
490 */
491int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
492{
493 if (dir)
494 return getsockname(fd, sa, &salen);
495 else
496 return getpeername(fd, sa, &salen);
497}
498
499
500/*
501 * Retrieves the original destination address for the socket <fd>, with <dir>
502 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
503 * listener, if the original destination address was translated, the original
504 * address is retrieved. It returns 0 in case of success, -1 in case of error.
505 * The socket's source address is stored in <sa> for <salen> bytes.
506 */
507int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
508{
509 if (dir)
510 return getpeername(fd, sa, &salen);
511#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
512 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
513 return 0;
514#endif
515 else
516 return getsockname(fd, sa, &salen);
517}
518
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200519/* Tries to drain any pending incoming data from the socket to reach the
520 * receive shutdown. Returns non-zero if the shutdown was found, otherwise
521 * zero. This is useful to decide whether we can close a connection cleanly
522 * are we must kill it hard.
523 */
524int tcp_drain(int fd)
525{
526 int turns = 2;
527 int len;
528
529 while (turns) {
530#ifdef MSG_TRUNC_CLEARS_INPUT
531 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
532 if (len == -1 && errno == EFAULT)
533#endif
534 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
535
536 if (len == 0) /* cool, shutdown received */
537 return 1;
538
539 if (len < 0) {
540 if (errno == EAGAIN) /* connection not closed yet */
541 return 0;
542 if (errno == EINTR) /* oops, try again */
543 continue;
544 /* other errors indicate a dead connection, fine. */
545 return 1;
546 }
547 /* OK we read some data, let's try again once */
548 turns--;
549 }
550 /* some data are still present, give up */
551 return 0;
552}
553
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200554/* This is the callback which is set when a connection establishment is pending
555 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200556 * once the connection is established. It updates the FD polling status. It
557 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
558 * it returns non-zero and removes itself from the connection's flags (the bit is
559 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200560 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200561int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200562{
Willy Tarreau239d7182012-07-23 18:53:03 +0200563 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200564
Willy Tarreau80184712012-07-06 14:54:49 +0200565 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200566 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200567
Willy Tarreau80184712012-07-06 14:54:49 +0200568 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200569 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200570
Willy Tarreau2da156f2012-07-23 15:07:23 +0200571 /* stop here if we reached the end of data */
572 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
573 goto out_error;
574
Willy Tarreau2c6be842012-07-06 17:12:34 +0200575 /* We have no data to send to check the connection, and
576 * getsockopt() will not inform us whether the connection
577 * is still pending. So we'll reuse connect() to check the
578 * state of the socket. This has the advantage of giving us
579 * the following info :
580 * - error
581 * - connecting (EALREADY, EINPROGRESS)
582 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200583 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200584 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200585 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100586 __conn_sock_stop_recv(conn);
587 __conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200588 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200589 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200590
Willy Tarreau2c6be842012-07-06 17:12:34 +0200591 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200592 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200593
Willy Tarreau2c6be842012-07-06 17:12:34 +0200594 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200595 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200596
Willy Tarreau076be252012-07-06 16:02:29 +0200597 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200598 * forward the event to the transport layer which will notify the
599 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200600 */
Willy Tarreau80184712012-07-06 14:54:49 +0200601 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200602 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200603
604 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200605 /* Write error on the file descriptor. Report it to the connection
606 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200607 */
608
Willy Tarreau80184712012-07-06 14:54:49 +0200609 conn->flags |= CO_FL_ERROR;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100610 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200611 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200612}
613
Willy Tarreau59b94792012-05-11 16:16:40 +0200614
Willy Tarreaue6b98942007-10-29 01:09:36 +0100615/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100616 * an error message in <errmsg> if the message is at most <errlen> bytes long
617 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
618 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100619 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
620 * was alright and that no message was returned. ERR_RETRYABLE means that an
621 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700622 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100623 * the meaning of the error, but just indicate that a message is present which
624 * should be displayed with the respective level. Last, ERR_ABORT indicates
625 * that it's pointless to try to start other listeners. No error message is
626 * returned if errlen is NULL.
627 */
628int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
629{
630 __label__ tcp_return, tcp_close_return;
631 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100632 int ext, ready;
633 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100634 const char *msg = NULL;
635
636 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100637 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100638 *errmsg = 0;
639
640 if (listener->state != LI_ASSIGNED)
641 return ERR_NONE; /* already bound */
642
643 err = ERR_NONE;
644
Willy Tarreau40aa0702013-03-10 23:51:38 +0100645 /* if the listener already has an fd assigned, then we were offered the
646 * fd by an external process (most likely the parent), and we don't want
647 * to create a new socket. However we still want to set a few flags on
648 * the socket.
649 */
650 fd = listener->fd;
651 ext = (fd >= 0);
652
653 if (!ext && (fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100654 err |= ERR_RETRYABLE | ERR_ALERT;
655 msg = "cannot create listening socket";
656 goto tcp_return;
657 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100658
Willy Tarreaue6b98942007-10-29 01:09:36 +0100659 if (fd >= global.maxsock) {
660 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
661 msg = "not enough free sockets (raise '-n' parameter)";
662 goto tcp_close_return;
663 }
664
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200665 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100666 err |= ERR_FATAL | ERR_ALERT;
667 msg = "cannot make socket non-blocking";
668 goto tcp_close_return;
669 }
670
Willy Tarreau40aa0702013-03-10 23:51:38 +0100671 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100672 /* not fatal but should be reported */
673 msg = "cannot do so_reuseaddr";
674 err |= ERR_ALERT;
675 }
676
677 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900678 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100679
Willy Tarreaue6b98942007-10-29 01:09:36 +0100680#ifdef SO_REUSEPORT
681 /* OpenBSD supports this. As it's present in old libc versions of Linux,
682 * it might return an error that we will silently ignore.
683 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100684 if (!ext)
685 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100686#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200687
Willy Tarreau40aa0702013-03-10 23:51:38 +0100688 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200689 switch (listener->addr.ss_family) {
690 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200691 if (1
692#if defined(IP_TRANSPARENT)
693 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
694#endif
695#if defined(IP_FREEBIND)
696 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
697#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200698#if defined(IP_BINDANY)
699 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
700#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200701#if defined(SO_BINDANY)
702 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
703#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200704 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200705 msg = "cannot make listening socket transparent";
706 err |= ERR_ALERT;
707 }
708 break;
709 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200710 if (1
711#if defined(IPV6_TRANSPARENT)
712 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
713#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200714#if defined(IPV6_BINDANY)
715 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
716#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200717#if defined(SO_BINDANY)
718 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
719#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200720 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200721 msg = "cannot make listening socket transparent";
722 err |= ERR_ALERT;
723 }
724 break;
725 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100726 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200727
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100728#ifdef SO_BINDTODEVICE
729 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100730 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100731 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100732 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100733 msg = "cannot bind listener to device";
734 err |= ERR_WARN;
735 }
736 }
737#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400738#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100739 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400740 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200741 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
742 msg = "cannot set MSS";
743 err |= ERR_WARN;
744 }
745 }
746#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200747#if defined(TCP_DEFER_ACCEPT)
748 if (listener->options & LI_O_DEF_ACCEPT) {
749 /* defer accept by up to one second */
750 int accept_delay = 1;
751 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
752 msg = "cannot enable DEFER_ACCEPT";
753 err |= ERR_WARN;
754 }
755 }
756#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200757#if defined(TCP_FASTOPEN)
758 if (listener->options & LI_O_TCP_FO) {
759 /* TFO needs a queue length, let's use the configured backlog */
760 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
761 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
762 msg = "cannot enable TCP_FASTOPEN";
763 err |= ERR_WARN;
764 }
765 }
766#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100767#if defined(IPV6_V6ONLY)
768 if (listener->options & LI_O_V6ONLY)
769 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100770 else if (listener->options & LI_O_V4V6)
771 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100772#endif
773
Willy Tarreau40aa0702013-03-10 23:51:38 +0100774 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100775 err |= ERR_RETRYABLE | ERR_ALERT;
776 msg = "cannot bind socket";
777 goto tcp_close_return;
778 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100779
Willy Tarreau40aa0702013-03-10 23:51:38 +0100780 ready = 0;
781 ready_len = sizeof(ready);
782 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
783 ready = 0;
784
785 if (!(ext && ready) && /* only listen if not already done by external process */
786 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100787 err |= ERR_RETRYABLE | ERR_ALERT;
788 msg = "cannot listen to socket";
789 goto tcp_close_return;
790 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100791
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400792#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200793 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900794 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200795#endif
796
Willy Tarreaue6b98942007-10-29 01:09:36 +0100797 /* the socket is ready */
798 listener->fd = fd;
799 listener->state = LI_LISTEN;
800
Willy Tarreaueabf3132008-08-29 23:36:51 +0200801 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200802 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200803 fd_insert(fd);
804
Willy Tarreaue6b98942007-10-29 01:09:36 +0100805 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100806 if (msg && errlen) {
807 char pn[INET6_ADDRSTRLEN];
808
Willy Tarreau631f01c2011-09-05 00:36:48 +0200809 addr_to_str(&listener->addr, pn, sizeof(pn));
810 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100811 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100812 return err;
813
814 tcp_close_return:
815 close(fd);
816 goto tcp_return;
817}
818
819/* This function creates all TCP sockets bound to the protocol entry <proto>.
820 * It is intended to be used as the protocol's bind_all() function.
821 * The sockets will be registered but not added to any fd_set, in order not to
822 * loose them across the fork(). A call to enable_all_listeners() is needed
823 * to complete initialization. The return value is composed from ERR_*.
824 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200825static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100826{
827 struct listener *listener;
828 int err = ERR_NONE;
829
830 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200831 err |= tcp_bind_listener(listener, errmsg, errlen);
832 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100833 break;
834 }
835
836 return err;
837}
838
839/* Add listener to the list of tcpv4 listeners. The listener's state
840 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
841 * listeners is updated. This is the function to use to add a new listener.
842 */
843void tcpv4_add_listener(struct listener *listener)
844{
845 if (listener->state != LI_INIT)
846 return;
847 listener->state = LI_ASSIGNED;
848 listener->proto = &proto_tcpv4;
849 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
850 proto_tcpv4.nb_listeners++;
851}
852
853/* Add listener to the list of tcpv4 listeners. The listener's state
854 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
855 * listeners is updated. This is the function to use to add a new listener.
856 */
857void tcpv6_add_listener(struct listener *listener)
858{
859 if (listener->state != LI_INIT)
860 return;
861 listener->state = LI_ASSIGNED;
862 listener->proto = &proto_tcpv6;
863 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
864 proto_tcpv6.nb_listeners++;
865}
866
Willy Tarreauedcf6682008-11-30 23:15:34 +0100867/* This function performs the TCP request analysis on the current request. It
868 * returns 1 if the processing can continue on next analysers, or zero if it
869 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200870 * request. It relies on buffers flags, and updates s->req->analysers. The
871 * function may be called for frontend rules and backend rules. It only relies
872 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100873 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200874int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100875{
876 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200877 struct stksess *ts;
878 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100879 int partial;
880
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100881 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 +0100882 now_ms, __FUNCTION__,
883 s,
884 req,
885 req->rex, req->wex,
886 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200887 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100888 req->analysers);
889
Willy Tarreauedcf6682008-11-30 23:15:34 +0100890 /* We don't know whether we have enough data, so must proceed
891 * this way :
892 * - iterate through all rules in their declaration order
893 * - if one rule returns MISS, it means the inspect delay is
894 * not over yet, then return immediately, otherwise consider
895 * it as a non-match.
896 * - if one rule returns OK, then return OK
897 * - if one rule returns KO, then return KO
898 */
899
Willy Tarreau9b28e032012-10-12 23:49:43 +0200900 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200901 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200902 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100903 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200904 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100905
Willy Tarreaufb356202010-08-03 14:02:05 +0200906 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100907 int ret = ACL_PAT_PASS;
908
909 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200910 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100911 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200912 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100913 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200914 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
915 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100916 return 0;
917 }
918
919 ret = acl_pass(ret);
920 if (rule->cond->pol == ACL_COND_UNLESS)
921 ret = !ret;
922 }
923
924 if (ret) {
925 /* we have a matching rule. */
926 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200927 channel_abort(req);
928 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100929 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200930
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100931 s->be->be_counters.denied_req++;
932 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200933 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200934 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200935
Willy Tarreauedcf6682008-11-30 23:15:34 +0100936 if (!(s->flags & SN_ERR_MASK))
937 s->flags |= SN_ERR_PRXCOND;
938 if (!(s->flags & SN_FINST_MASK))
939 s->flags |= SN_FINST_R;
940 return 0;
941 }
Willy Tarreaub4c84932013-07-23 19:15:30 +0200942 else if ((rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) &&
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200943 !s->stkctr[tcp_trk_idx(rule->action)].entry) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100944 /* Note: only the first valid tracking parameter of each
945 * applies.
946 */
947 struct stktable_key *key;
948
949 t = rule->act_prm.trk_ctr.table.t;
950 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
951
952 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200953 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
954 if (s->fe != s->be)
Willy Tarreaube4a3ef2013-06-17 15:04:07 +0200955 s->flags |= SN_BE_TRACK_SC0 << tcp_trk_idx(rule->action);
Willy Tarreaud1f96522010-08-03 19:34:32 +0200956 }
957 }
958 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100959 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200960 break;
961 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100962 }
963 }
964
965 /* if we get there, it means we have no rule which matches, or
966 * we have an explicit accept, so we apply the default accept.
967 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200968 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100969 req->analyse_exp = TICK_ETERNITY;
970 return 1;
971}
972
Emeric Brun97679e72010-09-23 17:56:44 +0200973/* This function performs the TCP response analysis on the current response. It
974 * returns 1 if the processing can continue on next analysers, or zero if it
975 * needs more data, encounters an error, or wants to immediately abort the
976 * response. It relies on buffers flags, and updates s->rep->analysers. The
977 * function may be called for backend rules.
978 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200979int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200980{
981 struct tcp_rule *rule;
982 int partial;
983
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100984 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 +0200985 now_ms, __FUNCTION__,
986 s,
987 rep,
988 rep->rex, rep->wex,
989 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200990 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200991 rep->analysers);
992
993 /* We don't know whether we have enough data, so must proceed
994 * this way :
995 * - iterate through all rules in their declaration order
996 * - if one rule returns MISS, it means the inspect delay is
997 * not over yet, then return immediately, otherwise consider
998 * it as a non-match.
999 * - if one rule returns OK, then return OK
1000 * - if one rule returns KO, then return KO
1001 */
1002
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001003 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001004 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001005 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001006 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001007
1008 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
1009 int ret = ACL_PAT_PASS;
1010
1011 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001012 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +02001013 if (ret == ACL_PAT_MISS) {
1014 /* just set the analyser timeout once at the beginning of the response */
1015 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
1016 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
1017 return 0;
1018 }
1019
1020 ret = acl_pass(ret);
1021 if (rule->cond->pol == ACL_COND_UNLESS)
1022 ret = !ret;
1023 }
1024
1025 if (ret) {
1026 /* we have a matching rule. */
1027 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001028 channel_abort(rep);
1029 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001030 rep->analysers = 0;
1031
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001032 s->be->be_counters.denied_resp++;
1033 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001034 if (s->listener->counters)
1035 s->listener->counters->denied_resp++;
1036
1037 if (!(s->flags & SN_ERR_MASK))
1038 s->flags |= SN_ERR_PRXCOND;
1039 if (!(s->flags & SN_FINST_MASK))
1040 s->flags |= SN_FINST_D;
1041 return 0;
1042 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001043 else if (rule->action == TCP_ACT_CLOSE) {
1044 rep->prod->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1045 si_shutr(rep->prod);
1046 si_shutw(rep->prod);
1047 break;
1048 }
Emeric Brun97679e72010-09-23 17:56:44 +02001049 else {
1050 /* otherwise accept */
1051 break;
1052 }
1053 }
1054 }
1055
1056 /* if we get there, it means we have no rule which matches, or
1057 * we have an explicit accept, so we apply the default accept.
1058 */
1059 rep->analysers &= ~an_bit;
1060 rep->analyse_exp = TICK_ETERNITY;
1061 return 1;
1062}
1063
1064
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001065/* This function performs the TCP layer4 analysis on the current request. It
1066 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1067 * matches or if no more rule matches. It can only use rules which don't need
1068 * any data.
1069 */
1070int tcp_exec_req_rules(struct session *s)
1071{
1072 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001073 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001074 struct stktable *t = NULL;
1075 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001076 int ret;
1077
1078 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1079 ret = ACL_PAT_PASS;
1080
1081 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001082 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001083 ret = acl_pass(ret);
1084 if (rule->cond->pol == ACL_COND_UNLESS)
1085 ret = !ret;
1086 }
1087
1088 if (ret) {
1089 /* we have a matching rule. */
1090 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001091 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001092 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001093 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001094
1095 if (!(s->flags & SN_ERR_MASK))
1096 s->flags |= SN_ERR_PRXCOND;
1097 if (!(s->flags & SN_FINST_MASK))
1098 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001099 result = 0;
1100 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001101 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001102 else if ((rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) &&
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001103 !s->stkctr[tcp_trk_idx(rule->action)].entry) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001104 /* Note: only the first valid tracking parameter of each
1105 * applies.
1106 */
1107 struct stktable_key *key;
1108
1109 t = rule->act_prm.trk_ctr.table.t;
1110 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
1111
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001112 if (key && (ts = stktable_get_entry(t, key)))
1113 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001114 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001115 else if (rule->action == TCP_ACT_EXPECT_PX) {
1116 s->si[0].conn->flags |= CO_FL_ACCEPT_PROXY;
1117 conn_sock_want_recv(s->si[0].conn);
1118 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001119 else {
1120 /* otherwise it's an accept */
1121 break;
1122 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001123 }
1124 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001125 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001126}
1127
Emeric Brun97679e72010-09-23 17:56:44 +02001128/* Parse a tcp-response rule. Return a negative value in case of failure */
1129static int tcp_parse_response_rule(char **args, int arg, int section_type,
1130 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau80aca902013-01-07 15:42:20 +01001131 struct tcp_rule *rule, char **err,
1132 unsigned int where)
Emeric Brun97679e72010-09-23 17:56:44 +02001133{
1134 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001135 memprintf(err, "%s %s is only allowed in 'backend' sections",
1136 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001137 return -1;
1138 }
1139
1140 if (strcmp(args[arg], "accept") == 0) {
1141 arg++;
1142 rule->action = TCP_ACT_ACCEPT;
1143 }
1144 else if (strcmp(args[arg], "reject") == 0) {
1145 arg++;
1146 rule->action = TCP_ACT_REJECT;
1147 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001148 else if (strcmp(args[arg], "close") == 0) {
1149 arg++;
1150 rule->action = TCP_ACT_CLOSE;
1151 }
Emeric Brun97679e72010-09-23 17:56:44 +02001152 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001153 memprintf(err,
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001154 "'%s %s' expects 'accept', 'close' or 'reject' in %s '%s' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001155 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001156 return -1;
1157 }
1158
1159 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001160 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1161 memprintf(err,
1162 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1163 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001164 return -1;
1165 }
1166 }
1167 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001168 memprintf(err,
1169 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001170 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1171 return -1;
1172 }
1173 return 0;
1174}
1175
1176
1177
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001178/* Parse a tcp-request rule. Return a negative value in case of failure */
1179static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001180 struct proxy *curpx, struct proxy *defpx,
1181 struct tcp_rule *rule, char **err,
1182 unsigned int where)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001183{
1184 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001185 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1186 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001187 return -1;
1188 }
1189
1190 if (!strcmp(args[arg], "accept")) {
1191 arg++;
1192 rule->action = TCP_ACT_ACCEPT;
1193 }
1194 else if (!strcmp(args[arg], "reject")) {
1195 arg++;
1196 rule->action = TCP_ACT_REJECT;
1197 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001198 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1199 args[arg][9] == '\0' && args[arg][8] >= '0' &&
1200 args[arg][8] <= '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001201 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001202 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001203
1204 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001205
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001206 curpx->conf.args.ctx = ARGC_TRK;
1207 expr = sample_parse_expr(args, &arg, trash.str, trash.size, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001208 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001209 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001210 "'%s %s %s' : %s",
1211 args[0], args[1], args[kw], trash.str);
1212 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001213 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001214
Willy Tarreau80aca902013-01-07 15:42:20 +01001215 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001216 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001217 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001218 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001219 free(expr);
1220 return -1;
1221 }
1222
1223 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001224 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001225
1226 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001227 arg++;
1228 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001229 memprintf(err,
1230 "'%s %s %s' : missing table name",
1231 args[0], args[1], args[kw]);
1232 free(expr);
1233 return -1;
1234 }
1235 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001236 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001237 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001238 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001239 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001240 rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001241 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001242 else if (strcmp(args[arg], "expect-proxy") == 0) {
1243 if (strcmp(args[arg+1], "layer4") != 0) {
1244 memprintf(err,
1245 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1246 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1247 return -1;
1248 }
1249
1250 if (!(where & SMP_VAL_FE_CON_ACC)) {
1251 memprintf(err,
1252 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1253 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1254 return -1;
1255 }
1256
1257 arg += 2;
1258 rule->action = TCP_ACT_EXPECT_PX;
1259 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001260 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001261 memprintf(err,
Willy Tarreaub4c84932013-07-23 19:15:30 +02001262 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d' "
1263 " in %s '%s' (got '%s')",
1264 args[0], args[1], MAX_SESS_STKCTR, proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001265 return -1;
1266 }
1267
1268 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001269 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1270 memprintf(err,
1271 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1272 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001273 return -1;
1274 }
1275 }
1276 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001277 memprintf(err,
1278 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001279 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1280 return -1;
1281 }
1282 return 0;
1283}
1284
Emeric Brun97679e72010-09-23 17:56:44 +02001285/* This function should be called to parse a line starting with the "tcp-response"
1286 * keyword.
1287 */
1288static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001289 struct proxy *defpx, const char *file, int line,
1290 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001291{
1292 const char *ptr = NULL;
1293 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001294 int warn = 0;
1295 int arg;
1296 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001297 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001298 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001299 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001300
1301 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001302 memprintf(err, "missing argument for '%s' in %s '%s'",
1303 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001304 return -1;
1305 }
1306
1307 if (strcmp(args[1], "inspect-delay") == 0) {
1308 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001309 memprintf(err, "%s %s is only allowed in 'backend' sections",
1310 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001311 return -1;
1312 }
1313
1314 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001315 memprintf(err,
1316 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1317 args[0], args[1], proxy_type_str(curpx), curpx->id);
1318 if (ptr)
1319 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001320 return -1;
1321 }
1322
1323 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001324 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1325 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001326 return 1;
1327 }
1328 curpx->tcp_rep.inspect_delay = val;
1329 return 0;
1330 }
1331
Simon Hormande072bd2011-06-24 15:11:37 +09001332 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001333 LIST_INIT(&rule->list);
1334 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001335 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001336
1337 if (strcmp(args[1], "content") == 0) {
1338 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001339
1340 if (curpx->cap & PR_CAP_FE)
1341 where |= SMP_VAL_FE_RES_CNT;
1342 if (curpx->cap & PR_CAP_BE)
1343 where |= SMP_VAL_BE_RES_CNT;
1344
1345 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001346 goto error;
1347
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001348 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1349 if (acl) {
1350 if (acl->name && *acl->name)
1351 memprintf(err,
1352 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1353 acl->name, args[0], args[1], sample_ckp_names(where));
1354 else
1355 memprintf(err,
1356 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1357 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001358 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001359 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001360
Emeric Brun97679e72010-09-23 17:56:44 +02001361 warn++;
1362 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001363 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1364 if (acl->name && *acl->name)
1365 memprintf(err,
1366 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001367 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001368 else
1369 memprintf(err,
1370 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001371 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001372 warn++;
1373 }
Emeric Brun97679e72010-09-23 17:56:44 +02001374
1375 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1376 }
1377 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001378 memprintf(err,
1379 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1380 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001381 goto error;
1382 }
1383
1384 return warn;
1385 error:
1386 free(rule);
1387 return -1;
1388}
1389
1390
Willy Tarreaub6866442008-07-14 23:54:42 +02001391/* This function should be called to parse a line starting with the "tcp-request"
1392 * keyword.
1393 */
1394static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001395 struct proxy *defpx, const char *file, int line,
1396 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001397{
1398 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001399 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001400 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001401 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001402 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001403 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001404 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001405 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001406
1407 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001408 if (curpx == defpx)
1409 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1410 else
1411 memprintf(err, "missing argument for '%s' in %s '%s'",
1412 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001413 return -1;
1414 }
1415
1416 if (!strcmp(args[1], "inspect-delay")) {
1417 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001418 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1419 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001420 return -1;
1421 }
1422
Willy Tarreaub6866442008-07-14 23:54:42 +02001423 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001424 memprintf(err,
1425 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1426 args[0], args[1], proxy_type_str(curpx), curpx->id);
1427 if (ptr)
1428 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001429 return -1;
1430 }
1431
1432 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001433 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1434 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001435 return 1;
1436 }
1437 curpx->tcp_req.inspect_delay = val;
1438 return 0;
1439 }
1440
Simon Hormande072bd2011-06-24 15:11:37 +09001441 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001442 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001443 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001444 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001445
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001446 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001447 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001448
1449 if (curpx->cap & PR_CAP_FE)
1450 where |= SMP_VAL_FE_REQ_CNT;
1451 if (curpx->cap & PR_CAP_BE)
1452 where |= SMP_VAL_BE_REQ_CNT;
1453
1454 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001455 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001456
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001457 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1458 if (acl) {
1459 if (acl->name && *acl->name)
1460 memprintf(err,
1461 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1462 acl->name, args[0], args[1], sample_ckp_names(where));
1463 else
1464 memprintf(err,
1465 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1466 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001467 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001468 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001469
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001470 warn++;
1471 }
1472 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1473 if (acl->name && *acl->name)
1474 memprintf(err,
1475 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001476 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001477 else
1478 memprintf(err,
1479 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001480 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001481 warn++;
1482 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001483
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001484 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001485 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001486 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001487 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001488
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001489 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001490 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1491 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001492 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001493 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001494
Willy Tarreau80aca902013-01-07 15:42:20 +01001495 where |= SMP_VAL_FE_CON_ACC;
1496
1497 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001498 goto error;
1499
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001500 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1501 if (acl) {
1502 if (acl->name && *acl->name)
1503 memprintf(err,
1504 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1505 acl->name, args[0], args[1], sample_ckp_names(where));
1506 else
1507 memprintf(err,
1508 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1509 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001510 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001511 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001512
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001513 warn++;
1514 }
1515 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1516 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001517 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001518 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001519 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001520 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001521 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001522 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001523 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001524 warn++;
1525 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001526
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001527 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001528 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001529 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001530 if (curpx == defpx)
1531 memprintf(err,
1532 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1533 args[0], args[1]);
1534 else
1535 memprintf(err,
1536 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001537 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001538 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001539 }
1540
Willy Tarreau1a687942010-05-23 22:40:30 +02001541 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001542 error:
1543 free(rule);
1544 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001545}
1546
Willy Tarreau645513a2010-05-24 20:55:15 +02001547
1548/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001549/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001550/************************************************************************/
1551
Willy Tarreau4a129812012-04-25 17:31:42 +02001552/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001553static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001554smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001555 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001556{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001557 switch (l4->si[0].conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001558 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001559 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001560 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001561 break;
1562 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001563 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001564 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001565 break;
1566 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001567 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001568 }
Emeric Brunf769f512010-10-22 17:14:01 +02001569
Willy Tarreau37406352012-04-23 16:16:37 +02001570 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001571 return 1;
1572}
1573
Willy Tarreaua5e37562011-12-16 17:06:15 +01001574/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001575static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001576smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001577 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001578{
Willy Tarreauf853c462012-04-23 18:53:56 +02001579 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001580 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001581 return 0;
1582
Willy Tarreau37406352012-04-23 16:16:37 +02001583 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001584 return 1;
1585}
1586
Willy Tarreau4a129812012-04-25 17:31:42 +02001587/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001588static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001589smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001590 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001591{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001592 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001593
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001594 switch (l4->si[0].conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001595 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001596 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001597 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001598 break;
1599 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001600 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001601 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001602 break;
1603 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001604 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001605 }
Emeric Brunf769f512010-10-22 17:14:01 +02001606
Willy Tarreau37406352012-04-23 16:16:37 +02001607 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001608 return 1;
1609}
1610
Willy Tarreaua5e37562011-12-16 17:06:15 +01001611/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001612static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001613smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001614 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001615{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001616 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001617
Willy Tarreauf853c462012-04-23 18:53:56 +02001618 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001619 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001620 return 0;
1621
Willy Tarreau37406352012-04-23 16:16:37 +02001622 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001623 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001624}
1625
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001626#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001627/* parse the "v4v6" bind keyword */
1628static int bind_parse_v4v6(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_V4V6;
1635 }
1636
1637 return 0;
1638}
1639
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001640/* parse the "v6only" bind keyword */
1641static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1642{
1643 struct listener *l;
1644
1645 list_for_each_entry(l, &conf->listeners, by_bind) {
1646 if (l->addr.ss_family == AF_INET6)
1647 l->options |= LI_O_V6ONLY;
1648 }
1649
1650 return 0;
1651}
1652#endif
1653
Pieter Baauwd551fb52013-05-08 22:49:23 +02001654#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001655/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001656static 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 +02001657{
1658 struct listener *l;
1659
Willy Tarreau4348fad2012-09-20 16:48:07 +02001660 list_for_each_entry(l, &conf->listeners, by_bind) {
1661 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1662 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001663 }
1664
Willy Tarreau44791242012-09-12 23:27:21 +02001665 return 0;
1666}
1667#endif
1668
1669#ifdef TCP_DEFER_ACCEPT
1670/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001671static 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 +02001672{
1673 struct listener *l;
1674
Willy Tarreau4348fad2012-09-20 16:48:07 +02001675 list_for_each_entry(l, &conf->listeners, by_bind) {
1676 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1677 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001678 }
1679
Willy Tarreau44791242012-09-12 23:27:21 +02001680 return 0;
1681}
1682#endif
1683
Willy Tarreau1c862c52012-10-05 16:21:00 +02001684#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001685/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001686static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1687{
1688 struct listener *l;
1689
1690 list_for_each_entry(l, &conf->listeners, by_bind) {
1691 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1692 l->options |= LI_O_TCP_FO;
1693 }
1694
1695 return 0;
1696}
1697#endif
1698
Willy Tarreau44791242012-09-12 23:27:21 +02001699#ifdef TCP_MAXSEG
1700/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001701static 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 +02001702{
1703 struct listener *l;
1704 int mss;
1705
Willy Tarreau44791242012-09-12 23:27:21 +02001706 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001707 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001708 return ERR_ALERT | ERR_FATAL;
1709 }
1710
1711 mss = atoi(args[cur_arg + 1]);
1712 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001713 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001714 return ERR_ALERT | ERR_FATAL;
1715 }
1716
Willy Tarreau4348fad2012-09-20 16:48:07 +02001717 list_for_each_entry(l, &conf->listeners, by_bind) {
1718 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1719 l->maxseg = mss;
1720 }
Willy Tarreau44791242012-09-12 23:27:21 +02001721
1722 return 0;
1723}
1724#endif
1725
1726#ifdef SO_BINDTODEVICE
1727/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001728static 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 +02001729{
1730 struct listener *l;
1731
Willy Tarreau44791242012-09-12 23:27:21 +02001732 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001733 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001734 return ERR_ALERT | ERR_FATAL;
1735 }
1736
Willy Tarreau4348fad2012-09-20 16:48:07 +02001737 list_for_each_entry(l, &conf->listeners, by_bind) {
1738 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1739 l->interface = strdup(args[cur_arg + 1]);
1740 }
Willy Tarreau44791242012-09-12 23:27:21 +02001741
1742 global.last_checks |= LSTCHK_NETADM;
1743 return 0;
1744}
1745#endif
1746
Willy Tarreaudc13c112013-06-21 23:16:39 +02001747static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001748 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001749 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001750 { 0, NULL, NULL },
1751}};
1752
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001753
Willy Tarreau61612d42012-04-19 18:42:05 +02001754/* Note: must not be declared <const> as its list will be overwritten.
1755 * Please take care of keeping this list alphabetically sorted.
1756 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001757static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001758 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02001759}};
1760
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001761
Willy Tarreau4a129812012-04-25 17:31:42 +02001762/* Note: must not be declared <const> as its list will be overwritten.
1763 * Note: fetches that may return multiple types must be declared as the lowest
1764 * common denominator, the type that can be casted into all other ones. For
1765 * instance v4/v6 must be declared v4.
1766 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001767static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001768 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1769 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1770 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1771 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1772 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001773}};
1774
Willy Tarreau44791242012-09-12 23:27:21 +02001775/************************************************************************/
1776/* All supported bind keywords must be declared here. */
1777/************************************************************************/
1778
1779/* Note: must not be declared <const> as its list will be overwritten.
1780 * Please take care of keeping this list alphabetically sorted, doing so helps
1781 * all code contributors.
1782 * Optional keywords are also declared with a NULL ->parse() function so that
1783 * the config parser can report an appropriate error when a known keyword was
1784 * not enabled.
1785 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001786static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001787#ifdef TCP_DEFER_ACCEPT
1788 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1789#endif
1790#ifdef SO_BINDTODEVICE
1791 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1792#endif
1793#ifdef TCP_MAXSEG
1794 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1795#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001796#ifdef TCP_FASTOPEN
1797 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1798#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001799#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001800 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1801#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001802#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001803 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001804 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1805#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001806 /* the versions with the NULL parse function*/
1807 { "defer-accept", NULL, 0 },
1808 { "interface", NULL, 1 },
1809 { "mss", NULL, 1 },
1810 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001811 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001812 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001813 { NULL, NULL, 0 },
1814}};
1815
Willy Tarreaue6b98942007-10-29 01:09:36 +01001816__attribute__((constructor))
1817static void __tcp_protocol_init(void)
1818{
1819 protocol_register(&proto_tcpv4);
1820 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001821 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001822 cfg_register_keywords(&cfg_kws);
1823 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001824 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001825}
1826
1827
1828/*
1829 * Local variables:
1830 * c-indent-level: 8
1831 * c-basic-offset: 8
1832 * End:
1833 */