blob: 0961fc21046eaae4a09a2cebdd7176deb671d6e5 [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;
Amaury Denoyellee83f9372023-04-18 11:10:54 +020054struct eb_root *quic_cid_tree;
55
56/* global CID trees */
57#define QUIC_CID_TREES_CNT 256
58struct quic_cid_tree *quic_cid_trees;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010059
Frédéric Lécaille71f3abb2022-02-15 16:59:48 +010060/* Size of the internal buffer of QUIC RX buffer at the fd level */
61#define QUIC_RX_BUFSZ (1UL << 18)
62
Willy Tarreaub8dec4a2022-06-23 11:02:08 +020063DECLARE_STATIC_POOL(pool_head_quic_rxbuf, "quic_rxbuf", QUIC_RX_BUFSZ);
Frédéric Lécaille71f3abb2022-02-15 16:59:48 +010064
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010065static void quic_add_listener(struct protocol *proto, struct listener *listener);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010066static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen);
67static int quic_connect_server(struct connection *conn, int flags);
68static void quic_enable_listener(struct listener *listener);
69static void quic_disable_listener(struct listener *listener);
70
71/* Note: must not be declared <const> as its list will be overwritten */
72struct protocol proto_quic4 = {
73 .name = "quic4",
74
75 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +020076 .xprt_type = PROTO_TYPE_STREAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010077 .listen = quic_bind_listener,
78 .enable = quic_enable_listener,
79 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +010080 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010081 .unbind = default_unbind_listener,
82 .suspend = default_suspend_listener,
83 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +010084 .accept_conn = quic_sock_accept_conn,
Willy Tarreaucdf7c8e2022-04-11 16:20:00 +020085 .get_src = quic_sock_get_src,
86 .get_dst = quic_sock_get_dst,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010087 .connect = quic_connect_server,
88
89 /* binding layer */
90 .rx_suspend = udp_suspend_receiver,
91 .rx_resume = udp_resume_receiver,
92
93 /* address family */
94 .fam = &proto_fam_inet4,
95
96 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020097 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010098 .sock_type = SOCK_DGRAM,
99 .sock_prot = IPPROTO_UDP,
100 .rx_enable = sock_enable,
101 .rx_disable = sock_disable,
102 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100103 .rx_listening = quic_sock_accepting_conn,
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200104 .default_iocb = quic_lstnr_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100105 .receivers = LIST_HEAD_INIT(proto_quic4.receivers),
106 .nb_receivers = 0,
107};
108
109INITCALL1(STG_REGISTER, protocol_register, &proto_quic4);
110
111/* Note: must not be declared <const> as its list will be overwritten */
112struct protocol proto_quic6 = {
113 .name = "quic6",
114
115 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +0200116 .xprt_type = PROTO_TYPE_STREAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100117 .listen = quic_bind_listener,
118 .enable = quic_enable_listener,
119 .disable = quic_disable_listener,
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100120 .add = quic_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100121 .unbind = default_unbind_listener,
122 .suspend = default_suspend_listener,
123 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100124 .accept_conn = quic_sock_accept_conn,
Willy Tarreaucdf7c8e2022-04-11 16:20:00 +0200125 .get_src = quic_sock_get_src,
126 .get_dst = quic_sock_get_dst,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100127 .connect = quic_connect_server,
128
129 /* binding layer */
130 .rx_suspend = udp_suspend_receiver,
131 .rx_resume = udp_resume_receiver,
132
133 /* address family */
134 .fam = &proto_fam_inet6,
135
136 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200137 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100138 .sock_type = SOCK_DGRAM,
139 .sock_prot = IPPROTO_UDP,
140 .rx_enable = sock_enable,
141 .rx_disable = sock_disable,
142 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100143 .rx_listening = quic_sock_accepting_conn,
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200144 .default_iocb = quic_lstnr_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100145 .receivers = LIST_HEAD_INIT(proto_quic6.receivers),
146 .nb_receivers = 0,
147};
148
149INITCALL1(STG_REGISTER, protocol_register, &proto_quic6);
150
151/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
152 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
153 * - 0 : ignore remote address (may even be a NULL pointer)
154 * - 1 : use provided address
155 * - 2 : use provided port
156 * - 3 : use both
157 *
158 * The function supports multiple foreign binding methods :
159 * - linux_tproxy: we directly bind to the foreign address
160 * The second one can be used as a fallback for the first one.
161 * This function returns 0 when everything's OK, 1 if it could not bind, to the
162 * local address, 2 if it could not bind to the foreign address.
163 */
164int quic_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
165{
166 struct sockaddr_storage bind_addr;
167 int foreign_ok = 0;
168 int ret;
169 static THREAD_LOCAL int ip_transp_working = 1;
170 static THREAD_LOCAL int ip6_transp_working = 1;
171
172 switch (local->ss_family) {
173 case AF_INET:
174 if (flags && ip_transp_working) {
175 /* This deserves some explanation. Some platforms will support
176 * multiple combinations of certain methods, so we try the
177 * supported ones until one succeeds.
178 */
179 if (sock_inet4_make_foreign(fd))
180 foreign_ok = 1;
181 else
182 ip_transp_working = 0;
183 }
184 break;
185 case AF_INET6:
186 if (flags && ip6_transp_working) {
187 if (sock_inet6_make_foreign(fd))
188 foreign_ok = 1;
189 else
190 ip6_transp_working = 0;
191 }
192 break;
193 }
194
195 if (flags) {
196 memset(&bind_addr, 0, sizeof(bind_addr));
197 bind_addr.ss_family = remote->ss_family;
198 switch (remote->ss_family) {
199 case AF_INET:
200 if (flags & 1)
201 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
202 if (flags & 2)
203 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
204 break;
205 case AF_INET6:
206 if (flags & 1)
207 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
208 if (flags & 2)
209 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
210 break;
211 default:
212 /* we don't want to try to bind to an unknown address family */
213 foreign_ok = 0;
214 }
215 }
216
217 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
218 if (foreign_ok) {
219 if (is_inet_addr(&bind_addr)) {
220 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
221 if (ret < 0)
222 return 2;
223 }
224 }
225 else {
226 if (is_inet_addr(local)) {
227 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
228 if (ret < 0)
229 return 1;
230 }
231 }
232
233 if (!flags)
234 return 0;
235
236 if (!foreign_ok)
237 /* we could not bind to a foreign address */
238 return 2;
239
240 return 0;
241}
242
243/*
244 * This function initiates a QUIC connection establishment to the target assigned
245 * to connection <conn> using (si->{target,dst}). A source address may be
246 * pointed to by conn->src in case of transparent proxying. Normal source
247 * bind addresses are still determined locally (due to the possible need of a
248 * source port). conn->target may point either to a valid server or to a backend,
249 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
250 * supported. The <data> parameter is a boolean indicating whether there are data
251 * waiting for being sent or not, in order to adjust data write polling and on
252 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
253 * is not used.
254 *
255 * Note that a pending send_proxy message accounts for data.
256 *
257 * It can return one of :
258 * - SF_ERR_NONE if everything's OK
259 * - SF_ERR_SRVTO if there are no more servers
260 * - SF_ERR_SRVCL if the connection was refused by the server
261 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
262 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
263 * - SF_ERR_INTERNAL for any other purely internal errors
264 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
265 *
266 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
267 * it's invalid and the caller has nothing to do.
268 */
269
270int quic_connect_server(struct connection *conn, int flags)
271{
272 int fd;
273 struct server *srv;
274 struct proxy *be;
275 struct conn_src *src;
276 struct sockaddr_storage *addr;
277
Willy Tarreau158b6cf2022-05-02 17:45:12 +0200278 BUG_ON(!conn->dst);
279
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100280 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
281
282 switch (obj_type(conn->target)) {
283 case OBJ_TYPE_PROXY:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100284 be = __objt_proxy(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100285 srv = NULL;
286 break;
287 case OBJ_TYPE_SERVER:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100288 srv = __objt_server(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100289 be = srv->proxy;
290 break;
291 default:
292 conn->flags |= CO_FL_ERROR;
293 return SF_ERR_INTERNAL;
294 }
295
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100296 fd = conn->handle.fd = sock_create_server_socket(conn);
297
298 if (fd == -1) {
299 qfprintf(stderr, "Cannot get a server socket.\n");
300
301 if (errno == ENFILE) {
302 conn->err_code = CO_ER_SYS_FDLIM;
303 send_log(be, LOG_EMERG,
304 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
305 be->id, global.maxsock);
306 }
307 else if (errno == EMFILE) {
308 conn->err_code = CO_ER_PROC_FDLIM;
309 send_log(be, LOG_EMERG,
310 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
311 be->id, global.maxsock);
312 }
313 else if (errno == ENOBUFS || errno == ENOMEM) {
314 conn->err_code = CO_ER_SYS_MEMLIM;
315 send_log(be, LOG_EMERG,
316 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
317 be->id, global.maxsock);
318 }
319 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
320 conn->err_code = CO_ER_NOPROTO;
321 }
322 else
323 conn->err_code = CO_ER_SOCK_ERR;
324
325 /* this is a resource error */
326 conn->flags |= CO_FL_ERROR;
327 return SF_ERR_RESOURCE;
328 }
329
330 if (fd >= global.maxsock) {
331 /* do not log anything there, it's a normal condition when this option
332 * is used to serialize connections to a server !
333 */
334 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
335 close(fd);
336 conn->err_code = CO_ER_CONF_FDLIM;
337 conn->flags |= CO_FL_ERROR;
338 return SF_ERR_PRXCOND; /* it is a configuration limit */
339 }
340
Willy Tarreau38247432022-04-26 10:24:14 +0200341 if (fd_set_nonblock(fd) == -1) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100342 qfprintf(stderr,"Cannot set client socket to non blocking mode.\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
Willy Tarreau38247432022-04-26 10:24:14 +0200349 if (master == 1 && fd_set_cloexec(fd) == -1) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100350 ha_alert("Cannot set CLOEXEC on client socket.\n");
351 close(fd);
352 conn->err_code = CO_ER_SOCK_ERR;
353 conn->flags |= CO_FL_ERROR;
354 return SF_ERR_INTERNAL;
355 }
356
357 /* allow specific binding :
358 * - server-specific at first
359 * - proxy-specific next
360 */
361 if (srv && srv->conn_src.opts & CO_SRC_BIND)
362 src = &srv->conn_src;
363 else if (be->conn_src.opts & CO_SRC_BIND)
364 src = &be->conn_src;
365 else
366 src = NULL;
367
368 if (src) {
369 int ret, flags = 0;
370
371 if (conn->src && is_inet_addr(conn->src)) {
372 switch (src->opts & CO_SRC_TPROXY_MASK) {
373 case CO_SRC_TPROXY_CLI:
374 conn_set_private(conn);
Willy Tarreau5c8b52f2022-11-14 07:02:00 +0100375 __fallthrough;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100376 case CO_SRC_TPROXY_ADDR:
377 flags = 3;
378 break;
379 case CO_SRC_TPROXY_CIP:
380 case CO_SRC_TPROXY_DYN:
381 conn_set_private(conn);
382 flags = 1;
383 break;
384 }
385 }
386
387#ifdef SO_BINDTODEVICE
388 /* Note: this might fail if not CAP_NET_RAW */
389 if (src->iface_name)
390 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
391#endif
392
393 if (src->sport_range) {
394 int attempts = 10; /* should be more than enough to find a spare port */
395 struct sockaddr_storage sa;
396
397 ret = 1;
398 memcpy(&sa, &src->source_addr, sizeof(sa));
399
400 do {
401 /* note: in case of retry, we may have to release a previously
402 * allocated port, hence this loop's construct.
403 */
404 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
405 fdinfo[fd].port_range = NULL;
406
407 if (!attempts)
408 break;
409 attempts--;
410
411 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
412 if (!fdinfo[fd].local_port) {
413 conn->err_code = CO_ER_PORT_RANGE;
414 break;
415 }
416
417 fdinfo[fd].port_range = src->sport_range;
418 set_host_port(&sa, fdinfo[fd].local_port);
419
420 ret = quic_bind_socket(fd, flags, &sa, conn->src);
421 if (ret != 0)
422 conn->err_code = CO_ER_CANT_BIND;
423 } while (ret != 0); /* binding NOK */
424 }
425 else {
426#ifdef IP_BIND_ADDRESS_NO_PORT
427 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200428 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 +0100429#endif
430 ret = quic_bind_socket(fd, flags, &src->source_addr, conn->src);
431 if (ret != 0)
432 conn->err_code = CO_ER_CANT_BIND;
433 }
434
435 if (unlikely(ret != 0)) {
436 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
437 fdinfo[fd].port_range = NULL;
438 close(fd);
439
440 if (ret == 1) {
441 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
442 be->id);
443 send_log(be, LOG_EMERG,
444 "Cannot bind to source address before connect() for backend %s.\n",
445 be->id);
446 } else {
447 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
448 be->id);
449 send_log(be, LOG_EMERG,
450 "Cannot bind to tproxy source address before connect() for backend %s.\n",
451 be->id);
452 }
453 conn->flags |= CO_FL_ERROR;
454 return SF_ERR_RESOURCE;
455 }
456 }
457
458 if (global.tune.server_sndbuf)
459 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
460
461 if (global.tune.server_rcvbuf)
462 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
463
464 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
465 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
466 if (errno == EINPROGRESS || errno == EALREADY) {
467 /* common case, let's wait for connect status */
468 conn->flags |= CO_FL_WAIT_L4_CONN;
469 }
470 else if (errno == EISCONN) {
471 /* should normally not happen but if so, indicates that it's OK */
472 conn->flags &= ~CO_FL_WAIT_L4_CONN;
473 }
Willy Tarreauacef5e22022-04-25 20:32:15 +0200474 else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100475 char *msg;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200476 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRNOTAVAIL) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100477 msg = "no free ports";
478 conn->err_code = CO_ER_FREE_PORTS;
479 }
480 else {
481 msg = "local address already in use";
482 conn->err_code = CO_ER_ADDR_INUSE;
483 }
484
485 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
486 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
487 fdinfo[fd].port_range = NULL;
488 close(fd);
489 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
490 conn->flags |= CO_FL_ERROR;
491 return SF_ERR_RESOURCE;
492 } else if (errno == ETIMEDOUT) {
493 //qfprintf(stderr,"Connect(): ETIMEDOUT");
494 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
495 fdinfo[fd].port_range = NULL;
496 close(fd);
497 conn->err_code = CO_ER_SOCK_ERR;
498 conn->flags |= CO_FL_ERROR;
499 return SF_ERR_SRVTO;
500 } else {
501 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
502 //qfprintf(stderr,"Connect(): %d", errno);
503 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
504 fdinfo[fd].port_range = NULL;
505 close(fd);
506 conn->err_code = CO_ER_SOCK_ERR;
507 conn->flags |= CO_FL_ERROR;
508 return SF_ERR_SRVCL;
509 }
510 }
511 else {
512 /* connect() == 0, this is great! */
513 conn->flags &= ~CO_FL_WAIT_L4_CONN;
514 }
515
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100516 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200517 HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100518
519 if (conn->flags & CO_FL_WAIT_L4_CONN) {
520 fd_want_send(fd);
521 fd_cant_send(fd);
522 fd_cant_recv(fd);
523 }
524
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100525 return SF_ERR_NONE; /* connection is OK */
526}
527
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100528/* Add listener <listener> to protocol <proto>. Technically speaking we just
529 * initialize a few entries which should be doable during quic_bind_listener().
530 * The end of the initialization goes on with the default function.
531 */
532static void quic_add_listener(struct protocol *proto, struct listener *listener)
533{
Amaury Denoyelle683b5fc2022-01-26 11:56:48 +0100534 listener->rx.flags |= RX_F_LOCAL_ACCEPT;
Amaury Denoyelle2ce99fe2022-01-19 15:46:11 +0100535
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100536 default_add_listener(proto, listener);
537}
538
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200539/* Allocate the RX buffers for <l> listener.
540 * Return 1 if succeeded, 0 if not.
541 */
542static int quic_alloc_rxbufs_listener(struct listener *l)
543{
544 int i;
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200545 struct quic_receiver_buf *tmp;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200546
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200547 MT_LIST_INIT(&l->rx.rxbuf_list);
Willy Tarreau8d492532022-12-21 09:16:55 +0100548 for (i = 0; i < my_popcountl(l->rx.bind_thread); i++) {
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200549 struct quic_receiver_buf *rxbuf;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200550 char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100551
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200552 rxbuf = calloc(1, sizeof(*rxbuf));
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100553 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
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200562 rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0);
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200563 LIST_INIT(&rxbuf->dgram_list);
564 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->rxbuf_el);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200565 }
566
567 return 1;
568
569 err:
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200570 while ((tmp = MT_LIST_POP(&l->rx.rxbuf_list, typeof(tmp), rxbuf_el))) {
571 pool_free(pool_head_quic_rxbuf, tmp->buf.area);
572 free(tmp);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100573 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200574 return 0;
575}
576
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100577/* Check if platform supports the required feature set for quic-conn owned
578 * socket. <l> listener must already be binded; a dummy socket will be opened
579 * on the same address as one of the support test.
580 *
581 * Returns true if platform is deemed compatible else false.
582 */
583static int quic_test_sock_per_conn_support(struct listener *l)
584{
585 const struct receiver *rx = &l->rx;
586 int ret = 1, fdtest;
587
Amaury Denoyelle8d46acd2022-12-01 14:51:16 +0100588 /* Check if IP destination address can be retrieved on recvfrom()
589 * operation.
590 */
591#if !defined(IP_PKTINFO) && !defined(IP_RECVDSTADDR)
592 ha_alert("Your platform does not seem to support UDP source address retrieval through IP_PKTINFO or an alternative flag. "
593 "QUIC connections will use listener socket.\n");
594 ret = 0;
595#endif
596
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100597 /* Check if platform support multiple UDP sockets bind on the same
598 * local address. Create a dummy socket and bind it on the same address
599 * as <l> listener. If bind system call fails, deactivate socket per
600 * connection. All other errors are not taken into account.
601 */
602 if (ret) {
603 fdtest = socket(rx->proto->fam->sock_domain,
604 rx->proto->sock_type, rx->proto->sock_prot);
Amaury Denoyelle30fc2772022-12-05 10:24:54 +0100605 if (fdtest >= 0) {
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100606 if (setsockopt(fdtest, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) &&
607 bind(fdtest, (struct sockaddr *)&rx->addr, rx->proto->fam->sock_addrlen) < 0) {
608 ha_alert("Your platform does not seem to support multiple UDP sockets binded on the same address. "
609 "QUIC connections will use listener socket.\n");
610 ret = 0;
611 }
612
613 close(fdtest);
614 }
615 }
616
617 return ret;
618}
619
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100620/* This function tries to bind a QUIC4/6 listener. It may return a warning or
621 * an error message in <errmsg> if the message is at most <errlen> bytes long
622 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
623 * The return value is composed from ERR_ABORT, ERR_WARN,
624 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
625 * was alright and that no message was returned. ERR_RETRYABLE means that an
626 * error occurred but that it may vanish after a retry (eg: port in use), and
627 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
628 * the meaning of the error, but just indicate that a message is present which
629 * should be displayed with the respective level. Last, ERR_ABORT indicates
630 * that it's pointless to try to start other listeners. No error message is
631 * returned if errlen is NULL.
632 */
633static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen)
634{
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200635 const struct sockaddr_storage addr = listener->rx.addr;
636 int fd, err = ERR_NONE;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100637 char *msg = NULL;
638
639 /* ensure we never return garbage */
640 if (errlen)
641 *errmsg = 0;
642
643 if (listener->state != LI_ASSIGNED)
644 return ERR_NONE; /* already bound */
645
646 if (!(listener->rx.flags & RX_F_BOUND)) {
647 msg = "receiving socket not bound";
648 goto udp_return;
649 }
650
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200651 /* Set IP_PKTINFO to retrieve destination address on recv. */
652 fd = listener->rx.fd;
653 switch (addr.ss_family) {
654 case AF_INET:
655#if defined(IP_PKTINFO)
656 setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
657#elif defined(IP_RECVDSTADDR)
658 setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &one, sizeof(one));
659#endif /* IP_PKTINFO || IP_RECVDSTADDR */
660 break;
661 case AF_INET6:
662#ifdef IPV6_RECVPKTINFO
663 setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
664#endif
665 break;
666 default:
667 break;
668 }
669
Willy Tarreaucab054b2022-10-11 08:36:21 +0200670 if (!quic_alloc_rxbufs_listener(listener)) {
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100671 msg = "could not initialize tx/rx rings";
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200672 err |= ERR_WARN;
673 goto udp_return;
674 }
675
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100676 if (global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) {
677 if (!quic_test_sock_per_conn_support(listener))
678 global.tune.options &= ~GTUNE_QUIC_SOCK_PER_CONN;
679 }
680
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100681 listener_set_state(listener, LI_LISTEN);
682
683 udp_return:
684 if (msg && errlen) {
685 char pn[INET6_ADDRSTRLEN];
686
687 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200688 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 +0100689 }
690 return err;
691}
692
693/* Enable receipt of incoming connections for listener <l>. The receiver must
694 * still be valid. Does nothing in early boot (needs fd_updt).
695 */
696static void quic_enable_listener(struct listener *l)
697{
698 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500699 * is the responsibility of the QUIC xprt to stop accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100700 * connections.
701 */
702 if (fd_updt)
703 fd_want_recv(l->rx.fd);
704}
705
706/* Disable receipt of incoming connections for listener <l>. The receiver must
707 * still be valid. Does nothing in early boot (needs fd_updt).
708 */
709static void quic_disable_listener(struct listener *l)
710{
711 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500712 * is the responsibility of the QUIC xprt to start accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100713 * connections again.
714 */
715 if (fd_updt)
716 fd_stop_recv(l->rx.fd);
717}
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100718
719static int quic_alloc_dghdlrs(void)
720{
721 int i;
722
Tim Duesterhus9fb57e82022-06-01 21:58:37 +0200723 quic_dghdlrs = calloc(global.nbthread, sizeof(*quic_dghdlrs));
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100724 if (!quic_dghdlrs) {
725 ha_alert("Failed to allocate the quic datagram handlers.\n");
726 return 0;
727 }
728
729 for (i = 0; i < global.nbthread; i++) {
730 struct quic_dghdlr *dghdlr = &quic_dghdlrs[i];
731
732 dghdlr->task = tasklet_new();
733 if (!dghdlr->task) {
734 ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i);
735 return 0;
736 }
737
738 tasklet_set_tid(dghdlr->task, i);
739 dghdlr->task->context = dghdlr;
740 dghdlr->task->process = quic_lstnr_dghdlr;
741
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100742 MT_LIST_INIT(&dghdlr->dgrams);
743 }
744
Amaury Denoyellee83f9372023-04-18 11:10:54 +0200745 quic_cid_trees = calloc(QUIC_CID_TREES_CNT, sizeof(struct quic_cid_tree));
746 if (!quic_cid_trees) {
747 ha_alert("Failed to allocate global CIDs trees.\n");
748 return 0;
749 }
750
751 for (i = 0; i < QUIC_CID_TREES_CNT; ++i) {
752 HA_RWLOCK_INIT(&quic_cid_trees[i].lock);
753 quic_cid_trees[i].root = EB_ROOT_UNIQUE;
754 }
755
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100756 return 1;
757}
758REGISTER_POST_CHECK(quic_alloc_dghdlrs);
759
760static int quic_deallocate_dghdlrs(void)
761{
762 int i;
763
764 if (quic_dghdlrs) {
765 for (i = 0; i < global.nbthread; ++i)
766 tasklet_free(quic_dghdlrs[i].task);
767 free(quic_dghdlrs);
768 }
769
Amaury Denoyellee83f9372023-04-18 11:10:54 +0200770 ha_free(&quic_cid_trees);
771
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100772 return 1;
773}
774REGISTER_POST_DEINIT(quic_deallocate_dghdlrs);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100775
776/*
777 * Local variables:
778 * c-indent-level: 8
779 * c-basic-offset: 8
780 * End:
781 */