blob: 8c263772dc142429df04024c0143807f91b4040c [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écailleca42b2c2020-11-02 14:27:08 +010065static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen);
66static int quic_connect_server(struct connection *conn, int flags);
67static void quic_enable_listener(struct listener *listener);
68static void quic_disable_listener(struct listener *listener);
Amaury Denoyelle1acbbca2023-04-05 18:17:51 +020069static int quic_set_affinity(struct connection *conn, int new_tid);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010070
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,
Amaury Denoyelle1acbbca2023-04-05 18:17:51 +020080 .add = default_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,
Amaury Denoyelle1acbbca2023-04-05 18:17:51 +020088 .set_affinity = quic_set_affinity,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010089
90 /* binding layer */
91 .rx_suspend = udp_suspend_receiver,
92 .rx_resume = udp_resume_receiver,
93
94 /* address family */
95 .fam = &proto_fam_inet4,
96
97 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +020098 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +010099 .sock_type = SOCK_DGRAM,
100 .sock_prot = IPPROTO_UDP,
101 .rx_enable = sock_enable,
102 .rx_disable = sock_disable,
103 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100104 .rx_listening = quic_sock_accepting_conn,
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200105 .default_iocb = quic_lstnr_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100106 .receivers = LIST_HEAD_INIT(proto_quic4.receivers),
107 .nb_receivers = 0,
108};
109
110INITCALL1(STG_REGISTER, protocol_register, &proto_quic4);
111
112/* Note: must not be declared <const> as its list will be overwritten */
113struct protocol proto_quic6 = {
114 .name = "quic6",
115
116 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +0200117 .xprt_type = PROTO_TYPE_STREAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100118 .listen = quic_bind_listener,
119 .enable = quic_enable_listener,
120 .disable = quic_disable_listener,
Amaury Denoyelle1acbbca2023-04-05 18:17:51 +0200121 .add = default_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100122 .unbind = default_unbind_listener,
123 .suspend = default_suspend_listener,
124 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100125 .accept_conn = quic_sock_accept_conn,
Willy Tarreaucdf7c8e2022-04-11 16:20:00 +0200126 .get_src = quic_sock_get_src,
127 .get_dst = quic_sock_get_dst,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100128 .connect = quic_connect_server,
Amaury Denoyelle1acbbca2023-04-05 18:17:51 +0200129 .set_affinity = quic_set_affinity,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100130
131 /* binding layer */
132 .rx_suspend = udp_suspend_receiver,
133 .rx_resume = udp_resume_receiver,
134
135 /* address family */
136 .fam = &proto_fam_inet6,
137
138 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200139 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100140 .sock_type = SOCK_DGRAM,
141 .sock_prot = IPPROTO_UDP,
142 .rx_enable = sock_enable,
143 .rx_disable = sock_disable,
144 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100145 .rx_listening = quic_sock_accepting_conn,
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200146 .default_iocb = quic_lstnr_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100147 .receivers = LIST_HEAD_INIT(proto_quic6.receivers),
148 .nb_receivers = 0,
149};
150
151INITCALL1(STG_REGISTER, protocol_register, &proto_quic6);
152
153/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
154 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
155 * - 0 : ignore remote address (may even be a NULL pointer)
156 * - 1 : use provided address
157 * - 2 : use provided port
158 * - 3 : use both
159 *
160 * The function supports multiple foreign binding methods :
161 * - linux_tproxy: we directly bind to the foreign address
162 * The second one can be used as a fallback for the first one.
163 * This function returns 0 when everything's OK, 1 if it could not bind, to the
164 * local address, 2 if it could not bind to the foreign address.
165 */
166int quic_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
167{
168 struct sockaddr_storage bind_addr;
169 int foreign_ok = 0;
170 int ret;
171 static THREAD_LOCAL int ip_transp_working = 1;
172 static THREAD_LOCAL int ip6_transp_working = 1;
173
174 switch (local->ss_family) {
175 case AF_INET:
176 if (flags && ip_transp_working) {
177 /* This deserves some explanation. Some platforms will support
178 * multiple combinations of certain methods, so we try the
179 * supported ones until one succeeds.
180 */
181 if (sock_inet4_make_foreign(fd))
182 foreign_ok = 1;
183 else
184 ip_transp_working = 0;
185 }
186 break;
187 case AF_INET6:
188 if (flags && ip6_transp_working) {
189 if (sock_inet6_make_foreign(fd))
190 foreign_ok = 1;
191 else
192 ip6_transp_working = 0;
193 }
194 break;
195 }
196
197 if (flags) {
198 memset(&bind_addr, 0, sizeof(bind_addr));
199 bind_addr.ss_family = remote->ss_family;
200 switch (remote->ss_family) {
201 case AF_INET:
202 if (flags & 1)
203 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
204 if (flags & 2)
205 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
206 break;
207 case AF_INET6:
208 if (flags & 1)
209 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
210 if (flags & 2)
211 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
212 break;
213 default:
214 /* we don't want to try to bind to an unknown address family */
215 foreign_ok = 0;
216 }
217 }
218
219 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
220 if (foreign_ok) {
221 if (is_inet_addr(&bind_addr)) {
222 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
223 if (ret < 0)
224 return 2;
225 }
226 }
227 else {
228 if (is_inet_addr(local)) {
229 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
230 if (ret < 0)
231 return 1;
232 }
233 }
234
235 if (!flags)
236 return 0;
237
238 if (!foreign_ok)
239 /* we could not bind to a foreign address */
240 return 2;
241
242 return 0;
243}
244
245/*
246 * This function initiates a QUIC connection establishment to the target assigned
247 * to connection <conn> using (si->{target,dst}). A source address may be
248 * pointed to by conn->src in case of transparent proxying. Normal source
249 * bind addresses are still determined locally (due to the possible need of a
250 * source port). conn->target may point either to a valid server or to a backend,
251 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
252 * supported. The <data> parameter is a boolean indicating whether there are data
253 * waiting for being sent or not, in order to adjust data write polling and on
254 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
255 * is not used.
256 *
257 * Note that a pending send_proxy message accounts for data.
258 *
259 * It can return one of :
260 * - SF_ERR_NONE if everything's OK
261 * - SF_ERR_SRVTO if there are no more servers
262 * - SF_ERR_SRVCL if the connection was refused by the server
263 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
264 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
265 * - SF_ERR_INTERNAL for any other purely internal errors
266 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
267 *
268 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
269 * it's invalid and the caller has nothing to do.
270 */
271
272int quic_connect_server(struct connection *conn, int flags)
273{
274 int fd;
275 struct server *srv;
276 struct proxy *be;
277 struct conn_src *src;
278 struct sockaddr_storage *addr;
279
Willy Tarreau158b6cf2022-05-02 17:45:12 +0200280 BUG_ON(!conn->dst);
281
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100282 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
283
284 switch (obj_type(conn->target)) {
285 case OBJ_TYPE_PROXY:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100286 be = __objt_proxy(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100287 srv = NULL;
288 break;
289 case OBJ_TYPE_SERVER:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100290 srv = __objt_server(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100291 be = srv->proxy;
292 break;
293 default:
294 conn->flags |= CO_FL_ERROR;
295 return SF_ERR_INTERNAL;
296 }
297
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100298 fd = conn->handle.fd = sock_create_server_socket(conn);
299
300 if (fd == -1) {
301 qfprintf(stderr, "Cannot get a server socket.\n");
302
303 if (errno == ENFILE) {
304 conn->err_code = CO_ER_SYS_FDLIM;
305 send_log(be, LOG_EMERG,
306 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
307 be->id, global.maxsock);
308 }
309 else if (errno == EMFILE) {
310 conn->err_code = CO_ER_PROC_FDLIM;
311 send_log(be, LOG_EMERG,
312 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
313 be->id, global.maxsock);
314 }
315 else if (errno == ENOBUFS || errno == ENOMEM) {
316 conn->err_code = CO_ER_SYS_MEMLIM;
317 send_log(be, LOG_EMERG,
318 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
319 be->id, global.maxsock);
320 }
321 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
322 conn->err_code = CO_ER_NOPROTO;
323 }
324 else
325 conn->err_code = CO_ER_SOCK_ERR;
326
327 /* this is a resource error */
328 conn->flags |= CO_FL_ERROR;
329 return SF_ERR_RESOURCE;
330 }
331
332 if (fd >= global.maxsock) {
333 /* do not log anything there, it's a normal condition when this option
334 * is used to serialize connections to a server !
335 */
336 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
337 close(fd);
338 conn->err_code = CO_ER_CONF_FDLIM;
339 conn->flags |= CO_FL_ERROR;
340 return SF_ERR_PRXCOND; /* it is a configuration limit */
341 }
342
Willy Tarreau38247432022-04-26 10:24:14 +0200343 if (fd_set_nonblock(fd) == -1) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100344 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
345 close(fd);
346 conn->err_code = CO_ER_SOCK_ERR;
347 conn->flags |= CO_FL_ERROR;
348 return SF_ERR_INTERNAL;
349 }
350
Willy Tarreau38247432022-04-26 10:24:14 +0200351 if (master == 1 && fd_set_cloexec(fd) == -1) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100352 ha_alert("Cannot set CLOEXEC on client socket.\n");
353 close(fd);
354 conn->err_code = CO_ER_SOCK_ERR;
355 conn->flags |= CO_FL_ERROR;
356 return SF_ERR_INTERNAL;
357 }
358
359 /* allow specific binding :
360 * - server-specific at first
361 * - proxy-specific next
362 */
363 if (srv && srv->conn_src.opts & CO_SRC_BIND)
364 src = &srv->conn_src;
365 else if (be->conn_src.opts & CO_SRC_BIND)
366 src = &be->conn_src;
367 else
368 src = NULL;
369
370 if (src) {
371 int ret, flags = 0;
372
373 if (conn->src && is_inet_addr(conn->src)) {
374 switch (src->opts & CO_SRC_TPROXY_MASK) {
375 case CO_SRC_TPROXY_CLI:
376 conn_set_private(conn);
Willy Tarreau5c8b52f2022-11-14 07:02:00 +0100377 __fallthrough;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100378 case CO_SRC_TPROXY_ADDR:
379 flags = 3;
380 break;
381 case CO_SRC_TPROXY_CIP:
382 case CO_SRC_TPROXY_DYN:
383 conn_set_private(conn);
384 flags = 1;
385 break;
386 }
387 }
388
389#ifdef SO_BINDTODEVICE
390 /* Note: this might fail if not CAP_NET_RAW */
391 if (src->iface_name)
392 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
393#endif
394
395 if (src->sport_range) {
396 int attempts = 10; /* should be more than enough to find a spare port */
397 struct sockaddr_storage sa;
398
399 ret = 1;
400 memcpy(&sa, &src->source_addr, sizeof(sa));
401
402 do {
403 /* note: in case of retry, we may have to release a previously
404 * allocated port, hence this loop's construct.
405 */
406 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
407 fdinfo[fd].port_range = NULL;
408
409 if (!attempts)
410 break;
411 attempts--;
412
413 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
414 if (!fdinfo[fd].local_port) {
415 conn->err_code = CO_ER_PORT_RANGE;
416 break;
417 }
418
419 fdinfo[fd].port_range = src->sport_range;
420 set_host_port(&sa, fdinfo[fd].local_port);
421
422 ret = quic_bind_socket(fd, flags, &sa, conn->src);
423 if (ret != 0)
424 conn->err_code = CO_ER_CANT_BIND;
425 } while (ret != 0); /* binding NOK */
426 }
427 else {
428#ifdef IP_BIND_ADDRESS_NO_PORT
429 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200430 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 +0100431#endif
432 ret = quic_bind_socket(fd, flags, &src->source_addr, conn->src);
433 if (ret != 0)
434 conn->err_code = CO_ER_CANT_BIND;
435 }
436
437 if (unlikely(ret != 0)) {
438 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
439 fdinfo[fd].port_range = NULL;
440 close(fd);
441
442 if (ret == 1) {
443 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
444 be->id);
445 send_log(be, LOG_EMERG,
446 "Cannot bind to source address before connect() for backend %s.\n",
447 be->id);
448 } else {
449 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
450 be->id);
451 send_log(be, LOG_EMERG,
452 "Cannot bind to tproxy source address before connect() for backend %s.\n",
453 be->id);
454 }
455 conn->flags |= CO_FL_ERROR;
456 return SF_ERR_RESOURCE;
457 }
458 }
459
460 if (global.tune.server_sndbuf)
461 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
462
463 if (global.tune.server_rcvbuf)
464 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
465
466 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
467 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
468 if (errno == EINPROGRESS || errno == EALREADY) {
469 /* common case, let's wait for connect status */
470 conn->flags |= CO_FL_WAIT_L4_CONN;
471 }
472 else if (errno == EISCONN) {
473 /* should normally not happen but if so, indicates that it's OK */
474 conn->flags &= ~CO_FL_WAIT_L4_CONN;
475 }
Willy Tarreauacef5e22022-04-25 20:32:15 +0200476 else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100477 char *msg;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200478 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRNOTAVAIL) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100479 msg = "no free ports";
480 conn->err_code = CO_ER_FREE_PORTS;
481 }
482 else {
483 msg = "local address already in use";
484 conn->err_code = CO_ER_ADDR_INUSE;
485 }
486
487 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
488 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
489 fdinfo[fd].port_range = NULL;
490 close(fd);
491 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
492 conn->flags |= CO_FL_ERROR;
493 return SF_ERR_RESOURCE;
494 } else if (errno == ETIMEDOUT) {
495 //qfprintf(stderr,"Connect(): ETIMEDOUT");
496 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
497 fdinfo[fd].port_range = NULL;
498 close(fd);
499 conn->err_code = CO_ER_SOCK_ERR;
500 conn->flags |= CO_FL_ERROR;
501 return SF_ERR_SRVTO;
502 } else {
503 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
504 //qfprintf(stderr,"Connect(): %d", errno);
505 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
506 fdinfo[fd].port_range = NULL;
507 close(fd);
508 conn->err_code = CO_ER_SOCK_ERR;
509 conn->flags |= CO_FL_ERROR;
510 return SF_ERR_SRVCL;
511 }
512 }
513 else {
514 /* connect() == 0, this is great! */
515 conn->flags &= ~CO_FL_WAIT_L4_CONN;
516 }
517
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100518 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200519 HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100520
521 if (conn->flags & CO_FL_WAIT_L4_CONN) {
522 fd_want_send(fd);
523 fd_cant_send(fd);
524 fd_cant_recv(fd);
525 }
526
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100527 return SF_ERR_NONE; /* connection is OK */
528}
529
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200530/* Allocate the RX buffers for <l> listener.
531 * Return 1 if succeeded, 0 if not.
532 */
533static int quic_alloc_rxbufs_listener(struct listener *l)
534{
535 int i;
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200536 struct quic_receiver_buf *tmp;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200537
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200538 MT_LIST_INIT(&l->rx.rxbuf_list);
Willy Tarreau8d492532022-12-21 09:16:55 +0100539 for (i = 0; i < my_popcountl(l->rx.bind_thread); i++) {
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200540 struct quic_receiver_buf *rxbuf;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200541 char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100542
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200543 rxbuf = calloc(1, sizeof(*rxbuf));
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100544 if (!rxbuf)
545 goto err;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200546
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200547 buf = pool_alloc(pool_head_quic_rxbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100548 if (!buf) {
549 free(rxbuf);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200550 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100551 }
552
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200553 rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0);
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200554 LIST_INIT(&rxbuf->dgram_list);
555 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->rxbuf_el);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200556 }
557
558 return 1;
559
560 err:
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200561 while ((tmp = MT_LIST_POP(&l->rx.rxbuf_list, typeof(tmp), rxbuf_el))) {
562 pool_free(pool_head_quic_rxbuf, tmp->buf.area);
563 free(tmp);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100564 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200565 return 0;
566}
567
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100568/* Check if platform supports the required feature set for quic-conn owned
569 * socket. <l> listener must already be binded; a dummy socket will be opened
570 * on the same address as one of the support test.
571 *
572 * Returns true if platform is deemed compatible else false.
573 */
574static int quic_test_sock_per_conn_support(struct listener *l)
575{
576 const struct receiver *rx = &l->rx;
577 int ret = 1, fdtest;
578
Amaury Denoyelle8d46acd2022-12-01 14:51:16 +0100579 /* Check if IP destination address can be retrieved on recvfrom()
580 * operation.
581 */
582#if !defined(IP_PKTINFO) && !defined(IP_RECVDSTADDR)
583 ha_alert("Your platform does not seem to support UDP source address retrieval through IP_PKTINFO or an alternative flag. "
584 "QUIC connections will use listener socket.\n");
585 ret = 0;
586#endif
587
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100588 /* Check if platform support multiple UDP sockets bind on the same
589 * local address. Create a dummy socket and bind it on the same address
590 * as <l> listener. If bind system call fails, deactivate socket per
591 * connection. All other errors are not taken into account.
592 */
593 if (ret) {
594 fdtest = socket(rx->proto->fam->sock_domain,
595 rx->proto->sock_type, rx->proto->sock_prot);
Amaury Denoyelle30fc2772022-12-05 10:24:54 +0100596 if (fdtest >= 0) {
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100597 if (setsockopt(fdtest, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) &&
598 bind(fdtest, (struct sockaddr *)&rx->addr, rx->proto->fam->sock_addrlen) < 0) {
599 ha_alert("Your platform does not seem to support multiple UDP sockets binded on the same address. "
600 "QUIC connections will use listener socket.\n");
601 ret = 0;
602 }
603
604 close(fdtest);
605 }
606 }
607
608 return ret;
609}
610
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100611/* This function tries to bind a QUIC4/6 listener. It may return a warning or
612 * an error message in <errmsg> if the message is at most <errlen> bytes long
613 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
614 * The return value is composed from ERR_ABORT, ERR_WARN,
615 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
616 * was alright and that no message was returned. ERR_RETRYABLE means that an
617 * error occurred but that it may vanish after a retry (eg: port in use), and
618 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
619 * the meaning of the error, but just indicate that a message is present which
620 * should be displayed with the respective level. Last, ERR_ABORT indicates
621 * that it's pointless to try to start other listeners. No error message is
622 * returned if errlen is NULL.
623 */
624static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen)
625{
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200626 const struct sockaddr_storage addr = listener->rx.addr;
627 int fd, err = ERR_NONE;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100628 char *msg = NULL;
629
630 /* ensure we never return garbage */
631 if (errlen)
632 *errmsg = 0;
633
634 if (listener->state != LI_ASSIGNED)
635 return ERR_NONE; /* already bound */
636
637 if (!(listener->rx.flags & RX_F_BOUND)) {
638 msg = "receiving socket not bound";
639 goto udp_return;
640 }
641
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200642 /* Set IP_PKTINFO to retrieve destination address on recv. */
643 fd = listener->rx.fd;
644 switch (addr.ss_family) {
645 case AF_INET:
646#if defined(IP_PKTINFO)
647 setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
648#elif defined(IP_RECVDSTADDR)
649 setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &one, sizeof(one));
650#endif /* IP_PKTINFO || IP_RECVDSTADDR */
651 break;
652 case AF_INET6:
653#ifdef IPV6_RECVPKTINFO
654 setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
655#endif
656 break;
657 default:
658 break;
659 }
660
Willy Tarreaucab054b2022-10-11 08:36:21 +0200661 if (!quic_alloc_rxbufs_listener(listener)) {
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100662 msg = "could not initialize tx/rx rings";
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200663 err |= ERR_WARN;
664 goto udp_return;
665 }
666
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100667 if (global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) {
668 if (!quic_test_sock_per_conn_support(listener))
669 global.tune.options &= ~GTUNE_QUIC_SOCK_PER_CONN;
670 }
671
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100672 listener_set_state(listener, LI_LISTEN);
673
674 udp_return:
675 if (msg && errlen) {
676 char pn[INET6_ADDRSTRLEN];
677
678 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200679 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 +0100680 }
681 return err;
682}
683
684/* Enable receipt of incoming connections for listener <l>. The receiver must
685 * still be valid. Does nothing in early boot (needs fd_updt).
686 */
687static void quic_enable_listener(struct listener *l)
688{
689 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500690 * is the responsibility of the QUIC xprt to stop accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100691 * connections.
692 */
693 if (fd_updt)
694 fd_want_recv(l->rx.fd);
695}
696
697/* Disable receipt of incoming connections for listener <l>. The receiver must
698 * still be valid. Does nothing in early boot (needs fd_updt).
699 */
700static void quic_disable_listener(struct listener *l)
701{
702 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500703 * is the responsibility of the QUIC xprt to start accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100704 * connections again.
705 */
706 if (fd_updt)
707 fd_stop_recv(l->rx.fd);
708}
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100709
Amaury Denoyelle1acbbca2023-04-05 18:17:51 +0200710static int quic_set_affinity(struct connection *conn, int new_tid)
711{
712 struct quic_conn *qc = conn->handle.qc;
713 return qc_set_tid_affinity(qc, new_tid);
714}
715
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100716static int quic_alloc_dghdlrs(void)
717{
718 int i;
719
Tim Duesterhus9fb57e82022-06-01 21:58:37 +0200720 quic_dghdlrs = calloc(global.nbthread, sizeof(*quic_dghdlrs));
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100721 if (!quic_dghdlrs) {
722 ha_alert("Failed to allocate the quic datagram handlers.\n");
723 return 0;
724 }
725
726 for (i = 0; i < global.nbthread; i++) {
727 struct quic_dghdlr *dghdlr = &quic_dghdlrs[i];
728
729 dghdlr->task = tasklet_new();
730 if (!dghdlr->task) {
731 ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i);
732 return 0;
733 }
734
735 tasklet_set_tid(dghdlr->task, i);
736 dghdlr->task->context = dghdlr;
737 dghdlr->task->process = quic_lstnr_dghdlr;
738
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100739 MT_LIST_INIT(&dghdlr->dgrams);
740 }
741
Amaury Denoyellee83f9372023-04-18 11:10:54 +0200742 quic_cid_trees = calloc(QUIC_CID_TREES_CNT, sizeof(struct quic_cid_tree));
743 if (!quic_cid_trees) {
744 ha_alert("Failed to allocate global CIDs trees.\n");
745 return 0;
746 }
747
748 for (i = 0; i < QUIC_CID_TREES_CNT; ++i) {
749 HA_RWLOCK_INIT(&quic_cid_trees[i].lock);
750 quic_cid_trees[i].root = EB_ROOT_UNIQUE;
751 }
752
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100753 return 1;
754}
755REGISTER_POST_CHECK(quic_alloc_dghdlrs);
756
757static int quic_deallocate_dghdlrs(void)
758{
759 int i;
760
761 if (quic_dghdlrs) {
762 for (i = 0; i < global.nbthread; ++i)
763 tasklet_free(quic_dghdlrs[i].task);
764 free(quic_dghdlrs);
765 }
766
Amaury Denoyellee83f9372023-04-18 11:10:54 +0200767 ha_free(&quic_cid_trees);
768
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100769 return 1;
770}
771REGISTER_POST_DEINIT(quic_deallocate_dghdlrs);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100772
773/*
774 * Local variables:
775 * c-indent-level: 8
776 * c-basic-offset: 8
777 * End:
778 */