blob: b890d4b5d56907166b4e06cf4169d3807a895178 [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>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010036#include <common/namespace.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010037
Willy Tarreaue6b98942007-10-29 01:09:36 +010038#include <types/global.h>
Willy Tarreau18bf01e2014-06-13 16:18:52 +020039#include <types/capture.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020040#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010041
42#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020043#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020044#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020045#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020046#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020047#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020048#include <proto/log.h>
49#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/protocol.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010051#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020052#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020053#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020054#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020055#include <proto/stick_table.h>
Willy Tarreaucc1e04b2013-09-11 23:20:29 +020056#include <proto/stream_interface.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020057#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010058
Willy Tarreaue8c66af2008-01-13 18:40:14 +010059#ifdef CONFIG_HAP_CTTPROXY
60#include <import/ip_tproxy.h>
61#endif
62
Emeric Bruncf20bf12010-10-22 16:06:11 +020063static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
64static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010065
66/* Note: must not be declared <const> as its list will be overwritten */
67static struct protocol proto_tcpv4 = {
68 .name = "tcpv4",
69 .sock_domain = AF_INET,
70 .sock_type = SOCK_STREAM,
71 .sock_prot = IPPROTO_TCP,
72 .sock_family = AF_INET,
73 .sock_addrlen = sizeof(struct sockaddr_in),
74 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020075 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020076 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020077 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010078 .bind_all = tcp_bind_listeners,
79 .unbind_all = unbind_all_listeners,
80 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020081 .get_src = tcp_get_src,
82 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +020083 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +020084 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010085 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
86 .nb_listeners = 0,
87};
88
89/* Note: must not be declared <const> as its list will be overwritten */
90static struct protocol proto_tcpv6 = {
91 .name = "tcpv6",
92 .sock_domain = AF_INET6,
93 .sock_type = SOCK_STREAM,
94 .sock_prot = IPPROTO_TCP,
95 .sock_family = AF_INET6,
96 .sock_addrlen = sizeof(struct sockaddr_in6),
97 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020098 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020099 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +0200100 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100101 .bind_all = tcp_bind_listeners,
102 .unbind_all = unbind_all_listeners,
103 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +0200104 .get_src = tcp_get_src,
105 .get_dst = tcp_get_dst,
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200106 .drain = tcp_drain,
Willy Tarreau092d8652014-07-07 20:22:12 +0200107 .pause = tcp_pause_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100108 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
109 .nb_listeners = 0,
110};
111
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100112
David du Colombier6f5ccb12011-03-10 22:26:24 +0100113/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100114 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
115 * - 0 : ignore remote address (may even be a NULL pointer)
116 * - 1 : use provided address
117 * - 2 : use provided port
118 * - 3 : use both
119 *
120 * The function supports multiple foreign binding methods :
121 * - linux_tproxy: we directly bind to the foreign address
122 * - cttproxy: we bind to a local address then nat.
123 * The second one can be used as a fallback for the first one.
124 * This function returns 0 when everything's OK, 1 if it could not bind, to the
125 * local address, 2 if it could not bind to the foreign address.
126 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100127int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100128{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100129 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100130 int foreign_ok = 0;
131 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100132 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200133 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200134
David du Colombier65c17962012-07-13 14:34:59 +0200135 switch (local->ss_family) {
136 case AF_INET:
137 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200138 /* This deserves some explanation. Some platforms will support
139 * multiple combinations of certain methods, so we try the
140 * supported ones until one succeeds.
141 */
142 if (0
143#if defined(IP_TRANSPARENT)
144 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
145#endif
146#if defined(IP_FREEBIND)
147 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
148#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200149#if defined(IP_BINDANY)
150 || (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == 0)
151#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200152#if defined(SO_BINDANY)
153 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
154#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200155 )
David du Colombier65c17962012-07-13 14:34:59 +0200156 foreign_ok = 1;
157 else
158 ip_transp_working = 0;
159 }
160 break;
161 case AF_INET6:
162 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200163 if (0
164#if defined(IPV6_TRANSPARENT)
165 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
166#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100167#if defined(IP_FREEBIND)
168 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
169#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200170#if defined(IPV6_BINDANY)
171 || (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == 0)
172#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200173#if defined(SO_BINDANY)
174 || (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == 0)
175#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200176 )
David du Colombier65c17962012-07-13 14:34:59 +0200177 foreign_ok = 1;
178 else
179 ip6_transp_working = 0;
180 }
181 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100182 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200183
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100184 if (flags) {
185 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200186 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100187 switch (remote->ss_family) {
188 case AF_INET:
189 if (flags & 1)
190 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
191 if (flags & 2)
192 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
193 break;
194 case AF_INET6:
195 if (flags & 1)
196 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
197 if (flags & 2)
198 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
199 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100200 default:
201 /* we don't want to try to bind to an unknown address family */
202 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100203 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100204 }
205
Simon Hormande072bd2011-06-24 15:11:37 +0900206 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100207 if (foreign_ok) {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200208 if (is_inet_addr(&bind_addr)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200209 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
210 if (ret < 0)
211 return 2;
212 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100213 }
214 else {
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200215 if (is_inet_addr(local)) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200216 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
217 if (ret < 0)
218 return 1;
219 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100220 }
221
222 if (!flags)
223 return 0;
224
225#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100226 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100227 struct in_tproxy itp1, itp2;
228 memset(&itp1, 0, sizeof(itp1));
229
230 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100231 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
232 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100233
234 /* set connect flag on socket */
235 itp2.op = TPROXY_FLAGS;
236 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
237
238 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
239 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
240 foreign_ok = 1;
241 }
242 }
243#endif
244 if (!foreign_ok)
245 /* we could not bind to a foreign address */
246 return 2;
247
248 return 0;
249}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100250
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100251static int create_server_socket(struct connection *conn)
252{
Willy Tarreau529c1392014-12-24 13:47:55 +0100253 const struct netns_entry *ns = NULL;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100254
Willy Tarreau529c1392014-12-24 13:47:55 +0100255#ifdef CONFIG_HAP_NS
256 if (objt_server(conn->target)) {
257 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
258 ns = conn->proxy_netns;
259 else
260 ns = __objt_server(conn->target)->netns;
261 }
262#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100263 return my_socketat(ns, conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP);
264}
Willy Tarreau9650f372009-08-16 14:02:45 +0200265
266/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200267 * This function initiates a TCP connection establishment to the target assigned
268 * to connection <conn> using (si->{target,addr.to}). A source address may be
269 * pointed to by conn->addr.from in case of transparent proxying. Normal source
270 * bind addresses are still determined locally (due to the possible need of a
271 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100272 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100273 * supported. The <data> parameter is a boolean indicating whether there are data
274 * waiting for being sent or not, in order to adjust data write polling and on
275 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
276 * allows the caller to force using a delayed ACK when establishing the connection :
277 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
278 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
279 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200280 *
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200281 * Note that a pending send_proxy message accounts for data.
282 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200283 * It can return one of :
284 * - SN_ERR_NONE if everything's OK
285 * - SN_ERR_SRVTO if there are no more servers
286 * - SN_ERR_SRVCL if the connection was refused by the server
287 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
288 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
289 * - SN_ERR_INTERNAL for any other purely internal errors
290 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100291 *
292 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
293 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200294 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100295
Willy Tarreauf0837b22012-11-24 10:24:27 +0100296int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200297{
298 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100299 struct server *srv;
300 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100301 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100302
Willy Tarreau9ce70132014-01-24 16:08:19 +0100303 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
304
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100305 switch (obj_type(conn->target)) {
306 case OBJ_TYPE_PROXY:
307 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100308 srv = NULL;
309 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100310 case OBJ_TYPE_SERVER:
311 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100312 be = srv->proxy;
313 break;
314 default:
Willy Tarreau9ce70132014-01-24 16:08:19 +0100315 conn->flags |= CO_FL_ERROR;
Willy Tarreauac825402011-03-04 22:04:29 +0100316 return SN_ERR_INTERNAL;
317 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200318
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100319 fd = conn->t.sock.fd = create_server_socket(conn);
320
321 if (fd == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200322 qfprintf(stderr, "Cannot get a server socket.\n");
323
Willy Tarreau9ce70132014-01-24 16:08:19 +0100324 if (errno == ENFILE) {
325 conn->err_code = CO_ER_SYS_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200326 send_log(be, LOG_EMERG,
327 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
328 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100329 }
330 else if (errno == EMFILE) {
331 conn->err_code = CO_ER_PROC_FDLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200332 send_log(be, LOG_EMERG,
333 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
334 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100335 }
336 else if (errno == ENOBUFS || errno == ENOMEM) {
337 conn->err_code = CO_ER_SYS_MEMLIM;
Willy Tarreau9650f372009-08-16 14:02:45 +0200338 send_log(be, LOG_EMERG,
339 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
340 be->id, maxfd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100341 }
342 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
343 conn->err_code = CO_ER_NOPROTO;
344 }
345 else
346 conn->err_code = CO_ER_SOCK_ERR;
347
Willy Tarreau9650f372009-08-16 14:02:45 +0200348 /* this is a resource error */
Willy Tarreau9ce70132014-01-24 16:08:19 +0100349 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200350 return SN_ERR_RESOURCE;
351 }
352
353 if (fd >= global.maxsock) {
354 /* do not log anything there, it's a normal condition when this option
355 * is used to serialize connections to a server !
356 */
357 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
358 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100359 conn->err_code = CO_ER_CONF_FDLIM;
360 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200361 return SN_ERR_PRXCOND; /* it is a configuration limit */
362 }
363
364 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900365 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200366 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
367 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100368 conn->err_code = CO_ER_SOCK_ERR;
369 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200370 return SN_ERR_INTERNAL;
371 }
372
373 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900374 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200375
Willy Tarreau9650f372009-08-16 14:02:45 +0200376 /* allow specific binding :
377 * - server-specific at first
378 * - proxy-specific next
379 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100380 if (srv && srv->conn_src.opts & CO_SRC_BIND)
381 src = &srv->conn_src;
382 else if (be->conn_src.opts & CO_SRC_BIND)
383 src = &be->conn_src;
384 else
385 src = NULL;
386
387 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200388 int ret, flags = 0;
389
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200390 if (is_inet_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100391 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100392 case CO_SRC_TPROXY_ADDR:
393 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200394 flags = 3;
395 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100396 case CO_SRC_TPROXY_CIP:
397 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200398 flags = 1;
399 break;
400 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200401 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200402
Willy Tarreau9650f372009-08-16 14:02:45 +0200403#ifdef SO_BINDTODEVICE
404 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100405 if (src->iface_name)
406 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200407#endif
408
Willy Tarreaua4380b42012-12-08 22:49:11 +0100409 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200410 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100411 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200412
413 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100414 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200415
416 do {
417 /* note: in case of retry, we may have to release a previously
418 * allocated port, hence this loop's construct.
419 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200420 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
421 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200422
423 if (!attempts)
424 break;
425 attempts--;
426
Willy Tarreaua4380b42012-12-08 22:49:11 +0100427 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100428 if (!fdinfo[fd].local_port) {
429 conn->err_code = CO_ER_PORT_RANGE;
Willy Tarreau9650f372009-08-16 14:02:45 +0200430 break;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100431 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200432
Willy Tarreaua4380b42012-12-08 22:49:11 +0100433 fdinfo[fd].port_range = src->sport_range;
434 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200435
Willy Tarreaua4380b42012-12-08 22:49:11 +0100436 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100437 if (ret != 0)
438 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200439 } while (ret != 0); /* binding NOK */
440 }
441 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100442 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100443 if (ret != 0)
444 conn->err_code = CO_ER_CANT_BIND;
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 }
446
Willy Tarreaua4380b42012-12-08 22:49:11 +0100447 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200448 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
449 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200450 close(fd);
451
Willy Tarreau9650f372009-08-16 14:02:45 +0200452 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100453 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200454 be->id);
455 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100456 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200457 be->id);
458 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100459 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200460 be->id);
461 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100462 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200463 be->id);
464 }
Willy Tarreau9ce70132014-01-24 16:08:19 +0100465 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200466 return SN_ERR_RESOURCE;
467 }
468 }
469
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400470#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200471 /* disabling tcp quick ack now allows the first request to leave the
472 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100473 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200474 */
Willy Tarreaufb20e462014-10-24 12:02:24 +0200475 if (delack == 2 || ((delack || data || conn->send_proxy_ofs) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900476 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200477#endif
478
Willy Tarreaue803de22010-01-21 17:43:04 +0100479 if (global.tune.server_sndbuf)
480 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
481
482 if (global.tune.server_rcvbuf)
483 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
484
Willy Tarreau986a9d22012-08-30 21:11:38 +0200485 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200486 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
487
Willy Tarreaub1719512012-12-08 23:03:28 +0100488 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200489 char *msg;
Willy Tarreau9ce70132014-01-24 16:08:19 +0100490 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200491 msg = "no free ports";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100492 conn->err_code = CO_ER_FREE_PORTS;
493 }
494 else {
Willy Tarreau9650f372009-08-16 14:02:45 +0200495 msg = "local address already in use";
Willy Tarreau9ce70132014-01-24 16:08:19 +0100496 conn->err_code = CO_ER_ADDR_INUSE;
497 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200498
Willy Tarreaub1719512012-12-08 23:03:28 +0100499 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
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 Tarreaub1719512012-12-08 23:03:28 +0100503 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100504 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200505 return SN_ERR_RESOURCE;
506 } else if (errno == ETIMEDOUT) {
507 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200508 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
509 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200510 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100511 conn->err_code = CO_ER_SOCK_ERR;
512 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200513 return SN_ERR_SRVTO;
514 } else {
515 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
516 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200517 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
518 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200519 close(fd);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100520 conn->err_code = CO_ER_SOCK_ERR;
521 conn->flags |= CO_FL_ERROR;
Willy Tarreau9650f372009-08-16 14:02:45 +0200522 return SN_ERR_SRVCL;
523 }
524 }
525
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100526 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200527
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200528 /* Prepare to send a few handshakes related to the on-wire protocol. */
529 if (conn->send_proxy_ofs)
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200530 conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreau1ec74bf2013-10-24 21:45:00 +0200531
Willy Tarreauf79c8172013-10-21 16:30:56 +0200532 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100533 fdtab[fd].linger_risk = 1; /* close hard if needed */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200534 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200535
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200536 if (conn_xprt_init(conn) < 0) {
Willy Tarreauf79c8172013-10-21 16:30:56 +0200537 conn_force_close(conn);
Willy Tarreau9ce70132014-01-24 16:08:19 +0100538 conn->flags |= CO_FL_ERROR;
Willy Tarreau15678ef2012-08-31 13:54:11 +0200539 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200540 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200541
Willy Tarreau14f8e862012-08-30 22:23:13 +0200542 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200543 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200544
Willy Tarreau9650f372009-08-16 14:02:45 +0200545 return SN_ERR_NONE; /* connection is OK */
546}
547
548
Willy Tarreau59b94792012-05-11 16:16:40 +0200549/*
550 * Retrieves the source address for the socket <fd>, with <dir> indicating
551 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
552 * success, -1 in case of error. The socket's source address is stored in
553 * <sa> for <salen> bytes.
554 */
555int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
556{
557 if (dir)
558 return getsockname(fd, sa, &salen);
559 else
560 return getpeername(fd, sa, &salen);
561}
562
563
564/*
565 * Retrieves the original destination address for the socket <fd>, with <dir>
566 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
567 * listener, if the original destination address was translated, the original
568 * address is retrieved. It returns 0 in case of success, -1 in case of error.
569 * The socket's source address is stored in <sa> for <salen> bytes.
570 */
571int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
572{
573 if (dir)
574 return getpeername(fd, sa, &salen);
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100575 else {
576 int ret = getsockname(fd, sa, &salen);
577
578 if (ret < 0)
579 return ret;
580
Willy Tarreau59b94792012-05-11 16:16:40 +0200581#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100582 /* For TPROXY and Netfilter's NAT, we can retrieve the original
583 * IPv4 address before DNAT/REDIRECT. We must not do that with
584 * other families because v6-mapped IPv4 addresses are still
585 * reported as v4.
586 */
587 if (((struct sockaddr_storage *)sa)->ss_family == AF_INET
588 && getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
589 return 0;
Willy Tarreau59b94792012-05-11 16:16:40 +0200590#endif
Willy Tarreau5e0d0e02014-10-29 21:46:01 +0100591 return ret;
592 }
Willy Tarreau59b94792012-05-11 16:16:40 +0200593}
594
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200595/* Tries to drain any pending incoming data from the socket to reach the
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100596 * receive shutdown. Returns positive if the shutdown was found, negative
597 * if EAGAIN was hit, otherwise zero. This is useful to decide whether we
598 * can close a connection cleanly are we must kill it hard.
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200599 */
600int tcp_drain(int fd)
601{
602 int turns = 2;
603 int len;
604
605 while (turns) {
606#ifdef MSG_TRUNC_CLEARS_INPUT
607 len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC);
608 if (len == -1 && errno == EFAULT)
609#endif
610 len = recv(fd, trash.str, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL);
611
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100612 if (len == 0) {
613 /* cool, shutdown received */
614 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200615 return 1;
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100616 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200617
618 if (len < 0) {
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100619 if (errno == EAGAIN) {
620 /* connection not closed yet */
621 fd_cant_recv(fd);
Willy Tarreau7f4bcc32014-01-20 11:26:12 +0100622 return -1;
Willy Tarreauf817e9f2014-01-10 16:58:45 +0100623 }
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200624 if (errno == EINTR) /* oops, try again */
625 continue;
626 /* other errors indicate a dead connection, fine. */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100627 fdtab[fd].linger_risk = 0;
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200628 return 1;
629 }
630 /* OK we read some data, let's try again once */
631 turns--;
632 }
633 /* some data are still present, give up */
634 return 0;
635}
636
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200637/* This is the callback which is set when a connection establishment is pending
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100638 * and we have nothing to send. It updates the FD polling status. It returns 0
639 * if it fails in a fatal way or needs to poll to go further, otherwise it
640 * returns non-zero and removes the CO_FL_WAIT_L4_CONN flag from the connection's
641 * flags. In case of error, it sets CO_FL_ERROR and leaves the error code in
642 * errno. The error checking is done in two passes in order to limit the number
643 * of syscalls in the normal case :
644 * - if POLL_ERR was reported by the poller, we check for a pending error on
645 * the socket before proceeding. If found, it's assigned to errno so that
646 * upper layers can see it.
647 * - otherwise connect() is used to check the connection state again, since
648 * the getsockopt return cannot reliably be used to know if the connection
649 * is still pending or ready. This one may often return an error as well,
650 * since we don't always have POLL_ERR (eg: OSX or cached events).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200651 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200652int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200653{
Willy Tarreau239d7182012-07-23 18:53:03 +0200654 int fd = conn->t.sock.fd;
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100655 socklen_t lskerr;
656 int skerr;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200657
Willy Tarreau80184712012-07-06 14:54:49 +0200658 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200659 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200660
Willy Tarreau3c728722014-01-23 13:50:42 +0100661 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200662 return 0;
663
Willy Tarreau80184712012-07-06 14:54:49 +0200664 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200665 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200666
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100667 if (!fd_send_ready(fd))
668 return 0;
669
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100670 /* we might be the first witness of FD_POLL_ERR. Note that FD_POLL_HUP
671 * without FD_POLL_IN also indicates a hangup without input data meaning
672 * there was no connection.
673 */
674 if (fdtab[fd].ev & FD_POLL_ERR ||
675 (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP) {
676 skerr = 0;
677 lskerr = sizeof(skerr);
678 getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
679 errno = skerr;
680 if (errno == EAGAIN)
681 errno = 0;
682 if (errno)
683 goto out_error;
684 }
Willy Tarreau2da156f2012-07-23 15:07:23 +0200685
Willy Tarreauf12a20e2013-12-04 16:11:04 +0100686 /* Use connect() to check the state of the socket. This has the
687 * advantage of giving us the following info :
Willy Tarreau2c6be842012-07-06 17:12:34 +0200688 * - error
689 * - connecting (EALREADY, EINPROGRESS)
690 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200691 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200692 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200693 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100694 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +0100695 fd_cant_send(fd);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200696 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200697 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200698
Willy Tarreau2c6be842012-07-06 17:12:34 +0200699 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200700 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200701
Willy Tarreau2c6be842012-07-06 17:12:34 +0200702 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200703 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200704
Willy Tarreau076be252012-07-06 16:02:29 +0200705 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200706 * forward the event to the transport layer which will notify the
707 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200708 */
Willy Tarreau80184712012-07-06 14:54:49 +0200709 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200710 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200711
712 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200713 /* Write error on the file descriptor. Report it to the connection
714 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200715 */
Willy Tarreau3bd3e572014-01-20 11:56:37 +0100716 fdtab[fd].linger_risk = 0;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100717 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100718 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200719 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200720}
721
Willy Tarreau59b94792012-05-11 16:16:40 +0200722
Willy Tarreaue6b98942007-10-29 01:09:36 +0100723/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100724 * an error message in <errmsg> if the message is at most <errlen> bytes long
725 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
726 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100727 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
728 * was alright and that no message was returned. ERR_RETRYABLE means that an
729 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700730 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100731 * the meaning of the error, but just indicate that a message is present which
732 * should be displayed with the respective level. Last, ERR_ABORT indicates
733 * that it's pointless to try to start other listeners. No error message is
734 * returned if errlen is NULL.
735 */
736int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
737{
738 __label__ tcp_return, tcp_close_return;
739 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100740 int ext, ready;
741 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100742 const char *msg = NULL;
743
744 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100745 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100746 *errmsg = 0;
747
748 if (listener->state != LI_ASSIGNED)
749 return ERR_NONE; /* already bound */
750
751 err = ERR_NONE;
752
Willy Tarreau40aa0702013-03-10 23:51:38 +0100753 /* if the listener already has an fd assigned, then we were offered the
754 * fd by an external process (most likely the parent), and we don't want
755 * to create a new socket. However we still want to set a few flags on
756 * the socket.
757 */
758 fd = listener->fd;
759 ext = (fd >= 0);
760
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100761 if (!ext) {
762 fd = my_socketat(listener->netns, listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
763
764 if (fd == -1) {
765 err |= ERR_RETRYABLE | ERR_ALERT;
766 msg = "cannot create listening socket";
767 goto tcp_return;
768 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100769 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100770
Willy Tarreaue6b98942007-10-29 01:09:36 +0100771 if (fd >= global.maxsock) {
772 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
773 msg = "not enough free sockets (raise '-n' parameter)";
774 goto tcp_close_return;
775 }
776
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200777 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100778 err |= ERR_FATAL | ERR_ALERT;
779 msg = "cannot make socket non-blocking";
780 goto tcp_close_return;
781 }
782
Willy Tarreau40aa0702013-03-10 23:51:38 +0100783 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100784 /* not fatal but should be reported */
785 msg = "cannot do so_reuseaddr";
786 err |= ERR_ALERT;
787 }
788
789 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900790 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100791
Willy Tarreaue6b98942007-10-29 01:09:36 +0100792#ifdef SO_REUSEPORT
793 /* OpenBSD supports this. As it's present in old libc versions of Linux,
794 * it might return an error that we will silently ignore.
795 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100796 if (!ext)
797 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100798#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200799
Willy Tarreau40aa0702013-03-10 23:51:38 +0100800 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200801 switch (listener->addr.ss_family) {
802 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200803 if (1
804#if defined(IP_TRANSPARENT)
805 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
806#endif
807#if defined(IP_FREEBIND)
808 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
809#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200810#if defined(IP_BINDANY)
811 && (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) == -1)
812#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200813#if defined(SO_BINDANY)
814 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
815#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200816 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200817 msg = "cannot make listening socket transparent";
818 err |= ERR_ALERT;
819 }
820 break;
821 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200822 if (1
823#if defined(IPV6_TRANSPARENT)
824 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
825#endif
Lukas Tribus7640e722014-03-03 21:10:51 +0100826#if defined(IP_FREEBIND)
827 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
828#endif
Pieter Baauwff30b662013-05-08 23:22:39 +0200829#if defined(IPV6_BINDANY)
830 && (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) == -1)
831#endif
Pieter Baauw1eb75922013-05-08 23:30:23 +0200832#if defined(SO_BINDANY)
833 && (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) == -1)
834#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200835 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200836 msg = "cannot make listening socket transparent";
837 err |= ERR_ALERT;
838 }
839 break;
840 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100841 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200842
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100843#ifdef SO_BINDTODEVICE
844 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100845 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100846 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100847 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100848 msg = "cannot bind listener to device";
849 err |= ERR_WARN;
850 }
851 }
852#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400853#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100854 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400855 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200856 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
857 msg = "cannot set MSS";
858 err |= ERR_WARN;
859 }
860 }
861#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +0100862#if defined(TCP_USER_TIMEOUT)
863 if (listener->tcp_ut) {
864 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
865 &listener->tcp_ut, sizeof(listener->tcp_ut)) == -1) {
866 msg = "cannot set TCP User Timeout";
867 err |= ERR_WARN;
868 }
869 }
870#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200871#if defined(TCP_DEFER_ACCEPT)
872 if (listener->options & LI_O_DEF_ACCEPT) {
873 /* defer accept by up to one second */
874 int accept_delay = 1;
875 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
876 msg = "cannot enable DEFER_ACCEPT";
877 err |= ERR_WARN;
878 }
879 }
880#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200881#if defined(TCP_FASTOPEN)
882 if (listener->options & LI_O_TCP_FO) {
883 /* TFO needs a queue length, let's use the configured backlog */
884 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
885 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
886 msg = "cannot enable TCP_FASTOPEN";
887 err |= ERR_WARN;
888 }
889 }
890#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100891#if defined(IPV6_V6ONLY)
892 if (listener->options & LI_O_V6ONLY)
893 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100894 else if (listener->options & LI_O_V4V6)
895 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100896#endif
897
Willy Tarreau40aa0702013-03-10 23:51:38 +0100898 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100899 err |= ERR_RETRYABLE | ERR_ALERT;
900 msg = "cannot bind socket";
901 goto tcp_close_return;
902 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100903
Willy Tarreau40aa0702013-03-10 23:51:38 +0100904 ready = 0;
905 ready_len = sizeof(ready);
906 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
907 ready = 0;
908
909 if (!(ext && ready) && /* only listen if not already done by external process */
910 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100911 err |= ERR_RETRYABLE | ERR_ALERT;
912 msg = "cannot listen to socket";
913 goto tcp_close_return;
914 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100915
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400916#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200917 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900918 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200919#endif
920
Willy Tarreaue6b98942007-10-29 01:09:36 +0100921 /* the socket is ready */
922 listener->fd = fd;
923 listener->state = LI_LISTEN;
924
Willy Tarreaueabf3132008-08-29 23:36:51 +0200925 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200926 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200927 fd_insert(fd);
928
Willy Tarreaue6b98942007-10-29 01:09:36 +0100929 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100930 if (msg && errlen) {
931 char pn[INET6_ADDRSTRLEN];
932
Willy Tarreau631f01c2011-09-05 00:36:48 +0200933 addr_to_str(&listener->addr, pn, sizeof(pn));
934 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100935 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100936 return err;
937
938 tcp_close_return:
939 close(fd);
940 goto tcp_return;
941}
942
943/* This function creates all TCP sockets bound to the protocol entry <proto>.
944 * It is intended to be used as the protocol's bind_all() function.
945 * The sockets will be registered but not added to any fd_set, in order not to
946 * loose them across the fork(). A call to enable_all_listeners() is needed
947 * to complete initialization. The return value is composed from ERR_*.
948 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200949static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100950{
951 struct listener *listener;
952 int err = ERR_NONE;
953
954 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200955 err |= tcp_bind_listener(listener, errmsg, errlen);
956 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100957 break;
958 }
959
960 return err;
961}
962
963/* Add listener to the list of tcpv4 listeners. The listener's state
964 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
965 * listeners is updated. This is the function to use to add a new listener.
966 */
967void tcpv4_add_listener(struct listener *listener)
968{
969 if (listener->state != LI_INIT)
970 return;
971 listener->state = LI_ASSIGNED;
972 listener->proto = &proto_tcpv4;
973 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
974 proto_tcpv4.nb_listeners++;
975}
976
977/* Add listener to the list of tcpv4 listeners. The listener's state
978 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
979 * listeners is updated. This is the function to use to add a new listener.
980 */
981void tcpv6_add_listener(struct listener *listener)
982{
983 if (listener->state != LI_INIT)
984 return;
985 listener->state = LI_ASSIGNED;
986 listener->proto = &proto_tcpv6;
987 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
988 proto_tcpv6.nb_listeners++;
989}
990
Willy Tarreau092d8652014-07-07 20:22:12 +0200991/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
992 * was totally stopped, or > 0 if correctly paused.
993 */
994int tcp_pause_listener(struct listener *l)
995{
996 if (shutdown(l->fd, SHUT_WR) != 0)
997 return -1; /* Solaris dies here */
998
999 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
1000 return -1; /* OpenBSD dies here */
1001
1002 if (shutdown(l->fd, SHUT_RD) != 0)
1003 return -1; /* should always be OK */
1004 return 1;
1005}
1006
Willy Tarreauedcf6682008-11-30 23:15:34 +01001007/* This function performs the TCP request analysis on the current request. It
1008 * returns 1 if the processing can continue on next analysers, or zero if it
1009 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +02001010 * request. It relies on buffers flags, and updates s->req->analysers. The
1011 * function may be called for frontend rules and backend rules. It only relies
1012 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001013 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001014int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +01001015{
1016 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +02001017 struct stksess *ts;
1018 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001019 int partial;
1020
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001021 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 +01001022 now_ms, __FUNCTION__,
1023 s,
1024 req,
1025 req->rex, req->wex,
1026 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001027 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +01001028 req->analysers);
1029
Willy Tarreauedcf6682008-11-30 23:15:34 +01001030 /* We don't know whether we have enough data, so must proceed
1031 * this way :
1032 * - iterate through all rules in their declaration order
1033 * - if one rule returns MISS, it means the inspect delay is
1034 * not over yet, then return immediately, otherwise consider
1035 * it as a non-match.
1036 * - if one rule returns OK, then return OK
1037 * - if one rule returns KO, then return KO
1038 */
1039
Willy Tarreau9b28e032012-10-12 23:49:43 +02001040 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02001041 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001042 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001043 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001044 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001045
Willy Tarreaufb356202010-08-03 14:02:05 +02001046 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001047 enum acl_test_res ret = ACL_TEST_PASS;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001048
1049 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001050 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001051 if (ret == ACL_TEST_MISS)
1052 goto missing_data;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001053
1054 ret = acl_pass(ret);
1055 if (rule->cond->pol == ACL_COND_UNLESS)
1056 ret = !ret;
1057 }
1058
1059 if (ret) {
1060 /* we have a matching rule. */
1061 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001062 channel_abort(req);
1063 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001064 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001065
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001066 s->be->be_counters.denied_req++;
1067 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001068 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +02001069 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001070
Willy Tarreauedcf6682008-11-30 23:15:34 +01001071 if (!(s->flags & SN_ERR_MASK))
1072 s->flags |= SN_ERR_PRXCOND;
1073 if (!(s->flags & SN_FINST_MASK))
1074 s->flags |= SN_FINST_R;
1075 return 0;
1076 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001077 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001078 /* Note: only the first valid tracking parameter of each
1079 * applies.
1080 */
1081 struct stktable_key *key;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001082 struct sample smp;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001083
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001084 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001085 continue;
1086
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001087 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau1b71eb52014-06-25 17:01:56 +02001088 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial, rule->act_prm.trk_ctr.expr, &smp);
1089
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001090 if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL))
1091 goto missing_data; /* key might appear later */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001092
1093 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001094 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001095 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001096 if (s->fe != s->be)
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001097 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreaud1f96522010-08-03 19:34:32 +02001098 }
1099 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001100 else if (rule->action == TCP_ACT_CAPTURE) {
1101 struct sample *key;
1102 struct cap_hdr *h = rule->act_prm.cap.hdr;
1103 char **cap = s->txn.req.cap;
1104 int len;
1105
1106 key = sample_fetch_string(s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial, rule->act_prm.cap.expr);
1107 if (!key)
1108 continue;
1109
1110 if (key->flags & SMP_F_MAY_CHANGE)
1111 goto missing_data;
1112
1113 if (cap[h->index] == NULL)
1114 cap[h->index] = pool_alloc2(h->pool);
1115
1116 if (cap[h->index] == NULL) /* no more capture memory */
1117 continue;
1118
1119 len = key->data.str.len;
1120 if (len > h->len)
1121 len = h->len;
1122
1123 memcpy(cap[h->index], key->data.str.str, len);
1124 cap[h->index][len] = 0;
1125 }
Willy Tarreaud1f96522010-08-03 19:34:32 +02001126 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +01001127 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +02001128 break;
1129 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001130 }
1131 }
1132
1133 /* if we get there, it means we have no rule which matches, or
1134 * we have an explicit accept, so we apply the default accept.
1135 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001136 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +01001137 req->analyse_exp = TICK_ETERNITY;
1138 return 1;
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001139
1140 missing_data:
1141 channel_dont_connect(req);
1142 /* just set the request timeout once at the beginning of the request */
1143 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
1144 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
1145 return 0;
1146
Willy Tarreauedcf6682008-11-30 23:15:34 +01001147}
1148
Emeric Brun97679e72010-09-23 17:56:44 +02001149/* This function performs the TCP response analysis on the current response. It
1150 * returns 1 if the processing can continue on next analysers, or zero if it
1151 * needs more data, encounters an error, or wants to immediately abort the
1152 * response. It relies on buffers flags, and updates s->rep->analysers. The
1153 * function may be called for backend rules.
1154 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001155int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +02001156{
1157 struct tcp_rule *rule;
1158 int partial;
1159
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001160 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 +02001161 now_ms, __FUNCTION__,
1162 s,
1163 rep,
1164 rep->rex, rep->wex,
1165 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001166 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +02001167 rep->analysers);
1168
1169 /* We don't know whether we have enough data, so must proceed
1170 * this way :
1171 * - iterate through all rules in their declaration order
1172 * - if one rule returns MISS, it means the inspect delay is
1173 * not over yet, then return immediately, otherwise consider
1174 * it as a non-match.
1175 * - if one rule returns OK, then return OK
1176 * - if one rule returns KO, then return KO
1177 */
1178
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001179 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001180 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +02001181 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001182 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001183
1184 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
Willy Tarreau0cba6072013-11-28 22:21:02 +01001185 enum acl_test_res ret = ACL_TEST_PASS;
Emeric Brun97679e72010-09-23 17:56:44 +02001186
1187 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001188 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001189 if (ret == ACL_TEST_MISS) {
Emeric Brun97679e72010-09-23 17:56:44 +02001190 /* just set the analyser timeout once at the beginning of the response */
1191 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
Willy Tarreau0bb166b2013-11-04 15:56:53 +01001192 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Emeric Brun97679e72010-09-23 17:56:44 +02001193 return 0;
1194 }
1195
1196 ret = acl_pass(ret);
1197 if (rule->cond->pol == ACL_COND_UNLESS)
1198 ret = !ret;
1199 }
1200
1201 if (ret) {
1202 /* we have a matching rule. */
1203 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001204 channel_abort(rep);
1205 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +02001206 rep->analysers = 0;
1207
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001208 s->be->be_counters.denied_resp++;
1209 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +02001210 if (s->listener->counters)
1211 s->listener->counters->denied_resp++;
1212
1213 if (!(s->flags & SN_ERR_MASK))
1214 s->flags |= SN_ERR_PRXCOND;
1215 if (!(s->flags & SN_FINST_MASK))
1216 s->flags |= SN_FINST_D;
1217 return 0;
1218 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001219 else if (rule->action == TCP_ACT_CLOSE) {
1220 rep->prod->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
1221 si_shutr(rep->prod);
1222 si_shutw(rep->prod);
1223 break;
1224 }
Emeric Brun97679e72010-09-23 17:56:44 +02001225 else {
1226 /* otherwise accept */
1227 break;
1228 }
1229 }
1230 }
1231
1232 /* if we get there, it means we have no rule which matches, or
1233 * we have an explicit accept, so we apply the default accept.
1234 */
1235 rep->analysers &= ~an_bit;
1236 rep->analyse_exp = TICK_ETERNITY;
1237 return 1;
1238}
1239
1240
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001241/* This function performs the TCP layer4 analysis on the current request. It
1242 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1243 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001244 * any data. This only works on connection-based client-facing stream interfaces.
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001245 */
1246int tcp_exec_req_rules(struct session *s)
1247{
1248 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001249 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001250 struct stktable *t = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001251 struct connection *conn = objt_conn(s->si[0].end);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001252 int result = 1;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001253 enum acl_test_res ret;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001254
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001255 if (!conn)
1256 return result;
1257
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001258 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001259 ret = ACL_TEST_PASS;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001260
1261 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001262 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001263 ret = acl_pass(ret);
1264 if (rule->cond->pol == ACL_COND_UNLESS)
1265 ret = !ret;
1266 }
1267
1268 if (ret) {
1269 /* we have a matching rule. */
1270 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001271 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001272 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001273 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001274
1275 if (!(s->flags & SN_ERR_MASK))
1276 s->flags |= SN_ERR_PRXCOND;
1277 if (!(s->flags & SN_FINST_MASK))
1278 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001279 result = 0;
1280 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001281 }
Willy Tarreau44778ad2013-10-30 19:24:00 +01001282 else if (rule->action >= TCP_ACT_TRK_SC0 && rule->action <= TCP_ACT_TRK_SCMAX) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001283 /* Note: only the first valid tracking parameter of each
1284 * applies.
1285 */
1286 struct stktable_key *key;
1287
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01001288 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
Willy Tarreau44778ad2013-10-30 19:24:00 +01001289 continue;
1290
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001291 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreaub5975de2014-06-25 16:20:53 +02001292 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 +01001293
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +02001294 if (key && (ts = stktable_get_entry(t, key)))
1295 session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001296 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001297 else if (rule->action == TCP_ACT_EXPECT_PX) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001298 conn->flags |= CO_FL_ACCEPT_PROXY;
1299 conn_sock_want_recv(conn);
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001300 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001301 else {
1302 /* otherwise it's an accept */
1303 break;
1304 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001305 }
1306 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001307 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001308}
1309
Emeric Brun97679e72010-09-23 17:56:44 +02001310/* Parse a tcp-response rule. Return a negative value in case of failure */
1311static int tcp_parse_response_rule(char **args, int arg, int section_type,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001312 struct proxy *curpx, struct proxy *defpx,
1313 struct tcp_rule *rule, char **err,
1314 unsigned int where,
1315 const char *file, int line)
Emeric Brun97679e72010-09-23 17:56:44 +02001316{
1317 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001318 memprintf(err, "%s %s is only allowed in 'backend' sections",
1319 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001320 return -1;
1321 }
1322
1323 if (strcmp(args[arg], "accept") == 0) {
1324 arg++;
1325 rule->action = TCP_ACT_ACCEPT;
1326 }
1327 else if (strcmp(args[arg], "reject") == 0) {
1328 arg++;
1329 rule->action = TCP_ACT_REJECT;
1330 }
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001331 else if (strcmp(args[arg], "close") == 0) {
1332 arg++;
1333 rule->action = TCP_ACT_CLOSE;
1334 }
Emeric Brun97679e72010-09-23 17:56:44 +02001335 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001336 memprintf(err,
Willy Tarreaucc1e04b2013-09-11 23:20:29 +02001337 "'%s %s' expects 'accept', 'close' or 'reject' in %s '%s' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001338 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001339 return -1;
1340 }
1341
1342 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001343 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001344 memprintf(err,
1345 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1346 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001347 return -1;
1348 }
1349 }
1350 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001351 memprintf(err,
1352 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001353 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1354 return -1;
1355 }
1356 return 0;
1357}
1358
1359
1360
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001361/* Parse a tcp-request rule. Return a negative value in case of failure */
1362static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001363 struct proxy *curpx, struct proxy *defpx,
1364 struct tcp_rule *rule, char **err,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001365 unsigned int where, const char *file, int line)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001366{
1367 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001368 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1369 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001370 return -1;
1371 }
1372
1373 if (!strcmp(args[arg], "accept")) {
1374 arg++;
1375 rule->action = TCP_ACT_ACCEPT;
1376 }
1377 else if (!strcmp(args[arg], "reject")) {
1378 arg++;
1379 rule->action = TCP_ACT_REJECT;
1380 }
Willy Tarreau18bf01e2014-06-13 16:18:52 +02001381 else if (strcmp(args[arg], "capture") == 0) {
1382 struct sample_expr *expr;
1383 struct cap_hdr *hdr;
1384 int kw = arg;
1385 int len = 0;
1386
1387 if (!(curpx->cap & PR_CAP_FE)) {
1388 memprintf(err,
1389 "'%s %s %s' : proxy '%s' has no frontend capability",
1390 args[0], args[1], args[kw], curpx->id);
1391 return -1;
1392 }
1393
1394 if (!(where & SMP_VAL_FE_REQ_CNT)) {
1395 memprintf(err,
1396 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1397 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1398 return -1;
1399 }
1400
1401 arg++;
1402
1403 curpx->conf.args.ctx = ARGC_CAP;
1404 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
1405 if (!expr) {
1406 memprintf(err,
1407 "'%s %s %s' : %s",
1408 args[0], args[1], args[kw], *err);
1409 return -1;
1410 }
1411
1412 if (!(expr->fetch->val & where)) {
1413 memprintf(err,
1414 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
1415 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
1416 free(expr);
1417 return -1;
1418 }
1419
1420 if (strcmp(args[arg], "len") == 0) {
1421 arg++;
1422 if (!args[arg]) {
1423 memprintf(err,
1424 "'%s %s %s' : missing length value",
1425 args[0], args[1], args[kw]);
1426 free(expr);
1427 return -1;
1428 }
1429 /* we copy the table name for now, it will be resolved later */
1430 len = atoi(args[arg]);
1431 if (len <= 0) {
1432 memprintf(err,
1433 "'%s %s %s' : length must be > 0",
1434 args[0], args[1], args[kw]);
1435 free(expr);
1436 return -1;
1437 }
1438 arg++;
1439 }
1440
1441 if (!len) {
1442 memprintf(err,
1443 "'%s %s %s' : a positive 'len' argument is mandatory",
1444 args[0], args[1], args[kw]);
1445 free(expr);
1446 return -1;
1447 }
1448
1449 hdr = calloc(sizeof(struct cap_hdr), 1);
1450 hdr->next = curpx->req_cap;
1451 hdr->name = NULL; /* not a header capture */
1452 hdr->namelen = 0;
1453 hdr->len = len;
1454 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
1455 hdr->index = curpx->nb_req_cap++;
1456
1457 curpx->req_cap = hdr;
1458 curpx->to_log |= LW_REQHDR;
1459
1460 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
1461 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1462
1463 rule->act_prm.cap.expr = expr;
1464 rule->act_prm.cap.hdr = hdr;
1465 rule->action = TCP_ACT_CAPTURE;
1466 }
Willy Tarreaub4c84932013-07-23 19:15:30 +02001467 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
1468 args[arg][9] == '\0' && args[arg][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02001469 args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001470 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001471 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001472
1473 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001474
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001475 curpx->conf.args.ctx = ARGC_TRK;
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001476 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001477 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001478 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001479 "'%s %s %s' : %s",
Willy Tarreau975c1782013-12-12 23:16:54 +01001480 args[0], args[1], args[kw], *err);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001481 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001482 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001483
Willy Tarreau80aca902013-01-07 15:42:20 +01001484 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001485 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001486 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001487 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001488 free(expr);
1489 return -1;
1490 }
1491
1492 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001493 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001494
1495 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001496 arg++;
1497 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001498 memprintf(err,
1499 "'%s %s %s' : missing table name",
1500 args[0], args[1], args[kw]);
1501 free(expr);
1502 return -1;
1503 }
1504 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001505 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001506 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001507 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001508 rule->act_prm.trk_ctr.expr = expr;
Willy Tarreaube4a3ef2013-06-17 15:04:07 +02001509 rule->action = TCP_ACT_TRK_SC0 + args[kw][8] - '0';
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001510 }
Willy Tarreau4f0d9192013-06-11 20:40:55 +02001511 else if (strcmp(args[arg], "expect-proxy") == 0) {
1512 if (strcmp(args[arg+1], "layer4") != 0) {
1513 memprintf(err,
1514 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1515 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1516 return -1;
1517 }
1518
1519 if (!(where & SMP_VAL_FE_CON_ACC)) {
1520 memprintf(err,
1521 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1522 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1523 return -1;
1524 }
1525
1526 arg += 2;
1527 rule->action = TCP_ACT_EXPECT_PX;
1528 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001529 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001530 memprintf(err,
Willy Tarreaub4c84932013-07-23 19:15:30 +02001531 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d' "
1532 " in %s '%s' (got '%s')",
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02001533 args[0], args[1], MAX_SESS_STKCTR-1, proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001534 return -1;
1535 }
1536
1537 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001538 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001539 memprintf(err,
1540 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1541 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001542 return -1;
1543 }
1544 }
1545 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001546 memprintf(err,
1547 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001548 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1549 return -1;
1550 }
1551 return 0;
1552}
1553
Emeric Brun97679e72010-09-23 17:56:44 +02001554/* This function should be called to parse a line starting with the "tcp-response"
1555 * keyword.
1556 */
1557static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001558 struct proxy *defpx, const char *file, int line,
1559 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001560{
1561 const char *ptr = NULL;
1562 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001563 int warn = 0;
1564 int arg;
1565 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001566 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001567 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001568 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001569
1570 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001571 memprintf(err, "missing argument for '%s' in %s '%s'",
1572 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001573 return -1;
1574 }
1575
1576 if (strcmp(args[1], "inspect-delay") == 0) {
1577 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001578 memprintf(err, "%s %s is only allowed in 'backend' sections",
1579 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001580 return -1;
1581 }
1582
1583 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001584 memprintf(err,
1585 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1586 args[0], args[1], proxy_type_str(curpx), curpx->id);
1587 if (ptr)
1588 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001589 return -1;
1590 }
1591
1592 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001593 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1594 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001595 return 1;
1596 }
1597 curpx->tcp_rep.inspect_delay = val;
1598 return 0;
1599 }
1600
Simon Hormande072bd2011-06-24 15:11:37 +09001601 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001602 LIST_INIT(&rule->list);
1603 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001604 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001605
1606 if (strcmp(args[1], "content") == 0) {
1607 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001608
1609 if (curpx->cap & PR_CAP_FE)
1610 where |= SMP_VAL_FE_RES_CNT;
1611 if (curpx->cap & PR_CAP_BE)
1612 where |= SMP_VAL_BE_RES_CNT;
1613
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001614 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001615 goto error;
1616
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001617 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1618 if (acl) {
1619 if (acl->name && *acl->name)
1620 memprintf(err,
1621 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1622 acl->name, args[0], args[1], sample_ckp_names(where));
1623 else
1624 memprintf(err,
1625 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1626 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001627 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001628 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001629
Emeric Brun97679e72010-09-23 17:56:44 +02001630 warn++;
1631 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001632 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1633 if (acl->name && *acl->name)
1634 memprintf(err,
1635 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001636 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001637 else
1638 memprintf(err,
1639 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001640 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001641 warn++;
1642 }
Emeric Brun97679e72010-09-23 17:56:44 +02001643
1644 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1645 }
1646 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001647 memprintf(err,
1648 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1649 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001650 goto error;
1651 }
1652
1653 return warn;
1654 error:
1655 free(rule);
1656 return -1;
1657}
1658
1659
Willy Tarreaub6866442008-07-14 23:54:42 +02001660/* This function should be called to parse a line starting with the "tcp-request"
1661 * keyword.
1662 */
1663static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001664 struct proxy *defpx, const char *file, int line,
1665 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001666{
1667 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001668 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001669 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001670 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001671 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001672 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001673 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001674 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001675
1676 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001677 if (curpx == defpx)
1678 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1679 else
1680 memprintf(err, "missing argument for '%s' in %s '%s'",
1681 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001682 return -1;
1683 }
1684
1685 if (!strcmp(args[1], "inspect-delay")) {
1686 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001687 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1688 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001689 return -1;
1690 }
1691
Willy Tarreaub6866442008-07-14 23:54:42 +02001692 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001693 memprintf(err,
1694 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1695 args[0], args[1], proxy_type_str(curpx), curpx->id);
1696 if (ptr)
1697 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001698 return -1;
1699 }
1700
1701 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001702 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1703 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001704 return 1;
1705 }
1706 curpx->tcp_req.inspect_delay = val;
1707 return 0;
1708 }
1709
Simon Hormande072bd2011-06-24 15:11:37 +09001710 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001711 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001712 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001713 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001714
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001715 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001716 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001717
1718 if (curpx->cap & PR_CAP_FE)
1719 where |= SMP_VAL_FE_REQ_CNT;
1720 if (curpx->cap & PR_CAP_BE)
1721 where |= SMP_VAL_BE_REQ_CNT;
1722
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001723 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001724 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001725
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001726 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1727 if (acl) {
1728 if (acl->name && *acl->name)
1729 memprintf(err,
1730 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1731 acl->name, args[0], args[1], sample_ckp_names(where));
1732 else
1733 memprintf(err,
1734 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1735 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001736 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001737 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001738
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001739 warn++;
1740 }
1741 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1742 if (acl->name && *acl->name)
1743 memprintf(err,
1744 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001745 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001746 else
1747 memprintf(err,
1748 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001749 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001750 warn++;
1751 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001752
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001753 /* the following function directly emits the warning */
1754 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001755 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001756 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001757 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001758 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001759
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001760 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001761 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1762 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001763 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001764 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001765
Willy Tarreau80aca902013-01-07 15:42:20 +01001766 where |= SMP_VAL_FE_CON_ACC;
1767
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001768 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001769 goto error;
1770
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001771 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1772 if (acl) {
1773 if (acl->name && *acl->name)
1774 memprintf(err,
1775 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1776 acl->name, args[0], args[1], sample_ckp_names(where));
1777 else
1778 memprintf(err,
1779 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1780 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001781 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001782 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001783
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001784 warn++;
1785 }
1786 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1787 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001788 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001789 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001790 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001791 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001792 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001793 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001794 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001795 warn++;
1796 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001797
Willy Tarreau3986b9c2014-09-16 15:39:51 +02001798 /* the following function directly emits the warning */
1799 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001800 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001801 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001802 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001803 if (curpx == defpx)
1804 memprintf(err,
1805 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1806 args[0], args[1]);
1807 else
1808 memprintf(err,
1809 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001810 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001811 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001812 }
1813
Willy Tarreau1a687942010-05-23 22:40:30 +02001814 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001815 error:
1816 free(rule);
1817 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001818}
1819
Willy Tarreau645513a2010-05-24 20:55:15 +02001820
1821/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001822/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001823/************************************************************************/
1824
Willy Tarreau4a129812012-04-25 17:31:42 +02001825/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001826static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001827smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001828 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001829{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001830 struct connection *cli_conn = objt_conn(l4->si[0].end);
1831
1832 if (!cli_conn)
1833 return 0;
1834
1835 switch (cli_conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001836 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001837 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001838 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001839 break;
1840 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001841 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001842 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001843 break;
1844 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001845 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001846 }
Emeric Brunf769f512010-10-22 17:14:01 +02001847
Willy Tarreau37406352012-04-23 16:16:37 +02001848 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001849 return 1;
1850}
1851
Willy Tarreaua5e37562011-12-16 17:06:15 +01001852/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001853static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001854smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001855 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001856{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001857 struct connection *cli_conn = objt_conn(l4->si[0].end);
1858
1859 if (!cli_conn)
1860 return 0;
1861
Willy Tarreauf853c462012-04-23 18:53:56 +02001862 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001863 if (!(smp->data.uint = get_host_port(&cli_conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001864 return 0;
1865
Willy Tarreau37406352012-04-23 16:16:37 +02001866 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001867 return 1;
1868}
1869
Willy Tarreau4a129812012-04-25 17:31:42 +02001870/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001871static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001872smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001873 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001874{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001875 struct connection *cli_conn = objt_conn(l4->si[0].end);
Willy Tarreau645513a2010-05-24 20:55:15 +02001876
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001877 if (!cli_conn)
1878 return 0;
1879
1880 conn_get_to_addr(cli_conn);
1881
1882 switch (cli_conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001883 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001884 smp->data.ipv4 = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001885 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001886 break;
1887 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001888 smp->data.ipv6 = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001889 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001890 break;
1891 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001892 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001893 }
Emeric Brunf769f512010-10-22 17:14:01 +02001894
Willy Tarreau37406352012-04-23 16:16:37 +02001895 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001896 return 1;
1897}
1898
Willy Tarreaua5e37562011-12-16 17:06:15 +01001899/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001900static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001901smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001902 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau645513a2010-05-24 20:55:15 +02001903{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001904 struct connection *cli_conn = objt_conn(l4->si[0].end);
1905
1906 if (!cli_conn)
1907 return 0;
1908
1909 conn_get_to_addr(cli_conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001910
Willy Tarreauf853c462012-04-23 18:53:56 +02001911 smp->type = SMP_T_UINT;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001912 if (!(smp->data.uint = get_host_port(&cli_conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001913 return 0;
1914
Willy Tarreau37406352012-04-23 16:16:37 +02001915 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001916 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001917}
1918
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001919#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001920/* parse the "v4v6" bind keyword */
1921static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1922{
1923 struct listener *l;
1924
1925 list_for_each_entry(l, &conf->listeners, by_bind) {
1926 if (l->addr.ss_family == AF_INET6)
1927 l->options |= LI_O_V4V6;
1928 }
1929
1930 return 0;
1931}
1932
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001933/* parse the "v6only" bind keyword */
1934static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1935{
1936 struct listener *l;
1937
1938 list_for_each_entry(l, &conf->listeners, by_bind) {
1939 if (l->addr.ss_family == AF_INET6)
1940 l->options |= LI_O_V6ONLY;
1941 }
1942
1943 return 0;
1944}
1945#endif
1946
Pieter Baauwd551fb52013-05-08 22:49:23 +02001947#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001948/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001949static 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 +02001950{
1951 struct listener *l;
1952
Willy Tarreau4348fad2012-09-20 16:48:07 +02001953 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_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001956 }
1957
Willy Tarreau44791242012-09-12 23:27:21 +02001958 return 0;
1959}
1960#endif
1961
1962#ifdef TCP_DEFER_ACCEPT
1963/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001964static 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 +02001965{
1966 struct listener *l;
1967
Willy Tarreau4348fad2012-09-20 16:48:07 +02001968 list_for_each_entry(l, &conf->listeners, by_bind) {
1969 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1970 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001971 }
1972
Willy Tarreau44791242012-09-12 23:27:21 +02001973 return 0;
1974}
1975#endif
1976
Willy Tarreau1c862c52012-10-05 16:21:00 +02001977#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001978/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001979static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1980{
1981 struct listener *l;
1982
1983 list_for_each_entry(l, &conf->listeners, by_bind) {
1984 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1985 l->options |= LI_O_TCP_FO;
1986 }
1987
1988 return 0;
1989}
1990#endif
1991
Willy Tarreau44791242012-09-12 23:27:21 +02001992#ifdef TCP_MAXSEG
1993/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001994static 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 +02001995{
1996 struct listener *l;
1997 int mss;
1998
Willy Tarreau44791242012-09-12 23:27:21 +02001999 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002000 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002001 return ERR_ALERT | ERR_FATAL;
2002 }
2003
2004 mss = atoi(args[cur_arg + 1]);
2005 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002006 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002007 return ERR_ALERT | ERR_FATAL;
2008 }
2009
Willy Tarreau4348fad2012-09-20 16:48:07 +02002010 list_for_each_entry(l, &conf->listeners, by_bind) {
2011 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2012 l->maxseg = mss;
2013 }
Willy Tarreau44791242012-09-12 23:27:21 +02002014
2015 return 0;
2016}
2017#endif
2018
Willy Tarreau2af207a2015-02-04 00:45:58 +01002019#ifdef TCP_USER_TIMEOUT
2020/* parse the "tcp-ut" bind keyword */
2021static int bind_parse_tcp_ut(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2022{
2023 const char *ptr = NULL;
2024 struct listener *l;
2025 unsigned int timeout;
2026
2027 if (!*args[cur_arg + 1]) {
2028 memprintf(err, "'%s' : missing TCP User Timeout value", args[cur_arg]);
2029 return ERR_ALERT | ERR_FATAL;
2030 }
2031
2032 ptr = parse_time_err(args[cur_arg + 1], &timeout, TIME_UNIT_MS);
2033 if (ptr) {
2034 memprintf(err, "'%s' : expects a positive delay in milliseconds", args[cur_arg]);
2035 return ERR_ALERT | ERR_FATAL;
2036 }
2037
2038 list_for_each_entry(l, &conf->listeners, by_bind) {
2039 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2040 l->tcp_ut = timeout;
2041 }
2042
2043 return 0;
2044}
2045#endif
2046
Willy Tarreau44791242012-09-12 23:27:21 +02002047#ifdef SO_BINDTODEVICE
Willy Tarreau2af207a2015-02-04 00:45:58 +01002048/* parse the "interface" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002049static 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 +02002050{
2051 struct listener *l;
2052
Willy Tarreau44791242012-09-12 23:27:21 +02002053 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002054 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02002055 return ERR_ALERT | ERR_FATAL;
2056 }
2057
Willy Tarreau4348fad2012-09-20 16:48:07 +02002058 list_for_each_entry(l, &conf->listeners, by_bind) {
2059 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
2060 l->interface = strdup(args[cur_arg + 1]);
2061 }
Willy Tarreau44791242012-09-12 23:27:21 +02002062
2063 global.last_checks |= LSTCHK_NETADM;
2064 return 0;
2065}
2066#endif
2067
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002068#ifdef CONFIG_HAP_NS
2069/* parse the "namespace" bind keyword */
2070static int bind_parse_namespace(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2071{
2072 struct listener *l;
2073 char *namespace = NULL;
2074
2075 if (!*args[cur_arg + 1]) {
2076 memprintf(err, "'%s' : missing namespace id", args[cur_arg]);
2077 return ERR_ALERT | ERR_FATAL;
2078 }
2079 namespace = args[cur_arg + 1];
2080
2081 list_for_each_entry(l, &conf->listeners, by_bind) {
2082 l->netns = netns_store_lookup(namespace, strlen(namespace));
2083
2084 if (l->netns == NULL)
2085 l->netns = netns_store_insert(namespace);
2086
2087 if (l->netns == NULL) {
2088 Alert("Cannot open namespace '%s'.\n", args[cur_arg + 1]);
2089 return ERR_ALERT | ERR_FATAL;
2090 }
2091 }
2092 return 0;
2093}
2094#endif
2095
Willy Tarreaudc13c112013-06-21 23:16:39 +02002096static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002097 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02002098 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02002099 { 0, NULL, NULL },
2100}};
2101
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002102
Willy Tarreau61612d42012-04-19 18:42:05 +02002103/* Note: must not be declared <const> as its list will be overwritten.
2104 * Please take care of keeping this list alphabetically sorted.
2105 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002106static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002107 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02002108}};
2109
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002110
Willy Tarreau4a129812012-04-25 17:31:42 +02002111/* Note: must not be declared <const> as its list will be overwritten.
2112 * Note: fetches that may return multiple types must be declared as the lowest
2113 * common denominator, the type that can be casted into all other ones. For
2114 * instance v4/v6 must be declared v4.
2115 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002116static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002117 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
2118 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
2119 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
2120 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
2121 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02002122}};
2123
Willy Tarreau44791242012-09-12 23:27:21 +02002124/************************************************************************/
2125/* All supported bind keywords must be declared here. */
2126/************************************************************************/
2127
2128/* Note: must not be declared <const> as its list will be overwritten.
2129 * Please take care of keeping this list alphabetically sorted, doing so helps
2130 * all code contributors.
2131 * Optional keywords are also declared with a NULL ->parse() function so that
2132 * the config parser can report an appropriate error when a known keyword was
2133 * not enabled.
2134 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02002135static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02002136#ifdef TCP_DEFER_ACCEPT
2137 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
2138#endif
2139#ifdef SO_BINDTODEVICE
2140 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
2141#endif
2142#ifdef TCP_MAXSEG
2143 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
2144#endif
Willy Tarreau2af207a2015-02-04 00:45:58 +01002145#ifdef TCP_USER_TIMEOUT
2146 { "tcp-ut", bind_parse_tcp_ut, 1 }, /* set User Timeout on listening socket */
2147#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02002148#ifdef TCP_FASTOPEN
2149 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
2150#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02002151#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02002152 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
2153#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002154#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01002155 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002156 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
2157#endif
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002158#ifdef CONFIG_HAP_NS
2159 { "namespace", bind_parse_namespace, 1 },
2160#endif
Willy Tarreau44791242012-09-12 23:27:21 +02002161 /* the versions with the NULL parse function*/
2162 { "defer-accept", NULL, 0 },
2163 { "interface", NULL, 1 },
2164 { "mss", NULL, 1 },
2165 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01002166 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01002167 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02002168 { NULL, NULL, 0 },
2169}};
2170
Willy Tarreaue6b98942007-10-29 01:09:36 +01002171__attribute__((constructor))
2172static void __tcp_protocol_init(void)
2173{
2174 protocol_register(&proto_tcpv4);
2175 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02002176 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02002177 cfg_register_keywords(&cfg_kws);
2178 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02002179 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01002180}
2181
2182
2183/*
2184 * Local variables:
2185 * c-indent-level: 8
2186 * c-basic-offset: 8
2187 * End:
2188 */