blob: 20323730182f0c471c65aa0ff9e48ffdc11fdd06 [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
27#include <haproxy/api.h>
28#include <haproxy/arg.h>
Frédéric Lécaille48f8e192021-07-06 15:39:26 +020029#include <haproxy/cbuf.h>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010030#include <haproxy/connection.h>
31#include <haproxy/errors.h>
32#include <haproxy/fd.h>
33#include <haproxy/global.h>
34#include <haproxy/list.h>
35#include <haproxy/listener.h>
36#include <haproxy/log.h>
37#include <haproxy/namespace.h>
38#include <haproxy/port_range.h>
39#include <haproxy/protocol.h>
40#include <haproxy/proto_quic.h>
41#include <haproxy/proto_udp.h>
42#include <haproxy/proxy-t.h>
43#include <haproxy/sock.h>
Frédéric Lécaille70da8892020-11-06 15:49:49 +010044#include <haproxy/quic_sock.h>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010045#include <haproxy/sock_inet.h>
46#include <haproxy/tools.h>
Frédéric Lécaille25bc8872022-01-27 09:15:40 +010047#include <haproxy/xprt_quic.h>
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010048
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010049/* per-thread quic datagram handlers */
50struct quic_dghdlr *quic_dghdlrs;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010051
Frédéric Lécaille71f3abb2022-02-15 16:59:48 +010052/* Size of the internal buffer of QUIC RX buffer at the fd level */
53#define QUIC_RX_BUFSZ (1UL << 18)
54
Willy Tarreaub8dec4a2022-06-23 11:02:08 +020055DECLARE_STATIC_POOL(pool_head_quic_rxbuf, "quic_rxbuf", QUIC_RX_BUFSZ);
Frédéric Lécaille71f3abb2022-02-15 16:59:48 +010056
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010057static void quic_add_listener(struct protocol *proto, struct listener *listener);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010058static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen);
59static int quic_connect_server(struct connection *conn, int flags);
60static void quic_enable_listener(struct listener *listener);
61static void quic_disable_listener(struct listener *listener);
62
63/* Note: must not be declared <const> as its list will be overwritten */
64struct protocol proto_quic4 = {
65 .name = "quic4",
66
67 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +020068 .xprt_type = PROTO_TYPE_STREAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010069 .listen = quic_bind_listener,
70 .enable = quic_enable_listener,
71 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010072 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010073 .unbind = default_unbind_listener,
74 .suspend = default_suspend_listener,
75 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +010076 .accept_conn = quic_sock_accept_conn,
Willy Tarreaucdf7c8e2022-04-11 16:20:00 +020077 .get_src = quic_sock_get_src,
78 .get_dst = quic_sock_get_dst,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010079 .connect = quic_connect_server,
80
81 /* binding layer */
82 .rx_suspend = udp_suspend_receiver,
83 .rx_resume = udp_resume_receiver,
84
85 /* address family */
86 .fam = &proto_fam_inet4,
87
88 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020089 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010090 .sock_type = SOCK_DGRAM,
91 .sock_prot = IPPROTO_UDP,
92 .rx_enable = sock_enable,
93 .rx_disable = sock_disable,
94 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +010095 .rx_listening = quic_sock_accepting_conn,
96 .default_iocb = quic_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010097 .receivers = LIST_HEAD_INIT(proto_quic4.receivers),
98 .nb_receivers = 0,
99};
100
101INITCALL1(STG_REGISTER, protocol_register, &proto_quic4);
102
103/* Note: must not be declared <const> as its list will be overwritten */
104struct protocol proto_quic6 = {
105 .name = "quic6",
106
107 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +0200108 .xprt_type = PROTO_TYPE_STREAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100109 .listen = quic_bind_listener,
110 .enable = quic_enable_listener,
111 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100112 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100113 .unbind = default_unbind_listener,
114 .suspend = default_suspend_listener,
115 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100116 .accept_conn = quic_sock_accept_conn,
Willy Tarreaucdf7c8e2022-04-11 16:20:00 +0200117 .get_src = quic_sock_get_src,
118 .get_dst = quic_sock_get_dst,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100119 .connect = quic_connect_server,
120
121 /* binding layer */
122 .rx_suspend = udp_suspend_receiver,
123 .rx_resume = udp_resume_receiver,
124
125 /* address family */
126 .fam = &proto_fam_inet6,
127
128 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200129 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100130 .sock_type = SOCK_DGRAM,
131 .sock_prot = IPPROTO_UDP,
132 .rx_enable = sock_enable,
133 .rx_disable = sock_disable,
134 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100135 .rx_listening = quic_sock_accepting_conn,
136 .default_iocb = quic_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100137 .receivers = LIST_HEAD_INIT(proto_quic6.receivers),
138 .nb_receivers = 0,
139};
140
141INITCALL1(STG_REGISTER, protocol_register, &proto_quic6);
142
143/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
144 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
145 * - 0 : ignore remote address (may even be a NULL pointer)
146 * - 1 : use provided address
147 * - 2 : use provided port
148 * - 3 : use both
149 *
150 * The function supports multiple foreign binding methods :
151 * - linux_tproxy: we directly bind to the foreign address
152 * The second one can be used as a fallback for the first one.
153 * This function returns 0 when everything's OK, 1 if it could not bind, to the
154 * local address, 2 if it could not bind to the foreign address.
155 */
156int quic_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
157{
158 struct sockaddr_storage bind_addr;
159 int foreign_ok = 0;
160 int ret;
161 static THREAD_LOCAL int ip_transp_working = 1;
162 static THREAD_LOCAL int ip6_transp_working = 1;
163
164 switch (local->ss_family) {
165 case AF_INET:
166 if (flags && ip_transp_working) {
167 /* This deserves some explanation. Some platforms will support
168 * multiple combinations of certain methods, so we try the
169 * supported ones until one succeeds.
170 */
171 if (sock_inet4_make_foreign(fd))
172 foreign_ok = 1;
173 else
174 ip_transp_working = 0;
175 }
176 break;
177 case AF_INET6:
178 if (flags && ip6_transp_working) {
179 if (sock_inet6_make_foreign(fd))
180 foreign_ok = 1;
181 else
182 ip6_transp_working = 0;
183 }
184 break;
185 }
186
187 if (flags) {
188 memset(&bind_addr, 0, sizeof(bind_addr));
189 bind_addr.ss_family = remote->ss_family;
190 switch (remote->ss_family) {
191 case AF_INET:
192 if (flags & 1)
193 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
194 if (flags & 2)
195 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
196 break;
197 case AF_INET6:
198 if (flags & 1)
199 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
200 if (flags & 2)
201 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
202 break;
203 default:
204 /* we don't want to try to bind to an unknown address family */
205 foreign_ok = 0;
206 }
207 }
208
209 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
210 if (foreign_ok) {
211 if (is_inet_addr(&bind_addr)) {
212 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
213 if (ret < 0)
214 return 2;
215 }
216 }
217 else {
218 if (is_inet_addr(local)) {
219 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
220 if (ret < 0)
221 return 1;
222 }
223 }
224
225 if (!flags)
226 return 0;
227
228 if (!foreign_ok)
229 /* we could not bind to a foreign address */
230 return 2;
231
232 return 0;
233}
234
235/*
236 * This function initiates a QUIC connection establishment to the target assigned
237 * to connection <conn> using (si->{target,dst}). A source address may be
238 * pointed to by conn->src in case of transparent proxying. Normal source
239 * bind addresses are still determined locally (due to the possible need of a
240 * source port). conn->target may point either to a valid server or to a backend,
241 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
242 * supported. The <data> parameter is a boolean indicating whether there are data
243 * waiting for being sent or not, in order to adjust data write polling and on
244 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
245 * is not used.
246 *
247 * Note that a pending send_proxy message accounts for data.
248 *
249 * It can return one of :
250 * - SF_ERR_NONE if everything's OK
251 * - SF_ERR_SRVTO if there are no more servers
252 * - SF_ERR_SRVCL if the connection was refused by the server
253 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
254 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
255 * - SF_ERR_INTERNAL for any other purely internal errors
256 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
257 *
258 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
259 * it's invalid and the caller has nothing to do.
260 */
261
262int quic_connect_server(struct connection *conn, int flags)
263{
264 int fd;
265 struct server *srv;
266 struct proxy *be;
267 struct conn_src *src;
268 struct sockaddr_storage *addr;
269
Willy Tarreau158b6cf2022-05-02 17:45:12 +0200270 BUG_ON(!conn->dst);
271
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100272 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
273
274 switch (obj_type(conn->target)) {
275 case OBJ_TYPE_PROXY:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100276 be = __objt_proxy(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100277 srv = NULL;
278 break;
279 case OBJ_TYPE_SERVER:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100280 srv = __objt_server(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100281 be = srv->proxy;
282 break;
283 default:
284 conn->flags |= CO_FL_ERROR;
285 return SF_ERR_INTERNAL;
286 }
287
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100288 fd = conn->handle.fd = sock_create_server_socket(conn);
289
290 if (fd == -1) {
291 qfprintf(stderr, "Cannot get a server socket.\n");
292
293 if (errno == ENFILE) {
294 conn->err_code = CO_ER_SYS_FDLIM;
295 send_log(be, LOG_EMERG,
296 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
297 be->id, global.maxsock);
298 }
299 else if (errno == EMFILE) {
300 conn->err_code = CO_ER_PROC_FDLIM;
301 send_log(be, LOG_EMERG,
302 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
303 be->id, global.maxsock);
304 }
305 else if (errno == ENOBUFS || errno == ENOMEM) {
306 conn->err_code = CO_ER_SYS_MEMLIM;
307 send_log(be, LOG_EMERG,
308 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
309 be->id, global.maxsock);
310 }
311 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
312 conn->err_code = CO_ER_NOPROTO;
313 }
314 else
315 conn->err_code = CO_ER_SOCK_ERR;
316
317 /* this is a resource error */
318 conn->flags |= CO_FL_ERROR;
319 return SF_ERR_RESOURCE;
320 }
321
322 if (fd >= global.maxsock) {
323 /* do not log anything there, it's a normal condition when this option
324 * is used to serialize connections to a server !
325 */
326 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
327 close(fd);
328 conn->err_code = CO_ER_CONF_FDLIM;
329 conn->flags |= CO_FL_ERROR;
330 return SF_ERR_PRXCOND; /* it is a configuration limit */
331 }
332
Willy Tarreau38247432022-04-26 10:24:14 +0200333 if (fd_set_nonblock(fd) == -1) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100334 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
335 close(fd);
336 conn->err_code = CO_ER_SOCK_ERR;
337 conn->flags |= CO_FL_ERROR;
338 return SF_ERR_INTERNAL;
339 }
340
Willy Tarreau38247432022-04-26 10:24:14 +0200341 if (master == 1 && fd_set_cloexec(fd) == -1) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100342 ha_alert("Cannot set CLOEXEC on client socket.\n");
343 close(fd);
344 conn->err_code = CO_ER_SOCK_ERR;
345 conn->flags |= CO_FL_ERROR;
346 return SF_ERR_INTERNAL;
347 }
348
349 /* allow specific binding :
350 * - server-specific at first
351 * - proxy-specific next
352 */
353 if (srv && srv->conn_src.opts & CO_SRC_BIND)
354 src = &srv->conn_src;
355 else if (be->conn_src.opts & CO_SRC_BIND)
356 src = &be->conn_src;
357 else
358 src = NULL;
359
360 if (src) {
361 int ret, flags = 0;
362
363 if (conn->src && is_inet_addr(conn->src)) {
364 switch (src->opts & CO_SRC_TPROXY_MASK) {
365 case CO_SRC_TPROXY_CLI:
366 conn_set_private(conn);
367 /* fall through */
368 case CO_SRC_TPROXY_ADDR:
369 flags = 3;
370 break;
371 case CO_SRC_TPROXY_CIP:
372 case CO_SRC_TPROXY_DYN:
373 conn_set_private(conn);
374 flags = 1;
375 break;
376 }
377 }
378
379#ifdef SO_BINDTODEVICE
380 /* Note: this might fail if not CAP_NET_RAW */
381 if (src->iface_name)
382 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
383#endif
384
385 if (src->sport_range) {
386 int attempts = 10; /* should be more than enough to find a spare port */
387 struct sockaddr_storage sa;
388
389 ret = 1;
390 memcpy(&sa, &src->source_addr, sizeof(sa));
391
392 do {
393 /* note: in case of retry, we may have to release a previously
394 * allocated port, hence this loop's construct.
395 */
396 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
397 fdinfo[fd].port_range = NULL;
398
399 if (!attempts)
400 break;
401 attempts--;
402
403 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
404 if (!fdinfo[fd].local_port) {
405 conn->err_code = CO_ER_PORT_RANGE;
406 break;
407 }
408
409 fdinfo[fd].port_range = src->sport_range;
410 set_host_port(&sa, fdinfo[fd].local_port);
411
412 ret = quic_bind_socket(fd, flags, &sa, conn->src);
413 if (ret != 0)
414 conn->err_code = CO_ER_CANT_BIND;
415 } while (ret != 0); /* binding NOK */
416 }
417 else {
418#ifdef IP_BIND_ADDRESS_NO_PORT
419 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200420 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 +0100421#endif
422 ret = quic_bind_socket(fd, flags, &src->source_addr, conn->src);
423 if (ret != 0)
424 conn->err_code = CO_ER_CANT_BIND;
425 }
426
427 if (unlikely(ret != 0)) {
428 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
429 fdinfo[fd].port_range = NULL;
430 close(fd);
431
432 if (ret == 1) {
433 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
434 be->id);
435 send_log(be, LOG_EMERG,
436 "Cannot bind to source address before connect() for backend %s.\n",
437 be->id);
438 } else {
439 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
440 be->id);
441 send_log(be, LOG_EMERG,
442 "Cannot bind to tproxy source address before connect() for backend %s.\n",
443 be->id);
444 }
445 conn->flags |= CO_FL_ERROR;
446 return SF_ERR_RESOURCE;
447 }
448 }
449
450 if (global.tune.server_sndbuf)
451 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
452
453 if (global.tune.server_rcvbuf)
454 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
455
456 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
457 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
458 if (errno == EINPROGRESS || errno == EALREADY) {
459 /* common case, let's wait for connect status */
460 conn->flags |= CO_FL_WAIT_L4_CONN;
461 }
462 else if (errno == EISCONN) {
463 /* should normally not happen but if so, indicates that it's OK */
464 conn->flags &= ~CO_FL_WAIT_L4_CONN;
465 }
Willy Tarreauacef5e22022-04-25 20:32:15 +0200466 else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100467 char *msg;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200468 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRNOTAVAIL) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100469 msg = "no free ports";
470 conn->err_code = CO_ER_FREE_PORTS;
471 }
472 else {
473 msg = "local address already in use";
474 conn->err_code = CO_ER_ADDR_INUSE;
475 }
476
477 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
478 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
479 fdinfo[fd].port_range = NULL;
480 close(fd);
481 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
482 conn->flags |= CO_FL_ERROR;
483 return SF_ERR_RESOURCE;
484 } else if (errno == ETIMEDOUT) {
485 //qfprintf(stderr,"Connect(): ETIMEDOUT");
486 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
487 fdinfo[fd].port_range = NULL;
488 close(fd);
489 conn->err_code = CO_ER_SOCK_ERR;
490 conn->flags |= CO_FL_ERROR;
491 return SF_ERR_SRVTO;
492 } else {
493 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
494 //qfprintf(stderr,"Connect(): %d", errno);
495 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
496 fdinfo[fd].port_range = NULL;
497 close(fd);
498 conn->err_code = CO_ER_SOCK_ERR;
499 conn->flags |= CO_FL_ERROR;
500 return SF_ERR_SRVCL;
501 }
502 }
503 else {
504 /* connect() == 0, this is great! */
505 conn->flags &= ~CO_FL_WAIT_L4_CONN;
506 }
507
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100508 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200509 HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100510
511 if (conn->flags & CO_FL_WAIT_L4_CONN) {
512 fd_want_send(fd);
513 fd_cant_send(fd);
514 fd_cant_recv(fd);
515 }
516
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100517 return SF_ERR_NONE; /* connection is OK */
518}
519
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100520/* Add listener <listener> to protocol <proto>. Technically speaking we just
521 * initialize a few entries which should be doable during quic_bind_listener().
522 * The end of the initialization goes on with the default function.
523 */
524static void quic_add_listener(struct protocol *proto, struct listener *listener)
525{
Amaury Denoyelleb59b8892022-01-25 17:48:47 +0100526 listener->flags |= LI_F_QUIC_LISTENER;
Amaury Denoyelle683b5fc2022-01-26 11:56:48 +0100527 listener->rx.flags |= RX_F_LOCAL_ACCEPT;
Amaury Denoyelle2ce99fe2022-01-19 15:46:11 +0100528
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100529 default_add_listener(proto, listener);
530}
531
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200532/* Allocate the TX ring buffers for <l> listener.
533 * Return 1 if succeeded, 0 if not.
534 */
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200535static int quic_alloc_tx_rings_listener(struct listener *l)
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200536{
537 struct qring *qr;
538 int i;
539
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200540 l->rx.tx_qrings = calloc(global.nbthread, sizeof *l->rx.tx_qrings);
541 if (!l->rx.tx_qrings)
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200542 return 0;
543
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200544 MT_LIST_INIT(&l->rx.tx_qring_list);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200545 for (i = 0; i < global.nbthread; i++) {
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200546 unsigned char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100547 struct qring *qr;
548
549 qr = calloc(1, sizeof *qr);
550 if (!qr)
551 goto err;
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200552
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200553 buf = pool_alloc(pool_head_quic_tx_ring);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100554 if (!buf) {
555 free(qr);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200556 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100557 }
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200558
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200559 qr->cbuf = cbuf_new(buf, QUIC_TX_RING_BUFSZ);
560 if (!qr->cbuf) {
561 pool_free(pool_head_quic_tx_ring, buf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100562 free(qr);
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200563 goto err;
564 }
565
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100566 l->rx.tx_qrings[i] = qr;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200567 MT_LIST_APPEND(&l->rx.tx_qring_list, &qr->mt_list);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200568 }
569
570 return 1;
571
572 err:
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200573 while ((qr = MT_LIST_POP(&l->rx.tx_qring_list, typeof(qr), mt_list))) {
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200574 pool_free(pool_head_quic_tx_ring, qr->cbuf->buf);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200575 cbuf_free(qr->cbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100576 free(qr);
Frédéric Lécaille8b19a9f2021-08-04 15:10:32 +0200577 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200578 free(l->rx.tx_qrings);
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200579 return 0;
580}
581
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200582/* Allocate the RX buffers for <l> listener.
583 * Return 1 if succeeded, 0 if not.
584 */
585static int quic_alloc_rxbufs_listener(struct listener *l)
586{
587 int i;
588 struct rxbuf *rxbuf;
589
590 l->rx.rxbufs = calloc(global.nbthread, sizeof *l->rx.rxbufs);
591 if (!l->rx.rxbufs)
592 return 0;
593
594 MT_LIST_INIT(&l->rx.rxbuf_list);
595 for (i = 0; i < global.nbthread; i++) {
596 char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100597 struct rxbuf *rxbuf;
598
599 rxbuf = calloc(1, sizeof *rxbuf);
600 if (!rxbuf)
601 goto err;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200602
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200603 buf = pool_alloc(pool_head_quic_rxbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100604 if (!buf) {
605 free(rxbuf);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200606 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100607 }
608
609 l->rx.rxbufs[i] = rxbuf;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200610
611 rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0);
Frédéric Lécaille53898bb2022-01-26 15:55:21 +0100612 LIST_INIT(&rxbuf->dgrams);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200613 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->mt_list);
614 }
615
616 return 1;
617
618 err:
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100619 while ((rxbuf = MT_LIST_POP(&l->rx.rxbuf_list, typeof(rxbuf), mt_list))) {
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200620 pool_free(pool_head_quic_rxbuf, rxbuf->buf.area);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100621 free(rxbuf);
622 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200623 free(l->rx.rxbufs);
624 return 0;
625}
626
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100627/* This function tries to bind a QUIC4/6 listener. It may return a warning or
628 * an error message in <errmsg> if the message is at most <errlen> bytes long
629 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
630 * The return value is composed from ERR_ABORT, ERR_WARN,
631 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
632 * was alright and that no message was returned. ERR_RETRYABLE means that an
633 * error occurred but that it may vanish after a retry (eg: port in use), and
634 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
635 * the meaning of the error, but just indicate that a message is present which
636 * should be displayed with the respective level. Last, ERR_ABORT indicates
637 * that it's pointless to try to start other listeners. No error message is
638 * returned if errlen is NULL.
639 */
640static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen)
641{
642 int err = ERR_NONE;
643 char *msg = NULL;
644
645 /* ensure we never return garbage */
646 if (errlen)
647 *errmsg = 0;
648
649 if (listener->state != LI_ASSIGNED)
650 return ERR_NONE; /* already bound */
651
652 if (!(listener->rx.flags & RX_F_BOUND)) {
653 msg = "receiving socket not bound";
654 goto udp_return;
655 }
656
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200657 if (!quic_alloc_tx_rings_listener(listener) ||
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100658 !quic_alloc_rxbufs_listener(listener)) {
659 msg = "could not initialize tx/rx rings";
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200660 err |= ERR_WARN;
661 goto udp_return;
662 }
663
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100664 listener_set_state(listener, LI_LISTEN);
665
666 udp_return:
667 if (msg && errlen) {
668 char pn[INET6_ADDRSTRLEN];
669
670 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200671 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 +0100672 }
673 return err;
674}
675
676/* Enable receipt of incoming connections for listener <l>. The receiver must
677 * still be valid. Does nothing in early boot (needs fd_updt).
678 */
679static void quic_enable_listener(struct listener *l)
680{
681 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500682 * is the responsibility of the QUIC xprt to stop accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100683 * connections.
684 */
685 if (fd_updt)
686 fd_want_recv(l->rx.fd);
687}
688
689/* Disable receipt of incoming connections for listener <l>. The receiver must
690 * still be valid. Does nothing in early boot (needs fd_updt).
691 */
692static void quic_disable_listener(struct listener *l)
693{
694 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500695 * is the responsibility of the QUIC xprt to start accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100696 * connections again.
697 */
698 if (fd_updt)
699 fd_stop_recv(l->rx.fd);
700}
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100701
702static int quic_alloc_dghdlrs(void)
703{
704 int i;
705
Tim Duesterhus9fb57e82022-06-01 21:58:37 +0200706 quic_dghdlrs = calloc(global.nbthread, sizeof(*quic_dghdlrs));
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100707 if (!quic_dghdlrs) {
708 ha_alert("Failed to allocate the quic datagram handlers.\n");
709 return 0;
710 }
711
712 for (i = 0; i < global.nbthread; i++) {
713 struct quic_dghdlr *dghdlr = &quic_dghdlrs[i];
714
715 dghdlr->task = tasklet_new();
716 if (!dghdlr->task) {
717 ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i);
718 return 0;
719 }
720
721 tasklet_set_tid(dghdlr->task, i);
722 dghdlr->task->context = dghdlr;
723 dghdlr->task->process = quic_lstnr_dghdlr;
724
725 dghdlr->odcids = EB_ROOT_UNIQUE;
726 dghdlr->cids = EB_ROOT_UNIQUE;
727
728 MT_LIST_INIT(&dghdlr->dgrams);
729 }
730
731 return 1;
732}
733REGISTER_POST_CHECK(quic_alloc_dghdlrs);
734
735static int quic_deallocate_dghdlrs(void)
736{
737 int i;
738
739 if (quic_dghdlrs) {
740 for (i = 0; i < global.nbthread; ++i)
741 tasklet_free(quic_dghdlrs[i].task);
742 free(quic_dghdlrs);
743 }
744
745 return 1;
746}
747REGISTER_POST_DEINIT(quic_deallocate_dghdlrs);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100748
749/*
750 * Local variables:
751 * c-indent-level: 8
752 * c-basic-offset: 8
753 * End:
754 */