blob: 7f3b02aebb2fb4e7edeeea31614b86fd6a1b337f [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01004 * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +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#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020025#include <import/ebmbtree.h>
26
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010027#include <haproxy/buf-t.h>
28#include <haproxy/compat.h>
29#include <haproxy/api.h>
30#include <haproxy/debug.h>
31#include <haproxy/tools.h>
32#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010033
34#include <haproxy/connection.h>
35#include <haproxy/fd.h>
36#include <haproxy/freq_ctr.h>
37#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010038#include <haproxy/h3.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010039#include <haproxy/hq_interop.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010041#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010042#include <haproxy/pipe.h>
43#include <haproxy/proxy.h>
44#include <haproxy/quic_cc.h>
45#include <haproxy/quic_frame.h>
46#include <haproxy/quic_loss.h>
Amaury Denoyellecfa2d562022-01-19 16:01:05 +010047#include <haproxy/quic_sock.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020048#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010049#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010050#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010051#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010052#include <haproxy/ssl_sock.h>
53#include <haproxy/stream_interface.h>
54#include <haproxy/task.h>
55#include <haproxy/trace.h>
56#include <haproxy/xprt_quic.h>
57
Amaury Denoyellea22d8602021-11-10 15:17:56 +010058/* list of supported QUIC versions by this implementation */
59static int quic_supported_version[] = {
60 0x00000001,
Frédéric Lécaille56d3e1b2021-11-19 14:32:52 +010061 0xff00001d, /* draft-29 */
Amaury Denoyellea22d8602021-11-10 15:17:56 +010062
63 /* placeholder, do not add entry after this */
64 0x0
65};
66
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020067/* This is the values of some QUIC transport parameters when absent.
68 * Should be used to initialize any transport parameters (local or remote)
69 * before updating them with customized values.
70 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010071struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020072 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020073 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
74 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020075 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010076};
77
78/* trace source and events */
79static void quic_trace(enum trace_level level, uint64_t mask, \
80 const struct trace_source *src,
81 const struct ist where, const struct ist func,
82 const void *a1, const void *a2, const void *a3, const void *a4);
83
84static const struct trace_event quic_trace_events[] = {
85 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
86 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
87 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
88 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
89 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
90 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
91 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050092 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010093 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
94 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
95 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
Frédéric Lécaille00e24002022-02-18 17:13:45 +010096 { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processin" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010097 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
98 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
99 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
100 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
101 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
102 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
103 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
104 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
105 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
106 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
107 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
108 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
109 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
110 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
111 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100112 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
113 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
114 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
115 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
116 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
117 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100118 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200119 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
120 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100121 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
122 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100123 { /* end */ }
124};
125
126static const struct name_desc quic_trace_lockon_args[4] = {
127 /* arg1 */ { /* already used by the connection */ },
128 /* arg2 */ { .name="quic", .desc="QUIC transport" },
129 /* arg3 */ { },
130 /* arg4 */ { }
131};
132
133static const struct name_desc quic_trace_decoding[] = {
134#define QUIC_VERB_CLEAN 1
135 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
136 { /* end */ }
137};
138
139
140struct trace_source trace_quic = {
141 .name = IST("quic"),
142 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100143 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100144 .default_cb = quic_trace,
145 .known_events = quic_trace_events,
146 .lockon_args = quic_trace_lockon_args,
147 .decoding = quic_trace_decoding,
148 .report_events = ~0, /* report everything by default */
149};
150
151#define TRACE_SOURCE &trace_quic
152INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
153
154static BIO_METHOD *ha_quic_meth;
155
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200156DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100157DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100158DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200159 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100161DECLARE_POOL(pool_head_quic_connection_id,
162 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100163DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100164DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100165DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100166DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100167DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100168DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200169DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100170DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100171
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200172static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +0100173 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100174 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +0100175 int padding, int ack, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100176static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100177static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100178
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100179/* Only for debug purpose */
180struct enc_debug_info {
181 unsigned char *payload;
182 size_t payload_len;
183 unsigned char *aad;
184 size_t aad_len;
185 uint64_t pn;
186};
187
188/* Initializes a enc_debug_info struct (only for debug purpose) */
189static inline void enc_debug_info_init(struct enc_debug_info *edi,
190 unsigned char *payload, size_t payload_len,
191 unsigned char *aad, size_t aad_len, uint64_t pn)
192{
193 edi->payload = payload;
194 edi->payload_len = payload_len;
195 edi->aad = aad;
196 edi->aad_len = aad_len;
197 edi->pn = pn;
198}
199
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100200/* Trace callback for QUIC.
201 * These traces always expect that arg1, if non-null, is of type connection.
202 */
203static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
204 const struct ist where, const struct ist func,
205 const void *a1, const void *a2, const void *a3, const void *a4)
206{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100207 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100208
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100209 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100210 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100211
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100212 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100213 if ((mask & QUIC_EV_CONN_INIT) && qc) {
214 chunk_appendf(&trace_buf, "\n odcid");
215 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100216 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100217 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100218 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100219 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100220 }
221
222 if (mask & QUIC_EV_CONN_ADDDATA) {
223 const enum ssl_encryption_level_t *level = a2;
224 const size_t *len = a3;
225
226 if (level) {
227 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
228
229 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
230 }
231 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100232 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100233 }
234 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
235 /* Initial read & write secrets. */
236 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
237 const unsigned char *rx_sec = a2;
238 const unsigned char *tx_sec = a3;
239
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100240 tls_ctx = &qc->els[level].tls_ctx;
241 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100242 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
243 if (rx_sec)
244 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100245 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100246 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
247 if (tx_sec)
248 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100249 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100250 }
251 }
252 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
253 const enum ssl_encryption_level_t *level = a2;
254 const unsigned char *secret = a3;
255 const size_t *secret_len = a4;
256
257 if (level) {
258 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
259
260 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
261 if (secret && secret_len)
262 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100263 tls_ctx = &qc->els[lvl].tls_ctx;
264 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
265 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100266 }
267 }
268
269 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
270 const enum ssl_encryption_level_t *level = a2;
271 const unsigned char *secret = a3;
272 const size_t *secret_len = a4;
273
274 if (level) {
275 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
276
277 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
278 if (secret && secret_len)
279 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100280 tls_ctx = &qc->els[lvl].tls_ctx;
281 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
282 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100283 }
284
285 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100286
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100287 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100288 const struct quic_tx_packet *pkt = a2;
289 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100290 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100291
292 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100293 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100294 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100295 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100296 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100297 (unsigned long long)qc->path->cwnd,
298 (unsigned long long)qc->path->prep_in_flight,
299 (unsigned long long)qc->path->in_flight,
300 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100301 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100302 }
303 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200304 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100305 if (pkt->pn_node.key != (uint64_t)-1)
306 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100307 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100308 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100309 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
310 (unsigned long long)qc->rx.bytes,
311 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100312 }
313
314 if (room) {
315 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
316 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
317 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100318 }
319 }
320
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100321 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100322 const enum quic_handshake_state *state = a2;
323 const int *err = a3;
324
325 if (state)
326 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
327 if (err)
328 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
329 }
330
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100331 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100332 const struct quic_rx_packet *pkt = a2;
333 const unsigned long *pktlen = a3;
334 const SSL *ssl = a4;
335
336 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100337 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100338 pkt, quic_packet_type_enc_level_char(pkt->type));
339 if (pkt->pnl)
340 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
341 (unsigned long long)pkt->pn);
342 if (pkt->token_len)
343 chunk_appendf(&trace_buf, " toklen=%llu",
344 (unsigned long long)pkt->token_len);
345 if (pkt->aad_len)
346 chunk_appendf(&trace_buf, " aadlen=%llu",
347 (unsigned long long)pkt->aad_len);
348 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
349 pkt->flags, (unsigned long long)pkt->len);
350 }
351 if (pktlen)
352 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
353 if (ssl) {
354 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
355 chunk_appendf(&trace_buf, " el=%c",
356 quic_enc_level_char(ssl_to_quic_enc_level(level)));
357 }
358 }
359
360 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
361 const struct quic_rx_packet *pkt = a2;
362 const struct quic_rx_crypto_frm *cf = a3;
363 const SSL *ssl = a4;
364
365 if (pkt)
366 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
367 quic_packet_type_enc_level_char(pkt->type),
368 (unsigned long long)pkt->pn);
369 if (cf)
370 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
371 (unsigned long long)cf->offset_node.key,
372 (unsigned long long)cf->len);
373 if (ssl) {
374 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200375 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100376 quic_enc_level_char(ssl_to_quic_enc_level(level)));
377 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100378
379 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100380 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100381 }
382
383 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
384 const struct quic_frame *frm = a2;
385
386 if (frm)
387 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
388 }
389
390 if (mask & QUIC_EV_CONN_PHPKTS) {
391 const struct quic_enc_level *qel = a2;
392
393 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100394 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100395 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100396 " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100397 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200398 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100399 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100400 (unsigned long long)qc->path->cwnd,
401 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100402 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100403 (unsigned long long)pktns->tx.in_flight,
404 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100405 }
406 }
407
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100408 if (mask & QUIC_EV_CONN_ENCPKT) {
409 const struct enc_debug_info *edi = a2;
410
411 if (edi)
412 chunk_appendf(&trace_buf,
413 " payload=@%p payload_len=%llu"
414 " aad=@%p aad_len=%llu pn=%llu",
415 edi->payload, (unsigned long long)edi->payload_len,
416 edi->aad, (unsigned long long)edi->aad_len,
417 (unsigned long long)edi->pn);
418 }
419
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100420 if (mask & QUIC_EV_CONN_RMHP) {
421 const struct quic_rx_packet *pkt = a2;
422
423 if (pkt) {
424 const int *ret = a3;
425
426 chunk_appendf(&trace_buf, " pkt@%p", pkt);
427 if (ret && *ret)
428 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
429 pkt->pnl, (unsigned long long)pkt->pn);
430 }
431 }
432
433 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200434 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100435 const unsigned long *val1 = a3;
436 const unsigned long *val2 = a4;
437
438 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100439 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100440 if (val1)
441 chunk_appendf(&trace_buf, " %lu", *val1);
442 if (val2)
443 chunk_appendf(&trace_buf, "..%lu", *val2);
444 }
445
446 if (mask & QUIC_EV_CONN_RTTUPDT) {
447 const unsigned int *rtt_sample = a2;
448 const unsigned int *ack_delay = a3;
449 const struct quic_loss *ql = a4;
450
451 if (rtt_sample)
452 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
453 if (ack_delay)
454 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
455 if (ql)
456 chunk_appendf(&trace_buf,
457 " srtt=%ums rttvar=%ums min_rtt=%ums",
458 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
459 }
460 if (mask & QUIC_EV_CONN_CC) {
461 const struct quic_cc_event *ev = a2;
462 const struct quic_cc *cc = a3;
463
464 if (a2)
465 quic_cc_event_trace(&trace_buf, ev);
466 if (a3)
467 quic_cc_state_trace(&trace_buf, cc);
468 }
469
470 if (mask & QUIC_EV_CONN_PKTLOSS) {
471 const struct quic_pktns *pktns = a2;
472 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100473
474 if (pktns) {
475 chunk_appendf(&trace_buf, " pktns=%s",
476 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
477 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
478 if (pktns->tx.loss_time)
479 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100480 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100481 }
482 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
483 struct quic_tx_packet *pkt;
484
485 chunk_appendf(&trace_buf, " lost_pkts:");
486 list_for_each_entry(pkt, lost_pkts, list)
487 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
488 }
489 }
490
491 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100492 const struct quic_pktns *pktns = a2;
493 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100494 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100495
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100496 if (ifae_pkts)
497 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
498 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100499 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100500 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100501 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100502 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
503 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100504 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
505 if (pktns->tx.in_flight)
506 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100507 if (pktns->tx.loss_time)
508 chunk_appendf(&trace_buf, " loss_time=%dms",
509 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
510 }
511 if (mask & QUIC_EV_CONN_SPTO) {
512 if (pktns->tx.time_of_last_eliciting)
513 chunk_appendf(&trace_buf, " tole=%dms",
514 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
515 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100516 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100517 }
518 }
519
520 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
521 chunk_appendf(&trace_buf,
522 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
523 }
524 }
525
526 if (mask & QUIC_EV_CONN_SPPKTS) {
527 const struct quic_tx_packet *pkt = a2;
528
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100529 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
530 (unsigned long long)qc->path->cwnd,
531 (unsigned long long)qc->path->prep_in_flight,
532 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100533 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100534 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100535 (unsigned long)pkt->pn_node.key,
536 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
537 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100538 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100539 }
540 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100541
542 if (mask & QUIC_EV_CONN_SSLALERT) {
543 const uint8_t *alert = a2;
544 const enum ssl_encryption_level_t *level = a3;
545
546 if (alert)
547 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
548 if (level)
549 chunk_appendf(&trace_buf, " el=%c",
550 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
551 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100552
553 if (mask & QUIC_EV_CONN_BCFRMS) {
554 const size_t *sz1 = a2;
555 const size_t *sz2 = a3;
556 const size_t *sz3 = a4;
557
558 if (sz1)
559 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
560 if (sz2)
561 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
562 if (sz3)
563 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
564 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100565
566 if (mask & QUIC_EV_CONN_PSTRM) {
567 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100568
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100569 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100570 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100571 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100572 }
573 if (mask & QUIC_EV_CONN_LPKT) {
574 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200575 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100576
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100577 if (pkt) {
578 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
579 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
580 if (pkt->pn_node.key != (uint64_t)-1)
581 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
582 }
583
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200584 if (len)
585 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100586 }
587
588}
589
590/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100591static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100592{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200593 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100594
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100595 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100596 return 1;
597
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200598 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
599 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
600 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
601 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200602 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100603 return 1;
604
605 return 0;
606}
607
608/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
609 * both loss detection and PTO and schedule the task assiated to this timer if needed.
610 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100611static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100612{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613 struct quic_pktns *pktns;
614 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200615 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100616
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100617 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100618 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100619
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100620 pktns = quic_loss_pktns(qc);
621 if (tick_isset(pktns->tx.loss_time)) {
622 qc->timer = pktns->tx.loss_time;
623 goto out;
624 }
625
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100626 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100627 * cancelled for a server which reached the anti-amplification limit.
628 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100629 if (!quic_peer_validated_addr(qc) &&
630 (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100631 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100632 qc->timer = TICK_ETERNITY;
633 goto out;
634 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100635
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100636 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100637 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100638 /* Timer cancellation. */
639 qc->timer = TICK_ETERNITY;
640 goto out;
641 }
642
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200643 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
644 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100645 if (tick_isset(pto))
646 qc->timer = pto;
647 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100648 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
649 if (tick_is_expired(qc->timer, now_ms))
650 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
651 else
652 task_schedule(qc->timer_task, qc->timer);
653 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100654 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100655}
656
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100657/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
658 * connection.
659 * Return 1 if succeeded, 0 if not.
660 */
661static int quic_tls_key_update(struct quic_conn *qc)
662{
663 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
664 struct quic_tls_secrets *rx, *tx;
665 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
666 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
667
668 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
669 rx = &tls_ctx->rx;
670 tx = &tls_ctx->tx;
671 nxt_rx = &qc->ku.nxt_rx;
672 nxt_tx = &qc->ku.nxt_tx;
673
674 /* Prepare new RX secrets */
675 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
676 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100677 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100678 return 0;
679 }
680
681 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
682 nxt_rx->key, nxt_rx->keylen,
683 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
684 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100685 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100686 return 0;
687 }
688
689 /* Prepare new TX secrets */
690 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
691 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100692 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100693 return 0;
694 }
695
696 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
697 nxt_tx->key, nxt_tx->keylen,
698 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
699 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100700 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100701 return 0;
702 }
703
704 return 1;
705}
706
707/* Rotate the Key Update information for <qc> QUIC connection.
708 * Must be used after having updated them.
709 * Always succeeds.
710 */
711static void quic_tls_rotate_keys(struct quic_conn *qc)
712{
713 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
714 unsigned char *curr_secret, *curr_iv, *curr_key;
715
716 /* Rotate the RX secrets */
717 curr_secret = tls_ctx->rx.secret;
718 curr_iv = tls_ctx->rx.iv;
719 curr_key = tls_ctx->rx.key;
720
721 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
722 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
723 tls_ctx->rx.key = qc->ku.nxt_rx.key;
724
725 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
726 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
727 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
728
729 qc->ku.prv_rx.secret = curr_secret;
730 qc->ku.prv_rx.iv = curr_iv;
731 qc->ku.prv_rx.key = curr_key;
732 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
733
734 /* Update the TX secrets */
735 curr_secret = tls_ctx->tx.secret;
736 curr_iv = tls_ctx->tx.iv;
737 curr_key = tls_ctx->tx.key;
738
739 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
740 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
741 tls_ctx->tx.key = qc->ku.nxt_tx.key;
742
743 qc->ku.nxt_tx.secret = curr_secret;
744 qc->ku.nxt_tx.iv = curr_iv;
745 qc->ku.nxt_tx.key = curr_key;
746}
747
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100748#ifndef OPENSSL_IS_BORINGSSL
749int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
750 const uint8_t *read_secret,
751 const uint8_t *write_secret, size_t secret_len)
752{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100753 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
754 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100755 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100756 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100757
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100758 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100759 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100760 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
761 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100762 goto out;
763 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100764
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100765 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100766 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100767 goto err;
768 }
769
770 rx = &tls_ctx->rx;
771 tx = &tls_ctx->tx;
772
773 rx->aead = tx->aead = tls_aead(cipher);
774 rx->md = tx->md = tls_md(cipher);
775 rx->hp = tx->hp = tls_hp(cipher);
776
777 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
778 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100779 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100780 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100781 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100782 }
783
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100784 /* Enqueue this connection asap if we could derive O-RTT secrets as
785 * listener. Note that a listener derives only RX secrets for this
786 * level.
787 */
788 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
789 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100790
791 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100792 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100793
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100794 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
795 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100796 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100797 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100798 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100799 }
800
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100801 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100802 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
803 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
804 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
805
806 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
807 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100808 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100809 goto err;
810 }
811
812 memcpy(rx->secret, read_secret, secret_len);
813 rx->secretlen = secret_len;
814 memcpy(tx->secret, write_secret, secret_len);
815 tx->secretlen = secret_len;
816 /* Initialize all the secret keys lengths */
817 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
818 /* Prepare the next key update */
819 if (!quic_tls_key_update(qc))
820 goto err;
821 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100822
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100823 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100824 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100825 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100826 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100827
828 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100829 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100830 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100831}
832#else
833/* ->set_read_secret callback to derive the RX secrets at <level> encryption
834 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500835 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100836 */
837int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
838 const SSL_CIPHER *cipher,
839 const uint8_t *secret, size_t secret_len)
840{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100841 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100842 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100843 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100844
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100845 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
846 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
847 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100848 goto out;
849 }
850
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100851 tls_ctx->rx.aead = tls_aead(cipher);
852 tls_ctx->rx.md = tls_md(cipher);
853 tls_ctx->rx.hp = tls_hp(cipher);
854
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100855 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
856 goto err;
857
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100858 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100859 tls_ctx->rx.key, tls_ctx->rx.keylen,
860 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100861 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
862 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100863 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100864 goto err;
865 }
866
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100867 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100868 const unsigned char *buf;
869 size_t buflen;
870
871 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
872 if (!buflen)
873 goto err;
874
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100875 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100876 goto err;
877 }
878
879 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100880 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100881 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100882
883 return 1;
884
885 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100886 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100887 return 0;
888}
889
890/* ->set_write_secret callback to derive the TX secrets at <level>
891 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500892 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100893 */
894int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
895 const SSL_CIPHER *cipher,
896 const uint8_t *secret, size_t secret_len)
897{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100898 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
899 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100900
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100901 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
902 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
903 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100904 goto out;
905 }
906
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100907 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
908 goto err;
909
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100910 tls_ctx->tx.aead = tls_aead(cipher);
911 tls_ctx->tx.md = tls_md(cipher);
912 tls_ctx->tx.hp = tls_hp(cipher);
913
914 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100915 tls_ctx->tx.key, tls_ctx->tx.keylen,
916 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100917 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
918 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100919 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100920 goto err;
921 }
922
923 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100924 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100925 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100926 return 1;
927
928 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100929 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100930 return 0;
931}
932#endif
933
934/* This function copies the CRYPTO data provided by the TLS stack found at <data>
935 * with <len> as size in CRYPTO buffers dedicated to store the information about
936 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
937 * It fails only if it could not managed to allocate enough CRYPTO buffers to
938 * store all the data.
939 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
940 */
941static int quic_crypto_data_cpy(struct quic_enc_level *qel,
942 const unsigned char *data, size_t len)
943{
944 struct quic_crypto_buf **qcb;
945 /* The remaining byte to store in CRYPTO buffers. */
946 size_t cf_offset, cf_len, *nb_buf;
947 unsigned char *pos;
948
949 nb_buf = &qel->tx.crypto.nb_buf;
950 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
951 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
952 cf_len = len;
953
954 while (len) {
955 size_t to_copy, room;
956
957 pos = (*qcb)->data + (*qcb)->sz;
958 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
959 to_copy = len > room ? room : len;
960 if (to_copy) {
961 memcpy(pos, data, to_copy);
962 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
963 qel->tx.crypto.sz += to_copy;
964 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100965 len -= to_copy;
966 data += to_copy;
967 }
968 else {
969 struct quic_crypto_buf **tmp;
970
971 tmp = realloc(qel->tx.crypto.bufs,
972 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
973 if (tmp) {
974 qel->tx.crypto.bufs = tmp;
975 qcb = &qel->tx.crypto.bufs[*nb_buf];
976 *qcb = pool_alloc(pool_head_quic_crypto_buf);
977 if (!*qcb)
978 return 0;
979
980 (*qcb)->sz = 0;
981 ++*nb_buf;
982 }
983 else {
984 break;
985 }
986 }
987 }
988
989 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
990 * have been buffered.
991 */
992 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200993 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100994 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100995
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100996 /* There is at most one CRYPTO frame in this packet number
997 * space. Let's look for it.
998 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +0100999 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001000 if (frm->type != QUIC_FT_CRYPTO)
1001 continue;
1002
1003 /* Found */
1004 found = frm;
1005 break;
1006 }
1007
1008 if (found) {
1009 found->crypto.len += cf_len;
1010 }
1011 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001012 frm = pool_alloc(pool_head_quic_frame);
1013 if (!frm)
1014 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001015
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001016 frm->type = QUIC_FT_CRYPTO;
1017 frm->crypto.offset = cf_offset;
1018 frm->crypto.len = cf_len;
1019 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001020 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001021 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001022 }
1023
1024 return len == 0;
1025}
1026
1027
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001028/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1029void quic_set_tls_alert(struct quic_conn *qc, int alert)
1030{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001031 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001032 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001033 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001034}
1035
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001036/* Set the application for <qc> QUIC connection.
1037 * Return 1 if succeeded, 0 if not.
1038 */
1039int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1040{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001041 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1042 qc->app_ops = &h3_ops;
1043 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1044 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001045 else
1046 return 0;
1047
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001048 return 1;
1049}
1050
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001051/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1052 * wants to provide the QUIC layer with CRYPTO data.
1053 * Returns 1 if succeeded, 0 if not.
1054 */
1055int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1056 const uint8_t *data, size_t len)
1057{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001058 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001059 enum quic_tls_enc_level tel;
1060 struct quic_enc_level *qel;
1061
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001062 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1063 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1064 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1065 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001066 goto out;
1067 }
1068
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001069 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001070 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001071 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001072 goto err;
1073 }
1074
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001075 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001076 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001077 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001078 goto err;
1079 }
1080
1081 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001082 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001083
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001084 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001085 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001086 return 1;
1087
1088 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001089 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001090 return 0;
1091}
1092
1093int ha_quic_flush_flight(SSL *ssl)
1094{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001095 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001096
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001097 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1098 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099
1100 return 1;
1101}
1102
1103int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1104{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001105 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001106
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001107 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1108 quic_set_tls_alert(qc, alert);
1109 HA_ATOMIC_STORE(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001110 return 1;
1111}
1112
1113/* QUIC TLS methods */
1114static SSL_QUIC_METHOD ha_quic_method = {
1115#ifdef OPENSSL_IS_BORINGSSL
1116 .set_read_secret = ha_set_rsec,
1117 .set_write_secret = ha_set_wsec,
1118#else
1119 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1120#endif
1121 .add_handshake_data = ha_quic_add_handshake_data,
1122 .flush_flight = ha_quic_flush_flight,
1123 .send_alert = ha_quic_send_alert,
1124};
1125
1126/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1127 * Returns an error count.
1128 */
1129int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1130{
1131 struct proxy *curproxy = bind_conf->frontend;
1132 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1133 int cfgerr = 0;
1134
1135#if 0
1136 /* XXX Did not manage to use this. */
1137 const char *ciphers =
1138 "TLS_AES_128_GCM_SHA256:"
1139 "TLS_AES_256_GCM_SHA384:"
1140 "TLS_CHACHA20_POLY1305_SHA256:"
1141 "TLS_AES_128_CCM_SHA256";
1142#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001143 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001144 long options =
1145 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1146 SSL_OP_SINGLE_ECDH_USE |
1147 SSL_OP_CIPHER_SERVER_PREFERENCE;
1148 SSL_CTX *ctx;
1149
1150 ctx = SSL_CTX_new(TLS_server_method());
1151 bind_conf->initial_ctx = ctx;
1152
1153 SSL_CTX_set_options(ctx, options);
1154#if 0
1155 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1156 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1157 "for bind '%s' at [%s:%d].\n",
1158 curproxy->id, ciphers,
1159 bind_conf->arg, bind_conf->file, bind_conf->line);
1160 cfgerr++;
1161 }
1162#endif
1163
1164 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1165 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1166 "for bind '%s' at [%s:%d].\n",
1167 curproxy->id, groups,
1168 bind_conf->arg, bind_conf->file, bind_conf->line);
1169 cfgerr++;
1170 }
1171
1172 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1173 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1174 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1175 SSL_CTX_set_default_verify_paths(ctx);
1176
1177#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1178#ifdef OPENSSL_IS_BORINGSSL
1179 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1180 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1181#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1182 if (bind_conf->ssl_conf.early_data) {
1183 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001184 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001185 }
1186 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1187 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1188#else
1189 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1190#endif
1191 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1192#endif
1193 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1194
1195 return cfgerr;
1196}
1197
1198/* Decode an expected packet number from <truncated_on> its truncated value,
1199 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1200 * the number of bits used to encode this packet number (its length in bytes * 8).
1201 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1202 */
1203static uint64_t decode_packet_number(uint64_t largest_pn,
1204 uint32_t truncated_pn, unsigned int pn_nbits)
1205{
1206 uint64_t expected_pn = largest_pn + 1;
1207 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1208 uint64_t pn_hwin = pn_win / 2;
1209 uint64_t pn_mask = pn_win - 1;
1210 uint64_t candidate_pn;
1211
1212
1213 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1214 /* Note that <pn_win> > <pn_hwin>. */
1215 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1216 candidate_pn + pn_hwin <= expected_pn)
1217 return candidate_pn + pn_win;
1218
1219 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1220 return candidate_pn - pn_win;
1221
1222 return candidate_pn;
1223}
1224
1225/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1226 * cryptographic context.
1227 * <largest_pn> is the largest received packet number and <pn> the address of
1228 * the packet number field for this packet with <byte0> address of its first byte.
1229 * <end> points to one byte past the end of this packet.
1230 * Returns 1 if succeeded, 0 if not.
1231 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001232static int qc_do_rm_hp(struct quic_conn *qc,
1233 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001234 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001235 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001236{
1237 int ret, outlen, i, pnlen;
1238 uint64_t packet_number;
1239 uint32_t truncated_pn = 0;
1240 unsigned char mask[5] = {0};
1241 unsigned char *sample;
1242 EVP_CIPHER_CTX *cctx;
1243 unsigned char *hp_key;
1244
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001245 /* Check there is enough data in this packet. */
1246 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001247 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001248 return 0;
1249 }
1250
1251 cctx = EVP_CIPHER_CTX_new();
1252 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001253 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001254 return 0;
1255 }
1256
1257 ret = 0;
1258 sample = pn + QUIC_PACKET_PN_MAXLEN;
1259
1260 hp_key = tls_ctx->rx.hp_key;
1261 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1262 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1263 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001264 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265 goto out;
1266 }
1267
1268 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1269 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1270 for (i = 0; i < pnlen; i++) {
1271 pn[i] ^= mask[i + 1];
1272 truncated_pn = (truncated_pn << 8) | pn[i];
1273 }
1274
1275 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1276 /* Store remaining information for this unprotected header */
1277 pkt->pn = packet_number;
1278 pkt->pnl = pnlen;
1279
1280 ret = 1;
1281
1282 out:
1283 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001284
1285 return ret;
1286}
1287
1288/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1289 * address, with <payload_len> as payload length, <aad> as address of
1290 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1291 * context.
1292 * Returns 1 if succeeded, 0 if not.
1293 */
1294static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1295 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001296 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001297{
1298 unsigned char iv[12];
1299 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001300 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001301 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001302
1303 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001304 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001305 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001306 }
1307
1308 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1309 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001310 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001311 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001312 }
1313
1314 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001315
1316 err:
1317 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001318 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001319 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001320}
1321
1322/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1323 * Returns 1 if succeeded, 0 if not.
1324 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001325static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001326{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001327 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001328 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001329 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001330 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001331 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1332 unsigned char *rx_key = tls_ctx->rx.key;
1333
1334 kp_changed = 0;
1335 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1336 /* The two tested bits are not at the same position,
1337 * this is why they are first both inversed.
1338 */
1339 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1340 if (pkt->pn < tls_ctx->rx.pn) {
1341 /* The lowest packet number of a previous key phase
1342 * cannot be null if it really stores previous key phase
1343 * secrets.
1344 */
1345 if (!pkt->qc->ku.prv_rx.pn)
1346 return 0;
1347
1348 rx_iv = pkt->qc->ku.prv_rx.iv;
1349 rx_key = pkt->qc->ku.prv_rx.key;
1350 }
1351 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1352 /* Next key phase */
1353 kp_changed = 1;
1354 rx_iv = pkt->qc->ku.nxt_rx.iv;
1355 rx_key = pkt->qc->ku.nxt_rx.key;
1356 }
1357 }
1358 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001359
1360 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1361 return 0;
1362
1363 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1364 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001365 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001366 if (!ret)
1367 return 0;
1368
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001369 /* Update the keys only if the packet decryption succeeded. */
1370 if (kp_changed) {
1371 quic_tls_rotate_keys(pkt->qc);
1372 /* Toggle the Key Phase bit */
1373 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1374 /* Store the lowest packet number received for the current key phase */
1375 tls_ctx->rx.pn = pkt->pn;
1376 /* Prepare the next key update */
1377 if (!quic_tls_key_update(pkt->qc))
1378 return 0;
1379 }
1380
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001381 /* Update the packet length (required to parse the frames). */
1382 pkt->len = pkt->aad_len + ret;
1383
1384 return 1;
1385}
1386
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001387/* Remove from <qcs> stream the acknowledged frames.
1388 * Never fails.
1389 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001390static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001391{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001392 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001393 struct eb64_node *frm_node;
1394
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001395 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001396 frm_node = eb64_first(&qcs->tx.acked_frms);
1397 while (frm_node) {
1398 struct quic_stream *strm;
1399
1400 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001401 if (strm->offset.key > qcs->tx.ack_offset)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001402 break;
1403
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001404 if (strm->offset.key + strm->len > qcs->tx.ack_offset) {
1405 const size_t diff = strm->offset.key + strm->len -
1406 qcs->tx.ack_offset;
1407 qcs->tx.ack_offset += diff;
1408 b_del(strm->buf, diff);
1409 ret = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001410
1411 if (!b_data(strm->buf)) {
1412 b_free(strm->buf);
1413 offer_buffers(NULL, 1);
1414 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001415 }
1416
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001417 frm_node = eb64_next(frm_node);
1418 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001419
1420 /* TODO
1421 *
1422 * memleak: The quic_frame container of the quic_stream should
1423 * be liberated here, as in qc_treat_acked_tx_frm. However this
1424 * code seems to cause a bug which can lead to interrupted
1425 * transfers.
1426 *
1427 * struct quic_frame frm = container_of(strm, struct quic_frame, stream);
1428 * LIST_DELETE(&frm->list);
1429 * pool_free(pool_head_quic_frame, frm);
1430 */
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001431 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001432
1433 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001434}
1435
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001436/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001437static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1438 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001439{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001440 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001441
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001442 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001443 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001444 switch (frm->type) {
1445 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1446 {
1447 struct qcs *qcs = frm->stream.qcs;
1448 struct quic_stream *strm = &frm->stream;
1449
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001450 if (strm->offset.key <= qcs->tx.ack_offset) {
1451 if (strm->offset.key + strm->len > qcs->tx.ack_offset) {
1452 const size_t diff = strm->offset.key + strm->len -
1453 qcs->tx.ack_offset;
1454 qcs->tx.ack_offset += diff;
1455 b_del(strm->buf, diff);
1456 stream_acked = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001457
1458 if (!b_data(strm->buf)) {
1459 b_free(strm->buf);
1460 offer_buffers(NULL, 1);
1461 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001462 }
1463
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001464 LIST_DELETE(&frm->list);
1465 pool_free(pool_head_quic_frame, frm);
1466 }
1467 else {
1468 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1469 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001470
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001471 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001472 }
1473 break;
1474 default:
1475 LIST_DELETE(&frm->list);
1476 pool_free(pool_head_quic_frame, frm);
1477 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001478
1479 if (stream_acked) {
1480 struct qcc *qcc = qc->qcc;
1481
1482 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1483 tasklet_wakeup(qcc->subs->tasklet);
1484 qcc->subs->events &= ~SUB_RETRY_SEND;
1485 if (!qcc->subs->events)
1486 qcc->subs = NULL;
1487 }
1488 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001489}
1490
1491/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1492 * deallocating them, and their TX frames.
1493 * Returns the last node reached to be used for the next range.
1494 * May be NULL if <largest> node could not be found.
1495 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001496static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1497 struct eb_root *pkts,
1498 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001499 struct list *newly_acked_pkts,
1500 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001501 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001502{
1503 struct eb64_node *node;
1504 struct quic_tx_packet *pkt;
1505
1506 if (largest_node)
1507 node = largest_node;
1508 else {
1509 node = eb64_lookup(pkts, largest);
1510 while (!node && largest > smallest) {
1511 node = eb64_lookup(pkts, --largest);
1512 }
1513 }
1514
1515 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001516 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001517
1518 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1519 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001520 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001521 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001522 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001523 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001524 node = eb64_prev(node);
1525 eb64_delete(&pkt->pn_node);
1526 }
1527
1528 return node;
1529}
1530
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001531/* Remove all frames from <pkt_frm_list> and reinsert them in the
1532 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001533 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001534static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1535 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001536 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001537{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001538 struct quic_frame *frm, *frmbak;
1539 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001540
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001541 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1542 LIST_DELETE(&frm->list);
1543 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001544 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001545 }
1546
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001547 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001548}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001549
1550/* Free the TX packets of <pkts> list */
1551static inline void free_quic_tx_pkts(struct list *pkts)
1552{
1553 struct quic_tx_packet *pkt, *tmp;
1554
1555 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001556 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001557 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001558 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001559 }
1560}
1561
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001562/* Send a packet ack event nofication for each newly acked packet of
1563 * <newly_acked_pkts> list and free them.
1564 * Always succeeds.
1565 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001566static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001567 struct list *newly_acked_pkts)
1568{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001569 struct quic_tx_packet *pkt, *tmp;
1570 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1571
1572 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1573 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001574 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001575 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001576 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001577 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001578 ev.ack.acked = pkt->in_flight_len;
1579 ev.ack.time_sent = pkt->time_sent;
1580 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001581 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001582 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001583 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001584 }
1585
1586}
1587
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001588/* Release all the frames attached to <pktns> packet number space */
1589static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1590{
1591 struct quic_frame *frm, *frmbak;
1592
1593 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1594 LIST_DELETE(&frm->list);
1595 pool_free(pool_head_quic_frame, frm);
1596 }
1597}
1598
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001599/* Handle <pkts> list of lost packets detected at <now_us> handling
1600 * their TX frames.
1601 * Send a packet loss event to the congestion controller if
1602 * in flight packet have been lost.
1603 * Also frees the packet in <pkts> list.
1604 * Never fails.
1605 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001606static inline void qc_release_lost_pkts(struct quic_conn *qc,
1607 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001608 struct list *pkts,
1609 uint64_t now_us)
1610{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001611 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001612 uint64_t lost_bytes;
1613
1614 lost_bytes = 0;
1615 oldest_lost = newest_lost = NULL;
1616 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001617 struct list tmp = LIST_HEAD_INIT(tmp);
1618
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001619 lost_bytes += pkt->in_flight_len;
1620 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001621 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001622 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001623 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001624 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001625 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001626 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001627 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001628 if (!oldest_lost) {
1629 oldest_lost = newest_lost = pkt;
1630 }
1631 else {
1632 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001633 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001634 newest_lost = pkt;
1635 }
1636 }
1637
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001638 if (newest_lost) {
1639 /* Sent a congestion event to the controller */
1640 struct quic_cc_event ev = {
1641 .type = QUIC_CC_EVT_LOSS,
1642 .loss.time_sent = newest_lost->time_sent,
1643 };
1644
1645 quic_cc_event(&qc->path->cc, &ev);
1646 }
1647
1648 /* If an RTT have been already sampled, <rtt_min> has been set.
1649 * We must check if we are experiencing a persistent congestion.
1650 * If this is the case, the congestion controller must re-enter
1651 * slow start state.
1652 */
1653 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1654 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1655
1656 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1657 now_ms, qc->max_ack_delay))
1658 qc->path->cc.algo->slow_start(&qc->path->cc);
1659 }
1660
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001661 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001662 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001663 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001664 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001665 }
1666}
1667
1668/* Look for packet loss from sent packets for <qel> encryption level of a
1669 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1670 * their tree if deemed as lost or set the <loss_time> value the packet number
1671 * space if any not deemed lost.
1672 * Should be called after having received an ACK frame with newly acknowledged
1673 * packets or when the the loss detection timer has expired.
1674 * Always succeeds.
1675 */
1676static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1677 struct quic_conn *qc,
1678 struct list *lost_pkts)
1679{
1680 struct eb_root *pkts;
1681 struct eb64_node *node;
1682 struct quic_loss *ql;
1683 unsigned int loss_delay;
1684
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001685 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001686 pkts = &pktns->tx.pkts;
1687 pktns->tx.loss_time = TICK_ETERNITY;
1688 if (eb_is_empty(pkts))
1689 goto out;
1690
1691 ql = &qc->path->loss;
1692 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001693 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1694
1695 node = eb64_first(pkts);
1696 while (node) {
1697 struct quic_tx_packet *pkt;
1698 int64_t largest_acked_pn;
1699 unsigned int loss_time_limit, time_sent;
1700
1701 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001702 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001703 node = eb64_next(node);
1704 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1705 break;
1706
1707 time_sent = pkt->time_sent;
1708 loss_time_limit = tick_add(time_sent, loss_delay);
1709 if (tick_is_le(time_sent, now_ms) ||
1710 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1711 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001712 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001713 }
1714 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001715 if (tick_isset(pktns->tx.loss_time))
1716 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1717 else
1718 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001719 }
1720 }
1721
1722 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001723 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001724}
1725
1726/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1727 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001728 * if the largest acked packet was newly acked and if there was at least one newly
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001729 * acked ack-eliciting packet.
1730 * Return 1, if succeeded, 0 if not.
1731 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001732static inline int qc_parse_ack_frm(struct quic_conn *qc,
1733 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001734 struct quic_enc_level *qel,
1735 unsigned int *rtt_sample,
1736 const unsigned char **pos, const unsigned char *end)
1737{
1738 struct quic_ack *ack = &frm->ack;
1739 uint64_t smallest, largest;
1740 struct eb_root *pkts;
1741 struct eb64_node *largest_node;
1742 unsigned int time_sent, pkt_flags;
1743 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1744 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1745
1746 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1747 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001748 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001749 goto err;
1750 }
1751
1752 if (ack->first_ack_range > ack->largest_ack) {
1753 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001754 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001755 goto err;
1756 }
1757
1758 largest = ack->largest_ack;
1759 smallest = largest - ack->first_ack_range;
1760 pkts = &qel->pktns->tx.pkts;
1761 pkt_flags = 0;
1762 largest_node = NULL;
1763 time_sent = 0;
1764
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001765 if ((int64_t)ack->largest_ack > HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001766 largest_node = eb64_lookup(pkts, largest);
1767 if (!largest_node) {
1768 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001769 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001770 }
1771 else {
1772 time_sent = eb64_entry(&largest_node->node,
1773 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001774 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001775 }
1776
1777 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001778 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001779 do {
1780 uint64_t gap, ack_range;
1781
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001782 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1783 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001784 if (!ack->ack_range_num--)
1785 break;
1786
1787 if (!quic_dec_int(&gap, pos, end))
1788 goto err;
1789
1790 if (smallest < gap + 2) {
1791 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001792 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001793 goto err;
1794 }
1795
1796 largest = smallest - gap - 2;
1797 if (!quic_dec_int(&ack_range, pos, end))
1798 goto err;
1799
1800 if (largest < ack_range) {
1801 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001802 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001803 goto err;
1804 }
1805
1806 /* Do not use this node anymore. */
1807 largest_node = NULL;
1808 /* Next range */
1809 smallest = largest - ack_range;
1810
1811 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001812 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001813 } while (1);
1814
1815 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001816 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001817
1818 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1819 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001820 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001821 }
1822
1823 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1824 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001825 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001826 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001827 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001828 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001829 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1830 if (quic_peer_validated_addr(qc))
1831 qc->path->loss.pto_count = 0;
1832 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001833 }
1834
1835
1836 return 1;
1837
1838 err:
1839 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001840 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001841 return 0;
1842}
1843
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001844/* This function gives the detail of the SSL error. It is used only
1845 * if the debug mode and the verbose mode are activated. It dump all
1846 * the SSL error until the stack was empty.
1847 */
1848static forceinline void qc_ssl_dump_errors(struct connection *conn)
1849{
1850 if (unlikely(global.mode & MODE_DEBUG)) {
1851 while (1) {
1852 unsigned long ret;
1853
1854 ret = ERR_get_error();
1855 if (!ret)
1856 return;
1857
1858 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1859 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1860 }
1861 }
1862}
1863
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001864int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1865 const char **str, int *len);
1866
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001867/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1868 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001869 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001870 * Return 1 if succeeded, 0 if not.
1871 */
1872static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001873 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001874 const unsigned char *data, size_t len,
1875 struct quic_rx_packet *pkt,
1876 struct quic_rx_crypto_frm *cf)
1877{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001878 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001879 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001880
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001881 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001882 qc = ctx->qc;
1883
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001884 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1885
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001886 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1887 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001888 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001889 goto err;
1890 }
1891
1892 el->rx.crypto.offset += len;
1893 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001894 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001895
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001896 state = HA_ATOMIC_LOAD(&qc->state);
1897 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001898 ssl_err = SSL_do_handshake(ctx->ssl);
1899 if (ssl_err != 1) {
1900 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1901 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1902 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001903 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001904 goto out;
1905 }
1906
1907 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001908 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001909 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001910 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001911 goto err;
1912 }
1913
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001914 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
1915 /* I/O callback switch */
1916 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001917 if (qc_is_listener(ctx->qc)) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001918 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001919 /* The connection is ready to be accepted. */
1920 quic_accept_push_qc(qc);
1921 }
1922 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001923 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001924 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001925 } else {
1926 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1927 if (ssl_err != 1) {
1928 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1929 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1930 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001931 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001932 goto out;
1933 }
1934
1935 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001936 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001937 goto err;
1938 }
1939
1940 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001941 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001942 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001943
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001945 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001946 return 1;
1947
1948 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001949 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001950 return 0;
1951}
1952
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001953/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1954 * <pkt> RX packet.
1955 * Return it if succeeded, NULL if not.
1956 */
1957static inline
1958struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1959 struct quic_rx_packet *pkt)
1960{
1961 struct quic_rx_strm_frm *frm;
1962
1963 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1964 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001965 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001966 frm->len = stream_frm->len;
1967 frm->data = stream_frm->data;
1968 frm->pkt = pkt;
Amaury Denoyelle3c430392022-02-28 11:38:36 +01001969 frm->fin = stream_frm->fin;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001970 }
1971
1972 return frm;
1973}
1974
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001975/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001976 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001977 */
1978static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1979{
1980 size_t ret;
1981
Amaury Denoyelle2d2d0302022-02-28 10:00:54 +01001982 ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len);
1983 strm_frm->len -= ret;
1984 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001985
1986 return ret;
1987}
1988
1989/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1990 * streams may be open. The data are copied to the stream RX buffer if possible.
1991 * If not, the STREAM frame is stored to be treated again later.
1992 * We rely on the flow control so that not to store too much STREAM frames.
1993 * Return 1 if succeeded, 0 if not.
1994 */
1995static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1996 struct quic_stream *strm_frm,
1997 struct quic_conn *qc)
1998{
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001999 struct quic_rx_strm_frm *frm;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002000 struct eb64_node *frm_node;
2001 struct qcs *qcs = NULL;
2002 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002003
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002004 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2005 strm_frm->offset.key, strm_frm->fin,
2006 (char *)strm_frm->data, &qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002007
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002008 /* invalid or already received frame */
2009 if (ret == 1)
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002010 return 1;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002011
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002012 if (ret == 2) {
2013 /* frame cannot be parsed at the moment and should be
2014 * buffered.
2015 */
2016 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2017 if (!frm) {
2018 TRACE_PROTO("Could not alloc RX STREAM frame",
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002019 QUIC_EV_CONN_PSTRM, qc);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002020 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002021 }
2022
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002023 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2024 quic_rx_packet_refinc(pkt);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002025
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002026 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002027 }
2028
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002029 /* Frame correctly received by the mux.
2030 * If there is buffered frame for next offset, it may be possible to
2031 * receive them now.
2032 */
2033 frm_node = eb64_first(&qcs->rx.frms);
2034 while (frm_node) {
2035 frm = eb64_entry(&frm_node->node,
2036 struct quic_rx_strm_frm, offset_node);
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002037
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002038 ret = qcc_recv(qc->qcc, qcs->by_id.key, frm->len,
2039 frm->offset_node.key, frm->fin,
2040 (char *)frm->data, &qcs);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002041
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002042 /* interrupt the parsing if the frame cannot be handled for the
2043 * moment only by the MUX.
2044 */
2045 if (ret == 2)
2046 break;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002047
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002048 /* Remove a newly received frame or an invalid one. */
2049 frm_node = eb64_next(frm_node);
2050 eb64_delete(&frm->offset_node);
2051 quic_rx_packet_refdec(frm->pkt);
2052 pool_free(pool_head_quic_rx_strm_frm, frm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002053 }
2054
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002055 /* Decode the received data. */
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002056 qcc_decode_qcs(qc->qcc, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002057
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002058 return 1;
2059}
2060
2061/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2062 * streams may be open. The data are copied to the stream RX buffer if possible.
2063 * If not, the STREAM frame is stored to be treated again later.
2064 * We rely on the flow control so that not to store too much STREAM frames.
2065 * Return 1 if succeeded, 0 if not.
2066 */
2067static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2068 struct quic_stream *strm_frm,
2069 struct quic_conn *qc)
2070{
2071 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002072 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002073 struct quic_rx_strm_frm *frm;
2074 size_t strm_frm_len;
2075
2076 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2077 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002078 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002079 return 0;
2080 }
2081
2082 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002083 if (strm_frm->offset.key < strm->rx.offset) {
2084 size_t diff;
2085
2086 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2087 TRACE_PROTO("Already received STREAM data",
2088 QUIC_EV_CONN_PSTRM, qc);
2089 goto out;
2090 }
2091
2092 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2093 diff = strm->rx.offset - strm_frm->offset.key;
2094 strm_frm->offset.key = strm->rx.offset;
2095 strm_frm->len -= diff;
2096 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002097 }
2098
2099 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002100 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002101 int ret;
2102
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002103 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002104 goto store_frm;
2105
2106 /* qc_strm_cpy() will modify the offset, depending on the number
2107 * of bytes copied.
2108 */
2109 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2110 /* Inform the application of the arrival of this new stream */
2111 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002112 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002113 return 0;
2114 }
2115
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002116 if (ret)
2117 qcs_notify_recv(strm);
2118
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002119 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002120 }
2121 /* Take this frame into an account for the stream flow control */
2122 strm->rx.offset += strm_frm_len;
2123 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002124 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002125 */
2126 if (!strm_frm->len)
2127 goto out;
2128
2129 store_frm:
2130 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2131 if (!frm) {
2132 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002133 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002134 return 0;
2135 }
2136
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002137 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002138 quic_rx_packet_refinc(pkt);
2139
2140 out:
2141 return 1;
2142}
2143
2144static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2145 struct quic_stream *strm_frm,
2146 struct quic_conn *qc)
2147{
2148 if (strm_frm->id & QCS_ID_DIR_BIT)
2149 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2150 else
2151 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2152}
2153
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002154/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002155static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2156 struct quic_conn *qc)
2157{
2158 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002159 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002160 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002161
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002162 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002163 pkts = &qel->pktns->tx.pkts;
2164 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002165 /* Skip the empty packet (they have already been retransmitted) */
2166 while (node) {
2167 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2168 if (!LIST_ISEMPTY(&pkt->frms))
2169 break;
2170 node = eb64_next(node);
2171 }
2172
2173 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002174 return;
2175
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002176 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002177}
2178
2179/* Prepare a fast retransmission during handshake after a client
2180 * has resent Initial packets. According to the RFC a server may retransmit
2181 * up to two datagrams of Initial packets if did not receive all Initial packets
2182 * and resend them coalescing with others (Handshake here).
2183 * (Listener only).
2184 */
2185static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2186{
2187 struct list itmp = LIST_HEAD_INIT(itmp);
2188 struct list htmp = LIST_HEAD_INIT(htmp);
2189 struct quic_frame *frm, *frmbak;
2190
2191 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2192 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2193 struct quic_enc_level *qel = iqel;
2194 struct eb_root *pkts;
2195 struct eb64_node *node;
2196 struct quic_tx_packet *pkt;
2197 struct list *tmp = &itmp;
2198
2199 start:
2200 pkt = NULL;
2201 pkts = &qel->pktns->tx.pkts;
2202 node = eb64_first(pkts);
2203 /* Skip the empty packet (they have already been retransmitted) */
2204 while (node) {
2205 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2206 if (!LIST_ISEMPTY(&pkt->frms))
2207 break;
2208 node = eb64_next(node);
2209 }
2210
2211 if (!pkt)
2212 goto end;
2213
2214 qel->pktns->tx.pto_probe += 1;
2215 requeue:
2216 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2217 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2218 LIST_DELETE(&frm->list);
2219 LIST_APPEND(tmp, &frm->list);
2220 }
2221
2222 if (qel == iqel) {
2223 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2224 pkt = pkt->next;
2225 tmp = &htmp;
2226 hqel->pktns->tx.pto_probe += 1;
2227 goto requeue;
2228 }
2229
2230 qel = hqel;
2231 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002232 goto start;
2233 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002234
2235 end:
2236 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2237 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002238}
2239
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002240/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2241 * as I/O handler context and <qel> as encryption level.
2242 * Returns 1 if succeeded, 0 if failed.
2243 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002244static int qc_parse_pkt_frms(struct quic_rx_packet *pkt, struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002245 struct quic_enc_level *qel)
2246{
2247 struct quic_frame frm;
2248 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002249 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002250 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002251
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002252 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002253 /* Skip the AAD */
2254 pos = pkt->data + pkt->aad_len;
2255 end = pkt->data + pkt->len;
2256
2257 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002258 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002259 goto err;
2260
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002261 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002262 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002263 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002264 break;
2265 case QUIC_FT_PING:
2266 break;
2267 case QUIC_FT_ACK:
2268 {
2269 unsigned int rtt_sample;
2270
2271 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002272 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002273 goto err;
2274
2275 if (rtt_sample) {
2276 unsigned int ack_delay;
2277
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002278 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002279 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2280 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2281 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002282 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002283 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002284 break;
2285 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002286 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002287 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002288 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002289 {
2290 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002291
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002292 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002293 /* XXX TO DO: <cfdebug> is used only for the traces. */
2294 struct quic_rx_crypto_frm cfdebug = { };
2295
2296 cfdebug.offset_node.key = frm.crypto.offset;
2297 cfdebug.len = frm.crypto.len;
2298 TRACE_PROTO("CRYPTO data discarded",
2299 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2300 break;
2301 }
2302
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002303 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2304 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2305 /* XXX TO DO: <cfdebug> is used only for the traces. */
2306 struct quic_rx_crypto_frm cfdebug = { };
2307
2308 cfdebug.offset_node.key = frm.crypto.offset;
2309 cfdebug.len = frm.crypto.len;
2310 /* Nothing to do */
2311 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002312 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002313 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002314 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2315 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002316 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002317 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002318 else {
2319 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2320 /* XXX TO DO: <cfdebug> is used only for the traces. */
2321 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002322
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002323 cfdebug.offset_node.key = frm.crypto.offset;
2324 cfdebug.len = frm.crypto.len;
2325 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002326 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002327 frm.crypto.len -= diff;
2328 frm.crypto.data += diff;
2329 frm.crypto.offset = qel->rx.crypto.offset;
2330 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002331 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002332
2333 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002334 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002335 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002336
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002337 cfdebug.offset_node.key = frm.crypto.offset;
2338 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002339 if (!qc_provide_cdata(qel, ctx,
2340 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002341 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002342 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002343
2344 break;
2345 }
2346
2347 /* frm.crypto.offset > qel->rx.crypto.offset */
2348 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2349 if (!cf) {
2350 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002351 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002352 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002353 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002354
2355 cf->offset_node.key = frm.crypto.offset;
2356 cf->len = frm.crypto.len;
2357 cf->data = frm.crypto.data;
2358 cf->pkt = pkt;
2359 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2360 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002361 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002362 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002363 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002364 {
2365 struct quic_stream *stream = &frm.stream;
2366
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002367 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002368 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2369 goto err;
2370 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2371 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002372
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002373 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002374 goto err;
2375
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002376 break;
2377 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002378 case QUIC_FT_MAX_DATA:
2379 case QUIC_FT_MAX_STREAM_DATA:
2380 case QUIC_FT_MAX_STREAMS_BIDI:
2381 case QUIC_FT_MAX_STREAMS_UNI:
2382 case QUIC_FT_DATA_BLOCKED:
2383 case QUIC_FT_STREAM_DATA_BLOCKED:
2384 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2385 case QUIC_FT_STREAMS_BLOCKED_UNI:
2386 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002387 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002388 case QUIC_FT_RETIRE_CONNECTION_ID:
2389 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002390 break;
2391 case QUIC_FT_CONNECTION_CLOSE:
2392 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002393 /* warn the mux to close the connection */
Amaury Denoyelle2d0f8732022-03-03 18:04:24 +01002394 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelle4af65952022-02-15 11:06:15 +01002395 qc->qcc->flags |= QC_CF_CC_RECV;
Amaury Denoyelle2d0f8732022-03-03 18:04:24 +01002396 tasklet_wakeup(qc->qcc->wait_event.tasklet);
2397 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002398 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002399 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002400 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002401 goto err;
2402
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002403 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002404 break;
2405 default:
2406 goto err;
2407 }
2408 }
2409
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002410 if (fast_retrans)
2411 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002412
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002413 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2414 * has successfully parse a Handshake packet. The Initial encryption must also
2415 * be discarded.
2416 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002417 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002418 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002419
2420 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002421 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002422 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002423 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002424 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002425 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002426 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002427 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002428 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002429 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002430 }
2431
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002432 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002433 return 1;
2434
2435 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002436 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002437 return 0;
2438}
2439
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002440/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002441 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002442 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2443 * This function must be called only after having built a correct datagram.
2444 * Always succeeds.
2445 */
2446static inline void qc_set_dg(struct cbuf *cbuf,
2447 uint16_t dglen, struct quic_tx_packet *pkt)
2448{
2449 write_u16(cb_wr(cbuf), dglen);
2450 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2451 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2452}
2453
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002454/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002455 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2456 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002457 * A header made of two fields is added to each datagram: the datagram length followed
2458 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002459 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2460 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002461 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002462static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2463 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002464{
2465 struct quic_enc_level *qel;
2466 struct cbuf *cbuf;
2467 unsigned char *end_buf, *end, *pos, *spos;
2468 struct quic_tx_packet *pkt;
2469 size_t total;
2470 size_t dg_headlen;
2471
2472 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2473 /* Each datagram is prepended with its length followed by the
2474 * address of the first packet in the datagram.
2475 */
2476 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2477 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2478 total = 0;
2479 start:
2480 cbuf = qr->cbuf;
2481 spos = pos = cb_wr(cbuf);
2482 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2483 * to ensure there is enough room to mark the end of prepared
2484 * contiguous data with a zero length.
2485 */
2486 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2487 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
2488 int err, probe, ack, cc;
2489
2490 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
2491 probe = ack = 0;
2492 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2493 if (!cc) {
2494 probe = qel->pktns->tx.pto_probe;
2495 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
2496 }
2497 /* Do not build any more packet if the TX secrets are not available or
2498 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2499 * and if there is no more packets to send upon PTO expiration
2500 * and if there is no more CRYPTO data available or in flight
2501 * congestion control limit is reached for prepared data
2502 */
2503 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2504 (!cc && !ack && !probe &&
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002505 (LIST_ISEMPTY(frms) ||
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002506 qc->path->prep_in_flight >= qc->path->cwnd))) {
2507 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2508 break;
2509 }
2510
2511 /* Leave room for the datagram header */
2512 pos += dg_headlen;
2513 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2514 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2515 }
2516 else {
2517 end = pos + qc->path->mtu;
2518 }
2519
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002520 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002521 QUIC_PACKET_TYPE_SHORT, ack, probe, cc, &err);
2522 /* Restore the PTO dgrams counter if a packet could not be built */
2523 if (err < 0) {
2524 if (ack)
2525 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
2526 }
2527 switch (err) {
2528 case -2:
2529 goto err;
2530 case -1:
Frédéric Lécailled5066dd2022-03-10 17:06:59 +01002531 goto stop_build;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002532 default:
2533 break;
2534 }
2535
2536 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2537 if (!pkt)
2538 goto err;
2539
2540 total += pkt->len;
2541 /* Set the current datagram as prepared into <cbuf>. */
2542 qc_set_dg(cbuf, pkt->len, pkt);
2543 }
2544
2545 stop_build:
2546 /* Reset <wr> writer index if in front of <rd> index */
2547 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2548 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2549
2550 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
2551 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2552 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2553 /* Mark the end of contiguous data for the reader */
2554 write_u16(cb_wr(cbuf), 0);
2555 cb_add(cbuf, sizeof(uint16_t));
2556 }
2557 }
2558
2559 if (rd && rd <= cbuf->wr) {
2560 cb_wr_reset(cbuf);
2561 /* Let's try to reuse this buffer */
2562 goto start;
2563 }
2564 }
2565
2566 out:
2567 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2568 return total;
2569
2570 err:
2571 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2572 return -1;
2573}
2574
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002575/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002576 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2577 * several packets in the same datagram. A header made of two fields is added
2578 * to each datagram: the datagram length followed by the address of the first
2579 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002580 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2581 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002582 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002583static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2584 enum quic_tls_enc_level tel,
2585 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002586{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002587 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002588 struct cbuf *cbuf;
2589 unsigned char *end_buf, *end, *pos, *spos;
2590 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2591 /* length of datagrams */
2592 uint16_t dglen;
2593 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002594 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002595 /* Each datagram is prepended with its length followed by the
2596 * address of the first packet in the datagram.
2597 */
2598 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002599
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002600 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2601
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002602 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002603 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002604 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002605 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002606 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002607 cbuf = qr->cbuf;
2608 spos = pos = cb_wr(cbuf);
2609 /* Leave at least <dglen> bytes at the end of this buffer
2610 * to ensure there is enough room to mark the end of prepared
2611 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002613 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2614 first_pkt = prv_pkt = NULL;
2615 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002616 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002617 enum quic_pkt_type pkt_type;
2618
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002619 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002620 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002621 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2622 if (!cc) {
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002623 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002624 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002625 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002626 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002627 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002628 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002629 * and if there is no more CRYPTO data available or in flight
2630 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631 */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002632 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002633 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002634 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002635 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002636 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002637 /* Set the current datagram as prepared into <cbuf> if
2638 * the was already a correct packet which was previously written.
2639 */
2640 if (prv_pkt)
2641 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002642 /* Let's select the next encryption level */
2643 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2644 tel = next_tel;
2645 qel = &qc->els[tel];
2646 /* Build a new datagram */
2647 prv_pkt = NULL;
2648 continue;
2649 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002650 break;
2651 }
2652
2653 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002654 if (!prv_pkt) {
2655 /* Leave room for the datagram header */
2656 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002657 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002658 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2659 }
2660 else {
2661 end = pos + qc->path->mtu;
2662 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002663 }
2664
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002665 cur_pkt = qc_build_pkt(&pos, end, qel, &qel->pktns->tx.frms,
2666 qc, dglen, padding, pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002667 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002668 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002669 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002670 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002671 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002672 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673 case -2:
2674 goto err;
2675 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002676 /* If there was already a correct packet present, set the
2677 * current datagram as prepared into <cbuf>.
2678 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002679 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002680 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002681 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002682 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002683 break;
2684 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002685
Frédéric Lécaille63556772021-12-29 17:18:21 +01002686 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2687 if (!cur_pkt)
2688 goto err;
2689
2690 total += cur_pkt->len;
2691 /* keep trace of the first packet in the datagram */
2692 if (!first_pkt)
2693 first_pkt = cur_pkt;
2694 /* Attach the current one to the previous one */
2695 if (prv_pkt)
2696 prv_pkt->next = cur_pkt;
2697 /* Let's say we have to build a new dgram */
2698 prv_pkt = NULL;
2699 dglen += cur_pkt->len;
2700 /* Client: discard the Initial encryption keys as soon as
2701 * a handshake packet could be built.
2702 */
2703 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2704 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2705 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2706 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2707 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2708 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002709 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002710 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille63556772021-12-29 17:18:21 +01002711 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2712 }
2713 /* If the data for the current encryption level have all been sent,
2714 * select the next level.
2715 */
2716 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002717 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002718 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2719 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2720 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2721 tel = next_tel;
2722 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002723 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002724 /* If there is data for the next level, do not
2725 * consume a datagram.
2726 */
2727 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002728 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002729 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002730 /* If we have to build a new datagram, set the current datagram as
2731 * prepared into <cbuf>.
2732 */
2733 if (!prv_pkt) {
2734 qc_set_dg(cbuf, dglen, first_pkt);
2735 first_pkt = NULL;
2736 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002737 padding = 0;
2738 }
2739 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002740 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002741 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2742 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002743 }
2744 }
2745
2746 stop_build:
2747 /* Reset <wr> writer index if in front of <rd> index */
2748 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2749 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2750
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002751 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002752 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2753 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2754 /* Mark the end of contiguous data for the reader */
2755 write_u16(cb_wr(cbuf), 0);
2756 cb_add(cbuf, sizeof(uint16_t));
2757 }
2758 }
2759
Frédéric Lécaille9355d502022-02-23 09:38:01 +01002760 if (rd && rd <= cbuf->wr) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002761 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002762 /* Let's try to reuse this buffer */
2763 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002764 }
2765 }
2766
2767 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002768 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002769 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002770
2771 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002772 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002773 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002774}
2775
2776/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002777 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002778 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002779int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002780{
2781 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002782 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002783
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002784 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002785 cbuf = qr->cbuf;
2786 while (cb_contig_data(cbuf)) {
2787 unsigned char *pos;
2788 struct buffer tmpbuf = { };
2789 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2790 uint16_t dglen;
2791 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002792 unsigned int time_sent;
2793
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002794 pos = cb_rd(cbuf);
2795 dglen = read_u16(pos);
2796 /* End of prepared datagrams.
2797 * Reset the reader index only if in front of the writer index.
2798 */
2799 if (!dglen) {
2800 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2801
2802 if (wr && wr < cbuf->rd) {
2803 cb_rd_reset(cbuf);
2804 continue;
2805 }
2806 break;
2807 }
2808
2809 pos += sizeof dglen;
2810 first_pkt = read_ptr(pos);
2811 pos += sizeof first_pkt;
2812 tmpbuf.area = (char *)pos;
2813 tmpbuf.size = tmpbuf.data = dglen;
2814
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002815 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002816 for (pkt = first_pkt; pkt; pkt = pkt->next)
2817 quic_tx_packet_refinc(pkt);
Amaury Denoyelle58a77042022-02-09 15:43:07 +01002818 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002819 for (pkt = first_pkt; pkt; pkt = pkt->next)
2820 quic_tx_packet_refdec(pkt);
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002821 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002822 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002823
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002824 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002825 qc->tx.bytes += tmpbuf.data;
2826 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002827
2828 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2829 pkt->time_sent = time_sent;
2830 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2831 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002832 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01002833 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
2834 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002835 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002836 qc->path->in_flight += pkt->in_flight_len;
2837 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2838 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002839 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002840 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002841 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002842 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002843 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002844 }
2845 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002846
2847 return 1;
2848}
2849
2850/* Build all the frames which must be sent just after the handshake have succeeded.
2851 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2852 * a HANDSHAKE_DONE frame.
2853 * Return 1 if succeeded, 0 if not.
2854 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002855static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002856{
2857 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002858 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002859 struct quic_frame *frm;
2860
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002861 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002862 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002863 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002864 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002865 if (!frm)
2866 return 0;
2867
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002868 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002869 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002870 }
2871
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002872 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002873 struct quic_connection_id *cid;
2874
2875 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002876 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002877 goto err;
2878
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01002879 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002880 if (!cid)
2881 goto err;
2882
Frédéric Lécaille74904a42022-01-27 15:35:56 +01002883 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01002884 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002885
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002886 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002887 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002888 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002889 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002890
2891 return 1;
2892
2893 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002894 return 0;
2895}
2896
2897/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002898void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002899{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002900 struct eb64_node *n;
2901 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002902
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002903 n = eb64_first(&arngs->root);
2904 while (n) {
2905 struct eb64_node *next;
2906
2907 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2908 next = eb64_next(n);
2909 eb64_delete(n);
2910 free(ar);
2911 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002912 }
2913}
2914
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002915/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2916 * descending order.
2917 */
2918static inline size_t sack_gap(struct quic_arng_node *p,
2919 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002920{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002921 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002922}
2923
2924
2925/* Remove the last elements of <ack_ranges> list of ack range updating its
2926 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002927 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002928 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002929static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002930{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002931 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002932
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002933 last = eb64_last(&arngs->root);
2934 while (last && arngs->enc_sz > limit) {
2935 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002936
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002937 prev = eb64_prev(last);
2938 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002939 return 0;
2940
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002941 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2942 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2943 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2944 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2945 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2946 --arngs->sz;
2947 eb64_delete(last);
2948 pool_free(pool_head_quic_arng, last);
2949 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002950 }
2951
2952 return 1;
2953}
2954
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002955/* Set the encoded size of <arngs> QUIC ack ranges. */
2956static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2957{
2958 struct eb64_node *node, *next;
2959 struct quic_arng_node *ar, *ar_next;
2960
2961 node = eb64_last(&arngs->root);
2962 if (!node)
2963 return;
2964
2965 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2966 arngs->enc_sz = quic_int_getsize(ar->last) +
2967 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2968
2969 while ((next = eb64_prev(node))) {
2970 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2971 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2972 quic_int_getsize(ar_next->last - ar_next->first.key);
2973 node = next;
2974 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2975 }
2976}
2977
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002978/* Insert <ar> ack range into <argns> tree of ack ranges.
2979 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002980 */
2981static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002982struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2983 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002984{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002985 struct quic_arng_node *new_ar;
2986
2987 new_ar = pool_alloc(pool_head_quic_arng);
2988 if (new_ar) {
2989 new_ar->first.key = ar->first;
2990 new_ar->last = ar->last;
2991 eb64_insert(&arngs->root, &new_ar->first);
2992 arngs->sz++;
2993 }
2994
2995 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002996}
2997
2998/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002999 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003000 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003001 *
3002 * Descending order
3003 * ------------->
3004 * range1 range2
3005 * ..........|--------|..............|--------|
3006 * ^ ^ ^ ^
3007 * | | | |
3008 * last1 first1 last2 first2
3009 * ..........+--------+--------------+--------+......
3010 * diff1 gap12 diff2
3011 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003012 * To encode the previous list of ranges we must encode integers as follows in
3013 * descending order:
3014 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003015 * with diff1 = last1 - first1
3016 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003017 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003018 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003019 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003020int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3021 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003022{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003023 struct eb64_node *le;
3024 struct quic_arng_node *new_node;
3025 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003027 new = NULL;
3028 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003029 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003030 if (!new_node)
3031 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003032
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003033 goto out;
3034 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003035
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003036 le = eb64_lookup_le(&arngs->root, ar->first);
3037 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003038 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003039 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003040 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003041
3042 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003043 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003044 else {
3045 struct quic_arng_node *le_ar =
3046 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003047
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003048 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003049 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003050 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003051
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003052 if (le_ar->last + 1 >= ar->first) {
3053 le_ar->last = ar->last;
3054 new = le;
3055 new_node = le_ar;
3056 }
3057 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003058 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003059 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003060 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003061
3062 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003063 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003064 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003065
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003066 /* Verify that the new inserted node does not overlap the nodes
3067 * which follow it.
3068 */
3069 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003070 struct eb64_node *next;
3071 struct quic_arng_node *next_node;
3072
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003073 while ((next = eb64_next(new))) {
3074 next_node =
3075 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003076 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003077 break;
3078
3079 if (next_node->last > new_node->last)
3080 new_node->last = next_node->last;
3081 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003082 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003083 /* Decrement the size of these ranges. */
3084 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003085 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003086 }
3087
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003088 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003089 quic_arngs_set_enc_sz(arngs);
3090
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003091 return 1;
3092}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003093/* Remove the header protection of packets at <el> encryption level.
3094 * Always succeeds.
3095 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003096static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003097{
3098 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003099 struct quic_rx_packet *pqpkt;
3100 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003101 struct quic_enc_level *app_qel;
3102
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003103 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3104 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003105 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003106 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003107 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003109 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003110 goto out;
3111 }
3112 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003113 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003114 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003115 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003116 pqpkt->data, pqpkt->data + pqpkt->len)) {
3117 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003118 /* XXX TO DO XXX */
3119 }
3120 else {
3121 /* The AAD includes the packet number field */
3122 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3123 /* Store the packet into the tree of packets to decrypt. */
3124 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003125 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003126 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3127 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003128 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003129 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003130 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003131 MT_LIST_DELETE_SAFE(pkttmp1);
3132 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003133 }
3134
3135 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003136 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003137}
3138
3139/* Process all the CRYPTO frame at <el> encryption level.
3140 * Return 1 if succeeded, 0 if not.
3141 */
3142static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003143 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003144{
3145 struct eb64_node *node;
3146
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003147 node = eb64_first(&el->rx.crypto.frms);
3148 while (node) {
3149 struct quic_rx_crypto_frm *cf;
3150
3151 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3152 if (cf->offset_node.key != el->rx.crypto.offset)
3153 break;
3154
3155 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3156 goto err;
3157
3158 node = eb64_next(node);
3159 quic_rx_packet_refdec(cf->pkt);
3160 eb64_delete(&cf->offset_node);
3161 pool_free(pool_head_quic_rx_crypto_frm, cf);
3162 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003163 return 1;
3164
3165 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003166 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003167 return 0;
3168}
3169
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003170/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003171 * This is the caller responsibility to check that <cur_el> is different of <next_el>
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003172 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003173 * Return 1 if succeeded, 0 if not.
3174 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003175int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3176 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003177{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003178 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003179 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003180 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003181 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003182
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003183 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003184 qel = cur_el;
3185 next_tel:
3186 if (!qel)
3187 goto out;
3188
3189 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3190 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003191 while (node) {
3192 struct quic_rx_packet *pkt;
3193
3194 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003195 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003196 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003197 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003198 /* Drop the packet */
3199 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003200 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003201 }
3202 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003203 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003204 /* Drop the packet */
3205 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003206 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003207 }
3208 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003209 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3210
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003211 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
3212 if (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack)
3213 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
3214 qc_idle_timer_rearm(qc, 1);
3215 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003216 if (pkt->pn > largest_pn)
3217 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003218 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003219 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003220 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003221 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003222 }
3223 }
3224 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003225 eb64_delete(&pkt->pn_node);
3226 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003227 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003228 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003229
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003230 /* Update the largest packet number. */
3231 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003232 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3233 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003234 goto err;
3235
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003236 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003237 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003238 qel = next_el;
3239 goto next_tel;
3240 }
3241
3242 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003243 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003244 return 1;
3245
3246 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003247 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003248 return 0;
3249}
3250
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003251/* Check if it's possible to remove header protection for packets related to
3252 * encryption level <qel>. If <qel> is NULL, assume it's false.
3253 *
3254 * Return true if the operation is possible else false.
3255 */
3256static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3257{
3258 enum quic_tls_enc_level tel;
3259
3260 if (!qel)
3261 return 0;
3262
3263 tel = ssl_to_quic_enc_level(qel->level);
3264
3265 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003266 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003267 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003268 return 0;
3269 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003270
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003271 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003272 return 0;
3273
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003274 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003275 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
3276 qc->mux_state != QC_MUX_READY)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003277 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003278
3279 return 1;
3280}
3281
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003282/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaillec2f561c2022-02-25 17:46:07 +01003283int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003284{
3285 int ret;
3286 struct qring *qr;
3287
3288 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3289 if (!qr)
3290 /* Never happens */
3291 return 1;
3292
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003293 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003294 if (ret == -1)
3295 goto err;
3296 else if (ret == 0)
3297 goto out;
3298
3299 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3300 goto err;
3301
3302 out:
3303 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3304 return 1;
3305
3306 err:
3307 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3308 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3309 return 0;
3310}
3311
3312/* QUIC connection packet handler task (post handshake) */
3313static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3314{
3315 struct ssl_sock_ctx *ctx;
3316 struct quic_conn *qc;
3317 struct quic_enc_level *qel;
3318 int st;
3319
3320
3321 ctx = context;
3322 qc = ctx->qc;
3323 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3324 st = HA_ATOMIC_LOAD(&qc->state);
3325
3326 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
3327
3328 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3329 qc_rm_hp_pkts(qc, qel);
3330
3331 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3332 goto err;
3333
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003334 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003335 goto err;
3336
3337 return t;
3338
3339 err:
3340 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st);
3341 return t;
3342}
3343
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003344/* QUIC connection packet handler task. */
3345struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003346{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003347 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003348 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003349 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003350 enum quic_tls_enc_level tel, next_tel;
3351 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003352 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003353 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003354
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003355 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003356 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003357 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003358 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003359 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003360 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003361 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3362 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3363 /* The I/O handler has been woken up by the dgram listener
3364 * after the anti-amplification was reached.
3365 */
3366 qc_set_timer(qc);
3367 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3368 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3369 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003370 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003371 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3372 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3373 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003374 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003375 if (st >= QUIC_HS_ST_COMPLETE &&
3376 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3377 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3378 /* There may be remaining Handshake packets to treat and acknowledge. */
3379 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3380 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3381 }
3382 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003383 goto err;
3384
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003385 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003386 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003387
3388 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003389 /* Treat packets waiting for header packet protection decryption */
3390 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003391 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003392
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003393 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3394 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3395 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003396 goto err;
3397
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003398 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003399 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003400 qel = next_qel;
3401 next_qel = NULL;
3402 goto next_level;
3403 }
3404
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003405 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003406 if (st >= QUIC_HS_ST_COMPLETE) {
3407 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3408 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003409 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003410
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003411 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003412 QUIC_FL_TLS_SECRETS_DCD)) {
3413 /* Discard the Handshake keys. */
3414 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3415 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3416 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3417 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003418 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003419 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003420 }
3421
3422 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3423 /* There may be remaining handshake to build (acks) */
3424 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3425 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003426 }
3427
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003428 if (!qr)
3429 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003430 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3431 * be considered.
3432 */
3433 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003434 goto err;
3435 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003436 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003437 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003438 else if (ret == 0)
3439 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003440
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003441 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003442 goto err;
3443
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003444 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003445 /* Check if there is something to do for the next level.
3446 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003447 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003448 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003449 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003450 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003451 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003452 goto next_level;
3453 }
3454
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003455 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003456 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003457 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003458
3459 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003460 if (qr)
3461 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003462 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003463 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003464}
3465
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003466/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003467static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3468{
3469 int i;
3470
3471 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3472 if (qel->tx.crypto.bufs[i]) {
3473 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3474 qel->tx.crypto.bufs[i] = NULL;
3475 }
3476 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003477 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003478}
3479
3480/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003481 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003482 * Returns 1 if succeeded, 0 if not.
3483 */
3484static int quic_conn_enc_level_init(struct quic_conn *qc,
3485 enum quic_tls_enc_level level)
3486{
3487 struct quic_enc_level *qel;
3488
3489 qel = &qc->els[level];
3490 qel->level = quic_to_ssl_enc_level(level);
3491 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3492 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3493 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003494 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003495
3496 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003497 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003498 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003499 qel->rx.crypto.offset = 0;
3500 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003501
3502 /* Allocate only one buffer. */
3503 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3504 if (!qel->tx.crypto.bufs)
3505 goto err;
3506
3507 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3508 if (!qel->tx.crypto.bufs[0])
3509 goto err;
3510
3511 qel->tx.crypto.bufs[0]->sz = 0;
3512 qel->tx.crypto.nb_buf = 1;
3513
3514 qel->tx.crypto.sz = 0;
3515 qel->tx.crypto.offset = 0;
3516
3517 return 1;
3518
3519 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003520 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521 return 0;
3522}
3523
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003524/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
3525 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003526 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003527 * This function must only be called by the thread responsible of the quic_conn
3528 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003529 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003530static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003531{
3532 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003533 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003534
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003535 if (qc->idle_timer_task) {
3536 task_destroy(qc->idle_timer_task);
3537 qc->idle_timer_task = NULL;
3538 }
3539
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003540 if (qc->timer_task) {
3541 task_destroy(qc->timer_task);
3542 qc->timer_task = NULL;
3543 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003544
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003545 /* remove the connection from receiver cids trees */
3546 ebmb_delete(&qc->odcid_node);
3547 ebmb_delete(&qc->scid_node);
3548 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003549
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003550 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003551 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003552 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003553 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003554 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003555 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003556
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003557 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003558 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003559
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003560 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3561 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003562 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003563}
3564
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003565void quic_close(struct connection *conn, void *xprt_ctx)
3566{
3567 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003568 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003569
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003570 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003571
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003572 /* Next application data can be dropped. */
3573 qc->mux_state = QC_MUX_RELEASED;
3574
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003575 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003576}
3577
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003578/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003579static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003580{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003581 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003582 struct quic_conn *qc;
3583 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003584 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003585
3586 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003587 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003588 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003589 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590 task->expire = TICK_ETERNITY;
3591 pktns = quic_loss_pktns(qc);
3592 if (tick_isset(pktns->tx.loss_time)) {
3593 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3594
3595 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3596 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003597 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3598 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003599 goto out;
3600 }
3601
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003602 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003603 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003604 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003605 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3606 pktns->tx.pto_probe = 1;
3607 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3608 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3609 }
3610 else {
3611 pktns->tx.pto_probe = 2;
3612 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003613 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003614 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003615 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3616 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3617
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003618 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003619 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003620 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003621 iel->pktns->tx.pto_probe = 1;
3622 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003623
3624 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003625 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3626 continue;
3627
Frédéric Lécaille53c7d8d2022-02-15 12:00:55 +01003628 qc_prep_fast_retrans(&qc->els[i], qc);
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003629 }
3630
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003631 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3632 qc->path->loss.pto_count++;
3633
3634 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003635 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003636
3637 return task;
3638}
3639
3640/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3641 * connections used to identify the first Initial packets of client connecting
3642 * to listeners. This parameter must be NULL for QUIC connections attached
3643 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3644 * <scid> is the source connection ID with <scid_len> as length.
3645 * Returns 1 if succeeded, 0 if not.
3646 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003647static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003648 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003649 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003650{
3651 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003652 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003653 /* Initial CID. */
3654 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003655 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003656 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003657
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003658 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003659 qc = pool_zalloc(pool_head_quic_conn);
3660 if (!qc) {
3661 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3662 goto err;
3663 }
3664
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003665 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3666 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003667 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003668 goto err;
3669 }
3670
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003671 qc->cids = EB_ROOT;
3672 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003673 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003674 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003675
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003676 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003677 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003678 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003679 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003680 qc->odcid.addrlen = dcid_addr_len;
3681 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003682
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003683 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003684 if (scid_len)
3685 memcpy(qc->dcid.data, scid, scid_len);
3686 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003687 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003688 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003689 }
3690 /* QUIC Client (outgoing connection to servers) */
3691 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003692 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003693 if (dcid_len)
3694 memcpy(qc->dcid.data, dcid, dcid_len);
3695 qc->dcid.len = dcid_len;
3696 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003697 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003698
3699 /* Initialize the output buffer */
3700 qc->obuf.pos = qc->obuf.data;
3701
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003702 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003703 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003704 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003705 goto err;
3706 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003708 /* insert the allocated CID in the receiver datagram handler tree */
3709 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003710 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003711
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712 /* Select our SCID which is the first CID with 0 as sequence number. */
3713 qc->scid = icid->cid;
3714
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003715 /* Packet number spaces initialization. */
3716 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3717 quic_pktns_init(&qc->pktns[i]);
3718 /* QUIC encryption level context initialization. */
3719 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003720 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003721 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003722 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003723 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003724 /* Initialize the packet number space. */
3725 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3726 }
3727
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003728 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003729 qc->tps_tls_ext = qc->version & 0xff000000 ?
3730 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3731 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003732 /* TX part. */
3733 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003734 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3735 qc->tx.wbuf = qc->tx.rbuf = 0;
3736 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003737 /* RX part. */
3738 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003739 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003740 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003741 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003742 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003743 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003744 goto err;
3745 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003746
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003747 /* XXX TO DO: Only one path at this time. */
3748 qc->path = &qc->paths[0];
3749 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3750
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01003751 /* required to use MTLIST_IN_LIST */
3752 MT_LIST_INIT(&qc->accept_list);
3753
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003754 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003755
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003756 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003757
3758 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003759 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003760 pool_free(pool_head_quic_conn_rxbuf, buf_area);
3761 if (qc)
3762 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003763 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003764 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003765}
3766
3767/* Initialize the timer task of <qc> QUIC connection.
3768 * Returns 1 if succeeded, 0 if not.
3769 */
3770static int quic_conn_init_timer(struct quic_conn *qc)
3771{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003772 /* Attach this task to the same thread ID used for the connection */
3773 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774 if (!qc->timer_task)
3775 return 0;
3776
3777 qc->timer = TICK_ETERNITY;
3778 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01003779 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003780
3781 return 1;
3782}
3783
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003784/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
3785 * which is set to 1 when receiving a packet , and 0 when sending packet
3786 */
3787static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
3788{
3789 unsigned int expire;
3790
3791 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
3792 if (read) {
3793 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
3794 }
3795 else {
3796 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
3797 }
3798 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
3799}
3800
3801/* The task handling the idle timeout */
3802static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
3803{
3804 struct quic_conn *qc = ctx;
3805
3806 quic_conn_release(qc);
3807
3808 return NULL;
3809}
3810
3811/* Initialize the idle timeout task for <qc>.
3812 * Returns 1 if succeeded, 0 if not.
3813 */
3814static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
3815{
3816 qc->idle_timer_task = task_new_here();
3817 if (!qc->idle_timer_task)
3818 return 0;
3819
3820 qc->idle_timer_task->process = qc_idle_timer_task;
3821 qc->idle_timer_task->context = qc;
3822 qc_idle_timer_rearm(qc, 1);
3823 task_queue(qc->idle_timer_task);
3824
3825 return 1;
3826}
3827
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003828/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3829 * past one byte of this buffer.
3830 */
3831static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3832 struct quic_rx_packet *pkt)
3833{
3834 unsigned char dcid_len, scid_len;
3835
3836 /* Version */
3837 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3838 return 0;
3839
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003840 /* Destination Connection ID Length */
3841 dcid_len = *(*buf)++;
3842 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3843 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3844 /* XXX MUST BE DROPPED */
3845 return 0;
3846
3847 if (dcid_len) {
3848 /* Check that the length of this received DCID matches the CID lengths
3849 * of our implementation for non Initials packets only.
3850 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003851 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3852 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003853 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003854 return 0;
3855
3856 memcpy(pkt->dcid.data, *buf, dcid_len);
3857 }
3858
3859 pkt->dcid.len = dcid_len;
3860 *buf += dcid_len;
3861
3862 /* Source Connection ID Length */
3863 scid_len = *(*buf)++;
3864 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3865 /* XXX MUST BE DROPPED */
3866 return 0;
3867
3868 if (scid_len)
3869 memcpy(pkt->scid.data, *buf, scid_len);
3870 pkt->scid.len = scid_len;
3871 *buf += scid_len;
3872
3873 return 1;
3874}
3875
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003876/* Insert <pkt> RX packet in its <qel> RX packets tree */
3877static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3878{
3879 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003880 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003881 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3882 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3883 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003884}
3885
3886/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003887 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3888 * byte past the end of the buffer containing this packet and <beg> the address of
3889 * the packet first byte.
3890 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3891 * Returns 1 if succeeded, 0 if not.
3892 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003893static inline int qc_try_rm_hp(struct quic_conn *qc,
3894 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003895 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003896 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003897 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003898{
3899 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003900 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003901 struct quic_enc_level *qel;
3902 /* Only for traces. */
3903 struct quic_rx_packet *qpkt_trace;
3904
3905 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003906 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003907 /* The packet number is here. This is also the start minus
3908 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3909 * protection.
3910 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003911 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003912
3913 tel = quic_packet_type_enc_level(pkt->type);
3914 qel = &qc->els[tel];
3915
3916 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003917 /* Note that the following function enables us to unprotect the packet
3918 * number and its length subsequently used to decrypt the entire
3919 * packets.
3920 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003921 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3922 qel->pktns->rx.largest_pn, pn, beg, end)) {
3923 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003924 goto err;
3925 }
3926
3927 /* The AAD includes the packet number field found at <pn>. */
3928 pkt->aad_len = pn - beg + pkt->pnl;
3929 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003930 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01003931 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003932 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003933 /* If the packet number space has been discarded, this packet
3934 * will be not parsed.
3935 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003936 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003937 goto out;
3938 }
3939
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003940 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003941 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003942 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3943 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003944 }
3945
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003946 *el = qel;
3947 /* No reference counter incrementation here!!! */
3948 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3949 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3950 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3951 b_add(&qc->rx.buf, pkt->len);
3952 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003953 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003954 return 1;
3955
3956 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003957 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003958 return 0;
3959}
3960
3961/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3962 * Also set <*long_header> to 1 if this form is long, 0 if not.
3963 */
3964static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3965 unsigned char byte0, int *long_header)
3966{
3967 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3968 pkt->type =
3969 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3970 *long_header = 1;
3971 }
3972 else {
3973 pkt->type = QUIC_PACKET_TYPE_SHORT;
3974 *long_header = 0;
3975 }
3976}
3977
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003978/*
3979 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3980 */
3981static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3982{
3983 int j = 0, version;
3984
3985 do {
3986 version = quic_supported_version[j];
3987 if (version == pkt->version)
3988 return 1;
3989
3990 version = quic_supported_version[++j];
3991 } while(version);
3992
3993 return 0;
3994}
3995
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003996/*
3997 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3998 * address <addr>.
3999 * Implementation of RFC9000 6. Version Negotiation
4000 *
4001 * TODO implement a rate-limiting sending of Version Negotiation packets
4002 *
4003 * Returns 0 on success else non-zero
4004 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004005static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4006 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004007{
4008 char buf[256];
4009 int i = 0, j, version;
4010 const socklen_t addrlen = get_addr_len(addr);
4011
4012 /*
4013 * header form
4014 * long header, fixed bit to 0 for Version Negotiation
4015 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004016 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4017 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004018
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004019 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004020 /* null version for Version Negotiation */
4021 buf[i++] = '\x00';
4022 buf[i++] = '\x00';
4023 buf[i++] = '\x00';
4024 buf[i++] = '\x00';
4025
4026 /* source connection id */
4027 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004028 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004029 i += pkt->scid.len;
4030
4031 /* destination connection id */
4032 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004033 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004034 i += pkt->dcid.len;
4035
4036 /* supported version */
4037 j = 0;
4038 do {
4039 version = htonl(quic_supported_version[j]);
4040 memcpy(&buf[i], &version, sizeof(version));
4041 i += sizeof(version);
4042
4043 version = quic_supported_version[++j];
4044 } while (version);
4045
4046 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4047 return 1;
4048
4049 return 0;
4050}
4051
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004052/* Generate the token to be used in Retry packets. The token is written to
4053 * <buf> which is expected to be <len> bytes.
4054 *
4055 * Various parameters are expected to be encoded in the token. For now, only
4056 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4057 * as this CID must be repeated by the server in the transport parameters.
4058 *
4059 * TODO add the client address to validate the token origin.
4060 *
4061 * Returns the length of the encoded token or 0 on error.
4062 */
4063static int generate_retry_token(unsigned char *buf, unsigned char len,
4064 struct quic_rx_packet *pkt)
4065{
4066 const size_t token_len = 1 + pkt->dcid.len;
4067 unsigned char i = 0;
4068
4069 if (token_len > len)
4070 return 0;
4071
4072 buf[i++] = pkt->dcid.len;
4073 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4074 i += pkt->dcid.len;
4075
4076 return i;
4077}
4078
4079/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4080 * the Initial <pkt> packet.
4081 *
4082 * Returns 0 on success else non-zero.
4083 */
4084static int send_retry(int fd, struct sockaddr_storage *addr,
4085 struct quic_rx_packet *pkt)
4086{
4087 unsigned char buf[128];
4088 int i = 0, token_len;
4089 const socklen_t addrlen = get_addr_len(addr);
4090 struct quic_cid scid;
4091
4092 /* long header + fixed bit + packet type 0x3 */
4093 buf[i++] = 0xf0;
4094 /* version */
4095 buf[i++] = 0x00;
4096 buf[i++] = 0x00;
4097 buf[i++] = 0x00;
4098 buf[i++] = 0x01;
4099
4100 /* Use the SCID from <pkt> for Retry DCID. */
4101 buf[i++] = pkt->scid.len;
4102 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4103 i += pkt->scid.len;
4104
4105 /* Generate a new CID to be used as SCID for the Retry packet. */
4106 scid.len = QUIC_HAP_CID_LEN;
4107 if (RAND_bytes(scid.data, scid.len) != 1)
4108 return 1;
4109
4110 buf[i++] = scid.len;
4111 memcpy(&buf[i], scid.data, scid.len);
4112 i += scid.len;
4113
4114 /* token */
4115 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
4116 return 1;
4117 i += token_len;
4118
4119 /* token integrity tag */
4120 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4121 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4122 pkt->dcid.len, buf, i)) {
4123 return 1;
4124 }
4125
4126 i += QUIC_TLS_TAG_LEN;
4127
4128 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4129 return 1;
4130
4131 return 0;
4132}
4133
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004134/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4135 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4136 * concatenated to the <pkt> DCID field.
4137 *
4138 * Returns the instance or NULL if not found.
4139 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004140static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004141 struct listener *l,
4142 struct sockaddr_storage *saddr)
4143{
4144 struct quic_conn *qc = NULL;
4145 struct ebmb_node *node;
4146 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004147 /* set if the quic_conn is found in the second DCID tree */
4148 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004149
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004150 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4151 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4152 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4153 /* DCIDs of first packets coming from multiple clients may have
4154 * the same values. Let's distinguish them by concatenating the
4155 * socket addresses.
4156 */
4157 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004158 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004159 pkt->dcid.len + pkt->dcid.addrlen);
4160 if (node) {
4161 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4162 goto end;
4163 }
4164 }
4165
4166 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4167 * also for INITIAL/0-RTT non-first packets with the final DCID in
4168 * used.
4169 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004170 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004171 if (!node)
4172 goto end;
4173
4174 id = ebmb_entry(node, struct quic_connection_id, node);
4175 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004176 found_in_dcid = 1;
4177
4178 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004179 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4180 * If already done, this is a noop.
4181 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004182 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004183 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004184
4185 return qc;
4186}
4187
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004188/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4189 * will extract the parameters stored in the token : <odcid>.
4190 *
4191 * Returns 0 on success else non-zero.
4192 */
4193static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4194 struct quic_cid *odcid)
4195{
4196 uint64_t odcid_len;
4197
4198 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4199 return 1;
4200
4201 memcpy(odcid->data, token, odcid_len);
4202 odcid->len = odcid_len;
4203
4204 return 0;
4205}
4206
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004207/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4208 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4209 * parameters of this session.
4210 * This is the responsibility of the caller to check the validity of all the
4211 * pointers passed as parameter to this function.
4212 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4213 * CO_ER_SSL_NO_MEM.
4214 */
4215static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4216 unsigned char *params, size_t params_len)
4217{
4218 int retry;
4219
4220 retry = 1;
4221 retry:
4222 *ssl = SSL_new(ssl_ctx);
4223 if (!*ssl) {
4224 if (!retry--)
4225 goto err;
4226
4227 pool_gc(NULL);
4228 goto retry;
4229 }
4230
4231 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4232 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4233 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004234 SSL_free(*ssl);
4235 *ssl = NULL;
4236 if (!retry--)
4237 goto err;
4238
4239 pool_gc(NULL);
4240 goto retry;
4241 }
4242
4243 return 0;
4244
4245 err:
4246 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4247 return -1;
4248}
4249
4250/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4251 * used to process <qc> received packets. The allocated context is stored in
4252 * <qc.xprt_ctx>.
4253 *
4254 * Returns 0 on success else non-zero.
4255 */
4256int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4257{
4258 struct bind_conf *bc = qc->li->bind_conf;
4259 struct ssl_sock_ctx *ctx = NULL;
4260
4261 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4262 if (!ctx)
4263 goto err;
4264
4265 ctx->wait_event.tasklet = tasklet_new();
4266 if (!ctx->wait_event.tasklet)
4267 goto err;
4268
4269 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4270 ctx->wait_event.tasklet->context = ctx;
4271 ctx->wait_event.events = 0;
4272 ctx->subs = NULL;
4273 ctx->xprt_ctx = NULL;
4274 ctx->qc = qc;
4275
4276 /* Set tasklet tid based on the SCID selected by us for this
4277 * connection. The upper layer will also be binded on the same thread.
4278 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004279 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004280
4281 if (qc_is_listener(qc)) {
4282 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4283 qc->enc_params, qc->enc_params_len) == -1) {
4284 goto err;
4285 }
4286
4287 /* Enabling 0-RTT */
4288 if (bc->ssl_conf.early_data)
4289 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4290
4291 SSL_set_accept_state(ctx->ssl);
4292 }
4293
4294 ctx->xprt = xprt_get(XPRT_QUIC);
4295
4296 /* Store the allocated context in <qc>. */
4297 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
4298
4299 return 0;
4300
4301 err:
4302 if (ctx && ctx->wait_event.tasklet)
4303 tasklet_free(ctx->wait_event.tasklet);
4304 pool_free(pool_head_quic_conn_ctx, ctx);
4305
4306 return 1;
4307}
4308
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004309static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004310 struct quic_rx_packet *pkt, int first_pkt,
4311 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004312{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004313 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004314 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004315 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004316 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004317 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004318 size_t b_cspace;
4319 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004320
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004321 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004322 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004323 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004324 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004325 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4326 /* This ist only to please to traces and distinguish the
4327 * packet with parsed packet number from others.
4328 */
4329 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004330 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004331 goto err;
4332
4333 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004334 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004335 /* XXX TO BE DISCARDED */
4336 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4337 goto err;
4338 }
4339
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004340 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004341 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004342 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004343 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004344 uint64_t len;
4345
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004346 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004347 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4348 goto err;
4349 }
4350
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004351 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4352 * they must have the same DCID.
4353 */
4354 if (!first_pkt &&
4355 (pkt->dcid.len != dgram->dcid_len ||
4356 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4357 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4358 goto err;
4359 }
4360
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004361 /* Retry of Version Negotiation packets are only sent by servers */
4362 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4363 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4364 goto err;
4365 }
4366
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004367 /* RFC9000 6. Version Negotiation */
4368 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004369 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004370 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004371 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4372 goto err;
4373 }
4374
4375 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004376 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004377 }
4378
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004379 /* For Initial packets, and for servers (QUIC clients connections),
4380 * there is no Initial connection IDs storage.
4381 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004382 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004383 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004384
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004385 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4386 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004387 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4388 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004389 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004390
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004391 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4392 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004393 */
4394 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004395
4396 /* TODO Retry should be automatically activated if
4397 * suspect network usage is detected.
4398 */
4399 if (!token_len && l->bind_conf->quic_force_retry) {
4400 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004401 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004402 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4403 goto err;
4404 }
4405
4406 goto err;
4407 }
4408 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004409 pkt->token = buf;
4410 buf += pkt->token_len;
4411 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004412 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004413 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004414 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004415 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4416 goto err;
4417 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004418 }
4419
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004420 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4421 end - buf < len) {
4422 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4423 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004424 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004425
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004426 payload = buf;
4427 pkt->len = len + payload - beg;
4428
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004429 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004430 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004431 int ipv4;
4432 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004433 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004434 const unsigned char *salt = initial_salt_v1;
4435 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004436
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004437 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004438 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004439 goto err;
4440 }
4441
Frédéric Lécailledc364042022-01-27 16:51:54 +01004442 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4443 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4444 goto err;
4445 }
4446
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004447 pkt->saddr = dgram->saddr;
4448 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004449 qc = qc_new_conn(pkt->version, ipv4,
4450 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004451 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004452 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004453 goto err;
4454
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004455 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4456
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004457 odcid = &qc->rx.params.original_destination_connection_id;
4458 /* Copy the transport parameters. */
4459 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004460
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004461 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004462 if (pkt->token_len) {
4463 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4464 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4465 goto err;
4466 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004467 /* Copy retry_source_connection_id transport parameter. */
4468 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4469 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004470 }
4471 else {
4472 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4473 odcid->len = pkt->dcid.len;
4474 }
4475
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004476 /* Copy the initial source connection ID. */
4477 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4478 qc->enc_params_len =
4479 quic_transport_params_encode(qc->enc_params,
4480 qc->enc_params + sizeof qc->enc_params,
4481 &qc->rx.params, 1);
4482 if (!qc->enc_params_len)
4483 goto err;
4484
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004485 if (qc_conn_alloc_ssl_ctx(qc))
4486 goto err;
4487
Frédéric Lécaille789413c2022-01-31 10:16:18 +01004488 if (!quic_conn_init_timer(qc))
4489 goto err;
4490
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004491 if (!quic_conn_init_idle_timer_task(qc))
4492 goto err;
4493
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004494 /* NOTE: the socket address has been concatenated to the destination ID
4495 * chosen by the client for Initial packets.
4496 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004497 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4498 salt = initial_salt_draft_29;
4499 salt_len = sizeof initial_salt_draft_29;
4500 }
4501 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004502 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004503 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004504 goto err;
4505 }
4506
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004507 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004508 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004509 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004510
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004511 /* If the insertion failed, it means that another
4512 * thread has already allocated a QUIC connection for
4513 * the same CID. Liberate our allocated connection.
4514 */
4515 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004516 qc_to_purge = qc;
4517
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004518 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4519 pkt->qc = qc;
4520 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004521
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004522 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004523 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004524 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004525 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004526 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004527 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004528 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004529 }
4530 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004531 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004532 }
4533 }
4534 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004535 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004536 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4537 goto err;
4538 }
4539
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004540 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004541 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004542
4543 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4544 * they must have the same DCID.
4545 */
4546 if (!first_pkt &&
4547 (pkt->dcid.len != dgram->dcid_len ||
4548 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4549 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4550 goto err;
4551 }
4552
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004553 buf += QUIC_HAP_CID_LEN;
4554
4555 /* A short packet is the last one of a UDP datagram. */
4556 payload = buf;
4557 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004558
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004559 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004560 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004561 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004562 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4563 goto err;
4564 }
4565
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004566 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004567 }
4568
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004569
4570 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4571 * they must have the same DCID.
4572 *
4573 * This check must be done after the final update to pkt.len to
4574 * properly drop the packet on failure.
4575 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004576 if (first_pkt && !quic_peer_validated_addr(qc) &&
4577 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4578 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4579 QUIC_EV_CONN_LPKT, qc);
4580 /* Reset the anti-amplification bit. It will be set again
4581 * when sending the next packet if reached again.
4582 */
4583 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4584 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4585 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004586 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004587
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004588 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004589
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004590 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004591 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004592 goto out;
4593 }
4594
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004595 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004596 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004597 b_cspace = b_contig_space(&qc->rx.buf);
4598 if (b_cspace < pkt->len) {
4599 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004600 if (b_cspace) {
4601 b_putchr(&qc->rx.buf, 0x00);
4602 b_cspace--;
4603 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004604 b_add(&qc->rx.buf, b_cspace);
4605 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004606 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004607 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004608 goto err;
4609 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004610 }
4611
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004612 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004613 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004614 goto err;
4615 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004616
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004617 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004618 if (pkt->aad_len)
4619 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004620 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004621 /* Wake up the connection packet handler task from here only if all
4622 * the contexts have been initialized, especially the mux context
4623 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4624 * will start it if these contexts for the connection are not already
4625 * initialized.
4626 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004627 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004628 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004629 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004630
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004631 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004632
4633 return pkt->len;
4634
4635 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004636 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4637 * This cannot be done by this thread.
4638 */
4639 if (io_cb_wakeup) {
4640 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4641 if (conn_ctx && conn_ctx->wait_event.tasklet)
4642 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4643 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004644 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004645 if (!pkt->len)
4646 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004647 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004648 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004649
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004650 return -1;
4651}
4652
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004653/* This function builds into <buf> buffer a QUIC long packet header.
4654 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004655 */
4656static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4657 int type, size_t pn_len, struct quic_conn *conn)
4658{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004659 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004660 return 0;
4661
4662 /* #0 byte flags */
4663 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4664 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4665 /* Version */
4666 quic_write_uint32(buf, end, conn->version);
4667 *(*buf)++ = conn->dcid.len;
4668 /* Destination connection ID */
4669 if (conn->dcid.len) {
4670 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4671 *buf += conn->dcid.len;
4672 }
4673 /* Source connection ID */
4674 *(*buf)++ = conn->scid.len;
4675 if (conn->scid.len) {
4676 memcpy(*buf, conn->scid.data, conn->scid.len);
4677 *buf += conn->scid.len;
4678 }
4679
4680 return 1;
4681}
4682
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004683/* This function builds into <buf> buffer a QUIC short packet header.
4684 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004685 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004686static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4687 size_t pn_len, struct quic_conn *conn,
4688 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004689{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004690 if (end - *buf < 1 + conn->dcid.len)
4691 return 0;
4692
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004693 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004694 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4695 ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004696 /* Destination connection ID */
4697 if (conn->dcid.len) {
4698 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4699 *buf += conn->dcid.len;
4700 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004701
4702 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004703}
4704
4705/* Apply QUIC header protection to the packet with <buf> as first byte address,
4706 * <pn> as address of the Packet number field, <pnlen> being this field length
4707 * with <aead> as AEAD cipher and <key> as secret key.
4708 * Returns 1 if succeeded or 0 if failed.
4709 */
4710static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4711 const EVP_CIPHER *aead, const unsigned char *key)
4712{
4713 int i, ret, outlen;
4714 EVP_CIPHER_CTX *ctx;
4715 /* We need an IV of at least 5 bytes: one byte for bytes #0
4716 * and at most 4 bytes for the packet number
4717 */
4718 unsigned char mask[5] = {0};
4719
4720 ret = 0;
4721 ctx = EVP_CIPHER_CTX_new();
4722 if (!ctx)
4723 return 0;
4724
4725 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4726 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4727 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4728 goto out;
4729
4730 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4731 for (i = 0; i < pnlen; i++)
4732 pn[i] ^= mask[i + 1];
4733
4734 ret = 1;
4735
4736 out:
4737 EVP_CIPHER_CTX_free(ctx);
4738
4739 return ret;
4740}
4741
4742/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4743 * ACK ranges if needed to a value below <limit> in bytes.
4744 * Return 1 if succeeded, 0 if not.
4745 */
4746static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4747{
4748 size_t room, ack_delay_sz;
4749
4750 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4751 /* A frame is made of 1 byte for the frame type. */
4752 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004753 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004754 return 0;
4755
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004756 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004757}
4758
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004759/* Prepare into <outlist> as most as possible ack-eliciting frame from their
4760 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
4761 * with <room> as available room, and <*len> the packet Length field initialized
4762 * with the number of bytes already present in this buffer which must be taken
4763 * into an account for the Length packet field value. <headlen> is the number of
4764 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004765 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004766 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004767 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004768 * Return 1 if succeeded, 0 if not.
4769 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004770static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004771 size_t room, size_t *len, size_t headlen,
4772 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004773 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004774{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004775 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004776 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004777
Frédéric Lécailleea604992020-12-24 13:01:37 +01004778 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004779 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004780 return 0;
4781
Frédéric Lécailleea604992020-12-24 13:01:37 +01004782 /* If we are not probing we must take into an account the congestion
4783 * control window.
4784 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004785 if (!qel->pktns->tx.pto_probe) {
4786 size_t remain = quic_path_prep_data(qc->path);
4787
4788 if (headlen > remain)
4789 return 0;
4790
4791 room = QUIC_MIN(room, remain - headlen);
4792 }
4793
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004794 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004795 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004796 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004797 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004798 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004799
Frédéric Lécailleea604992020-12-24 13:01:37 +01004800 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004801 break;
4802
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004803 switch (cf->type) {
4804 case QUIC_FT_CRYPTO:
4805 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004806 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004807 /* Compute the length of this CRYPTO frame header */
4808 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4809 /* Compute the data length of this CRyPTO frame. */
4810 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4811 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004812 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004813 if (!dlen)
4814 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004815
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004816 /* CRYPTO frame length. */
4817 flen = hlen + quic_int_getsize(dlen) + dlen;
4818 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004819 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004820 /* Add the CRYPTO data length and its encoded length to the packet
4821 * length and the length of this length.
4822 */
4823 *len += flen;
4824 room -= flen;
4825 if (dlen == cf->crypto.len) {
4826 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004827 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004828 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004829 }
4830 else {
4831 struct quic_frame *new_cf;
4832
4833 new_cf = pool_alloc(pool_head_quic_frame);
4834 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004835 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004836 return 0;
4837 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004838
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004839 new_cf->type = QUIC_FT_CRYPTO;
4840 new_cf->crypto.len = dlen;
4841 new_cf->crypto.offset = cf->crypto.offset;
4842 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004843 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004844 /* Consume <dlen> bytes of the current frame. */
4845 cf->crypto.len -= dlen;
4846 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004847 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004848 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004849
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004850 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004851 /* Note that these frames are accepted in short packets only without
4852 * "Length" packet field. Here, <*len> is used only to compute the
4853 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004854 *
4855 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004856 * excepting the variable ones. Note that +1 is for the type of this frame.
4857 */
4858 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004859 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004860 /* Compute the data length of this STREAM frame. */
4861 avail_room = room - hlen - *len;
4862 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004863 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004864
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004865 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004866 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004867 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4868 dlen = max_available_room(avail_room, &dlen_sz);
4869 if (dlen > cf->stream.len) {
4870 dlen = cf->stream.len;
4871 }
4872 dlen_sz = quic_int_getsize(dlen);
4873 flen = hlen + dlen_sz + dlen;
4874 }
4875 else {
4876 dlen = QUIC_MIN(avail_room, cf->stream.len);
4877 flen = hlen + dlen;
4878 }
4879 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004880 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004881 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004882 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004883 /* Add the STREAM data length and its encoded length to the packet
4884 * length and the length of this length.
4885 */
4886 *len += flen;
4887 room -= flen;
4888 if (dlen == cf->stream.len) {
4889 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004890 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004891 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004892 }
4893 else {
4894 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004895 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004896
4897 new_cf = pool_zalloc(pool_head_quic_frame);
4898 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004899 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004900 return 0;
4901 }
4902
4903 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004904 new_cf->stream.qcs = cf->stream.qcs;
4905 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004906 new_cf->stream.id = cf->stream.id;
4907 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4908 new_cf->stream.offset = cf->stream.offset;
4909 new_cf->stream.len = dlen;
4910 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4911 /* FIN bit reset */
4912 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4913 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004914 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004915 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4916 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004917 cf_buf = b_make(b_orig(cf->stream.buf),
4918 b_size(cf->stream.buf),
4919 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004920 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004921 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004922 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004923 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004924 break;
4925
4926 default:
4927 flen = qc_frm_len(cf);
4928 BUG_ON(!flen);
4929 if (flen > room)
4930 continue;
4931
4932 *len += flen;
4933 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004934 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004935 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004936 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004937 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004938 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004939 }
4940
Frédéric Lécailleea604992020-12-24 13:01:37 +01004941 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004942}
4943
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004944/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004945 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4946 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01004947 * filling the buffer with as much frames as possible from <frms> list of
4948 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004949 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004950 * reserved so that to ensure there is enough room to build this AEAD TAG after
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004951 * having returned from this function.
4952 * This function also updates the value of <buf_pn> pointer to point to the packet
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004953 * number field in this packet. <pn_len> will also have the packet number
4954 * length as value.
4955 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004956 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004957 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004958static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4959 size_t dglen, struct quic_tx_packet *pkt,
4960 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004961 int ack, int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01004962 struct quic_enc_level *qel, struct quic_conn *qc,
4963 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004964{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004965 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004966 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004967 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4968 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004969 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004970 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004971 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004972 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004973 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004974
Frédéric Lécailleea604992020-12-24 13:01:37 +01004975 /* Length field value with CRYPTO frames if present. */
4976 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004977 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004978 /* When not probing and not acking, and no immediate close is required,
4979 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004980 * the congestion controller window. So, we do not limit the size of this
4981 * packet if we have an ACK frame to send because an ACK frame is not
4982 * ack-eliciting. This size will be limited if we have ack-eliciting
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01004983 * frames to send from <frms>.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004984 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004985 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004986 size_t path_room;
4987
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004988 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004989 if (end - beg > path_room)
4990 end = beg + path_room;
4991 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004992
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004993 /* Ensure there is enough room for the TLS encryption tag and a zero token
4994 * length field if any.
4995 */
4996 if (end - pos < QUIC_TLS_TAG_LEN +
4997 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4998 goto no_room;
4999
5000 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02005001 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005002 /* packet number length */
5003 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005004 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005005 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005006 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005007 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005008 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005009 goto no_room;
5010
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005011 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
5012 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005013 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005014 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005015 /* Build an ACK frame if required. */
5016 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005017 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005018 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005019 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005020 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
5021 * smallest frame (PING) and <*pn_len> more for the packet number. Note
5022 * that from here, we do not know if we will have to send a PING frame.
5023 * This will be decided after having computed the ack-eliciting frames
5024 * to be added to this packet.
5025 */
5026 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - 1 - *pn_len - pos);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005027 if (!ack_frm_len)
5028 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005029 }
5030
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005031 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005032 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005033 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005034 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005035 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01005036
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005037 /* Initialize the length of the frames built below to <len>.
5038 * If any frame could be successfully built by qc_build_frms(),
5039 * we will have len_frms > len.
5040 */
5041 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005042 if (!qc_build_frms(&frm_list, frms,
5043 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01005044 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005045 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005046 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005047 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005048
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005049 /* Length (of the remaining data). Must not fail because, the buffer size
5050 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
5051 * for the encryption tag. It must be taken into an account for the length
5052 * of this packet.
5053 */
5054 if (len_frms)
5055 len = len_frms + QUIC_TLS_TAG_LEN;
5056 else
5057 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005058 /* CONNECTION_CLOSE frame */
5059 if (cc) {
5060 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005061
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005062 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005063 len += qc_frm_len(&cc_frm);
5064 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005065 add_ping_frm = 0;
5066 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005067 len_sz = quic_int_getsize(len);
5068 /* Add this packet size to <dglen> */
5069 dglen += head_len + len_sz + len;
5070 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
5071 /* This is a maximum padding size */
5072 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
5073 /* The length field value is of this packet is <len> + <padding_len>
5074 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05005075 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005076 * sizes from <padding_len>.
5077 */
5078 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
5079 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005080 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005081 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005082 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005083 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005084 add_ping_frm = 1;
5085 len += 1;
5086 }
5087 /* If there is no frame at all to follow, add at least a PADDING frame. */
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005088 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005089 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
5090 }
5091
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005092 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
5093 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005094
5095 /* Packet number field address. */
5096 *buf_pn = pos;
5097
5098 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005099 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5100 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005101
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005102 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005103 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005104
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005105 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005106 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005107 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005108
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005109 list_for_each_entry(cf, &frm_list, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005110 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005111 ssize_t room = end - pos;
5112 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005113 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005114 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005115 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005116 }
5117 }
5118
5119 /* Build a PING frame if needed. */
5120 if (add_ping_frm) {
5121 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005122 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005123 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005124 }
5125
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005126 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005127 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005128 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005129
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005130 /* Build a PADDING frame if needed. */
5131 if (padding_len) {
5132 frm.type = QUIC_FT_PADDING;
5133 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005134 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005135 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005136 }
5137
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005138 /* If this packet is ack-eliciting and we are probing let's
5139 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005140 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005141 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5142 qel->pktns->tx.pto_probe)
5143 qel->pktns->tx.pto_probe--;
5144
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005145 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005146 LIST_SPLICE(&pkt->frms, &frm_list);
5147 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005148
5149 return 1;
5150
5151 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005152 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005153 LIST_SPLICE(frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005154 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005155 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005156}
5157
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005158static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005159{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005160 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005161 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005162 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005163 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005164 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005165 pkt->next = NULL;
5166 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005167}
5168
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005169/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005170static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
5171{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005172 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005173
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005174 if (!pkt)
5175 return;
5176
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005177 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02005178 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005179 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005180 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005181 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005182}
5183
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005184/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005185 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
5186 * of prebuilt frames.
5187 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005188 * Return -2 if the packet could not be allocated or encrypted for any reason,
5189 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005190 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005191static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5192 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005193 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005194 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005195 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005196{
5197 /* The pointer to the packet number field. */
5198 unsigned char *buf_pn;
5199 unsigned char *beg, *end, *payload;
5200 int64_t pn;
5201 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005202 struct quic_tls_ctx *tls_ctx;
5203 struct quic_tx_packet *pkt;
5204
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005205 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005206 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005207 pkt = pool_alloc(pool_head_quic_tx_packet);
5208 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005209 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005210 *err = -2;
5211 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005212 }
5213
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005214 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005215 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005216 pn_len = 0;
5217 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005218
5219 pn = qel->pktns->tx.next_pn + 1;
5220 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005221 ack, padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005222 *err = -1;
5223 goto err;
5224 }
5225
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005226 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005227 payload = buf_pn + pn_len;
5228 payload_len = end - payload;
5229 aad_len = payload - beg;
5230
5231 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005232 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005233 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005234 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005235 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005236
5237 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005238 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005239 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5240 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005241 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005242 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005243 goto err;
5244 }
5245
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005246 /* Consume a packet number */
5247 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005248 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01005249 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005250 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005251 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5252 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005253 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005254 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005255 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005256 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005257 pkt->in_flight_len = pkt->len;
5258 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005259 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005260 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005261 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005262
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005263 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005264
5265 err:
5266 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005267 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005268 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005269}
5270
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005271
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005272/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5273 * event subscriber <es> is not allowed to change from a previous call as long
5274 * as at least one event is still subscribed. The <event_type> must only be a
5275 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5276 */
5277static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5278{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005279 struct qcc *qcc = conn->qc->qcc;
5280
5281 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5282 BUG_ON(qcc->subs && qcc->subs != es);
5283
5284 es->events |= event_type;
5285 qcc->subs = es;
5286
5287 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005288 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005289
5290 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005291 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005292
5293 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005294}
5295
5296/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5297 * The <es> pointer is not allowed to differ from the one passed to the
5298 * subscribe() call. It always returns zero.
5299 */
5300static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5301{
5302 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5303}
5304
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005305/* Store in <xprt_ctx> the context attached to <conn>.
5306 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005307 */
5308static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5309{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005310 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005311
5312 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5313
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005314 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005315 if (*xprt_ctx)
5316 goto out;
5317
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005318 HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005319
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005320 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005321 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005322
5323 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005324}
5325
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005326/* Start the QUIC transport layer */
5327static int qc_xprt_start(struct connection *conn, void *ctx)
5328{
5329 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005330 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005331
5332 qc = conn->qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005333 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5334 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5335 return 0;
5336 }
5337
5338 /* mux-quic can now be considered ready. */
5339 qc->mux_state = QC_MUX_READY;
5340
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005341 tasklet_wakeup(qctx->wait_event.tasklet);
5342 return 1;
5343}
5344
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005345/* transport-layer operations for QUIC connections. */
5346static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005347 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005348 .subscribe = quic_conn_subscribe,
5349 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005350 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005351 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005352 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5353 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005354 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005355 .name = "QUIC",
5356};
5357
5358__attribute__((constructor))
5359static void __quic_conn_init(void)
5360{
5361 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5362 xprt_register(XPRT_QUIC, &ssl_quic);
5363}
5364
5365__attribute__((destructor))
5366static void __quic_conn_deinit(void)
5367{
5368 BIO_meth_free(ha_quic_meth);
5369}
5370
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005371/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5372 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005373 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005374 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005375struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005376{
5377 unsigned char *pos;
5378 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005379 struct quic_dghdlr *dghdlr = ctx;
5380 struct quic_dgram *dgram;
5381 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005382
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005383 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005384 pos = dgram->buf;
5385 end = pos + dgram->len;
5386 do {
5387 int ret;
5388 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005389
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005390 pkt = pool_zalloc(pool_head_quic_rx_packet);
5391 if (!pkt)
5392 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005393
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005394 quic_rx_packet_refinc(pkt);
5395 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5396 first_pkt = 0;
5397 pos += pkt->len;
5398 quic_rx_packet_refdec(pkt);
5399 if (ret == -1)
5400 /* If the packet length could not be found, we cannot continue. */
5401 break;
5402 } while (pos < end);
5403
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005404 /* Increasing the received bytes counter by the UDP datagram length
5405 * if this datagram could be associated to a connection.
5406 */
5407 if (dgram->qc)
5408 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01005409
5410 /* Mark this datagram as consumed */
5411 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005412 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005413
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005414 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005415
5416 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005417 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005418}
5419
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005420/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5421 * Returns 1 if succeeded, 0 if not.
5422 */
5423static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5424 unsigned char **dcid, size_t *dcid_len)
5425{
5426 int long_header;
5427 size_t minlen, skip;
5428
5429 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5430 goto err;
5431
5432 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5433 minlen = long_header ?
5434 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5435 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01005436 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005437 goto err;
5438
5439 buf += skip;
5440 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5441 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5442 goto err;
5443
5444 *dcid = buf;
5445
5446 return 1;
5447
5448 err:
5449 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5450 return 0;
5451}
5452
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005453/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5454 * correct datagram handler.
5455 * Return 1 if a correct datagram could be found, 0 if not.
5456 */
5457int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5458 struct sockaddr_storage *saddr,
5459 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005460{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005461 struct quic_dgram *dgram;
5462 unsigned char *dcid;
5463 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005464 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005465
5466 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005467 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005468
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005469 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005470 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005471 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005472
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005473 cid_tid = quic_get_cid_tid(dcid);
5474
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005475 /* All the members must be initialized! */
5476 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005477 dgram->buf = buf;
5478 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005479 dgram->dcid = dcid;
5480 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005481 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005482 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005483 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005484 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005485
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005486 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005487
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005488 return 1;
5489
5490 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005491 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005492}
5493
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005494/* Function to automatically activate QUIC traces on stdout.
5495 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5496 * Main use for now is in the docker image for QUIC interop testing.
5497 */
5498static void quic_init_stdout_traces(void)
5499{
5500#ifdef ENABLE_QUIC_STDOUT_TRACES
5501 trace_quic.sink = sink_find("stdout");
5502 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005503 trace_quic.state = TRACE_STATE_RUNNING;
5504#endif
5505}
5506INITCALL0(STG_INIT, quic_init_stdout_traces);
5507
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005508/*
5509 * Local variables:
5510 * c-indent-level: 8
5511 * c-basic-offset: 8
5512 * End:
5513 */