blob: 4a831150536f8af6bf60691e757376a4d439571e [file] [log] [blame]
Willy Tarreaue6b98942007-10-29 01:09:36 +01001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
Willy Tarreaue8c66af2008-01-13 18:40:14 +01004 * Copyright 2000-2008 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 Tarreaue6b98942007-10-29 01:09:36 +010041#include <proto/buffers.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020042#include <proto/log.h>
43#include <proto/port_range.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010044#include <proto/protocols.h>
45#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020046#include <proto/proxy.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010047#include <proto/stream_sock.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010048
Willy Tarreaue8c66af2008-01-13 18:40:14 +010049#ifdef CONFIG_HAP_CTTPROXY
50#include <import/ip_tproxy.h>
51#endif
52
Willy Tarreaue6b98942007-10-29 01:09:36 +010053static int tcp_bind_listeners(struct protocol *proto);
54
55/* Note: must not be declared <const> as its list will be overwritten */
56static struct protocol proto_tcpv4 = {
57 .name = "tcpv4",
58 .sock_domain = AF_INET,
59 .sock_type = SOCK_STREAM,
60 .sock_prot = IPPROTO_TCP,
61 .sock_family = AF_INET,
62 .sock_addrlen = sizeof(struct sockaddr_in),
63 .l3_addrlen = 32/8,
64 .read = &stream_sock_read,
65 .write = &stream_sock_write,
66 .bind_all = tcp_bind_listeners,
67 .unbind_all = unbind_all_listeners,
68 .enable_all = enable_all_listeners,
69 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
70 .nb_listeners = 0,
71};
72
73/* Note: must not be declared <const> as its list will be overwritten */
74static struct protocol proto_tcpv6 = {
75 .name = "tcpv6",
76 .sock_domain = AF_INET6,
77 .sock_type = SOCK_STREAM,
78 .sock_prot = IPPROTO_TCP,
79 .sock_family = AF_INET6,
80 .sock_addrlen = sizeof(struct sockaddr_in6),
81 .l3_addrlen = 128/8,
82 .read = &stream_sock_read,
83 .write = &stream_sock_write,
84 .bind_all = tcp_bind_listeners,
85 .unbind_all = unbind_all_listeners,
86 .enable_all = enable_all_listeners,
87 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
88 .nb_listeners = 0,
89};
90
Willy Tarreaue8c66af2008-01-13 18:40:14 +010091
92/* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which
93 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
94 * - 0 : ignore remote address (may even be a NULL pointer)
95 * - 1 : use provided address
96 * - 2 : use provided port
97 * - 3 : use both
98 *
99 * The function supports multiple foreign binding methods :
100 * - linux_tproxy: we directly bind to the foreign address
101 * - cttproxy: we bind to a local address then nat.
102 * The second one can be used as a fallback for the first one.
103 * This function returns 0 when everything's OK, 1 if it could not bind, to the
104 * local address, 2 if it could not bind to the foreign address.
105 */
106int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote)
107{
108 struct sockaddr_in bind_addr;
109 int foreign_ok = 0;
110 int ret;
111
112#ifdef CONFIG_HAP_LINUX_TPROXY
113 static int ip_transp_working = 1;
114 if (flags && ip_transp_working) {
115 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0
116 || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0)
117 foreign_ok = 1;
118 else
119 ip_transp_working = 0;
120 }
121#endif
122 if (flags) {
123 memset(&bind_addr, 0, sizeof(bind_addr));
124 if (flags & 1)
125 bind_addr.sin_addr = remote->sin_addr;
126 if (flags & 2)
127 bind_addr.sin_port = remote->sin_port;
128 }
129
130 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
131 if (foreign_ok) {
132 ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
133 if (ret < 0)
134 return 2;
135 }
136 else {
137 ret = bind(fd, (struct sockaddr *)local, sizeof(*local));
138 if (ret < 0)
139 return 1;
140 }
141
142 if (!flags)
143 return 0;
144
145#ifdef CONFIG_HAP_CTTPROXY
146 if (!foreign_ok) {
147 struct in_tproxy itp1, itp2;
148 memset(&itp1, 0, sizeof(itp1));
149
150 itp1.op = TPROXY_ASSIGN;
151 itp1.v.addr.faddr = bind_addr.sin_addr;
152 itp1.v.addr.fport = bind_addr.sin_port;
153
154 /* set connect flag on socket */
155 itp2.op = TPROXY_FLAGS;
156 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
157
158 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
159 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
160 foreign_ok = 1;
161 }
162 }
163#endif
164 if (!foreign_ok)
165 /* we could not bind to a foreign address */
166 return 2;
167
168 return 0;
169}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100170
Willy Tarreau9650f372009-08-16 14:02:45 +0200171
172/*
173 * This function initiates a connection to the server assigned to this session
Willy Tarreaub1d67742010-03-29 19:36:59 +0200174 * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. A
175 * source address may be pointed to by <from_addr>. Note that this is only used
176 * in case of transparent proxying. Normal source bind addresses are still
177 * determined locally (due to the possible need of a source port).
178 *
Willy Tarreau9650f372009-08-16 14:02:45 +0200179 * It can return one of :
180 * - SN_ERR_NONE if everything's OK
181 * - SN_ERR_SRVTO if there are no more servers
182 * - SN_ERR_SRVCL if the connection was refused by the server
183 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
184 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
185 * - SN_ERR_INTERNAL for any other purely internal errors
186 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
187 */
188int tcpv4_connect_server(struct stream_interface *si,
189 struct proxy *be, struct server *srv,
Willy Tarreaub1d67742010-03-29 19:36:59 +0200190 struct sockaddr *srv_addr, struct sockaddr *from_addr)
Willy Tarreau9650f372009-08-16 14:02:45 +0200191{
192 int fd;
193
194 if ((fd = si->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
195 qfprintf(stderr, "Cannot get a server socket.\n");
196
197 if (errno == ENFILE)
198 send_log(be, LOG_EMERG,
199 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
200 be->id, maxfd);
201 else if (errno == EMFILE)
202 send_log(be, LOG_EMERG,
203 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
204 be->id, maxfd);
205 else if (errno == ENOBUFS || errno == ENOMEM)
206 send_log(be, LOG_EMERG,
207 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
208 be->id, maxfd);
209 /* this is a resource error */
210 return SN_ERR_RESOURCE;
211 }
212
213 if (fd >= global.maxsock) {
214 /* do not log anything there, it's a normal condition when this option
215 * is used to serialize connections to a server !
216 */
217 Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
218 close(fd);
219 return SN_ERR_PRXCOND; /* it is a configuration limit */
220 }
221
222 if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
223 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
224 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
225 close(fd);
226 return SN_ERR_INTERNAL;
227 }
228
229 if (be->options & PR_O_TCP_SRV_KA)
230 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
231
232 if (be->options & PR_O_TCP_NOLING)
233 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
234
235 /* allow specific binding :
236 * - server-specific at first
237 * - proxy-specific next
238 */
239 if (srv != NULL && srv->state & SRV_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200240 int ret, flags = 0;
241
Willy Tarreau9650f372009-08-16 14:02:45 +0200242 switch (srv->state & SRV_TPROXY_MASK) {
243 case SRV_TPROXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200244 case SRV_TPROXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200245 flags = 3;
246 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200247 case SRV_TPROXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200248 case SRV_TPROXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200249 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200250 break;
251 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200252
Willy Tarreau9650f372009-08-16 14:02:45 +0200253#ifdef SO_BINDTODEVICE
254 /* Note: this might fail if not CAP_NET_RAW */
255 if (srv->iface_name)
256 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1);
257#endif
258
259 if (srv->sport_range) {
260 int attempts = 10; /* should be more than enough to find a spare port */
261 struct sockaddr_in src;
262
263 ret = 1;
264 src = srv->source_addr;
265
266 do {
267 /* note: in case of retry, we may have to release a previously
268 * allocated port, hence this loop's construct.
269 */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200270 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
271 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200272
273 if (!attempts)
274 break;
275 attempts--;
276
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200277 fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range);
278 if (!fdinfo[fd].local_port)
Willy Tarreau9650f372009-08-16 14:02:45 +0200279 break;
280
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200281 fdinfo[fd].port_range = srv->sport_range;
282 src.sin_port = htons(fdinfo[fd].local_port);
Willy Tarreau9650f372009-08-16 14:02:45 +0200283
Willy Tarreaub1d67742010-03-29 19:36:59 +0200284 ret = tcpv4_bind_socket(fd, flags, &src, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200285 } while (ret != 0); /* binding NOK */
286 }
287 else {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200288 ret = tcpv4_bind_socket(fd, flags, &srv->source_addr, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200289 }
290
291 if (ret) {
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200292 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
293 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200294 close(fd);
295
296 if (ret == 1) {
297 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
298 be->id, srv->id);
299 send_log(be, LOG_EMERG,
300 "Cannot bind to source address before connect() for server %s/%s.\n",
301 be->id, srv->id);
302 } else {
303 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
304 be->id, srv->id);
305 send_log(be, LOG_EMERG,
306 "Cannot bind to tproxy source address before connect() for server %s/%s.\n",
307 be->id, srv->id);
308 }
309 return SN_ERR_RESOURCE;
310 }
311 }
312 else if (be->options & PR_O_BIND_SRC) {
Willy Tarreau9650f372009-08-16 14:02:45 +0200313 int ret, flags = 0;
314
Willy Tarreau9650f372009-08-16 14:02:45 +0200315 switch (be->options & PR_O_TPXY_MASK) {
316 case PR_O_TPXY_ADDR:
Willy Tarreau9650f372009-08-16 14:02:45 +0200317 case PR_O_TPXY_CLI:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200318 flags = 3;
319 break;
Willy Tarreau9650f372009-08-16 14:02:45 +0200320 case PR_O_TPXY_CIP:
Willy Tarreau090466c2009-09-07 11:51:47 +0200321 case PR_O_TPXY_DYN:
Willy Tarreaub1d67742010-03-29 19:36:59 +0200322 flags = 1;
Willy Tarreau9650f372009-08-16 14:02:45 +0200323 break;
324 }
Willy Tarreaub1d67742010-03-29 19:36:59 +0200325
Willy Tarreau9650f372009-08-16 14:02:45 +0200326#ifdef SO_BINDTODEVICE
327 /* Note: this might fail if not CAP_NET_RAW */
328 if (be->iface_name)
329 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1);
330#endif
Willy Tarreaub1d67742010-03-29 19:36:59 +0200331 ret = tcpv4_bind_socket(fd, flags, &be->source_addr, (struct sockaddr_in *)from_addr);
Willy Tarreau9650f372009-08-16 14:02:45 +0200332 if (ret) {
333 close(fd);
334 if (ret == 1) {
335 Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n",
336 be->id);
337 send_log(be, LOG_EMERG,
338 "Cannot bind to source address before connect() for proxy %s.\n",
339 be->id);
340 } else {
341 Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n",
342 be->id);
343 send_log(be, LOG_EMERG,
344 "Cannot bind to tproxy source address before connect() for proxy %s.\n",
345 be->id);
346 }
347 return SN_ERR_RESOURCE;
348 }
349 }
350
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400351#if defined(TCP_QUICKACK)
Willy Tarreau9650f372009-08-16 14:02:45 +0200352 /* disabling tcp quick ack now allows the first request to leave the
353 * machine with the first ACK. We only do this if there are pending
354 * data in the buffer.
355 */
356 if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400357 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9650f372009-08-16 14:02:45 +0200358#endif
359
Willy Tarreaue803de22010-01-21 17:43:04 +0100360 if (global.tune.server_sndbuf)
361 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
362
363 if (global.tune.server_rcvbuf)
364 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
365
Willy Tarreau9650f372009-08-16 14:02:45 +0200366 if ((connect(fd, (struct sockaddr *)srv_addr, sizeof(struct sockaddr_in)) == -1) &&
367 (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) {
368
369 if (errno == EAGAIN || errno == EADDRINUSE) {
370 char *msg;
371 if (errno == EAGAIN) /* no free ports left, try again later */
372 msg = "no free ports";
373 else
374 msg = "local address already in use";
375
376 qfprintf(stderr,"Cannot connect: %s.\n",msg);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200377 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
378 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200379 close(fd);
380 send_log(be, LOG_EMERG,
381 "Connect() failed for server %s/%s: %s.\n",
382 be->id, srv->id, msg);
383 return SN_ERR_RESOURCE;
384 } else if (errno == ETIMEDOUT) {
385 //qfprintf(stderr,"Connect(): ETIMEDOUT");
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200386 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
387 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200388 close(fd);
389 return SN_ERR_SRVTO;
390 } else {
391 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
392 //qfprintf(stderr,"Connect(): %d", errno);
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200393 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
394 fdinfo[fd].port_range = NULL;
Willy Tarreau9650f372009-08-16 14:02:45 +0200395 close(fd);
396 return SN_ERR_SRVCL;
397 }
398 }
399
400 fdtab[fd].owner = si;
401 fdtab[fd].state = FD_STCONN; /* connection in progress */
402 fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
403 fdtab[fd].cb[DIR_RD].f = &stream_sock_read;
404 fdtab[fd].cb[DIR_RD].b = si->ib;
405 fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
406 fdtab[fd].cb[DIR_WR].b = si->ob;
407
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200408 fdinfo[fd].peeraddr = (struct sockaddr *)srv_addr;
409 fdinfo[fd].peerlen = sizeof(struct sockaddr_in);
Willy Tarreau9650f372009-08-16 14:02:45 +0200410
411 fd_insert(fd);
412 EV_FD_SET(fd, DIR_WR); /* for connect status */
413
414 si->state = SI_ST_CON;
415 si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */
416 si->exp = tick_add_ifset(now_ms, be->timeout.connect);
417
418 return SN_ERR_NONE; /* connection is OK */
419}
420
421
Willy Tarreaue6b98942007-10-29 01:09:36 +0100422/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
423 * an error message in <err> if the message is at most <errlen> bytes long
424 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
425 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
426 * was alright and that no message was returned. ERR_RETRYABLE means that an
427 * error occurred but that it may vanish after a retry (eg: port in use), and
428 * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter
429 * the meaning of the error, but just indicate that a message is present which
430 * should be displayed with the respective level. Last, ERR_ABORT indicates
431 * that it's pointless to try to start other listeners. No error message is
432 * returned if errlen is NULL.
433 */
434int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
435{
436 __label__ tcp_return, tcp_close_return;
437 int fd, err;
438 const char *msg = NULL;
439
440 /* ensure we never return garbage */
441 if (errmsg && errlen)
442 *errmsg = 0;
443
444 if (listener->state != LI_ASSIGNED)
445 return ERR_NONE; /* already bound */
446
447 err = ERR_NONE;
448
449 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
450 err |= ERR_RETRYABLE | ERR_ALERT;
451 msg = "cannot create listening socket";
452 goto tcp_return;
453 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100454
Willy Tarreaue6b98942007-10-29 01:09:36 +0100455 if (fd >= global.maxsock) {
456 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
457 msg = "not enough free sockets (raise '-n' parameter)";
458 goto tcp_close_return;
459 }
460
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200461 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100462 err |= ERR_FATAL | ERR_ALERT;
463 msg = "cannot make socket non-blocking";
464 goto tcp_close_return;
465 }
466
467 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
468 /* not fatal but should be reported */
469 msg = "cannot do so_reuseaddr";
470 err |= ERR_ALERT;
471 }
472
473 if (listener->options & LI_O_NOLINGER)
474 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauedcf6682008-11-30 23:15:34 +0100475
Willy Tarreaue6b98942007-10-29 01:09:36 +0100476#ifdef SO_REUSEPORT
477 /* OpenBSD supports this. As it's present in old libc versions of Linux,
478 * it might return an error that we will silently ignore.
479 */
480 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
481#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100482#ifdef CONFIG_HAP_LINUX_TPROXY
Willy Tarreauedcf6682008-11-30 23:15:34 +0100483 if ((listener->options & LI_O_FOREIGN)
Willy Tarreau0a459892008-01-13 17:37:16 +0100484 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1)
485 && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100486 msg = "cannot make listening socket transparent";
487 err |= ERR_ALERT;
488 }
489#endif
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100490#ifdef SO_BINDTODEVICE
491 /* Note: this might fail if not CAP_NET_RAW */
492 if (listener->interface) {
493 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
Willy Tarreau604e8302009-03-06 00:48:23 +0100494 listener->interface, strlen(listener->interface) + 1) == -1) {
Willy Tarreau5e6e2042009-02-04 17:19:29 +0100495 msg = "cannot bind listener to device";
496 err |= ERR_WARN;
497 }
498 }
499#endif
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400500#if defined(TCP_MAXSEG)
Willy Tarreaube1b9182009-06-14 18:48:19 +0200501 if (listener->maxseg) {
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400502 if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG,
Willy Tarreaube1b9182009-06-14 18:48:19 +0200503 &listener->maxseg, sizeof(listener->maxseg)) == -1) {
504 msg = "cannot set MSS";
505 err |= ERR_WARN;
506 }
507 }
508#endif
Willy Tarreaucb6cd432009-10-13 07:34:14 +0200509#if defined(TCP_DEFER_ACCEPT)
510 if (listener->options & LI_O_DEF_ACCEPT) {
511 /* defer accept by up to one second */
512 int accept_delay = 1;
513 if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) {
514 msg = "cannot enable DEFER_ACCEPT";
515 err |= ERR_WARN;
516 }
517 }
518#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100519 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
520 err |= ERR_RETRYABLE | ERR_ALERT;
521 msg = "cannot bind socket";
522 goto tcp_close_return;
523 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100524
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100525 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100526 err |= ERR_RETRYABLE | ERR_ALERT;
527 msg = "cannot listen to socket";
528 goto tcp_close_return;
529 }
Willy Tarreauedcf6682008-11-30 23:15:34 +0100530
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400531#if defined(TCP_QUICKACK)
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200532 if (listener->options & LI_O_NOQUICKACK)
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +0400533 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero));
Willy Tarreau9ea05a72009-06-14 12:07:01 +0200534#endif
535
Willy Tarreaue6b98942007-10-29 01:09:36 +0100536 /* the socket is ready */
537 listener->fd = fd;
538 listener->state = LI_LISTEN;
539
540 /* the function for the accept() event */
541 fd_insert(fd);
542 fdtab[fd].cb[DIR_RD].f = listener->accept;
543 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
544 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200545 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaue6b98942007-10-29 01:09:36 +0100546 fdtab[fd].state = FD_STLISTEN;
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200547 fdtab[fd].flags = FD_FL_TCP;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200548 if (listener->options & LI_O_NOLINGER)
549 fdtab[fd].flags |= FD_FL_TCP_NOLING;
550
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200551 fdinfo[fd].peeraddr = NULL;
552 fdinfo[fd].peerlen = 0;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100553 tcp_return:
554 if (msg && errlen)
555 strlcpy2(errmsg, msg, errlen);
556 return err;
557
558 tcp_close_return:
559 close(fd);
560 goto tcp_return;
561}
562
563/* This function creates all TCP sockets bound to the protocol entry <proto>.
564 * It is intended to be used as the protocol's bind_all() function.
565 * The sockets will be registered but not added to any fd_set, in order not to
566 * loose them across the fork(). A call to enable_all_listeners() is needed
567 * to complete initialization. The return value is composed from ERR_*.
568 */
569static int tcp_bind_listeners(struct protocol *proto)
570{
571 struct listener *listener;
572 int err = ERR_NONE;
573
574 list_for_each_entry(listener, &proto->listeners, proto_list) {
575 err |= tcp_bind_listener(listener, NULL, 0);
576 if ((err & ERR_CODE) == ERR_ABORT)
577 break;
578 }
579
580 return err;
581}
582
583/* Add listener to the list of tcpv4 listeners. The listener's state
584 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
585 * listeners is updated. This is the function to use to add a new listener.
586 */
587void tcpv4_add_listener(struct listener *listener)
588{
589 if (listener->state != LI_INIT)
590 return;
591 listener->state = LI_ASSIGNED;
592 listener->proto = &proto_tcpv4;
593 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
594 proto_tcpv4.nb_listeners++;
595}
596
597/* Add listener to the list of tcpv4 listeners. The listener's state
598 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
599 * listeners is updated. This is the function to use to add a new listener.
600 */
601void tcpv6_add_listener(struct listener *listener)
602{
603 if (listener->state != LI_INIT)
604 return;
605 listener->state = LI_ASSIGNED;
606 listener->proto = &proto_tcpv6;
607 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
608 proto_tcpv6.nb_listeners++;
609}
610
Willy Tarreauedcf6682008-11-30 23:15:34 +0100611/* This function performs the TCP request analysis on the current request. It
612 * returns 1 if the processing can continue on next analysers, or zero if it
613 * needs more data, encounters an error, or wants to immediately abort the
614 * request. It relies on buffers flags, and updates s->req->analysers. Its
615 * behaviour is rather simple:
Willy Tarreau86ef7dc2009-03-15 22:55:47 +0100616 * - the analyser should check for errors and timeouts, and react as expected.
617 * It does not have to close anything upon error, the caller will. Note that
618 * the caller also knows how to report errors and timeouts.
619 * - if the analyser does not have enough data, it must return 0 without calling
Willy Tarreauedcf6682008-11-30 23:15:34 +0100620 * other ones. It should also probably do a buffer_write_dis() to ensure
621 * that unprocessed data will not be forwarded. But that probably depends on
622 * the protocol.
623 * - if an analyser has enough data, it just has to pass on to the next
624 * analyser without using buffer_write_dis() (enabled by default).
625 * - if an analyser thinks it has no added value anymore staying here, it must
626 * reset its bit from the analysers flags in order not to be called anymore.
627 *
628 * In the future, analysers should be able to indicate that they want to be
629 * called after XXX bytes have been received (or transfered), and the min of
630 * all's wishes will be used to ring back (unless a special condition occurs).
631 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200632int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100633{
634 struct tcp_rule *rule;
635 int partial;
636
637 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
638 now_ms, __FUNCTION__,
639 s,
640 req,
641 req->rex, req->wex,
642 req->flags,
643 req->l,
644 req->analysers);
645
Willy Tarreauedcf6682008-11-30 23:15:34 +0100646 /* We don't know whether we have enough data, so must proceed
647 * this way :
648 * - iterate through all rules in their declaration order
649 * - if one rule returns MISS, it means the inspect delay is
650 * not over yet, then return immediately, otherwise consider
651 * it as a non-match.
652 * - if one rule returns OK, then return OK
653 * - if one rule returns KO, then return KO
654 */
655
Willy Tarreaud869b242009-03-15 14:43:58 +0100656 if (req->flags & BF_SHUTR || !s->fe->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100657 partial = 0;
658 else
659 partial = ACL_PARTIAL;
660
661 list_for_each_entry(rule, &s->fe->tcp_req.inspect_rules, list) {
662 int ret = ACL_PAT_PASS;
663
664 if (rule->cond) {
Willy Tarreau51d5dad2009-07-12 10:10:05 +0200665 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ | partial);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100666 if (ret == ACL_PAT_MISS) {
Willy Tarreau520d95e2009-09-19 21:04:57 +0200667 buffer_dont_connect(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100668 /* just set the request timeout once at the beginning of the request */
Willy Tarreaud869b242009-03-15 14:43:58 +0100669 if (!tick_isset(req->analyse_exp) && s->fe->tcp_req.inspect_delay)
Willy Tarreauedcf6682008-11-30 23:15:34 +0100670 req->analyse_exp = tick_add_ifset(now_ms, s->fe->tcp_req.inspect_delay);
671 return 0;
672 }
673
674 ret = acl_pass(ret);
675 if (rule->cond->pol == ACL_COND_UNLESS)
676 ret = !ret;
677 }
678
679 if (ret) {
680 /* we have a matching rule. */
681 if (rule->action == TCP_ACT_REJECT) {
682 buffer_abort(req);
683 buffer_abort(s->rep);
684 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200685
Willy Tarreau23968d82010-05-23 23:50:44 +0200686 s->fe->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200687 if (s->listener->counters)
Willy Tarreau23968d82010-05-23 23:50:44 +0200688 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200689
Willy Tarreauedcf6682008-11-30 23:15:34 +0100690 if (!(s->flags & SN_ERR_MASK))
691 s->flags |= SN_ERR_PRXCOND;
692 if (!(s->flags & SN_FINST_MASK))
693 s->flags |= SN_FINST_R;
694 return 0;
695 }
696 /* otherwise accept */
697 break;
698 }
699 }
700
701 /* if we get there, it means we have no rule which matches, or
702 * we have an explicit accept, so we apply the default accept.
703 */
Willy Tarreau3a816292009-07-07 10:55:49 +0200704 req->analysers &= ~an_bit;
Willy Tarreauedcf6682008-11-30 23:15:34 +0100705 req->analyse_exp = TICK_ETERNITY;
706 return 1;
707}
708
Willy Tarreaub6866442008-07-14 23:54:42 +0200709/* This function should be called to parse a line starting with the "tcp-request"
710 * keyword.
711 */
712static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
713 struct proxy *defpx, char *err, int errlen)
714{
715 const char *ptr = NULL;
Willy Tarreauc7e961e2008-08-17 17:13:47 +0200716 unsigned int val;
Willy Tarreaub6866442008-07-14 23:54:42 +0200717 int retlen;
718
719 if (!*args[1]) {
720 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
721 args[0], proxy_type_str(proxy), curpx->id);
722 return -1;
723 }
724
725 if (!strcmp(args[1], "inspect-delay")) {
726 if (curpx == defpx) {
727 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
728 args[0], args[1]);
729 return -1;
730 }
731
732 if (!(curpx->cap & PR_CAP_FE)) {
733 snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability",
734 args[0], args[1], proxy_type_str(proxy), curpx->id,
735 "frontend");
736 return 1;
737 }
738
739 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
740 retlen = snprintf(err, errlen,
741 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
742 args[0], args[1], proxy_type_str(proxy), curpx->id);
743 if (ptr && retlen < errlen)
744 retlen += snprintf(err+retlen, errlen - retlen,
745 " (unexpected character '%c')", *ptr);
746 return -1;
747 }
748
749 if (curpx->tcp_req.inspect_delay) {
750 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
751 args[0], args[1], proxy_type_str(proxy), curpx->id);
752 return 1;
753 }
754 curpx->tcp_req.inspect_delay = val;
755 return 0;
756 }
757
758 if (!strcmp(args[1], "content")) {
759 int action;
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200760 int warn = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +0200761 int pol = ACL_COND_NONE;
762 struct acl_cond *cond;
763 struct tcp_rule *rule;
764
765 if (curpx == defpx) {
766 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
767 args[0], args[1]);
768 return -1;
769 }
770
771 if (!strcmp(args[2], "accept"))
772 action = TCP_ACT_ACCEPT;
773 else if (!strcmp(args[2], "reject"))
774 action = TCP_ACT_REJECT;
775 else {
776 retlen = snprintf(err, errlen,
777 "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')",
778 args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]);
779 return -1;
780 }
781
782 pol = ACL_COND_NONE;
783 cond = NULL;
784
Willy Tarreauef6494c2010-01-28 17:12:36 +0100785 if (strcmp(args[3], "if") == 0 || strcmp(args[3], "unless") == 0) {
786 if ((cond = build_acl_cond(NULL, 0, curpx, (const char **)args+3)) == NULL) {
787 retlen = snprintf(err, errlen,
788 "error detected in %s '%s' while parsing '%s' condition",
789 proxy_type_str(curpx), curpx->id, args[3]);
790 return -1;
791 }
792 }
793 else if (*args[3]) {
Willy Tarreau606ad732009-07-14 21:17:05 +0200794 retlen = snprintf(err, errlen,
795 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')",
796 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[3]);
797 return -1;
798 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200799
Willy Tarreau1a211942009-07-14 13:53:17 +0200800 if (cond && (cond->requires & ACL_USE_RTR_ANY)) {
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200801 struct acl *acl;
802 const char *name;
803
Willy Tarreau1a211942009-07-14 13:53:17 +0200804 acl = cond_find_require(cond, ACL_USE_RTR_ANY);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200805 name = acl ? acl->name : "(unknown)";
806
807 retlen = snprintf(err, errlen,
Willy Tarreau1a211942009-07-14 13:53:17 +0200808 "acl '%s' involves some response-only criteria which will be ignored.",
809 name);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200810 warn++;
811 }
Willy Tarreaub6866442008-07-14 23:54:42 +0200812 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
813 rule->cond = cond;
814 rule->action = action;
815 LIST_INIT(&rule->list);
816 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200817 return warn;
Willy Tarreaub6866442008-07-14 23:54:42 +0200818 }
819
820 snprintf(err, errlen, "unknown argument '%s' after '%s' in %s '%s'",
821 args[1], args[0], proxy_type_str(proxy), curpx->id);
822 return -1;
823}
824
Willy Tarreaub6866442008-07-14 23:54:42 +0200825static struct cfg_kw_list cfg_kws = {{ },{
826 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
827 { 0, NULL, NULL },
828}};
829
830static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaub6866442008-07-14 23:54:42 +0200831 { NULL, NULL, NULL, NULL },
832}};
833
Willy Tarreaue6b98942007-10-29 01:09:36 +0100834__attribute__((constructor))
835static void __tcp_protocol_init(void)
836{
837 protocol_register(&proto_tcpv4);
838 protocol_register(&proto_tcpv6);
Willy Tarreaub6866442008-07-14 23:54:42 +0200839 cfg_register_keywords(&cfg_kws);
840 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100841}
842
843
844/*
845 * Local variables:
846 * c-indent-level: 8
847 * c-basic-offset: 8
848 * End:
849 */