blob: e2afb9467f36307d1261acbd803471333d196ba6 [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
Willy Tarreaub6866442008-07-14 23:54:42 +020027#include <common/cfgparse.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/errors.h>
32#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
35#include <common/time.h>
36#include <common/version.h>
37
38#include <types/acl.h>
39#include <types/client.h>
40#include <types/global.h>
41#include <types/polling.h>
42#include <types/proxy.h>
43#include <types/server.h>
44
45#include <proto/acl.h>
46#include <proto/backend.h>
47#include <proto/buffers.h>
48#include <proto/fd.h>
49#include <proto/protocols.h>
50#include <proto/proto_tcp.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020051#include <proto/proxy.h>
Willy Tarreaue6b98942007-10-29 01:09:36 +010052#include <proto/queue.h>
53#include <proto/senddata.h>
54#include <proto/session.h>
55#include <proto/stream_sock.h>
56#include <proto/task.h>
57
Willy Tarreaue8c66af2008-01-13 18:40:14 +010058#ifdef CONFIG_HAP_CTTPROXY
59#include <import/ip_tproxy.h>
60#endif
61
Willy Tarreaue6b98942007-10-29 01:09:36 +010062static int tcp_bind_listeners(struct protocol *proto);
63
64/* Note: must not be declared <const> as its list will be overwritten */
65static struct protocol proto_tcpv4 = {
66 .name = "tcpv4",
67 .sock_domain = AF_INET,
68 .sock_type = SOCK_STREAM,
69 .sock_prot = IPPROTO_TCP,
70 .sock_family = AF_INET,
71 .sock_addrlen = sizeof(struct sockaddr_in),
72 .l3_addrlen = 32/8,
73 .read = &stream_sock_read,
74 .write = &stream_sock_write,
75 .bind_all = tcp_bind_listeners,
76 .unbind_all = unbind_all_listeners,
77 .enable_all = enable_all_listeners,
78 .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
79 .nb_listeners = 0,
80};
81
82/* Note: must not be declared <const> as its list will be overwritten */
83static struct protocol proto_tcpv6 = {
84 .name = "tcpv6",
85 .sock_domain = AF_INET6,
86 .sock_type = SOCK_STREAM,
87 .sock_prot = IPPROTO_TCP,
88 .sock_family = AF_INET6,
89 .sock_addrlen = sizeof(struct sockaddr_in6),
90 .l3_addrlen = 128/8,
91 .read = &stream_sock_read,
92 .write = &stream_sock_write,
93 .bind_all = tcp_bind_listeners,
94 .unbind_all = unbind_all_listeners,
95 .enable_all = enable_all_listeners,
96 .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
97 .nb_listeners = 0,
98};
99
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100100
101/* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which
102 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
103 * - 0 : ignore remote address (may even be a NULL pointer)
104 * - 1 : use provided address
105 * - 2 : use provided port
106 * - 3 : use both
107 *
108 * The function supports multiple foreign binding methods :
109 * - linux_tproxy: we directly bind to the foreign address
110 * - cttproxy: we bind to a local address then nat.
111 * The second one can be used as a fallback for the first one.
112 * This function returns 0 when everything's OK, 1 if it could not bind, to the
113 * local address, 2 if it could not bind to the foreign address.
114 */
115int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote)
116{
117 struct sockaddr_in bind_addr;
118 int foreign_ok = 0;
119 int ret;
120
121#ifdef CONFIG_HAP_LINUX_TPROXY
122 static int ip_transp_working = 1;
123 if (flags && ip_transp_working) {
124 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0
125 || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0)
126 foreign_ok = 1;
127 else
128 ip_transp_working = 0;
129 }
130#endif
131 if (flags) {
132 memset(&bind_addr, 0, sizeof(bind_addr));
133 if (flags & 1)
134 bind_addr.sin_addr = remote->sin_addr;
135 if (flags & 2)
136 bind_addr.sin_port = remote->sin_port;
137 }
138
139 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
140 if (foreign_ok) {
141 ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
142 if (ret < 0)
143 return 2;
144 }
145 else {
146 ret = bind(fd, (struct sockaddr *)local, sizeof(*local));
147 if (ret < 0)
148 return 1;
149 }
150
151 if (!flags)
152 return 0;
153
154#ifdef CONFIG_HAP_CTTPROXY
155 if (!foreign_ok) {
156 struct in_tproxy itp1, itp2;
157 memset(&itp1, 0, sizeof(itp1));
158
159 itp1.op = TPROXY_ASSIGN;
160 itp1.v.addr.faddr = bind_addr.sin_addr;
161 itp1.v.addr.fport = bind_addr.sin_port;
162
163 /* set connect flag on socket */
164 itp2.op = TPROXY_FLAGS;
165 itp2.v.flags = ITP_CONNECT | ITP_ONCE;
166
167 if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 &&
168 setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) {
169 foreign_ok = 1;
170 }
171 }
172#endif
173 if (!foreign_ok)
174 /* we could not bind to a foreign address */
175 return 2;
176
177 return 0;
178}
Willy Tarreaue6b98942007-10-29 01:09:36 +0100179
180/* This function tries to bind a TCPv4/v6 listener. It may return a warning or
181 * an error message in <err> if the message is at most <errlen> bytes long
182 * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN,
183 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
184 * was alright and that no message was returned. ERR_RETRYABLE means that an
185 * error occurred but that it may vanish after a retry (eg: port in use), and
186 * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter
187 * the meaning of the error, but just indicate that a message is present which
188 * should be displayed with the respective level. Last, ERR_ABORT indicates
189 * that it's pointless to try to start other listeners. No error message is
190 * returned if errlen is NULL.
191 */
192int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
193{
194 __label__ tcp_return, tcp_close_return;
195 int fd, err;
196 const char *msg = NULL;
197
198 /* ensure we never return garbage */
199 if (errmsg && errlen)
200 *errmsg = 0;
201
202 if (listener->state != LI_ASSIGNED)
203 return ERR_NONE; /* already bound */
204
205 err = ERR_NONE;
206
207 if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
208 err |= ERR_RETRYABLE | ERR_ALERT;
209 msg = "cannot create listening socket";
210 goto tcp_return;
211 }
212
213 if (fd >= global.maxsock) {
214 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
215 msg = "not enough free sockets (raise '-n' parameter)";
216 goto tcp_close_return;
217 }
218
219 if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) ||
220 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
221 (char *) &one, sizeof(one)) == -1)) {
222 err |= ERR_FATAL | ERR_ALERT;
223 msg = "cannot make socket non-blocking";
224 goto tcp_close_return;
225 }
226
227 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
228 /* not fatal but should be reported */
229 msg = "cannot do so_reuseaddr";
230 err |= ERR_ALERT;
231 }
232
233 if (listener->options & LI_O_NOLINGER)
234 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
235
236#ifdef SO_REUSEPORT
237 /* OpenBSD supports this. As it's present in old libc versions of Linux,
238 * it might return an error that we will silently ignore.
239 */
240 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one));
241#endif
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100242#ifdef CONFIG_HAP_LINUX_TPROXY
243 if ((listener->options & LI_O_FOREIGN)
Willy Tarreau0a459892008-01-13 17:37:16 +0100244 && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1)
245 && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100246 msg = "cannot make listening socket transparent";
247 err |= ERR_ALERT;
248 }
249#endif
Willy Tarreaue6b98942007-10-29 01:09:36 +0100250 if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) {
251 err |= ERR_RETRYABLE | ERR_ALERT;
252 msg = "cannot bind socket";
253 goto tcp_close_return;
254 }
255
Willy Tarreauc73ce2b2008-01-06 10:55:10 +0100256 if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) {
Willy Tarreaue6b98942007-10-29 01:09:36 +0100257 err |= ERR_RETRYABLE | ERR_ALERT;
258 msg = "cannot listen to socket";
259 goto tcp_close_return;
260 }
261
262 /* the socket is ready */
263 listener->fd = fd;
264 listener->state = LI_LISTEN;
265
266 /* the function for the accept() event */
267 fd_insert(fd);
268 fdtab[fd].cb[DIR_RD].f = listener->accept;
269 fdtab[fd].cb[DIR_WR].f = NULL; /* never called */
270 fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
271 fdtab[fd].owner = (struct task *)listener; /* reference the listener instead of a task */
272 fdtab[fd].state = FD_STLISTEN;
273 fdtab[fd].peeraddr = NULL;
274 fdtab[fd].peerlen = 0;
275 fdtab[fd].listener = NULL;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100276 tcp_return:
277 if (msg && errlen)
278 strlcpy2(errmsg, msg, errlen);
279 return err;
280
281 tcp_close_return:
282 close(fd);
283 goto tcp_return;
284}
285
286/* This function creates all TCP sockets bound to the protocol entry <proto>.
287 * It is intended to be used as the protocol's bind_all() function.
288 * The sockets will be registered but not added to any fd_set, in order not to
289 * loose them across the fork(). A call to enable_all_listeners() is needed
290 * to complete initialization. The return value is composed from ERR_*.
291 */
292static int tcp_bind_listeners(struct protocol *proto)
293{
294 struct listener *listener;
295 int err = ERR_NONE;
296
297 list_for_each_entry(listener, &proto->listeners, proto_list) {
298 err |= tcp_bind_listener(listener, NULL, 0);
299 if ((err & ERR_CODE) == ERR_ABORT)
300 break;
301 }
302
303 return err;
304}
305
306/* Add listener to the list of tcpv4 listeners. The listener's state
307 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
308 * listeners is updated. This is the function to use to add a new listener.
309 */
310void tcpv4_add_listener(struct listener *listener)
311{
312 if (listener->state != LI_INIT)
313 return;
314 listener->state = LI_ASSIGNED;
315 listener->proto = &proto_tcpv4;
316 LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list);
317 proto_tcpv4.nb_listeners++;
318}
319
320/* Add listener to the list of tcpv4 listeners. The listener's state
321 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
322 * listeners is updated. This is the function to use to add a new listener.
323 */
324void tcpv6_add_listener(struct listener *listener)
325{
326 if (listener->state != LI_INIT)
327 return;
328 listener->state = LI_ASSIGNED;
329 listener->proto = &proto_tcpv6;
330 LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list);
331 proto_tcpv6.nb_listeners++;
332}
333
Willy Tarreaub6866442008-07-14 23:54:42 +0200334/* This function should be called to parse a line starting with the "tcp-request"
335 * keyword.
336 */
337static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
338 struct proxy *defpx, char *err, int errlen)
339{
340 const char *ptr = NULL;
341 int val;
342 int retlen;
343
344 if (!*args[1]) {
345 snprintf(err, errlen, "missing argument for '%s' in %s '%s'",
346 args[0], proxy_type_str(proxy), curpx->id);
347 return -1;
348 }
349
350 if (!strcmp(args[1], "inspect-delay")) {
351 if (curpx == defpx) {
352 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
353 args[0], args[1]);
354 return -1;
355 }
356
357 if (!(curpx->cap & PR_CAP_FE)) {
358 snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability",
359 args[0], args[1], proxy_type_str(proxy), curpx->id,
360 "frontend");
361 return 1;
362 }
363
364 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
365 retlen = snprintf(err, errlen,
366 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
367 args[0], args[1], proxy_type_str(proxy), curpx->id);
368 if (ptr && retlen < errlen)
369 retlen += snprintf(err+retlen, errlen - retlen,
370 " (unexpected character '%c')", *ptr);
371 return -1;
372 }
373
374 if (curpx->tcp_req.inspect_delay) {
375 snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'",
376 args[0], args[1], proxy_type_str(proxy), curpx->id);
377 return 1;
378 }
379 curpx->tcp_req.inspect_delay = val;
380 return 0;
381 }
382
383 if (!strcmp(args[1], "content")) {
384 int action;
385 int pol = ACL_COND_NONE;
386 struct acl_cond *cond;
387 struct tcp_rule *rule;
388
389 if (curpx == defpx) {
390 snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections",
391 args[0], args[1]);
392 return -1;
393 }
394
395 if (!strcmp(args[2], "accept"))
396 action = TCP_ACT_ACCEPT;
397 else if (!strcmp(args[2], "reject"))
398 action = TCP_ACT_REJECT;
399 else {
400 retlen = snprintf(err, errlen,
401 "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')",
402 args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]);
403 return -1;
404 }
405
406 pol = ACL_COND_NONE;
407 cond = NULL;
408
409 if (!strcmp(args[3], "if"))
410 pol = ACL_COND_IF;
411 else if (!strcmp(args[3], "unless"))
412 pol = ACL_COND_UNLESS;
413
414 /* Note: we consider "if TRUE" when there is no condition */
415 if (pol != ACL_COND_NONE &&
416 (cond = parse_acl_cond((const char **)args+4, &curpx->acl, pol)) == NULL) {
417 retlen = snprintf(err, errlen,
418 "Error detected in %s '%s' while parsing '%s' condition",
419 proxy_type_str(curpx), curpx->id, args[3]);
420 return -1;
421 }
422
423 rule = (struct tcp_rule *)calloc(1, sizeof(*rule));
424 rule->cond = cond;
425 rule->action = action;
426 LIST_INIT(&rule->list);
427 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
428 return 0;
429 }
430
431 snprintf(err, errlen, "unknown argument '%s' after '%s' in %s '%s'",
432 args[1], args[0], proxy_type_str(proxy), curpx->id);
433 return -1;
434}
435
436/* return the number of bytes in the request buffer */
437static int
438acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
439 struct acl_expr *expr, struct acl_test *test)
440{
441 if (!l4 || !l4->req)
442 return 0;
443
444 test->i = l4->req->l;
445 test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE;
446 return 1;
447}
448
449
450static struct cfg_kw_list cfg_kws = {{ },{
451 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
452 { 0, NULL, NULL },
453}};
454
455static struct acl_kw_list acl_kws = {{ },{
456 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int },
457 { NULL, NULL, NULL, NULL },
458}};
459
Willy Tarreaue6b98942007-10-29 01:09:36 +0100460__attribute__((constructor))
461static void __tcp_protocol_init(void)
462{
463 protocol_register(&proto_tcpv4);
464 protocol_register(&proto_tcpv6);
Willy Tarreaub6866442008-07-14 23:54:42 +0200465 cfg_register_keywords(&cfg_kws);
466 acl_register_keywords(&acl_kws);
Willy Tarreaue6b98942007-10-29 01:09:36 +0100467}
468
469
470/*
471 * Local variables:
472 * c-indent-level: 8
473 * c-basic-offset: 8
474 * End:
475 */