blob: cfa62f72223cdf1033225b46a0d5b0470b5bed7e [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 Tarreau18bf01e2014-06-13 16:18:52 +020038#include <types/capture.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020039#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010040
41#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020042#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020043#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020044#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020045#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020046#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020047#include <proto/log.h>
48#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020049#include <proto/protocol.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010050#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020051#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020052#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020053#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020054#include <proto/stick_table.h>
Willy Tarreaucc1e04b2013-09-11 23:20:29 +020055#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020056#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010057
Willy Tarreaue8c66af2008-01-13 18:40:14 +010058#ifdef CONFIG_HAP_CTTPROXY
59#include <import/ip_tproxy.h>
60#endif
61
Emeric Bruncf20bf12010-10-22 16:06:11 +020062static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
63static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010064
65/* Note: must not be declared <const> as its list will be overwritten */
66static struct protocol proto_tcpv4 = {
67 .name = "tcpv4",
68 .sock_domain = AF_INET,
69 .sock_type = SOCK_STREAM,
70 .sock_prot = IPPROTO_TCP,
71 .sock_family = AF_INET,
72 .sock_addrlen = sizeof(struct sockaddr_in),
73 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020074 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020075 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020076 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010077 .bind_all = tcp_bind_listeners,
78 .unbind_all = unbind_all_listeners,
79 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020080 .get_src = tcp_get_src,
81 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020082 .drain = tcp_drain,
Willy Tarreaud903bb32014-07-07 20:22:12 +020083 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010084 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
85 .nb_listeners = 0,
86};
87
88/* Note: must not be declared <const> as its list will be overwritten */
89static struct protocol proto_tcpv6 = {
90 .name = "tcpv6",
91 .sock_domain = AF_INET6,
92 .sock_type = SOCK_STREAM,
93 .sock_prot = IPPROTO_TCP,
94 .sock_family = AF_INET6,
95 .sock_addrlen = sizeof(struct sockaddr_in6),
96 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020097 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020098 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020099 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100100 .bind_all = tcp_bind_listeners,
101 .unbind_all = unbind_all_listeners,
102 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200103 .get_src = tcp_get_src,
104 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200105 .drain = tcp_drain,
Willy Tarreaud903bb32014-07-07 20:22:12 +0200106 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100107 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
108 .nb_listeners = 0,
109};
110
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100111
David du Colombier6f5ccb12011-03-10 22:26:24 +0100112/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100113 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
114 * - 0 : ignore remote address (may even be a NULL pointer)
115 * - 1 : use provided address
116 * - 2 : use provided port
117 * - 3 : use both
118 *
119 * The function supports multiple foreign binding methods :
120 * - linux_tproxy: we directly bind to the foreign address
121 * - cttproxy: we bind to a local address then nat.
122 * The second one can be used as a fallback for the first one.
123 * This function returns 0 when everything's OK, 1 if it could not bind, to the
124 * local address, 2 if it could not bind to the foreign address.
125 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100126int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100127{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100128 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100129 int foreign_ok = 0;
130 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100131 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200132 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200133
David du Colombier65c17962012-07-13 14:34:59 +0200134 switch (local->ss_family) {
135 case AF_INET:
136 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200137 /* This deserves some explanation. Some platforms will support
138 * multiple combinations of certain methods, so we try the
139 * supported ones until one succeeds.
140 */
141 if (0
142#if defined(IP_TRANSPARENT)
143 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
144#endif
145#if defined(IP_FREEBIND)
146 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
147#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200148#if defined(IP_BINDANY)
149 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
150#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200151#if defined(SO_BINDANY)
152 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
153#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200154 )
David du Colombier65c17962012-07-13 14:34:59 +0200155 foreign_ok = 1;
156 else
157 ip_transp_working = 0;
158 }
159 break;
160 case AF_INET6:
161 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200162 if (0
163#if defined(IPV6_TRANSPARENT)
164 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
165#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100166#if defined(IP_FREEBIND)
167 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
168#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200169#if defined(IPV6_BINDANY)
170 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
171#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200172#if defined(SO_BINDANY)
173 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
174#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200175 )
David du Colombier65c17962012-07-13 14:34:59 +0200176 foreign_ok = 1;
177 else
178 ip6_transp_working = 0;
179 }
180 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100181 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200182
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100183 if (flags) {
184 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200185 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100186 switch (remote->ss_family) {
187 case AF_INET:
188 if (flags & 1)
189 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
190 if (flags & 2)
191 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
192 break;
193 case AF_INET6:
194 if (flags & 1)
195 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
196 if (flags & 2)
197 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
198 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100199 default:
200 /* we don't want to try to bind to an unknown address family */
201 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100202 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100203 }
204
Simon Hormande072bd2011-06-24 15:11:37 +0900205 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100206 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200207 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200208 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
209 if (ret < 0)
210 return 2;
211 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100212 }
213 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200214 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200215 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
216 if (ret < 0)
217 return 1;
218 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100219 }
220
221 if (!flags)
222 return 0;
223
224#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100225 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100226 struct in_tproxy itp1, itp2;
227 memset(&itp1, 0, sizeof(itp1));
228
229 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100230 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
231 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100232
233 /* set connect flag on socket */
234 itp2.op = TPROXY_FLAGS;
235 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
236
237 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
238 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
239 foreign_ok = 1;
240 }
241 }
242#endif
243 if (!foreign_ok)
244 /* we could not bind to a foreign address */
245 return 2;
246
247 return 0;
248}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100249
Willy Tarreau9650f372009-08-16 14:02:45 +0200250
251/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200252 * This function initiates a TCP connection establishment to the target assigned
253 * to connection <conn> using (si->{target,addr.to}). A source address may be
254 * pointed to by conn->addr.from in case of transparent proxying. Normal source
255 * bind addresses are still determined locally (due to the possible need of a
256 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100257 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100258 * supported. The <data> parameter is a boolean indicating whether there are data
259 * waiting for being sent or not, in order to adjust data write polling and on
260 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
261 * allows the caller to force using a delayed ACK when establishing the connection :
262 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
263 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
264 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200265 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200266 * Note that a pending send_proxy message accounts for data.
267 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200268 * It can return one of :
269 * - SN_ERR_NONE if everything's OK
270 * - SN_ERR_SRVTO if there are no more servers
271 * - SN_ERR_SRVCL if the connection was refused by the server
272 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
273 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
274 * - SN_ERR_INTERNAL for any other purely internal errors
275 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100276 *
277 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
278 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200279 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100280
Willy Tarreauf0837b22012-11-24 10:24:27 +0100281int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200282{
283 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100284 struct server *srv;
285 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100286 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100287
Willy Tarreau9ce70132014-01-24 16:08:19 +0100288 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
289
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100290 switch (obj_type(conn->target)) {
291 case OBJ_TYPE_PROXY:
292 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100293 srv = NULL;
294 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100295 case OBJ_TYPE_SERVER:
296 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100297 be = srv->proxy;
298 break;
299 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100300 conn->flags |= CO_FL_ERROR;
Willy Tarreauac825402011-03-04 22:04:29 +0100301 return SN_ERR_INTERNAL;
302 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200303
Willy Tarreau986a9d22012-08-30 21:11:38 +0200304 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200305 qfprintf(stderr, "Cannot get a server socket.\n");
306
Willy Tarreau9ce70132014-01-24 16:08:19 +0100307 if (errno == ENFILE) {
308 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200309 send_log(be, LOG_EMERG,
310 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
311 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100312 }
313 else if (errno == EMFILE) {
314 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200315 send_log(be, LOG_EMERG,
316 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
317 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100318 }
319 else if (errno == ENOBUFS || errno == ENOMEM) {
320 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200321 send_log(be, LOG_EMERG,
322 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
323 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100324 }
325 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
326 conn->err_code = CO_ER_NOPROTO;
327 }
328 else
329 conn->err_code = CO_ER_SOCK_ERR;
330
Willy Tarreau9650f372009-08-16 14:02:45 +0200331 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100332 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200333 return SN_ERR_RESOURCE;
334 }
335
336 if (fd >= global.maxsock) {
337 /* do not log anything there, it's a normal condition when this option
338 * is used to serialize connections to a server !
339 */
340 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
341 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100342 conn->err_code = CO_ER_CONF_FDLIM;
343 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200344 return SN_ERR_PRXCOND; /* it is a configuration limit */
345 }
346
347 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900348 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200349 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
350 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100351 conn->err_code = CO_ER_SOCK_ERR;
352 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200353 return SN_ERR_INTERNAL;
354 }
355
356 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900357 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200358
Willy Tarreau9650f372009-08-16 14:02:45 +0200359 /* allow specific binding :
360 * - server-specific at first
361 * - proxy-specific next
362 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100363 if (srv && srv->conn_src.opts & CO_SRC_BIND)
364 src = &srv->conn_src;
365 else if (be->conn_src.opts & CO_SRC_BIND)
366 src = &be->conn_src;
367 else
368 src = NULL;
369
370 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200371 int ret, flags = 0;
372
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200373 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100374 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100375 case CO_SRC_TPROXY_ADDR:
376 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200377 flags = 3;
378 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100379 case CO_SRC_TPROXY_CIP:
380 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200381 flags = 1;
382 break;
383 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200384 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200385
Willy Tarreau9650f372009-08-16 14:02:45 +0200386#ifdef SO_BINDTODEVICE
387 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100388 if (src->iface_name)
389 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200390#endif
391
Willy Tarreaua4380b42012-12-08 22:49:11 +0100392 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200393 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100394 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200395
396 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100397 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200398
399 do {
400 /* note: in case of retry, we may have to release a previously
401 * allocated port, hence this loop's construct.
402 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200403 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
404 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200405
406 if (!attempts)
407 break;
408 attempts--;
409
Willy Tarreaua4380b42012-12-08 22:49:11 +0100410 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100411 if (!fdinfo[fd].local_port) {
412 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200413 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100414 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200415
Willy Tarreaua4380b42012-12-08 22:49:11 +0100416 fdinfo[fd].port_range = src->sport_range;
417 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200418
Willy Tarreaua4380b42012-12-08 22:49:11 +0100419 ret = tcp_bind_socket(fd, flags, &sa, &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 } while (ret != 0); /* binding NOK */
423 }
424 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100425 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100426 if (ret != 0)
427 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200428 }
429
Willy Tarreaua4380b42012-12-08 22:49:11 +0100430 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200431 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
432 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200433 close(fd);
434
Willy Tarreau9650f372009-08-16 14:02:45 +0200435 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100436 Alert("Cannot bind to 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 source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200440 be->id);
441 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100442 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200443 be->id);
444 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100445 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200446 be->id);
447 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100448 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200449 return SN_ERR_RESOURCE;
450 }
451 }
452
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400453#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 /* disabling tcp quick ack now allows the first request to leave the
455 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100456 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200457 */
Willy Tarreaua94af2c2014-10-24 12:02:24 +0200458 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900459 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200460#endif
461
Willy Tarreaue803de22010-01-21 17:43:04 +0100462 if (global.tune.server_sndbuf)
463 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
464
465 if (global.tune.server_rcvbuf)
466 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
467
Willy Tarreau986a9d22012-08-30 21:11:38 +0200468 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200469 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
470
Willy Tarreaub1719512012-12-08 23:03:28 +0100471 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200472 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100473 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200474 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100475 conn->err_code = CO_ER_FREE_PORTS;
476 }
477 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200478 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100479 conn->err_code = CO_ER_ADDR_INUSE;
480 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200481
Willy Tarreaub1719512012-12-08 23:03:28 +0100482 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200483 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
484 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200485 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100486 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100487 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200488 return SN_ERR_RESOURCE;
489 } else if (errno == ETIMEDOUT) {
490 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200491 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
492 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200493 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100494 conn->err_code = CO_ER_SOCK_ERR;
495 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200496 return SN_ERR_SRVTO;
497 } else {
498 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
499 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200500 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
501 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200502 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100503 conn->err_code = CO_ER_SOCK_ERR;
504 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200505 return SN_ERR_SRVCL;
506 }
507 }
508
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100509 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200510
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200511 /* Prepare to send a few handshakes related to the on-wire protocol. */
512 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200513 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200514
Willy Tarreauf79c8172013-10-21 16:30:56 +0200515 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100516 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200517 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200518
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200519 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200520 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100521 conn->flags |= CO_FL_ERROR;
Willy Tarreau15678ef2012-08-31 13:54:11 +0200522 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200523 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200524
Willy Tarreau14f8e862012-08-30 22:23:13 +0200525 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200526 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200527
Willy Tarreau9650f372009-08-16 14:02:45 +0200528 return SN_ERR_NONE; /* connection is OK */
529}
530
531
Willy Tarreau59b94792012-05-11 16:16:40 +0200532/*
533 * Retrieves the source address for the socket <fd>, with <dir> indicating
534 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
535 * success, -1 in case of error. The socket's source address is stored in
536 * <sa> for <salen> bytes.
537 */
538int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
539{
540 if (dir)
541 return getsockname(fd, sa, &salen);
542 else
543 return getpeername(fd, sa, &salen);
544}
545
546
547/*
548 * Retrieves the original destination address for the socket <fd>, with <dir>
549 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
550 * listener, if the original destination address was translated, the original
551 * address is retrieved. It returns 0 in case of success, -1 in case of error.
552 * The socket's source address is stored in <sa> for <salen> bytes.
553 */
554int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
555{
556 if (dir)
557 return getpeername(fd, sa, &salen);
Willy Tarreaufab22302014-10-29 21:46:01 +0100558 else {
559 int ret = getsockname(fd, sa, &salen);
560
561 if (ret < 0)
562 return ret;
563
Willy Tarreau59b94792012-05-11 16:16:40 +0200564#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreaufab22302014-10-29 21:46:01 +0100565 /* For TPROXY and Netfilter's NAT, we can retrieve the original
566 * IPv4 address before DNAT/REDIRECT. We must not do that with
567 * other families because v6-mapped IPv4 addresses are still
568 * reported as v4.
569 */
570 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
571 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
572 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200573#endif
Willy Tarreaufab22302014-10-29 21:46:01 +0100574 return ret;
575 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200576}
577
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200578/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100579 * receive shutdown. Returns positive if the shutdown was found, negative
580 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
581 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200582 */
583int tcp_drain(int fd)
584{
585 int turns = 2;
586 int len;
587
588 while (turns) {
589#ifdef MSG_TRUNC_CLEARS_INPUT
590 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
591 if (len == -1 && errno == EFAULT)
592#endif
593 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
594
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100595 if (len == 0) {
596 /* cool, shutdown received */
597 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200598 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100599 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200600
601 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100602 if (errno == EAGAIN) {
603 /* connection not closed yet */
604 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100605 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100606 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200607 if (errno == EINTR) /* oops, try again */
608 continue;
609 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100610 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200611 return 1;
612 }
613 /* OK we read some data, let's try again once */
614 turns--;
615 }
616 /* some data are still present, give up */
617 return 0;
618}
619
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200620/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100621 * and we have nothing to send. It updates the FD polling status. It returns 0
622 * if it fails in a fatal way or needs to poll to go further, otherwise it
623 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
624 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
625 * errno. The error checking is done in two passes in order to limit the number
626 * of syscalls in the normal case :
627 * - if POLL_ERR was reported by the poller, we check for a pending error on
628 * the socket before proceeding. If found, it's assigned to errno so that
629 * upper layers can see it.
630 * - otherwise connect() is used to check the connection state again, since
631 * the getsockopt return cannot reliably be used to know if the connection
632 * is still pending or ready. This one may often return an error as well,
633 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200634 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200635int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200636{
Willy Tarreau239d7182012-07-23 18:53:03 +0200637 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100638 socklen_t lskerr;
639 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200640
Willy Tarreau80184712012-07-06 14:54:49 +0200641 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200642 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200643
Willy Tarreau3c728722014-01-23 13:50:42 +0100644 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200645 return 0;
646
Willy Tarreau80184712012-07-06 14:54:49 +0200647 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200648 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200649
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100650 if (!fd_send_ready(fd))
651 return 0;
652
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100653 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
654 * without FD_POLL_IN also indicates a hangup without input data meaning
655 * there was no connection.
656 */
657 if (fdtab[fd].ev & FD_POLL_ERR ||
658 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
659 skerr = 0;
660 lskerr = sizeof(skerr);
661 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
662 errno = skerr;
663 if (errno == EAGAIN)
664 errno = 0;
665 if (errno)
666 goto out_error;
667 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200668
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100669 /* Use connect() to check the state of the socket. This has the
670 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200671 * - error
672 * - connecting (EALREADY, EINPROGRESS)
673 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200674 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200675 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200676 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100677 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100678 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200679 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200680 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200681
Willy Tarreau2c6be842012-07-06 17:12:34 +0200682 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200683 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200684
Willy Tarreau2c6be842012-07-06 17:12:34 +0200685 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200686 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200687
Willy Tarreau076be252012-07-06 16:02:29 +0200688 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200689 * forward the event to the transport layer which will notify the
690 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200691 */
Willy Tarreau80184712012-07-06 14:54:49 +0200692 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200693 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200694
695 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200696 /* Write error on the file descriptor. Report it to the connection
697 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200698 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100699 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100700 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100701 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200702 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200703}
704
Willy Tarreau59b94792012-05-11 16:16:40 +0200705
Willy Tarreaue6b98942007-10-29 01:09:36 +0100706/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100707 * an error message in <errmsg> if the message is at most <errlen> bytes long
708 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
709 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100710 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
711 * was alright and that no message was returned. ERR_RETRYABLE means that an
712 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700713 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100714 * the meaning of the error, but just indicate that a message is present which
715 * should be displayed with the respective level. Last, ERR_ABORT indicates
716 * that it's pointless to try to start other listeners. No error message is
717 * returned if errlen is NULL.
718 */
719int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
720{
721 __label__ tcp_return, tcp_close_return;
722 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100723 int ext, ready;
724 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100725 const char *msg = NULL;
726
727 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100728 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100729 *errmsg = 0;
730
731 if (listener->state != LI_ASSIGNED)
732 return ERR_NONE; /* already bound */
733
734 err = ERR_NONE;
735
Willy Tarreau40aa0702013-03-10 23:51:38 +0100736 /* if the listener already has an fd assigned, then we were offered the
737 * fd by an external process (most likely the parent), and we don't want
738 * to create a new socket. However we still want to set a few flags on
739 * the socket.
740 */
741 fd = listener->fd;
742 ext = (fd >= 0);
743
744 if (!ext && (fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100745 err |= ERR_RETRYABLE | ERR_ALERT;
746 msg = "cannot create listening socket";
747 goto tcp_return;
748 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100749
Willy Tarreaue6b98942007-10-29 01:09:36 +0100750 if (fd >= global.maxsock) {
751 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
752 msg = "not enough free sockets (raise '-n' parameter)";
753 goto tcp_close_return;
754 }
755
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200756 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100757 err |= ERR_FATAL | ERR_ALERT;
758 msg = "cannot make socket non-blocking";
759 goto tcp_close_return;
760 }
761
Willy Tarreau40aa0702013-03-10 23:51:38 +0100762 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100763 /* not fatal but should be reported */
764 msg = "cannot do so_reuseaddr";
765 err |= ERR_ALERT;
766 }
767
768 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900769 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100770
Willy Tarreaue6b98942007-10-29 01:09:36 +0100771#ifdef SO_REUSEPORT
772 /* OpenBSD supports this. As it's present in old libc versions of Linux,
773 * it might return an error that we will silently ignore.
774 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100775 if (!ext)
776 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100777#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200778
Willy Tarreau40aa0702013-03-10 23:51:38 +0100779 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200780 switch (listener->addr.ss_family) {
781 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200782 if (1
783#if defined(IP_TRANSPARENT)
784 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
785#endif
786#if defined(IP_FREEBIND)
787 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
788#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200789#if defined(IP_BINDANY)
790 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
791#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200792#if defined(SO_BINDANY)
793 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
794#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200795 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200796 msg = "cannot make listening socket transparent";
797 err |= ERR_ALERT;
798 }
799 break;
800 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200801 if (1
802#if defined(IPV6_TRANSPARENT)
803 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
804#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100805#if defined(IP_FREEBIND)
806 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
807#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200808#if defined(IPV6_BINDANY)
809 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
810#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200811#if defined(SO_BINDANY)
812 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
813#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200814 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200815 msg = "cannot make listening socket transparent";
816 err |= ERR_ALERT;
817 }
818 break;
819 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100820 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200821
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100822#ifdef SO_BINDTODEVICE
823 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100824 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100825 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100826 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100827 msg = "cannot bind listener to device";
828 err |= ERR_WARN;
829 }
830 }
831#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400832#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100833 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400834 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200835 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
836 msg = "cannot set MSS";
837 err |= ERR_WARN;
838 }
839 }
840#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200841#if defined(TCP_DEFER_ACCEPT)
842 if (listener->options & LI_O_DEF_ACCEPT) {
843 /* defer accept by up to one second */
844 int accept_delay = 1;
845 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
846 msg = "cannot enable DEFER_ACCEPT";
847 err |= ERR_WARN;
848 }
849 }
850#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200851#if defined(TCP_FASTOPEN)
852 if (listener->options & LI_O_TCP_FO) {
853 /* TFO needs a queue length, let's use the configured backlog */
854 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
855 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
856 msg = "cannot enable TCP_FASTOPEN";
857 err |= ERR_WARN;
858 }
859 }
860#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100861#if defined(IPV6_V6ONLY)
862 if (listener->options & LI_O_V6ONLY)
863 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100864 else if (listener->options & LI_O_V4V6)
865 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100866#endif
867
Willy Tarreau40aa0702013-03-10 23:51:38 +0100868 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100869 err |= ERR_RETRYABLE | ERR_ALERT;
870 msg = "cannot bind socket";
871 goto tcp_close_return;
872 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100873
Willy Tarreau40aa0702013-03-10 23:51:38 +0100874 ready = 0;
875 ready_len = sizeof(ready);
876 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
877 ready = 0;
878
879 if (!(ext && ready) && /* only listen if not already done by external process */
880 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100881 err |= ERR_RETRYABLE | ERR_ALERT;
882 msg = "cannot listen to socket";
883 goto tcp_close_return;
884 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100885
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400886#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200887 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900888 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200889#endif
890
Willy Tarreaue6b98942007-10-29 01:09:36 +0100891 /* the socket is ready */
892 listener->fd = fd;
893 listener->state = LI_LISTEN;
894
Willy Tarreaueabf3132008-08-29 23:36:51 +0200895 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200896 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200897 fd_insert(fd);
898
Willy Tarreaue6b98942007-10-29 01:09:36 +0100899 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100900 if (msg && errlen) {
901 char pn[INET6_ADDRSTRLEN];
902
Willy Tarreau631f01c2011-09-05 00:36:48 +0200903 addr_to_str(&listener->addr, pn, sizeof(pn));
904 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100905 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100906 return err;
907
908 tcp_close_return:
909 close(fd);
910 goto tcp_return;
911}
912
913/* This function creates all TCP sockets bound to the protocol entry <proto>.
914 * It is intended to be used as the protocol's bind_all() function.
915 * The sockets will be registered but not added to any fd_set, in order not to
916 * loose them across the fork(). A call to enable_all_listeners() is needed
917 * to complete initialization. The return value is composed from ERR_*.
918 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200919static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100920{
921 struct listener *listener;
922 int err = ERR_NONE;
923
924 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200925 err |= tcp_bind_listener(listener, errmsg, errlen);
926 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100927 break;
928 }
929
930 return err;
931}
932
933/* Add listener to the list of tcpv4 listeners. The listener's state
934 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
935 * listeners is updated. This is the function to use to add a new listener.
936 */
937void tcpv4_add_listener(struct listener *listener)
938{
939 if (listener->state != LI_INIT)
940 return;
941 listener->state = LI_ASSIGNED;
942 listener->proto = &proto_tcpv4;
943 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
944 proto_tcpv4.nb_listeners++;
945}
946
947/* Add listener to the list of tcpv4 listeners. The listener's state
948 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
949 * listeners is updated. This is the function to use to add a new listener.
950 */
951void tcpv6_add_listener(struct listener *listener)
952{
953 if (listener->state != LI_INIT)
954 return;
955 listener->state = LI_ASSIGNED;
956 listener->proto = &proto_tcpv6;
957 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
958 proto_tcpv6.nb_listeners++;
959}
960
Willy Tarreaud903bb32014-07-07 20:22:12 +0200961/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
962 * was totally stopped, or > 0 if correctly paused.
963 */
964int tcp_pause_listener(struct listener *l)
965{
966 if (shutdown(l->fd, SHUT_WR) != 0)
967 return -1; /* Solaris dies here */
968
969 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
970 return -1; /* OpenBSD dies here */
971
972 if (shutdown(l->fd, SHUT_RD) != 0)
973 return -1; /* should always be OK */
974 return 1;
975}
976
Willy Tarreauedcf6682008-11-30 23:15:34 +0100977/* This function performs the TCP request analysis on the current request. It
978 * returns 1 if the processing can continue on next analysers, or zero if it
979 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200980 * request. It relies on buffers flags, and updates s->req->analysers. The
981 * function may be called for frontend rules and backend rules. It only relies
982 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100983 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200984int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100985{
986 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200987 struct stksess *ts;
988 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100989 int partial;
990
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100991 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 +0100992 now_ms, __FUNCTION__,
993 s,
994 req,
995 req->rex, req->wex,
996 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200997 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100998 req->analysers);
999
Willy Tarreauedcf6682008-11-30 23:15:34 +01001000 /* We don't know whether we have enough data, so must proceed
1001 * this way :
1002 * - iterate through all rules in their declaration order
1003 * - if one rule returns MISS, it means the inspect delay is
1004 * not over yet, then return immediately, otherwise consider
1005 * it as a non-match.
1006 * - if one rule returns OK, then return OK
1007 * - if one rule returns KO, then return KO
1008 */
1009
Willy Tarreau9b28e032012-10-12 23:49:43 +02001010 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001011 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001012 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001013 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001014 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001015
Willy Tarreaufb356202010-08-03 14:02:05 +02001016 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001017 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001018
1019 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001020 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001021 if (ret == ACL_TEST_MISS)
1022 goto missing_data;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001023
1024 ret = acl_pass(ret);
1025 if (rule->cond->pol == ACL_COND_UNLESS)
1026 ret = !ret;
1027 }
1028
1029 if (ret) {
1030 /* we have a matching rule. */
1031 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001032 channel_abort(req);
1033 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001034 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001035
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001036 s->be->be_counters.denied_req++;
1037 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001038 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +02001039 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001040
Willy Tarreauedcf6682008-11-30 23:15:34 +01001041 if (!(s->flags & SN_ERR_MASK))
1042 s->flags |= SN_ERR_PRXCOND;
1043 if (!(s->flags & SN_FINST_MASK))
1044 s->flags |= SN_FINST_R;
1045 return 0;
1046 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001047 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001048 /* Note: only the first valid tracking parameter of each
1049 * applies.
1050 */
1051 struct stktable_key *key;
Willy Tarreau86bd33a2014-06-25 17:01:56 +02001052 struct sample smp;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001053
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001054 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001055 continue;
1056
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001057 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau86bd33a2014-06-25 17:01:56 +02001058 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial, rule->act_prm.trk_ctr.expr, &smp);
1059
Willy Tarreauf94735e2014-07-30 08:56:35 +02001060 if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL))
1061 goto missing_data; /* key might appear later */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001062
1063 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001064 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001065 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001066 if (s->fe != s->be)
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001067 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreaud1f96522010-08-03 19:34:32 +02001068 }
1069 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001070 else if (rule->action == TCP_ACT_CAPTURE) {
1071 struct sample *key;
1072 struct cap_hdr *h = rule->act_prm.cap.hdr;
1073 char **cap = s->txn.req.cap;
1074 int len;
1075
1076 key = sample_fetch_string(s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial, rule->act_prm.cap.expr);
1077 if (!key)
1078 continue;
1079
1080 if (key->flags & SMP_F_MAY_CHANGE)
1081 goto missing_data;
1082
1083 if (cap[h->index] == NULL)
1084 cap[h->index] = pool_alloc2(h->pool);
1085
1086 if (cap[h->index] == NULL) /* no more capture memory */
1087 continue;
1088
1089 len = key->data.str.len;
1090 if (len > h->len)
1091 len = h->len;
1092
1093 memcpy(cap[h->index], key->data.str.str, len);
1094 cap[h->index][len] = 0;
1095 }
Willy Tarreaud1f96522010-08-03 19:34:32 +02001096 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +01001097 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001098 break;
1099 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001100 }
1101 }
1102
1103 /* if we get there, it means we have no rule which matches, or
1104 * we have an explicit accept, so we apply the default accept.
1105 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001106 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001107 req->analyse_exp = TICK_ETERNITY;
1108 return 1;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001109
1110 missing_data:
1111 channel_dont_connect(req);
1112 /* just set the request timeout once at the beginning of the request */
1113 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
1114 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
1115 return 0;
1116
Willy Tarreauedcf6682008-11-30 23:15:34 +01001117}
1118
Emeric Brun97679e72010-09-23 17:56:44 +02001119/* This function performs the TCP response analysis on the current response. It
1120 * returns 1 if the processing can continue on next analysers, or zero if it
1121 * needs more data, encounters an error, or wants to immediately abort the
1122 * response. It relies on buffers flags, and updates s->rep->analysers. The
1123 * function may be called for backend rules.
1124 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001125int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001126{
1127 struct tcp_rule *rule;
1128 int partial;
1129
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001130 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 +02001131 now_ms, __FUNCTION__,
1132 s,
1133 rep,
1134 rep->rex, rep->wex,
1135 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001136 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001137 rep->analysers);
1138
1139 /* We don't know whether we have enough data, so must proceed
1140 * this way :
1141 * - iterate through all rules in their declaration order
1142 * - if one rule returns MISS, it means the inspect delay is
1143 * not over yet, then return immediately, otherwise consider
1144 * it as a non-match.
1145 * - if one rule returns OK, then return OK
1146 * - if one rule returns KO, then return KO
1147 */
1148
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001149 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001150 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001151 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001152 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001153
1154 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001155 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001156
1157 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001158 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001159 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001160 /* just set the analyser timeout once at the beginning of the response */
1161 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001162 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001163 return 0;
1164 }
1165
1166 ret = acl_pass(ret);
1167 if (rule->cond->pol == ACL_COND_UNLESS)
1168 ret = !ret;
1169 }
1170
1171 if (ret) {
1172 /* we have a matching rule. */
1173 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001174 channel_abort(rep);
1175 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001176 rep->analysers = 0;
1177
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001178 s->be->be_counters.denied_resp++;
1179 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001180 if (s->listener->counters)
1181 s->listener->counters->denied_resp++;
1182
1183 if (!(s->flags & SN_ERR_MASK))
1184 s->flags |= SN_ERR_PRXCOND;
1185 if (!(s->flags & SN_FINST_MASK))
1186 s->flags |= SN_FINST_D;
1187 return 0;
1188 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001189 else if (rule->action == TCP_ACT_CLOSE) {
1190 rep->prod->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1191 si_shutr(rep->prod);
1192 si_shutw(rep->prod);
1193 break;
1194 }
Emeric Brun97679e72010-09-23 17:56:44 +02001195 else {
1196 /* otherwise accept */
1197 break;
1198 }
1199 }
1200 }
1201
1202 /* if we get there, it means we have no rule which matches, or
1203 * we have an explicit accept, so we apply the default accept.
1204 */
1205 rep->analysers &= ~an_bit;
1206 rep->analyse_exp = TICK_ETERNITY;
1207 return 1;
1208}
1209
1210
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001211/* This function performs the TCP layer4 analysis on the current request. It
1212 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1213 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001214 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001215 */
1216int tcp_exec_req_rules(struct session *s)
1217{
1218 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001219 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001220 struct stktable *t = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001221 struct connection *conn = objt_conn(s->si[0].end);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001222 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001223 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001224
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001225 if (!conn)
1226 return result;
1227
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001228 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001229 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001230
1231 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001232 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001233 ret = acl_pass(ret);
1234 if (rule->cond->pol == ACL_COND_UNLESS)
1235 ret = !ret;
1236 }
1237
1238 if (ret) {
1239 /* we have a matching rule. */
1240 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001241 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001242 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001243 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001244
1245 if (!(s->flags & SN_ERR_MASK))
1246 s->flags |= SN_ERR_PRXCOND;
1247 if (!(s->flags & SN_FINST_MASK))
1248 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001249 result = 0;
1250 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001251 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001252 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001253 /* Note: only the first valid tracking parameter of each
1254 * applies.
1255 */
1256 struct stktable_key *key;
1257
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001258 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001259 continue;
1260
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001261 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreaud0083942014-06-25 16:20:53 +02001262 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001263
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001264 if (key && (ts = stktable_get_entry(t, key)))
1265 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001266 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001267 else if (rule->action == TCP_ACT_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001268 conn->flags |= CO_FL_ACCEPT_PROXY;
1269 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001270 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001271 else {
1272 /* otherwise it's an accept */
1273 break;
1274 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001275 }
1276 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001277 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001278}
1279
Emeric Brun97679e72010-09-23 17:56:44 +02001280/* Parse a tcp-response rule. Return a negative value in case of failure */
1281static int tcp_parse_response_rule(char **args, int arg, int section_type,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001282 struct proxy *curpx, struct proxy *defpx,
1283 struct tcp_rule *rule, char **err,
1284 unsigned int where,
1285 const char *file, int line)
Emeric Brun97679e72010-09-23 17:56:44 +02001286{
1287 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001288 memprintf(err, "%s %s is only allowed in 'backend' sections",
1289 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001290 return -1;
1291 }
1292
1293 if (strcmp(args[arg], "accept") == 0) {
1294 arg++;
1295 rule->action = TCP_ACT_ACCEPT;
1296 }
1297 else if (strcmp(args[arg], "reject") == 0) {
1298 arg++;
1299 rule->action = TCP_ACT_REJECT;
1300 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001301 else if (strcmp(args[arg], "close") == 0) {
1302 arg++;
1303 rule->action = TCP_ACT_CLOSE;
1304 }
Emeric Brun97679e72010-09-23 17:56:44 +02001305 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001306 memprintf(err,
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001307 "'%s %s' expects 'accept', 'close' or 'reject' in %s '%s' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001308 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001309 return -1;
1310 }
1311
1312 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001313 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001314 memprintf(err,
1315 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1316 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001317 return -1;
1318 }
1319 }
1320 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001321 memprintf(err,
1322 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001323 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1324 return -1;
1325 }
1326 return 0;
1327}
1328
1329
1330
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001331/* Parse a tcp-request rule. Return a negative value in case of failure */
1332static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001333 struct proxy *curpx, struct proxy *defpx,
1334 struct tcp_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001335 unsigned int where, const char *file, int line)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001336{
1337 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001338 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1339 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001340 return -1;
1341 }
1342
1343 if (!strcmp(args[arg], "accept")) {
1344 arg++;
1345 rule->action = TCP_ACT_ACCEPT;
1346 }
1347 else if (!strcmp(args[arg], "reject")) {
1348 arg++;
1349 rule->action = TCP_ACT_REJECT;
1350 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001351 else if (strcmp(args[arg], "capture") == 0) {
1352 struct sample_expr *expr;
1353 struct cap_hdr *hdr;
1354 int kw = arg;
1355 int len = 0;
1356
1357 if (!(curpx->cap & PR_CAP_FE)) {
1358 memprintf(err,
1359 "'%s %s %s' : proxy '%s' has no frontend capability",
1360 args[0], args[1], args[kw], curpx->id);
1361 return -1;
1362 }
1363
1364 if (!(where & SMP_VAL_FE_REQ_CNT)) {
1365 memprintf(err,
1366 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1367 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1368 return -1;
1369 }
1370
1371 arg++;
1372
1373 curpx->conf.args.ctx = ARGC_CAP;
1374 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
1375 if (!expr) {
1376 memprintf(err,
1377 "'%s %s %s' : %s",
1378 args[0], args[1], args[kw], *err);
1379 return -1;
1380 }
1381
1382 if (!(expr->fetch->val & where)) {
1383 memprintf(err,
1384 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
1385 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
1386 free(expr);
1387 return -1;
1388 }
1389
1390 if (strcmp(args[arg], "len") == 0) {
1391 arg++;
1392 if (!args[arg]) {
1393 memprintf(err,
1394 "'%s %s %s' : missing length value",
1395 args[0], args[1], args[kw]);
1396 free(expr);
1397 return -1;
1398 }
1399 /* we copy the table name for now, it will be resolved later */
1400 len = atoi(args[arg]);
1401 if (len <= 0) {
1402 memprintf(err,
1403 "'%s %s %s' : length must be > 0",
1404 args[0], args[1], args[kw]);
1405 free(expr);
1406 return -1;
1407 }
1408 arg++;
1409 }
1410
1411 if (!len) {
1412 memprintf(err,
1413 "'%s %s %s' : a positive 'len' argument is mandatory",
1414 args[0], args[1], args[kw]);
1415 free(expr);
1416 return -1;
1417 }
1418
1419 hdr = calloc(sizeof(struct cap_hdr), 1);
1420 hdr->next = curpx->req_cap;
1421 hdr->name = NULL; /* not a header capture */
1422 hdr->namelen = 0;
1423 hdr->len = len;
1424 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
1425 hdr->index = curpx->nb_req_cap++;
1426
1427 curpx->req_cap = hdr;
1428 curpx->to_log |= LW_REQHDR;
1429
1430 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
1431 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1432
1433 rule->act_prm.cap.expr = expr;
1434 rule->act_prm.cap.hdr = hdr;
1435 rule->action = TCP_ACT_CAPTURE;
1436 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001437 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1438 args[arg][9] == '\0' && args[arg][8] >= '0' &&
Willy Tarreau0363a152014-10-17 11:53:05 +02001439 args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001440 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001441 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001442
1443 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001444
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001445 curpx->conf.args.ctx = ARGC_TRK;
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001446 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001447 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001448 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001449 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001450 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001451 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001452 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001453
Willy Tarreau80aca902013-01-07 15:42:20 +01001454 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001455 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001456 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001457 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001458 free(expr);
1459 return -1;
1460 }
1461
1462 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001463 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001464
1465 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001466 arg++;
1467 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001468 memprintf(err,
1469 "'%s %s %s' : missing table name",
1470 args[0], args[1], args[kw]);
1471 free(expr);
1472 return -1;
1473 }
1474 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001475 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001476 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001477 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001478 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001479 rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001480 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001481 else if (strcmp(args[arg], "expect-proxy") == 0) {
1482 if (strcmp(args[arg+1], "layer4") != 0) {
1483 memprintf(err,
1484 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1485 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1486 return -1;
1487 }
1488
1489 if (!(where & SMP_VAL_FE_CON_ACC)) {
1490 memprintf(err,
1491 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1492 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1493 return -1;
1494 }
1495
1496 arg += 2;
1497 rule->action = TCP_ACT_EXPECT_PX;
1498 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001499 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001500 memprintf(err,
Willy Tarreaub4c84932013-07-23 19:15:30 +02001501 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d' "
1502 " in %s '%s' (got '%s')",
Willy Tarreau0363a152014-10-17 11:53:05 +02001503 args[0], args[1], MAX_SESS_STKCTR-1, proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001504 return -1;
1505 }
1506
1507 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001508 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001509 memprintf(err,
1510 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1511 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001512 return -1;
1513 }
1514 }
1515 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001516 memprintf(err,
1517 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001518 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1519 return -1;
1520 }
1521 return 0;
1522}
1523
Emeric Brun97679e72010-09-23 17:56:44 +02001524/* This function should be called to parse a line starting with the "tcp-response"
1525 * keyword.
1526 */
1527static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001528 struct proxy *defpx, const char *file, int line,
1529 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001530{
1531 const char *ptr = NULL;
1532 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001533 int warn = 0;
1534 int arg;
1535 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001536 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001537 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001538 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001539
1540 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001541 memprintf(err, "missing argument for '%s' in %s '%s'",
1542 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001543 return -1;
1544 }
1545
1546 if (strcmp(args[1], "inspect-delay") == 0) {
1547 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001548 memprintf(err, "%s %s is only allowed in 'backend' sections",
1549 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001550 return -1;
1551 }
1552
1553 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001554 memprintf(err,
1555 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1556 args[0], args[1], proxy_type_str(curpx), curpx->id);
1557 if (ptr)
1558 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001559 return -1;
1560 }
1561
1562 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001563 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1564 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001565 return 1;
1566 }
1567 curpx->tcp_rep.inspect_delay = val;
1568 return 0;
1569 }
1570
Simon Hormande072bd2011-06-24 15:11:37 +09001571 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001572 LIST_INIT(&rule->list);
1573 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001574 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001575
1576 if (strcmp(args[1], "content") == 0) {
1577 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001578
1579 if (curpx->cap & PR_CAP_FE)
1580 where |= SMP_VAL_FE_RES_CNT;
1581 if (curpx->cap & PR_CAP_BE)
1582 where |= SMP_VAL_BE_RES_CNT;
1583
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001584 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001585 goto error;
1586
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001587 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1588 if (acl) {
1589 if (acl->name && *acl->name)
1590 memprintf(err,
1591 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1592 acl->name, args[0], args[1], sample_ckp_names(where));
1593 else
1594 memprintf(err,
1595 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1596 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001597 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001598 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001599
Emeric Brun97679e72010-09-23 17:56:44 +02001600 warn++;
1601 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001602 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1603 if (acl->name && *acl->name)
1604 memprintf(err,
1605 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001606 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001607 else
1608 memprintf(err,
1609 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001610 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001611 warn++;
1612 }
Emeric Brun97679e72010-09-23 17:56:44 +02001613
1614 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1615 }
1616 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001617 memprintf(err,
1618 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1619 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001620 goto error;
1621 }
1622
1623 return warn;
1624 error:
1625 free(rule);
1626 return -1;
1627}
1628
1629
Willy Tarreaub6866442008-07-14 23:54:42 +02001630/* This function should be called to parse a line starting with the "tcp-request"
1631 * keyword.
1632 */
1633static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001634 struct proxy *defpx, const char *file, int line,
1635 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001636{
1637 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001638 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001639 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001640 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001641 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001642 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001643 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001644 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001645
1646 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001647 if (curpx == defpx)
1648 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1649 else
1650 memprintf(err, "missing argument for '%s' in %s '%s'",
1651 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001652 return -1;
1653 }
1654
1655 if (!strcmp(args[1], "inspect-delay")) {
1656 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001657 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1658 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001659 return -1;
1660 }
1661
Willy Tarreaub6866442008-07-14 23:54:42 +02001662 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001663 memprintf(err,
1664 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1665 args[0], args[1], proxy_type_str(curpx), curpx->id);
1666 if (ptr)
1667 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001668 return -1;
1669 }
1670
1671 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001672 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1673 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001674 return 1;
1675 }
1676 curpx->tcp_req.inspect_delay = val;
1677 return 0;
1678 }
1679
Simon Hormande072bd2011-06-24 15:11:37 +09001680 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001681 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001682 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001683 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001684
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001685 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001686 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001687
1688 if (curpx->cap & PR_CAP_FE)
1689 where |= SMP_VAL_FE_REQ_CNT;
1690 if (curpx->cap & PR_CAP_BE)
1691 where |= SMP_VAL_BE_REQ_CNT;
1692
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001693 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001694 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001695
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001696 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1697 if (acl) {
1698 if (acl->name && *acl->name)
1699 memprintf(err,
1700 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1701 acl->name, args[0], args[1], sample_ckp_names(where));
1702 else
1703 memprintf(err,
1704 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1705 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001706 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001707 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001708
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001709 warn++;
1710 }
1711 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1712 if (acl->name && *acl->name)
1713 memprintf(err,
1714 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001715 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001716 else
1717 memprintf(err,
1718 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001719 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001720 warn++;
1721 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001722
Willy Tarreau8b3c8082014-09-16 15:39:51 +02001723 /* the following function directly emits the warning */
1724 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001725 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001726 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001727 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001728 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001729
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001730 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001731 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1732 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001733 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001734 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001735
Willy Tarreau80aca902013-01-07 15:42:20 +01001736 where |= SMP_VAL_FE_CON_ACC;
1737
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001738 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001739 goto error;
1740
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001741 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1742 if (acl) {
1743 if (acl->name && *acl->name)
1744 memprintf(err,
1745 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1746 acl->name, args[0], args[1], sample_ckp_names(where));
1747 else
1748 memprintf(err,
1749 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1750 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001751 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001752 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001753
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001754 warn++;
1755 }
1756 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1757 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001758 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001759 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001760 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001761 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001762 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001763 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001764 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001765 warn++;
1766 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001767
Willy Tarreau8b3c8082014-09-16 15:39:51 +02001768 /* the following function directly emits the warning */
1769 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001770 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001771 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001772 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001773 if (curpx == defpx)
1774 memprintf(err,
1775 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1776 args[0], args[1]);
1777 else
1778 memprintf(err,
1779 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001780 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001781 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001782 }
1783
Willy Tarreau1a687942010-05-23 22:40:30 +02001784 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001785 error:
1786 free(rule);
1787 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001788}
1789
Willy Tarreau645513a2010-05-24 20:55:15 +02001790
1791/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001792/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001793/************************************************************************/
1794
Willy Tarreau4a129812012-04-25 17:31:42 +02001795/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001796static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001797smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001798 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001799{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001800 struct connection *cli_conn = objt_conn(l4->si[0].end);
1801
1802 if (!cli_conn)
1803 return 0;
1804
1805 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001806 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001807 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001808 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001809 break;
1810 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001811 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001812 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001813 break;
1814 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001815 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001816 }
Emeric Brunf769f512010-10-22 17:14:01 +02001817
Willy Tarreau37406352012-04-23 16:16:37 +02001818 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001819 return 1;
1820}
1821
Willy Tarreaua5e37562011-12-16 17:06:15 +01001822/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001823static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001824smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001825 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001826{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001827 struct connection *cli_conn = objt_conn(l4->si[0].end);
1828
1829 if (!cli_conn)
1830 return 0;
1831
Willy Tarreauf853c462012-04-23 18:53:56 +02001832 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001833 if (!(smp->data.uint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001834 return 0;
1835
Willy Tarreau37406352012-04-23 16:16:37 +02001836 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001837 return 1;
1838}
1839
Willy Tarreau4a129812012-04-25 17:31:42 +02001840/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001841static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001842smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001843 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001844{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001845 struct connection *cli_conn = objt_conn(l4->si[0].end);
Willy Tarreau645513a2010-05-24 20:55:15 +02001846
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001847 if (!cli_conn)
1848 return 0;
1849
1850 conn_get_to_addr(cli_conn);
1851
1852 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001853 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001854 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001855 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001856 break;
1857 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001858 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001859 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001860 break;
1861 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001862 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001863 }
Emeric Brunf769f512010-10-22 17:14:01 +02001864
Willy Tarreau37406352012-04-23 16:16:37 +02001865 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001866 return 1;
1867}
1868
Willy Tarreaua5e37562011-12-16 17:06:15 +01001869/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001870static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001871smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001872 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001873{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001874 struct connection *cli_conn = objt_conn(l4->si[0].end);
1875
1876 if (!cli_conn)
1877 return 0;
1878
1879 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001880
Willy Tarreauf853c462012-04-23 18:53:56 +02001881 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001882 if (!(smp->data.uint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001883 return 0;
1884
Willy Tarreau37406352012-04-23 16:16:37 +02001885 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001886 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001887}
1888
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001889#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001890/* parse the "v4v6" bind keyword */
1891static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1892{
1893 struct listener *l;
1894
1895 list_for_each_entry(l, &conf->listeners, by_bind) {
1896 if (l->addr.ss_family == AF_INET6)
1897 l->options |= LI_O_V4V6;
1898 }
1899
1900 return 0;
1901}
1902
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001903/* parse the "v6only" bind keyword */
1904static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1905{
1906 struct listener *l;
1907
1908 list_for_each_entry(l, &conf->listeners, by_bind) {
1909 if (l->addr.ss_family == AF_INET6)
1910 l->options |= LI_O_V6ONLY;
1911 }
1912
1913 return 0;
1914}
1915#endif
1916
Pieter Baauwd551fb52013-05-08 22:49:23 +02001917#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001918/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001919static 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 +02001920{
1921 struct listener *l;
1922
Willy Tarreau4348fad2012-09-20 16:48:07 +02001923 list_for_each_entry(l, &conf->listeners, by_bind) {
1924 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1925 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001926 }
1927
Willy Tarreau44791242012-09-12 23:27:21 +02001928 return 0;
1929}
1930#endif
1931
1932#ifdef TCP_DEFER_ACCEPT
1933/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001934static 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 +02001935{
1936 struct listener *l;
1937
Willy Tarreau4348fad2012-09-20 16:48:07 +02001938 list_for_each_entry(l, &conf->listeners, by_bind) {
1939 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1940 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001941 }
1942
Willy Tarreau44791242012-09-12 23:27:21 +02001943 return 0;
1944}
1945#endif
1946
Willy Tarreau1c862c52012-10-05 16:21:00 +02001947#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001948/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001949static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1950{
1951 struct listener *l;
1952
1953 list_for_each_entry(l, &conf->listeners, by_bind) {
1954 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1955 l->options |= LI_O_TCP_FO;
1956 }
1957
1958 return 0;
1959}
1960#endif
1961
Willy Tarreau44791242012-09-12 23:27:21 +02001962#ifdef TCP_MAXSEG
1963/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001964static 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 +02001965{
1966 struct listener *l;
1967 int mss;
1968
Willy Tarreau44791242012-09-12 23:27:21 +02001969 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001970 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001971 return ERR_ALERT | ERR_FATAL;
1972 }
1973
1974 mss = atoi(args[cur_arg + 1]);
1975 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001976 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001977 return ERR_ALERT | ERR_FATAL;
1978 }
1979
Willy Tarreau4348fad2012-09-20 16:48:07 +02001980 list_for_each_entry(l, &conf->listeners, by_bind) {
1981 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1982 l->maxseg = mss;
1983 }
Willy Tarreau44791242012-09-12 23:27:21 +02001984
1985 return 0;
1986}
1987#endif
1988
1989#ifdef SO_BINDTODEVICE
1990/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001991static 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 +02001992{
1993 struct listener *l;
1994
Willy Tarreau44791242012-09-12 23:27:21 +02001995 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001996 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001997 return ERR_ALERT | ERR_FATAL;
1998 }
1999
Willy Tarreau4348fad2012-09-20 16:48:07 +02002000 list_for_each_entry(l, &conf->listeners, by_bind) {
2001 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2002 l->interface = strdup(args[cur_arg + 1]);
2003 }
Willy Tarreau44791242012-09-12 23:27:21 +02002004
2005 global.last_checks |= LSTCHK_NETADM;
2006 return 0;
2007}
2008#endif
2009
Willy Tarreaudc13c112013-06-21 23:16:39 +02002010static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002011 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02002012 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02002013 { 0, NULL, NULL },
2014}};
2015
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002016
Willy Tarreau61612d42012-04-19 18:42:05 +02002017/* Note: must not be declared <const> as its list will be overwritten.
2018 * Please take care of keeping this list alphabetically sorted.
2019 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002020static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002021 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02002022}};
2023
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002024
Willy Tarreau4a129812012-04-25 17:31:42 +02002025/* Note: must not be declared <const> as its list will be overwritten.
2026 * Note: fetches that may return multiple types must be declared as the lowest
2027 * common denominator, the type that can be casted into all other ones. For
2028 * instance v4/v6 must be declared v4.
2029 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002030static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002031 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
2032 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
2033 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
2034 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
2035 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002036}};
2037
Willy Tarreau44791242012-09-12 23:27:21 +02002038/************************************************************************/
2039/* All supported bind keywords must be declared here. */
2040/************************************************************************/
2041
2042/* Note: must not be declared <const> as its list will be overwritten.
2043 * Please take care of keeping this list alphabetically sorted, doing so helps
2044 * all code contributors.
2045 * Optional keywords are also declared with a NULL ->parse() function so that
2046 * the config parser can report an appropriate error when a known keyword was
2047 * not enabled.
2048 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002049static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02002050#ifdef TCP_DEFER_ACCEPT
2051 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
2052#endif
2053#ifdef SO_BINDTODEVICE
2054 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
2055#endif
2056#ifdef TCP_MAXSEG
2057 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
2058#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02002059#ifdef TCP_FASTOPEN
2060 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
2061#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02002062#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002063 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
2064#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002065#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002066 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002067 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
2068#endif
Willy Tarreau44791242012-09-12 23:27:21 +02002069 /* the versions with the NULL parse function*/
2070 { "defer-accept", NULL, 0 },
2071 { "interface", NULL, 1 },
2072 { "mss", NULL, 1 },
2073 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002074 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002075 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002076 { NULL, NULL, 0 },
2077}};
2078
Willy Tarreaue6b98942007-10-29 01:09:36 +01002079__attribute__((constructor))
2080static void __tcp_protocol_init(void)
2081{
2082 protocol_register(&proto_tcpv4);
2083 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002084 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02002085 cfg_register_keywords(&cfg_kws);
2086 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02002087 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002088}
2089
2090
2091/*
2092 * Local variables:
2093 * c-indent-level: 8
2094 * c-basic-offset: 8
2095 * End:
2096 */