blob: 253aa85df46747ff4a46da6217c6051723c2c6f6 [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écaillece69cbc2022-03-22 12:45:33 +0100123 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100124 { /* end */ }
125};
126
127static const struct name_desc quic_trace_lockon_args[4] = {
128 /* arg1 */ { /* already used by the connection */ },
129 /* arg2 */ { .name="quic", .desc="QUIC transport" },
130 /* arg3 */ { },
131 /* arg4 */ { }
132};
133
134static const struct name_desc quic_trace_decoding[] = {
135#define QUIC_VERB_CLEAN 1
136 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
137 { /* end */ }
138};
139
140
141struct trace_source trace_quic = {
142 .name = IST("quic"),
143 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100144 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100145 .default_cb = quic_trace,
146 .known_events = quic_trace_events,
147 .lockon_args = quic_trace_lockon_args,
148 .decoding = quic_trace_decoding,
149 .report_events = ~0, /* report everything by default */
150};
151
152#define TRACE_SOURCE &trace_quic
153INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
154
155static BIO_METHOD *ha_quic_meth;
156
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200157DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100158DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100159DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200160 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100161DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162DECLARE_POOL(pool_head_quic_connection_id,
163 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100164DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100165DECLARE_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 +0100166DECLARE_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 +0100167DECLARE_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 +0100168DECLARE_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 +0100169DECLARE_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 +0200170DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100171DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +0200172DECLARE_STATIC_POOL(pool_head_quic_conn_stream, "qc_stream_desc", sizeof(struct qc_stream_desc));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100173
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200174static 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 +0100175 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100176 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécailleb0021452022-03-29 11:42:03 +0200177 int padding, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100178static 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 +0100179static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100180
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100181/* Only for debug purpose */
182struct enc_debug_info {
183 unsigned char *payload;
184 size_t payload_len;
185 unsigned char *aad;
186 size_t aad_len;
187 uint64_t pn;
188};
189
190/* Initializes a enc_debug_info struct (only for debug purpose) */
191static inline void enc_debug_info_init(struct enc_debug_info *edi,
192 unsigned char *payload, size_t payload_len,
193 unsigned char *aad, size_t aad_len, uint64_t pn)
194{
195 edi->payload = payload;
196 edi->payload_len = payload_len;
197 edi->aad = aad;
198 edi->aad_len = aad_len;
199 edi->pn = pn;
200}
201
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100202/* Trace callback for QUIC.
203 * These traces always expect that arg1, if non-null, is of type connection.
204 */
205static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
206 const struct ist where, const struct ist func,
207 const void *a1, const void *a2, const void *a3, const void *a4)
208{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100209 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100210
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100211 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100212 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100213
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100214 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215 if ((mask & QUIC_EV_CONN_INIT) && qc) {
216 chunk_appendf(&trace_buf, "\n odcid");
217 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100218 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100219 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100220 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100221 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100222 }
223
224 if (mask & QUIC_EV_CONN_ADDDATA) {
225 const enum ssl_encryption_level_t *level = a2;
226 const size_t *len = a3;
227
228 if (level) {
229 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
230
231 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
232 }
233 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100234 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100235 }
236 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
237 /* Initial read & write secrets. */
238 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
239 const unsigned char *rx_sec = a2;
240 const unsigned char *tx_sec = a3;
241
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100242 tls_ctx = &qc->els[level].tls_ctx;
243 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100244 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
245 if (rx_sec)
246 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100247 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100248 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
249 if (tx_sec)
250 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100251 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100252 }
253 }
254 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
255 const enum ssl_encryption_level_t *level = a2;
256 const unsigned char *secret = a3;
257 const size_t *secret_len = a4;
258
259 if (level) {
260 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
261
262 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
263 if (secret && secret_len)
264 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100265 tls_ctx = &qc->els[lvl].tls_ctx;
266 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
267 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100268 }
269 }
270
271 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
272 const enum ssl_encryption_level_t *level = a2;
273 const unsigned char *secret = a3;
274 const size_t *secret_len = a4;
275
276 if (level) {
277 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
278
279 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
280 if (secret && secret_len)
281 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100282 tls_ctx = &qc->els[lvl].tls_ctx;
283 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
284 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100285 }
286
287 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100288
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100289 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290 const struct quic_tx_packet *pkt = a2;
291 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100292 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100293
294 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100295 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100296 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100297 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100298 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100299 (unsigned long long)qc->path->cwnd,
300 (unsigned long long)qc->path->prep_in_flight,
301 (unsigned long long)qc->path->in_flight,
302 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100303 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100304 }
305 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200306 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100307 if (pkt->pn_node.key != (uint64_t)-1)
308 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100309 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100310 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100311 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
312 (unsigned long long)qc->rx.bytes,
313 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100314 }
315
316 if (room) {
317 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
318 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
319 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100320 }
321 }
322
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100323 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100324 const enum quic_handshake_state *state = a2;
325 const int *err = a3;
326
327 if (state)
328 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
329 if (err)
330 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
331 }
332
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100333 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100334 const struct quic_rx_packet *pkt = a2;
335 const unsigned long *pktlen = a3;
336 const SSL *ssl = a4;
337
338 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100339 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100340 pkt, quic_packet_type_enc_level_char(pkt->type));
341 if (pkt->pnl)
342 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
343 (unsigned long long)pkt->pn);
344 if (pkt->token_len)
345 chunk_appendf(&trace_buf, " toklen=%llu",
346 (unsigned long long)pkt->token_len);
347 if (pkt->aad_len)
348 chunk_appendf(&trace_buf, " aadlen=%llu",
349 (unsigned long long)pkt->aad_len);
350 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
351 pkt->flags, (unsigned long long)pkt->len);
352 }
353 if (pktlen)
354 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
355 if (ssl) {
356 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
357 chunk_appendf(&trace_buf, " el=%c",
358 quic_enc_level_char(ssl_to_quic_enc_level(level)));
359 }
360 }
361
362 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
363 const struct quic_rx_packet *pkt = a2;
364 const struct quic_rx_crypto_frm *cf = a3;
365 const SSL *ssl = a4;
366
367 if (pkt)
368 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
369 quic_packet_type_enc_level_char(pkt->type),
370 (unsigned long long)pkt->pn);
371 if (cf)
372 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
373 (unsigned long long)cf->offset_node.key,
374 (unsigned long long)cf->len);
375 if (ssl) {
376 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200377 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100378 quic_enc_level_char(ssl_to_quic_enc_level(level)));
379 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100380
381 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100382 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100383 }
384
385 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
386 const struct quic_frame *frm = a2;
387
388 if (frm)
389 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
390 }
391
392 if (mask & QUIC_EV_CONN_PHPKTS) {
393 const struct quic_enc_level *qel = a2;
394
395 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100396 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100397 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100398 " 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 +0100399 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200400 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200401 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100402 (unsigned long long)qc->path->cwnd,
403 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100404 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100405 (unsigned long long)pktns->tx.in_flight,
406 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100407 }
408 }
409
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100410 if (mask & QUIC_EV_CONN_ENCPKT) {
411 const struct enc_debug_info *edi = a2;
412
413 if (edi)
414 chunk_appendf(&trace_buf,
415 " payload=@%p payload_len=%llu"
416 " aad=@%p aad_len=%llu pn=%llu",
417 edi->payload, (unsigned long long)edi->payload_len,
418 edi->aad, (unsigned long long)edi->aad_len,
419 (unsigned long long)edi->pn);
420 }
421
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100422 if (mask & QUIC_EV_CONN_RMHP) {
423 const struct quic_rx_packet *pkt = a2;
424
425 if (pkt) {
426 const int *ret = a3;
427
428 chunk_appendf(&trace_buf, " pkt@%p", pkt);
429 if (ret && *ret)
430 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
431 pkt->pnl, (unsigned long long)pkt->pn);
432 }
433 }
434
435 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200436 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100437 const unsigned long *val1 = a3;
438 const unsigned long *val2 = a4;
439
440 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100441 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100442 if (val1)
443 chunk_appendf(&trace_buf, " %lu", *val1);
444 if (val2)
445 chunk_appendf(&trace_buf, "..%lu", *val2);
446 }
447
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100448 if (mask & QUIC_EV_CONN_ACKSTRM) {
449 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200450 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100451
452 if (s)
453 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200454 if (stream)
455 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100456 }
457
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100458 if (mask & QUIC_EV_CONN_RTTUPDT) {
459 const unsigned int *rtt_sample = a2;
460 const unsigned int *ack_delay = a3;
461 const struct quic_loss *ql = a4;
462
463 if (rtt_sample)
464 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
465 if (ack_delay)
466 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
467 if (ql)
468 chunk_appendf(&trace_buf,
469 " srtt=%ums rttvar=%ums min_rtt=%ums",
470 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
471 }
472 if (mask & QUIC_EV_CONN_CC) {
473 const struct quic_cc_event *ev = a2;
474 const struct quic_cc *cc = a3;
475
476 if (a2)
477 quic_cc_event_trace(&trace_buf, ev);
478 if (a3)
479 quic_cc_state_trace(&trace_buf, cc);
480 }
481
482 if (mask & QUIC_EV_CONN_PKTLOSS) {
483 const struct quic_pktns *pktns = a2;
484 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100485
486 if (pktns) {
487 chunk_appendf(&trace_buf, " pktns=%s",
488 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
489 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
490 if (pktns->tx.loss_time)
491 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100492 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100493 }
494 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
495 struct quic_tx_packet *pkt;
496
497 chunk_appendf(&trace_buf, " lost_pkts:");
498 list_for_each_entry(pkt, lost_pkts, list)
499 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
500 }
501 }
502
503 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100504 const struct quic_pktns *pktns = a2;
505 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100506 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100507
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100508 if (ifae_pkts)
509 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
510 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100511 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100512 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100513 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100514 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
515 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100516 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
517 if (pktns->tx.in_flight)
518 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100519 if (pktns->tx.loss_time)
520 chunk_appendf(&trace_buf, " loss_time=%dms",
521 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
522 }
523 if (mask & QUIC_EV_CONN_SPTO) {
524 if (pktns->tx.time_of_last_eliciting)
525 chunk_appendf(&trace_buf, " tole=%dms",
526 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
527 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100528 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100529 }
530 }
531
532 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
533 chunk_appendf(&trace_buf,
534 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
535 }
536 }
537
538 if (mask & QUIC_EV_CONN_SPPKTS) {
539 const struct quic_tx_packet *pkt = a2;
540
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100541 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
542 (unsigned long long)qc->path->cwnd,
543 (unsigned long long)qc->path->prep_in_flight,
544 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100545 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100546 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100547 (unsigned long)pkt->pn_node.key,
548 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
549 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100550 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100551 }
552 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100553
554 if (mask & QUIC_EV_CONN_SSLALERT) {
555 const uint8_t *alert = a2;
556 const enum ssl_encryption_level_t *level = a3;
557
558 if (alert)
559 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
560 if (level)
561 chunk_appendf(&trace_buf, " el=%c",
562 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
563 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100564
565 if (mask & QUIC_EV_CONN_BCFRMS) {
566 const size_t *sz1 = a2;
567 const size_t *sz2 = a3;
568 const size_t *sz3 = a4;
569
570 if (sz1)
571 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
572 if (sz2)
573 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
574 if (sz3)
575 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
576 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100577
578 if (mask & QUIC_EV_CONN_PSTRM) {
579 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100580
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100581 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100582 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100583 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100584 }
585 if (mask & QUIC_EV_CONN_LPKT) {
586 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200587 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100588
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100589 if (pkt) {
590 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
591 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
592 if (pkt->pn_node.key != (uint64_t)-1)
593 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
594 }
595
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200596 if (len)
597 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598 }
599
600}
601
602/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100603static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100604{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200605 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100606
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100607 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100608 return 1;
609
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200610 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
611 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200612 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
613 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200614 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615 return 1;
616
617 return 0;
618}
619
620/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
621 * both loss detection and PTO and schedule the task assiated to this timer if needed.
622 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100623static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100624{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100625 struct quic_pktns *pktns;
626 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200627 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100628
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100629 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100630 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100631
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100632 pktns = quic_loss_pktns(qc);
633 if (tick_isset(pktns->tx.loss_time)) {
634 qc->timer = pktns->tx.loss_time;
635 goto out;
636 }
637
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100638 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100639 * cancelled for a server which reached the anti-amplification limit.
640 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100641 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200642 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100643 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100644 qc->timer = TICK_ETERNITY;
645 goto out;
646 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100647
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100648 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100649 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100650 /* Timer cancellation. */
651 qc->timer = TICK_ETERNITY;
652 goto out;
653 }
654
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200655 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200656 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100657 if (tick_isset(pto))
658 qc->timer = pto;
659 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100660 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100661 if (tick_is_expired(qc->timer, now_ms)) {
662 TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100663 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100664 }
665 else {
666 TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100667 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100668 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100669 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100670 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100671}
672
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100673/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
674 * connection.
675 * Return 1 if succeeded, 0 if not.
676 */
677static int quic_tls_key_update(struct quic_conn *qc)
678{
679 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
680 struct quic_tls_secrets *rx, *tx;
681 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
682 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
683
684 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
685 rx = &tls_ctx->rx;
686 tx = &tls_ctx->tx;
687 nxt_rx = &qc->ku.nxt_rx;
688 nxt_tx = &qc->ku.nxt_tx;
689
690 /* Prepare new RX secrets */
691 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
692 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100693 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100694 return 0;
695 }
696
697 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
698 nxt_rx->key, nxt_rx->keylen,
699 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
700 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100701 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100702 return 0;
703 }
704
705 /* Prepare new TX secrets */
706 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
707 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100708 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100709 return 0;
710 }
711
712 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
713 nxt_tx->key, nxt_tx->keylen,
714 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
715 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100716 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100717 return 0;
718 }
719
720 return 1;
721}
722
723/* Rotate the Key Update information for <qc> QUIC connection.
724 * Must be used after having updated them.
725 * Always succeeds.
726 */
727static void quic_tls_rotate_keys(struct quic_conn *qc)
728{
729 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
730 unsigned char *curr_secret, *curr_iv, *curr_key;
731
732 /* Rotate the RX secrets */
733 curr_secret = tls_ctx->rx.secret;
734 curr_iv = tls_ctx->rx.iv;
735 curr_key = tls_ctx->rx.key;
736
737 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
738 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
739 tls_ctx->rx.key = qc->ku.nxt_rx.key;
740
741 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
742 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
743 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
744
745 qc->ku.prv_rx.secret = curr_secret;
746 qc->ku.prv_rx.iv = curr_iv;
747 qc->ku.prv_rx.key = curr_key;
748 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
749
750 /* Update the TX secrets */
751 curr_secret = tls_ctx->tx.secret;
752 curr_iv = tls_ctx->tx.iv;
753 curr_key = tls_ctx->tx.key;
754
755 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
756 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
757 tls_ctx->tx.key = qc->ku.nxt_tx.key;
758
759 qc->ku.nxt_tx.secret = curr_secret;
760 qc->ku.nxt_tx.iv = curr_iv;
761 qc->ku.nxt_tx.key = curr_key;
762}
763
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100764#ifndef OPENSSL_IS_BORINGSSL
765int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
766 const uint8_t *read_secret,
767 const uint8_t *write_secret, size_t secret_len)
768{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100769 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
770 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 +0100771 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100772 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100773
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100774 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100775 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200776 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100777 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100778 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100779 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100780
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100781 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100782 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100783 goto err;
784 }
785
786 rx = &tls_ctx->rx;
787 tx = &tls_ctx->tx;
788
789 rx->aead = tx->aead = tls_aead(cipher);
790 rx->md = tx->md = tls_md(cipher);
791 rx->hp = tx->hp = tls_hp(cipher);
792
793 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
794 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100795 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100796 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100797 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100798 }
799
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100800 /* Enqueue this connection asap if we could derive O-RTT secrets as
801 * listener. Note that a listener derives only RX secrets for this
802 * level.
803 */
804 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
805 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100806
807 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100808 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100809
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100810 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
811 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100812 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100813 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100814 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100815 }
816
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100817 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100818 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
819 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
820 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
821
822 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
823 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100824 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100825 goto err;
826 }
827
828 memcpy(rx->secret, read_secret, secret_len);
829 rx->secretlen = secret_len;
830 memcpy(tx->secret, write_secret, secret_len);
831 tx->secretlen = secret_len;
832 /* Initialize all the secret keys lengths */
833 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
834 /* Prepare the next key update */
835 if (!quic_tls_key_update(qc))
836 goto err;
837 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100838
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100839 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100840 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100841 no_secret:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100842 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100843 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100844
845 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100846 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100847 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100848}
849#else
850/* ->set_read_secret callback to derive the RX secrets at <level> encryption
851 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500852 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100853 */
854int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
855 const SSL_CIPHER *cipher,
856 const uint8_t *secret, size_t secret_len)
857{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100858 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100859 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100860 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100861
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100862 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200863 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100864 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100865 goto out;
866 }
867
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100868 tls_ctx->rx.aead = tls_aead(cipher);
869 tls_ctx->rx.md = tls_md(cipher);
870 tls_ctx->rx.hp = tls_hp(cipher);
871
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100872 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
873 goto err;
874
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100875 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 +0100876 tls_ctx->rx.key, tls_ctx->rx.keylen,
877 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100878 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
879 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100880 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100881 goto err;
882 }
883
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100884 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100885 const unsigned char *buf;
886 size_t buflen;
887
888 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
889 if (!buflen)
890 goto err;
891
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100892 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100893 goto err;
894 }
895
896 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100897 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100898 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100899
900 return 1;
901
902 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100903 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100904 return 0;
905}
906
907/* ->set_write_secret callback to derive the TX secrets at <level>
908 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500909 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100910 */
911int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
912 const SSL_CIPHER *cipher,
913 const uint8_t *secret, size_t secret_len)
914{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100915 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
916 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 +0100917
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100918 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200919 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100920 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100921 goto out;
922 }
923
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100924 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
925 goto err;
926
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100927 tls_ctx->tx.aead = tls_aead(cipher);
928 tls_ctx->tx.md = tls_md(cipher);
929 tls_ctx->tx.hp = tls_hp(cipher);
930
931 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 +0100932 tls_ctx->tx.key, tls_ctx->tx.keylen,
933 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100934 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
935 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100936 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100937 goto err;
938 }
939
940 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100941 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100942 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100943 return 1;
944
945 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100946 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100947 return 0;
948}
949#endif
950
951/* This function copies the CRYPTO data provided by the TLS stack found at <data>
952 * with <len> as size in CRYPTO buffers dedicated to store the information about
953 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
954 * It fails only if it could not managed to allocate enough CRYPTO buffers to
955 * store all the data.
956 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
957 */
958static int quic_crypto_data_cpy(struct quic_enc_level *qel,
959 const unsigned char *data, size_t len)
960{
961 struct quic_crypto_buf **qcb;
962 /* The remaining byte to store in CRYPTO buffers. */
963 size_t cf_offset, cf_len, *nb_buf;
964 unsigned char *pos;
965
966 nb_buf = &qel->tx.crypto.nb_buf;
967 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
968 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
969 cf_len = len;
970
971 while (len) {
972 size_t to_copy, room;
973
974 pos = (*qcb)->data + (*qcb)->sz;
975 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
976 to_copy = len > room ? room : len;
977 if (to_copy) {
978 memcpy(pos, data, to_copy);
979 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
980 qel->tx.crypto.sz += to_copy;
981 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100982 len -= to_copy;
983 data += to_copy;
984 }
985 else {
986 struct quic_crypto_buf **tmp;
987
988 tmp = realloc(qel->tx.crypto.bufs,
989 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
990 if (tmp) {
991 qel->tx.crypto.bufs = tmp;
992 qcb = &qel->tx.crypto.bufs[*nb_buf];
993 *qcb = pool_alloc(pool_head_quic_crypto_buf);
994 if (!*qcb)
995 return 0;
996
997 (*qcb)->sz = 0;
998 ++*nb_buf;
999 }
1000 else {
1001 break;
1002 }
1003 }
1004 }
1005
1006 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1007 * have been buffered.
1008 */
1009 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001010 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001011 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001012
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001013 /* There is at most one CRYPTO frame in this packet number
1014 * space. Let's look for it.
1015 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001016 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001017 if (frm->type != QUIC_FT_CRYPTO)
1018 continue;
1019
1020 /* Found */
1021 found = frm;
1022 break;
1023 }
1024
1025 if (found) {
1026 found->crypto.len += cf_len;
1027 }
1028 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001029 frm = pool_alloc(pool_head_quic_frame);
1030 if (!frm)
1031 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001032
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001033 frm->type = QUIC_FT_CRYPTO;
1034 frm->crypto.offset = cf_offset;
1035 frm->crypto.len = cf_len;
1036 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001037 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001038 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001039 }
1040
1041 return len == 0;
1042}
1043
1044
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001045/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1046void quic_set_tls_alert(struct quic_conn *qc, int alert)
1047{
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001048 qc->err_code = QC_ERR_CRYPTO_ERROR | alert;
1049 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001050 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001051}
1052
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001053/* Set the application for <qc> QUIC connection.
1054 * Return 1 if succeeded, 0 if not.
1055 */
1056int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1057{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001058 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1059 qc->app_ops = &h3_ops;
1060 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1061 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001062 else
1063 return 0;
1064
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001065 return 1;
1066}
1067
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001068/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1069 * wants to provide the QUIC layer with CRYPTO data.
1070 * Returns 1 if succeeded, 0 if not.
1071 */
1072int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1073 const uint8_t *data, size_t len)
1074{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001075 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001076 enum quic_tls_enc_level tel;
1077 struct quic_enc_level *qel;
1078
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001079 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1080 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001081 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001082 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001083 goto out;
1084 }
1085
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001086 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001087 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001088 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001089 goto err;
1090 }
1091
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001092 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001093 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001094 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001095 goto err;
1096 }
1097
1098 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001099 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001100
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001101 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001102 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001103 return 1;
1104
1105 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001106 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107 return 0;
1108}
1109
1110int ha_quic_flush_flight(SSL *ssl)
1111{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001112 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001113
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001114 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1115 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001116
1117 return 1;
1118}
1119
1120int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1121{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001122 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001123
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001124 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1125 quic_set_tls_alert(qc, alert);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001126 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001127 return 1;
1128}
1129
1130/* QUIC TLS methods */
1131static SSL_QUIC_METHOD ha_quic_method = {
1132#ifdef OPENSSL_IS_BORINGSSL
1133 .set_read_secret = ha_set_rsec,
1134 .set_write_secret = ha_set_wsec,
1135#else
1136 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1137#endif
1138 .add_handshake_data = ha_quic_add_handshake_data,
1139 .flush_flight = ha_quic_flush_flight,
1140 .send_alert = ha_quic_send_alert,
1141};
1142
1143/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1144 * Returns an error count.
1145 */
1146int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1147{
1148 struct proxy *curproxy = bind_conf->frontend;
1149 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1150 int cfgerr = 0;
1151
1152#if 0
1153 /* XXX Did not manage to use this. */
1154 const char *ciphers =
1155 "TLS_AES_128_GCM_SHA256:"
1156 "TLS_AES_256_GCM_SHA384:"
1157 "TLS_CHACHA20_POLY1305_SHA256:"
1158 "TLS_AES_128_CCM_SHA256";
1159#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001160 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001161 long options =
1162 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1163 SSL_OP_SINGLE_ECDH_USE |
1164 SSL_OP_CIPHER_SERVER_PREFERENCE;
1165 SSL_CTX *ctx;
1166
1167 ctx = SSL_CTX_new(TLS_server_method());
1168 bind_conf->initial_ctx = ctx;
1169
1170 SSL_CTX_set_options(ctx, options);
1171#if 0
1172 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1173 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1174 "for bind '%s' at [%s:%d].\n",
1175 curproxy->id, ciphers,
1176 bind_conf->arg, bind_conf->file, bind_conf->line);
1177 cfgerr++;
1178 }
1179#endif
1180
1181 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1182 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1183 "for bind '%s' at [%s:%d].\n",
1184 curproxy->id, groups,
1185 bind_conf->arg, bind_conf->file, bind_conf->line);
1186 cfgerr++;
1187 }
1188
1189 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1190 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1191 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1192 SSL_CTX_set_default_verify_paths(ctx);
1193
1194#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1195#ifdef OPENSSL_IS_BORINGSSL
1196 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1197 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1198#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1199 if (bind_conf->ssl_conf.early_data) {
1200 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001201 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001202 }
1203 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1204 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1205#else
1206 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1207#endif
1208 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1209#endif
1210 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1211
1212 return cfgerr;
1213}
1214
1215/* Decode an expected packet number from <truncated_on> its truncated value,
1216 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1217 * the number of bits used to encode this packet number (its length in bytes * 8).
1218 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1219 */
1220static uint64_t decode_packet_number(uint64_t largest_pn,
1221 uint32_t truncated_pn, unsigned int pn_nbits)
1222{
1223 uint64_t expected_pn = largest_pn + 1;
1224 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1225 uint64_t pn_hwin = pn_win / 2;
1226 uint64_t pn_mask = pn_win - 1;
1227 uint64_t candidate_pn;
1228
1229
1230 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1231 /* Note that <pn_win> > <pn_hwin>. */
1232 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1233 candidate_pn + pn_hwin <= expected_pn)
1234 return candidate_pn + pn_win;
1235
1236 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1237 return candidate_pn - pn_win;
1238
1239 return candidate_pn;
1240}
1241
1242/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1243 * cryptographic context.
1244 * <largest_pn> is the largest received packet number and <pn> the address of
1245 * the packet number field for this packet with <byte0> address of its first byte.
1246 * <end> points to one byte past the end of this packet.
1247 * Returns 1 if succeeded, 0 if not.
1248 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001249static int qc_do_rm_hp(struct quic_conn *qc,
1250 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001251 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001252 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001253{
1254 int ret, outlen, i, pnlen;
1255 uint64_t packet_number;
1256 uint32_t truncated_pn = 0;
1257 unsigned char mask[5] = {0};
1258 unsigned char *sample;
1259 EVP_CIPHER_CTX *cctx;
1260 unsigned char *hp_key;
1261
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001262 /* Check there is enough data in this packet. */
1263 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001264 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265 return 0;
1266 }
1267
1268 cctx = EVP_CIPHER_CTX_new();
1269 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001270 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001271 return 0;
1272 }
1273
1274 ret = 0;
1275 sample = pn + QUIC_PACKET_PN_MAXLEN;
1276
1277 hp_key = tls_ctx->rx.hp_key;
1278 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1279 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1280 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001281 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001282 goto out;
1283 }
1284
1285 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1286 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1287 for (i = 0; i < pnlen; i++) {
1288 pn[i] ^= mask[i + 1];
1289 truncated_pn = (truncated_pn << 8) | pn[i];
1290 }
1291
1292 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1293 /* Store remaining information for this unprotected header */
1294 pkt->pn = packet_number;
1295 pkt->pnl = pnlen;
1296
1297 ret = 1;
1298
1299 out:
1300 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001301
1302 return ret;
1303}
1304
1305/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1306 * address, with <payload_len> as payload length, <aad> as address of
1307 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1308 * context.
1309 * Returns 1 if succeeded, 0 if not.
1310 */
1311static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1312 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001313 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001314{
1315 unsigned char iv[12];
1316 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001317 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001318 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001319
1320 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001321 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001322 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001323 }
1324
1325 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1326 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001327 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001328 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001329 }
1330
1331 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001332
1333 err:
1334 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001335 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001336 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001337}
1338
1339/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1340 * Returns 1 if succeeded, 0 if not.
1341 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001342static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001343{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001344 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001345 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001346 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001347 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001348 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1349 unsigned char *rx_key = tls_ctx->rx.key;
1350
1351 kp_changed = 0;
1352 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1353 /* The two tested bits are not at the same position,
1354 * this is why they are first both inversed.
1355 */
1356 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1357 if (pkt->pn < tls_ctx->rx.pn) {
1358 /* The lowest packet number of a previous key phase
1359 * cannot be null if it really stores previous key phase
1360 * secrets.
1361 */
1362 if (!pkt->qc->ku.prv_rx.pn)
1363 return 0;
1364
1365 rx_iv = pkt->qc->ku.prv_rx.iv;
1366 rx_key = pkt->qc->ku.prv_rx.key;
1367 }
1368 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1369 /* Next key phase */
1370 kp_changed = 1;
1371 rx_iv = pkt->qc->ku.nxt_rx.iv;
1372 rx_key = pkt->qc->ku.nxt_rx.key;
1373 }
1374 }
1375 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001376
1377 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1378 return 0;
1379
1380 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1381 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001382 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001383 if (!ret)
1384 return 0;
1385
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001386 /* Update the keys only if the packet decryption succeeded. */
1387 if (kp_changed) {
1388 quic_tls_rotate_keys(pkt->qc);
1389 /* Toggle the Key Phase bit */
1390 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1391 /* Store the lowest packet number received for the current key phase */
1392 tls_ctx->rx.pn = pkt->pn;
1393 /* Prepare the next key update */
1394 if (!quic_tls_key_update(pkt->qc))
1395 return 0;
1396 }
1397
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001398 /* Update the packet length (required to parse the frames). */
1399 pkt->len = pkt->aad_len + ret;
1400
1401 return 1;
1402}
1403
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02001404/* Free the stream descriptor <stream> buffer. This function should be used
1405 * when all its data have been acknowledged. If the stream was released by the
1406 * upper layer, the stream descriptor will be freed.
1407 *
1408 * Returns 0 if the stream was not freed else non-zero.
1409 */
1410static int qc_stream_desc_free(struct qc_stream_desc *stream)
1411{
1412 b_free(&stream->buf);
1413 offer_buffers(NULL, 1);
1414
1415 if (stream->release) {
1416 eb64_delete(&stream->by_id);
1417 pool_free(pool_head_quic_conn_stream, stream);
1418
1419 return 1;
1420 }
1421
1422 return 0;
1423}
1424
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001425/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001426 *
1427 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001428 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001429static int quic_stream_try_to_consume(struct quic_conn *qc,
1430 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001431{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001432 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001433 struct eb64_node *frm_node;
1434
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001435 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001436 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001437 while (frm_node) {
1438 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001439 struct quic_frame *frm;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001440
1441 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001442 if (strm->offset.key > stream->ack_offset)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001443 break;
1444
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +01001445 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001446 qc, strm, stream);
1447
1448 if (strm->offset.key + strm->len > stream->ack_offset) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001449 const size_t diff = strm->offset.key + strm->len -
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001450 stream->ack_offset;
1451 stream->ack_offset += diff;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001452 b_del(strm->buf, diff);
1453 ret = 1;
1454 }
1455
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001456 frm_node = eb64_next(frm_node);
1457 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001458
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001459 frm = container_of(strm, struct quic_frame, stream);
1460 LIST_DELETE(&frm->list);
1461 quic_tx_packet_refdec(frm->pkt);
1462 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001463 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001464
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001465 if (!b_data(&stream->buf))
1466 qc_stream_desc_free(stream);
1467
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001468 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001469}
1470
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001471/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001472static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1473 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001474{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001475 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001476
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001477 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001478 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001479 switch (frm->type) {
1480 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1481 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001482 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001483 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001484 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001485
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001486 /* do not use strm_frm->stream as the qc_stream_desc instance
1487 * might be freed at this stage. Use the id to do a proper
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001488 * lookup. First search in the MUX then in the released stream
1489 * list.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001490 *
1491 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001492 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001493 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001494 if (qc->mux_state == QC_MUX_READY)
1495 stream = qcc_get_stream(qc->qcc, strm_frm->id);
1496 if (!stream) {
1497 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1498 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1499 }
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001500
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001501 if (!stream) {
1502 TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001503 LIST_DELETE(&frm->list);
1504 quic_tx_packet_refdec(frm->pkt);
1505 pool_free(pool_head_quic_frame, frm);
1506
1507 /* early return */
1508 return;
1509 }
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001510
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001511 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
1512 if (strm_frm->offset.key <= stream->ack_offset) {
1513 if (strm_frm->offset.key + strm_frm->len > stream->ack_offset) {
1514 const size_t diff = strm_frm->offset.key + strm_frm->len -
1515 stream->ack_offset;
1516 stream->ack_offset += diff;
1517 b_del(strm_frm->buf, diff);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001518 stream_acked = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001519
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001520 if (!b_data(strm_frm->buf)) {
1521 if (qc_stream_desc_free(stream)) {
1522 /* early return */
1523 return;
1524 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001525 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001526 }
1527
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +01001528 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001529 qc, strm_frm, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001530 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001531 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001532 pool_free(pool_head_quic_frame, frm);
1533 }
1534 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001535 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001536 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001537
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001538 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001539 }
1540 break;
1541 default:
1542 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001543 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001544 pool_free(pool_head_quic_frame, frm);
1545 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001546
1547 if (stream_acked) {
1548 struct qcc *qcc = qc->qcc;
1549
1550 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1551 tasklet_wakeup(qcc->subs->tasklet);
1552 qcc->subs->events &= ~SUB_RETRY_SEND;
1553 if (!qcc->subs->events)
1554 qcc->subs = NULL;
1555 }
1556 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001557}
1558
1559/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1560 * deallocating them, and their TX frames.
1561 * Returns the last node reached to be used for the next range.
1562 * May be NULL if <largest> node could not be found.
1563 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001564static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1565 struct eb_root *pkts,
1566 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001567 struct list *newly_acked_pkts,
1568 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001569 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001570{
1571 struct eb64_node *node;
1572 struct quic_tx_packet *pkt;
1573
1574 if (largest_node)
1575 node = largest_node;
1576 else {
1577 node = eb64_lookup(pkts, largest);
1578 while (!node && largest > smallest) {
1579 node = eb64_lookup(pkts, --largest);
1580 }
1581 }
1582
1583 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001584 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001585
1586 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1587 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001588 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001589 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001590 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001591 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001592 node = eb64_prev(node);
1593 eb64_delete(&pkt->pn_node);
1594 }
1595
1596 return node;
1597}
1598
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001599/* Remove all frames from <pkt_frm_list> and reinsert them in the
1600 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001601 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001602static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1603 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001604 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001605{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001606 struct quic_frame *frm, *frmbak;
1607 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001608
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001609 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1610 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001611 quic_tx_packet_refdec(frm->pkt);
1612 /* This frame is not freed but removed from its packet */
1613 frm->pkt = NULL;
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001614 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001615 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001616 }
1617
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001618 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001619}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001620
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001621/* Free <pkt> TX packet and its attached frames.
1622 * This is the responsability of the caller to remove this packet of
1623 * any data structure it was possibly attached to.
1624 */
1625static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1626{
1627 struct quic_frame *frm, *frmbak;
1628
1629 if (!pkt)
1630 return;
1631
1632 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1633 LIST_DELETE(&frm->list);
1634 pool_free(pool_head_quic_frame, frm);
1635 }
1636 pool_free(pool_head_quic_tx_packet, pkt);
1637}
1638
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001639/* Free the TX packets of <pkts> list */
1640static inline void free_quic_tx_pkts(struct list *pkts)
1641{
1642 struct quic_tx_packet *pkt, *tmp;
1643
1644 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001645 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001646 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001647 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001648 }
1649}
1650
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001651/* Remove already sent ranges of acknowledged packet numbers from
1652 * <pktns> packet number space tree below <largest_acked_pn> possibly
1653 * updating the range which contains <largest_acked_pn>.
1654 * Never fails.
1655 */
1656static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1657 int64_t largest_acked_pn)
1658{
1659 struct eb64_node *ar, *next_ar;
1660 struct quic_arngs *arngs = &pktns->rx.arngs;
1661
1662 ar = eb64_first(&arngs->root);
1663 while (ar) {
1664 struct quic_arng_node *ar_node;
1665
1666 next_ar = eb64_next(ar);
1667 ar_node = eb64_entry(&ar->node, struct quic_arng_node, first);
1668 if ((int64_t)ar_node->first.key > largest_acked_pn)
1669 break;
1670
1671 if (largest_acked_pn < ar_node->last) {
1672 eb64_delete(ar);
1673 ar_node->first.key = largest_acked_pn + 1;
1674 eb64_insert(&arngs->root, ar);
1675 break;
1676 }
1677
1678 eb64_delete(ar);
1679 pool_free(pool_head_quic_arng, ar_node);
1680 arngs->sz--;
1681 ar = next_ar;
1682 }
1683}
1684
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001685/* Send a packet ack event nofication for each newly acked packet of
1686 * <newly_acked_pkts> list and free them.
1687 * Always succeeds.
1688 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001689static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001690 struct list *newly_acked_pkts)
1691{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001692 struct quic_tx_packet *pkt, *tmp;
1693 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1694
1695 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1696 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001697 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001698 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001699 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001700 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001701 /* If this packet contained an ACK frame, proceed to the
1702 * acknowledging of range of acks from the largest acknowledged
1703 * packet number which was sent in an ACK frame by this packet.
1704 */
1705 if (pkt->largest_acked_pn != -1)
1706 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001707 ev.ack.acked = pkt->in_flight_len;
1708 ev.ack.time_sent = pkt->time_sent;
1709 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001710 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001711 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001712 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001713 }
1714
1715}
1716
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001717/* Release all the frames attached to <pktns> packet number space */
1718static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1719{
1720 struct quic_frame *frm, *frmbak;
1721
1722 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1723 LIST_DELETE(&frm->list);
1724 pool_free(pool_head_quic_frame, frm);
1725 }
1726}
1727
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001728/* Handle <pkts> list of lost packets detected at <now_us> handling
1729 * their TX frames.
1730 * Send a packet loss event to the congestion controller if
1731 * in flight packet have been lost.
1732 * Also frees the packet in <pkts> list.
1733 * Never fails.
1734 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001735static inline void qc_release_lost_pkts(struct quic_conn *qc,
1736 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001737 struct list *pkts,
1738 uint64_t now_us)
1739{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001740 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001741 uint64_t lost_bytes;
1742
1743 lost_bytes = 0;
1744 oldest_lost = newest_lost = NULL;
1745 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001746 struct list tmp = LIST_HEAD_INIT(tmp);
1747
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001748 lost_bytes += pkt->in_flight_len;
1749 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001750 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001751 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001752 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001753 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001754 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001755 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001756 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001757 if (!oldest_lost) {
1758 oldest_lost = newest_lost = pkt;
1759 }
1760 else {
1761 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001762 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001763 newest_lost = pkt;
1764 }
1765 }
1766
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001767 if (newest_lost) {
1768 /* Sent a congestion event to the controller */
1769 struct quic_cc_event ev = {
1770 .type = QUIC_CC_EVT_LOSS,
1771 .loss.time_sent = newest_lost->time_sent,
1772 };
1773
1774 quic_cc_event(&qc->path->cc, &ev);
1775 }
1776
1777 /* If an RTT have been already sampled, <rtt_min> has been set.
1778 * We must check if we are experiencing a persistent congestion.
1779 * If this is the case, the congestion controller must re-enter
1780 * slow start state.
1781 */
1782 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1783 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1784
1785 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1786 now_ms, qc->max_ack_delay))
1787 qc->path->cc.algo->slow_start(&qc->path->cc);
1788 }
1789
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001790 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001791 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001792 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001793 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001794 }
1795}
1796
1797/* Look for packet loss from sent packets for <qel> encryption level of a
1798 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1799 * their tree if deemed as lost or set the <loss_time> value the packet number
1800 * space if any not deemed lost.
1801 * Should be called after having received an ACK frame with newly acknowledged
1802 * packets or when the the loss detection timer has expired.
1803 * Always succeeds.
1804 */
1805static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1806 struct quic_conn *qc,
1807 struct list *lost_pkts)
1808{
1809 struct eb_root *pkts;
1810 struct eb64_node *node;
1811 struct quic_loss *ql;
1812 unsigned int loss_delay;
1813
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001814 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001815 pkts = &pktns->tx.pkts;
1816 pktns->tx.loss_time = TICK_ETERNITY;
1817 if (eb_is_empty(pkts))
1818 goto out;
1819
1820 ql = &qc->path->loss;
1821 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001822 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1823
1824 node = eb64_first(pkts);
1825 while (node) {
1826 struct quic_tx_packet *pkt;
1827 int64_t largest_acked_pn;
1828 unsigned int loss_time_limit, time_sent;
1829
1830 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001831 largest_acked_pn = pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001832 node = eb64_next(node);
1833 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1834 break;
1835
1836 time_sent = pkt->time_sent;
1837 loss_time_limit = tick_add(time_sent, loss_delay);
1838 if (tick_is_le(time_sent, now_ms) ||
1839 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1840 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001841 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001842 }
1843 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001844 if (tick_isset(pktns->tx.loss_time))
1845 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1846 else
1847 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001848 }
1849 }
1850
1851 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001852 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001853}
1854
1855/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1856 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001857 * 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 +01001858 * acked ack-eliciting packet.
1859 * Return 1, if succeeded, 0 if not.
1860 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001861static inline int qc_parse_ack_frm(struct quic_conn *qc,
1862 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001863 struct quic_enc_level *qel,
1864 unsigned int *rtt_sample,
1865 const unsigned char **pos, const unsigned char *end)
1866{
1867 struct quic_ack *ack = &frm->ack;
1868 uint64_t smallest, largest;
1869 struct eb_root *pkts;
1870 struct eb64_node *largest_node;
1871 unsigned int time_sent, pkt_flags;
1872 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1873 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1874
1875 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1876 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001877 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001878 goto err;
1879 }
1880
1881 if (ack->first_ack_range > ack->largest_ack) {
1882 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001883 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001884 goto err;
1885 }
1886
1887 largest = ack->largest_ack;
1888 smallest = largest - ack->first_ack_range;
1889 pkts = &qel->pktns->tx.pkts;
1890 pkt_flags = 0;
1891 largest_node = NULL;
1892 time_sent = 0;
1893
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001894 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001895 largest_node = eb64_lookup(pkts, largest);
1896 if (!largest_node) {
1897 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001898 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001899 }
1900 else {
1901 time_sent = eb64_entry(&largest_node->node,
1902 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001903 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001904 }
1905
1906 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001907 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001908 do {
1909 uint64_t gap, ack_range;
1910
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001911 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1912 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001913 if (!ack->ack_range_num--)
1914 break;
1915
1916 if (!quic_dec_int(&gap, pos, end))
1917 goto err;
1918
1919 if (smallest < gap + 2) {
1920 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001921 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001922 goto err;
1923 }
1924
1925 largest = smallest - gap - 2;
1926 if (!quic_dec_int(&ack_range, pos, end))
1927 goto err;
1928
1929 if (largest < ack_range) {
1930 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001931 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001932 goto err;
1933 }
1934
1935 /* Do not use this node anymore. */
1936 largest_node = NULL;
1937 /* Next range */
1938 smallest = largest - ack_range;
1939
1940 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001941 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001942 } while (1);
1943
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1945 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001946 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001947 }
1948
1949 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1950 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001951 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001952 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001953 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001954 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001955 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1956 if (quic_peer_validated_addr(qc))
1957 qc->path->loss.pto_count = 0;
1958 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001959 }
1960
1961
1962 return 1;
1963
1964 err:
1965 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001966 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001967 return 0;
1968}
1969
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001970/* This function gives the detail of the SSL error. It is used only
1971 * if the debug mode and the verbose mode are activated. It dump all
1972 * the SSL error until the stack was empty.
1973 */
1974static forceinline void qc_ssl_dump_errors(struct connection *conn)
1975{
1976 if (unlikely(global.mode & MODE_DEBUG)) {
1977 while (1) {
1978 unsigned long ret;
1979
1980 ret = ERR_get_error();
1981 if (!ret)
1982 return;
1983
1984 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1985 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1986 }
1987 }
1988}
1989
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001990int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1991 const char **str, int *len);
1992
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001993/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1994 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001995 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001996 * Return 1 if succeeded, 0 if not.
1997 */
1998static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001999 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002000 const unsigned char *data, size_t len,
2001 struct quic_rx_packet *pkt,
2002 struct quic_rx_crypto_frm *cf)
2003{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002004 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002005 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002006
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002007 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002008 qc = ctx->qc;
2009
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002010 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2011
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002012 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2013 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002014 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002015 goto err;
2016 }
2017
2018 el->rx.crypto.offset += len;
2019 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002020 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002021
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002022 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002023 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002024 ssl_err = SSL_do_handshake(ctx->ssl);
2025 if (ssl_err != 1) {
2026 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2027 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2028 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002029 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002030 goto out;
2031 }
2032
2033 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002034 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002035 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002036 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002037 goto err;
2038 }
2039
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002040 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
2041 /* I/O callback switch */
2042 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002043 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002044 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002045 /* The connection is ready to be accepted. */
2046 quic_accept_push_qc(qc);
2047 }
2048 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002049 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002050 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002051 } else {
2052 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2053 if (ssl_err != 1) {
2054 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2055 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2056 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002057 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002058 goto out;
2059 }
2060
2061 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002062 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002063 goto err;
2064 }
2065
2066 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002067 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002068 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002069
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002070 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002071 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002072 return 1;
2073
2074 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002075 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002076 return 0;
2077}
2078
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002079/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
2080 * <pkt> RX packet.
2081 * Return it if succeeded, NULL if not.
2082 */
2083static inline
2084struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
2085 struct quic_rx_packet *pkt)
2086{
2087 struct quic_rx_strm_frm *frm;
2088
2089 frm = pool_alloc(pool_head_quic_rx_strm_frm);
2090 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002091 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002092 frm->len = stream_frm->len;
2093 frm->data = stream_frm->data;
2094 frm->pkt = pkt;
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002095 frm->fin = stream_frm->fin;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002096 }
2097
2098 return frm;
2099}
2100
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002101/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01002102 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002103 */
2104static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
2105{
2106 size_t ret;
2107
Amaury Denoyelle2d2d0302022-02-28 10:00:54 +01002108 ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len);
2109 strm_frm->len -= ret;
2110 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002111
2112 return ret;
2113}
2114
2115/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2116 * streams may be open. The data are copied to the stream RX buffer if possible.
2117 * If not, the STREAM frame is stored to be treated again later.
2118 * We rely on the flow control so that not to store too much STREAM frames.
2119 * Return 1 if succeeded, 0 if not.
2120 */
2121static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2122 struct quic_stream *strm_frm,
2123 struct quic_conn *qc)
2124{
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002125 struct quic_rx_strm_frm *frm;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002126 struct eb64_node *frm_node;
2127 struct qcs *qcs = NULL;
2128 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002129
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002130 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2131 strm_frm->offset.key, strm_frm->fin,
2132 (char *)strm_frm->data, &qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002133
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002134 /* invalid or already received frame */
2135 if (ret == 1)
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002136 return 1;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002137
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002138 if (ret == 2) {
2139 /* frame cannot be parsed at the moment and should be
2140 * buffered.
2141 */
2142 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2143 if (!frm) {
2144 TRACE_PROTO("Could not alloc RX STREAM frame",
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002145 QUIC_EV_CONN_PSTRM, qc);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002146 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002147 }
2148
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002149 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2150 quic_rx_packet_refinc(pkt);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002151
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002152 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002153 }
2154
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002155 /* Frame correctly received by the mux.
2156 * If there is buffered frame for next offset, it may be possible to
2157 * receive them now.
2158 */
2159 frm_node = eb64_first(&qcs->rx.frms);
2160 while (frm_node) {
2161 frm = eb64_entry(&frm_node->node,
2162 struct quic_rx_strm_frm, offset_node);
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002163
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02002164 ret = qcc_recv(qc->qcc, qcs->id, frm->len,
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002165 frm->offset_node.key, frm->fin,
2166 (char *)frm->data, &qcs);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002167
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002168 /* interrupt the parsing if the frame cannot be handled for the
2169 * moment only by the MUX.
2170 */
2171 if (ret == 2)
2172 break;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002173
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002174 /* Remove a newly received frame or an invalid one. */
2175 frm_node = eb64_next(frm_node);
2176 eb64_delete(&frm->offset_node);
2177 quic_rx_packet_refdec(frm->pkt);
2178 pool_free(pool_head_quic_rx_strm_frm, frm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002179 }
2180
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002181 /* Decode the received data. */
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002182 qcc_decode_qcs(qc->qcc, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002183
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002184 return 1;
2185}
2186
2187/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2188 * streams may be open. The data are copied to the stream RX buffer if possible.
2189 * If not, the STREAM frame is stored to be treated again later.
2190 * We rely on the flow control so that not to store too much STREAM frames.
2191 * Return 1 if succeeded, 0 if not.
2192 */
2193static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2194 struct quic_stream *strm_frm,
2195 struct quic_conn *qc)
2196{
2197 struct qcs *strm;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002198 struct quic_rx_strm_frm *frm;
2199 size_t strm_frm_len;
2200
Amaury Denoyelle50742292022-03-29 14:57:19 +02002201 strm = qcc_get_qcs(qc->qcc, strm_frm->id);
2202 if (!strm) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002203 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002204 return 0;
2205 }
2206
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002207 if (strm_frm->offset.key < strm->rx.offset) {
2208 size_t diff;
2209
2210 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2211 TRACE_PROTO("Already received STREAM data",
2212 QUIC_EV_CONN_PSTRM, qc);
2213 goto out;
2214 }
2215
2216 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2217 diff = strm->rx.offset - strm_frm->offset.key;
2218 strm_frm->offset.key = strm->rx.offset;
2219 strm_frm->len -= diff;
2220 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002221 }
2222
2223 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002224 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002225 int ret;
2226
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002227 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002228 goto store_frm;
2229
2230 /* qc_strm_cpy() will modify the offset, depending on the number
2231 * of bytes copied.
2232 */
2233 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2234 /* Inform the application of the arrival of this new stream */
2235 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002236 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002237 return 0;
2238 }
2239
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002240 if (ret)
2241 qcs_notify_recv(strm);
2242
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002243 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002244 }
2245 /* Take this frame into an account for the stream flow control */
2246 strm->rx.offset += strm_frm_len;
2247 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002248 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002249 */
2250 if (!strm_frm->len)
2251 goto out;
2252
2253 store_frm:
2254 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2255 if (!frm) {
2256 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002257 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002258 return 0;
2259 }
2260
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002261 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002262 quic_rx_packet_refinc(pkt);
2263
2264 out:
2265 return 1;
2266}
2267
2268static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2269 struct quic_stream *strm_frm,
2270 struct quic_conn *qc)
2271{
2272 if (strm_frm->id & QCS_ID_DIR_BIT)
2273 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2274 else
2275 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2276}
2277
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002278/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002279static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2280 struct quic_conn *qc)
2281{
2282 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002283 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002284 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002285
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002286 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002287 pkts = &qel->pktns->tx.pkts;
2288 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002289 /* Skip the empty packet (they have already been retransmitted) */
2290 while (node) {
2291 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2292 if (!LIST_ISEMPTY(&pkt->frms))
2293 break;
2294 node = eb64_next(node);
2295 }
2296
2297 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002298 return;
2299
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002300 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002301}
2302
2303/* Prepare a fast retransmission during handshake after a client
2304 * has resent Initial packets. According to the RFC a server may retransmit
2305 * up to two datagrams of Initial packets if did not receive all Initial packets
2306 * and resend them coalescing with others (Handshake here).
2307 * (Listener only).
2308 */
2309static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2310{
2311 struct list itmp = LIST_HEAD_INIT(itmp);
2312 struct list htmp = LIST_HEAD_INIT(htmp);
2313 struct quic_frame *frm, *frmbak;
2314
2315 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2316 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2317 struct quic_enc_level *qel = iqel;
2318 struct eb_root *pkts;
2319 struct eb64_node *node;
2320 struct quic_tx_packet *pkt;
2321 struct list *tmp = &itmp;
2322
2323 start:
2324 pkt = NULL;
2325 pkts = &qel->pktns->tx.pkts;
2326 node = eb64_first(pkts);
2327 /* Skip the empty packet (they have already been retransmitted) */
2328 while (node) {
2329 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2330 if (!LIST_ISEMPTY(&pkt->frms))
2331 break;
2332 node = eb64_next(node);
2333 }
2334
2335 if (!pkt)
2336 goto end;
2337
2338 qel->pktns->tx.pto_probe += 1;
2339 requeue:
2340 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2341 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2342 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01002343 frm->pkt = NULL;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002344 LIST_APPEND(tmp, &frm->list);
2345 }
2346
2347 if (qel == iqel) {
2348 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2349 pkt = pkt->next;
2350 tmp = &htmp;
2351 hqel->pktns->tx.pto_probe += 1;
2352 goto requeue;
2353 }
2354
2355 qel = hqel;
2356 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002357 goto start;
2358 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002359
2360 end:
2361 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2362 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002363}
2364
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002365/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2366 * as I/O handler context and <qel> as encryption level.
2367 * Returns 1 if succeeded, 0 if failed.
2368 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002369static 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 +01002370 struct quic_enc_level *qel)
2371{
2372 struct quic_frame frm;
2373 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002374 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002375 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002376
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002377 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002378 /* Skip the AAD */
2379 pos = pkt->data + pkt->aad_len;
2380 end = pkt->data + pkt->len;
2381
2382 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002383 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002384 goto err;
2385
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002386 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002387 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002388 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002389 break;
2390 case QUIC_FT_PING:
2391 break;
2392 case QUIC_FT_ACK:
2393 {
2394 unsigned int rtt_sample;
2395
2396 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002397 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002398 goto err;
2399
2400 if (rtt_sample) {
2401 unsigned int ack_delay;
2402
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002403 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002404 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002405 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2406 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002407 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002408 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002409 break;
2410 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002411 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002412 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002413 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002414 {
2415 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002416
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002417 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002418 /* XXX TO DO: <cfdebug> is used only for the traces. */
2419 struct quic_rx_crypto_frm cfdebug = { };
2420
2421 cfdebug.offset_node.key = frm.crypto.offset;
2422 cfdebug.len = frm.crypto.len;
2423 TRACE_PROTO("CRYPTO data discarded",
2424 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2425 break;
2426 }
2427
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002428 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2429 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2430 /* XXX TO DO: <cfdebug> is used only for the traces. */
2431 struct quic_rx_crypto_frm cfdebug = { };
2432
2433 cfdebug.offset_node.key = frm.crypto.offset;
2434 cfdebug.len = frm.crypto.len;
2435 /* Nothing to do */
2436 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002437 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002438 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002439 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2440 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002441 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002442 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002443 else {
2444 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2445 /* XXX TO DO: <cfdebug> is used only for the traces. */
2446 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002447
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002448 cfdebug.offset_node.key = frm.crypto.offset;
2449 cfdebug.len = frm.crypto.len;
2450 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002451 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002452 frm.crypto.len -= diff;
2453 frm.crypto.data += diff;
2454 frm.crypto.offset = qel->rx.crypto.offset;
2455 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002456 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002457
2458 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002459 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002460 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002461
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002462 cfdebug.offset_node.key = frm.crypto.offset;
2463 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002464 if (!qc_provide_cdata(qel, ctx,
2465 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002466 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002467 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002468
2469 break;
2470 }
2471
2472 /* frm.crypto.offset > qel->rx.crypto.offset */
2473 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2474 if (!cf) {
2475 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002476 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002477 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002478 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002479
2480 cf->offset_node.key = frm.crypto.offset;
2481 cf->len = frm.crypto.len;
2482 cf->data = frm.crypto.data;
2483 cf->pkt = pkt;
2484 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2485 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002486 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002487 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002488 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002489 {
2490 struct quic_stream *stream = &frm.stream;
2491
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002492 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002493 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2494 goto err;
2495 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2496 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002497
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002498 /* At the application layer the connection may have already been closed. */
2499 if (qc->mux_state != QC_MUX_READY)
2500 break;
2501
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002502 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002503 goto err;
2504
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002505 break;
2506 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002507 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002508 if (qc->mux_state == QC_MUX_READY) {
2509 struct quic_max_data *data = &frm.max_data;
2510 qcc_recv_max_data(qc->qcc, data->max_data);
2511 }
2512 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002513 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002514 if (qc->mux_state == QC_MUX_READY) {
2515 struct quic_max_stream_data *data = &frm.max_stream_data;
2516 qcc_recv_max_stream_data(qc->qcc, data->id,
2517 data->max_stream_data);
2518 }
2519 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002520 case QUIC_FT_MAX_STREAMS_BIDI:
2521 case QUIC_FT_MAX_STREAMS_UNI:
2522 case QUIC_FT_DATA_BLOCKED:
2523 case QUIC_FT_STREAM_DATA_BLOCKED:
2524 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2525 case QUIC_FT_STREAMS_BLOCKED_UNI:
2526 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002527 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002528 case QUIC_FT_RETIRE_CONNECTION_ID:
2529 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002530 break;
2531 case QUIC_FT_CONNECTION_CLOSE:
2532 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002533 /* warn the mux to close the connection */
Amaury Denoyelle2d0f8732022-03-03 18:04:24 +01002534 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelle4af65952022-02-15 11:06:15 +01002535 qc->qcc->flags |= QC_CF_CC_RECV;
Amaury Denoyelle2d0f8732022-03-03 18:04:24 +01002536 tasklet_wakeup(qc->qcc->wait_event.tasklet);
2537 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002538 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002539 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002540 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002541 goto err;
2542
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002543 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002544 break;
2545 default:
2546 goto err;
2547 }
2548 }
2549
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002550 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002551 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002552
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002553 if (fast_retrans)
2554 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002555
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002556 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2557 * has successfully parse a Handshake packet. The Initial encryption must also
2558 * be discarded.
2559 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002560 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002561 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002562 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002563 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002564 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002565 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002566 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002567 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002568 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2569 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002570 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002571 }
2572
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002573 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002574 return 1;
2575
2576 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002577 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002578 return 0;
2579}
2580
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002581/* Must be called only by a <cbuf> writer (packet builder).
2582 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2583 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2584 * index after having marked the end of written data. This the responsability
2585 * of the caller to ensure there is enough room in <cbuf> to write the end of
2586 * data made of a uint16_t null field.
2587 *
2588 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2589 * ^ ^
2590 * r w
2591 *
2592 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2593 * ^ ^
2594 * r w
2595
2596 * +--------------------------------------+ (empty buffer, can be reused)
2597 * ^
2598 * (r = w)
2599 *
2600 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2601 * ^ ^
2602 * w r
2603 */
2604static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2605{
2606 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2607
2608 /* We can reset the writer index only if in front of the reader index and
2609 * if the reader index is not null. Resetting the writer when the reader
2610 * index is null would empty the buffer.
2611 * XXX Note than the writer index cannot reach the reader index.
2612 * Only the reader index can reach the writer index.
2613 */
2614 if (rd && rd <= cbuf->wr) {
2615 /* Mark the end of contiguous data for the reader */
2616 write_u16(cb_wr(cbuf), 0);
2617 cb_add(cbuf, sizeof(uint16_t));
2618 cb_wr_reset(cbuf);
2619 return 1;
2620 }
2621
2622 return 0;
2623}
2624
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002625/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002626 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002627 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2628 * This function must be called only after having built a correct datagram.
2629 * Always succeeds.
2630 */
2631static inline void qc_set_dg(struct cbuf *cbuf,
2632 uint16_t dglen, struct quic_tx_packet *pkt)
2633{
2634 write_u16(cb_wr(cbuf), dglen);
2635 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2636 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2637}
2638
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002639/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2640 * with <frms> as ack-eliciting frame list to send, 0 if not.
2641 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2642 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
2643 */
2644static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
2645 struct quic_enc_level *qel, int cc, int probe)
2646{
2647 unsigned int must_ack =
2648 qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK;
2649
2650 /* Do not build any more packet if the TX secrets are not available or
2651 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2652 * and if there is no more packets to send upon PTO expiration
2653 * and if there is no more ack-eliciting frames to send or in flight
2654 * congestion control limit is reached for prepared data
2655 */
2656 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2657 (!cc && !probe && !must_ack &&
2658 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
2659 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2660 return 0;
2661 }
2662
2663 return 1;
2664}
2665
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002666/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002667 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2668 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002669 * A header made of two fields is added to each datagram: the datagram length followed
2670 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002671 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2672 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002673 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002674static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2675 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002676{
2677 struct quic_enc_level *qel;
2678 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002679 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002680 struct quic_tx_packet *pkt;
2681 size_t total;
2682 size_t dg_headlen;
2683
2684 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2685 /* Each datagram is prepended with its length followed by the
2686 * address of the first packet in the datagram.
2687 */
2688 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2689 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2690 total = 0;
2691 start:
2692 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002693 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002694 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2695 * to ensure there is enough room to mark the end of prepared
2696 * contiguous data with a zero length.
2697 */
2698 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2699 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002700 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002701
2702 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002703 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002704 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002705 /* We do not probe if an immediate close was asked */
2706 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002707 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002708
2709 if (!qc_may_build_pkt(qc, frms, qel, cc, probe))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002710 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002711
2712 /* Leave room for the datagram header */
2713 pos += dg_headlen;
2714 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2715 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2716 }
2717 else {
2718 end = pos + qc->path->mtu;
2719 }
2720
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002721 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002722 QUIC_PACKET_TYPE_SHORT, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002723 switch (err) {
2724 case -2:
2725 goto err;
2726 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002727 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2728 * MTU, we are here because of the congestion control window. There is
2729 * no need to try to reuse this buffer.
2730 */
2731 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002732 default:
2733 break;
2734 }
2735
2736 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2737 if (!pkt)
2738 goto err;
2739
2740 total += pkt->len;
2741 /* Set the current datagram as prepared into <cbuf>. */
2742 qc_set_dg(cbuf, pkt->len, pkt);
2743 }
2744
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002745 /* Reset <wr> writer index if in front of <rd> index */
2746 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002747 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002748 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002749 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002750 }
2751
2752 out:
2753 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2754 return total;
2755
2756 err:
2757 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2758 return -1;
2759}
2760
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002761/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002762 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2763 * several packets in the same datagram. A header made of two fields is added
2764 * to each datagram: the datagram length followed by the address of the first
2765 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002766 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2767 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002768 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002769static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2770 enum quic_tls_enc_level tel,
2771 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002772{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002773 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002774 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002775 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002776 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2777 /* length of datagrams */
2778 uint16_t dglen;
2779 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002780 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002781 /* Each datagram is prepended with its length followed by the
2782 * address of the first packet in the datagram.
2783 */
2784 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002785
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002786 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2787
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002788 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002789 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002790 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002791 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002792 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002793 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002794 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002795 /* Leave at least <dglen> bytes at the end of this buffer
2796 * to ensure there is enough room to mark the end of prepared
2797 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002798 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002799 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2800 first_pkt = prv_pkt = NULL;
2801 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002802 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002803 enum quic_pkt_type pkt_type;
2804
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002805 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002806 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002807 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002808 /* We do not probe if an immediate close was asked */
2809 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002810 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002811
2812 if (!qc_may_build_pkt(qc, &qel->pktns->tx.frms, qel, cc, probe)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002813 if (prv_pkt)
2814 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002815 /* Let's select the next encryption level */
2816 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2817 tel = next_tel;
2818 qel = &qc->els[tel];
2819 /* Build a new datagram */
2820 prv_pkt = NULL;
2821 continue;
2822 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002823 break;
2824 }
2825
2826 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002827 if (!prv_pkt) {
2828 /* Leave room for the datagram header */
2829 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002830 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002831 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2832 }
2833 else {
2834 end = pos + qc->path->mtu;
2835 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002836 }
2837
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002838 cur_pkt = qc_build_pkt(&pos, end, qel, &qel->pktns->tx.frms,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002839 qc, dglen, padding, pkt_type, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002840 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002841 case -2:
2842 goto err;
2843 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002844 /* If there was already a correct packet present, set the
2845 * current datagram as prepared into <cbuf>.
2846 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002847 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002848 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002849 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002850 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002851 break;
2852 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002853
Frédéric Lécaille63556772021-12-29 17:18:21 +01002854 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2855 if (!cur_pkt)
2856 goto err;
2857
2858 total += cur_pkt->len;
2859 /* keep trace of the first packet in the datagram */
2860 if (!first_pkt)
2861 first_pkt = cur_pkt;
2862 /* Attach the current one to the previous one */
2863 if (prv_pkt)
2864 prv_pkt->next = cur_pkt;
2865 /* Let's say we have to build a new dgram */
2866 prv_pkt = NULL;
2867 dglen += cur_pkt->len;
2868 /* Client: discard the Initial encryption keys as soon as
2869 * a handshake packet could be built.
2870 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002871 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01002872 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2873 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2874 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2875 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2876 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002877 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002878 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002879 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01002880 }
2881 /* If the data for the current encryption level have all been sent,
2882 * select the next level.
2883 */
2884 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécailled6570e12022-03-29 17:41:57 +02002885 (LIST_ISEMPTY(&qel->pktns->tx.frms) && !qel->pktns->tx.pto_probe)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002886 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2887 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2888 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2889 tel = next_tel;
2890 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002891 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002892 /* If there is data for the next level, do not
2893 * consume a datagram.
2894 */
2895 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002896 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002897 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002898 /* If we have to build a new datagram, set the current datagram as
2899 * prepared into <cbuf>.
2900 */
2901 if (!prv_pkt) {
2902 qc_set_dg(cbuf, dglen, first_pkt);
2903 first_pkt = NULL;
2904 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002905 padding = 0;
2906 }
2907 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002908 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002909 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2910 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002911 }
2912 }
2913
2914 stop_build:
2915 /* Reset <wr> writer index if in front of <rd> index */
2916 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002917 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002918 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002919 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002920 }
2921
2922 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002923 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002924 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002925
2926 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002927 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002928 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002929}
2930
2931/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002932 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002933 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002934int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002935{
2936 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002937 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002938
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002939 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002940 cbuf = qr->cbuf;
2941 while (cb_contig_data(cbuf)) {
2942 unsigned char *pos;
2943 struct buffer tmpbuf = { };
2944 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2945 uint16_t dglen;
2946 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002947 unsigned int time_sent;
2948
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002949 pos = cb_rd(cbuf);
2950 dglen = read_u16(pos);
2951 /* End of prepared datagrams.
2952 * Reset the reader index only if in front of the writer index.
2953 */
2954 if (!dglen) {
2955 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2956
2957 if (wr && wr < cbuf->rd) {
2958 cb_rd_reset(cbuf);
2959 continue;
2960 }
2961 break;
2962 }
2963
2964 pos += sizeof dglen;
2965 first_pkt = read_ptr(pos);
2966 pos += sizeof first_pkt;
2967 tmpbuf.area = (char *)pos;
2968 tmpbuf.size = tmpbuf.data = dglen;
2969
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002970 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01002971 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002972 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002973
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002974 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002975 qc->tx.bytes += tmpbuf.data;
2976 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002977
2978 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2979 pkt->time_sent = time_sent;
2980 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2981 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002982 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01002983 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
2984 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002985 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002986 qc->path->in_flight += pkt->in_flight_len;
2987 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2988 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002989 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002990 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002991 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01002992 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002993 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002994 }
2995 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002996
2997 return 1;
2998}
2999
3000/* Build all the frames which must be sent just after the handshake have succeeded.
3001 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3002 * a HANDSHAKE_DONE frame.
3003 * Return 1 if succeeded, 0 if not.
3004 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003005static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003006{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003007 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003008 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003009 struct quic_frame *frm, *frmbak;
3010 struct list frm_list = LIST_HEAD_INIT(frm_list);
3011 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003012
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003013 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003014 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003015 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003016 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003017 if (!frm)
3018 return 0;
3019
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003020 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003021 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003022 }
3023
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003024 first = 1;
3025 max = qc->tx.params.active_connection_id_limit;
3026 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003027 struct quic_connection_id *cid;
3028
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003029 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003030 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003031 goto err;
3032
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003033 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003034 if (!cid)
3035 goto err;
3036
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003037 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003038 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003039
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003040 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003041 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003042 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003043
3044 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003045 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046
3047 return 1;
3048
3049 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003050 /* free the frames */
3051 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3052 pool_free(pool_head_quic_frame, frm);
3053
3054 node = eb64_first(&qc->cids);
3055 while (node) {
3056 struct quic_connection_id *cid;
3057
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003058
3059 cid = eb64_entry(&node->node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003060 if (cid->seq_num.key >= max)
3061 break;
3062
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003063 if (cid->seq_num.key < first)
3064 continue;
3065
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003066 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003067 ebmb_delete(&cid->node);
3068 eb64_delete(&cid->seq_num);
3069 pool_free(pool_head_quic_connection_id, cid);
3070 }
3071
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003072 return 0;
3073}
3074
3075/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003076void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003077{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003078 struct eb64_node *n;
3079 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003080
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003081 n = eb64_first(&arngs->root);
3082 while (n) {
3083 struct eb64_node *next;
3084
3085 ar = eb64_entry(&n->node, struct quic_arng_node, first);
3086 next = eb64_next(n);
3087 eb64_delete(n);
3088 free(ar);
3089 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003090 }
3091}
3092
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003093/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3094 * descending order.
3095 */
3096static inline size_t sack_gap(struct quic_arng_node *p,
3097 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003098{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003099 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003100}
3101
3102
3103/* Remove the last elements of <ack_ranges> list of ack range updating its
3104 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003105 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003106 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003107static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003109 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003110
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003111 last = eb64_last(&arngs->root);
3112 while (last && arngs->enc_sz > limit) {
3113 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003114
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003115 prev = eb64_prev(last);
3116 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003117 return 0;
3118
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003119 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
3120 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
3121 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3122 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3123 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3124 --arngs->sz;
3125 eb64_delete(last);
3126 pool_free(pool_head_quic_arng, last);
3127 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003128 }
3129
3130 return 1;
3131}
3132
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003133/* Set the encoded size of <arngs> QUIC ack ranges. */
3134static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3135{
3136 struct eb64_node *node, *next;
3137 struct quic_arng_node *ar, *ar_next;
3138
3139 node = eb64_last(&arngs->root);
3140 if (!node)
3141 return;
3142
3143 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3144 arngs->enc_sz = quic_int_getsize(ar->last) +
3145 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3146
3147 while ((next = eb64_prev(node))) {
3148 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
3149 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3150 quic_int_getsize(ar_next->last - ar_next->first.key);
3151 node = next;
3152 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3153 }
3154}
3155
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003156/* Insert <ar> ack range into <argns> tree of ack ranges.
3157 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003158 */
3159static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003160struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3161 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003162{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003163 struct quic_arng_node *new_ar;
3164
3165 new_ar = pool_alloc(pool_head_quic_arng);
3166 if (new_ar) {
3167 new_ar->first.key = ar->first;
3168 new_ar->last = ar->last;
3169 eb64_insert(&arngs->root, &new_ar->first);
3170 arngs->sz++;
3171 }
3172
3173 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003174}
3175
3176/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003177 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003178 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003179 *
3180 * Descending order
3181 * ------------->
3182 * range1 range2
3183 * ..........|--------|..............|--------|
3184 * ^ ^ ^ ^
3185 * | | | |
3186 * last1 first1 last2 first2
3187 * ..........+--------+--------------+--------+......
3188 * diff1 gap12 diff2
3189 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003190 * To encode the previous list of ranges we must encode integers as follows in
3191 * descending order:
3192 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003193 * with diff1 = last1 - first1
3194 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003195 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003196 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003197 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003198int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3199 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003200{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003201 struct eb64_node *le;
3202 struct quic_arng_node *new_node;
3203 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003204
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003205 new = NULL;
3206 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003207 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003208 if (!new_node)
3209 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003210
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003211 goto out;
3212 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003213
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003214 le = eb64_lookup_le(&arngs->root, ar->first);
3215 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003216 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003217 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003218 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003219
3220 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003221 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003222 else {
3223 struct quic_arng_node *le_ar =
3224 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003225
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003226 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003227 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003228 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003229
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003230 if (le_ar->last + 1 >= ar->first) {
3231 le_ar->last = ar->last;
3232 new = le;
3233 new_node = le_ar;
3234 }
3235 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003236 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003237 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003238 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003239
3240 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003241 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003242 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003243
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003244 /* Verify that the new inserted node does not overlap the nodes
3245 * which follow it.
3246 */
3247 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003248 struct eb64_node *next;
3249 struct quic_arng_node *next_node;
3250
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003251 while ((next = eb64_next(new))) {
3252 next_node =
3253 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003254 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003255 break;
3256
3257 if (next_node->last > new_node->last)
3258 new_node->last = next_node->last;
3259 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003260 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003261 /* Decrement the size of these ranges. */
3262 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003263 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003264 }
3265
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003266 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003267 quic_arngs_set_enc_sz(arngs);
3268
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003269 return 1;
3270}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003271/* Remove the header protection of packets at <el> encryption level.
3272 * Always succeeds.
3273 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003274static 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 +01003275{
3276 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003277 struct quic_rx_packet *pqpkt;
3278 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003279 struct quic_enc_level *app_qel;
3280
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003281 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3282 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003283 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003284 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003285 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003286 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003287 goto out;
3288 }
3289 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003290 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003291 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003292 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003293 pqpkt->data, pqpkt->data + pqpkt->len)) {
3294 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003295 /* XXX TO DO XXX */
3296 }
3297 else {
3298 /* The AAD includes the packet number field */
3299 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3300 /* Store the packet into the tree of packets to decrypt. */
3301 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003302 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003303 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3304 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003305 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003306 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003307 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003308 MT_LIST_DELETE_SAFE(pkttmp1);
3309 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003310 }
3311
3312 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003313 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003314}
3315
3316/* Process all the CRYPTO frame at <el> encryption level.
3317 * Return 1 if succeeded, 0 if not.
3318 */
3319static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003320 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321{
3322 struct eb64_node *node;
3323
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003324 node = eb64_first(&el->rx.crypto.frms);
3325 while (node) {
3326 struct quic_rx_crypto_frm *cf;
3327
3328 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3329 if (cf->offset_node.key != el->rx.crypto.offset)
3330 break;
3331
3332 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3333 goto err;
3334
3335 node = eb64_next(node);
3336 quic_rx_packet_refdec(cf->pkt);
3337 eb64_delete(&cf->offset_node);
3338 pool_free(pool_head_quic_rx_crypto_frm, cf);
3339 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003340 return 1;
3341
3342 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003343 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003344 return 0;
3345}
3346
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003347/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003348 * 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 +02003349 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003350 * Return 1 if succeeded, 0 if not.
3351 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003352int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3353 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003354{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003355 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003356 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003357 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003358 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003359
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003360 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003361 qel = cur_el;
3362 next_tel:
3363 if (!qel)
3364 goto out;
3365
3366 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3367 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003368 while (node) {
3369 struct quic_rx_packet *pkt;
3370
3371 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003372 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003373 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003374 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003375 /* Drop the packet */
3376 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003377 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003378 }
3379 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003380 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003381 /* Drop the packet */
3382 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003383 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003384 }
3385 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003386 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3387
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003388 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3389 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3390 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003391 qc_idle_timer_rearm(qc, 1);
3392 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003393 if (pkt->pn > largest_pn)
3394 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003395 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003396 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003397 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003398 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003399 }
3400 }
3401 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003402 eb64_delete(&pkt->pn_node);
3403 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003404 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003405 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003406
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003407 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003408 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn)
3409 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003410 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003411 goto err;
3412
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003413 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003414 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003415 qel = next_el;
3416 goto next_tel;
3417 }
3418
3419 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003420 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003421 return 1;
3422
3423 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003424 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003425 return 0;
3426}
3427
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003428/* Check if it's possible to remove header protection for packets related to
3429 * encryption level <qel>. If <qel> is NULL, assume it's false.
3430 *
3431 * Return true if the operation is possible else false.
3432 */
3433static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3434{
3435 enum quic_tls_enc_level tel;
3436
3437 if (!qel)
3438 return 0;
3439
3440 tel = ssl_to_quic_enc_level(qel->level);
3441
3442 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003443 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003444 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003445 return 0;
3446 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003447
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003448 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003449 return 0;
3450
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003451 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003452 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003453 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003454 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003455
3456 return 1;
3457}
3458
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003459/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaillec2f561c2022-02-25 17:46:07 +01003460int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003461{
3462 int ret;
3463 struct qring *qr;
3464
3465 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3466 if (!qr)
3467 /* Never happens */
3468 return 1;
3469
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003470 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003471 if (ret == -1)
3472 goto err;
3473 else if (ret == 0)
3474 goto out;
3475
3476 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3477 goto err;
3478
3479 out:
3480 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3481 return 1;
3482
3483 err:
3484 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3485 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3486 return 0;
3487}
3488
3489/* QUIC connection packet handler task (post handshake) */
3490static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3491{
3492 struct ssl_sock_ctx *ctx;
3493 struct quic_conn *qc;
3494 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003495
3496
3497 ctx = context;
3498 qc = ctx->qc;
3499 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003500
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003501 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003502
3503 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3504 qc_rm_hp_pkts(qc, qel);
3505
3506 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3507 goto err;
3508
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003509 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003510 goto err;
3511
3512 return t;
3513
3514 err:
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003515 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003516 return t;
3517}
3518
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003519/* QUIC connection packet handler task. */
3520struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003522 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003523 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003524 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003525 enum quic_tls_enc_level tel, next_tel;
3526 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003527 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003528 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003529
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003530 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003531 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003532 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003533 qr = NULL;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003534 st = qc->state;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003535 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003536 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
3537 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003538 /* The I/O handler has been woken up by the dgram listener
3539 * after the anti-amplification was reached.
3540 */
3541 qc_set_timer(qc);
3542 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3543 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3544 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003545 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003546 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3547 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3548 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003549 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003550 if (st >= QUIC_HS_ST_COMPLETE &&
3551 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3552 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3553 /* There may be remaining Handshake packets to treat and acknowledge. */
3554 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3555 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3556 }
3557 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558 goto err;
3559
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003560 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003561 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003562
3563 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003564 /* Treat packets waiting for header packet protection decryption */
3565 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003566 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003567
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003568 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3569 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3570 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003571 goto err;
3572
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003573 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003574 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003575 qel = next_qel;
3576 next_qel = NULL;
3577 goto next_level;
3578 }
3579
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003580 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003581 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003582 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003583 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003584 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003585
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003586 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003587 QUIC_FL_TLS_SECRETS_DCD)) {
3588 /* Discard the Handshake keys. */
3589 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3590 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3591 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3592 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003593 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003594 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003595 }
3596
3597 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3598 /* There may be remaining handshake to build (acks) */
3599 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3600 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003601 }
3602
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003603 if (!qr)
3604 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003605 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3606 * be considered.
3607 */
3608 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003609 goto err;
3610 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003611 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003613 else if (ret == 0)
3614 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003615
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003616 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003617 goto err;
3618
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003619 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003620 /* Check if there is something to do for the next level.
3621 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003622 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003623 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003624 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003625 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003626 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003627 goto next_level;
3628 }
3629
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003630 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003631 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003632 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003633
3634 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003635 if (qr)
3636 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003637 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003638 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003639}
3640
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003641/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003642static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3643{
3644 int i;
3645
3646 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3647 if (qel->tx.crypto.bufs[i]) {
3648 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3649 qel->tx.crypto.bufs[i] = NULL;
3650 }
3651 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003652 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003653}
3654
3655/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003656 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003657 * Returns 1 if succeeded, 0 if not.
3658 */
3659static int quic_conn_enc_level_init(struct quic_conn *qc,
3660 enum quic_tls_enc_level level)
3661{
3662 struct quic_enc_level *qel;
3663
3664 qel = &qc->els[level];
3665 qel->level = quic_to_ssl_enc_level(level);
3666 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3667 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3668 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003669 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003670
3671 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003672 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003673 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003674 qel->rx.crypto.offset = 0;
3675 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003676
3677 /* Allocate only one buffer. */
3678 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3679 if (!qel->tx.crypto.bufs)
3680 goto err;
3681
3682 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3683 if (!qel->tx.crypto.bufs[0])
3684 goto err;
3685
3686 qel->tx.crypto.bufs[0]->sz = 0;
3687 qel->tx.crypto.nb_buf = 1;
3688
3689 qel->tx.crypto.sz = 0;
3690 qel->tx.crypto.offset = 0;
3691
3692 return 1;
3693
3694 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003695 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003696 return 0;
3697}
3698
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003699/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
3700 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003701 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003702 * This function must only be called by the thread responsible of the quic_conn
3703 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003704 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003705static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003706{
3707 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003708 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003709 struct eb64_node *node;
3710
3711 /* free remaining stream descriptors */
3712 node = eb64_first(&qc->streams_by_id);
3713 while (node) {
3714 struct qc_stream_desc *stream;
3715
3716 stream = eb64_entry(node, struct qc_stream_desc, by_id);
3717 node = eb64_next(node);
3718
3719 eb64_delete(&stream->by_id);
3720 pool_free(pool_head_quic_conn_stream, stream);
3721 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003722
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003723 if (qc->idle_timer_task) {
3724 task_destroy(qc->idle_timer_task);
3725 qc->idle_timer_task = NULL;
3726 }
3727
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003728 if (qc->timer_task) {
3729 task_destroy(qc->timer_task);
3730 qc->timer_task = NULL;
3731 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003732
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003733 /* remove the connection from receiver cids trees */
3734 ebmb_delete(&qc->odcid_node);
3735 ebmb_delete(&qc->scid_node);
3736 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003737
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003738 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003739 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003740 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003741 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003742 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003743 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003744
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003745 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003746 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003747
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003748 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3749 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003750 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003751}
3752
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003753void quic_close(struct connection *conn, void *xprt_ctx)
3754{
3755 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003756 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003757
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003758 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003759
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003760 /* Next application data can be dropped. */
3761 qc->mux_state = QC_MUX_RELEASED;
3762
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003763 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003764}
3765
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003766/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003767static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003768{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003769 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003770 struct quic_conn *qc;
3771 struct quic_pktns *pktns;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003772 int i;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003773
3774 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003775 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003776 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003777 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003778 task->expire = TICK_ETERNITY;
3779 pktns = quic_loss_pktns(qc);
3780 if (tick_isset(pktns->tx.loss_time)) {
3781 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3782
3783 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3784 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003785 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3786 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003787 goto out;
3788 }
3789
3790 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003791 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003792 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3793 pktns->tx.pto_probe = 1;
3794 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3795 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3796 }
3797 else {
3798 pktns->tx.pto_probe = 2;
3799 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003800 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003801 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003802 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3803 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3804
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003805 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003806 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003807 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003808 iel->pktns->tx.pto_probe = 1;
3809 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003810
3811 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003812 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3813 continue;
3814
Frédéric Lécaille53c7d8d2022-02-15 12:00:55 +01003815 qc_prep_fast_retrans(&qc->els[i], qc);
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003816 }
3817
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003818 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3819 qc->path->loss.pto_count++;
3820
3821 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003822 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003823
3824 return task;
3825}
3826
3827/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3828 * connections used to identify the first Initial packets of client connecting
3829 * to listeners. This parameter must be NULL for QUIC connections attached
3830 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3831 * <scid> is the source connection ID with <scid_len> as length.
3832 * Returns 1 if succeeded, 0 if not.
3833 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003834static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003835 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003836 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003837{
3838 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003839 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003840 /* Initial CID. */
3841 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003842 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003843 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003844
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003845 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003846 qc = pool_zalloc(pool_head_quic_conn);
3847 if (!qc) {
3848 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3849 goto err;
3850 }
3851
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003852 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3853 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003854 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003855 goto err;
3856 }
3857
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003858 qc->cids = EB_ROOT;
3859 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003860 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003861 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003862
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003863 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003864 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003865 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003866 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003867 qc->odcid.addrlen = dcid_addr_len;
3868 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003869
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003870 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003871 if (scid_len)
3872 memcpy(qc->dcid.data, scid, scid_len);
3873 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003874 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003875 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003876 }
3877 /* QUIC Client (outgoing connection to servers) */
3878 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003879 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003880 if (dcid_len)
3881 memcpy(qc->dcid.data, dcid, dcid_len);
3882 qc->dcid.len = dcid_len;
3883 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003884 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003885
3886 /* Initialize the output buffer */
3887 qc->obuf.pos = qc->obuf.data;
3888
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003889 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003890 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003891 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003892 goto err;
3893 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003894
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003895 /* insert the allocated CID in the receiver datagram handler tree */
3896 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003897 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003898
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003899 /* Select our SCID which is the first CID with 0 as sequence number. */
3900 qc->scid = icid->cid;
3901
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003902 /* Packet number spaces initialization. */
3903 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3904 quic_pktns_init(&qc->pktns[i]);
3905 /* QUIC encryption level context initialization. */
3906 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003907 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003908 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003909 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003910 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003911 /* Initialize the packet number space. */
3912 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3913 }
3914
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003915 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003916 qc->tps_tls_ext = qc->version & 0xff000000 ?
3917 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3918 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003919 /* TX part. */
3920 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003921 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3922 qc->tx.wbuf = qc->tx.rbuf = 0;
3923 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003924 /* RX part. */
3925 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003926 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003927 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003928 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003929 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003930 goto err;
3931 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003932
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003933 /* XXX TO DO: Only one path at this time. */
3934 qc->path = &qc->paths[0];
3935 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3936
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01003937 /* required to use MTLIST_IN_LIST */
3938 MT_LIST_INIT(&qc->accept_list);
3939
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003940 qc->streams_by_id = EB_ROOT_UNIQUE;
3941
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003942 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003943
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003944 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003945
3946 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003947 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003948 pool_free(pool_head_quic_conn_rxbuf, buf_area);
3949 if (qc)
3950 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003951 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003952 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003953}
3954
3955/* Initialize the timer task of <qc> QUIC connection.
3956 * Returns 1 if succeeded, 0 if not.
3957 */
3958static int quic_conn_init_timer(struct quic_conn *qc)
3959{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003960 /* Attach this task to the same thread ID used for the connection */
3961 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003962 if (!qc->timer_task)
3963 return 0;
3964
3965 qc->timer = TICK_ETERNITY;
3966 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01003967 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003968
3969 return 1;
3970}
3971
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003972/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
3973 * which is set to 1 when receiving a packet , and 0 when sending packet
3974 */
3975static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
3976{
3977 unsigned int expire;
3978
3979 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
3980 if (read) {
3981 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
3982 }
3983 else {
3984 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
3985 }
3986 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
3987}
3988
3989/* The task handling the idle timeout */
3990static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
3991{
3992 struct quic_conn *qc = ctx;
3993
3994 quic_conn_release(qc);
3995
3996 return NULL;
3997}
3998
3999/* Initialize the idle timeout task for <qc>.
4000 * Returns 1 if succeeded, 0 if not.
4001 */
4002static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4003{
4004 qc->idle_timer_task = task_new_here();
4005 if (!qc->idle_timer_task)
4006 return 0;
4007
4008 qc->idle_timer_task->process = qc_idle_timer_task;
4009 qc->idle_timer_task->context = qc;
4010 qc_idle_timer_rearm(qc, 1);
4011 task_queue(qc->idle_timer_task);
4012
4013 return 1;
4014}
4015
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004016/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4017 * past one byte of this buffer.
4018 */
4019static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4020 struct quic_rx_packet *pkt)
4021{
4022 unsigned char dcid_len, scid_len;
4023
4024 /* Version */
4025 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
4026 return 0;
4027
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004028 /* Destination Connection ID Length */
4029 dcid_len = *(*buf)++;
4030 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
4031 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
4032 /* XXX MUST BE DROPPED */
4033 return 0;
4034
4035 if (dcid_len) {
4036 /* Check that the length of this received DCID matches the CID lengths
4037 * of our implementation for non Initials packets only.
4038 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004039 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4040 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01004041 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004042 return 0;
4043
4044 memcpy(pkt->dcid.data, *buf, dcid_len);
4045 }
4046
4047 pkt->dcid.len = dcid_len;
4048 *buf += dcid_len;
4049
4050 /* Source Connection ID Length */
4051 scid_len = *(*buf)++;
4052 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
4053 /* XXX MUST BE DROPPED */
4054 return 0;
4055
4056 if (scid_len)
4057 memcpy(pkt->scid.data, *buf, scid_len);
4058 pkt->scid.len = scid_len;
4059 *buf += scid_len;
4060
4061 return 1;
4062}
4063
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004064/* Insert <pkt> RX packet in its <qel> RX packets tree */
4065static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4066{
4067 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004068 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004069 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4070 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4071 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004072}
4073
4074/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004075 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4076 * byte past the end of the buffer containing this packet and <beg> the address of
4077 * the packet first byte.
4078 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4079 * Returns 1 if succeeded, 0 if not.
4080 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004081static inline int qc_try_rm_hp(struct quic_conn *qc,
4082 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004083 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004084 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004085 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004086{
4087 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004088 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004089 struct quic_enc_level *qel;
4090 /* Only for traces. */
4091 struct quic_rx_packet *qpkt_trace;
4092
4093 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004094 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004095 /* The packet number is here. This is also the start minus
4096 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4097 * protection.
4098 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004099 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004100
4101 tel = quic_packet_type_enc_level(pkt->type);
4102 qel = &qc->els[tel];
4103
4104 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004105 /* Note that the following function enables us to unprotect the packet
4106 * number and its length subsequently used to decrypt the entire
4107 * packets.
4108 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004109 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4110 qel->pktns->rx.largest_pn, pn, beg, end)) {
4111 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004112 goto err;
4113 }
4114
4115 /* The AAD includes the packet number field found at <pn>. */
4116 pkt->aad_len = pn - beg + pkt->pnl;
4117 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004118 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004119 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004120 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004121 /* If the packet number space has been discarded, this packet
4122 * will be not parsed.
4123 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004124 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004125 goto out;
4126 }
4127
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004128 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004129 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004130 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4131 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004132 }
4133
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004134 *el = qel;
4135 /* No reference counter incrementation here!!! */
4136 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4137 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4138 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4139 b_add(&qc->rx.buf, pkt->len);
4140 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004141 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004142 return 1;
4143
4144 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004145 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004146 return 0;
4147}
4148
4149/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
4150 * Also set <*long_header> to 1 if this form is long, 0 if not.
4151 */
4152static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
4153 unsigned char byte0, int *long_header)
4154{
4155 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
4156 pkt->type =
4157 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
4158 *long_header = 1;
4159 }
4160 else {
4161 pkt->type = QUIC_PACKET_TYPE_SHORT;
4162 *long_header = 0;
4163 }
4164}
4165
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004166/*
4167 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
4168 */
4169static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
4170{
4171 int j = 0, version;
4172
4173 do {
4174 version = quic_supported_version[j];
4175 if (version == pkt->version)
4176 return 1;
4177
4178 version = quic_supported_version[++j];
4179 } while(version);
4180
4181 return 0;
4182}
4183
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004184/*
4185 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4186 * address <addr>.
4187 * Implementation of RFC9000 6. Version Negotiation
4188 *
4189 * TODO implement a rate-limiting sending of Version Negotiation packets
4190 *
4191 * Returns 0 on success else non-zero
4192 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004193static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4194 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004195{
4196 char buf[256];
4197 int i = 0, j, version;
4198 const socklen_t addrlen = get_addr_len(addr);
4199
4200 /*
4201 * header form
4202 * long header, fixed bit to 0 for Version Negotiation
4203 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004204 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4205 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004206
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004207 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004208 /* null version for Version Negotiation */
4209 buf[i++] = '\x00';
4210 buf[i++] = '\x00';
4211 buf[i++] = '\x00';
4212 buf[i++] = '\x00';
4213
4214 /* source connection id */
4215 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004216 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004217 i += pkt->scid.len;
4218
4219 /* destination connection id */
4220 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004221 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004222 i += pkt->dcid.len;
4223
4224 /* supported version */
4225 j = 0;
4226 do {
4227 version = htonl(quic_supported_version[j]);
4228 memcpy(&buf[i], &version, sizeof(version));
4229 i += sizeof(version);
4230
4231 version = quic_supported_version[++j];
4232 } while (version);
4233
4234 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4235 return 1;
4236
4237 return 0;
4238}
4239
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004240/* Generate the token to be used in Retry packets. The token is written to
4241 * <buf> which is expected to be <len> bytes.
4242 *
4243 * Various parameters are expected to be encoded in the token. For now, only
4244 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4245 * as this CID must be repeated by the server in the transport parameters.
4246 *
4247 * TODO add the client address to validate the token origin.
4248 *
4249 * Returns the length of the encoded token or 0 on error.
4250 */
4251static int generate_retry_token(unsigned char *buf, unsigned char len,
4252 struct quic_rx_packet *pkt)
4253{
4254 const size_t token_len = 1 + pkt->dcid.len;
4255 unsigned char i = 0;
4256
4257 if (token_len > len)
4258 return 0;
4259
4260 buf[i++] = pkt->dcid.len;
4261 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4262 i += pkt->dcid.len;
4263
4264 return i;
4265}
4266
4267/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4268 * the Initial <pkt> packet.
4269 *
4270 * Returns 0 on success else non-zero.
4271 */
4272static int send_retry(int fd, struct sockaddr_storage *addr,
4273 struct quic_rx_packet *pkt)
4274{
4275 unsigned char buf[128];
4276 int i = 0, token_len;
4277 const socklen_t addrlen = get_addr_len(addr);
4278 struct quic_cid scid;
4279
4280 /* long header + fixed bit + packet type 0x3 */
4281 buf[i++] = 0xf0;
4282 /* version */
4283 buf[i++] = 0x00;
4284 buf[i++] = 0x00;
4285 buf[i++] = 0x00;
4286 buf[i++] = 0x01;
4287
4288 /* Use the SCID from <pkt> for Retry DCID. */
4289 buf[i++] = pkt->scid.len;
4290 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4291 i += pkt->scid.len;
4292
4293 /* Generate a new CID to be used as SCID for the Retry packet. */
4294 scid.len = QUIC_HAP_CID_LEN;
4295 if (RAND_bytes(scid.data, scid.len) != 1)
4296 return 1;
4297
4298 buf[i++] = scid.len;
4299 memcpy(&buf[i], scid.data, scid.len);
4300 i += scid.len;
4301
4302 /* token */
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004303 if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt)))
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004304 return 1;
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004305
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004306 i += token_len;
4307
4308 /* token integrity tag */
4309 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4310 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4311 pkt->dcid.len, buf, i)) {
4312 return 1;
4313 }
4314
4315 i += QUIC_TLS_TAG_LEN;
4316
4317 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4318 return 1;
4319
4320 return 0;
4321}
4322
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004323/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4324 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4325 * concatenated to the <pkt> DCID field.
4326 *
4327 * Returns the instance or NULL if not found.
4328 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004329static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004330 struct listener *l,
4331 struct sockaddr_storage *saddr)
4332{
4333 struct quic_conn *qc = NULL;
4334 struct ebmb_node *node;
4335 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004336 /* set if the quic_conn is found in the second DCID tree */
4337 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004338
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004339 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4340 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4341 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4342 /* DCIDs of first packets coming from multiple clients may have
4343 * the same values. Let's distinguish them by concatenating the
4344 * socket addresses.
4345 */
4346 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004347 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004348 pkt->dcid.len + pkt->dcid.addrlen);
4349 if (node) {
4350 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4351 goto end;
4352 }
4353 }
4354
4355 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4356 * also for INITIAL/0-RTT non-first packets with the final DCID in
4357 * used.
4358 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004359 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004360 if (!node)
4361 goto end;
4362
4363 id = ebmb_entry(node, struct quic_connection_id, node);
4364 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004365 found_in_dcid = 1;
4366
4367 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004368 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4369 * If already done, this is a noop.
4370 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004371 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004372 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004373
4374 return qc;
4375}
4376
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004377/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4378 * will extract the parameters stored in the token : <odcid>.
4379 *
4380 * Returns 0 on success else non-zero.
4381 */
4382static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4383 struct quic_cid *odcid)
4384{
4385 uint64_t odcid_len;
4386
4387 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4388 return 1;
4389
Frédéric Lécaillef1f812b2022-03-17 16:22:02 +01004390 if (odcid_len > QUIC_CID_MAXLEN)
4391 return 1;
4392
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004393 memcpy(odcid->data, token, odcid_len);
4394 odcid->len = odcid_len;
4395
4396 return 0;
4397}
4398
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004399/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4400 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4401 * parameters of this session.
4402 * This is the responsibility of the caller to check the validity of all the
4403 * pointers passed as parameter to this function.
4404 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4405 * CO_ER_SSL_NO_MEM.
4406 */
4407static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4408 unsigned char *params, size_t params_len)
4409{
4410 int retry;
4411
4412 retry = 1;
4413 retry:
4414 *ssl = SSL_new(ssl_ctx);
4415 if (!*ssl) {
4416 if (!retry--)
4417 goto err;
4418
4419 pool_gc(NULL);
4420 goto retry;
4421 }
4422
4423 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4424 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4425 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004426 SSL_free(*ssl);
4427 *ssl = NULL;
4428 if (!retry--)
4429 goto err;
4430
4431 pool_gc(NULL);
4432 goto retry;
4433 }
4434
4435 return 0;
4436
4437 err:
4438 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4439 return -1;
4440}
4441
4442/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4443 * used to process <qc> received packets. The allocated context is stored in
4444 * <qc.xprt_ctx>.
4445 *
4446 * Returns 0 on success else non-zero.
4447 */
4448int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4449{
4450 struct bind_conf *bc = qc->li->bind_conf;
4451 struct ssl_sock_ctx *ctx = NULL;
4452
4453 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4454 if (!ctx)
4455 goto err;
4456
4457 ctx->wait_event.tasklet = tasklet_new();
4458 if (!ctx->wait_event.tasklet)
4459 goto err;
4460
4461 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4462 ctx->wait_event.tasklet->context = ctx;
4463 ctx->wait_event.events = 0;
4464 ctx->subs = NULL;
4465 ctx->xprt_ctx = NULL;
4466 ctx->qc = qc;
4467
4468 /* Set tasklet tid based on the SCID selected by us for this
4469 * connection. The upper layer will also be binded on the same thread.
4470 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004471 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004472
4473 if (qc_is_listener(qc)) {
4474 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4475 qc->enc_params, qc->enc_params_len) == -1) {
4476 goto err;
4477 }
4478
4479 /* Enabling 0-RTT */
4480 if (bc->ssl_conf.early_data)
4481 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4482
4483 SSL_set_accept_state(ctx->ssl);
4484 }
4485
4486 ctx->xprt = xprt_get(XPRT_QUIC);
4487
4488 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004489 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004490
4491 return 0;
4492
4493 err:
4494 if (ctx && ctx->wait_event.tasklet)
4495 tasklet_free(ctx->wait_event.tasklet);
4496 pool_free(pool_head_quic_conn_ctx, ctx);
4497
4498 return 1;
4499}
4500
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004501static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004502 struct quic_rx_packet *pkt, int first_pkt,
4503 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004504{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004505 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004506 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004507 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004508 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004509 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004510 size_t b_cspace;
4511 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004512
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004513 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004514 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004515 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004516 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004517 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4518 /* This ist only to please to traces and distinguish the
4519 * packet with parsed packet number from others.
4520 */
4521 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004522 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004523 goto err;
4524
4525 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004526 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004527 /* XXX TO BE DISCARDED */
4528 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4529 goto err;
4530 }
4531
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004532 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004533 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004534 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004535 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004536 uint64_t len;
4537
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004538 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004539 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4540 goto err;
4541 }
4542
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004543 /* 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écaille2c15a662021-12-22 20:39:12 +01004553 /* Retry of Version Negotiation packets are only sent by servers */
4554 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4555 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4556 goto err;
4557 }
4558
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004559 /* RFC9000 6. Version Negotiation */
4560 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004561 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004562 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004563 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4564 goto err;
4565 }
4566
4567 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004568 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004569 }
4570
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004571 /* For Initial packets, and for servers (QUIC clients connections),
4572 * there is no Initial connection IDs storage.
4573 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004574 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004575 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004576
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004577 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4578 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004579 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4580 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004581 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004582
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004583 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4584 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004585 */
4586 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004587
4588 /* TODO Retry should be automatically activated if
4589 * suspect network usage is detected.
4590 */
4591 if (!token_len && l->bind_conf->quic_force_retry) {
4592 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004593 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004594 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4595 goto err;
4596 }
4597
4598 goto err;
4599 }
4600 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004601 pkt->token = buf;
4602 buf += pkt->token_len;
4603 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004604 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004605 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004606 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004607 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4608 goto err;
4609 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004610 }
4611
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004612 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4613 end - buf < len) {
4614 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4615 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004616 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004617
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004618 payload = buf;
4619 pkt->len = len + payload - beg;
4620
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004621 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004622 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004623 int ipv4;
4624 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004625 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004626 const unsigned char *salt = initial_salt_v1;
4627 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004628
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004629 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004630 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004631 goto err;
4632 }
4633
Frédéric Lécailledc364042022-01-27 16:51:54 +01004634 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4635 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4636 goto err;
4637 }
4638
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004639 pkt->saddr = dgram->saddr;
4640 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004641 qc = qc_new_conn(pkt->version, ipv4,
4642 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004643 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004644 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004645 goto err;
4646
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004647 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4648
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004649 odcid = &qc->rx.params.original_destination_connection_id;
4650 /* Copy the transport parameters. */
4651 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004652
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004653 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004654 if (pkt->token_len) {
4655 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4656 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4657 goto err;
4658 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004659 /* Copy retry_source_connection_id transport parameter. */
4660 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4661 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004662 }
4663 else {
4664 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4665 odcid->len = pkt->dcid.len;
4666 }
4667
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004668 /* Copy the initial source connection ID. */
4669 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4670 qc->enc_params_len =
4671 quic_transport_params_encode(qc->enc_params,
4672 qc->enc_params + sizeof qc->enc_params,
4673 &qc->rx.params, 1);
4674 if (!qc->enc_params_len)
4675 goto err;
4676
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004677 if (qc_conn_alloc_ssl_ctx(qc))
4678 goto err;
4679
Frédéric Lécaille789413c2022-01-31 10:16:18 +01004680 if (!quic_conn_init_timer(qc))
4681 goto err;
4682
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004683 if (!quic_conn_init_idle_timer_task(qc))
4684 goto err;
4685
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004686 /* NOTE: the socket address has been concatenated to the destination ID
4687 * chosen by the client for Initial packets.
4688 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004689 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4690 salt = initial_salt_draft_29;
4691 salt_len = sizeof initial_salt_draft_29;
4692 }
4693 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004694 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004695 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004696 goto err;
4697 }
4698
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004699 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004700 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004701 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004702
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004703 /* If the insertion failed, it means that another
4704 * thread has already allocated a QUIC connection for
4705 * the same CID. Liberate our allocated connection.
4706 */
4707 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004708 qc_to_purge = qc;
4709
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004710 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4711 pkt->qc = qc;
4712 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004713
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004714 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004715 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004716 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004717 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004718 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004719 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004720 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721 }
4722 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004723 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004724 }
4725 }
4726 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004727 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004728 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4729 goto err;
4730 }
4731
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004732 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004733 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004734
4735 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4736 * they must have the same DCID.
4737 */
4738 if (!first_pkt &&
4739 (pkt->dcid.len != dgram->dcid_len ||
4740 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4741 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4742 goto err;
4743 }
4744
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004745 buf += QUIC_HAP_CID_LEN;
4746
4747 /* A short packet is the last one of a UDP datagram. */
4748 payload = buf;
4749 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004750
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004751 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004752 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004753 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004754 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4755 goto err;
4756 }
4757
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004758 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004759 }
4760
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004761
4762 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4763 * they must have the same DCID.
4764 *
4765 * This check must be done after the final update to pkt.len to
4766 * properly drop the packet on failure.
4767 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004768 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004769 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004770 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4771 QUIC_EV_CONN_LPKT, qc);
4772 /* Reset the anti-amplification bit. It will be set again
4773 * when sending the next packet if reached again.
4774 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004775 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
4776 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004777 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004778 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004779
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004780 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004781
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004782 if (qc->err_code) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004783 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004784 goto out;
4785 }
4786
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004787 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004788 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004789 b_cspace = b_contig_space(&qc->rx.buf);
4790 if (b_cspace < pkt->len) {
4791 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004792 if (b_cspace) {
4793 b_putchr(&qc->rx.buf, 0x00);
4794 b_cspace--;
4795 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004796 b_add(&qc->rx.buf, b_cspace);
4797 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004798 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004799 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004800 goto err;
4801 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004802 }
4803
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004804 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004805 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004806 goto err;
4807 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004808
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004809 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004810 if (pkt->aad_len)
4811 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004812 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004813 /* Wake up the connection packet handler task from here only if all
4814 * the contexts have been initialized, especially the mux context
4815 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4816 * will start it if these contexts for the connection are not already
4817 * initialized.
4818 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004819 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004820 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004821 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004822
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004823 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004824
4825 return pkt->len;
4826
4827 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004828 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4829 * This cannot be done by this thread.
4830 */
4831 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004832 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004833 if (conn_ctx && conn_ctx->wait_event.tasklet)
4834 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4835 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004836 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004837 if (!pkt->len)
4838 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004839 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004840 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004841
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004842 return -1;
4843}
4844
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004845/* This function builds into <buf> buffer a QUIC long packet header.
4846 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004847 */
4848static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4849 int type, size_t pn_len, struct quic_conn *conn)
4850{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004851 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004852 return 0;
4853
4854 /* #0 byte flags */
4855 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4856 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4857 /* Version */
4858 quic_write_uint32(buf, end, conn->version);
4859 *(*buf)++ = conn->dcid.len;
4860 /* Destination connection ID */
4861 if (conn->dcid.len) {
4862 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4863 *buf += conn->dcid.len;
4864 }
4865 /* Source connection ID */
4866 *(*buf)++ = conn->scid.len;
4867 if (conn->scid.len) {
4868 memcpy(*buf, conn->scid.data, conn->scid.len);
4869 *buf += conn->scid.len;
4870 }
4871
4872 return 1;
4873}
4874
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004875/* This function builds into <buf> buffer a QUIC short packet header.
4876 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004877 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004878static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4879 size_t pn_len, struct quic_conn *conn,
4880 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004881{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004882 if (end - *buf < 1 + conn->dcid.len)
4883 return 0;
4884
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004885 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004886 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4887 ((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 +01004888 /* Destination connection ID */
4889 if (conn->dcid.len) {
4890 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4891 *buf += conn->dcid.len;
4892 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004893
4894 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004895}
4896
4897/* Apply QUIC header protection to the packet with <buf> as first byte address,
4898 * <pn> as address of the Packet number field, <pnlen> being this field length
4899 * with <aead> as AEAD cipher and <key> as secret key.
4900 * Returns 1 if succeeded or 0 if failed.
4901 */
4902static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4903 const EVP_CIPHER *aead, const unsigned char *key)
4904{
4905 int i, ret, outlen;
4906 EVP_CIPHER_CTX *ctx;
4907 /* We need an IV of at least 5 bytes: one byte for bytes #0
4908 * and at most 4 bytes for the packet number
4909 */
4910 unsigned char mask[5] = {0};
4911
4912 ret = 0;
4913 ctx = EVP_CIPHER_CTX_new();
4914 if (!ctx)
4915 return 0;
4916
4917 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4918 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4919 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4920 goto out;
4921
4922 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4923 for (i = 0; i < pnlen; i++)
4924 pn[i] ^= mask[i + 1];
4925
4926 ret = 1;
4927
4928 out:
4929 EVP_CIPHER_CTX_free(ctx);
4930
4931 return ret;
4932}
4933
4934/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4935 * ACK ranges if needed to a value below <limit> in bytes.
4936 * Return 1 if succeeded, 0 if not.
4937 */
4938static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4939{
4940 size_t room, ack_delay_sz;
4941
4942 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4943 /* A frame is made of 1 byte for the frame type. */
4944 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004945 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004946 return 0;
4947
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004948 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004949}
4950
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004951/* Prepare into <outlist> as most as possible ack-eliciting frame from their
4952 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
4953 * with <room> as available room, and <*len> the packet Length field initialized
4954 * with the number of bytes already present in this buffer which must be taken
4955 * into an account for the Length packet field value. <headlen> is the number of
4956 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004957 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004958 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004959 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004960 * Return 1 if succeeded, 0 if not.
4961 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004962static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004963 size_t room, size_t *len, size_t headlen,
4964 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004965 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004966{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004967 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004968 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004969
Frédéric Lécailleea604992020-12-24 13:01:37 +01004970 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004971 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004972 return 0;
4973
Frédéric Lécailleea604992020-12-24 13:01:37 +01004974 /* If we are not probing we must take into an account the congestion
4975 * control window.
4976 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004977 if (!qel->pktns->tx.pto_probe) {
4978 size_t remain = quic_path_prep_data(qc->path);
4979
4980 if (headlen > remain)
4981 return 0;
4982
4983 room = QUIC_MIN(room, remain - headlen);
4984 }
4985
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004986 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004987 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004988 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004989 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004990 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004991
Frédéric Lécailleea604992020-12-24 13:01:37 +01004992 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004993 break;
4994
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004995 switch (cf->type) {
4996 case QUIC_FT_CRYPTO:
4997 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004998 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004999 /* Compute the length of this CRYPTO frame header */
5000 hlen = 1 + quic_int_getsize(cf->crypto.offset);
5001 /* Compute the data length of this CRyPTO frame. */
5002 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
5003 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005004 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005005 if (!dlen)
5006 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005007
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005008 /* CRYPTO frame length. */
5009 flen = hlen + quic_int_getsize(dlen) + dlen;
5010 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005011 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005012 /* Add the CRYPTO data length and its encoded length to the packet
5013 * length and the length of this length.
5014 */
5015 *len += flen;
5016 room -= flen;
5017 if (dlen == cf->crypto.len) {
5018 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005019 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005020 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005021 }
5022 else {
5023 struct quic_frame *new_cf;
5024
5025 new_cf = pool_alloc(pool_head_quic_frame);
5026 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005027 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005028 return 0;
5029 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005030
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005031 new_cf->type = QUIC_FT_CRYPTO;
5032 new_cf->crypto.len = dlen;
5033 new_cf->crypto.offset = cf->crypto.offset;
5034 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005035 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005036 /* Consume <dlen> bytes of the current frame. */
5037 cf->crypto.len -= dlen;
5038 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005039 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005040 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005041
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005042 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005043 /* Note that these frames are accepted in short packets only without
5044 * "Length" packet field. Here, <*len> is used only to compute the
5045 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005046 *
5047 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005048 * excepting the variable ones. Note that +1 is for the type of this frame.
5049 */
5050 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005051 ((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 +02005052 /* Compute the data length of this STREAM frame. */
5053 avail_room = room - hlen - *len;
5054 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005055 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005056
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005057 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005058 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005059 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5060 dlen = max_available_room(avail_room, &dlen_sz);
5061 if (dlen > cf->stream.len) {
5062 dlen = cf->stream.len;
5063 }
5064 dlen_sz = quic_int_getsize(dlen);
5065 flen = hlen + dlen_sz + dlen;
5066 }
5067 else {
5068 dlen = QUIC_MIN(avail_room, cf->stream.len);
5069 flen = hlen + dlen;
5070 }
5071 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005072 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005073 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005074 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005075 /* Add the STREAM data length and its encoded length to the packet
5076 * length and the length of this length.
5077 */
5078 *len += flen;
5079 room -= flen;
5080 if (dlen == cf->stream.len) {
5081 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005082 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005083 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005084
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005085 /* The MUX stream might be released at this
5086 * stage. This can most notably happen on
5087 * retransmission.
5088 */
5089 if (qc->mux_state == QC_MUX_READY &&
5090 !cf->stream.stream->release) {
5091 qcc_streams_sent_done(cf->stream.stream->ctx,
5092 cf->stream.len,
5093 cf->stream.offset.key);
5094 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005095 }
5096 else {
5097 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005098 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005099
5100 new_cf = pool_zalloc(pool_head_quic_frame);
5101 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005102 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005103 return 0;
5104 }
5105
5106 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005107 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005108 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005109 new_cf->stream.id = cf->stream.id;
5110 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5111 new_cf->stream.offset = cf->stream.offset;
5112 new_cf->stream.len = dlen;
5113 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5114 /* FIN bit reset */
5115 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5116 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005117 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005118 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5119 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005120 cf_buf = b_make(b_orig(cf->stream.buf),
5121 b_size(cf->stream.buf),
5122 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005123 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005124 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005125 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005126
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005127 /* The MUX stream might be released at this
5128 * stage. This can most notably happen on
5129 * retransmission.
5130 */
5131 if (qc->mux_state == QC_MUX_READY &&
5132 !cf->stream.stream->release) {
5133 qcc_streams_sent_done(new_cf->stream.stream->ctx,
5134 new_cf->stream.len,
5135 new_cf->stream.offset.key);
5136 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005137 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005138
5139 /* TODO the MUX is notified about the frame sending via
5140 * previous qcc_streams_sent_done call. However, the
5141 * sending can fail later, for example if the sendto
5142 * system call returns an error. As the MUX has been
5143 * notified, the transport layer is responsible to
5144 * bufferize and resent the announced data later.
5145 */
5146
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005147 break;
5148
5149 default:
5150 flen = qc_frm_len(cf);
5151 BUG_ON(!flen);
5152 if (flen > room)
5153 continue;
5154
5155 *len += flen;
5156 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005157 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005158 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005159 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005160 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01005161 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005162 }
5163
Frédéric Lécailleea604992020-12-24 13:01:37 +01005164 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005165}
5166
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005167/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005168 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
5169 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005170 * filling the buffer with as much frames as possible from <frms> list of
5171 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005172 * 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 +02005173 * 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 +02005174 * having returned from this function.
5175 * 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 +01005176 * number field in this packet. <pn_len> will also have the packet number
5177 * length as value.
5178 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005179 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005180 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005181static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
5182 size_t dglen, struct quic_tx_packet *pkt,
5183 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005184 int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005185 struct quic_enc_level *qel, struct quic_conn *qc,
5186 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005187{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005188 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005189 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005190 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
5191 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005192 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005193 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005194 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005195 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005196 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005197 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005198
Frédéric Lécailleea604992020-12-24 13:01:37 +01005199 /* Length field value with CRYPTO frames if present. */
5200 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005201 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005202 /* When not probing, and no immediate close is required, reduce the size of this
5203 * buffer to respect the congestion controller window.
5204 * This size will be limited if we have ack-eliciting frames to send from <frms>.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005205 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005206 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005207 size_t path_room;
5208
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005209 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005210 if (end - beg > path_room)
5211 end = beg + path_room;
5212 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005213
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005214 /* Ensure there is enough room for the TLS encryption tag and a zero token
5215 * length field if any.
5216 */
5217 if (end - pos < QUIC_TLS_TAG_LEN +
5218 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
5219 goto no_room;
5220
5221 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005222 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005223 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005224 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005225 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005226 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005227 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005228 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005229 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005230 goto no_room;
5231
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005232 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
5233 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005234 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005235 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005236 /* Build an ACK frame if required. */
5237 ack_frm_len = 0;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005238 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
5239 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005240 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005241 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005242 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
5243 * smallest frame (PING) and <*pn_len> more for the packet number. Note
5244 * that from here, we do not know if we will have to send a PING frame.
5245 * This will be decided after having computed the ack-eliciting frames
5246 * to be added to this packet.
5247 */
5248 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 +01005249 if (!ack_frm_len)
5250 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005251 }
5252
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005253 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005254 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005255 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005256 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005257 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01005258
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005259 /* Initialize the length of the frames built below to <len>.
5260 * If any frame could be successfully built by qc_build_frms(),
5261 * we will have len_frms > len.
5262 */
5263 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005264 if (!qc_build_frms(&frm_list, frms,
5265 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01005266 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005267 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005268 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005269 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005270
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005271 /* Length (of the remaining data). Must not fail because, the buffer size
5272 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
5273 * for the encryption tag. It must be taken into an account for the length
5274 * of this packet.
5275 */
5276 if (len_frms)
5277 len = len_frms + QUIC_TLS_TAG_LEN;
5278 else
5279 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005280 /* CONNECTION_CLOSE frame */
5281 if (cc) {
5282 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005283
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005284 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005285 len += qc_frm_len(&cc_frm);
5286 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005287 add_ping_frm = 0;
5288 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005289 len_sz = quic_int_getsize(len);
5290 /* Add this packet size to <dglen> */
5291 dglen += head_len + len_sz + len;
5292 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
5293 /* This is a maximum padding size */
5294 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
5295 /* The length field value is of this packet is <len> + <padding_len>
5296 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05005297 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005298 * sizes from <padding_len>.
5299 */
5300 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
5301 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005302 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005303 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005304 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005305 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005306 add_ping_frm = 1;
5307 len += 1;
5308 }
5309 /* 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 +01005310 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005311 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
5312 }
5313
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005314 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
5315 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005316
5317 /* Packet number field address. */
5318 *buf_pn = pos;
5319
5320 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005321 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5322 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005323
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005324 if (ack_frm_len) {
5325 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
5326 goto no_room;
5327
5328 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005329 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005330 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005331
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005332 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005333 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005334 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005335 unsigned char *spos = pos;
5336
5337 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005338 ssize_t room = end - pos;
5339 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005340 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005341 /* TODO: this should not have happened if qc_build_frms()
5342 * had correctly computed and sized the frames to be
5343 * added to this packet. Note that <cf> was added
5344 * from <frm_list> to <frms> list by qc_build_frms().
5345 */
5346 LIST_DELETE(&cf->list);
5347 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005348 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005349 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005350
5351 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005352 quic_tx_packet_refinc(pkt);
5353 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005354 }
5355 }
5356
5357 /* Build a PING frame if needed. */
5358 if (add_ping_frm) {
5359 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005360 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005361 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005362 }
5363
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005364 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005365 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005366 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005367
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005368 /* Build a PADDING frame if needed. */
5369 if (padding_len) {
5370 frm.type = QUIC_FT_PADDING;
5371 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005372 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005373 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005374 }
5375
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005376 /* If this packet is ack-eliciting and we are probing let's
5377 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005378 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005379 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5380 qel->pktns->tx.pto_probe)
5381 qel->pktns->tx.pto_probe--;
5382
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005383 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005384 LIST_SPLICE(&pkt->frms, &frm_list);
5385 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005386
5387 return 1;
5388
5389 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005390 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005391 LIST_SPLICE(frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005392 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005393 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005394}
5395
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005396static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005397{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005398 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005399 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005400 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005401 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005402 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005403 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005404 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005405 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005406 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005407 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005408}
5409
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005410/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005411 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
5412 * of prebuilt frames.
5413 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005414 * Return -2 if the packet could not be allocated or encrypted for any reason,
5415 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01005416 * XXX NOTE XXX
5417 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
5418 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
5419 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005420 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005421static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5422 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005423 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005424 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005425 int pkt_type, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005426{
5427 /* The pointer to the packet number field. */
5428 unsigned char *buf_pn;
5429 unsigned char *beg, *end, *payload;
5430 int64_t pn;
5431 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005432 struct quic_tls_ctx *tls_ctx;
5433 struct quic_tx_packet *pkt;
5434
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005435 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005436 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005437 pkt = pool_alloc(pool_head_quic_tx_packet);
5438 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005439 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005440 *err = -2;
5441 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005442 }
5443
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005444 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005445 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005446 pn_len = 0;
5447 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005448
5449 pn = qel->pktns->tx.next_pn + 1;
5450 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005451 padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005452 *err = -1;
5453 goto err;
5454 }
5455
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005456 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005457 payload = buf_pn + pn_len;
5458 payload_len = end - payload;
5459 aad_len = payload - beg;
5460
5461 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005462 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005463 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005464 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005465 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005466
5467 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005468 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005469 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5470 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005471 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005472 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005473 goto err;
5474 }
5475
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005476 /* Consume a packet number */
5477 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005478 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01005479 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005480 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005481 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5482 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005483 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005484 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005485 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005486 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005487 pkt->in_flight_len = pkt->len;
5488 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005489 }
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005490 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
5491 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
5492 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
5493 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005494
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005495 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005496 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005497
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005498 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005499
5500 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005501 /* TODO: what about the frames which have been built
5502 * for this packet.
5503 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005504 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005505 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005506 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005507}
5508
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005509
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005510/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5511 * event subscriber <es> is not allowed to change from a previous call as long
5512 * as at least one event is still subscribed. The <event_type> must only be a
5513 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5514 */
5515static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5516{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005517 struct qcc *qcc = conn->qc->qcc;
5518
5519 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5520 BUG_ON(qcc->subs && qcc->subs != es);
5521
5522 es->events |= event_type;
5523 qcc->subs = es;
5524
5525 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005526 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005527
5528 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005529 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005530
5531 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005532}
5533
5534/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5535 * The <es> pointer is not allowed to differ from the one passed to the
5536 * subscribe() call. It always returns zero.
5537 */
5538static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5539{
5540 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5541}
5542
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005543/* Store in <xprt_ctx> the context attached to <conn>.
5544 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005545 */
5546static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5547{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005548 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005549
5550 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5551
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005552 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005553 if (*xprt_ctx)
5554 goto out;
5555
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005556 *xprt_ctx = conn->qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005557
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005558 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005559 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005560
5561 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005562}
5563
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005564/* Start the QUIC transport layer */
5565static int qc_xprt_start(struct connection *conn, void *ctx)
5566{
5567 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005568 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005569
5570 qc = conn->qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005571 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5572 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5573 return 0;
5574 }
5575
5576 /* mux-quic can now be considered ready. */
5577 qc->mux_state = QC_MUX_READY;
5578
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005579 tasklet_wakeup(qctx->wait_event.tasklet);
5580 return 1;
5581}
5582
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005583/* transport-layer operations for QUIC connections. */
5584static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005585 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005586 .subscribe = quic_conn_subscribe,
5587 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005588 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005589 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005590 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5591 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005592 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005593 .name = "QUIC",
5594};
5595
5596__attribute__((constructor))
5597static void __quic_conn_init(void)
5598{
5599 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5600 xprt_register(XPRT_QUIC, &ssl_quic);
5601}
5602
5603__attribute__((destructor))
5604static void __quic_conn_deinit(void)
5605{
5606 BIO_meth_free(ha_quic_meth);
5607}
5608
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005609/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5610 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005611 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005612 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005613struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005614{
5615 unsigned char *pos;
5616 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005617 struct quic_dghdlr *dghdlr = ctx;
5618 struct quic_dgram *dgram;
5619 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005620
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005621 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005622 pos = dgram->buf;
5623 end = pos + dgram->len;
5624 do {
5625 int ret;
5626 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005627
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005628 pkt = pool_zalloc(pool_head_quic_rx_packet);
5629 if (!pkt)
5630 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005631
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005632 quic_rx_packet_refinc(pkt);
5633 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5634 first_pkt = 0;
5635 pos += pkt->len;
5636 quic_rx_packet_refdec(pkt);
5637 if (ret == -1)
5638 /* If the packet length could not be found, we cannot continue. */
5639 break;
5640 } while (pos < end);
5641
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005642 /* Increasing the received bytes counter by the UDP datagram length
5643 * if this datagram could be associated to a connection.
5644 */
5645 if (dgram->qc)
5646 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01005647
5648 /* Mark this datagram as consumed */
5649 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005650 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005651
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005652 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005653
5654 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005655 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005656}
5657
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005658/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5659 * Returns 1 if succeeded, 0 if not.
5660 */
5661static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5662 unsigned char **dcid, size_t *dcid_len)
5663{
5664 int long_header;
5665 size_t minlen, skip;
5666
5667 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5668 goto err;
5669
5670 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5671 minlen = long_header ?
5672 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5673 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01005674 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005675 goto err;
5676
5677 buf += skip;
5678 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5679 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5680 goto err;
5681
5682 *dcid = buf;
5683
5684 return 1;
5685
5686 err:
5687 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5688 return 0;
5689}
5690
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005691/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5692 * correct datagram handler.
5693 * Return 1 if a correct datagram could be found, 0 if not.
5694 */
5695int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5696 struct sockaddr_storage *saddr,
5697 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005698{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005699 struct quic_dgram *dgram;
5700 unsigned char *dcid;
5701 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005702 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005703
5704 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005705 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005706
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005707 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005708 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005709 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005710
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005711 cid_tid = quic_get_cid_tid(dcid);
5712
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005713 /* All the members must be initialized! */
5714 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005715 dgram->buf = buf;
5716 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005717 dgram->dcid = dcid;
5718 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005719 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005720 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005721 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005722 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005723
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005724 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005725
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005726 return 1;
5727
5728 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005729 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005730}
5731
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005732/* Allocate a new stream descriptor with id <id>. The caller is responsible to
5733 * store the stream in the appropriate tree.
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005734 *
5735 * Returns the newly allocated instance on success or else NULL.
5736 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005737struct qc_stream_desc *qc_stream_desc_new(uint64_t id, void *ctx)
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005738{
5739 struct qc_stream_desc *stream;
5740
5741 stream = pool_alloc(pool_head_quic_conn_stream);
5742 if (!stream)
5743 return NULL;
5744
5745 stream->by_id.key = id;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005746 stream->by_id.node.leaf_p = NULL;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005747
5748 stream->buf = BUF_NULL;
5749 stream->acked_frms = EB_ROOT;
5750 stream->ack_offset = 0;
5751 stream->release = 0;
5752 stream->ctx = ctx;
5753
5754 return stream;
5755}
5756
5757/* Mark the stream descriptor <stream> as released by the upper layer. It will
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005758 * be freed as soon as all its buffered data are acknowledged. In the meantime,
5759 * the stream is stored in the <qc> tree : thus it must have been removed from
5760 * any other tree before calling this function.
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005761 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005762void qc_stream_desc_release(struct qc_stream_desc *stream,
5763 struct quic_conn *qc)
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005764{
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005765 BUG_ON(stream->by_id.node.leaf_p);
5766
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005767 stream->release = 1;
5768 stream->ctx = NULL;
5769
5770 if (!b_data(&stream->buf))
5771 qc_stream_desc_free(stream);
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005772 else
5773 eb64_insert(&qc->streams_by_id, &stream->by_id);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005774}
5775
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005776/* Function to automatically activate QUIC traces on stdout.
5777 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5778 * Main use for now is in the docker image for QUIC interop testing.
5779 */
5780static void quic_init_stdout_traces(void)
5781{
5782#ifdef ENABLE_QUIC_STDOUT_TRACES
5783 trace_quic.sink = sink_find("stdout");
5784 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005785 trace_quic.state = TRACE_STATE_RUNNING;
5786#endif
5787}
5788INITCALL0(STG_INIT, quic_init_stdout_traces);
5789
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005790/*
5791 * Local variables:
5792 * c-indent-level: 8
5793 * c-basic-offset: 8
5794 * End:
5795 */