blob: ab8f343bbc78824c65cf753bcccb542712ebda11 [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040027#include <netinet/tcp.h>
28
Willy Tarreaub6866442008-07-14 23:54:42 +020029#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010036
Willy Tarreaue6b98942007-10-29 01:09:36 +010037#include <types/global.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020038#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039
40#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020041#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020044#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020046#include <proto/log.h>
47#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020048#include <proto/protocol.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010049#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020051#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020052#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/stick_table.h>
Willy Tarreaucc1e04b2013-09-11 23:20:29 +020054#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020055#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010056
Willy Tarreaue8c66af2008-01-13 18:40:14 +010057#ifdef CONFIG_HAP_CTTPROXY
58#include <import/ip_tproxy.h>
59#endif
60
Emeric Bruncf20bf12010-10-22 16:06:11 +020061static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
62static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010063
64/* Note: must not be declared <const> as its list will be overwritten */
65static struct protocol proto_tcpv4 = {
66 .name = "tcpv4",
67 .sock_domain = AF_INET,
68 .sock_type = SOCK_STREAM,
69 .sock_prot = IPPROTO_TCP,
70 .sock_family = AF_INET,
71 .sock_addrlen = sizeof(struct sockaddr_in),
72 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020073 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020074 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020075 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010076 .bind_all = tcp_bind_listeners,
77 .unbind_all = unbind_all_listeners,
78 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020079 .get_src = tcp_get_src,
80 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020081 .drain = tcp_drain,
Willy Tarreaue6b98942007-10-29 01:09:36 +010082 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
83 .nb_listeners = 0,
84};
85
86/* Note: must not be declared <const> as its list will be overwritten */
87static struct protocol proto_tcpv6 = {
88 .name = "tcpv6",
89 .sock_domain = AF_INET6,
90 .sock_type = SOCK_STREAM,
91 .sock_prot = IPPROTO_TCP,
92 .sock_family = AF_INET6,
93 .sock_addrlen = sizeof(struct sockaddr_in6),
94 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020095 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020096 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020097 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010098 .bind_all = tcp_bind_listeners,
99 .unbind_all = unbind_all_listeners,
100 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200101 .get_src = tcp_get_src,
102 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200103 .drain = tcp_drain,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100104 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
105 .nb_listeners = 0,
106};
107
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100108
David du Colombier6f5ccb12011-03-10 22:26:24 +0100109/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100110 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
111 * - 0 : ignore remote address (may even be a NULL pointer)
112 * - 1 : use provided address
113 * - 2 : use provided port
114 * - 3 : use both
115 *
116 * The function supports multiple foreign binding methods :
117 * - linux_tproxy: we directly bind to the foreign address
118 * - cttproxy: we bind to a local address then nat.
119 * The second one can be used as a fallback for the first one.
120 * This function returns 0 when everything's OK, 1 if it could not bind, to the
121 * local address, 2 if it could not bind to the foreign address.
122 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100123int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100124{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100125 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100126 int foreign_ok = 0;
127 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100128 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200129 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200130
David du Colombier65c17962012-07-13 14:34:59 +0200131 switch (local->ss_family) {
132 case AF_INET:
133 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200134 /* This deserves some explanation. Some platforms will support
135 * multiple combinations of certain methods, so we try the
136 * supported ones until one succeeds.
137 */
138 if (0
139#if defined(IP_TRANSPARENT)
140 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
141#endif
142#if defined(IP_FREEBIND)
143 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
144#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200145#if defined(IP_BINDANY)
146 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
147#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200148#if defined(SO_BINDANY)
149 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
150#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200151 )
David du Colombier65c17962012-07-13 14:34:59 +0200152 foreign_ok = 1;
153 else
154 ip_transp_working = 0;
155 }
156 break;
157 case AF_INET6:
158 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200159 if (0
160#if defined(IPV6_TRANSPARENT)
161 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
162#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200163#if defined(IPV6_BINDANY)
164 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
165#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200166#if defined(SO_BINDANY)
167 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
168#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200169 )
David du Colombier65c17962012-07-13 14:34:59 +0200170 foreign_ok = 1;
171 else
172 ip6_transp_working = 0;
173 }
174 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100175 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200176
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100177 if (flags) {
178 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200179 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100180 switch (remote->ss_family) {
181 case AF_INET:
182 if (flags & 1)
183 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
184 if (flags & 2)
185 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
186 break;
187 case AF_INET6:
188 if (flags & 1)
189 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
190 if (flags & 2)
191 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
192 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100193 default:
194 /* we don't want to try to bind to an unknown address family */
195 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100196 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100197 }
198
Simon Hormande072bd2011-06-24 15:11:37 +0900199 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100200 if (foreign_ok) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200201 if (is_addr(&bind_addr)) {
202 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
203 if (ret < 0)
204 return 2;
205 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100206 }
207 else {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200208 if (is_addr(local)) {
209 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
210 if (ret < 0)
211 return 1;
212 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100213 }
214
215 if (!flags)
216 return 0;
217
218#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100219 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100220 struct in_tproxy itp1, itp2;
221 memset(&itp1, 0, sizeof(itp1));
222
223 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100224 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
225 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100226
227 /* set connect flag on socket */
228 itp2.op = TPROXY_FLAGS;
229 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
230
231 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
232 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
233 foreign_ok = 1;
234 }
235 }
236#endif
237 if (!foreign_ok)
238 /* we could not bind to a foreign address */
239 return 2;
240
241 return 0;
242}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100243
Willy Tarreau9650f372009-08-16 14:02:45 +0200244
245/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200246 * This function initiates a TCP connection establishment to the target assigned
247 * to connection <conn> using (si->{target,addr.to}). A source address may be
248 * pointed to by conn->addr.from in case of transparent proxying. Normal source
249 * bind addresses are still determined locally (due to the possible need of a
250 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100251 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100252 * supported. The <data> parameter is a boolean indicating whether there are data
253 * waiting for being sent or not, in order to adjust data write polling and on
254 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
255 * allows the caller to force using a delayed ACK when establishing the connection :
256 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
257 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
258 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200259 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200260 * Note that a pending send_proxy message accounts for data.
261 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200262 * It can return one of :
263 * - SN_ERR_NONE if everything's OK
264 * - SN_ERR_SRVTO if there are no more servers
265 * - SN_ERR_SRVCL if the connection was refused by the server
266 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
267 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
268 * - SN_ERR_INTERNAL for any other purely internal errors
269 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100270 *
271 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
272 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200273 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100274
Willy Tarreauf0837b22012-11-24 10:24:27 +0100275int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200276{
277 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100278 struct server *srv;
279 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100280 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100281
Willy Tarreau9ce70132014-01-24 16:08:19 +0100282 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
283
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100284 switch (obj_type(conn->target)) {
285 case OBJ_TYPE_PROXY:
286 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100287 srv = NULL;
288 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100289 case OBJ_TYPE_SERVER:
290 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100291 be = srv->proxy;
292 break;
293 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100294 conn->flags |= CO_FL_ERROR;
Willy Tarreauac825402011-03-04 22:04:29 +0100295 return SN_ERR_INTERNAL;
296 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200297
Willy Tarreau986a9d22012-08-30 21:11:38 +0200298 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200299 qfprintf(stderr, "Cannot get a server socket.\n");
300
Willy Tarreau9ce70132014-01-24 16:08:19 +0100301 if (errno == ENFILE) {
302 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200303 send_log(be, LOG_EMERG,
304 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
305 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100306 }
307 else if (errno == EMFILE) {
308 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200309 send_log(be, LOG_EMERG,
310 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
311 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100312 }
313 else if (errno == ENOBUFS || errno == ENOMEM) {
314 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200315 send_log(be, LOG_EMERG,
316 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
317 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100318 }
319 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
320 conn->err_code = CO_ER_NOPROTO;
321 }
322 else
323 conn->err_code = CO_ER_SOCK_ERR;
324
Willy Tarreau9650f372009-08-16 14:02:45 +0200325 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100326 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200327 return SN_ERR_RESOURCE;
328 }
329
330 if (fd >= global.maxsock) {
331 /* do not log anything there, it's a normal condition when this option
332 * is used to serialize connections to a server !
333 */
334 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
335 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100336 conn->err_code = CO_ER_CONF_FDLIM;
337 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200338 return SN_ERR_PRXCOND; /* it is a configuration limit */
339 }
340
341 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900342 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200343 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
344 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100345 conn->err_code = CO_ER_SOCK_ERR;
346 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200347 return SN_ERR_INTERNAL;
348 }
349
350 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900351 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200352
Willy Tarreau9650f372009-08-16 14:02:45 +0200353 /* allow specific binding :
354 * - server-specific at first
355 * - proxy-specific next
356 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100357 if (srv && srv->conn_src.opts & CO_SRC_BIND)
358 src = &srv->conn_src;
359 else if (be->conn_src.opts & CO_SRC_BIND)
360 src = &be->conn_src;
361 else
362 src = NULL;
363
364 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200365 int ret, flags = 0;
366
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200367 if (is_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100368 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100369 case CO_SRC_TPROXY_ADDR:
370 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200371 flags = 3;
372 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100373 case CO_SRC_TPROXY_CIP:
374 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200375 flags = 1;
376 break;
377 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200378 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200379
Willy Tarreau9650f372009-08-16 14:02:45 +0200380#ifdef SO_BINDTODEVICE
381 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100382 if (src->iface_name)
383 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200384#endif
385
Willy Tarreaua4380b42012-12-08 22:49:11 +0100386 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200387 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100388 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200389
390 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100391 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200392
393 do {
394 /* note: in case of retry, we may have to release a previously
395 * allocated port, hence this loop's construct.
396 */
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
400 if (!attempts)
401 break;
402 attempts--;
403
Willy Tarreaua4380b42012-12-08 22:49:11 +0100404 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100405 if (!fdinfo[fd].local_port) {
406 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200407 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100408 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200409
Willy Tarreaua4380b42012-12-08 22:49:11 +0100410 fdinfo[fd].port_range = src->sport_range;
411 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200412
Willy Tarreaua4380b42012-12-08 22:49:11 +0100413 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100414 if (ret != 0)
415 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200416 } while (ret != 0); /* binding NOK */
417 }
418 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100419 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100420 if (ret != 0)
421 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200422 }
423
Willy Tarreaua4380b42012-12-08 22:49:11 +0100424 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200425 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
426 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200427 close(fd);
428
Willy Tarreau9650f372009-08-16 14:02:45 +0200429 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100430 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200431 be->id);
432 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100433 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200434 be->id);
435 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100436 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200437 be->id);
438 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100439 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200440 be->id);
441 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100442 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200443 return SN_ERR_RESOURCE;
444 }
445 }
446
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200447 /* if a send_proxy is there, there are data */
448 data |= conn->send_proxy_ofs;
449
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400450#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200451 /* disabling tcp quick ack now allows the first request to leave the
452 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100453 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100455 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900456 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200457#endif
458
Willy Tarreaue803de22010-01-21 17:43:04 +0100459 if (global.tune.server_sndbuf)
460 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
461
462 if (global.tune.server_rcvbuf)
463 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
464
Willy Tarreau986a9d22012-08-30 21:11:38 +0200465 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200466 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
467
Willy Tarreaub1719512012-12-08 23:03:28 +0100468 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100470 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200471 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100472 conn->err_code = CO_ER_FREE_PORTS;
473 }
474 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200475 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100476 conn->err_code = CO_ER_ADDR_INUSE;
477 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200478
Willy Tarreaub1719512012-12-08 23:03:28 +0100479 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200480 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
481 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200482 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100483 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100484 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200485 return SN_ERR_RESOURCE;
486 } else if (errno == ETIMEDOUT) {
487 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200488 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
489 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200490 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100491 conn->err_code = CO_ER_SOCK_ERR;
492 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200493 return SN_ERR_SRVTO;
494 } else {
495 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
496 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200497 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
498 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200499 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100500 conn->err_code = CO_ER_SOCK_ERR;
501 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200502 return SN_ERR_SRVCL;
503 }
504 }
505
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100506 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200507
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200508 /* Prepare to send a few handshakes related to the on-wire protocol. */
509 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200510 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200511
Willy Tarreauf79c8172013-10-21 16:30:56 +0200512 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100513 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200514 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200515
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200516 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200517 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100518 conn->flags |= CO_FL_ERROR;
Willy Tarreau15678ef2012-08-31 13:54:11 +0200519 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200520 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200521
Willy Tarreau14f8e862012-08-30 22:23:13 +0200522 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200523 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200524
Willy Tarreau9650f372009-08-16 14:02:45 +0200525 return SN_ERR_NONE; /* connection is OK */
526}
527
528
Willy Tarreau59b94792012-05-11 16:16:40 +0200529/*
530 * Retrieves the source address for the socket <fd>, with <dir> indicating
531 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
532 * success, -1 in case of error. The socket's source address is stored in
533 * <sa> for <salen> bytes.
534 */
535int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
536{
537 if (dir)
538 return getsockname(fd, sa, &salen);
539 else
540 return getpeername(fd, sa, &salen);
541}
542
543
544/*
545 * Retrieves the original destination address for the socket <fd>, with <dir>
546 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
547 * listener, if the original destination address was translated, the original
548 * address is retrieved. It returns 0 in case of success, -1 in case of error.
549 * The socket's source address is stored in <sa> for <salen> bytes.
550 */
551int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
552{
553 if (dir)
554 return getpeername(fd, sa, &salen);
555#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
556 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
557 return 0;
558#endif
559 else
560 return getsockname(fd, sa, &salen);
561}
562
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200563/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100564 * receive shutdown. Returns positive if the shutdown was found, negative
565 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
566 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200567 */
568int tcp_drain(int fd)
569{
570 int turns = 2;
571 int len;
572
573 while (turns) {
574#ifdef MSG_TRUNC_CLEARS_INPUT
575 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
576 if (len == -1 && errno == EFAULT)
577#endif
578 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
579
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100580 if (len == 0) {
581 /* cool, shutdown received */
582 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200583 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100584 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200585
586 if (len < 0) {
587 if (errno == EAGAIN) /* connection not closed yet */
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100588 return -1;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200589 if (errno == EINTR) /* oops, try again */
590 continue;
591 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100592 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200593 return 1;
594 }
595 /* OK we read some data, let's try again once */
596 turns--;
597 }
598 /* some data are still present, give up */
599 return 0;
600}
601
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200602/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100603 * and we have nothing to send. It updates the FD polling status. It returns 0
604 * if it fails in a fatal way or needs to poll to go further, otherwise it
605 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
606 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
607 * errno. The error checking is done in two passes in order to limit the number
608 * of syscalls in the normal case :
609 * - if POLL_ERR was reported by the poller, we check for a pending error on
610 * the socket before proceeding. If found, it's assigned to errno so that
611 * upper layers can see it.
612 * - otherwise connect() is used to check the connection state again, since
613 * the getsockopt return cannot reliably be used to know if the connection
614 * is still pending or ready. This one may often return an error as well,
615 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200616 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200617int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200618{
Willy Tarreau239d7182012-07-23 18:53:03 +0200619 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100620 socklen_t lskerr;
621 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200622
Willy Tarreau80184712012-07-06 14:54:49 +0200623 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200624 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200625
Willy Tarreauf79c8172013-10-21 16:30:56 +0200626 if (!(conn->flags & CO_FL_CTRL_READY))
627 return 0;
628
Willy Tarreau80184712012-07-06 14:54:49 +0200629 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200630 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200631
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100632 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
633 * without FD_POLL_IN also indicates a hangup without input data meaning
634 * there was no connection.
635 */
636 if (fdtab[fd].ev & FD_POLL_ERR ||
637 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
638 skerr = 0;
639 lskerr = sizeof(skerr);
640 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
641 errno = skerr;
642 if (errno == EAGAIN)
643 errno = 0;
644 if (errno)
645 goto out_error;
646 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200647
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100648 /* Use connect() to check the state of the socket. This has the
649 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200650 * - error
651 * - connecting (EALREADY, EINPROGRESS)
652 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200653 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200654 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200655 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100656 __conn_sock_stop_recv(conn);
657 __conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200658 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200659 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200660
Willy Tarreau2c6be842012-07-06 17:12:34 +0200661 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200662 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200663
Willy Tarreau2c6be842012-07-06 17:12:34 +0200664 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200665 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200666
Willy Tarreau076be252012-07-06 16:02:29 +0200667 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200668 * forward the event to the transport layer which will notify the
669 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200670 */
Willy Tarreau80184712012-07-06 14:54:49 +0200671 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200672 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200673
674 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200675 /* Write error on the file descriptor. Report it to the connection
676 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200677 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100678 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100679 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100680 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200681 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200682}
683
Willy Tarreau59b94792012-05-11 16:16:40 +0200684
Willy Tarreaue6b98942007-10-29 01:09:36 +0100685/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100686 * an error message in <errmsg> if the message is at most <errlen> bytes long
687 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
688 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100689 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
690 * was alright and that no message was returned. ERR_RETRYABLE means that an
691 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700692 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100693 * the meaning of the error, but just indicate that a message is present which
694 * should be displayed with the respective level. Last, ERR_ABORT indicates
695 * that it's pointless to try to start other listeners. No error message is
696 * returned if errlen is NULL.
697 */
698int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
699{
700 __label__ tcp_return, tcp_close_return;
701 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100702 int ext, ready;
703 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100704 const char *msg = NULL;
705
706 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100707 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100708 *errmsg = 0;
709
710 if (listener->state != LI_ASSIGNED)
711 return ERR_NONE; /* already bound */
712
713 err = ERR_NONE;
714
Willy Tarreau40aa0702013-03-10 23:51:38 +0100715 /* if the listener already has an fd assigned, then we were offered the
716 * fd by an external process (most likely the parent), and we don't want
717 * to create a new socket. However we still want to set a few flags on
718 * the socket.
719 */
720 fd = listener->fd;
721 ext = (fd >= 0);
722
723 if (!ext && (fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100724 err |= ERR_RETRYABLE | ERR_ALERT;
725 msg = "cannot create listening socket";
726 goto tcp_return;
727 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100728
Willy Tarreaue6b98942007-10-29 01:09:36 +0100729 if (fd >= global.maxsock) {
730 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
731 msg = "not enough free sockets (raise '-n' parameter)";
732 goto tcp_close_return;
733 }
734
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200735 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100736 err |= ERR_FATAL | ERR_ALERT;
737 msg = "cannot make socket non-blocking";
738 goto tcp_close_return;
739 }
740
Willy Tarreau40aa0702013-03-10 23:51:38 +0100741 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100742 /* not fatal but should be reported */
743 msg = "cannot do so_reuseaddr";
744 err |= ERR_ALERT;
745 }
746
747 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900748 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100749
Willy Tarreaue6b98942007-10-29 01:09:36 +0100750#ifdef SO_REUSEPORT
751 /* OpenBSD supports this. As it's present in old libc versions of Linux,
752 * it might return an error that we will silently ignore.
753 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100754 if (!ext)
755 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100756#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200757
Willy Tarreau40aa0702013-03-10 23:51:38 +0100758 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200759 switch (listener->addr.ss_family) {
760 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200761 if (1
762#if defined(IP_TRANSPARENT)
763 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
764#endif
765#if defined(IP_FREEBIND)
766 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
767#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200768#if defined(IP_BINDANY)
769 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
770#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200771#if defined(SO_BINDANY)
772 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
773#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200774 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200775 msg = "cannot make listening socket transparent";
776 err |= ERR_ALERT;
777 }
778 break;
779 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200780 if (1
781#if defined(IPV6_TRANSPARENT)
782 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
783#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200784#if defined(IPV6_BINDANY)
785 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
786#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200787#if defined(SO_BINDANY)
788 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
789#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200790 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200791 msg = "cannot make listening socket transparent";
792 err |= ERR_ALERT;
793 }
794 break;
795 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100796 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200797
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100798#ifdef SO_BINDTODEVICE
799 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100800 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100801 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100802 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100803 msg = "cannot bind listener to device";
804 err |= ERR_WARN;
805 }
806 }
807#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400808#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100809 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400810 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200811 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
812 msg = "cannot set MSS";
813 err |= ERR_WARN;
814 }
815 }
816#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200817#if defined(TCP_DEFER_ACCEPT)
818 if (listener->options & LI_O_DEF_ACCEPT) {
819 /* defer accept by up to one second */
820 int accept_delay = 1;
821 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
822 msg = "cannot enable DEFER_ACCEPT";
823 err |= ERR_WARN;
824 }
825 }
826#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200827#if defined(TCP_FASTOPEN)
828 if (listener->options & LI_O_TCP_FO) {
829 /* TFO needs a queue length, let's use the configured backlog */
830 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
831 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
832 msg = "cannot enable TCP_FASTOPEN";
833 err |= ERR_WARN;
834 }
835 }
836#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100837#if defined(IPV6_V6ONLY)
838 if (listener->options & LI_O_V6ONLY)
839 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100840 else if (listener->options & LI_O_V4V6)
841 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100842#endif
843
Willy Tarreau40aa0702013-03-10 23:51:38 +0100844 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100845 err |= ERR_RETRYABLE | ERR_ALERT;
846 msg = "cannot bind socket";
847 goto tcp_close_return;
848 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100849
Willy Tarreau40aa0702013-03-10 23:51:38 +0100850 ready = 0;
851 ready_len = sizeof(ready);
852 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
853 ready = 0;
854
855 if (!(ext && ready) && /* only listen if not already done by external process */
856 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100857 err |= ERR_RETRYABLE | ERR_ALERT;
858 msg = "cannot listen to socket";
859 goto tcp_close_return;
860 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100861
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400862#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200863 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900864 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200865#endif
866
Willy Tarreaue6b98942007-10-29 01:09:36 +0100867 /* the socket is ready */
868 listener->fd = fd;
869 listener->state = LI_LISTEN;
870
Willy Tarreaueabf3132008-08-29 23:36:51 +0200871 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200872 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200873 fd_insert(fd);
874
Willy Tarreaue6b98942007-10-29 01:09:36 +0100875 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100876 if (msg && errlen) {
877 char pn[INET6_ADDRSTRLEN];
878
Willy Tarreau631f01c2011-09-05 00:36:48 +0200879 addr_to_str(&listener->addr, pn, sizeof(pn));
880 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100881 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100882 return err;
883
884 tcp_close_return:
885 close(fd);
886 goto tcp_return;
887}
888
889/* This function creates all TCP sockets bound to the protocol entry <proto>.
890 * It is intended to be used as the protocol's bind_all() function.
891 * The sockets will be registered but not added to any fd_set, in order not to
892 * loose them across the fork(). A call to enable_all_listeners() is needed
893 * to complete initialization. The return value is composed from ERR_*.
894 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200895static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100896{
897 struct listener *listener;
898 int err = ERR_NONE;
899
900 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200901 err |= tcp_bind_listener(listener, errmsg, errlen);
902 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100903 break;
904 }
905
906 return err;
907}
908
909/* Add listener to the list of tcpv4 listeners. The listener's state
910 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
911 * listeners is updated. This is the function to use to add a new listener.
912 */
913void tcpv4_add_listener(struct listener *listener)
914{
915 if (listener->state != LI_INIT)
916 return;
917 listener->state = LI_ASSIGNED;
918 listener->proto = &proto_tcpv4;
919 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
920 proto_tcpv4.nb_listeners++;
921}
922
923/* Add listener to the list of tcpv4 listeners. The listener's state
924 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
925 * listeners is updated. This is the function to use to add a new listener.
926 */
927void tcpv6_add_listener(struct listener *listener)
928{
929 if (listener->state != LI_INIT)
930 return;
931 listener->state = LI_ASSIGNED;
932 listener->proto = &proto_tcpv6;
933 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
934 proto_tcpv6.nb_listeners++;
935}
936
Willy Tarreauedcf6682008-11-30 23:15:34 +0100937/* This function performs the TCP request analysis on the current request. It
938 * returns 1 if the processing can continue on next analysers, or zero if it
939 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200940 * request. It relies on buffers flags, and updates s->req->analysers. The
941 * function may be called for frontend rules and backend rules. It only relies
942 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100943 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200944int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100945{
946 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200947 struct stksess *ts;
948 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100949 int partial;
950
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100951 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 +0100952 now_ms, __FUNCTION__,
953 s,
954 req,
955 req->rex, req->wex,
956 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200957 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100958 req->analysers);
959
Willy Tarreauedcf6682008-11-30 23:15:34 +0100960 /* We don't know whether we have enough data, so must proceed
961 * this way :
962 * - iterate through all rules in their declaration order
963 * - if one rule returns MISS, it means the inspect delay is
964 * not over yet, then return immediately, otherwise consider
965 * it as a non-match.
966 * - if one rule returns OK, then return OK
967 * - if one rule returns KO, then return KO
968 */
969
Willy Tarreau9b28e032012-10-12 23:49:43 +0200970 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200971 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200972 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100973 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200974 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100975
Willy Tarreaufb356202010-08-03 14:02:05 +0200976 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +0100977 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100978
979 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200980 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100981 if (ret == ACL_TEST_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200982 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100983 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200984 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +0100985 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100986 return 0;
987 }
988
989 ret = acl_pass(ret);
990 if (rule->cond->pol == ACL_COND_UNLESS)
991 ret = !ret;
992 }
993
994 if (ret) {
995 /* we have a matching rule. */
996 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200997 channel_abort(req);
998 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100999 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001000
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001001 s->be->be_counters.denied_req++;
1002 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001003 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +02001004 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001005
Willy Tarreauedcf6682008-11-30 23:15:34 +01001006 if (!(s->flags & SN_ERR_MASK))
1007 s->flags |= SN_ERR_PRXCOND;
1008 if (!(s->flags & SN_FINST_MASK))
1009 s->flags |= SN_FINST_R;
1010 return 0;
1011 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001012 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001013 /* Note: only the first valid tracking parameter of each
1014 * applies.
1015 */
1016 struct stktable_key *key;
1017
Willy Tarreau44778ad2013-10-30 19:24:00 +01001018 if (s->stkctr[tcp_trk_idx(rule->action)].entry)
1019 continue;
1020
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001021 t = rule->act_prm.trk_ctr.table.t;
1022 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
1023
1024 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001025 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
1026 if (s->fe != s->be)
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001027 s->flags |= SN_BE_TRACK_SC0 << tcp_trk_idx(rule->action);
Willy Tarreaud1f96522010-08-03 19:34:32 +02001028 }
1029 }
1030 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +01001031 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001032 break;
1033 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001034 }
1035 }
1036
1037 /* if we get there, it means we have no rule which matches, or
1038 * we have an explicit accept, so we apply the default accept.
1039 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001040 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001041 req->analyse_exp = TICK_ETERNITY;
1042 return 1;
1043}
1044
Emeric Brun97679e72010-09-23 17:56:44 +02001045/* This function performs the TCP response analysis on the current response. It
1046 * returns 1 if the processing can continue on next analysers, or zero if it
1047 * needs more data, encounters an error, or wants to immediately abort the
1048 * response. It relies on buffers flags, and updates s->rep->analysers. The
1049 * function may be called for backend rules.
1050 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001051int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001052{
1053 struct tcp_rule *rule;
1054 int partial;
1055
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001056 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 +02001057 now_ms, __FUNCTION__,
1058 s,
1059 rep,
1060 rep->rex, rep->wex,
1061 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001062 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001063 rep->analysers);
1064
1065 /* We don't know whether we have enough data, so must proceed
1066 * this way :
1067 * - iterate through all rules in their declaration order
1068 * - if one rule returns MISS, it means the inspect delay is
1069 * not over yet, then return immediately, otherwise consider
1070 * it as a non-match.
1071 * - if one rule returns OK, then return OK
1072 * - if one rule returns KO, then return KO
1073 */
1074
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001075 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001076 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001077 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001078 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001079
1080 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001081 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001082
1083 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001084 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001085 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001086 /* just set the analyser timeout once at the beginning of the response */
1087 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001088 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001089 return 0;
1090 }
1091
1092 ret = acl_pass(ret);
1093 if (rule->cond->pol == ACL_COND_UNLESS)
1094 ret = !ret;
1095 }
1096
1097 if (ret) {
1098 /* we have a matching rule. */
1099 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001100 channel_abort(rep);
1101 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001102 rep->analysers = 0;
1103
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001104 s->be->be_counters.denied_resp++;
1105 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001106 if (s->listener->counters)
1107 s->listener->counters->denied_resp++;
1108
1109 if (!(s->flags & SN_ERR_MASK))
1110 s->flags |= SN_ERR_PRXCOND;
1111 if (!(s->flags & SN_FINST_MASK))
1112 s->flags |= SN_FINST_D;
1113 return 0;
1114 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001115 else if (rule->action == TCP_ACT_CLOSE) {
1116 rep->prod->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1117 si_shutr(rep->prod);
1118 si_shutw(rep->prod);
1119 break;
1120 }
Emeric Brun97679e72010-09-23 17:56:44 +02001121 else {
1122 /* otherwise accept */
1123 break;
1124 }
1125 }
1126 }
1127
1128 /* if we get there, it means we have no rule which matches, or
1129 * we have an explicit accept, so we apply the default accept.
1130 */
1131 rep->analysers &= ~an_bit;
1132 rep->analyse_exp = TICK_ETERNITY;
1133 return 1;
1134}
1135
1136
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001137/* This function performs the TCP layer4 analysis on the current request. It
1138 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1139 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001140 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001141 */
1142int tcp_exec_req_rules(struct session *s)
1143{
1144 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001145 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001146 struct stktable *t = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001147 struct connection *conn = objt_conn(s->si[0].end);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001148 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001149 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001150
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001151 if (!conn)
1152 return result;
1153
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001154 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001155 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001156
1157 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001158 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001159 ret = acl_pass(ret);
1160 if (rule->cond->pol == ACL_COND_UNLESS)
1161 ret = !ret;
1162 }
1163
1164 if (ret) {
1165 /* we have a matching rule. */
1166 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001167 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001168 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001169 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001170
1171 if (!(s->flags & SN_ERR_MASK))
1172 s->flags |= SN_ERR_PRXCOND;
1173 if (!(s->flags & SN_FINST_MASK))
1174 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001175 result = 0;
1176 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001177 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001178 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001179 /* Note: only the first valid tracking parameter of each
1180 * applies.
1181 */
1182 struct stktable_key *key;
1183
Willy Tarreau44778ad2013-10-30 19:24:00 +01001184 if (s->stkctr[tcp_trk_idx(rule->action)].entry)
1185 continue;
1186
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001187 t = rule->act_prm.trk_ctr.table.t;
1188 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
1189
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001190 if (key && (ts = stktable_get_entry(t, key)))
1191 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001192 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001193 else if (rule->action == TCP_ACT_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001194 conn->flags |= CO_FL_ACCEPT_PROXY;
1195 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001196 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001197 else {
1198 /* otherwise it's an accept */
1199 break;
1200 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001201 }
1202 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001203 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001204}
1205
Emeric Brun97679e72010-09-23 17:56:44 +02001206/* Parse a tcp-response rule. Return a negative value in case of failure */
1207static int tcp_parse_response_rule(char **args, int arg, int section_type,
1208 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau80aca902013-01-07 15:42:20 +01001209 struct tcp_rule *rule, char **err,
1210 unsigned int where)
Emeric Brun97679e72010-09-23 17:56:44 +02001211{
1212 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001213 memprintf(err, "%s %s is only allowed in 'backend' sections",
1214 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001215 return -1;
1216 }
1217
1218 if (strcmp(args[arg], "accept") == 0) {
1219 arg++;
1220 rule->action = TCP_ACT_ACCEPT;
1221 }
1222 else if (strcmp(args[arg], "reject") == 0) {
1223 arg++;
1224 rule->action = TCP_ACT_REJECT;
1225 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001226 else if (strcmp(args[arg], "close") == 0) {
1227 arg++;
1228 rule->action = TCP_ACT_CLOSE;
1229 }
Emeric Brun97679e72010-09-23 17:56:44 +02001230 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001231 memprintf(err,
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001232 "'%s %s' expects 'accept', 'close' or 'reject' in %s '%s' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001233 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001234 return -1;
1235 }
1236
1237 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001238 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1239 memprintf(err,
1240 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1241 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001242 return -1;
1243 }
1244 }
1245 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001246 memprintf(err,
1247 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001248 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1249 return -1;
1250 }
1251 return 0;
1252}
1253
1254
1255
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001256/* Parse a tcp-request rule. Return a negative value in case of failure */
1257static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001258 struct proxy *curpx, struct proxy *defpx,
1259 struct tcp_rule *rule, char **err,
1260 unsigned int where)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001261{
1262 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001263 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1264 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001265 return -1;
1266 }
1267
1268 if (!strcmp(args[arg], "accept")) {
1269 arg++;
1270 rule->action = TCP_ACT_ACCEPT;
1271 }
1272 else if (!strcmp(args[arg], "reject")) {
1273 arg++;
1274 rule->action = TCP_ACT_REJECT;
1275 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001276 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1277 args[arg][9] == '\0' && args[arg][8] >= '0' &&
1278 args[arg][8] <= '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001279 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001280 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001281
1282 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001283
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001284 curpx->conf.args.ctx = ARGC_TRK;
Willy Tarreau975c1782013-12-12 23:16:54 +01001285 expr = sample_parse_expr(args, &arg, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001286 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001287 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001288 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001289 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001290 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001291 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001292
Willy Tarreau80aca902013-01-07 15:42:20 +01001293 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001294 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001295 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001296 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001297 free(expr);
1298 return -1;
1299 }
1300
1301 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001302 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001303
1304 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001305 arg++;
1306 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001307 memprintf(err,
1308 "'%s %s %s' : missing table name",
1309 args[0], args[1], args[kw]);
1310 free(expr);
1311 return -1;
1312 }
1313 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001314 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001315 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001316 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001317 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001318 rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001319 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001320 else if (strcmp(args[arg], "expect-proxy") == 0) {
1321 if (strcmp(args[arg+1], "layer4") != 0) {
1322 memprintf(err,
1323 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1324 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1325 return -1;
1326 }
1327
1328 if (!(where & SMP_VAL_FE_CON_ACC)) {
1329 memprintf(err,
1330 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1331 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1332 return -1;
1333 }
1334
1335 arg += 2;
1336 rule->action = TCP_ACT_EXPECT_PX;
1337 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001338 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001339 memprintf(err,
Willy Tarreaub4c84932013-07-23 19:15:30 +02001340 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d' "
1341 " in %s '%s' (got '%s')",
1342 args[0], args[1], MAX_SESS_STKCTR, proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001343 return -1;
1344 }
1345
1346 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001347 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1348 memprintf(err,
1349 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1350 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001351 return -1;
1352 }
1353 }
1354 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001355 memprintf(err,
1356 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001357 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1358 return -1;
1359 }
1360 return 0;
1361}
1362
Emeric Brun97679e72010-09-23 17:56:44 +02001363/* This function should be called to parse a line starting with the "tcp-response"
1364 * keyword.
1365 */
1366static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001367 struct proxy *defpx, const char *file, int line,
1368 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001369{
1370 const char *ptr = NULL;
1371 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001372 int warn = 0;
1373 int arg;
1374 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001375 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001376 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001377 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001378
1379 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001380 memprintf(err, "missing argument for '%s' in %s '%s'",
1381 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001382 return -1;
1383 }
1384
1385 if (strcmp(args[1], "inspect-delay") == 0) {
1386 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001387 memprintf(err, "%s %s is only allowed in 'backend' sections",
1388 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001389 return -1;
1390 }
1391
1392 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001393 memprintf(err,
1394 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1395 args[0], args[1], proxy_type_str(curpx), curpx->id);
1396 if (ptr)
1397 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001398 return -1;
1399 }
1400
1401 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001402 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1403 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001404 return 1;
1405 }
1406 curpx->tcp_rep.inspect_delay = val;
1407 return 0;
1408 }
1409
Simon Hormande072bd2011-06-24 15:11:37 +09001410 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001411 LIST_INIT(&rule->list);
1412 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001413 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001414
1415 if (strcmp(args[1], "content") == 0) {
1416 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001417
1418 if (curpx->cap & PR_CAP_FE)
1419 where |= SMP_VAL_FE_RES_CNT;
1420 if (curpx->cap & PR_CAP_BE)
1421 where |= SMP_VAL_BE_RES_CNT;
1422
1423 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001424 goto error;
1425
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001426 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1427 if (acl) {
1428 if (acl->name && *acl->name)
1429 memprintf(err,
1430 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1431 acl->name, args[0], args[1], sample_ckp_names(where));
1432 else
1433 memprintf(err,
1434 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1435 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001436 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001437 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001438
Emeric Brun97679e72010-09-23 17:56:44 +02001439 warn++;
1440 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001441 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1442 if (acl->name && *acl->name)
1443 memprintf(err,
1444 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001445 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001446 else
1447 memprintf(err,
1448 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001449 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001450 warn++;
1451 }
Emeric Brun97679e72010-09-23 17:56:44 +02001452
1453 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1454 }
1455 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001456 memprintf(err,
1457 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1458 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001459 goto error;
1460 }
1461
1462 return warn;
1463 error:
1464 free(rule);
1465 return -1;
1466}
1467
1468
Willy Tarreaub6866442008-07-14 23:54:42 +02001469/* This function should be called to parse a line starting with the "tcp-request"
1470 * keyword.
1471 */
1472static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001473 struct proxy *defpx, const char *file, int line,
1474 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001475{
1476 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001477 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001478 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001479 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001480 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001481 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001482 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001483 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001484
1485 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001486 if (curpx == defpx)
1487 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1488 else
1489 memprintf(err, "missing argument for '%s' in %s '%s'",
1490 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001491 return -1;
1492 }
1493
1494 if (!strcmp(args[1], "inspect-delay")) {
1495 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001496 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1497 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001498 return -1;
1499 }
1500
Willy Tarreaub6866442008-07-14 23:54:42 +02001501 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001502 memprintf(err,
1503 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1504 args[0], args[1], proxy_type_str(curpx), curpx->id);
1505 if (ptr)
1506 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001507 return -1;
1508 }
1509
1510 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001511 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1512 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001513 return 1;
1514 }
1515 curpx->tcp_req.inspect_delay = val;
1516 return 0;
1517 }
1518
Simon Hormande072bd2011-06-24 15:11:37 +09001519 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001520 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001521 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001522 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001523
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001524 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001525 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001526
1527 if (curpx->cap & PR_CAP_FE)
1528 where |= SMP_VAL_FE_REQ_CNT;
1529 if (curpx->cap & PR_CAP_BE)
1530 where |= SMP_VAL_BE_REQ_CNT;
1531
1532 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001533 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001534
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001535 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1536 if (acl) {
1537 if (acl->name && *acl->name)
1538 memprintf(err,
1539 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1540 acl->name, args[0], args[1], sample_ckp_names(where));
1541 else
1542 memprintf(err,
1543 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1544 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001545 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001546 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001547
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001548 warn++;
1549 }
1550 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1551 if (acl->name && *acl->name)
1552 memprintf(err,
1553 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001554 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001555 else
1556 memprintf(err,
1557 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001558 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001559 warn++;
1560 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001561
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001562 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001563 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001564 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001565 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001566
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001567 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001568 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1569 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001570 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001571 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001572
Willy Tarreau80aca902013-01-07 15:42:20 +01001573 where |= SMP_VAL_FE_CON_ACC;
1574
1575 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001576 goto error;
1577
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001578 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1579 if (acl) {
1580 if (acl->name && *acl->name)
1581 memprintf(err,
1582 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1583 acl->name, args[0], args[1], sample_ckp_names(where));
1584 else
1585 memprintf(err,
1586 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1587 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001588 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001589 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001590
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001591 warn++;
1592 }
1593 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1594 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001595 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001596 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001597 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001598 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001599 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001600 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001601 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001602 warn++;
1603 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001604
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001605 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001606 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001607 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001608 if (curpx == defpx)
1609 memprintf(err,
1610 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1611 args[0], args[1]);
1612 else
1613 memprintf(err,
1614 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001615 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001616 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001617 }
1618
Willy Tarreau1a687942010-05-23 22:40:30 +02001619 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001620 error:
1621 free(rule);
1622 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001623}
1624
Willy Tarreau645513a2010-05-24 20:55:15 +02001625
1626/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001627/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001628/************************************************************************/
1629
Willy Tarreau4a129812012-04-25 17:31:42 +02001630/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001631static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001632smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001633 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001634{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001635 struct connection *cli_conn = objt_conn(l4->si[0].end);
1636
1637 if (!cli_conn)
1638 return 0;
1639
1640 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001641 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001642 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001643 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001644 break;
1645 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001646 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001647 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001648 break;
1649 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001650 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001651 }
Emeric Brunf769f512010-10-22 17:14:01 +02001652
Willy Tarreau37406352012-04-23 16:16:37 +02001653 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001654 return 1;
1655}
1656
Willy Tarreaua5e37562011-12-16 17:06:15 +01001657/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001658static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001659smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001660 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001661{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001662 struct connection *cli_conn = objt_conn(l4->si[0].end);
1663
1664 if (!cli_conn)
1665 return 0;
1666
Willy Tarreauf853c462012-04-23 18:53:56 +02001667 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001668 if (!(smp->data.uint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001669 return 0;
1670
Willy Tarreau37406352012-04-23 16:16:37 +02001671 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001672 return 1;
1673}
1674
Willy Tarreau4a129812012-04-25 17:31:42 +02001675/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001676static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001677smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001678 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001679{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001680 struct connection *cli_conn = objt_conn(l4->si[0].end);
Willy Tarreau645513a2010-05-24 20:55:15 +02001681
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001682 if (!cli_conn)
1683 return 0;
1684
1685 conn_get_to_addr(cli_conn);
1686
1687 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001688 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001689 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001690 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001691 break;
1692 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001693 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001694 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001695 break;
1696 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001697 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001698 }
Emeric Brunf769f512010-10-22 17:14:01 +02001699
Willy Tarreau37406352012-04-23 16:16:37 +02001700 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001701 return 1;
1702}
1703
Willy Tarreaua5e37562011-12-16 17:06:15 +01001704/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001705static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001706smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001707 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001708{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001709 struct connection *cli_conn = objt_conn(l4->si[0].end);
1710
1711 if (!cli_conn)
1712 return 0;
1713
1714 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001715
Willy Tarreauf853c462012-04-23 18:53:56 +02001716 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001717 if (!(smp->data.uint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001718 return 0;
1719
Willy Tarreau37406352012-04-23 16:16:37 +02001720 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001721 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001722}
1723
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001724#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001725/* parse the "v4v6" bind keyword */
1726static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1727{
1728 struct listener *l;
1729
1730 list_for_each_entry(l, &conf->listeners, by_bind) {
1731 if (l->addr.ss_family == AF_INET6)
1732 l->options |= LI_O_V4V6;
1733 }
1734
1735 return 0;
1736}
1737
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001738/* parse the "v6only" bind keyword */
1739static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1740{
1741 struct listener *l;
1742
1743 list_for_each_entry(l, &conf->listeners, by_bind) {
1744 if (l->addr.ss_family == AF_INET6)
1745 l->options |= LI_O_V6ONLY;
1746 }
1747
1748 return 0;
1749}
1750#endif
1751
Pieter Baauwd551fb52013-05-08 22:49:23 +02001752#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001753/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001754static 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 +02001755{
1756 struct listener *l;
1757
Willy Tarreau4348fad2012-09-20 16:48:07 +02001758 list_for_each_entry(l, &conf->listeners, by_bind) {
1759 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1760 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001761 }
1762
Willy Tarreau44791242012-09-12 23:27:21 +02001763 return 0;
1764}
1765#endif
1766
1767#ifdef TCP_DEFER_ACCEPT
1768/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001769static 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 +02001770{
1771 struct listener *l;
1772
Willy Tarreau4348fad2012-09-20 16:48:07 +02001773 list_for_each_entry(l, &conf->listeners, by_bind) {
1774 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1775 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001776 }
1777
Willy Tarreau44791242012-09-12 23:27:21 +02001778 return 0;
1779}
1780#endif
1781
Willy Tarreau1c862c52012-10-05 16:21:00 +02001782#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001783/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001784static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1785{
1786 struct listener *l;
1787
1788 list_for_each_entry(l, &conf->listeners, by_bind) {
1789 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1790 l->options |= LI_O_TCP_FO;
1791 }
1792
1793 return 0;
1794}
1795#endif
1796
Willy Tarreau44791242012-09-12 23:27:21 +02001797#ifdef TCP_MAXSEG
1798/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001799static 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 +02001800{
1801 struct listener *l;
1802 int mss;
1803
Willy Tarreau44791242012-09-12 23:27:21 +02001804 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001805 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001806 return ERR_ALERT | ERR_FATAL;
1807 }
1808
1809 mss = atoi(args[cur_arg + 1]);
1810 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001811 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001812 return ERR_ALERT | ERR_FATAL;
1813 }
1814
Willy Tarreau4348fad2012-09-20 16:48:07 +02001815 list_for_each_entry(l, &conf->listeners, by_bind) {
1816 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1817 l->maxseg = mss;
1818 }
Willy Tarreau44791242012-09-12 23:27:21 +02001819
1820 return 0;
1821}
1822#endif
1823
1824#ifdef SO_BINDTODEVICE
1825/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001826static 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 +02001827{
1828 struct listener *l;
1829
Willy Tarreau44791242012-09-12 23:27:21 +02001830 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001831 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001832 return ERR_ALERT | ERR_FATAL;
1833 }
1834
Willy Tarreau4348fad2012-09-20 16:48:07 +02001835 list_for_each_entry(l, &conf->listeners, by_bind) {
1836 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1837 l->interface = strdup(args[cur_arg + 1]);
1838 }
Willy Tarreau44791242012-09-12 23:27:21 +02001839
1840 global.last_checks |= LSTCHK_NETADM;
1841 return 0;
1842}
1843#endif
1844
Willy Tarreaudc13c112013-06-21 23:16:39 +02001845static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001846 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001847 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001848 { 0, NULL, NULL },
1849}};
1850
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001851
Willy Tarreau61612d42012-04-19 18:42:05 +02001852/* Note: must not be declared <const> as its list will be overwritten.
1853 * Please take care of keeping this list alphabetically sorted.
1854 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001855static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001856 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02001857}};
1858
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001859
Willy Tarreau4a129812012-04-25 17:31:42 +02001860/* Note: must not be declared <const> as its list will be overwritten.
1861 * Note: fetches that may return multiple types must be declared as the lowest
1862 * common denominator, the type that can be casted into all other ones. For
1863 * instance v4/v6 must be declared v4.
1864 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001865static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001866 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1867 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1868 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1869 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1870 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001871}};
1872
Willy Tarreau44791242012-09-12 23:27:21 +02001873/************************************************************************/
1874/* All supported bind keywords must be declared here. */
1875/************************************************************************/
1876
1877/* Note: must not be declared <const> as its list will be overwritten.
1878 * Please take care of keeping this list alphabetically sorted, doing so helps
1879 * all code contributors.
1880 * Optional keywords are also declared with a NULL ->parse() function so that
1881 * the config parser can report an appropriate error when a known keyword was
1882 * not enabled.
1883 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001884static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001885#ifdef TCP_DEFER_ACCEPT
1886 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1887#endif
1888#ifdef SO_BINDTODEVICE
1889 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1890#endif
1891#ifdef TCP_MAXSEG
1892 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1893#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001894#ifdef TCP_FASTOPEN
1895 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1896#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001897#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001898 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1899#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001900#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001901 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001902 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1903#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001904 /* the versions with the NULL parse function*/
1905 { "defer-accept", NULL, 0 },
1906 { "interface", NULL, 1 },
1907 { "mss", NULL, 1 },
1908 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001909 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001910 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001911 { NULL, NULL, 0 },
1912}};
1913
Willy Tarreaue6b98942007-10-29 01:09:36 +01001914__attribute__((constructor))
1915static void __tcp_protocol_init(void)
1916{
1917 protocol_register(&proto_tcpv4);
1918 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001919 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001920 cfg_register_keywords(&cfg_kws);
1921 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001922 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001923}
1924
1925
1926/*
1927 * Local variables:
1928 * c-indent-level: 8
1929 * c-basic-offset: 8
1930 * End:
1931 */