blob: cf1e0ded108061227b8ed3b412230a28c61ace0a [file] [log] [blame]
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +01001/*
2 * AF_INET/AF_INET6 QUIC protocol layer.
3 *
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01004 * Copyright 2020 Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +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>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010015#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <time.h>
19
20#include <sys/param.h>
21#include <sys/socket.h>
22#include <sys/types.h>
23
24#include <netinet/udp.h>
25#include <netinet/in.h>
26
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020027#include <import/ebtree-t.h>
28
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010029#include <haproxy/api.h>
30#include <haproxy/arg.h>
Frédéric Lécaille48f8e192021-07-06 15:39:26 +020031#include <haproxy/cbuf.h>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010032#include <haproxy/connection.h>
33#include <haproxy/errors.h>
34#include <haproxy/fd.h>
35#include <haproxy/global.h>
36#include <haproxy/list.h>
37#include <haproxy/listener.h>
38#include <haproxy/log.h>
39#include <haproxy/namespace.h>
40#include <haproxy/port_range.h>
41#include <haproxy/protocol.h>
42#include <haproxy/proto_quic.h>
43#include <haproxy/proto_udp.h>
44#include <haproxy/proxy-t.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020045#include <haproxy/quic_conn.h>
Frédéric Lécaille70da8892020-11-06 15:49:49 +010046#include <haproxy/quic_sock.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020047#include <haproxy/sock.h>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010048#include <haproxy/sock_inet.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020049#include <haproxy/task.h>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010050#include <haproxy/tools.h>
51
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010052/* per-thread quic datagram handlers */
53struct quic_dghdlr *quic_dghdlrs;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010054
Frédéric Lécaille71f3abb2022-02-15 16:59:48 +010055/* Size of the internal buffer of QUIC RX buffer at the fd level */
56#define QUIC_RX_BUFSZ (1UL << 18)
57
Willy Tarreaub8dec4a2022-06-23 11:02:08 +020058DECLARE_STATIC_POOL(pool_head_quic_rxbuf, "quic_rxbuf", QUIC_RX_BUFSZ);
Frédéric Lécaille71f3abb2022-02-15 16:59:48 +010059
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010060static void quic_add_listener(struct protocol *proto, struct listener *listener);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010061static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen);
62static int quic_connect_server(struct connection *conn, int flags);
63static void quic_enable_listener(struct listener *listener);
64static void quic_disable_listener(struct listener *listener);
65
66/* Note: must not be declared <const> as its list will be overwritten */
67struct protocol proto_quic4 = {
68 .name = "quic4",
69
70 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +020071 .xprt_type = PROTO_TYPE_STREAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010072 .listen = quic_bind_listener,
73 .enable = quic_enable_listener,
74 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010075 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010076 .unbind = default_unbind_listener,
77 .suspend = default_suspend_listener,
78 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +010079 .accept_conn = quic_sock_accept_conn,
Willy Tarreaucdf7c8e2022-04-11 16:20:00 +020080 .get_src = quic_sock_get_src,
81 .get_dst = quic_sock_get_dst,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010082 .connect = quic_connect_server,
83
84 /* binding layer */
85 .rx_suspend = udp_suspend_receiver,
86 .rx_resume = udp_resume_receiver,
87
88 /* address family */
89 .fam = &proto_fam_inet4,
90
91 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020092 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010093 .sock_type = SOCK_DGRAM,
94 .sock_prot = IPPROTO_UDP,
95 .rx_enable = sock_enable,
96 .rx_disable = sock_disable,
97 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +010098 .rx_listening = quic_sock_accepting_conn,
99 .default_iocb = quic_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100100 .receivers = LIST_HEAD_INIT(proto_quic4.receivers),
101 .nb_receivers = 0,
102};
103
104INITCALL1(STG_REGISTER, protocol_register, &proto_quic4);
105
106/* Note: must not be declared <const> as its list will be overwritten */
107struct protocol proto_quic6 = {
108 .name = "quic6",
109
110 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +0200111 .xprt_type = PROTO_TYPE_STREAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100112 .listen = quic_bind_listener,
113 .enable = quic_enable_listener,
114 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100115 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100116 .unbind = default_unbind_listener,
117 .suspend = default_suspend_listener,
118 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100119 .accept_conn = quic_sock_accept_conn,
Willy Tarreaucdf7c8e2022-04-11 16:20:00 +0200120 .get_src = quic_sock_get_src,
121 .get_dst = quic_sock_get_dst,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100122 .connect = quic_connect_server,
123
124 /* binding layer */
125 .rx_suspend = udp_suspend_receiver,
126 .rx_resume = udp_resume_receiver,
127
128 /* address family */
129 .fam = &proto_fam_inet6,
130
131 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200132 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100133 .sock_type = SOCK_DGRAM,
134 .sock_prot = IPPROTO_UDP,
135 .rx_enable = sock_enable,
136 .rx_disable = sock_disable,
137 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100138 .rx_listening = quic_sock_accepting_conn,
139 .default_iocb = quic_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100140 .receivers = LIST_HEAD_INIT(proto_quic6.receivers),
141 .nb_receivers = 0,
142};
143
144INITCALL1(STG_REGISTER, protocol_register, &proto_quic6);
145
146/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
147 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
148 * - 0 : ignore remote address (may even be a NULL pointer)
149 * - 1 : use provided address
150 * - 2 : use provided port
151 * - 3 : use both
152 *
153 * The function supports multiple foreign binding methods :
154 * - linux_tproxy: we directly bind to the foreign address
155 * The second one can be used as a fallback for the first one.
156 * This function returns 0 when everything's OK, 1 if it could not bind, to the
157 * local address, 2 if it could not bind to the foreign address.
158 */
159int quic_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
160{
161 struct sockaddr_storage bind_addr;
162 int foreign_ok = 0;
163 int ret;
164 static THREAD_LOCAL int ip_transp_working = 1;
165 static THREAD_LOCAL int ip6_transp_working = 1;
166
167 switch (local->ss_family) {
168 case AF_INET:
169 if (flags && ip_transp_working) {
170 /* This deserves some explanation. Some platforms will support
171 * multiple combinations of certain methods, so we try the
172 * supported ones until one succeeds.
173 */
174 if (sock_inet4_make_foreign(fd))
175 foreign_ok = 1;
176 else
177 ip_transp_working = 0;
178 }
179 break;
180 case AF_INET6:
181 if (flags && ip6_transp_working) {
182 if (sock_inet6_make_foreign(fd))
183 foreign_ok = 1;
184 else
185 ip6_transp_working = 0;
186 }
187 break;
188 }
189
190 if (flags) {
191 memset(&bind_addr, 0, sizeof(bind_addr));
192 bind_addr.ss_family = remote->ss_family;
193 switch (remote->ss_family) {
194 case AF_INET:
195 if (flags & 1)
196 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
197 if (flags & 2)
198 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
199 break;
200 case AF_INET6:
201 if (flags & 1)
202 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
203 if (flags & 2)
204 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
205 break;
206 default:
207 /* we don't want to try to bind to an unknown address family */
208 foreign_ok = 0;
209 }
210 }
211
212 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
213 if (foreign_ok) {
214 if (is_inet_addr(&bind_addr)) {
215 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
216 if (ret < 0)
217 return 2;
218 }
219 }
220 else {
221 if (is_inet_addr(local)) {
222 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
223 if (ret < 0)
224 return 1;
225 }
226 }
227
228 if (!flags)
229 return 0;
230
231 if (!foreign_ok)
232 /* we could not bind to a foreign address */
233 return 2;
234
235 return 0;
236}
237
238/*
239 * This function initiates a QUIC connection establishment to the target assigned
240 * to connection <conn> using (si->{target,dst}). A source address may be
241 * pointed to by conn->src in case of transparent proxying. Normal source
242 * bind addresses are still determined locally (due to the possible need of a
243 * source port). conn->target may point either to a valid server or to a backend,
244 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
245 * supported. The <data> parameter is a boolean indicating whether there are data
246 * waiting for being sent or not, in order to adjust data write polling and on
247 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
248 * is not used.
249 *
250 * Note that a pending send_proxy message accounts for data.
251 *
252 * It can return one of :
253 * - SF_ERR_NONE if everything's OK
254 * - SF_ERR_SRVTO if there are no more servers
255 * - SF_ERR_SRVCL if the connection was refused by the server
256 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
257 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
258 * - SF_ERR_INTERNAL for any other purely internal errors
259 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
260 *
261 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
262 * it's invalid and the caller has nothing to do.
263 */
264
265int quic_connect_server(struct connection *conn, int flags)
266{
267 int fd;
268 struct server *srv;
269 struct proxy *be;
270 struct conn_src *src;
271 struct sockaddr_storage *addr;
272
Willy Tarreau158b6cf2022-05-02 17:45:12 +0200273 BUG_ON(!conn->dst);
274
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100275 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
276
277 switch (obj_type(conn->target)) {
278 case OBJ_TYPE_PROXY:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100279 be = __objt_proxy(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100280 srv = NULL;
281 break;
282 case OBJ_TYPE_SERVER:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100283 srv = __objt_server(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100284 be = srv->proxy;
285 break;
286 default:
287 conn->flags |= CO_FL_ERROR;
288 return SF_ERR_INTERNAL;
289 }
290
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100291 fd = conn->handle.fd = sock_create_server_socket(conn);
292
293 if (fd == -1) {
294 qfprintf(stderr, "Cannot get a server socket.\n");
295
296 if (errno == ENFILE) {
297 conn->err_code = CO_ER_SYS_FDLIM;
298 send_log(be, LOG_EMERG,
299 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
300 be->id, global.maxsock);
301 }
302 else if (errno == EMFILE) {
303 conn->err_code = CO_ER_PROC_FDLIM;
304 send_log(be, LOG_EMERG,
305 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
306 be->id, global.maxsock);
307 }
308 else if (errno == ENOBUFS || errno == ENOMEM) {
309 conn->err_code = CO_ER_SYS_MEMLIM;
310 send_log(be, LOG_EMERG,
311 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
312 be->id, global.maxsock);
313 }
314 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
315 conn->err_code = CO_ER_NOPROTO;
316 }
317 else
318 conn->err_code = CO_ER_SOCK_ERR;
319
320 /* this is a resource error */
321 conn->flags |= CO_FL_ERROR;
322 return SF_ERR_RESOURCE;
323 }
324
325 if (fd >= global.maxsock) {
326 /* do not log anything there, it's a normal condition when this option
327 * is used to serialize connections to a server !
328 */
329 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
330 close(fd);
331 conn->err_code = CO_ER_CONF_FDLIM;
332 conn->flags |= CO_FL_ERROR;
333 return SF_ERR_PRXCOND; /* it is a configuration limit */
334 }
335
Willy Tarreau38247432022-04-26 10:24:14 +0200336 if (fd_set_nonblock(fd) == -1) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100337 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
338 close(fd);
339 conn->err_code = CO_ER_SOCK_ERR;
340 conn->flags |= CO_FL_ERROR;
341 return SF_ERR_INTERNAL;
342 }
343
Willy Tarreau38247432022-04-26 10:24:14 +0200344 if (master == 1 && fd_set_cloexec(fd) == -1) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100345 ha_alert("Cannot set CLOEXEC on client socket.\n");
346 close(fd);
347 conn->err_code = CO_ER_SOCK_ERR;
348 conn->flags |= CO_FL_ERROR;
349 return SF_ERR_INTERNAL;
350 }
351
352 /* allow specific binding :
353 * - server-specific at first
354 * - proxy-specific next
355 */
356 if (srv && srv->conn_src.opts & CO_SRC_BIND)
357 src = &srv->conn_src;
358 else if (be->conn_src.opts & CO_SRC_BIND)
359 src = &be->conn_src;
360 else
361 src = NULL;
362
363 if (src) {
364 int ret, flags = 0;
365
366 if (conn->src && is_inet_addr(conn->src)) {
367 switch (src->opts & CO_SRC_TPROXY_MASK) {
368 case CO_SRC_TPROXY_CLI:
369 conn_set_private(conn);
370 /* fall through */
371 case CO_SRC_TPROXY_ADDR:
372 flags = 3;
373 break;
374 case CO_SRC_TPROXY_CIP:
375 case CO_SRC_TPROXY_DYN:
376 conn_set_private(conn);
377 flags = 1;
378 break;
379 }
380 }
381
382#ifdef SO_BINDTODEVICE
383 /* Note: this might fail if not CAP_NET_RAW */
384 if (src->iface_name)
385 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
386#endif
387
388 if (src->sport_range) {
389 int attempts = 10; /* should be more than enough to find a spare port */
390 struct sockaddr_storage sa;
391
392 ret = 1;
393 memcpy(&sa, &src->source_addr, sizeof(sa));
394
395 do {
396 /* note: in case of retry, we may have to release a previously
397 * allocated port, hence this loop's construct.
398 */
399 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
400 fdinfo[fd].port_range = NULL;
401
402 if (!attempts)
403 break;
404 attempts--;
405
406 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
407 if (!fdinfo[fd].local_port) {
408 conn->err_code = CO_ER_PORT_RANGE;
409 break;
410 }
411
412 fdinfo[fd].port_range = src->sport_range;
413 set_host_port(&sa, fdinfo[fd].local_port);
414
415 ret = quic_bind_socket(fd, flags, &sa, conn->src);
416 if (ret != 0)
417 conn->err_code = CO_ER_CANT_BIND;
418 } while (ret != 0); /* binding NOK */
419 }
420 else {
421#ifdef IP_BIND_ADDRESS_NO_PORT
422 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200423 setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, (const void *) &bind_address_no_port, sizeof(int));
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100424#endif
425 ret = quic_bind_socket(fd, flags, &src->source_addr, conn->src);
426 if (ret != 0)
427 conn->err_code = CO_ER_CANT_BIND;
428 }
429
430 if (unlikely(ret != 0)) {
431 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
432 fdinfo[fd].port_range = NULL;
433 close(fd);
434
435 if (ret == 1) {
436 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
437 be->id);
438 send_log(be, LOG_EMERG,
439 "Cannot bind to source address before connect() for backend %s.\n",
440 be->id);
441 } else {
442 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
443 be->id);
444 send_log(be, LOG_EMERG,
445 "Cannot bind to tproxy source address before connect() for backend %s.\n",
446 be->id);
447 }
448 conn->flags |= CO_FL_ERROR;
449 return SF_ERR_RESOURCE;
450 }
451 }
452
453 if (global.tune.server_sndbuf)
454 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
455
456 if (global.tune.server_rcvbuf)
457 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
458
459 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
460 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
461 if (errno == EINPROGRESS || errno == EALREADY) {
462 /* common case, let's wait for connect status */
463 conn->flags |= CO_FL_WAIT_L4_CONN;
464 }
465 else if (errno == EISCONN) {
466 /* should normally not happen but if so, indicates that it's OK */
467 conn->flags &= ~CO_FL_WAIT_L4_CONN;
468 }
Willy Tarreauacef5e22022-04-25 20:32:15 +0200469 else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100470 char *msg;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200471 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRNOTAVAIL) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100472 msg = "no free ports";
473 conn->err_code = CO_ER_FREE_PORTS;
474 }
475 else {
476 msg = "local address already in use";
477 conn->err_code = CO_ER_ADDR_INUSE;
478 }
479
480 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
481 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
482 fdinfo[fd].port_range = NULL;
483 close(fd);
484 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
485 conn->flags |= CO_FL_ERROR;
486 return SF_ERR_RESOURCE;
487 } else if (errno == ETIMEDOUT) {
488 //qfprintf(stderr,"Connect(): ETIMEDOUT");
489 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
490 fdinfo[fd].port_range = NULL;
491 close(fd);
492 conn->err_code = CO_ER_SOCK_ERR;
493 conn->flags |= CO_FL_ERROR;
494 return SF_ERR_SRVTO;
495 } else {
496 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
497 //qfprintf(stderr,"Connect(): %d", errno);
498 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
499 fdinfo[fd].port_range = NULL;
500 close(fd);
501 conn->err_code = CO_ER_SOCK_ERR;
502 conn->flags |= CO_FL_ERROR;
503 return SF_ERR_SRVCL;
504 }
505 }
506 else {
507 /* connect() == 0, this is great! */
508 conn->flags &= ~CO_FL_WAIT_L4_CONN;
509 }
510
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100511 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200512 HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100513
514 if (conn->flags & CO_FL_WAIT_L4_CONN) {
515 fd_want_send(fd);
516 fd_cant_send(fd);
517 fd_cant_recv(fd);
518 }
519
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100520 return SF_ERR_NONE; /* connection is OK */
521}
522
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100523/* Add listener <listener> to protocol <proto>. Technically speaking we just
524 * initialize a few entries which should be doable during quic_bind_listener().
525 * The end of the initialization goes on with the default function.
526 */
527static void quic_add_listener(struct protocol *proto, struct listener *listener)
528{
Amaury Denoyelleb59b8892022-01-25 17:48:47 +0100529 listener->flags |= LI_F_QUIC_LISTENER;
Amaury Denoyelle683b5fc2022-01-26 11:56:48 +0100530 listener->rx.flags |= RX_F_LOCAL_ACCEPT;
Amaury Denoyelle2ce99fe2022-01-19 15:46:11 +0100531
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100532 default_add_listener(proto, listener);
533}
534
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200535/* Allocate the RX buffers for <l> listener.
536 * Return 1 if succeeded, 0 if not.
537 */
538static int quic_alloc_rxbufs_listener(struct listener *l)
539{
540 int i;
541 struct rxbuf *rxbuf;
542
543 l->rx.rxbufs = calloc(global.nbthread, sizeof *l->rx.rxbufs);
544 if (!l->rx.rxbufs)
545 return 0;
546
547 MT_LIST_INIT(&l->rx.rxbuf_list);
548 for (i = 0; i < global.nbthread; i++) {
549 char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100550 struct rxbuf *rxbuf;
551
552 rxbuf = calloc(1, sizeof *rxbuf);
553 if (!rxbuf)
554 goto err;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200555
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200556 buf = pool_alloc(pool_head_quic_rxbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100557 if (!buf) {
558 free(rxbuf);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200559 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100560 }
561
562 l->rx.rxbufs[i] = rxbuf;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200563
564 rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0);
Frédéric Lécaille53898bb2022-01-26 15:55:21 +0100565 LIST_INIT(&rxbuf->dgrams);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200566 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->mt_list);
567 }
568
569 return 1;
570
571 err:
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100572 while ((rxbuf = MT_LIST_POP(&l->rx.rxbuf_list, typeof(rxbuf), mt_list))) {
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200573 pool_free(pool_head_quic_rxbuf, rxbuf->buf.area);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100574 free(rxbuf);
575 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200576 free(l->rx.rxbufs);
577 return 0;
578}
579
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100580/* This function tries to bind a QUIC4/6 listener. It may return a warning or
581 * an error message in <errmsg> if the message is at most <errlen> bytes long
582 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
583 * The return value is composed from ERR_ABORT, ERR_WARN,
584 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
585 * was alright and that no message was returned. ERR_RETRYABLE means that an
586 * error occurred but that it may vanish after a retry (eg: port in use), and
587 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
588 * the meaning of the error, but just indicate that a message is present which
589 * should be displayed with the respective level. Last, ERR_ABORT indicates
590 * that it's pointless to try to start other listeners. No error message is
591 * returned if errlen is NULL.
592 */
593static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen)
594{
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200595 const struct sockaddr_storage addr = listener->rx.addr;
596 int fd, err = ERR_NONE;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100597 char *msg = NULL;
598
599 /* ensure we never return garbage */
600 if (errlen)
601 *errmsg = 0;
602
603 if (listener->state != LI_ASSIGNED)
604 return ERR_NONE; /* already bound */
605
606 if (!(listener->rx.flags & RX_F_BOUND)) {
607 msg = "receiving socket not bound";
608 goto udp_return;
609 }
610
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200611 /* Set IP_PKTINFO to retrieve destination address on recv. */
612 fd = listener->rx.fd;
613 switch (addr.ss_family) {
614 case AF_INET:
615#if defined(IP_PKTINFO)
616 setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
617#elif defined(IP_RECVDSTADDR)
618 setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &one, sizeof(one));
619#endif /* IP_PKTINFO || IP_RECVDSTADDR */
620 break;
621 case AF_INET6:
622#ifdef IPV6_RECVPKTINFO
623 setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
624#endif
625 break;
626 default:
627 break;
628 }
629
Willy Tarreaucab054b2022-10-11 08:36:21 +0200630 if (!quic_alloc_rxbufs_listener(listener)) {
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100631 msg = "could not initialize tx/rx rings";
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200632 err |= ERR_WARN;
633 goto udp_return;
634 }
635
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100636 listener_set_state(listener, LI_LISTEN);
637
638 udp_return:
639 if (msg && errlen) {
640 char pn[INET6_ADDRSTRLEN];
641
642 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200643 snprintf(errmsg, errlen, "%s for [%s:%d]", msg, pn, get_host_port(&listener->rx.addr));
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100644 }
645 return err;
646}
647
648/* Enable receipt of incoming connections for listener <l>. The receiver must
649 * still be valid. Does nothing in early boot (needs fd_updt).
650 */
651static void quic_enable_listener(struct listener *l)
652{
653 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500654 * is the responsibility of the QUIC xprt to stop accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100655 * connections.
656 */
657 if (fd_updt)
658 fd_want_recv(l->rx.fd);
659}
660
661/* Disable receipt of incoming connections for listener <l>. The receiver must
662 * still be valid. Does nothing in early boot (needs fd_updt).
663 */
664static void quic_disable_listener(struct listener *l)
665{
666 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500667 * is the responsibility of the QUIC xprt to start accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100668 * connections again.
669 */
670 if (fd_updt)
671 fd_stop_recv(l->rx.fd);
672}
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100673
674static int quic_alloc_dghdlrs(void)
675{
676 int i;
677
Tim Duesterhus9fb57e82022-06-01 21:58:37 +0200678 quic_dghdlrs = calloc(global.nbthread, sizeof(*quic_dghdlrs));
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100679 if (!quic_dghdlrs) {
680 ha_alert("Failed to allocate the quic datagram handlers.\n");
681 return 0;
682 }
683
684 for (i = 0; i < global.nbthread; i++) {
685 struct quic_dghdlr *dghdlr = &quic_dghdlrs[i];
686
687 dghdlr->task = tasklet_new();
688 if (!dghdlr->task) {
689 ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i);
690 return 0;
691 }
692
693 tasklet_set_tid(dghdlr->task, i);
694 dghdlr->task->context = dghdlr;
695 dghdlr->task->process = quic_lstnr_dghdlr;
696
697 dghdlr->odcids = EB_ROOT_UNIQUE;
698 dghdlr->cids = EB_ROOT_UNIQUE;
699
700 MT_LIST_INIT(&dghdlr->dgrams);
701 }
702
703 return 1;
704}
705REGISTER_POST_CHECK(quic_alloc_dghdlrs);
706
707static int quic_deallocate_dghdlrs(void)
708{
709 int i;
710
711 if (quic_dghdlrs) {
712 for (i = 0; i < global.nbthread; ++i)
713 tasklet_free(quic_dghdlrs[i].task);
714 free(quic_dghdlrs);
715 }
716
717 return 1;
718}
719REGISTER_POST_DEINIT(quic_deallocate_dghdlrs);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100720
721/*
722 * Local variables:
723 * c-indent-level: 8
724 * c-basic-offset: 8
725 * End:
726 */