blob: 57dafc3c227718524ce7c7234c8cdf5ef66dbcd1 [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,
Amaury Denoyelle5b414862022-10-24 17:40:37 +020099 .default_iocb = quic_lstnr_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,
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200139 .default_iocb = quic_lstnr_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);
Willy Tarreau5c8b52f2022-11-14 07:02:00 +0100370 __fallthrough;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100371 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;
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200541 struct quic_receiver_buf *tmp;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200542
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200543 MT_LIST_INIT(&l->rx.rxbuf_list);
544 for (i = 0; i < global.nbthread; i++) {
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200545 struct quic_receiver_buf *rxbuf;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200546 char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100547
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200548 rxbuf = calloc(1, sizeof(*rxbuf));
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100549 if (!rxbuf)
550 goto err;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200551
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200552 buf = pool_alloc(pool_head_quic_rxbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100553 if (!buf) {
554 free(rxbuf);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200555 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100556 }
557
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200558 rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0);
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200559 LIST_INIT(&rxbuf->dgram_list);
560 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->rxbuf_el);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200561 }
562
563 return 1;
564
565 err:
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200566 while ((tmp = MT_LIST_POP(&l->rx.rxbuf_list, typeof(tmp), rxbuf_el))) {
567 pool_free(pool_head_quic_rxbuf, tmp->buf.area);
568 free(tmp);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100569 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200570 return 0;
571}
572
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100573/* Check if platform supports the required feature set for quic-conn owned
574 * socket. <l> listener must already be binded; a dummy socket will be opened
575 * on the same address as one of the support test.
576 *
577 * Returns true if platform is deemed compatible else false.
578 */
579static int quic_test_sock_per_conn_support(struct listener *l)
580{
581 const struct receiver *rx = &l->rx;
582 int ret = 1, fdtest;
583
Amaury Denoyelle8d46acd2022-12-01 14:51:16 +0100584 /* Check if IP destination address can be retrieved on recvfrom()
585 * operation.
586 */
587#if !defined(IP_PKTINFO) && !defined(IP_RECVDSTADDR)
588 ha_alert("Your platform does not seem to support UDP source address retrieval through IP_PKTINFO or an alternative flag. "
589 "QUIC connections will use listener socket.\n");
590 ret = 0;
591#endif
592
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100593 /* Check if platform support multiple UDP sockets bind on the same
594 * local address. Create a dummy socket and bind it on the same address
595 * as <l> listener. If bind system call fails, deactivate socket per
596 * connection. All other errors are not taken into account.
597 */
598 if (ret) {
599 fdtest = socket(rx->proto->fam->sock_domain,
600 rx->proto->sock_type, rx->proto->sock_prot);
601 if (fdtest > 0) {
602 if (setsockopt(fdtest, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) &&
603 bind(fdtest, (struct sockaddr *)&rx->addr, rx->proto->fam->sock_addrlen) < 0) {
604 ha_alert("Your platform does not seem to support multiple UDP sockets binded on the same address. "
605 "QUIC connections will use listener socket.\n");
606 ret = 0;
607 }
608
609 close(fdtest);
610 }
611 }
612
613 return ret;
614}
615
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100616/* This function tries to bind a QUIC4/6 listener. It may return a warning or
617 * an error message in <errmsg> if the message is at most <errlen> bytes long
618 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
619 * The return value is composed from ERR_ABORT, ERR_WARN,
620 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
621 * was alright and that no message was returned. ERR_RETRYABLE means that an
622 * error occurred but that it may vanish after a retry (eg: port in use), and
623 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
624 * the meaning of the error, but just indicate that a message is present which
625 * should be displayed with the respective level. Last, ERR_ABORT indicates
626 * that it's pointless to try to start other listeners. No error message is
627 * returned if errlen is NULL.
628 */
629static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen)
630{
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200631 const struct sockaddr_storage addr = listener->rx.addr;
632 int fd, err = ERR_NONE;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100633 char *msg = NULL;
634
635 /* ensure we never return garbage */
636 if (errlen)
637 *errmsg = 0;
638
639 if (listener->state != LI_ASSIGNED)
640 return ERR_NONE; /* already bound */
641
642 if (!(listener->rx.flags & RX_F_BOUND)) {
643 msg = "receiving socket not bound";
644 goto udp_return;
645 }
646
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200647 /* Set IP_PKTINFO to retrieve destination address on recv. */
648 fd = listener->rx.fd;
649 switch (addr.ss_family) {
650 case AF_INET:
651#if defined(IP_PKTINFO)
652 setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
653#elif defined(IP_RECVDSTADDR)
654 setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &one, sizeof(one));
655#endif /* IP_PKTINFO || IP_RECVDSTADDR */
656 break;
657 case AF_INET6:
658#ifdef IPV6_RECVPKTINFO
659 setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
660#endif
661 break;
662 default:
663 break;
664 }
665
Willy Tarreaucab054b2022-10-11 08:36:21 +0200666 if (!quic_alloc_rxbufs_listener(listener)) {
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100667 msg = "could not initialize tx/rx rings";
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200668 err |= ERR_WARN;
669 goto udp_return;
670 }
671
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100672 if (global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) {
673 if (!quic_test_sock_per_conn_support(listener))
674 global.tune.options &= ~GTUNE_QUIC_SOCK_PER_CONN;
675 }
676
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100677 listener_set_state(listener, LI_LISTEN);
678
679 udp_return:
680 if (msg && errlen) {
681 char pn[INET6_ADDRSTRLEN];
682
683 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200684 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 +0100685 }
686 return err;
687}
688
689/* Enable 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_enable_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 stop accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100696 * connections.
697 */
698 if (fd_updt)
699 fd_want_recv(l->rx.fd);
700}
701
702/* Disable receipt of incoming connections for listener <l>. The receiver must
703 * still be valid. Does nothing in early boot (needs fd_updt).
704 */
705static void quic_disable_listener(struct listener *l)
706{
707 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500708 * is the responsibility of the QUIC xprt to start accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100709 * connections again.
710 */
711 if (fd_updt)
712 fd_stop_recv(l->rx.fd);
713}
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100714
715static int quic_alloc_dghdlrs(void)
716{
717 int i;
718
Tim Duesterhus9fb57e82022-06-01 21:58:37 +0200719 quic_dghdlrs = calloc(global.nbthread, sizeof(*quic_dghdlrs));
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100720 if (!quic_dghdlrs) {
721 ha_alert("Failed to allocate the quic datagram handlers.\n");
722 return 0;
723 }
724
725 for (i = 0; i < global.nbthread; i++) {
726 struct quic_dghdlr *dghdlr = &quic_dghdlrs[i];
727
728 dghdlr->task = tasklet_new();
729 if (!dghdlr->task) {
730 ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i);
731 return 0;
732 }
733
734 tasklet_set_tid(dghdlr->task, i);
735 dghdlr->task->context = dghdlr;
736 dghdlr->task->process = quic_lstnr_dghdlr;
737
738 dghdlr->odcids = EB_ROOT_UNIQUE;
739 dghdlr->cids = EB_ROOT_UNIQUE;
740
741 MT_LIST_INIT(&dghdlr->dgrams);
742 }
743
744 return 1;
745}
746REGISTER_POST_CHECK(quic_alloc_dghdlrs);
747
748static int quic_deallocate_dghdlrs(void)
749{
750 int i;
751
752 if (quic_dghdlrs) {
753 for (i = 0; i < global.nbthread; ++i)
754 tasklet_free(quic_dghdlrs[i].task);
755 free(quic_dghdlrs);
756 }
757
758 return 1;
759}
760REGISTER_POST_DEINIT(quic_deallocate_dghdlrs);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100761
762/*
763 * Local variables:
764 * c-indent-level: 8
765 * c-basic-offset: 8
766 * End:
767 */