blob: 22776cd83c38dae29ae9740cf664dcce444aaf14 [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,
Willy Tarreau785b89f2023-04-22 15:09:07 +0200108#ifdef SO_REUSEPORT
109 .flags = PROTO_F_REUSEPORT_SUPPORTED,
110#endif
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100111};
112
113INITCALL1(STG_REGISTER, protocol_register, &proto_quic4);
114
115/* Note: must not be declared <const> as its list will be overwritten */
116struct protocol proto_quic6 = {
117 .name = "quic6",
118
119 /* connection layer */
Willy Tarreau91b47262022-05-20 16:36:46 +0200120 .xprt_type = PROTO_TYPE_STREAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100121 .listen = quic_bind_listener,
122 .enable = quic_enable_listener,
123 .disable = quic_disable_listener,
Amaury Denoyelle1acbbca2023-04-05 18:17:51 +0200124 .add = default_add_listener,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100125 .unbind = default_unbind_listener,
126 .suspend = default_suspend_listener,
127 .resume = default_resume_listener,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100128 .accept_conn = quic_sock_accept_conn,
Willy Tarreaucdf7c8e2022-04-11 16:20:00 +0200129 .get_src = quic_sock_get_src,
130 .get_dst = quic_sock_get_dst,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100131 .connect = quic_connect_server,
Amaury Denoyelle1acbbca2023-04-05 18:17:51 +0200132 .set_affinity = quic_set_affinity,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100133
134 /* binding layer */
135 .rx_suspend = udp_suspend_receiver,
136 .rx_resume = udp_resume_receiver,
137
138 /* address family */
139 .fam = &proto_fam_inet6,
140
141 /* socket layer */
Willy Tarreau337edfd2021-10-27 17:05:36 +0200142 .proto_type = PROTO_TYPE_DGRAM,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100143 .sock_type = SOCK_DGRAM,
144 .sock_prot = IPPROTO_UDP,
145 .rx_enable = sock_enable,
146 .rx_disable = sock_disable,
147 .rx_unbind = sock_unbind,
Frédéric Lécaille70da8892020-11-06 15:49:49 +0100148 .rx_listening = quic_sock_accepting_conn,
Amaury Denoyelle5b414862022-10-24 17:40:37 +0200149 .default_iocb = quic_lstnr_sock_fd_iocb,
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100150 .receivers = LIST_HEAD_INIT(proto_quic6.receivers),
151 .nb_receivers = 0,
Willy Tarreau785b89f2023-04-22 15:09:07 +0200152#ifdef SO_REUSEPORT
153 .flags = PROTO_F_REUSEPORT_SUPPORTED,
154#endif
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100155};
156
157INITCALL1(STG_REGISTER, protocol_register, &proto_quic6);
158
159/* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which
160 * case we try to bind <remote>. <flags> is a 2-bit field consisting of :
161 * - 0 : ignore remote address (may even be a NULL pointer)
162 * - 1 : use provided address
163 * - 2 : use provided port
164 * - 3 : use both
165 *
166 * The function supports multiple foreign binding methods :
167 * - linux_tproxy: we directly bind to the foreign address
168 * The second one can be used as a fallback for the first one.
169 * This function returns 0 when everything's OK, 1 if it could not bind, to the
170 * local address, 2 if it could not bind to the foreign address.
171 */
172int quic_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote)
173{
174 struct sockaddr_storage bind_addr;
175 int foreign_ok = 0;
176 int ret;
177 static THREAD_LOCAL int ip_transp_working = 1;
178 static THREAD_LOCAL int ip6_transp_working = 1;
179
180 switch (local->ss_family) {
181 case AF_INET:
182 if (flags && ip_transp_working) {
183 /* This deserves some explanation. Some platforms will support
184 * multiple combinations of certain methods, so we try the
185 * supported ones until one succeeds.
186 */
187 if (sock_inet4_make_foreign(fd))
188 foreign_ok = 1;
189 else
190 ip_transp_working = 0;
191 }
192 break;
193 case AF_INET6:
194 if (flags && ip6_transp_working) {
195 if (sock_inet6_make_foreign(fd))
196 foreign_ok = 1;
197 else
198 ip6_transp_working = 0;
199 }
200 break;
201 }
202
203 if (flags) {
204 memset(&bind_addr, 0, sizeof(bind_addr));
205 bind_addr.ss_family = remote->ss_family;
206 switch (remote->ss_family) {
207 case AF_INET:
208 if (flags & 1)
209 ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr;
210 if (flags & 2)
211 ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port;
212 break;
213 case AF_INET6:
214 if (flags & 1)
215 ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr;
216 if (flags & 2)
217 ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port;
218 break;
219 default:
220 /* we don't want to try to bind to an unknown address family */
221 foreign_ok = 0;
222 }
223 }
224
225 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
226 if (foreign_ok) {
227 if (is_inet_addr(&bind_addr)) {
228 ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr));
229 if (ret < 0)
230 return 2;
231 }
232 }
233 else {
234 if (is_inet_addr(local)) {
235 ret = bind(fd, (struct sockaddr *)local, get_addr_len(local));
236 if (ret < 0)
237 return 1;
238 }
239 }
240
241 if (!flags)
242 return 0;
243
244 if (!foreign_ok)
245 /* we could not bind to a foreign address */
246 return 2;
247
248 return 0;
249}
250
251/*
252 * This function initiates a QUIC connection establishment to the target assigned
253 * to connection <conn> using (si->{target,dst}). A source address may be
254 * pointed to by conn->src in case of transparent proxying. Normal source
255 * bind addresses are still determined locally (due to the possible need of a
256 * source port). conn->target may point either to a valid server or to a backend,
257 * depending on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are
258 * supported. The <data> parameter is a boolean indicating whether there are data
259 * waiting for being sent or not, in order to adjust data write polling and on
260 * some platforms, the ability to avoid an empty initial ACK. The <flags> argument
261 * is not used.
262 *
263 * Note that a pending send_proxy message accounts for data.
264 *
265 * It can return one of :
266 * - SF_ERR_NONE if everything's OK
267 * - SF_ERR_SRVTO if there are no more servers
268 * - SF_ERR_SRVCL if the connection was refused by the server
269 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
270 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
271 * - SF_ERR_INTERNAL for any other purely internal errors
272 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
273 *
274 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
275 * it's invalid and the caller has nothing to do.
276 */
277
278int quic_connect_server(struct connection *conn, int flags)
279{
280 int fd;
281 struct server *srv;
282 struct proxy *be;
283 struct conn_src *src;
284 struct sockaddr_storage *addr;
285
Willy Tarreau158b6cf2022-05-02 17:45:12 +0200286 BUG_ON(!conn->dst);
287
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100288 conn->flags |= CO_FL_WAIT_L4_CONN; /* connection in progress */
289
290 switch (obj_type(conn->target)) {
291 case OBJ_TYPE_PROXY:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100292 be = __objt_proxy(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100293 srv = NULL;
294 break;
295 case OBJ_TYPE_SERVER:
Frédéric Lécaillee1c35462022-02-14 19:01:21 +0100296 srv = __objt_server(conn->target);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100297 be = srv->proxy;
298 break;
299 default:
300 conn->flags |= CO_FL_ERROR;
301 return SF_ERR_INTERNAL;
302 }
303
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100304 fd = conn->handle.fd = sock_create_server_socket(conn);
305
306 if (fd == -1) {
307 qfprintf(stderr, "Cannot get a server socket.\n");
308
309 if (errno == ENFILE) {
310 conn->err_code = CO_ER_SYS_FDLIM;
311 send_log(be, LOG_EMERG,
312 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
313 be->id, global.maxsock);
314 }
315 else if (errno == EMFILE) {
316 conn->err_code = CO_ER_PROC_FDLIM;
317 send_log(be, LOG_EMERG,
318 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
319 be->id, global.maxsock);
320 }
321 else if (errno == ENOBUFS || errno == ENOMEM) {
322 conn->err_code = CO_ER_SYS_MEMLIM;
323 send_log(be, LOG_EMERG,
324 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
325 be->id, global.maxsock);
326 }
327 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
328 conn->err_code = CO_ER_NOPROTO;
329 }
330 else
331 conn->err_code = CO_ER_SOCK_ERR;
332
333 /* this is a resource error */
334 conn->flags |= CO_FL_ERROR;
335 return SF_ERR_RESOURCE;
336 }
337
338 if (fd >= global.maxsock) {
339 /* do not log anything there, it's a normal condition when this option
340 * is used to serialize connections to a server !
341 */
342 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
343 close(fd);
344 conn->err_code = CO_ER_CONF_FDLIM;
345 conn->flags |= CO_FL_ERROR;
346 return SF_ERR_PRXCOND; /* it is a configuration limit */
347 }
348
Willy Tarreau38247432022-04-26 10:24:14 +0200349 if (fd_set_nonblock(fd) == -1) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100350 qfprintf(stderr,"Cannot set client socket to non blocking mode.\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
Willy Tarreau38247432022-04-26 10:24:14 +0200357 if (master == 1 && fd_set_cloexec(fd) == -1) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100358 ha_alert("Cannot set CLOEXEC on client socket.\n");
359 close(fd);
360 conn->err_code = CO_ER_SOCK_ERR;
361 conn->flags |= CO_FL_ERROR;
362 return SF_ERR_INTERNAL;
363 }
364
365 /* allow specific binding :
366 * - server-specific at first
367 * - proxy-specific next
368 */
369 if (srv && srv->conn_src.opts & CO_SRC_BIND)
370 src = &srv->conn_src;
371 else if (be->conn_src.opts & CO_SRC_BIND)
372 src = &be->conn_src;
373 else
374 src = NULL;
375
376 if (src) {
377 int ret, flags = 0;
378
379 if (conn->src && is_inet_addr(conn->src)) {
380 switch (src->opts & CO_SRC_TPROXY_MASK) {
381 case CO_SRC_TPROXY_CLI:
382 conn_set_private(conn);
Willy Tarreau5c8b52f2022-11-14 07:02:00 +0100383 __fallthrough;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100384 case CO_SRC_TPROXY_ADDR:
385 flags = 3;
386 break;
387 case CO_SRC_TPROXY_CIP:
388 case CO_SRC_TPROXY_DYN:
389 conn_set_private(conn);
390 flags = 1;
391 break;
392 }
393 }
394
395#ifdef SO_BINDTODEVICE
396 /* Note: this might fail if not CAP_NET_RAW */
397 if (src->iface_name)
398 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, src->iface_name, src->iface_len + 1);
399#endif
400
401 if (src->sport_range) {
402 int attempts = 10; /* should be more than enough to find a spare port */
403 struct sockaddr_storage sa;
404
405 ret = 1;
406 memcpy(&sa, &src->source_addr, sizeof(sa));
407
408 do {
409 /* note: in case of retry, we may have to release a previously
410 * allocated port, hence this loop's construct.
411 */
412 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
413 fdinfo[fd].port_range = NULL;
414
415 if (!attempts)
416 break;
417 attempts--;
418
419 fdinfo[fd].local_port = port_range_alloc_port(src->sport_range);
420 if (!fdinfo[fd].local_port) {
421 conn->err_code = CO_ER_PORT_RANGE;
422 break;
423 }
424
425 fdinfo[fd].port_range = src->sport_range;
426 set_host_port(&sa, fdinfo[fd].local_port);
427
428 ret = quic_bind_socket(fd, flags, &sa, conn->src);
429 if (ret != 0)
430 conn->err_code = CO_ER_CANT_BIND;
431 } while (ret != 0); /* binding NOK */
432 }
433 else {
434#ifdef IP_BIND_ADDRESS_NO_PORT
435 static THREAD_LOCAL int bind_address_no_port = 1;
Willy Tarreau4bfc6632021-03-31 08:45:47 +0200436 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 +0100437#endif
438 ret = quic_bind_socket(fd, flags, &src->source_addr, conn->src);
439 if (ret != 0)
440 conn->err_code = CO_ER_CANT_BIND;
441 }
442
443 if (unlikely(ret != 0)) {
444 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
445 fdinfo[fd].port_range = NULL;
446 close(fd);
447
448 if (ret == 1) {
449 ha_alert("Cannot bind to source address before connect() for backend %s. Aborting.\n",
450 be->id);
451 send_log(be, LOG_EMERG,
452 "Cannot bind to source address before connect() for backend %s.\n",
453 be->id);
454 } else {
455 ha_alert("Cannot bind to tproxy source address before connect() for backend %s. Aborting.\n",
456 be->id);
457 send_log(be, LOG_EMERG,
458 "Cannot bind to tproxy source address before connect() for backend %s.\n",
459 be->id);
460 }
461 conn->flags |= CO_FL_ERROR;
462 return SF_ERR_RESOURCE;
463 }
464 }
465
466 if (global.tune.server_sndbuf)
467 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
468
469 if (global.tune.server_rcvbuf)
470 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
471
472 addr = (conn->flags & CO_FL_SOCKS4) ? &srv->socks4_addr : conn->dst;
473 if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) {
474 if (errno == EINPROGRESS || errno == EALREADY) {
475 /* common case, let's wait for connect status */
476 conn->flags |= CO_FL_WAIT_L4_CONN;
477 }
478 else if (errno == EISCONN) {
479 /* should normally not happen but if so, indicates that it's OK */
480 conn->flags &= ~CO_FL_WAIT_L4_CONN;
481 }
Willy Tarreauacef5e22022-04-25 20:32:15 +0200482 else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100483 char *msg;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200484 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EADDRNOTAVAIL) {
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100485 msg = "no free ports";
486 conn->err_code = CO_ER_FREE_PORTS;
487 }
488 else {
489 msg = "local address already in use";
490 conn->err_code = CO_ER_ADDR_INUSE;
491 }
492
493 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
494 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
495 fdinfo[fd].port_range = NULL;
496 close(fd);
497 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
498 conn->flags |= CO_FL_ERROR;
499 return SF_ERR_RESOURCE;
500 } else if (errno == ETIMEDOUT) {
501 //qfprintf(stderr,"Connect(): ETIMEDOUT");
502 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
503 fdinfo[fd].port_range = NULL;
504 close(fd);
505 conn->err_code = CO_ER_SOCK_ERR;
506 conn->flags |= CO_FL_ERROR;
507 return SF_ERR_SRVTO;
508 } else {
509 // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
510 //qfprintf(stderr,"Connect(): %d", errno);
511 port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
512 fdinfo[fd].port_range = NULL;
513 close(fd);
514 conn->err_code = CO_ER_SOCK_ERR;
515 conn->flags |= CO_FL_ERROR;
516 return SF_ERR_SRVCL;
517 }
518 }
519 else {
520 /* connect() == 0, this is great! */
521 conn->flags &= ~CO_FL_WAIT_L4_CONN;
522 }
523
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100524 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreaub41a6e92021-04-06 17:49:19 +0200525 HA_ATOMIC_OR(&fdtab[fd].state, FD_LINGER_RISK); /* close hard if needed */
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100526
527 if (conn->flags & CO_FL_WAIT_L4_CONN) {
528 fd_want_send(fd);
529 fd_cant_send(fd);
530 fd_cant_recv(fd);
531 }
532
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100533 return SF_ERR_NONE; /* connection is OK */
534}
535
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200536/* Allocate the RX buffers for <l> listener.
537 * Return 1 if succeeded, 0 if not.
538 */
539static int quic_alloc_rxbufs_listener(struct listener *l)
540{
541 int i;
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200542 struct quic_receiver_buf *tmp;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200543
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200544 MT_LIST_INIT(&l->rx.rxbuf_list);
Willy Tarreau8d492532022-12-21 09:16:55 +0100545 for (i = 0; i < my_popcountl(l->rx.bind_thread); i++) {
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200546 struct quic_receiver_buf *rxbuf;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200547 char *buf;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100548
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200549 rxbuf = calloc(1, sizeof(*rxbuf));
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100550 if (!rxbuf)
551 goto err;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200552
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200553 buf = pool_alloc(pool_head_quic_rxbuf);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100554 if (!buf) {
555 free(rxbuf);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200556 goto err;
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100557 }
558
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200559 rxbuf->buf = b_make(buf, QUIC_RX_BUFSZ, 0, 0);
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200560 LIST_INIT(&rxbuf->dgram_list);
561 MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->rxbuf_el);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200562 }
563
564 return 1;
565
566 err:
Amaury Denoyelle1cba8d62022-10-06 15:16:22 +0200567 while ((tmp = MT_LIST_POP(&l->rx.rxbuf_list, typeof(tmp), rxbuf_el))) {
568 pool_free(pool_head_quic_rxbuf, tmp->buf.area);
569 free(tmp);
Frédéric Lécaille794d0682022-01-27 10:23:31 +0100570 }
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200571 return 0;
572}
573
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100574/* Check if platform supports the required feature set for quic-conn owned
575 * socket. <l> listener must already be binded; a dummy socket will be opened
576 * on the same address as one of the support test.
577 *
578 * Returns true if platform is deemed compatible else false.
579 */
580static int quic_test_sock_per_conn_support(struct listener *l)
581{
582 const struct receiver *rx = &l->rx;
583 int ret = 1, fdtest;
584
Amaury Denoyelle8d46acd2022-12-01 14:51:16 +0100585 /* Check if IP destination address can be retrieved on recvfrom()
586 * operation.
587 */
588#if !defined(IP_PKTINFO) && !defined(IP_RECVDSTADDR)
589 ha_alert("Your platform does not seem to support UDP source address retrieval through IP_PKTINFO or an alternative flag. "
590 "QUIC connections will use listener socket.\n");
591 ret = 0;
592#endif
593
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100594 /* Check if platform support multiple UDP sockets bind on the same
595 * local address. Create a dummy socket and bind it on the same address
596 * as <l> listener. If bind system call fails, deactivate socket per
597 * connection. All other errors are not taken into account.
598 */
599 if (ret) {
600 fdtest = socket(rx->proto->fam->sock_domain,
601 rx->proto->sock_type, rx->proto->sock_prot);
Amaury Denoyelle30fc2772022-12-05 10:24:54 +0100602 if (fdtest >= 0) {
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100603 if (setsockopt(fdtest, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) &&
604 bind(fdtest, (struct sockaddr *)&rx->addr, rx->proto->fam->sock_addrlen) < 0) {
605 ha_alert("Your platform does not seem to support multiple UDP sockets binded on the same address. "
606 "QUIC connections will use listener socket.\n");
607 ret = 0;
608 }
609
610 close(fdtest);
611 }
612 }
613
614 return ret;
615}
616
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100617/* This function tries to bind a QUIC4/6 listener. It may return a warning or
618 * an error message in <errmsg> if the message is at most <errlen> bytes long
619 * (including '\0'). Note that <errmsg> may be NULL if <errlen> is also zero.
620 * The return value is composed from ERR_ABORT, ERR_WARN,
621 * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything
622 * was alright and that no message was returned. ERR_RETRYABLE means that an
623 * error occurred but that it may vanish after a retry (eg: port in use), and
624 * ERR_FATAL indicates a non-fixable error. ERR_WARN and ERR_ALERT do not alter
625 * the meaning of the error, but just indicate that a message is present which
626 * should be displayed with the respective level. Last, ERR_ABORT indicates
627 * that it's pointless to try to start other listeners. No error message is
628 * returned if errlen is NULL.
629 */
630static int quic_bind_listener(struct listener *listener, char *errmsg, int errlen)
631{
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200632 const struct sockaddr_storage addr = listener->rx.addr;
633 int fd, err = ERR_NONE;
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100634 char *msg = NULL;
635
636 /* ensure we never return garbage */
637 if (errlen)
638 *errmsg = 0;
639
640 if (listener->state != LI_ASSIGNED)
641 return ERR_NONE; /* already bound */
642
643 if (!(listener->rx.flags & RX_F_BOUND)) {
644 msg = "receiving socket not bound";
645 goto udp_return;
646 }
647
Amaury Denoyelle487d04f2022-10-11 16:22:18 +0200648 /* Set IP_PKTINFO to retrieve destination address on recv. */
649 fd = listener->rx.fd;
650 switch (addr.ss_family) {
651 case AF_INET:
652#if defined(IP_PKTINFO)
653 setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one));
654#elif defined(IP_RECVDSTADDR)
655 setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &one, sizeof(one));
656#endif /* IP_PKTINFO || IP_RECVDSTADDR */
657 break;
658 case AF_INET6:
659#ifdef IPV6_RECVPKTINFO
660 setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
661#endif
662 break;
663 default:
664 break;
665 }
666
Willy Tarreaucab054b2022-10-11 08:36:21 +0200667 if (!quic_alloc_rxbufs_listener(listener)) {
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100668 msg = "could not initialize tx/rx rings";
Frédéric Lécaille48f8e192021-07-06 15:39:26 +0200669 err |= ERR_WARN;
670 goto udp_return;
671 }
672
Amaury Denoyelle75839a42022-11-21 10:04:14 +0100673 if (global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) {
674 if (!quic_test_sock_per_conn_support(listener))
675 global.tune.options &= ~GTUNE_QUIC_SOCK_PER_CONN;
676 }
677
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100678 listener_set_state(listener, LI_LISTEN);
679
680 udp_return:
681 if (msg && errlen) {
682 char pn[INET6_ADDRSTRLEN];
683
684 addr_to_str(&listener->rx.addr, pn, sizeof(pn));
Willy Tarreau6823a3a2021-10-14 11:59:15 +0200685 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 +0100686 }
687 return err;
688}
689
690/* Enable receipt of incoming connections for listener <l>. The receiver must
691 * still be valid. Does nothing in early boot (needs fd_updt).
692 */
693static void quic_enable_listener(struct listener *l)
694{
695 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500696 * is the responsibility of the QUIC xprt to stop accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100697 * connections.
698 */
699 if (fd_updt)
700 fd_want_recv(l->rx.fd);
701}
702
703/* Disable receipt of incoming connections for listener <l>. The receiver must
704 * still be valid. Does nothing in early boot (needs fd_updt).
705 */
706static void quic_disable_listener(struct listener *l)
707{
708 /* FIXME: The following statements are incorrect. This
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +0500709 * is the responsibility of the QUIC xprt to start accepting new
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100710 * connections again.
711 */
712 if (fd_updt)
713 fd_stop_recv(l->rx.fd);
714}
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100715
Willy Tarreau77d37b02023-04-20 19:03:49 +0200716/* change the connection's thread to <new_tid>. For frontend connections, the
717 * target is a listener, and the caller is responsible for guaranteeing that
718 * the listener assigned to the connection is bound to the requested thread.
719 */
Amaury Denoyelle1acbbca2023-04-05 18:17:51 +0200720static int quic_set_affinity(struct connection *conn, int new_tid)
721{
722 struct quic_conn *qc = conn->handle.qc;
Willy Tarreau77d37b02023-04-20 19:03:49 +0200723 return qc_set_tid_affinity(qc, new_tid, objt_listener(conn->target));
Amaury Denoyelle1acbbca2023-04-05 18:17:51 +0200724}
725
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100726static int quic_alloc_dghdlrs(void)
727{
728 int i;
729
Tim Duesterhus9fb57e82022-06-01 21:58:37 +0200730 quic_dghdlrs = calloc(global.nbthread, sizeof(*quic_dghdlrs));
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100731 if (!quic_dghdlrs) {
732 ha_alert("Failed to allocate the quic datagram handlers.\n");
733 return 0;
734 }
735
736 for (i = 0; i < global.nbthread; i++) {
737 struct quic_dghdlr *dghdlr = &quic_dghdlrs[i];
738
739 dghdlr->task = tasklet_new();
740 if (!dghdlr->task) {
741 ha_alert("Failed to allocate the quic datagram handler on thread %d.\n", i);
742 return 0;
743 }
744
745 tasklet_set_tid(dghdlr->task, i);
746 dghdlr->task->context = dghdlr;
747 dghdlr->task->process = quic_lstnr_dghdlr;
748
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100749 MT_LIST_INIT(&dghdlr->dgrams);
750 }
751
Amaury Denoyellee83f9372023-04-18 11:10:54 +0200752 quic_cid_trees = calloc(QUIC_CID_TREES_CNT, sizeof(struct quic_cid_tree));
753 if (!quic_cid_trees) {
754 ha_alert("Failed to allocate global CIDs trees.\n");
755 return 0;
756 }
757
758 for (i = 0; i < QUIC_CID_TREES_CNT; ++i) {
759 HA_RWLOCK_INIT(&quic_cid_trees[i].lock);
760 quic_cid_trees[i].root = EB_ROOT_UNIQUE;
761 }
762
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100763 return 1;
764}
765REGISTER_POST_CHECK(quic_alloc_dghdlrs);
766
767static int quic_deallocate_dghdlrs(void)
768{
769 int i;
770
771 if (quic_dghdlrs) {
772 for (i = 0; i < global.nbthread; ++i)
773 tasklet_free(quic_dghdlrs[i].task);
774 free(quic_dghdlrs);
775 }
776
Amaury Denoyellee83f9372023-04-18 11:10:54 +0200777 ha_free(&quic_cid_trees);
778
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +0100779 return 1;
780}
781REGISTER_POST_DEINIT(quic_deallocate_dghdlrs);
Frédéric Lécailleca42b2c2020-11-02 14:27:08 +0100782
783/*
784 * Local variables:
785 * c-indent-level: 8
786 * c-basic-offset: 8
787 * End:
788 */