blob: b1708eb9f2a4b31c4dc2e89ff15fcc42b451b233 [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 Denoyelle683b5fc2022-01-26 11:56:48 +0100529 listener->rx.flags |= RX_F_LOCAL_ACCEPT;
Amaury Denoyelle2ce99fe2022-01-19 15:46:11 +0100530
Frédéric Lécaille884f2e92020-11-23 14:23:21 +0100531 default_add_listener(proto, listener);
532}
533
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200534/* Allocate the RX buffers for <l> listener.
535 * Return 1 if succeeded, 0 if not.
536 */
537static int quic_alloc_rxbufs_listener(struct listener *l)
538{
539 int i;
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200540 struct quic_receiver_buf *tmp;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200541
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200542 MT_LIST_INIT(&l->rx.rxbuf_list);
Willy Tarreau8d492532022-12-21 09:16:55 +0100543 for (i = 0; i < my_popcountl(l->rx.bind_thread); i++) {
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200544 struct quic_receiver_buf *rxbuf;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200545 char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100546
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200547 rxbuf = calloc(1, sizeof(*rxbuf));
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100548 if (!rxbuf)
549 goto err;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200550
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200551 buf = pool_alloc(pool_head_quic_rxbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100552 if (!buf) {
553 free(rxbuf);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200554 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100555 }
556
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200557 rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0);
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200558 LIST_INIT(&rxbuf->dgram_list);
559 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->rxbuf_el);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200560 }
561
562 return 1;
563
564 err:
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200565 while ((tmp = MT_LIST_POP(&l->rx.rxbuf_list, typeof(tmp), rxbuf_el))) {
566 pool_free(pool_head_quic_rxbuf, tmp->buf.area);
567 free(tmp);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100568 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200569 return 0;
570}
571
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100572/* Check if platform supports the required feature set for quic-conn owned
573 * socket. <l> listener must already be binded; a dummy socket will be opened
574 * on the same address as one of the support test.
575 *
576 * Returns true if platform is deemed compatible else false.
577 */
578static int quic_test_sock_per_conn_support(struct listener *l)
579{
580 const struct receiver *rx = &l->rx;
581 int ret = 1, fdtest;
582
Amaury Denoyelle8d46acd2022-12-01 14:51:16 +0100583 /* Check if IP destination address can be retrieved on recvfrom()
584 * operation.
585 */
586#if !defined(IP_PKTINFO) && !defined(IP_RECVDSTADDR)
587 ha_alert("Your platform does not seem to support UDP source address retrieval through IP_PKTINFO or an alternative flag. "
588 "QUIC connections will use listener socket.\n");
589 ret = 0;
590#endif
591
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100592 /* Check if platform support multiple UDP sockets bind on the same
593 * local address. Create a dummy socket and bind it on the same address
594 * as <l> listener. If bind system call fails, deactivate socket per
595 * connection. All other errors are not taken into account.
596 */
597 if (ret) {
598 fdtest = socket(rx->proto->fam->sock_domain,
599 rx->proto->sock_type, rx->proto->sock_prot);
Amaury Denoyelle30fc2772022-12-05 10:24:54 +0100600 if (fdtest >= 0) {
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100601 if (setsockopt(fdtest, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) &&
602 bind(fdtest, (struct sockaddr *)&rx->addr, rx->proto->fam->sock_addrlen) < 0) {
603 ha_alert("Your platform does not seem to support multiple UDP sockets binded on the same address. "
604 "QUIC connections will use listener socket.\n");
605 ret = 0;
606 }
607
608 close(fdtest);
609 }
610 }
611
612 return ret;
613}
614
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100615/* This function tries to bind a QUIC4/6 listener. It may return a warning or
616 * an error message in <errmsg> if the message is at most <errlen> bytes long
617 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
618 * The return value is composed from ERR_ABORT, ERR_WARN,
619 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
620 * was alright and that no message was returned. ERR_RETRYABLE means that an
621 * error occurred but that it may vanish after a retry (eg: port in use), and
622 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
623 * the meaning of the error, but just indicate that a message is present which
624 * should be displayed with the respective level. Last, ERR_ABORT indicates
625 * that it's pointless to try to start other listeners. No error message is
626 * returned if errlen is NULL.
627 */
628static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen)
629{
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200630 const struct sockaddr_storage addr = listener->rx.addr;
631 int fd, err = ERR_NONE;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100632 char *msg = NULL;
633
634 /* ensure we never return garbage */
635 if (errlen)
636 *errmsg = 0;
637
638 if (listener->state != LI_ASSIGNED)
639 return ERR_NONE; /* already bound */
640
641 if (!(listener->rx.flags & RX_F_BOUND)) {
642 msg = "receiving socket not bound";
643 goto udp_return;
644 }
645
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200646 /* Set IP_PKTINFO to retrieve destination address on recv. */
647 fd = listener->rx.fd;
648 switch (addr.ss_family) {
649 case AF_INET:
650#if defined(IP_PKTINFO)
651 setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
652#elif defined(IP_RECVDSTADDR)
653 setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &one, sizeof(one));
654#endif /* IP_PKTINFO || IP_RECVDSTADDR */
655 break;
656 case AF_INET6:
657#ifdef IPV6_RECVPKTINFO
658 setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
659#endif
660 break;
661 default:
662 break;
663 }
664
Willy Tarreaucab054b2022-10-11 08:36:21 +0200665 if (!quic_alloc_rxbufs_listener(listener)) {
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100666 msg = "could not initialize tx/rx rings";
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200667 err |= ERR_WARN;
668 goto udp_return;
669 }
670
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100671 if (global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) {
672 if (!quic_test_sock_per_conn_support(listener))
673 global.tune.options &= ~GTUNE_QUIC_SOCK_PER_CONN;
674 }
675
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100676 listener_set_state(listener, LI_LISTEN);
677
678 udp_return:
679 if (msg && errlen) {
680 char pn[INET6_ADDRSTRLEN];
681
682 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200683 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 +0100684 }
685 return err;
686}
687
688/* Enable receipt of incoming connections for listener <l>. The receiver must
689 * still be valid. Does nothing in early boot (needs fd_updt).
690 */
691static void quic_enable_listener(struct listener *l)
692{
693 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500694 * is the responsibility of the QUIC xprt to stop accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100695 * connections.
696 */
697 if (fd_updt)
698 fd_want_recv(l->rx.fd);
699}
700
701/* Disable receipt of incoming connections for listener <l>. The receiver must
702 * still be valid. Does nothing in early boot (needs fd_updt).
703 */
704static void quic_disable_listener(struct listener *l)
705{
706 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500707 * is the responsibility of the QUIC xprt to start accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100708 * connections again.
709 */
710 if (fd_updt)
711 fd_stop_recv(l->rx.fd);
712}
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100713
714static int quic_alloc_dghdlrs(void)
715{
716 int i;
717
Tim Duesterhus9fb57e82022-06-01 21:58:37 +0200718 quic_dghdlrs = calloc(global.nbthread, sizeof(*quic_dghdlrs));
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100719 if (!quic_dghdlrs) {
720 ha_alert("Failed to allocate the quic datagram handlers.\n");
721 return 0;
722 }
723
724 for (i = 0; i < global.nbthread; i++) {
725 struct quic_dghdlr *dghdlr = &quic_dghdlrs[i];
726
727 dghdlr->task = tasklet_new();
728 if (!dghdlr->task) {
729 ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i);
730 return 0;
731 }
732
733 tasklet_set_tid(dghdlr->task, i);
734 dghdlr->task->context = dghdlr;
735 dghdlr->task->process = quic_lstnr_dghdlr;
736
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100737 dghdlr->cids = EB_ROOT_UNIQUE;
738
739 MT_LIST_INIT(&dghdlr->dgrams);
740 }
741
742 return 1;
743}
744REGISTER_POST_CHECK(quic_alloc_dghdlrs);
745
746static int quic_deallocate_dghdlrs(void)
747{
748 int i;
749
750 if (quic_dghdlrs) {
751 for (i = 0; i < global.nbthread; ++i)
752 tasklet_free(quic_dghdlrs[i].task);
753 free(quic_dghdlrs);
754 }
755
756 return 1;
757}
758REGISTER_POST_DEINIT(quic_deallocate_dghdlrs);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100759
760/*
761 * Local variables:
762 * c-indent-level: 8
763 * c-basic-offset: 8
764 * End:
765 */