blob: e8d7d60c9b35fd02be14a74751efe314f63ae4fa [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;
125
126#ifdef CONFIG_HAP_LINUX_TPROXY
127 static int ip_transp_working = 1;
David du Colombier65c17962012-07-13 14:34:59 +0200128 static int ip6_transp_working = 1;
129 switch (local->ss_family) {
130 case AF_INET:
131 if (flags && ip_transp_working) {
132 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0
133 || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0)
134 foreign_ok = 1;
135 else
136 ip_transp_working = 0;
137 }
138 break;
139 case AF_INET6:
140 if (flags && ip6_transp_working) {
141 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == 0)
142 foreign_ok = 1;
143 else
144 ip6_transp_working = 0;
145 }
146 break;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100147 }
148#endif
149 if (flags) {
150 memset(&bind_addr, 0, sizeof(bind_addr));
Willy Tarreau96dd0792011-04-19 07:20:57 +0200151 bind_addr.ss_family = remote->ss_family;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100152 switch (remote->ss_family) {
153 case AF_INET:
154 if (flags & 1)
155 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
156 if (flags & 2)
157 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
158 break;
159 case AF_INET6:
160 if (flags & 1)
161 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
162 if (flags & 2)
163 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
164 break;
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100165 default:
166 /* we don't want to try to bind to an unknown address family */
167 foreign_ok = 0;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100168 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100169 }
170
Simon Hormande072bd2011-06-24 15:11:37 +0900171 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100172 if (foreign_ok) {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200173 if (is_addr(&bind_addr)) {
174 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
175 if (ret < 0)
176 return 2;
177 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100178 }
179 else {
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200180 if (is_addr(local)) {
181 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
182 if (ret < 0)
183 return 1;
184 }
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100185 }
186
187 if (!flags)
188 return 0;
189
190#ifdef CONFIG_HAP_CTTPROXY
Willy Tarreau6f831b42011-03-20 14:03:54 +0100191 if (!foreign_ok && remote->ss_family == AF_INET) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100192 struct in_tproxy itp1, itp2;
193 memset(&itp1, 0, sizeof(itp1));
194
195 itp1.op = TPROXY_ASSIGN;
Willy Tarreau6f831b42011-03-20 14:03:54 +0100196 itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr;
197 itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port;
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100198
199 /* set connect flag on socket */
200 itp2.op = TPROXY_FLAGS;
201 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
202
203 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
204 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
205 foreign_ok = 1;
206 }
207 }
208#endif
209 if (!foreign_ok)
210 /* we could not bind to a foreign address */
211 return 2;
212
213 return 0;
214}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100215
Willy Tarreau9650f372009-08-16 14:02:45 +0200216
217/*
Willy Tarreau14f8e862012-08-30 22:23:13 +0200218 * This function initiates a TCP connection establishment to the target assigned
219 * to connection <conn> using (si->{target,addr.to}). A source address may be
220 * pointed to by conn->addr.from in case of transparent proxying. Normal source
221 * bind addresses are still determined locally (due to the possible need of a
222 * source port). conn->target may point either to a valid server or to a backend,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100223 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
Willy Tarreauf0837b22012-11-24 10:24:27 +0100224 * supported. The <data> parameter is a boolean indicating whether there are data
225 * waiting for being sent or not, in order to adjust data write polling and on
226 * some platforms, the ability to avoid an empty initial ACK. The <delack> argument
227 * allows the caller to force using a delayed ACK when establishing the connection :
228 * - 0 = no delayed ACK unless data are advertised and backend has tcp-smart-connect
229 * - 1 = delayed ACK if backend has tcp-smart-connect, regardless of data
230 * - 2 = delayed ACK regardless of backend options
Willy Tarreaub1d67742010-03-29 19:36:59 +0200231 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200232 * It can return one of :
233 * - SN_ERR_NONE if everything's OK
234 * - SN_ERR_SRVTO if there are no more servers
235 * - SN_ERR_SRVCL if the connection was refused by the server
236 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
237 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
238 * - SN_ERR_INTERNAL for any other purely internal errors
239 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau6b0a8502012-11-23 08:51:32 +0100240 *
241 * The connection's fd is inserted only when SN_ERR_NONE is returned, otherwise
242 * it's invalid and the caller has nothing to do.
Willy Tarreau9650f372009-08-16 14:02:45 +0200243 */
Willy Tarreauf1536862011-03-03 18:27:32 +0100244
Willy Tarreauf0837b22012-11-24 10:24:27 +0100245int tcp_connect_server(struct connection *conn, int data, int delack)
Willy Tarreau9650f372009-08-16 14:02:45 +0200246{
247 int fd;
Willy Tarreauac825402011-03-04 22:04:29 +0100248 struct server *srv;
249 struct proxy *be;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100250 struct conn_src *src;
Willy Tarreauac825402011-03-04 22:04:29 +0100251
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100252 switch (obj_type(conn->target)) {
253 case OBJ_TYPE_PROXY:
254 be = objt_proxy(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100255 srv = NULL;
256 break;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100257 case OBJ_TYPE_SERVER:
258 srv = objt_server(conn->target);
Willy Tarreauac825402011-03-04 22:04:29 +0100259 be = srv->proxy;
260 break;
261 default:
262 return SN_ERR_INTERNAL;
263 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200264
Willy Tarreau986a9d22012-08-30 21:11:38 +0200265 if ((fd = conn->t.sock.fd = socket(conn->addr.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200266 qfprintf(stderr, "Cannot get a server socket.\n");
267
268 if (errno == ENFILE)
269 send_log(be, LOG_EMERG,
270 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
271 be->id, maxfd);
272 else if (errno == EMFILE)
273 send_log(be, LOG_EMERG,
274 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
275 be->id, maxfd);
276 else if (errno == ENOBUFS || errno == ENOMEM)
277 send_log(be, LOG_EMERG,
278 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
279 be->id, maxfd);
280 /* this is a resource error */
281 return SN_ERR_RESOURCE;
282 }
283
284 if (fd >= global.maxsock) {
285 /* do not log anything there, it's a normal condition when this option
286 * is used to serialize connections to a server !
287 */
288 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
289 close(fd);
290 return SN_ERR_PRXCOND; /* it is a configuration limit */
291 }
292
293 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
Simon Hormande072bd2011-06-24 15:11:37 +0900294 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200295 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
296 close(fd);
297 return SN_ERR_INTERNAL;
298 }
299
300 if (be->options & PR_O_TCP_SRV_KA)
Simon Hormande072bd2011-06-24 15:11:37 +0900301 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
Willy Tarreau9650f372009-08-16 14:02:45 +0200302
Willy Tarreau9650f372009-08-16 14:02:45 +0200303 /* allow specific binding :
304 * - server-specific at first
305 * - proxy-specific next
306 */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100307 if (srv && srv->conn_src.opts & CO_SRC_BIND)
308 src = &srv->conn_src;
309 else if (be->conn_src.opts & CO_SRC_BIND)
310 src = &be->conn_src;
311 else
312 src = NULL;
313
314 if (src) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200315 int ret, flags = 0;
316
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200317 if (is_addr(&conn->addr.from)) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100318 switch (src->opts & CO_SRC_TPROXY_MASK) {
Willy Tarreauef9a3602012-12-08 22:29:20 +0100319 case CO_SRC_TPROXY_ADDR:
320 case CO_SRC_TPROXY_CLI:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200321 flags = 3;
322 break;
Willy Tarreauef9a3602012-12-08 22:29:20 +0100323 case CO_SRC_TPROXY_CIP:
324 case CO_SRC_TPROXY_DYN:
Willy Tarreau5f2877a2012-10-26 19:57:58 +0200325 flags = 1;
326 break;
327 }
Willy Tarreau9650f372009-08-16 14:02:45 +0200328 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200329
Willy Tarreau9650f372009-08-16 14:02:45 +0200330#ifdef SO_BINDTODEVICE
331 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100332 if (src->iface_name)
333 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
Willy Tarreau9650f372009-08-16 14:02:45 +0200334#endif
335
Willy Tarreaua4380b42012-12-08 22:49:11 +0100336 if (src->sport_range) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200337 int attempts = 10; /* should be more than enough to find a spare port */
Willy Tarreaua4380b42012-12-08 22:49:11 +0100338 struct sockaddr_storage sa;
Willy Tarreau9650f372009-08-16 14:02:45 +0200339
340 ret = 1;
Willy Tarreaua4380b42012-12-08 22:49:11 +0100341 sa = src->source_addr;
Willy Tarreau9650f372009-08-16 14:02:45 +0200342
343 do {
344 /* note: in case of retry, we may have to release a previously
345 * allocated port, hence this loop's construct.
346 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200347 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
348 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200349
350 if (!attempts)
351 break;
352 attempts--;
353
Willy Tarreaua4380b42012-12-08 22:49:11 +0100354 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200355 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200356 break;
357
Willy Tarreaua4380b42012-12-08 22:49:11 +0100358 fdinfo[fd].port_range = src->sport_range;
359 set_host_port(&sa, fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200360
Willy Tarreaua4380b42012-12-08 22:49:11 +0100361 ret = tcp_bind_socket(fd, flags, &sa, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200362 } while (ret != 0); /* binding NOK */
363 }
364 else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100365 ret = tcp_bind_socket(fd, flags, &src->source_addr, &conn->addr.from);
Willy Tarreau9650f372009-08-16 14:02:45 +0200366 }
367
Willy Tarreaua4380b42012-12-08 22:49:11 +0100368 if (unlikely(ret != 0)) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200369 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
370 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200371 close(fd);
372
Willy Tarreau9650f372009-08-16 14:02:45 +0200373 if (ret == 1) {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100374 Alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200375 be->id);
376 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100377 "Cannot bind to source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200378 be->id);
379 } else {
Willy Tarreaua4380b42012-12-08 22:49:11 +0100380 Alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200381 be->id);
382 send_log(be, LOG_EMERG,
Willy Tarreaua4380b42012-12-08 22:49:11 +0100383 "Cannot bind to tproxy source address before connect() for backend %s.\n",
Willy Tarreau9650f372009-08-16 14:02:45 +0200384 be->id);
385 }
386 return SN_ERR_RESOURCE;
387 }
388 }
389
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400390#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200391 /* disabling tcp quick ack now allows the first request to leave the
392 * machine with the first ACK. We only do this if there are pending
Willy Tarreauf0837b22012-11-24 10:24:27 +0100393 * data in the buffer.
Willy Tarreau9650f372009-08-16 14:02:45 +0200394 */
Willy Tarreauf0837b22012-11-24 10:24:27 +0100395 if (delack == 2 || ((delack || data) && (be->options2 & PR_O2_SMARTCON)))
Simon Hormande072bd2011-06-24 15:11:37 +0900396 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200397#endif
398
Willy Tarreaue803de22010-01-21 17:43:04 +0100399 if (global.tune.server_sndbuf)
400 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
401
402 if (global.tune.server_rcvbuf)
403 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
404
Willy Tarreau986a9d22012-08-30 21:11:38 +0200405 if ((connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) == -1) &&
Willy Tarreau9650f372009-08-16 14:02:45 +0200406 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
407
Willy Tarreaub1719512012-12-08 23:03:28 +0100408 if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200409 char *msg;
Willy Tarreaub1719512012-12-08 23:03:28 +0100410 if (errno == EAGAIN || errno == EADDRNOTAVAIL)
Willy Tarreau9650f372009-08-16 14:02:45 +0200411 msg = "no free ports";
412 else
413 msg = "local address already in use";
414
Willy Tarreaub1719512012-12-08 23:03:28 +0100415 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200416 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
417 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200418 close(fd);
Willy Tarreaub1719512012-12-08 23:03:28 +0100419 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
Willy Tarreau9650f372009-08-16 14:02:45 +0200420 return SN_ERR_RESOURCE;
421 } else if (errno == ETIMEDOUT) {
422 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200423 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
424 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200425 close(fd);
426 return SN_ERR_SRVTO;
427 } else {
428 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
429 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200430 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
431 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200432 close(fd);
433 return SN_ERR_SRVCL;
434 }
435 }
436
Willy Tarreau986a9d22012-08-30 21:11:38 +0200437 fdtab[fd].owner = conn;
Willy Tarreau986a9d22012-08-30 21:11:38 +0200438 conn->flags = CO_FL_WAIT_L4_CONN; /* connection in progress */
Willy Tarreaufc8f1f02012-12-08 18:53:44 +0100439 conn->flags |= CO_FL_ADDR_TO_SET;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200440
Willy Tarreaud2274c62012-07-06 14:29:45 +0200441 fdtab[fd].iocb = conn_fd_handler;
Willy Tarreau9650f372009-08-16 14:02:45 +0200442 fd_insert(fd);
Willy Tarreau986a9d22012-08-30 21:11:38 +0200443 conn_sock_want_send(conn); /* for connect status */
Willy Tarreau15678ef2012-08-31 13:54:11 +0200444
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200445 if (conn_xprt_init(conn) < 0) {
Willy Tarreau184636e2012-09-06 14:04:41 +0200446 fd_delete(fd);
Willy Tarreau15678ef2012-08-31 13:54:11 +0200447 return SN_ERR_RESOURCE;
Willy Tarreau184636e2012-09-06 14:04:41 +0200448 }
Willy Tarreau15678ef2012-08-31 13:54:11 +0200449
Willy Tarreau14f8e862012-08-30 22:23:13 +0200450 if (data)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200451 conn_data_want_send(conn); /* prepare to send data if any */
Willy Tarreau9650f372009-08-16 14:02:45 +0200452
Willy Tarreau9650f372009-08-16 14:02:45 +0200453 return SN_ERR_NONE; /* connection is OK */
454}
455
456
Willy Tarreau59b94792012-05-11 16:16:40 +0200457/*
458 * Retrieves the source address for the socket <fd>, with <dir> indicating
459 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
460 * success, -1 in case of error. The socket's source address is stored in
461 * <sa> for <salen> bytes.
462 */
463int tcp_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
464{
465 if (dir)
466 return getsockname(fd, sa, &salen);
467 else
468 return getpeername(fd, sa, &salen);
469}
470
471
472/*
473 * Retrieves the original destination address for the socket <fd>, with <dir>
474 * indicating if we're a listener (=0) or an initiator (!=0). In the case of a
475 * listener, if the original destination address was translated, the original
476 * address is retrieved. It returns 0 in case of success, -1 in case of error.
477 * The socket's source address is stored in <sa> for <salen> bytes.
478 */
479int tcp_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
480{
481 if (dir)
482 return getpeername(fd, sa, &salen);
483#if defined(TPROXY) && defined(SO_ORIGINAL_DST)
484 else if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, &salen) == 0)
485 return 0;
486#endif
487 else
488 return getsockname(fd, sa, &salen);
489}
490
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200491/* This is the callback which is set when a connection establishment is pending
492 * and we have nothing to send, or if we have an init function we want to call
Willy Tarreauafad0e02012-08-09 14:45:22 +0200493 * once the connection is established. It updates the FD polling status. It
494 * returns 0 if it fails in a fatal way or needs to poll to go further, otherwise
495 * it returns non-zero and removes itself from the connection's flags (the bit is
496 * provided in <flag> by the caller).
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200497 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200498int tcp_connect_probe(struct connection *conn)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200499{
Willy Tarreau239d7182012-07-23 18:53:03 +0200500 int fd = conn->t.sock.fd;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200501
Willy Tarreau80184712012-07-06 14:54:49 +0200502 if (conn->flags & CO_FL_ERROR)
Willy Tarreauafad0e02012-08-09 14:45:22 +0200503 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200504
Willy Tarreau80184712012-07-06 14:54:49 +0200505 if (!(conn->flags & CO_FL_WAIT_L4_CONN))
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200506 return 1; /* strange we were called while ready */
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200507
Willy Tarreau2da156f2012-07-23 15:07:23 +0200508 /* stop here if we reached the end of data */
509 if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP)) == FD_POLL_HUP)
510 goto out_error;
511
Willy Tarreau2c6be842012-07-06 17:12:34 +0200512 /* We have no data to send to check the connection, and
513 * getsockopt() will not inform us whether the connection
514 * is still pending. So we'll reuse connect() to check the
515 * state of the socket. This has the advantage of giving us
516 * the following info :
517 * - error
518 * - connecting (EALREADY, EINPROGRESS)
519 * - connected (EISCONN, 0)
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200520 */
Willy Tarreau986a9d22012-08-30 21:11:38 +0200521 if (connect(fd, (struct sockaddr *)&conn->addr.to, get_addr_len(&conn->addr.to)) < 0) {
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200522 if (errno == EALREADY || errno == EINPROGRESS) {
Willy Tarreaud486ef52012-12-10 17:03:52 +0100523 __conn_sock_stop_recv(conn);
524 __conn_sock_poll_send(conn);
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200525 return 0;
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200526 }
Willy Tarreaua190d592012-05-20 18:35:19 +0200527
Willy Tarreau2c6be842012-07-06 17:12:34 +0200528 if (errno && errno != EISCONN)
Willy Tarreaua190d592012-05-20 18:35:19 +0200529 goto out_error;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200530
Willy Tarreau2c6be842012-07-06 17:12:34 +0200531 /* otherwise we're connected */
Willy Tarreaua190d592012-05-20 18:35:19 +0200532 }
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200533
Willy Tarreau076be252012-07-06 16:02:29 +0200534 /* The FD is ready now, we'll mark the connection as complete and
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200535 * forward the event to the transport layer which will notify the
536 * data layer.
Willy Tarreaua190d592012-05-20 18:35:19 +0200537 */
Willy Tarreau80184712012-07-06 14:54:49 +0200538 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200539 return 1;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200540
541 out_error:
Willy Tarreau0b0c0972012-07-23 20:05:00 +0200542 /* Write error on the file descriptor. Report it to the connection
543 * and disable polling on this FD.
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200544 */
545
Willy Tarreau80184712012-07-06 14:54:49 +0200546 conn->flags |= CO_FL_ERROR;
Willy Tarreaud486ef52012-12-10 17:03:52 +0100547 __conn_sock_stop_both(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200548 return 0;
Willy Tarreaueeda90e2012-05-11 19:53:32 +0200549}
550
Willy Tarreau59b94792012-05-11 16:16:40 +0200551
Willy Tarreaue6b98942007-10-29 01:09:36 +0100552/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100553 * an error message in <errmsg> if the message is at most <errlen> bytes long
554 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
555 * The return value is composed from ERR_ABORT, ERR_WARN,
Willy Tarreaue6b98942007-10-29 01:09:36 +0100556 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
557 * was alright and that no message was returned. ERR_RETRYABLE means that an
558 * error occurred but that it may vanish after a retry (eg: port in use), and
Aman Guptad94991d2012-04-06 17:39:26 -0700559 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
Willy Tarreaue6b98942007-10-29 01:09:36 +0100560 * the meaning of the error, but just indicate that a message is present which
561 * should be displayed with the respective level. Last, ERR_ABORT indicates
562 * that it's pointless to try to start other listeners. No error message is
563 * returned if errlen is NULL.
564 */
565int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
566{
567 __label__ tcp_return, tcp_close_return;
568 int fd, err;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100569 int ext, ready;
570 socklen_t ready_len;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100571 const char *msg = NULL;
572
573 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100574 if (errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100575 *errmsg = 0;
576
577 if (listener->state != LI_ASSIGNED)
578 return ERR_NONE; /* already bound */
579
580 err = ERR_NONE;
581
Willy Tarreau40aa0702013-03-10 23:51:38 +0100582 /* if the listener already has an fd assigned, then we were offered the
583 * fd by an external process (most likely the parent), and we don't want
584 * to create a new socket. However we still want to set a few flags on
585 * the socket.
586 */
587 fd = listener->fd;
588 ext = (fd >= 0);
589
590 if (!ext && (fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100591 err |= ERR_RETRYABLE | ERR_ALERT;
592 msg = "cannot create listening socket";
593 goto tcp_return;
594 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100595
Willy Tarreaue6b98942007-10-29 01:09:36 +0100596 if (fd >= global.maxsock) {
597 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
598 msg = "not enough free sockets (raise '-n' parameter)";
599 goto tcp_close_return;
600 }
601
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200602 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100603 err |= ERR_FATAL | ERR_ALERT;
604 msg = "cannot make socket non-blocking";
605 goto tcp_close_return;
606 }
607
Willy Tarreau40aa0702013-03-10 23:51:38 +0100608 if (!ext && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100609 /* not fatal but should be reported */
610 msg = "cannot do so_reuseaddr";
611 err |= ERR_ALERT;
612 }
613
614 if (listener->options & LI_O_NOLINGER)
Simon Hormande072bd2011-06-24 15:11:37 +0900615 setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100616
Willy Tarreaue6b98942007-10-29 01:09:36 +0100617#ifdef SO_REUSEPORT
618 /* OpenBSD supports this. As it's present in old libc versions of Linux,
619 * it might return an error that we will silently ignore.
620 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100621 if (!ext)
622 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
Willy Tarreaue6b98942007-10-29 01:09:36 +0100623#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100624#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreau40aa0702013-03-10 23:51:38 +0100625 if (!ext && (listener->options & LI_O_FOREIGN)) {
David du Colombier65c17962012-07-13 14:34:59 +0200626 switch (listener->addr.ss_family) {
627 case AF_INET:
628 if ((setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1)
629 && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) {
630 msg = "cannot make listening socket transparent";
631 err |= ERR_ALERT;
632 }
633 break;
634 case AF_INET6:
635 if (setsockopt(fd, SOL_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)) == -1) {
636 msg = "cannot make listening socket transparent";
637 err |= ERR_ALERT;
638 }
639 break;
640 }
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100641 }
642#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100643#ifdef SO_BINDTODEVICE
644 /* Note: this might fail if not CAP_NET_RAW */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100645 if (!ext && listener->interface) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100646 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100647 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100648 msg = "cannot bind listener to device";
649 err |= ERR_WARN;
650 }
651 }
652#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400653#if defined(TCP_MAXSEG)
Willy Tarreau48a7e722010-12-24 15:26:39 +0100654 if (listener->maxseg > 0) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400655 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200656 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
657 msg = "cannot set MSS";
658 err |= ERR_WARN;
659 }
660 }
661#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200662#if defined(TCP_DEFER_ACCEPT)
663 if (listener->options & LI_O_DEF_ACCEPT) {
664 /* defer accept by up to one second */
665 int accept_delay = 1;
666 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
667 msg = "cannot enable DEFER_ACCEPT";
668 err |= ERR_WARN;
669 }
670 }
671#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +0200672#if defined(TCP_FASTOPEN)
673 if (listener->options & LI_O_TCP_FO) {
674 /* TFO needs a queue length, let's use the configured backlog */
675 int qlen = listener->backlog ? listener->backlog : listener->maxconn;
676 if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1) {
677 msg = "cannot enable TCP_FASTOPEN";
678 err |= ERR_WARN;
679 }
680 }
681#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100682#if defined(IPV6_V6ONLY)
683 if (listener->options & LI_O_V6ONLY)
684 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Willy Tarreau77e3af92012-11-24 15:07:23 +0100685 else if (listener->options & LI_O_V4V6)
686 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));
Willy Tarreau9b6700f2012-11-24 11:55:28 +0100687#endif
688
Willy Tarreau40aa0702013-03-10 23:51:38 +0100689 if (!ext && bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100690 err |= ERR_RETRYABLE | ERR_ALERT;
691 msg = "cannot bind socket";
692 goto tcp_close_return;
693 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100694
Willy Tarreau40aa0702013-03-10 23:51:38 +0100695 ready = 0;
696 ready_len = sizeof(ready);
697 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
698 ready = 0;
699
700 if (!(ext && ready) && /* only listen if not already done by external process */
701 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100702 err |= ERR_RETRYABLE | ERR_ALERT;
703 msg = "cannot listen to socket";
704 goto tcp_close_return;
705 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100706
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400707#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200708 if (listener->options & LI_O_NOQUICKACK)
Simon Hormande072bd2011-06-24 15:11:37 +0900709 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200710#endif
711
Willy Tarreaue6b98942007-10-29 01:09:36 +0100712 /* the socket is ready */
713 listener->fd = fd;
714 listener->state = LI_LISTEN;
715
Willy Tarreaueabf3132008-08-29 23:36:51 +0200716 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreauaece46a2012-07-06 12:25:58 +0200717 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueb472682010-05-28 18:46:57 +0200718 fd_insert(fd);
719
Willy Tarreaue6b98942007-10-29 01:09:36 +0100720 tcp_return:
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100721 if (msg && errlen) {
722 char pn[INET6_ADDRSTRLEN];
723
Willy Tarreau631f01c2011-09-05 00:36:48 +0200724 addr_to_str(&listener->addr, pn, sizeof(pn));
725 snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr));
Cyril Bonté43ba1b32010-11-01 19:26:01 +0100726 }
Willy Tarreaue6b98942007-10-29 01:09:36 +0100727 return err;
728
729 tcp_close_return:
730 close(fd);
731 goto tcp_return;
732}
733
734/* This function creates all TCP sockets bound to the protocol entry <proto>.
735 * It is intended to be used as the protocol's bind_all() function.
736 * The sockets will be registered but not added to any fd_set, in order not to
737 * loose them across the fork(). A call to enable_all_listeners() is needed
738 * to complete initialization. The return value is composed from ERR_*.
739 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200740static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100741{
742 struct listener *listener;
743 int err = ERR_NONE;
744
745 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200746 err |= tcp_bind_listener(listener, errmsg, errlen);
747 if (err & ERR_ABORT)
Willy Tarreaue6b98942007-10-29 01:09:36 +0100748 break;
749 }
750
751 return err;
752}
753
754/* Add listener to the list of tcpv4 listeners. The listener's state
755 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
756 * listeners is updated. This is the function to use to add a new listener.
757 */
758void tcpv4_add_listener(struct listener *listener)
759{
760 if (listener->state != LI_INIT)
761 return;
762 listener->state = LI_ASSIGNED;
763 listener->proto = &proto_tcpv4;
764 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
765 proto_tcpv4.nb_listeners++;
766}
767
768/* Add listener to the list of tcpv4 listeners. The listener's state
769 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
770 * listeners is updated. This is the function to use to add a new listener.
771 */
772void tcpv6_add_listener(struct listener *listener)
773{
774 if (listener->state != LI_INIT)
775 return;
776 listener->state = LI_ASSIGNED;
777 listener->proto = &proto_tcpv6;
778 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
779 proto_tcpv6.nb_listeners++;
780}
781
Willy Tarreauedcf6682008-11-30 23:15:34 +0100782/* This function performs the TCP request analysis on the current request. It
783 * returns 1 if the processing can continue on next analysers, or zero if it
784 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreaufb356202010-08-03 14:02:05 +0200785 * request. It relies on buffers flags, and updates s->req->analysers. The
786 * function may be called for frontend rules and backend rules. It only relies
787 * on the backend pointer so this works for both cases.
Willy Tarreauedcf6682008-11-30 23:15:34 +0100788 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200789int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100790{
791 struct tcp_rule *rule;
Willy Tarreaud1f96522010-08-03 19:34:32 +0200792 struct stksess *ts;
793 struct stktable *t;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100794 int partial;
795
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100796 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 +0100797 now_ms, __FUNCTION__,
798 s,
799 req,
800 req->rex, req->wex,
801 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200802 req->buf->i,
Willy Tarreauedcf6682008-11-30 23:15:34 +0100803 req->analysers);
804
Willy Tarreauedcf6682008-11-30 23:15:34 +0100805 /* We don't know whether we have enough data, so must proceed
806 * this way :
807 * - iterate through all rules in their declaration order
808 * - if one rule returns MISS, it means the inspect delay is
809 * not over yet, then return immediately, otherwise consider
810 * it as a non-match.
811 * - if one rule returns OK, then return OK
812 * - if one rule returns KO, then return KO
813 */
814
Willy Tarreau9b28e032012-10-12 23:49:43 +0200815 if ((req->flags & CF_SHUTR) || buffer_full(req->buf, global.tune.maxrewrite) ||
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +0200816 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200817 partial = SMP_OPT_FINAL;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100818 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200819 partial = 0;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100820
Willy Tarreaufb356202010-08-03 14:02:05 +0200821 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100822 int ret = ACL_PAT_PASS;
823
824 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200825 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100826 if (ret == ACL_PAT_MISS) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200827 channel_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100828 /* just set the request timeout once at the beginning of the request */
Willy Tarreaufb356202010-08-03 14:02:05 +0200829 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
830 req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100831 return 0;
832 }
833
834 ret = acl_pass(ret);
835 if (rule->cond->pol == ACL_COND_UNLESS)
836 ret = !ret;
837 }
838
839 if (ret) {
840 /* we have a matching rule. */
841 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200842 channel_abort(req);
843 channel_abort(s->rep);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100844 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200845
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100846 s->be->be_counters.denied_req++;
847 s->fe->fe_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200848 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200849 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200850
Willy Tarreauedcf6682008-11-30 23:15:34 +0100851 if (!(s->flags & SN_ERR_MASK))
852 s->flags |= SN_ERR_PRXCOND;
853 if (!(s->flags & SN_FINST_MASK))
854 s->flags |= SN_FINST_R;
855 return 0;
856 }
Willy Tarreau20d46a52012-12-09 15:55:40 +0100857 else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) ||
858 (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100859 /* Note: only the first valid tracking parameter of each
860 * applies.
861 */
862 struct stktable_key *key;
863
864 t = rule->act_prm.trk_ctr.table.t;
865 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
866
867 if (key && (ts = stktable_get_entry(t, key))) {
868 if (rule->action == TCP_ACT_TRK_SC1) {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100869 session_track_stkctr(&s->stkctr[0], t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200870 if (s->fe != s->be)
871 s->flags |= SN_BE_TRACK_SC1;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100872 } else {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100873 session_track_stkctr(&s->stkctr[1], t, ts);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200874 if (s->fe != s->be)
875 s->flags |= SN_BE_TRACK_SC2;
876 }
Willy Tarreaud1f96522010-08-03 19:34:32 +0200877 }
878 }
879 else {
Willy Tarreauedcf6682008-11-30 23:15:34 +0100880 /* otherwise accept */
Willy Tarreaud1f96522010-08-03 19:34:32 +0200881 break;
882 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100883 }
884 }
885
886 /* if we get there, it means we have no rule which matches, or
887 * we have an explicit accept, so we apply the default accept.
888 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200889 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100890 req->analyse_exp = TICK_ETERNITY;
891 return 1;
892}
893
Emeric Brun97679e72010-09-23 17:56:44 +0200894/* This function performs the TCP response analysis on the current response. It
895 * returns 1 if the processing can continue on next analysers, or zero if it
896 * needs more data, encounters an error, or wants to immediately abort the
897 * response. It relies on buffers flags, and updates s->rep->analysers. The
898 * function may be called for backend rules.
899 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200900int tcp_inspect_response(struct session *s, struct channel *rep, int an_bit)
Emeric Brun97679e72010-09-23 17:56:44 +0200901{
902 struct tcp_rule *rule;
903 int partial;
904
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100905 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 +0200906 now_ms, __FUNCTION__,
907 s,
908 rep,
909 rep->rex, rep->wex,
910 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200911 rep->buf->i,
Emeric Brun97679e72010-09-23 17:56:44 +0200912 rep->analysers);
913
914 /* We don't know whether we have enough data, so must proceed
915 * this way :
916 * - iterate through all rules in their declaration order
917 * - if one rule returns MISS, it means the inspect delay is
918 * not over yet, then return immediately, otherwise consider
919 * it as a non-match.
920 * - if one rule returns OK, then return OK
921 * - if one rule returns KO, then return KO
922 */
923
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200924 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200925 partial = SMP_OPT_FINAL;
Emeric Brun97679e72010-09-23 17:56:44 +0200926 else
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200927 partial = 0;
Emeric Brun97679e72010-09-23 17:56:44 +0200928
929 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
930 int ret = ACL_PAT_PASS;
931
932 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200933 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_RES | partial);
Emeric Brun97679e72010-09-23 17:56:44 +0200934 if (ret == ACL_PAT_MISS) {
935 /* just set the analyser timeout once at the beginning of the response */
936 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
937 rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay);
938 return 0;
939 }
940
941 ret = acl_pass(ret);
942 if (rule->cond->pol == ACL_COND_UNLESS)
943 ret = !ret;
944 }
945
946 if (ret) {
947 /* we have a matching rule. */
948 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200949 channel_abort(rep);
950 channel_abort(s->req);
Emeric Brun97679e72010-09-23 17:56:44 +0200951 rep->analysers = 0;
952
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100953 s->be->be_counters.denied_resp++;
954 s->fe->fe_counters.denied_resp++;
Emeric Brun97679e72010-09-23 17:56:44 +0200955 if (s->listener->counters)
956 s->listener->counters->denied_resp++;
957
958 if (!(s->flags & SN_ERR_MASK))
959 s->flags |= SN_ERR_PRXCOND;
960 if (!(s->flags & SN_FINST_MASK))
961 s->flags |= SN_FINST_D;
962 return 0;
963 }
964 else {
965 /* otherwise accept */
966 break;
967 }
968 }
969 }
970
971 /* if we get there, it means we have no rule which matches, or
972 * we have an explicit accept, so we apply the default accept.
973 */
974 rep->analysers &= ~an_bit;
975 rep->analyse_exp = TICK_ETERNITY;
976 return 1;
977}
978
979
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200980/* This function performs the TCP layer4 analysis on the current request. It
981 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
982 * matches or if no more rule matches. It can only use rules which don't need
983 * any data.
984 */
985int tcp_exec_req_rules(struct session *s)
986{
987 struct tcp_rule *rule;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200988 struct stksess *ts;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200989 struct stktable *t = NULL;
990 int result = 1;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200991 int ret;
992
993 list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) {
994 ret = ACL_PAT_PASS;
995
996 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200997 ret = acl_exec_cond(rule->cond, s->fe, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200998 ret = acl_pass(ret);
999 if (rule->cond->pol == ACL_COND_UNLESS)
1000 ret = !ret;
1001 }
1002
1003 if (ret) {
1004 /* we have a matching rule. */
1005 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001006 s->fe->fe_counters.denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001007 if (s->listener->counters)
Willy Tarreau2799e982010-06-05 15:43:21 +02001008 s->listener->counters->denied_conn++;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001009
1010 if (!(s->flags & SN_ERR_MASK))
1011 s->flags |= SN_ERR_PRXCOND;
1012 if (!(s->flags & SN_FINST_MASK))
1013 s->flags |= SN_FINST_R;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001014 result = 0;
1015 break;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001016 }
Willy Tarreau20d46a52012-12-09 15:55:40 +01001017 else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) ||
1018 (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001019 /* Note: only the first valid tracking parameter of each
1020 * applies.
1021 */
1022 struct stktable_key *key;
1023
1024 t = rule->act_prm.trk_ctr.table.t;
1025 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
1026
1027 if (key && (ts = stktable_get_entry(t, key))) {
1028 if (rule->action == TCP_ACT_TRK_SC1)
Willy Tarreau20d46a52012-12-09 15:55:40 +01001029 session_track_stkctr(&s->stkctr[0], t, ts);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001030 else
Willy Tarreau20d46a52012-12-09 15:55:40 +01001031 session_track_stkctr(&s->stkctr[1], t, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001032 }
1033 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001034 else {
1035 /* otherwise it's an accept */
1036 break;
1037 }
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001038 }
1039 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001040 return result;
Willy Tarreaua5c0ab22010-05-31 10:30:33 +02001041}
1042
Emeric Brun97679e72010-09-23 17:56:44 +02001043/* Parse a tcp-response rule. Return a negative value in case of failure */
1044static int tcp_parse_response_rule(char **args, int arg, int section_type,
1045 struct proxy *curpx, struct proxy *defpx,
Willy Tarreau80aca902013-01-07 15:42:20 +01001046 struct tcp_rule *rule, char **err,
1047 unsigned int where)
Emeric Brun97679e72010-09-23 17:56:44 +02001048{
1049 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001050 memprintf(err, "%s %s is only allowed in 'backend' sections",
1051 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001052 return -1;
1053 }
1054
1055 if (strcmp(args[arg], "accept") == 0) {
1056 arg++;
1057 rule->action = TCP_ACT_ACCEPT;
1058 }
1059 else if (strcmp(args[arg], "reject") == 0) {
1060 arg++;
1061 rule->action = TCP_ACT_REJECT;
1062 }
1063 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001064 memprintf(err,
1065 "'%s %s' expects 'accept' or 'reject' in %s '%s' (got '%s')",
1066 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Emeric Brun97679e72010-09-23 17:56:44 +02001067 return -1;
1068 }
1069
1070 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001071 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1072 memprintf(err,
1073 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1074 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Emeric Brun97679e72010-09-23 17:56:44 +02001075 return -1;
1076 }
1077 }
1078 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001079 memprintf(err,
1080 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Emeric Brun97679e72010-09-23 17:56:44 +02001081 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1082 return -1;
1083 }
1084 return 0;
1085}
1086
1087
1088
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001089/* Parse a tcp-request rule. Return a negative value in case of failure */
1090static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau80aca902013-01-07 15:42:20 +01001091 struct proxy *curpx, struct proxy *defpx,
1092 struct tcp_rule *rule, char **err,
1093 unsigned int where)
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001094{
1095 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001096 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1097 args[0], args[1]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001098 return -1;
1099 }
1100
1101 if (!strcmp(args[arg], "accept")) {
1102 arg++;
1103 rule->action = TCP_ACT_ACCEPT;
1104 }
1105 else if (!strcmp(args[arg], "reject")) {
1106 arg++;
1107 rule->action = TCP_ACT_REJECT;
1108 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001109 else if (strcmp(args[arg], "track-sc1") == 0 || strcmp(args[arg], "track-sc2") == 0) {
1110 struct sample_expr *expr;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001111 int kw = arg;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001112
1113 arg++;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001114
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001115 expr = sample_parse_expr(args, &arg, trash.str, trash.size);
1116 if (!expr) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001117 memprintf(err,
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001118 "'%s %s %s' : %s",
1119 args[0], args[1], args[kw], trash.str);
1120 return -1;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001121 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001122
Willy Tarreau80aca902013-01-07 15:42:20 +01001123 if (!(expr->fetch->val & where)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001124 memprintf(err,
Willy Tarreau80aca902013-01-07 15:42:20 +01001125 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
1126 args[0], args[1], args[kw], args[arg], sample_src_names(expr->fetch->use));
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001127 free(expr);
1128 return -1;
1129 }
1130
1131 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
Willy Tarreau25320b22013-03-24 07:22:08 +01001132 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001133
1134 if (strcmp(args[arg], "table") == 0) {
Willy Tarreau598718a2012-12-09 16:57:27 +01001135 arg++;
1136 if (!args[arg]) {
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001137 memprintf(err,
1138 "'%s %s %s' : missing table name",
1139 args[0], args[1], args[kw]);
1140 free(expr);
1141 return -1;
1142 }
1143 /* we copy the table name for now, it will be resolved later */
Willy Tarreau598718a2012-12-09 16:57:27 +01001144 rule->act_prm.trk_ctr.table.n = strdup(args[arg]);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001145 arg++;
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001146 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001147 rule->act_prm.trk_ctr.expr = expr;
1148
1149 if (args[kw][8] == '1')
1150 rule->action = TCP_ACT_TRK_SC1;
1151 else
1152 rule->action = TCP_ACT_TRK_SC2;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001153 }
1154 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001155 memprintf(err,
1156 "'%s %s' expects 'accept', 'reject', 'track-sc1' "
1157 "or 'track-sc2' in %s '%s' (got '%s')",
1158 args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001159 return -1;
1160 }
1161
1162 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001163 if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg, err)) == NULL) {
1164 memprintf(err,
1165 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1166 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001167 return -1;
1168 }
1169 }
1170 else if (*args[arg]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001171 memprintf(err,
1172 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001173 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1174 return -1;
1175 }
1176 return 0;
1177}
1178
Emeric Brun97679e72010-09-23 17:56:44 +02001179/* This function should be called to parse a line starting with the "tcp-response"
1180 * keyword.
1181 */
1182static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001183 struct proxy *defpx, const char *file, int line,
1184 char **err)
Emeric Brun97679e72010-09-23 17:56:44 +02001185{
1186 const char *ptr = NULL;
1187 unsigned int val;
Emeric Brun97679e72010-09-23 17:56:44 +02001188 int warn = 0;
1189 int arg;
1190 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001191 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001192 const struct acl *acl;
1193 const struct acl_keyword *kw;
Emeric Brun97679e72010-09-23 17:56:44 +02001194
1195 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001196 memprintf(err, "missing argument for '%s' in %s '%s'",
1197 args[0], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001198 return -1;
1199 }
1200
1201 if (strcmp(args[1], "inspect-delay") == 0) {
1202 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001203 memprintf(err, "%s %s is only allowed in 'backend' sections",
1204 args[0], args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001205 return -1;
1206 }
1207
1208 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001209 memprintf(err,
1210 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1211 args[0], args[1], proxy_type_str(curpx), curpx->id);
1212 if (ptr)
1213 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Emeric Brun97679e72010-09-23 17:56:44 +02001214 return -1;
1215 }
1216
1217 if (curpx->tcp_rep.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001218 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1219 args[0], args[1], proxy_type_str(curpx), curpx->id);
Emeric Brun97679e72010-09-23 17:56:44 +02001220 return 1;
1221 }
1222 curpx->tcp_rep.inspect_delay = val;
1223 return 0;
1224 }
1225
Simon Hormande072bd2011-06-24 15:11:37 +09001226 rule = calloc(1, sizeof(*rule));
Emeric Brun97679e72010-09-23 17:56:44 +02001227 LIST_INIT(&rule->list);
1228 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001229 where = 0;
Emeric Brun97679e72010-09-23 17:56:44 +02001230
1231 if (strcmp(args[1], "content") == 0) {
1232 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001233
1234 if (curpx->cap & PR_CAP_FE)
1235 where |= SMP_VAL_FE_RES_CNT;
1236 if (curpx->cap & PR_CAP_BE)
1237 where |= SMP_VAL_BE_RES_CNT;
1238
1239 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Emeric Brun97679e72010-09-23 17:56:44 +02001240 goto error;
1241
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001242 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1243 if (acl) {
1244 if (acl->name && *acl->name)
1245 memprintf(err,
1246 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1247 acl->name, args[0], args[1], sample_ckp_names(where));
1248 else
1249 memprintf(err,
1250 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1251 args[0], args[1],
1252 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw->kw,
1253 sample_ckp_names(where));
Emeric Brun97679e72010-09-23 17:56:44 +02001254
Emeric Brun97679e72010-09-23 17:56:44 +02001255 warn++;
1256 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001257 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1258 if (acl->name && *acl->name)
1259 memprintf(err,
1260 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1261 acl->name, kw->kw, sample_ckp_names(where));
1262 else
1263 memprintf(err,
1264 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1265 kw->kw, sample_ckp_names(where));
1266 warn++;
1267 }
Emeric Brun97679e72010-09-23 17:56:44 +02001268
1269 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1270 }
1271 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001272 memprintf(err,
1273 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1274 args[0], proxy_type_str(curpx), curpx->id, args[1]);
Emeric Brun97679e72010-09-23 17:56:44 +02001275 goto error;
1276 }
1277
1278 return warn;
1279 error:
1280 free(rule);
1281 return -1;
1282}
1283
1284
Willy Tarreaub6866442008-07-14 23:54:42 +02001285/* This function should be called to parse a line starting with the "tcp-request"
1286 * keyword.
1287 */
1288static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +02001289 struct proxy *defpx, const char *file, int line,
1290 char **err)
Willy Tarreaub6866442008-07-14 23:54:42 +02001291{
1292 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +02001293 unsigned int val;
Willy Tarreau1a687942010-05-23 22:40:30 +02001294 int warn = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001295 int arg;
Willy Tarreau1a687942010-05-23 22:40:30 +02001296 struct tcp_rule *rule;
Willy Tarreau80aca902013-01-07 15:42:20 +01001297 unsigned int where;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001298 const struct acl *acl;
1299 const struct acl_keyword *kw;
Willy Tarreaub6866442008-07-14 23:54:42 +02001300
1301 if (!*args[1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001302 if (curpx == defpx)
1303 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1304 else
1305 memprintf(err, "missing argument for '%s' in %s '%s'",
1306 args[0], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001307 return -1;
1308 }
1309
1310 if (!strcmp(args[1], "inspect-delay")) {
1311 if (curpx == defpx) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001312 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1313 args[0], args[1]);
Willy Tarreaub6866442008-07-14 23:54:42 +02001314 return -1;
1315 }
1316
Willy Tarreaub6866442008-07-14 23:54:42 +02001317 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001318 memprintf(err,
1319 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1320 args[0], args[1], proxy_type_str(curpx), curpx->id);
1321 if (ptr)
1322 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
Willy Tarreaub6866442008-07-14 23:54:42 +02001323 return -1;
1324 }
1325
1326 if (curpx->tcp_req.inspect_delay) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001327 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1328 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreaub6866442008-07-14 23:54:42 +02001329 return 1;
1330 }
1331 curpx->tcp_req.inspect_delay = val;
1332 return 0;
1333 }
1334
Simon Hormande072bd2011-06-24 15:11:37 +09001335 rule = calloc(1, sizeof(*rule));
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001336 LIST_INIT(&rule->list);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001337 arg = 1;
Willy Tarreau80aca902013-01-07 15:42:20 +01001338 where = 0;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001339
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001340 if (strcmp(args[1], "content") == 0) {
Willy Tarreaud1f96522010-08-03 19:34:32 +02001341 arg++;
Willy Tarreau80aca902013-01-07 15:42:20 +01001342
1343 if (curpx->cap & PR_CAP_FE)
1344 where |= SMP_VAL_FE_REQ_CNT;
1345 if (curpx->cap & PR_CAP_BE)
1346 where |= SMP_VAL_BE_REQ_CNT;
1347
1348 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001349 goto error;
Willy Tarreaub6866442008-07-14 23:54:42 +02001350
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001351 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1352 if (acl) {
1353 if (acl->name && *acl->name)
1354 memprintf(err,
1355 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1356 acl->name, args[0], args[1], sample_ckp_names(where));
1357 else
1358 memprintf(err,
1359 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1360 args[0], args[1],
1361 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw->kw,
1362 sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001363
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001364 warn++;
1365 }
1366 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1367 if (acl->name && *acl->name)
1368 memprintf(err,
1369 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1370 acl->name, kw->kw, sample_ckp_names(where));
1371 else
1372 memprintf(err,
1373 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1374 kw->kw, sample_ckp_names(where));
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001375 warn++;
1376 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001377
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001378 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001379 }
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001380 else if (strcmp(args[1], "connection") == 0) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001381 arg++;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001382
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001383 if (!(curpx->cap & PR_CAP_FE)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001384 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1385 args[0], args[1], proxy_type_str(curpx), curpx->id);
Simon Horman6c54d8b2011-07-15 13:14:06 +09001386 goto error;
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001387 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001388
Willy Tarreau80aca902013-01-07 15:42:20 +01001389 where |= SMP_VAL_FE_CON_ACC;
1390
1391 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where) < 0)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001392 goto error;
1393
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001394 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1395 if (acl) {
1396 if (acl->name && *acl->name)
1397 memprintf(err,
1398 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1399 acl->name, args[0], args[1], sample_ckp_names(where));
1400 else
1401 memprintf(err,
1402 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1403 args[0], args[1],
1404 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw->kw,
1405 sample_ckp_names(where));
Willy Tarreauf059a0f2010-08-03 16:29:52 +02001406
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001407 warn++;
1408 }
1409 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1410 if (acl->name && *acl->name)
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001411 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001412 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1413 acl->name, kw->kw, sample_ckp_names(where));
1414 else
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001415 memprintf(err,
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001416 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1417 kw->kw, sample_ckp_names(where));
Willy Tarreau68c03ab2010-08-06 15:08:45 +02001418 warn++;
1419 }
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01001420
Willy Tarreaufb024dc2010-08-20 13:35:41 +02001421 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02001422 }
Willy Tarreau1a687942010-05-23 22:40:30 +02001423 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02001424 if (curpx == defpx)
1425 memprintf(err,
1426 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1427 args[0], args[1]);
1428 else
1429 memprintf(err,
1430 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1431 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001432 goto error;
Willy Tarreau1a687942010-05-23 22:40:30 +02001433 }
1434
Willy Tarreau1a687942010-05-23 22:40:30 +02001435 return warn;
Willy Tarreau6a984fa2010-06-14 16:44:27 +02001436 error:
1437 free(rule);
1438 return -1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001439}
1440
Willy Tarreau645513a2010-05-24 20:55:15 +02001441
1442/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001443/* All supported sample fetch functions must be declared here */
Willy Tarreau32389b72012-04-23 23:13:20 +02001444/************************************************************************/
1445
Willy Tarreau4a129812012-04-25 17:31:42 +02001446/* fetch the connection's source IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001447static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001448smp_fetch_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001449 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001450{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001451 switch (l4->si[0].conn->addr.from.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001452 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001453 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001454 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001455 break;
1456 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001457 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001458 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001459 break;
1460 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001461 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001462 }
Emeric Brunf769f512010-10-22 17:14:01 +02001463
Willy Tarreau37406352012-04-23 16:16:37 +02001464 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001465 return 1;
1466}
1467
Willy Tarreaua5e37562011-12-16 17:06:15 +01001468/* set temp integer to the connection's source port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001469static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001470smp_fetch_sport(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001471 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001472{
Willy Tarreauf853c462012-04-23 18:53:56 +02001473 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001474 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.from)))
Emeric Brunf769f512010-10-22 17:14:01 +02001475 return 0;
1476
Willy Tarreau37406352012-04-23 16:16:37 +02001477 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001478 return 1;
1479}
1480
Willy Tarreau4a129812012-04-25 17:31:42 +02001481/* fetch the connection's destination IPv4/IPv6 address */
Willy Tarreau645513a2010-05-24 20:55:15 +02001482static int
Willy Tarreau4a129812012-04-25 17:31:42 +02001483smp_fetch_dst(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001484 const struct arg *args, struct sample *smp)
Willy Tarreau645513a2010-05-24 20:55:15 +02001485{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001486 conn_get_to_addr(l4->si[0].conn);
Willy Tarreau645513a2010-05-24 20:55:15 +02001487
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001488 switch (l4->si[0].conn->addr.to.ss_family) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01001489 case AF_INET:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001490 smp->data.ipv4 = ((struct sockaddr_in *)&l4->si[0].conn->addr.to)->sin_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001491 smp->type = SMP_T_IPV4;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001492 break;
1493 case AF_INET6:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001494 smp->data.ipv6 = ((struct sockaddr_in6 *)(&l4->si[0].conn->addr.to))->sin6_addr;
Willy Tarreauf853c462012-04-23 18:53:56 +02001495 smp->type = SMP_T_IPV6;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001496 break;
1497 default:
Emeric Brunf769f512010-10-22 17:14:01 +02001498 return 0;
Willy Tarreauf4362b32011-12-16 17:49:52 +01001499 }
Emeric Brunf769f512010-10-22 17:14:01 +02001500
Willy Tarreau37406352012-04-23 16:16:37 +02001501 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001502 return 1;
1503}
1504
Willy Tarreaua5e37562011-12-16 17:06:15 +01001505/* set temp integer to the frontend connexion's destination port */
Willy Tarreau645513a2010-05-24 20:55:15 +02001506static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02001507smp_fetch_dport(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 Tarreauf853c462012-04-23 18:53:56 +02001512 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001513 if (!(smp->data.uint = get_host_port(&l4->si[0].conn->addr.to)))
Emeric Brunf769f512010-10-22 17:14:01 +02001514 return 0;
1515
Willy Tarreau37406352012-04-23 16:16:37 +02001516 smp->flags = 0;
Willy Tarreau645513a2010-05-24 20:55:15 +02001517 return 1;
Emericf2d7cae2010-11-05 18:13:50 +01001518}
1519
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001520#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001521/* parse the "v4v6" bind keyword */
1522static int bind_parse_v4v6(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1523{
1524 struct listener *l;
1525
1526 list_for_each_entry(l, &conf->listeners, by_bind) {
1527 if (l->addr.ss_family == AF_INET6)
1528 l->options |= LI_O_V4V6;
1529 }
1530
1531 return 0;
1532}
1533
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001534/* parse the "v6only" bind keyword */
1535static int bind_parse_v6only(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1536{
1537 struct listener *l;
1538
1539 list_for_each_entry(l, &conf->listeners, by_bind) {
1540 if (l->addr.ss_family == AF_INET6)
1541 l->options |= LI_O_V6ONLY;
1542 }
1543
1544 return 0;
1545}
1546#endif
1547
Willy Tarreau44791242012-09-12 23:27:21 +02001548#ifdef CONFIG_HAP_LINUX_TPROXY
1549/* parse the "transparent" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001550static 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 +02001551{
1552 struct listener *l;
1553
Willy Tarreau4348fad2012-09-20 16:48:07 +02001554 list_for_each_entry(l, &conf->listeners, by_bind) {
1555 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1556 l->options |= LI_O_FOREIGN;
Willy Tarreau44791242012-09-12 23:27:21 +02001557 }
1558
Willy Tarreau44791242012-09-12 23:27:21 +02001559 return 0;
1560}
1561#endif
1562
1563#ifdef TCP_DEFER_ACCEPT
1564/* parse the "defer-accept" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001565static 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 +02001566{
1567 struct listener *l;
1568
Willy Tarreau4348fad2012-09-20 16:48:07 +02001569 list_for_each_entry(l, &conf->listeners, by_bind) {
1570 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1571 l->options |= LI_O_DEF_ACCEPT;
Willy Tarreau44791242012-09-12 23:27:21 +02001572 }
1573
Willy Tarreau44791242012-09-12 23:27:21 +02001574 return 0;
1575}
1576#endif
1577
Willy Tarreau1c862c52012-10-05 16:21:00 +02001578#ifdef TCP_FASTOPEN
Lukas Tribus0defb902013-02-13 23:35:39 +01001579/* parse the "tfo" bind keyword */
Willy Tarreau1c862c52012-10-05 16:21:00 +02001580static int bind_parse_tfo(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1581{
1582 struct listener *l;
1583
1584 list_for_each_entry(l, &conf->listeners, by_bind) {
1585 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1586 l->options |= LI_O_TCP_FO;
1587 }
1588
1589 return 0;
1590}
1591#endif
1592
Willy Tarreau44791242012-09-12 23:27:21 +02001593#ifdef TCP_MAXSEG
1594/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001595static 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 +02001596{
1597 struct listener *l;
1598 int mss;
1599
Willy Tarreau44791242012-09-12 23:27:21 +02001600 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001601 memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001602 return ERR_ALERT | ERR_FATAL;
1603 }
1604
1605 mss = atoi(args[cur_arg + 1]);
1606 if (!mss || abs(mss) > 65535) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001607 memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001608 return ERR_ALERT | ERR_FATAL;
1609 }
1610
Willy Tarreau4348fad2012-09-20 16:48:07 +02001611 list_for_each_entry(l, &conf->listeners, by_bind) {
1612 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1613 l->maxseg = mss;
1614 }
Willy Tarreau44791242012-09-12 23:27:21 +02001615
1616 return 0;
1617}
1618#endif
1619
1620#ifdef SO_BINDTODEVICE
1621/* parse the "mss" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02001622static 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 +02001623{
1624 struct listener *l;
1625
Willy Tarreau44791242012-09-12 23:27:21 +02001626 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001627 memprintf(err, "'%s' : missing interface name", args[cur_arg]);
Willy Tarreau44791242012-09-12 23:27:21 +02001628 return ERR_ALERT | ERR_FATAL;
1629 }
1630
Willy Tarreau4348fad2012-09-20 16:48:07 +02001631 list_for_each_entry(l, &conf->listeners, by_bind) {
1632 if (l->addr.ss_family == AF_INET || l->addr.ss_family == AF_INET6)
1633 l->interface = strdup(args[cur_arg + 1]);
1634 }
Willy Tarreau44791242012-09-12 23:27:21 +02001635
1636 global.last_checks |= LSTCHK_NETADM;
1637 return 0;
1638}
1639#endif
1640
Willy Tarreaub6866442008-07-14 23:54:42 +02001641static struct cfg_kw_list cfg_kws = {{ },{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001642 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
Emeric Brun97679e72010-09-23 17:56:44 +02001643 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
Willy Tarreaub6866442008-07-14 23:54:42 +02001644 { 0, NULL, NULL },
1645}};
1646
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001647
Willy Tarreau61612d42012-04-19 18:42:05 +02001648/* Note: must not be declared <const> as its list will be overwritten.
1649 * Please take care of keeping this list alphabetically sorted.
1650 */
Willy Tarreaub6866442008-07-14 23:54:42 +02001651static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaud86e29d2013-03-25 08:21:05 +01001652 { "dst", NULL, acl_parse_ip, acl_match_ip },
1653 { "dst_port", NULL, acl_parse_int, acl_match_int },
1654 { "src", NULL, acl_parse_ip, acl_match_ip },
1655 { "src_port", NULL, acl_parse_int, acl_match_int },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001656 { /* END */ },
Willy Tarreaub6866442008-07-14 23:54:42 +02001657}};
1658
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001659
Willy Tarreau4a129812012-04-25 17:31:42 +02001660/* Note: must not be declared <const> as its list will be overwritten.
1661 * Note: fetches that may return multiple types must be declared as the lowest
1662 * common denominator, the type that can be casted into all other ones. For
1663 * instance v4/v6 must be declared v4.
1664 */
Willy Tarreau12785782012-04-27 21:37:17 +02001665static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001666 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1667 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1668 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
1669 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_UINT, SMP_USE_L4CLI },
1670 { /* END */ },
Willy Tarreau645513a2010-05-24 20:55:15 +02001671}};
1672
Willy Tarreau44791242012-09-12 23:27:21 +02001673/************************************************************************/
1674/* All supported bind keywords must be declared here. */
1675/************************************************************************/
1676
1677/* Note: must not be declared <const> as its list will be overwritten.
1678 * Please take care of keeping this list alphabetically sorted, doing so helps
1679 * all code contributors.
1680 * Optional keywords are also declared with a NULL ->parse() function so that
1681 * the config parser can report an appropriate error when a known keyword was
1682 * not enabled.
1683 */
Willy Tarreau51fb7652012-09-18 18:24:39 +02001684static struct bind_kw_list bind_kws = { "TCP", { }, {
Willy Tarreau44791242012-09-12 23:27:21 +02001685#ifdef TCP_DEFER_ACCEPT
1686 { "defer-accept", bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
1687#endif
1688#ifdef SO_BINDTODEVICE
1689 { "interface", bind_parse_interface, 1 }, /* specifically bind to this interface */
1690#endif
1691#ifdef TCP_MAXSEG
1692 { "mss", bind_parse_mss, 1 }, /* set MSS of listening socket */
1693#endif
Willy Tarreau1c862c52012-10-05 16:21:00 +02001694#ifdef TCP_FASTOPEN
1695 { "tfo", bind_parse_tfo, 0 }, /* enable TCP_FASTOPEN of listening socket */
1696#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001697#ifdef CONFIG_HAP_LINUX_TPROXY
1698 { "transparent", bind_parse_transparent, 0 }, /* transparently bind to the specified addresses */
1699#endif
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001700#ifdef IPV6_V6ONLY
Willy Tarreau77e3af92012-11-24 15:07:23 +01001701 { "v4v6", bind_parse_v4v6, 0 }, /* force socket to bind to IPv4+IPv6 */
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001702 { "v6only", bind_parse_v6only, 0 }, /* force socket to bind to IPv6 only */
1703#endif
Willy Tarreau44791242012-09-12 23:27:21 +02001704 /* the versions with the NULL parse function*/
1705 { "defer-accept", NULL, 0 },
1706 { "interface", NULL, 1 },
1707 { "mss", NULL, 1 },
1708 { "transparent", NULL, 0 },
Willy Tarreau77e3af92012-11-24 15:07:23 +01001709 { "v4v6", NULL, 0 },
Willy Tarreau9b6700f2012-11-24 11:55:28 +01001710 { "v6only", NULL, 0 },
Willy Tarreau44791242012-09-12 23:27:21 +02001711 { NULL, NULL, 0 },
1712}};
1713
Willy Tarreaue6b98942007-10-29 01:09:36 +01001714__attribute__((constructor))
1715static void __tcp_protocol_init(void)
1716{
1717 protocol_register(&proto_tcpv4);
1718 protocol_register(&proto_tcpv6);
Willy Tarreau12785782012-04-27 21:37:17 +02001719 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreaub6866442008-07-14 23:54:42 +02001720 cfg_register_keywords(&cfg_kws);
1721 acl_register_keywords(&acl_kws);
Willy Tarreau44791242012-09-12 23:27:21 +02001722 bind_register_keywords(&bind_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +01001723}
1724
1725
1726/*
1727 * Local variables:
1728 * c-indent-level: 8
1729 * c-basic-offset: 8
1730 * End:
1731 */