blob: f59c23b41315f8ab11142d7e33acd8dc263d2ddb [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaue6b98942007-10-29 01:09:36 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040027#include <netinet/tcp.h>
28
Willy Tarreaub6866442008-07-14 23:54:42 +020029#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/errors.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010034#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010036
Willy Tarreaue6b98942007-10-29 01:09:36 +010037#include <types/global.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020038#include <types/server.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010039
40#include <proto/acl.h>
Willy Tarreau9fcb9842012-04-20 14:45:49 +020041#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020043#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020044#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/listener.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020046#include <proto/log.h>
47#include <proto/port_range.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020048#include <proto/protocol.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010049#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020051#include <proto/sample.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020052#include <proto/session.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020053#include <proto/stick_table.h>
Willy Tarreaua975b8f2010-06-05 19:13:27 +020054#include <proto/task.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010055
Willy Tarreaue8c66af2008-01-13 18:40:14 +010056#ifdef CONFIG_HAP_CTTPROXY
57#include <import/ip_tproxy.h>
58#endif
59
Emeric Bruncf20bf12010-10-22 16:06:11 +020060static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
61static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreaue6b98942007-10-29 01:09:36 +010062
63/* Note: must not be declared <const> as its list will be overwritten */
64static struct protocol proto_tcpv4 = {
65 .name = "tcpv4",
66 .sock_domain = AF_INET,
67 .sock_type = SOCK_STREAM,
68 .sock_prot = IPPROTO_TCP,
69 .sock_family = AF_INET,
70 .sock_addrlen = sizeof(struct sockaddr_in),
71 .l3_addrlen = 32/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020072 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020073 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020074 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010075 .bind_all = tcp_bind_listeners,
76 .unbind_all = unbind_all_listeners,
77 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020078 .get_src = tcp_get_src,
79 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +010080 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
81 .nb_listeners = 0,
82};
83
84/* Note: must not be declared <const> as its list will be overwritten */
85static struct protocol proto_tcpv6 = {
86 .name = "tcpv6",
87 .sock_domain = AF_INET6,
88 .sock_type = SOCK_STREAM,
89 .sock_prot = IPPROTO_TCP,
90 .sock_family = AF_INET6,
91 .sock_addrlen = sizeof(struct sockaddr_in6),
92 .l3_addrlen = 128/8,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020093 .accept = &listener_accept,
Willy Tarreau26d8c592012-05-07 18:12:14 +020094 .connect = tcp_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020095 .bind = tcp_bind_listener,
Willy Tarreaue6b98942007-10-29 01:09:36 +010096 .bind_all = tcp_bind_listeners,
97 .unbind_all = unbind_all_listeners,
98 .enable_all = enable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020099 .get_src = tcp_get_src,
100 .get_dst = tcp_get_dst,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100101 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
102 .nb_listeners = 0,
103};
104
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100105
David du Colombier6f5ccb12011-03-10 22:26:24 +0100106/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100107 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
108 * - 0 : ignore remote address (may even be a NULL pointer)
109 * - 1 : use provided address
110 * - 2 : use provided port
111 * - 3 : use both
112 *
113 * The function supports multiple foreign binding methods :
114 * - linux_tproxy: we directly bind to the foreign address
115 * - cttproxy: we bind to a local address then nat.
116 * The second one can be used as a fallback for the first one.
117 * This function returns 0 when everything's OK, 1 if it could not bind, to the
118 * local address, 2 if it could not bind to the foreign address.
119 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100120int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100121{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100122 struct sockaddr_storage bind_addr;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100123 int foreign_ok = 0;
124 int ret;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100125 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200126 static int ip6_transp_working = 1;
Pieter Baauwd551fb52013-05-08 22:49:23 +0200127
David du Colombier65c17962012-07-13 14:34:59 +0200128 switch (local->ss_family) {
129 case AF_INET:
130 if (flags && ip_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200131 /* This deserves some explanation. Some platforms will support
132 * multiple combinations of certain methods, so we try the
133 * supported ones until one succeeds.
134 */
135 if (0
136#if defined(IP_TRANSPARENT)
137 || (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0)
138#endif
139#if defined(IP_FREEBIND)
140 || (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
141#endif
142 )
David du Colombier65c17962012-07-13 14:34:59 +0200143 foreign_ok = 1;
144 else
145 ip_transp_working = 0;
146 }
147 break;
148 case AF_INET6:
149 if (flags && ip6_transp_working) {
Pieter Baauwd551fb52013-05-08 22:49:23 +0200150 if (0
151#if defined(IPV6_TRANSPARENT)
152 || (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
153#endif
154 )
David du Colombier65c17962012-07-13 14:34:59 +0200155 foreign_ok = 1;
156 else
157 ip6_transp_working = 0;
158 }
159 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100160 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200161
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100162 if (flags) {
163 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200164 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100165 switch (remote->ss_family) {
166 case AF_INET:
167 if (flags & 1)
168 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
169 if (flags & 2)
170 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
171 break;
172 case AF_INET6:
173 if (flags & 1)
174 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
175 if (flags & 2)
176 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
177 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100178 default:
179 /* we don't want to try to bind to an unknown address family */
180 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100181 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100182 }
183
Simon Hormande072bd2011-06-24 15:11:37 +0900184 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100185 if (foreign_ok) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200186 if (is_addr(&bind_addr)) {
187 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
188 if (ret < 0)
189 return 2;
190 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100191 }
192 else {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200193 if (is_addr(local)) {
194 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
195 if (ret < 0)
196 return 1;
197 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100198 }
199
200 if (!flags)
201 return 0;
202
203#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100204 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100205 struct in_tproxy itp1, itp2;
206 memset(&itp1, 0, sizeof(itp1));
207
208 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100209 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
210 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100211
212 /* set connect flag on socket */
213 itp2.op = TPROXY_FLAGS;
214 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
215
216 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
217 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
218 foreign_ok = 1;
219 }
220 }
221#endif
222 if (!foreign_ok)
223 /* we could not bind to a foreign address */
224 return 2;
225
226 return 0;
227}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100228
Willy Tarreau9650f372009-08-16 14:02:45 +0200229
230/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200231 * This function initiates a TCP connection establishment to the target assigned
232 * to connection <conn> using (si->{target,addr.to}). A source address may be
233 * pointed to by conn->addr.from in case of transparent proxying. Normal source
234 * bind addresses are still determined locally (due to the possible need of a
235 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100236 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100237 * supported. The <data> parameter is a boolean indicating whether there are data
238 * waiting for being sent or not, in order to adjust data write polling and on
239 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
240 * allows the caller to force using a delayed ACK when establishing the connection :
241 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
242 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
243 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200244 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200245 * It can return one of :
246 * - SN_ERR_NONE if everything's OK
247 * - SN_ERR_SRVTO if there are no more servers
248 * - SN_ERR_SRVCL if the connection was refused by the server
249 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
250 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
251 * - SN_ERR_INTERNAL for any other purely internal errors
252 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100253 *
254 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
255 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200256 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100257
Willy Tarreauf0837b22012-11-24 10:24:27 +0100258int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200259{
260 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100261 struct server *srv;
262 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100263 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100264
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100265 switch (obj_type(conn->target)) {
266 case OBJ_TYPE_PROXY:
267 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100268 srv = NULL;
269 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100270 case OBJ_TYPE_SERVER:
271 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100272 be = srv->proxy;
273 break;
274 default:
275 return SN_ERR_INTERNAL;
276 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200277
Willy Tarreau986a9d22012-08-30 21:11:38 +0200278 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200279 qfprintf(stderr, "Cannot get a server socket.\n");
280
281 if (errno == ENFILE)
282 send_log(be, LOG_EMERG,
283 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
284 be->id, maxfd);
285 else if (errno == EMFILE)
286 send_log(be, LOG_EMERG,
287 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
288 be->id, maxfd);
289 else if (errno == ENOBUFS || errno == ENOMEM)
290 send_log(be, LOG_EMERG,
291 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
292 be->id, maxfd);
293 /* this is a resource error */
294 return SN_ERR_RESOURCE;
295 }
296
297 if (fd >= global.maxsock) {
298 /* do not log anything there, it's a normal condition when this option
299 * is used to serialize connections to a server !
300 */
301 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
302 close(fd);
303 return SN_ERR_PRXCOND; /* it is a configuration limit */
304 }
305
306 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900307 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200308 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
309 close(fd);
310 return SN_ERR_INTERNAL;
311 }
312
313 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900314 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200315
Willy Tarreau9650f372009-08-16 14:02:45 +0200316 /* allow specific binding :
317 * - server-specific at first
318 * - proxy-specific next
319 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100320 if (srv && srv->conn_src.opts & CO_SRC_BIND)
321 src = &srv->conn_src;
322 else if (be->conn_src.opts & CO_SRC_BIND)
323 src = &be->conn_src;
324 else
325 src = NULL;
326
327 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200328 int ret, flags = 0;
329
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200330 if (is_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100331 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100332 case CO_SRC_TPROXY_ADDR:
333 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200334 flags = 3;
335 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100336 case CO_SRC_TPROXY_CIP:
337 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200338 flags = 1;
339 break;
340 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200341 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200342
Willy Tarreau9650f372009-08-16 14:02:45 +0200343#ifdef SO_BINDTODEVICE
344 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100345 if (src->iface_name)
346 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200347#endif
348
Willy Tarreaua4380b42012-12-08 22:49:11 +0100349 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200350 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100351 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200352
353 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100354 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200355
356 do {
357 /* note: in case of retry, we may have to release a previously
358 * allocated port, hence this loop's construct.
359 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200360 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
361 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200362
363 if (!attempts)
364 break;
365 attempts--;
366
Willy Tarreaua4380b42012-12-08 22:49:11 +0100367 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200368 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200369 break;
370
Willy Tarreaua4380b42012-12-08 22:49:11 +0100371 fdinfo[fd].port_range = src->sport_range;
372 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200373
Willy Tarreaua4380b42012-12-08 22:49:11 +0100374 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 } while (ret != 0); /* binding NOK */
376 }
377 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100378 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200379 }
380
Willy Tarreaua4380b42012-12-08 22:49:11 +0100381 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200382 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
383 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200384 close(fd);
385
Willy Tarreau9650f372009-08-16 14:02:45 +0200386 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100387 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200388 be->id);
389 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100390 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200391 be->id);
392 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100393 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200394 be->id);
395 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100396 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200397 be->id);
398 }
399 return SN_ERR_RESOURCE;
400 }
401 }
402
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400403#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200404 /* disabling tcp quick ack now allows the first request to leave the
405 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100406 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200407 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100408 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900409 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200410#endif
411
Willy Tarreaue803de22010-01-21 17:43:04 +0100412 if (global.tune.server_sndbuf)
413 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
414
415 if (global.tune.server_rcvbuf)
416 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
417
Willy Tarreau986a9d22012-08-30 21:11:38 +0200418 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200419 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
420
Willy Tarreaub1719512012-12-08 23:03:28 +0100421 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200422 char *msg;
Willy Tarreaub1719512012-12-08 23:03:28 +0100423 if (errno == EAGAIN || errno == EADDRNOTAVAIL)
Willy Tarreau9650f372009-08-16 14:02:45 +0200424 msg = "no free ports";
425 else
426 msg = "local address already in use";
427
Willy Tarreaub1719512012-12-08 23:03:28 +0100428 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200429 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
430 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200431 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100432 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9650f372009-08-16 14:02:45 +0200433 return SN_ERR_RESOURCE;
434 } else if (errno == ETIMEDOUT) {
435 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200436 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
437 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200438 close(fd);
439 return SN_ERR_SRVTO;
440 } else {
441 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
442 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200443 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
444 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200445 close(fd);
446 return SN_ERR_SRVCL;
447 }
448 }
449
Willy Tarreau986a9d22012-08-30 21:11:38 +0200450 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200451 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100452 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200453
Willy Tarreaud2274c62012-07-06 14:29:45 +0200454 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200455 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200456 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200457
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200458 if (conn_xprt_init(conn) < 0) {
Willy Tarreau184636e2012-09-06 14:04:41 +0200459 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200460 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200461 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200462
Willy Tarreau14f8e862012-08-30 22:23:13 +0200463 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200464 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200465
Willy Tarreau9650f372009-08-16 14:02:45 +0200466 return SN_ERR_NONE; /* connection is OK */
467}
468
469
Willy Tarreau59b94792012-05-11 16:16:40 +0200470/*
471 * Retrieves the source address for the socket <fd>, with <dir> indicating
472 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
473 * success, -1 in case of error. The socket's source address is stored in
474 * <sa> for <salen> bytes.
475 */
476int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
477{
478 if (dir)
479 return getsockname(fd, sa, &salen);
480 else
481 return getpeername(fd, sa, &salen);
482}
483
484
485/*
486 * Retrieves the original destination address for the socket <fd>, with <dir>
487 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
488 * listener, if the original destination address was translated, the original
489 * address is retrieved. It returns 0 in case of success, -1 in case of error.
490 * The socket's source address is stored in <sa> for <salen> bytes.
491 */
492int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
493{
494 if (dir)
495 return getpeername(fd, sa, &salen);
496#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
497 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
498 return 0;
499#endif
500 else
501 return getsockname(fd, sa, &salen);
502}
503
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200504/* This is the callback which is set when a connection establishment is pending
505 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200506 * once the connection is established. It updates the FD polling status. It
507 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
508 * it returns non-zero and removes itself from the connection's flags (the bit is
509 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200510 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200511int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200512{
Willy Tarreau239d7182012-07-23 18:53:03 +0200513 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200514
Willy Tarreau80184712012-07-06 14:54:49 +0200515 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200516 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200517
Willy Tarreau80184712012-07-06 14:54:49 +0200518 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200519 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200520
Willy Tarreau2da156f2012-07-23 15:07:23 +0200521 /* stop here if we reached the end of data */
522 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
523 goto out_error;
524
Willy Tarreau2c6be842012-07-06 17:12:34 +0200525 /* We have no data to send to check the connection, and
526 * getsockopt() will not inform us whether the connection
527 * is still pending. So we'll reuse connect() to check the
528 * state of the socket. This has the advantage of giving us
529 * the following info :
530 * - error
531 * - connecting (EALREADY, EINPROGRESS)
532 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200533 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200534 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200535 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100536 __conn_sock_stop_recv(conn);
537 __conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200538 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200539 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200540
Willy Tarreau2c6be842012-07-06 17:12:34 +0200541 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200542 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200543
Willy Tarreau2c6be842012-07-06 17:12:34 +0200544 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200545 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200546
Willy Tarreau076be252012-07-06 16:02:29 +0200547 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200548 * forward the event to the transport layer which will notify the
549 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200550 */
Willy Tarreau80184712012-07-06 14:54:49 +0200551 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200552 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200553
554 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200555 /* Write error on the file descriptor. Report it to the connection
556 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200557 */
558
Willy Tarreau80184712012-07-06 14:54:49 +0200559 conn->flags |= CO_FL_ERROR;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100560 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200561 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200562}
563
Willy Tarreau59b94792012-05-11 16:16:40 +0200564
Willy Tarreaue6b98942007-10-29 01:09:36 +0100565/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100566 * an error message in <errmsg> if the message is at most <errlen> bytes long
567 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
568 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100569 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
570 * was alright and that no message was returned. ERR_RETRYABLE means that an
571 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700572 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100573 * the meaning of the error, but just indicate that a message is present which
574 * should be displayed with the respective level. Last, ERR_ABORT indicates
575 * that it's pointless to try to start other listeners. No error message is
576 * returned if errlen is NULL.
577 */
578int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
579{
580 __label__ tcp_return, tcp_close_return;
581 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100582 int ext, ready;
583 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100584 const char *msg = NULL;
585
586 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100587 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100588 *errmsg = 0;
589
590 if (listener->state != LI_ASSIGNED)
591 return ERR_NONE; /* already bound */
592
593 err = ERR_NONE;
594
Willy Tarreau40aa0702013-03-10 23:51:38 +0100595 /* if the listener already has an fd assigned, then we were offered the
596 * fd by an external process (most likely the parent), and we don't want
597 * to create a new socket. However we still want to set a few flags on
598 * the socket.
599 */
600 fd = listener->fd;
601 ext = (fd >= 0);
602
603 if (!ext && (fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100604 err |= ERR_RETRYABLE | ERR_ALERT;
605 msg = "cannot create listening socket";
606 goto tcp_return;
607 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100608
Willy Tarreaue6b98942007-10-29 01:09:36 +0100609 if (fd >= global.maxsock) {
610 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
611 msg = "not enough free sockets (raise '-n' parameter)";
612 goto tcp_close_return;
613 }
614
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200615 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100616 err |= ERR_FATAL | ERR_ALERT;
617 msg = "cannot make socket non-blocking";
618 goto tcp_close_return;
619 }
620
Willy Tarreau40aa0702013-03-10 23:51:38 +0100621 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100622 /* not fatal but should be reported */
623 msg = "cannot do so_reuseaddr";
624 err |= ERR_ALERT;
625 }
626
627 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900628 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100629
Willy Tarreaue6b98942007-10-29 01:09:36 +0100630#ifdef SO_REUSEPORT
631 /* OpenBSD supports this. As it's present in old libc versions of Linux,
632 * it might return an error that we will silently ignore.
633 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100634 if (!ext)
635 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100636#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +0200637
Willy Tarreau40aa0702013-03-10 23:51:38 +0100638 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200639 switch (listener->addr.ss_family) {
640 case AF_INET:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200641 if (1
642#if defined(IP_TRANSPARENT)
643 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
644#endif
645#if defined(IP_FREEBIND)
646 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)
647#endif
648 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200649 msg = "cannot make listening socket transparent";
650 err |= ERR_ALERT;
651 }
652 break;
653 case AF_INET6:
Pieter Baauwd551fb52013-05-08 22:49:23 +0200654 if (1
655#if defined(IPV6_TRANSPARENT)
656 && (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1)
657#endif
658 ) {
David du Colombier65c17962012-07-13 14:34:59 +0200659 msg = "cannot make listening socket transparent";
660 err |= ERR_ALERT;
661 }
662 break;
663 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100664 }
Pieter Baauwd551fb52013-05-08 22:49:23 +0200665
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100666#ifdef SO_BINDTODEVICE
667 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100668 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100669 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100670 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100671 msg = "cannot bind listener to device";
672 err |= ERR_WARN;
673 }
674 }
675#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400676#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100677 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400678 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200679 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
680 msg = "cannot set MSS";
681 err |= ERR_WARN;
682 }
683 }
684#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200685#if defined(TCP_DEFER_ACCEPT)
686 if (listener->options & LI_O_DEF_ACCEPT) {
687 /* defer accept by up to one second */
688 int accept_delay = 1;
689 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
690 msg = "cannot enable DEFER_ACCEPT";
691 err |= ERR_WARN;
692 }
693 }
694#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200695#if defined(TCP_FASTOPEN)
696 if (listener->options & LI_O_TCP_FO) {
697 /* TFO needs a queue length, let's use the configured backlog */
698 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
699 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
700 msg = "cannot enable TCP_FASTOPEN";
701 err |= ERR_WARN;
702 }
703 }
704#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100705#if defined(IPV6_V6ONLY)
706 if (listener->options & LI_O_V6ONLY)
707 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100708 else if (listener->options & LI_O_V4V6)
709 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100710#endif
711
Willy Tarreau40aa0702013-03-10 23:51:38 +0100712 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100713 err |= ERR_RETRYABLE | ERR_ALERT;
714 msg = "cannot bind socket";
715 goto tcp_close_return;
716 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100717
Willy Tarreau40aa0702013-03-10 23:51:38 +0100718 ready = 0;
719 ready_len = sizeof(ready);
720 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
721 ready = 0;
722
723 if (!(ext && ready) && /* only listen if not already done by external process */
724 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100725 err |= ERR_RETRYABLE | ERR_ALERT;
726 msg = "cannot listen to socket";
727 goto tcp_close_return;
728 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100729
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400730#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200731 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900732 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200733#endif
734
Willy Tarreaue6b98942007-10-29 01:09:36 +0100735 /* the socket is ready */
736 listener->fd = fd;
737 listener->state = LI_LISTEN;
738
Willy Tarreaueabf3132008-08-29 23:36:51 +0200739 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200740 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200741 fd_insert(fd);
742
Willy Tarreaue6b98942007-10-29 01:09:36 +0100743 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100744 if (msg && errlen) {
745 char pn[INET6_ADDRSTRLEN];
746
Willy Tarreau631f01c2011-09-05 00:36:48 +0200747 addr_to_str(&listener->addr, pn, sizeof(pn));
748 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100749 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100750 return err;
751
752 tcp_close_return:
753 close(fd);
754 goto tcp_return;
755}
756
757/* This function creates all TCP sockets bound to the protocol entry <proto>.
758 * It is intended to be used as the protocol's bind_all() function.
759 * The sockets will be registered but not added to any fd_set, in order not to
760 * loose them across the fork(). A call to enable_all_listeners() is needed
761 * to complete initialization. The return value is composed from ERR_*.
762 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200763static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100764{
765 struct listener *listener;
766 int err = ERR_NONE;
767
768 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200769 err |= tcp_bind_listener(listener, errmsg, errlen);
770 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100771 break;
772 }
773
774 return err;
775}
776
777/* Add listener to the list of tcpv4 listeners. The listener's state
778 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
779 * listeners is updated. This is the function to use to add a new listener.
780 */
781void tcpv4_add_listener(struct listener *listener)
782{
783 if (listener->state != LI_INIT)
784 return;
785 listener->state = LI_ASSIGNED;
786 listener->proto = &proto_tcpv4;
787 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
788 proto_tcpv4.nb_listeners++;
789}
790
791/* Add listener to the list of tcpv4 listeners. The listener's state
792 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
793 * listeners is updated. This is the function to use to add a new listener.
794 */
795void tcpv6_add_listener(struct listener *listener)
796{
797 if (listener->state != LI_INIT)
798 return;
799 listener->state = LI_ASSIGNED;
800 listener->proto = &proto_tcpv6;
801 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
802 proto_tcpv6.nb_listeners++;
803}
804
Willy Tarreauedcf6682008-11-30 23:15:34 +0100805/* This function performs the TCP request analysis on the current request. It
806 * returns 1 if the processing can continue on next analysers, or zero if it
807 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200808 * request. It relies on buffers flags, and updates s->req->analysers. The
809 * function may be called for frontend rules and backend rules. It only relies
810 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100811 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200812int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100813{
814 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200815 struct stksess *ts;
816 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100817 int partial;
818
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100819 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 +0100820 now_ms, __FUNCTION__,
821 s,
822 req,
823 req->rex, req->wex,
824 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200825 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100826 req->analysers);
827
Willy Tarreauedcf6682008-11-30 23:15:34 +0100828 /* We don't know whether we have enough data, so must proceed
829 * this way :
830 * - iterate through all rules in their declaration order
831 * - if one rule returns MISS, it means the inspect delay is
832 * not over yet, then return immediately, otherwise consider
833 * it as a non-match.
834 * - if one rule returns OK, then return OK
835 * - if one rule returns KO, then return KO
836 */
837
Willy Tarreau9b28e032012-10-12 23:49:43 +0200838 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200839 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200840 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100841 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200842 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100843
Willy Tarreaufb356202010-08-03 14:02:05 +0200844 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100845 int ret = ACL_PAT_PASS;
846
847 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200848 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100849 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200850 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100851 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200852 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
853 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100854 return 0;
855 }
856
857 ret = acl_pass(ret);
858 if (rule->cond->pol == ACL_COND_UNLESS)
859 ret = !ret;
860 }
861
862 if (ret) {
863 /* we have a matching rule. */
864 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200865 channel_abort(req);
866 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100867 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200868
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100869 s->be->be_counters.denied_req++;
870 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200871 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200872 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200873
Willy Tarreauedcf6682008-11-30 23:15:34 +0100874 if (!(s->flags & SN_ERR_MASK))
875 s->flags |= SN_ERR_PRXCOND;
876 if (!(s->flags & SN_FINST_MASK))
877 s->flags |= SN_FINST_R;
878 return 0;
879 }
Willy Tarreau20d46a52012-12-09 15:55:40 +0100880 else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) ||
881 (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100882 /* Note: only the first valid tracking parameter of each
883 * applies.
884 */
885 struct stktable_key *key;
886
887 t = rule->act_prm.trk_ctr.table.t;
888 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
889
890 if (key && (ts = stktable_get_entry(t, key))) {
891 if (rule->action == TCP_ACT_TRK_SC1) {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100892 session_track_stkctr(&s->stkctr[0], t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200893 if (s->fe != s->be)
894 s->flags |= SN_BE_TRACK_SC1;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100895 } else {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100896 session_track_stkctr(&s->stkctr[1], t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200897 if (s->fe != s->be)
898 s->flags |= SN_BE_TRACK_SC2;
899 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200900 }
901 }
902 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100903 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200904 break;
905 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100906 }
907 }
908
909 /* if we get there, it means we have no rule which matches, or
910 * we have an explicit accept, so we apply the default accept.
911 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200912 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100913 req->analyse_exp = TICK_ETERNITY;
914 return 1;
915}
916
Emeric Brun97679e72010-09-23 17:56:44 +0200917/* This function performs the TCP response analysis on the current response. It
918 * returns 1 if the processing can continue on next analysers, or zero if it
919 * needs more data, encounters an error, or wants to immediately abort the
920 * response. It relies on buffers flags, and updates s->rep->analysers. The
921 * function may be called for backend rules.
922 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200923int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200924{
925 struct tcp_rule *rule;
926 int partial;
927
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100928 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 +0200929 now_ms, __FUNCTION__,
930 s,
931 rep,
932 rep->rex, rep->wex,
933 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200934 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200935 rep->analysers);
936
937 /* We don't know whether we have enough data, so must proceed
938 * this way :
939 * - iterate through all rules in their declaration order
940 * - if one rule returns MISS, it means the inspect delay is
941 * not over yet, then return immediately, otherwise consider
942 * it as a non-match.
943 * - if one rule returns OK, then return OK
944 * - if one rule returns KO, then return KO
945 */
946
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200947 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200948 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200949 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200950 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200951
952 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
953 int ret = ACL_PAT_PASS;
954
955 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200956 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200957 if (ret == ACL_PAT_MISS) {
958 /* just set the analyser timeout once at the beginning of the response */
959 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
960 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
961 return 0;
962 }
963
964 ret = acl_pass(ret);
965 if (rule->cond->pol == ACL_COND_UNLESS)
966 ret = !ret;
967 }
968
969 if (ret) {
970 /* we have a matching rule. */
971 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200972 channel_abort(rep);
973 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200974 rep->analysers = 0;
975
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100976 s->be->be_counters.denied_resp++;
977 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200978 if (s->listener->counters)
979 s->listener->counters->denied_resp++;
980
981 if (!(s->flags & SN_ERR_MASK))
982 s->flags |= SN_ERR_PRXCOND;
983 if (!(s->flags & SN_FINST_MASK))
984 s->flags |= SN_FINST_D;
985 return 0;
986 }
987 else {
988 /* otherwise accept */
989 break;
990 }
991 }
992 }
993
994 /* if we get there, it means we have no rule which matches, or
995 * we have an explicit accept, so we apply the default accept.
996 */
997 rep->analysers &= ~an_bit;
998 rep->analyse_exp = TICK_ETERNITY;
999 return 1;
1000}
1001
1002
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001003/* This function performs the TCP layer4 analysis on the current request. It
1004 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
1005 * matches or if no more rule matches. It can only use rules which don't need
1006 * any data.
1007 */
1008int tcp_exec_req_rules(struct session *s)
1009{
1010 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001011 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001012 struct stktable *t = NULL;
1013 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001014 int ret;
1015
1016 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
1017 ret = ACL_PAT_PASS;
1018
1019 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001020 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001021 ret = acl_pass(ret);
1022 if (rule->cond->pol == ACL_COND_UNLESS)
1023 ret = !ret;
1024 }
1025
1026 if (ret) {
1027 /* we have a matching rule. */
1028 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001029 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001030 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001031 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001032
1033 if (!(s->flags & SN_ERR_MASK))
1034 s->flags |= SN_ERR_PRXCOND;
1035 if (!(s->flags & SN_FINST_MASK))
1036 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001037 result = 0;
1038 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001039 }
Willy Tarreau20d46a52012-12-09 15:55:40 +01001040 else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) ||
1041 (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001042 /* Note: only the first valid tracking parameter of each
1043 * applies.
1044 */
1045 struct stktable_key *key;
1046
1047 t = rule->act_prm.trk_ctr.table.t;
1048 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
1049
1050 if (key && (ts = stktable_get_entry(t, key))) {
1051 if (rule->action == TCP_ACT_TRK_SC1)
Willy Tarreau20d46a52012-12-09 15:55:40 +01001052 session_track_stkctr(&s->stkctr[0], t, ts);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001053 else
Willy Tarreau20d46a52012-12-09 15:55:40 +01001054 session_track_stkctr(&s->stkctr[1], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001055 }
1056 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001057 else {
1058 /* otherwise it's an accept */
1059 break;
1060 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001061 }
1062 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001063 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001064}
1065
Emeric Brun97679e72010-09-23 17:56:44 +02001066/* Parse a tcp-response rule. Return a negative value in case of failure */
1067static int tcp_parse_response_rule(char **args, int arg, int section_type,
1068 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau80aca902013-01-07 15:42:20 +01001069 struct tcp_rule *rule, char **err,
1070 unsigned int where)
Emeric Brun97679e72010-09-23 17:56:44 +02001071{
1072 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001073 memprintf(err, "%s %s is only allowed in 'backend' sections",
1074 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001075 return -1;
1076 }
1077
1078 if (strcmp(args[arg], "accept") == 0) {
1079 arg++;
1080 rule->action = TCP_ACT_ACCEPT;
1081 }
1082 else if (strcmp(args[arg], "reject") == 0) {
1083 arg++;
1084 rule->action = TCP_ACT_REJECT;
1085 }
1086 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001087 memprintf(err,
1088 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1089 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001090 return -1;
1091 }
1092
1093 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001094 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1095 memprintf(err,
1096 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1097 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001098 return -1;
1099 }
1100 }
1101 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001102 memprintf(err,
1103 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001104 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1105 return -1;
1106 }
1107 return 0;
1108}
1109
1110
1111
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001112/* Parse a tcp-request rule. Return a negative value in case of failure */
1113static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001114 struct proxy *curpx, struct proxy *defpx,
1115 struct tcp_rule *rule, char **err,
1116 unsigned int where)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001117{
1118 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001119 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1120 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001121 return -1;
1122 }
1123
1124 if (!strcmp(args[arg], "accept")) {
1125 arg++;
1126 rule->action = TCP_ACT_ACCEPT;
1127 }
1128 else if (!strcmp(args[arg], "reject")) {
1129 arg++;
1130 rule->action = TCP_ACT_REJECT;
1131 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001132 else if (strcmp(args[arg], "track-sc1") == 0 || strcmp(args[arg], "track-sc2") == 0) {
1133 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001134 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001135
1136 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001137
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001138 curpx->conf.args.ctx = ARGC_TRK;
1139 expr = sample_parse_expr(args, &arg, trash.str, trash.size, &curpx->conf.args);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001140 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001141 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001142 "'%s %s %s' : %s",
1143 args[0], args[1], args[kw], trash.str);
1144 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001145 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001146
Willy Tarreau80aca902013-01-07 15:42:20 +01001147 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001148 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001149 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
Willy Tarreau33c60de2013-04-12 08:26:32 +02001150 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001151 free(expr);
1152 return -1;
1153 }
1154
1155 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001156 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001157
1158 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001159 arg++;
1160 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001161 memprintf(err,
1162 "'%s %s %s' : missing table name",
1163 args[0], args[1], args[kw]);
1164 free(expr);
1165 return -1;
1166 }
1167 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001168 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001169 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001170 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001171 rule->act_prm.trk_ctr.expr = expr;
1172
1173 if (args[kw][8] == '1')
1174 rule->action = TCP_ACT_TRK_SC1;
1175 else
1176 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001177 }
1178 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001179 memprintf(err,
1180 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1181 "or 'track-sc2' in %s '%s' (got '%s')",
1182 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001183 return -1;
1184 }
1185
1186 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001187 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1188 memprintf(err,
1189 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1190 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001191 return -1;
1192 }
1193 }
1194 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001195 memprintf(err,
1196 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001197 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1198 return -1;
1199 }
1200 return 0;
1201}
1202
Emeric Brun97679e72010-09-23 17:56:44 +02001203/* This function should be called to parse a line starting with the "tcp-response"
1204 * keyword.
1205 */
1206static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001207 struct proxy *defpx, const char *file, int line,
1208 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001209{
1210 const char *ptr = NULL;
1211 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001212 int warn = 0;
1213 int arg;
1214 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001215 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001216 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001217 const char *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001218
1219 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001220 memprintf(err, "missing argument for '%s' in %s '%s'",
1221 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001222 return -1;
1223 }
1224
1225 if (strcmp(args[1], "inspect-delay") == 0) {
1226 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001227 memprintf(err, "%s %s is only allowed in 'backend' sections",
1228 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001229 return -1;
1230 }
1231
1232 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001233 memprintf(err,
1234 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1235 args[0], args[1], proxy_type_str(curpx), curpx->id);
1236 if (ptr)
1237 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001238 return -1;
1239 }
1240
1241 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001242 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1243 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001244 return 1;
1245 }
1246 curpx->tcp_rep.inspect_delay = val;
1247 return 0;
1248 }
1249
Simon Hormande072bd2011-06-24 15:11:37 +09001250 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001251 LIST_INIT(&rule->list);
1252 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001253 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001254
1255 if (strcmp(args[1], "content") == 0) {
1256 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001257
1258 if (curpx->cap & PR_CAP_FE)
1259 where |= SMP_VAL_FE_RES_CNT;
1260 if (curpx->cap & PR_CAP_BE)
1261 where |= SMP_VAL_BE_RES_CNT;
1262
1263 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001264 goto error;
1265
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001266 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1267 if (acl) {
1268 if (acl->name && *acl->name)
1269 memprintf(err,
1270 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1271 acl->name, args[0], args[1], sample_ckp_names(where));
1272 else
1273 memprintf(err,
1274 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1275 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001276 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001277 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001278
Emeric Brun97679e72010-09-23 17:56:44 +02001279 warn++;
1280 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001281 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1282 if (acl->name && *acl->name)
1283 memprintf(err,
1284 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001285 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001286 else
1287 memprintf(err,
1288 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001289 kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001290 warn++;
1291 }
Emeric Brun97679e72010-09-23 17:56:44 +02001292
1293 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1294 }
1295 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001296 memprintf(err,
1297 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1298 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001299 goto error;
1300 }
1301
1302 return warn;
1303 error:
1304 free(rule);
1305 return -1;
1306}
1307
1308
Willy Tarreaub6866442008-07-14 23:54:42 +02001309/* This function should be called to parse a line starting with the "tcp-request"
1310 * keyword.
1311 */
1312static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001313 struct proxy *defpx, const char *file, int line,
1314 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001315{
1316 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001317 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001318 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001319 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001320 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001321 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001322 const struct acl *acl;
Willy Tarreau93fddf12013-03-31 22:59:32 +02001323 const char *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001324
1325 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001326 if (curpx == defpx)
1327 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1328 else
1329 memprintf(err, "missing argument for '%s' in %s '%s'",
1330 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001331 return -1;
1332 }
1333
1334 if (!strcmp(args[1], "inspect-delay")) {
1335 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001336 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1337 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001338 return -1;
1339 }
1340
Willy Tarreaub6866442008-07-14 23:54:42 +02001341 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001342 memprintf(err,
1343 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1344 args[0], args[1], proxy_type_str(curpx), curpx->id);
1345 if (ptr)
1346 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001347 return -1;
1348 }
1349
1350 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001351 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1352 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001353 return 1;
1354 }
1355 curpx->tcp_req.inspect_delay = val;
1356 return 0;
1357 }
1358
Simon Hormande072bd2011-06-24 15:11:37 +09001359 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001360 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001361 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001362 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001363
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001364 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001365 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001366
1367 if (curpx->cap & PR_CAP_FE)
1368 where |= SMP_VAL_FE_REQ_CNT;
1369 if (curpx->cap & PR_CAP_BE)
1370 where |= SMP_VAL_BE_REQ_CNT;
1371
1372 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001373 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001374
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001375 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1376 if (acl) {
1377 if (acl->name && *acl->name)
1378 memprintf(err,
1379 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1380 acl->name, args[0], args[1], sample_ckp_names(where));
1381 else
1382 memprintf(err,
1383 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1384 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001385 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001386 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001387
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001388 warn++;
1389 }
1390 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1391 if (acl->name && *acl->name)
1392 memprintf(err,
1393 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001394 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001395 else
1396 memprintf(err,
1397 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001398 kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001399 warn++;
1400 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001401
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001402 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001403 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001404 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001405 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001406
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001407 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001408 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1409 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001410 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001411 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001412
Willy Tarreau80aca902013-01-07 15:42:20 +01001413 where |= SMP_VAL_FE_CON_ACC;
1414
1415 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001416 goto error;
1417
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001418 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1419 if (acl) {
1420 if (acl->name && *acl->name)
1421 memprintf(err,
1422 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1423 acl->name, args[0], args[1], sample_ckp_names(where));
1424 else
1425 memprintf(err,
1426 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1427 args[0], args[1],
Willy Tarreau93fddf12013-03-31 22:59:32 +02001428 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001429 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001430
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001431 warn++;
1432 }
1433 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1434 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001435 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001436 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001437 acl->name, kw, sample_ckp_names(where));
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001438 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001439 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001440 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001441 kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001442 warn++;
1443 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001444
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001445 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001446 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001447 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001448 if (curpx == defpx)
1449 memprintf(err,
1450 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1451 args[0], args[1]);
1452 else
1453 memprintf(err,
1454 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
Willy Tarreaudeaec2f2013-04-10 16:31:11 +02001455 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001456 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001457 }
1458
Willy Tarreau1a687942010-05-23 22:40:30 +02001459 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001460 error:
1461 free(rule);
1462 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001463}
1464
Willy Tarreau645513a2010-05-24 20:55:15 +02001465
1466/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001467/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001468/************************************************************************/
1469
Willy Tarreau4a129812012-04-25 17:31:42 +02001470/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001471static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001472smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001473 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001474{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001475 switch (l4->si[0].conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001476 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001477 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001478 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001479 break;
1480 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001481 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001482 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001483 break;
1484 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001485 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001486 }
Emeric Brunf769f512010-10-22 17:14:01 +02001487
Willy Tarreau37406352012-04-23 16:16:37 +02001488 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001489 return 1;
1490}
1491
Willy Tarreaua5e37562011-12-16 17:06:15 +01001492/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001493static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001494smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001495 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001496{
Willy Tarreauf853c462012-04-23 18:53:56 +02001497 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001498 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001499 return 0;
1500
Willy Tarreau37406352012-04-23 16:16:37 +02001501 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001502 return 1;
1503}
1504
Willy Tarreau4a129812012-04-25 17:31:42 +02001505/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001506static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001507smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001508 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001509{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001510 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001511
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001512 switch (l4->si[0].conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001513 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001514 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001515 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001516 break;
1517 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001518 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001519 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001520 break;
1521 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001522 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001523 }
Emeric Brunf769f512010-10-22 17:14:01 +02001524
Willy Tarreau37406352012-04-23 16:16:37 +02001525 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001526 return 1;
1527}
1528
Willy Tarreaua5e37562011-12-16 17:06:15 +01001529/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001530static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001531smp_fetch_dport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001532 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001533{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001534 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001535
Willy Tarreauf853c462012-04-23 18:53:56 +02001536 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001537 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001538 return 0;
1539
Willy Tarreau37406352012-04-23 16:16:37 +02001540 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001541 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001542}
1543
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001544#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001545/* parse the "v4v6" bind keyword */
1546static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1547{
1548 struct listener *l;
1549
1550 list_for_each_entry(l, &conf->listeners, by_bind) {
1551 if (l->addr.ss_family == AF_INET6)
1552 l->options |= LI_O_V4V6;
1553 }
1554
1555 return 0;
1556}
1557
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001558/* parse the "v6only" bind keyword */
1559static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1560{
1561 struct listener *l;
1562
1563 list_for_each_entry(l, &conf->listeners, by_bind) {
1564 if (l->addr.ss_family == AF_INET6)
1565 l->options |= LI_O_V6ONLY;
1566 }
1567
1568 return 0;
1569}
1570#endif
1571
Pieter Baauwd551fb52013-05-08 22:49:23 +02001572#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001573/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001574static 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 +02001575{
1576 struct listener *l;
1577
Willy Tarreau4348fad2012-09-20 16:48:07 +02001578 list_for_each_entry(l, &conf->listeners, by_bind) {
1579 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1580 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001581 }
1582
Willy Tarreau44791242012-09-12 23:27:21 +02001583 return 0;
1584}
1585#endif
1586
1587#ifdef TCP_DEFER_ACCEPT
1588/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001589static 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 +02001590{
1591 struct listener *l;
1592
Willy Tarreau4348fad2012-09-20 16:48:07 +02001593 list_for_each_entry(l, &conf->listeners, by_bind) {
1594 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1595 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001596 }
1597
Willy Tarreau44791242012-09-12 23:27:21 +02001598 return 0;
1599}
1600#endif
1601
Willy Tarreau1c862c52012-10-05 16:21:00 +02001602#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001603/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001604static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1605{
1606 struct listener *l;
1607
1608 list_for_each_entry(l, &conf->listeners, by_bind) {
1609 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1610 l->options |= LI_O_TCP_FO;
1611 }
1612
1613 return 0;
1614}
1615#endif
1616
Willy Tarreau44791242012-09-12 23:27:21 +02001617#ifdef TCP_MAXSEG
1618/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001619static 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 +02001620{
1621 struct listener *l;
1622 int mss;
1623
Willy Tarreau44791242012-09-12 23:27:21 +02001624 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001625 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001626 return ERR_ALERT | ERR_FATAL;
1627 }
1628
1629 mss = atoi(args[cur_arg + 1]);
1630 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001631 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001632 return ERR_ALERT | ERR_FATAL;
1633 }
1634
Willy Tarreau4348fad2012-09-20 16:48:07 +02001635 list_for_each_entry(l, &conf->listeners, by_bind) {
1636 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1637 l->maxseg = mss;
1638 }
Willy Tarreau44791242012-09-12 23:27:21 +02001639
1640 return 0;
1641}
1642#endif
1643
1644#ifdef SO_BINDTODEVICE
1645/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001646static 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 +02001647{
1648 struct listener *l;
1649
Willy Tarreau44791242012-09-12 23:27:21 +02001650 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001651 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001652 return ERR_ALERT | ERR_FATAL;
1653 }
1654
Willy Tarreau4348fad2012-09-20 16:48:07 +02001655 list_for_each_entry(l, &conf->listeners, by_bind) {
1656 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1657 l->interface = strdup(args[cur_arg + 1]);
1658 }
Willy Tarreau44791242012-09-12 23:27:21 +02001659
1660 global.last_checks |= LSTCHK_NETADM;
1661 return 0;
1662}
1663#endif
1664
Willy Tarreaub6866442008-07-14 23:54:42 +02001665static struct cfg_kw_list cfg_kws = {{ },{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001666 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001667 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001668 { 0, NULL, NULL },
1669}};
1670
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001671
Willy Tarreau61612d42012-04-19 18:42:05 +02001672/* Note: must not be declared <const> as its list will be overwritten.
1673 * Please take care of keeping this list alphabetically sorted.
1674 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001675static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaud86e29d2013-03-25 08:21:05 +01001676 { "dst", NULL, acl_parse_ip, acl_match_ip },
1677 { "dst_port", NULL, acl_parse_int, acl_match_int },
1678 { "src", NULL, acl_parse_ip, acl_match_ip },
1679 { "src_port", NULL, acl_parse_int, acl_match_int },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001680 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02001681}};
1682
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001683
Willy Tarreau4a129812012-04-25 17:31:42 +02001684/* Note: must not be declared <const> as its list will be overwritten.
1685 * Note: fetches that may return multiple types must be declared as the lowest
1686 * common denominator, the type that can be casted into all other ones. For
1687 * instance v4/v6 must be declared v4.
1688 */
Willy Tarreau12785782012-04-27 21:37:17 +02001689static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001690 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1691 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1692 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1693 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1694 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001695}};
1696
Willy Tarreau44791242012-09-12 23:27:21 +02001697/************************************************************************/
1698/* All supported bind keywords must be declared here. */
1699/************************************************************************/
1700
1701/* Note: must not be declared <const> as its list will be overwritten.
1702 * Please take care of keeping this list alphabetically sorted, doing so helps
1703 * all code contributors.
1704 * Optional keywords are also declared with a NULL ->parse() function so that
1705 * the config parser can report an appropriate error when a known keyword was
1706 * not enabled.
1707 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001708static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001709#ifdef TCP_DEFER_ACCEPT
1710 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1711#endif
1712#ifdef SO_BINDTODEVICE
1713 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1714#endif
1715#ifdef TCP_MAXSEG
1716 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1717#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001718#ifdef TCP_FASTOPEN
1719 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1720#endif
Pieter Baauwd551fb52013-05-08 22:49:23 +02001721#ifdef CONFIG_HAP_TRANSPARENT
Willy Tarreau44791242012-09-12 23:27:21 +02001722 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1723#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001724#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001725 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001726 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1727#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001728 /* the versions with the NULL parse function*/
1729 { "defer-accept", NULL, 0 },
1730 { "interface", NULL, 1 },
1731 { "mss", NULL, 1 },
1732 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001733 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001734 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001735 { NULL, NULL, 0 },
1736}};
1737
Willy Tarreaue6b98942007-10-29 01:09:36 +01001738__attribute__((constructor))
1739static void __tcp_protocol_init(void)
1740{
1741 protocol_register(&proto_tcpv4);
1742 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001743 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001744 cfg_register_keywords(&cfg_kws);
1745 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001746 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001747}
1748
1749
1750/*
1751 * Local variables:
1752 * c-indent-level: 8
1753 * c-basic-offset: 8
1754 * End:
1755 */