blob: 0c503cf7519ef0c0d363b9ed944643b716a8345a [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>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010015#include <stdio.h>
16#include <stdlib.h>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
22#include <netinet/tcp.h>
23
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020024#include <import/ebmbtree.h>
25
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010026#include <haproxy/buf-t.h>
27#include <haproxy/compat.h>
28#include <haproxy/api.h>
29#include <haproxy/debug.h>
30#include <haproxy/tools.h>
31#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010032
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010038#include <haproxy/hq_interop.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010039#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010040#include <haproxy/mux_quic.h>
Amaury Denoyelle3db98e92022-05-13 15:41:04 +020041#include <haproxy/ncbuf.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écaille3ccea6d2022-05-23 22:54:54 +020048#include <haproxy/quic_stats.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020049#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020050#include <haproxy/quic_tp.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020051#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010052#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010053#include <haproxy/quic_tls.h>
54#include <haproxy/ssl_sock.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010055#include <haproxy/task.h>
56#include <haproxy/trace.h>
57#include <haproxy/xprt_quic.h>
58
Amaury Denoyellea22d8602021-11-10 15:17:56 +010059/* list of supported QUIC versions by this implementation */
Frédéric Lécaille301425b2022-06-14 17:40:39 +020060const struct quic_version quic_versions[] = {
Frédéric Lécaille86845c52022-06-08 19:28:36 +020061 {
62 .num = QUIC_PROTOCOL_VERSION_DRAFT_29,
63 .initial_salt = initial_salt_draft_29,
64 .initial_salt_len = sizeof initial_salt_draft_29,
65 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020066 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020067 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020068 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020069 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020070 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020071 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020072 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020073 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT,
74 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT,
75 },
76 {
77 .num = QUIC_PROTOCOL_VERSION_1,
78 .initial_salt = initial_salt_v1,
79 .initial_salt_len = sizeof initial_salt_v1,
80 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020081 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020082 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020083 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020084 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020085 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020086 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020087 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020088 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1,
89 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1,
90 },
91 {
92 .num = QUIC_PROTOCOL_VERSION_2_DRAFT,
93 .initial_salt = initial_salt_v2_draft,
94 .initial_salt_len = sizeof initial_salt_v2_draft,
95 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020096 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020097 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020098 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020099 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +0200100 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200101 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +0200102 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200103 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2_DRAFT,
104 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2_DRAFT,
105 },
Amaury Denoyellea22d8602021-11-10 15:17:56 +0100106};
107
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200108/* The total number of supported versions */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200109const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions;
110/* Listener only preferred version */
111const struct quic_version *preferred_version;
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200112
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100113/* trace source and events */
114static void quic_trace(enum trace_level level, uint64_t mask, \
115 const struct trace_source *src,
116 const struct ist where, const struct ist func,
117 const void *a1, const void *a2, const void *a3, const void *a4);
118
119static const struct trace_event quic_trace_events[] = {
120 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
121 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
122 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
123 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
124 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
125 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
126 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500127 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200128 { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100129 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
130 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200131 { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100132 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
133 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
134 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
135 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
136 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
137 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
138 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
139 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
140 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200141 { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100142 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
143 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
144 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
145 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
146 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100147 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
148 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
149 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
150 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
151 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
152 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100153 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200154 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
155 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100156 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
157 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100158 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200159 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200160 { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"},
Frédéric Lécaillec7785b52022-05-23 09:08:54 +0200161 { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"},
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200162 { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"},
163 { .mask = QUIC_EV_CONN_SUB, .name = "xprt_sub", .desc = "RX/TX subcription or unsubscription to QUIC xprt"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100164 { /* end */ }
165};
166
167static const struct name_desc quic_trace_lockon_args[4] = {
168 /* arg1 */ { /* already used by the connection */ },
169 /* arg2 */ { .name="quic", .desc="QUIC transport" },
170 /* arg3 */ { },
171 /* arg4 */ { }
172};
173
174static const struct name_desc quic_trace_decoding[] = {
175#define QUIC_VERB_CLEAN 1
176 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
177 { /* end */ }
178};
179
180
181struct trace_source trace_quic = {
182 .name = IST("quic"),
183 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100184 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100185 .default_cb = quic_trace,
186 .known_events = quic_trace_events,
187 .lockon_args = quic_trace_lockon_args,
188 .decoding = quic_trace_decoding,
189 .report_events = ~0, /* report everything by default */
190};
191
192#define TRACE_SOURCE &trace_quic
193INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
194
195static BIO_METHOD *ha_quic_meth;
196
Willy Tarreaub8dec4a2022-06-23 11:02:08 +0200197DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100198DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100199DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Willy Tarreaub8dec4a2022-06-23 11:02:08 +0200200 "quic_conn_ctx", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100201DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100202DECLARE_POOL(pool_head_quic_connection_id,
Willy Tarreaub8dec4a2022-06-23 11:02:08 +0200203 "quic_connnection_id", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100204DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Willy Tarreaub8dec4a2022-06-23 11:02:08 +0200205DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet));
206DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet));
207DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm));
208DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf", sizeof(struct quic_crypto_buf));
209DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame));
210DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100211
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200212static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200213 struct quic_enc_level *qel, struct quic_tls_ctx *ctx,
214 struct list *frms, struct quic_conn *qc,
215 const struct quic_version *ver, size_t dglen, int pkt_type,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +0200216 int force_ack, int padding, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100217static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécaille47756802022-03-25 09:12:16 +0100218static void qc_idle_timer_do_rearm(struct quic_conn *qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100219static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +0200220static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc);
221static int quic_conn_init_timer(struct quic_conn *qc);
222static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100223
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100224/* Only for debug purpose */
225struct enc_debug_info {
226 unsigned char *payload;
227 size_t payload_len;
228 unsigned char *aad;
229 size_t aad_len;
230 uint64_t pn;
231};
232
233/* Initializes a enc_debug_info struct (only for debug purpose) */
234static inline void enc_debug_info_init(struct enc_debug_info *edi,
235 unsigned char *payload, size_t payload_len,
236 unsigned char *aad, size_t aad_len, uint64_t pn)
237{
238 edi->payload = payload;
239 edi->payload_len = payload_len;
240 edi->aad = aad;
241 edi->aad_len = aad_len;
242 edi->pn = pn;
243}
244
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100245/* Trace callback for QUIC.
246 * These traces always expect that arg1, if non-null, is of type connection.
247 */
248static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
249 const struct ist where, const struct ist func,
250 const void *a1, const void *a2, const void *a3, const void *a4)
251{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100252 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100253
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100254 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100255 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100256
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100257 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200258 if (mask & QUIC_EV_CONN_INIT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100259 chunk_appendf(&trace_buf, "\n odcid");
260 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100261 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100262 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100263 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100264 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100265 }
266
Frédéric Lécaillec7785b52022-05-23 09:08:54 +0200267 if (mask & QUIC_EV_TRANSP_PARAMS) {
268 const struct quic_transport_params *p = a2;
Frédéric Lécaille4f5777a2022-06-20 19:39:26 +0200269 quic_transport_params_dump(&trace_buf, qc, p);
Frédéric Lécaillec7785b52022-05-23 09:08:54 +0200270 }
271
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100272 if (mask & QUIC_EV_CONN_ADDDATA) {
273 const enum ssl_encryption_level_t *level = a2;
274 const size_t *len = a3;
275
276 if (level) {
277 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
278
279 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
280 }
281 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100282 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100283 }
284 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
285 /* Initial read & write secrets. */
286 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
287 const unsigned char *rx_sec = a2;
288 const unsigned char *tx_sec = a3;
289
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100290 tls_ctx = &qc->els[level].tls_ctx;
291 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100292 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
293 if (rx_sec)
294 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100295 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100296 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
297 if (tx_sec)
298 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100299 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 }
301 }
302 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
303 const enum ssl_encryption_level_t *level = a2;
304 const unsigned char *secret = a3;
305 const size_t *secret_len = a4;
306
307 if (level) {
308 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
309
310 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
311 if (secret && secret_len)
312 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100313 tls_ctx = &qc->els[lvl].tls_ctx;
314 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
315 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100316 }
317 }
318
319 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
320 const enum ssl_encryption_level_t *level = a2;
321 const unsigned char *secret = a3;
322 const size_t *secret_len = a4;
323
324 if (level) {
325 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
326
327 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
328 if (secret && secret_len)
329 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100330 tls_ctx = &qc->els[lvl].tls_ctx;
331 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
332 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100333 }
334
335 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100336
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200337 if (mask & QUIC_EV_CONN_FRMLIST) {
338 const struct list *l = a2;
339
340 if (l) {
341 const struct quic_frame *frm;
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200342 list_for_each_entry(frm, l, list) {
343 chunk_appendf(&trace_buf, " frm@%p", frm);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200344 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200345 }
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200346 }
347 }
348
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200349 if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100350 const struct quic_tx_packet *pkt = a2;
351 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100352 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100353
354 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100355 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100356 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100357 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100358 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100359 (unsigned long long)qc->path->cwnd,
360 (unsigned long long)qc->path->prep_in_flight,
361 (unsigned long long)qc->path->in_flight,
362 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100363 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100364 }
365 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200366 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100367 if (pkt->pn_node.key != (uint64_t)-1)
368 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200369 list_for_each_entry(frm, &pkt->frms, list) {
370 chunk_appendf(&trace_buf, " frm@%p", frm);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100371 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200372 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100373 }
374
375 if (room) {
376 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
377 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
378 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100379 }
380 }
381
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100382 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100383 const enum quic_handshake_state *state = a2;
384 const int *err = a3;
385
386 if (state)
387 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
388 if (err)
389 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
390 }
391
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100392 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100393 const struct quic_rx_packet *pkt = a2;
394 const unsigned long *pktlen = a3;
395 const SSL *ssl = a4;
396
397 if (pkt) {
Frédéric Lécaille3dfd4c42022-04-05 15:29:14 +0200398 chunk_appendf(&trace_buf, " pkt@%p", pkt);
399 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
400 chunk_appendf(&trace_buf, " kp=%d",
401 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
402 chunk_appendf(&trace_buf, " el=%c",
403 quic_packet_type_enc_level_char(pkt->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100404 if (pkt->pnl)
405 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
406 (unsigned long long)pkt->pn);
407 if (pkt->token_len)
408 chunk_appendf(&trace_buf, " toklen=%llu",
409 (unsigned long long)pkt->token_len);
410 if (pkt->aad_len)
411 chunk_appendf(&trace_buf, " aadlen=%llu",
412 (unsigned long long)pkt->aad_len);
413 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
414 pkt->flags, (unsigned long long)pkt->len);
415 }
416 if (pktlen)
417 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
418 if (ssl) {
419 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
420 chunk_appendf(&trace_buf, " el=%c",
421 quic_enc_level_char(ssl_to_quic_enc_level(level)));
422 }
423 }
424
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200425 if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100426 const struct quic_rx_packet *pkt = a2;
427 const struct quic_rx_crypto_frm *cf = a3;
428 const SSL *ssl = a4;
429
430 if (pkt)
431 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
432 quic_packet_type_enc_level_char(pkt->type),
433 (unsigned long long)pkt->pn);
434 if (cf)
435 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
436 (unsigned long long)cf->offset_node.key,
437 (unsigned long long)cf->len);
438 if (ssl) {
439 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200440 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100441 quic_enc_level_char(ssl_to_quic_enc_level(level)));
442 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100443
Amaury Denoyelle57e6db72022-07-13 15:07:56 +0200444 if (qc->err.code)
445 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100446 }
447
448 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
449 const struct quic_frame *frm = a2;
450
451 if (frm)
452 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
453 }
454
455 if (mask & QUIC_EV_CONN_PHPKTS) {
456 const struct quic_enc_level *qel = a2;
457
458 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100459 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100460 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100461 " 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 +0100462 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200463 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200464 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100465 (unsigned long long)qc->path->cwnd,
466 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100467 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100468 (unsigned long long)pktns->tx.in_flight,
469 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100470 }
471 }
472
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100473 if (mask & QUIC_EV_CONN_ENCPKT) {
474 const struct enc_debug_info *edi = a2;
475
476 if (edi)
477 chunk_appendf(&trace_buf,
478 " payload=@%p payload_len=%llu"
479 " aad=@%p aad_len=%llu pn=%llu",
480 edi->payload, (unsigned long long)edi->payload_len,
481 edi->aad, (unsigned long long)edi->aad_len,
482 (unsigned long long)edi->pn);
483 }
484
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100485 if (mask & QUIC_EV_CONN_RMHP) {
486 const struct quic_rx_packet *pkt = a2;
487
488 if (pkt) {
489 const int *ret = a3;
490
491 chunk_appendf(&trace_buf, " pkt@%p", pkt);
492 if (ret && *ret)
493 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
494 pkt->pnl, (unsigned long long)pkt->pn);
495 }
496 }
497
498 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200499 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500 const unsigned long *val1 = a3;
501 const unsigned long *val2 = a4;
502
Frédéric Lécaille2f163482022-08-19 09:23:15 +0200503 if (frm) {
504 chunk_appendf(&trace_buf, " frm@%p", frm);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100505 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille2f163482022-08-19 09:23:15 +0200506 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100507 if (val1)
508 chunk_appendf(&trace_buf, " %lu", *val1);
509 if (val2)
510 chunk_appendf(&trace_buf, "..%lu", *val2);
511 }
512
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100513 if (mask & QUIC_EV_CONN_ACKSTRM) {
514 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200515 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100516
517 if (s)
518 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200519 if (stream)
520 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100521 }
522
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100523 if (mask & QUIC_EV_CONN_RTTUPDT) {
524 const unsigned int *rtt_sample = a2;
525 const unsigned int *ack_delay = a3;
526 const struct quic_loss *ql = a4;
527
528 if (rtt_sample)
529 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
530 if (ack_delay)
531 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
532 if (ql)
533 chunk_appendf(&trace_buf,
534 " srtt=%ums rttvar=%ums min_rtt=%ums",
535 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
536 }
537 if (mask & QUIC_EV_CONN_CC) {
538 const struct quic_cc_event *ev = a2;
539 const struct quic_cc *cc = a3;
540
541 if (a2)
542 quic_cc_event_trace(&trace_buf, ev);
543 if (a3)
544 quic_cc_state_trace(&trace_buf, cc);
545 }
546
547 if (mask & QUIC_EV_CONN_PKTLOSS) {
548 const struct quic_pktns *pktns = a2;
549 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100550
551 if (pktns) {
552 chunk_appendf(&trace_buf, " pktns=%s",
553 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
554 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
555 if (pktns->tx.loss_time)
556 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100557 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100558 }
559 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
560 struct quic_tx_packet *pkt;
561
562 chunk_appendf(&trace_buf, " lost_pkts:");
563 list_for_each_entry(pkt, lost_pkts, list)
564 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
565 }
566 }
567
568 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100569 const struct quic_pktns *pktns = a2;
570 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100571 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100572
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100573 if (ifae_pkts)
574 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
575 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100576 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100577 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100578 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100579 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
580 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100581 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
582 if (pktns->tx.in_flight)
583 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100584 if (pktns->tx.loss_time)
585 chunk_appendf(&trace_buf, " loss_time=%dms",
586 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
587 }
588 if (mask & QUIC_EV_CONN_SPTO) {
589 if (pktns->tx.time_of_last_eliciting)
590 chunk_appendf(&trace_buf, " tole=%dms",
591 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
592 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100593 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100594 }
595 }
596
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200597 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598 chunk_appendf(&trace_buf,
599 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
600 }
601 }
602
603 if (mask & QUIC_EV_CONN_SPPKTS) {
604 const struct quic_tx_packet *pkt = a2;
605
Frédéric Lécaille8ecb7362022-08-04 12:00:00 +0200606 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100607 (unsigned long long)qc->path->cwnd,
608 (unsigned long long)qc->path->prep_in_flight,
609 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100610 if (pkt) {
Frédéric Lécaille449804e2022-09-02 22:16:10 +0200611 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100612 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613 (unsigned long)pkt->pn_node.key,
614 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
615 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100616 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +0200617 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
618 (unsigned long long)qc->rx.bytes,
619 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille449804e2022-09-02 22:16:10 +0200620 list_for_each_entry(frm, &pkt->frms, list) {
621 chunk_appendf(&trace_buf, " frm@%p", frm);
622 chunk_frm_appendf(&trace_buf, frm);
623 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100624 }
625 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100626
627 if (mask & QUIC_EV_CONN_SSLALERT) {
628 const uint8_t *alert = a2;
629 const enum ssl_encryption_level_t *level = a3;
630
631 if (alert)
632 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
633 if (level)
634 chunk_appendf(&trace_buf, " el=%c",
635 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
636 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100637
638 if (mask & QUIC_EV_CONN_BCFRMS) {
639 const size_t *sz1 = a2;
640 const size_t *sz2 = a3;
641 const size_t *sz3 = a4;
642
643 if (sz1)
644 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
645 if (sz2)
646 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
647 if (sz3)
648 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
649 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100650
651 if (mask & QUIC_EV_CONN_PSTRM) {
652 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100653
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200654 if (frm) {
655 chunk_appendf(&trace_buf, " frm@%p", frm);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100656 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200657 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100658 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100659 }
660 if (mask & QUIC_EV_CONN_LPKT) {
661 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200662 const uint64_t *len = a3;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200663 const struct quic_version *ver = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100664
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100665 if (pkt) {
666 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
667 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
668 if (pkt->pn_node.key != (uint64_t)-1)
669 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
670 }
671
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200672 if (len)
673 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200674
675 if (ver)
676 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100677 }
678
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200679 if (mask & QUIC_EV_STATELESS_RST) {
680 const struct quic_cid *cid = a2;
681
682 if (cid)
683 quic_cid_dump(&trace_buf, cid);
684 }
685
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100686}
687
688/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100689static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100690{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200691 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100692
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100693 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100694 return 1;
695
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200696 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
697 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200698 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
699 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200700 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100701 return 1;
702
703 return 0;
704}
705
706/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
707 * both loss detection and PTO and schedule the task assiated to this timer if needed.
708 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100709static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100710{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100711 struct quic_pktns *pktns;
712 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200713 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100714
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100715 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100716 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100717
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100718 pktns = quic_loss_pktns(qc);
719 if (tick_isset(pktns->tx.loss_time)) {
720 qc->timer = pktns->tx.loss_time;
721 goto out;
722 }
723
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100724 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100725 * cancelled for a server which reached the anti-amplification limit.
726 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100727 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200728 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100729 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100730 qc->timer = TICK_ETERNITY;
731 goto out;
732 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100733
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100734 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100735 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100736 /* Timer cancellation. */
737 qc->timer = TICK_ETERNITY;
738 goto out;
739 }
740
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200741 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200742 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100743 if (tick_isset(pto))
744 qc->timer = pto;
745 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100746 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100747 if (tick_is_expired(qc->timer, now_ms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200748 TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100749 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100750 }
751 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200752 TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100753 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100754 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100755 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100756 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100757}
758
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100759/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
760 * connection.
761 * Return 1 if succeeded, 0 if not.
762 */
763static int quic_tls_key_update(struct quic_conn *qc)
764{
765 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
766 struct quic_tls_secrets *rx, *tx;
767 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
768 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200769 const struct quic_version *ver =
770 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200771 int ret = 0;
772
773 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100774
775 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
776 rx = &tls_ctx->rx;
777 tx = &tls_ctx->tx;
778 nxt_rx = &qc->ku.nxt_rx;
779 nxt_tx = &qc->ku.nxt_tx;
780
781 /* Prepare new RX secrets */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200782 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100783 rx->secret, rx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200784 TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
785 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100786 }
787
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200788 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100789 nxt_rx->key, nxt_rx->keylen,
790 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
791 nxt_rx->secret, nxt_rx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200792 TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
793 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100794 }
795
796 /* Prepare new TX secrets */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200797 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100798 tx->secret, tx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200799 TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
800 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100801 }
802
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200803 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100804 nxt_tx->key, nxt_tx->keylen,
805 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
806 nxt_tx->secret, nxt_tx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200807 TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
808 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100809 }
810
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200811 if (nxt_rx->ctx) {
812 EVP_CIPHER_CTX_free(nxt_rx->ctx);
813 nxt_rx->ctx = NULL;
814 }
815
816 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200817 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
818 goto leave;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200819 }
820
821 if (nxt_tx->ctx) {
822 EVP_CIPHER_CTX_free(nxt_tx->ctx);
823 nxt_tx->ctx = NULL;
824 }
825
826 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200827 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
828 goto leave;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200829 }
830
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200831 ret = 1;
832 leave:
833 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc);
834 return ret;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100835}
836
837/* Rotate the Key Update information for <qc> QUIC connection.
838 * Must be used after having updated them.
839 * Always succeeds.
840 */
841static void quic_tls_rotate_keys(struct quic_conn *qc)
842{
843 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
844 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200845 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100846
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200847 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
848
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100849 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200850 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100851 curr_secret = tls_ctx->rx.secret;
852 curr_iv = tls_ctx->rx.iv;
853 curr_key = tls_ctx->rx.key;
854
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200855 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100856 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
857 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
858 tls_ctx->rx.key = qc->ku.nxt_rx.key;
859
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200860 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100861 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
862 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
863 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
864
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200865 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100866 qc->ku.prv_rx.secret = curr_secret;
867 qc->ku.prv_rx.iv = curr_iv;
868 qc->ku.prv_rx.key = curr_key;
869 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
870
871 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200872 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100873 curr_secret = tls_ctx->tx.secret;
874 curr_iv = tls_ctx->tx.iv;
875 curr_key = tls_ctx->tx.key;
876
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200877 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100878 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
879 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
880 tls_ctx->tx.key = qc->ku.nxt_tx.key;
881
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200882 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100883 qc->ku.nxt_tx.secret = curr_secret;
884 qc->ku.nxt_tx.iv = curr_iv;
885 qc->ku.nxt_tx.key = curr_key;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200886
887 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100888}
889
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200890/* returns 0 on error, 1 on success */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100891int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
892 const uint8_t *read_secret,
893 const uint8_t *write_secret, size_t secret_len)
894{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100895 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
896 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 +0100897 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100898 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200899 const struct quic_version *ver =
900 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200901 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100902
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100903 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100904 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200905 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100906 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100907 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100908 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100909
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100910 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200911 TRACE_ERROR("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
912 goto leave;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100913 }
914
915 rx = &tls_ctx->rx;
916 tx = &tls_ctx->tx;
917
918 rx->aead = tx->aead = tls_aead(cipher);
919 rx->md = tx->md = tls_md(cipher);
920 rx->hp = tx->hp = tls_hp(cipher);
921
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200922 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100923 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100924 read_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200925 TRACE_ERROR("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
926 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100927 }
928
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200929 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200930 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
931 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200932 }
933
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200934 if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) {
935 TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
936 goto leave;
937 }
938
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100939 /* Enqueue this connection asap if we could derive O-RTT secrets as
940 * listener. Note that a listener derives only RX secrets for this
941 * level.
942 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200943 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
944 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100945 quic_accept_push_qc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200946 }
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100947
948 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100949 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100950
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200951 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100952 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100953 write_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200954 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
955 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100956 }
957
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200958 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200959 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
960 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200961 }
962
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200963 if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) {
964 TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
965 goto leave;
966 }
967
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100968 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100969 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
970 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
971 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
972
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200973 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100974 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
975 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200976 TRACE_ERROR("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
977 goto leave;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100978 }
979
980 memcpy(rx->secret, read_secret, secret_len);
981 rx->secretlen = secret_len;
982 memcpy(tx->secret, write_secret, secret_len);
983 tx->secretlen = secret_len;
984 /* Initialize all the secret keys lengths */
985 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
986 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200987 if (!quic_tls_key_update(qc)) {
988 // trace already emitted by function above
989 goto leave;
990 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100991 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100992
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100993 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100994 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100995 no_secret:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200996 ret = 1;
997 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100998 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200999 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001000}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001001
1002/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1003 * with <len> as size in CRYPTO buffers dedicated to store the information about
1004 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001005 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
1006 * buffers to store all the data.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001007 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1008 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001009static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001010 const unsigned char *data, size_t len)
1011{
1012 struct quic_crypto_buf **qcb;
1013 /* The remaining byte to store in CRYPTO buffers. */
1014 size_t cf_offset, cf_len, *nb_buf;
1015 unsigned char *pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001016 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001017
1018 nb_buf = &qel->tx.crypto.nb_buf;
1019 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1020 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1021 cf_len = len;
1022
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001023 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1024
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001025 while (len) {
1026 size_t to_copy, room;
1027
1028 pos = (*qcb)->data + (*qcb)->sz;
1029 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1030 to_copy = len > room ? room : len;
1031 if (to_copy) {
1032 memcpy(pos, data, to_copy);
1033 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1034 qel->tx.crypto.sz += to_copy;
1035 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001036 len -= to_copy;
1037 data += to_copy;
1038 }
1039 else {
1040 struct quic_crypto_buf **tmp;
1041
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001042 // FIXME: realloc!
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001043 tmp = realloc(qel->tx.crypto.bufs,
1044 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1045 if (tmp) {
1046 qel->tx.crypto.bufs = tmp;
1047 qcb = &qel->tx.crypto.bufs[*nb_buf];
1048 *qcb = pool_alloc(pool_head_quic_crypto_buf);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001049 if (!*qcb) {
1050 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1051 goto leave;
1052 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001053
1054 (*qcb)->sz = 0;
1055 ++*nb_buf;
1056 }
1057 else {
1058 break;
1059 }
1060 }
1061 }
1062
1063 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1064 * have been buffered.
1065 */
1066 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001067 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001068 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001069
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001070 /* There is at most one CRYPTO frame in this packet number
1071 * space. Let's look for it.
1072 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001073 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001074 if (frm->type != QUIC_FT_CRYPTO)
1075 continue;
1076
1077 /* Found */
1078 found = frm;
1079 break;
1080 }
1081
1082 if (found) {
1083 found->crypto.len += cf_len;
1084 }
1085 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001086 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001087 if (!frm) {
1088 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1089 goto leave;
1090 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001091
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001092 LIST_INIT(&frm->reflist);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001093 frm->type = QUIC_FT_CRYPTO;
1094 frm->crypto.offset = cf_offset;
1095 frm->crypto.len = cf_len;
1096 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001097 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001098 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001100 ret = len == 0;
1101 leave:
1102 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1103 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001104}
1105
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001106/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1107 * activity for <qc> will be interrupted.
1108 */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001109void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001110{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001111 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001112 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001113 goto leave;
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001114
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001115 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001116 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001117 qc->err.code = err.code;
1118 qc->err.app = err.app;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001119 leave:
1120 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001121}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001122
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001123/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1124void quic_set_tls_alert(struct quic_conn *qc, int alert)
1125{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001126 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1127
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001128 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1129 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001130 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001131 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1132 }
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001133 quic_set_connection_close(qc, quic_err_tls(alert));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001134 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001135 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1136
1137 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001138}
1139
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001140/* Set the application for <qc> QUIC connection.
1141 * Return 1 if succeeded, 0 if not.
1142 */
1143int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1144{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001145 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1146 qc->app_ops = &h3_ops;
1147 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1148 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001149 else
1150 return 0;
1151
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001152 return 1;
1153}
1154
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001155/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1156 * wants to provide the QUIC layer with CRYPTO data.
1157 * Returns 1 if succeeded, 0 if not.
1158 */
1159int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1160 const uint8_t *data, size_t len)
1161{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001162 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001163 enum quic_tls_enc_level tel;
1164 struct quic_enc_level *qel;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001165 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001166
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001167 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1168 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001169
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001170 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001171 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001172 goto out;
1173 }
1174
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001175 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001176 if (tel == -1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001177 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1178 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001179 }
1180
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001181 qel = &qc->els[tel];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001182 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1183 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1184 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001185 }
1186
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001187 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001188 qc, &level, &len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001189 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001190 ret = 1;
1191 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001192 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001193 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001194}
1195
1196int ha_quic_flush_flight(SSL *ssl)
1197{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001198 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001199
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001200 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1201 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001202
1203 return 1;
1204}
1205
1206int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1207{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001208 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001209
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001210 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1211
1212 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1213
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001214 quic_set_tls_alert(qc, alert);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001215 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001216 return 1;
1217}
1218
1219/* QUIC TLS methods */
1220static SSL_QUIC_METHOD ha_quic_method = {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001221 .set_encryption_secrets = ha_quic_set_encryption_secrets,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001222 .add_handshake_data = ha_quic_add_handshake_data,
1223 .flush_flight = ha_quic_flush_flight,
1224 .send_alert = ha_quic_send_alert,
1225};
1226
1227/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1228 * Returns an error count.
1229 */
1230int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1231{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001232 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1233 int cfgerr = 0;
1234
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001235 long options =
1236 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1237 SSL_OP_SINGLE_ECDH_USE |
1238 SSL_OP_CIPHER_SERVER_PREFERENCE;
1239 SSL_CTX *ctx;
1240
1241 ctx = SSL_CTX_new(TLS_server_method());
1242 bind_conf->initial_ctx = ctx;
1243
1244 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001245 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1246 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1247 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001248
1249#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand373ce732022-09-07 11:11:59 +02001250# if defined(HAVE_SSL_CLIENT_HELLO_CB)
1251# if defined(SSL_OP_NO_ANTI_REPLAY)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001252 if (bind_conf->ssl_conf.early_data) {
1253 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001254 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001255 }
William Lallemand373ce732022-09-07 11:11:59 +02001256# endif /* !SSL_OP_NO_ANTI_REPLAY */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001257 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1258 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
William Lallemand373ce732022-09-07 11:11:59 +02001259# else /* ! HAVE_SSL_CLIENT_HELLO_CB */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001260 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
William Lallemand373ce732022-09-07 11:11:59 +02001261# endif
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001262 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1263#endif
1264 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1265
1266 return cfgerr;
1267}
1268
1269/* Decode an expected packet number from <truncated_on> its truncated value,
1270 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1271 * the number of bits used to encode this packet number (its length in bytes * 8).
1272 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1273 */
1274static uint64_t decode_packet_number(uint64_t largest_pn,
1275 uint32_t truncated_pn, unsigned int pn_nbits)
1276{
1277 uint64_t expected_pn = largest_pn + 1;
1278 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1279 uint64_t pn_hwin = pn_win / 2;
1280 uint64_t pn_mask = pn_win - 1;
1281 uint64_t candidate_pn;
1282
1283
1284 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1285 /* Note that <pn_win> > <pn_hwin>. */
1286 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1287 candidate_pn + pn_hwin <= expected_pn)
1288 return candidate_pn + pn_win;
1289
1290 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1291 return candidate_pn - pn_win;
1292
1293 return candidate_pn;
1294}
1295
1296/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1297 * cryptographic context.
1298 * <largest_pn> is the largest received packet number and <pn> the address of
1299 * the packet number field for this packet with <byte0> address of its first byte.
1300 * <end> points to one byte past the end of this packet.
1301 * Returns 1 if succeeded, 0 if not.
1302 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001303static int qc_do_rm_hp(struct quic_conn *qc,
1304 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001305 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001306{
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02001307 int ret, i, pnlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308 uint64_t packet_number;
1309 uint32_t truncated_pn = 0;
1310 unsigned char mask[5] = {0};
1311 unsigned char *sample;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001312 EVP_CIPHER_CTX *cctx = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001313
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001314 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1315
1316 ret = 0;
1317
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001318 /* Check there is enough data in this packet. */
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001319 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001320 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1321 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001322 }
1323
1324 cctx = EVP_CIPHER_CTX_new();
1325 if (!cctx) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001326 TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
1327 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001328 }
1329
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001330 sample = pn + QUIC_PACKET_PN_MAXLEN;
1331
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02001332 if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
1333 TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001334 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001335 }
1336
1337 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1338 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1339 for (i = 0; i < pnlen; i++) {
1340 pn[i] ^= mask[i + 1];
1341 truncated_pn = (truncated_pn << 8) | pn[i];
1342 }
1343
1344 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1345 /* Store remaining information for this unprotected header */
1346 pkt->pn = packet_number;
1347 pkt->pnl = pnlen;
1348
1349 ret = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001350 leave:
1351 if (cctx)
1352 EVP_CIPHER_CTX_free(cctx);
1353 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001354 return ret;
1355}
1356
1357/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1358 * address, with <payload_len> as payload length, <aad> as address of
1359 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1360 * context.
1361 * Returns 1 if succeeded, 0 if not.
1362 */
1363static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1364 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001365 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001366{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001367 int ret = 0;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001368 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001369 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001370 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001371 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001372
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001373 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
1374
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001375 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001376 TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001377 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001378 }
1379
1380 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001381 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001382 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001383 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001384 }
1385
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001386 ret = 1;
1387 leave:
1388 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
1389 return ret;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001390
1391 err:
1392 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001393 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001394}
1395
1396/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1397 * Returns 1 if succeeded, 0 if not.
1398 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001399static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel,
1400 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001401{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001402 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001403 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001404 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001405 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001406 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001407 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1408 unsigned char *rx_key = tls_ctx->rx.key;
1409
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001410 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1411
1412 ret = 0;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001413 kp_changed = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001414
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001415 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1416 /* The two tested bits are not at the same position,
1417 * this is why they are first both inversed.
1418 */
1419 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1420 if (pkt->pn < tls_ctx->rx.pn) {
1421 /* The lowest packet number of a previous key phase
1422 * cannot be null if it really stores previous key phase
1423 * secrets.
1424 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001425 // TODO: check if BUG_ON() more suitable
1426 if (!pkt->qc->ku.prv_rx.pn) {
1427 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1428 goto leave;
1429 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001430
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001431 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1432 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001433 rx_key = pkt->qc->ku.prv_rx.key;
1434 }
1435 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1436 /* Next key phase */
1437 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001438 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1439 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001440 rx_key = pkt->qc->ku.nxt_rx.key;
1441 }
1442 }
1443 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001444
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001445 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) {
1446 TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc);
1447 goto leave;
1448 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001449
1450 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1451 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001452 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001453 if (!ret) {
1454 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1455 goto leave;
1456 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001457
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001458 /* Update the keys only if the packet decryption succeeded. */
1459 if (kp_changed) {
1460 quic_tls_rotate_keys(pkt->qc);
1461 /* Toggle the Key Phase bit */
1462 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1463 /* Store the lowest packet number received for the current key phase */
1464 tls_ctx->rx.pn = pkt->pn;
1465 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001466 if (!quic_tls_key_update(pkt->qc)) {
1467 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1468 goto leave;
1469 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001470 }
1471
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001472 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001473 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001474 ret = 1;
1475 leave:
1476 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille11a6f402022-08-16 14:48:59 +02001477 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001478}
1479
Frédéric Lécaille96367152022-04-25 09:40:19 +02001480
1481/* Remove references to <frm> frame */
1482static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1483{
Frédéric Lécaille96367152022-04-25 09:40:19 +02001484 struct quic_frame *f, *tmp;
1485
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001486 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1487
Frédéric Lécaille96367152022-04-25 09:40:19 +02001488 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001489 f->origin = NULL;
1490 LIST_DELETE(&f->ref);
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001491 if (f->pkt) {
1492 TRACE_DEVEL("remove frame reference",
1493 QUIC_EV_CONN_PRSAFRM, qc, f, &f->pkt->pn_node.key);
1494 }
1495 else {
1496 TRACE_DEVEL("remove frame reference for unsent frame",
1497 QUIC_EV_CONN_PRSAFRM, qc, f);
1498 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001499 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001500
1501 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001502}
1503
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001504/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001505void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001506{
1507 uint64_t pn;
1508 struct quic_frame *origin, *f, *tmp;
1509
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001510 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1511
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001512 /* Identify this frame: a frame copy or one of its copies */
1513 origin = frm->origin ? frm->origin : frm;
1514 /* Ensure the source of the copies is flagged as acked, <frm> being
1515 * possibly a copy of <origin>
1516 */
1517 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1518 /* Mark all the copy of <origin> as acknowledged. We must
1519 * not release the packets (releasing the frames) at this time as
1520 * they are possibly also to be acknowledged alongside the
1521 * the current one.
1522 */
1523 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001524 if (f->pkt) {
Frédéric Lécaille025945f2022-09-06 09:55:21 +02001525 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1526 f->origin = NULL;
1527 LIST_DELETE(&f->ref);
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001528 pn = f->pkt->pn_node.key;
1529 TRACE_DEVEL("mark frame as acked from packet",
1530 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1531 }
1532 else {
Frédéric Lécaille025945f2022-09-06 09:55:21 +02001533 TRACE_DEVEL("freeing unsent frame",
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001534 QUIC_EV_CONN_PRSAFRM, qc, f);
Frédéric Lécaille025945f2022-09-06 09:55:21 +02001535 LIST_DELETE(&f->ref);
1536 LIST_DELETE(&f->list);
1537 pool_free(pool_head_quic_frame, f);
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001538 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001539 }
1540 LIST_DELETE(&frm->list);
1541 pn = frm->pkt->pn_node.key;
1542 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001543 TRACE_DEVEL("freeing frame from packet",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001544 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1545 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001546
1547 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001548}
1549
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001550/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1551 * and all STREAM data are acknowledged. The MUX is responsible to have set
1552 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1553 *
1554 * TODO this should also be called on lost packet detection
1555 */
1556static void qc_check_close_on_released_mux(struct quic_conn *qc)
1557{
1558 struct ssl_sock_ctx *ctx = qc->xprt_ctx;
1559
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001560 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1561
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001562 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1563 /* Reuse errcode which should have been previously set by the MUX on release. */
1564 quic_set_connection_close(qc, qc->err);
1565 tasklet_wakeup(ctx->wait_event.tasklet);
1566 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001567
1568 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001569}
1570
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001571/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001572 *
1573 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001574 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001575static int quic_stream_try_to_consume(struct quic_conn *qc,
1576 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001577{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001578 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001579 struct eb64_node *frm_node;
1580
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001581 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1582
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001583 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001584 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001585 while (frm_node) {
1586 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001587 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001588 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001589
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001590 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001591 offset = strm->offset.key;
1592 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001593
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001594 if (offset > stream->ack_offset)
1595 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001596
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001597 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001598 /* cf. next comment : frame may be freed at this stage. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001599 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001600 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001601 ret = 1;
1602 }
1603
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001604 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1605 * has been freed. with the stream frames tree. Nothing to do
1606 * anymore in here.
1607 */
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001608 if (!stream) {
1609 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001610 ret = 1;
1611 goto leave;
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001612 }
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001613
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001614 frm_node = eb64_next(frm_node);
1615 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001616
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001617 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001618 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001619 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001620
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001621 leave:
1622 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001623 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001624}
1625
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001626/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001627static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1628 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001629{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001630 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001631
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001632 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm);
1633
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001634 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001635 switch (frm->type) {
1636 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1637 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001638 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001639 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001640 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001641 const size_t offset = strm_frm->offset.key;
1642 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001643
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001644 /* do not use strm_frm->stream as the qc_stream_desc instance
1645 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001646 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001647 *
1648 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001649 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001650 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001651 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1652 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001653 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001654 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001655 /* early return */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001656 goto leave;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001657 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001658 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001659
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001660 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001661 if (offset <= stream->ack_offset) {
1662 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001663 stream_acked = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001664 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001665 qc, strm_frm, stream);
1666 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001667
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001668 if (!stream) {
1669 /* no need to continue if stream freed. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001670 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001671 qc_release_frm(qc, frm);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001672 qc_check_close_on_released_mux(qc);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001673 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001674 }
1675
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001676 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Frédéric Lécailleda342552022-04-25 10:28:49 +02001677 qc, strm_frm, stream);
1678 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001679 }
1680 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001681 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001682 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001683
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001684 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001685 }
1686 break;
1687 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001688 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001689 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001690
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001691 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001692 struct qcc *qcc = qc->qcc;
1693
1694 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1695 tasklet_wakeup(qcc->subs->tasklet);
1696 qcc->subs->events &= ~SUB_RETRY_SEND;
1697 if (!qcc->subs->events)
1698 qcc->subs = NULL;
1699 }
1700 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001701 leave:
1702 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001703}
1704
1705/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1706 * deallocating them, and their TX frames.
1707 * Returns the last node reached to be used for the next range.
1708 * May be NULL if <largest> node could not be found.
1709 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001710static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1711 struct eb_root *pkts,
1712 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001713 struct list *newly_acked_pkts,
1714 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001715 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001716{
1717 struct eb64_node *node;
1718 struct quic_tx_packet *pkt;
1719
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001720 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1721
Frédéric Lécaille843399f2022-07-22 16:27:44 +02001722 node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001723 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001724 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001725
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001726 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001727 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001728 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001729 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001730 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001731 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001732 node = eb64_prev(node);
1733 eb64_delete(&pkt->pn_node);
1734 }
1735
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001736 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001737 return node;
1738}
1739
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001740/* Remove all frames from <pkt_frm_list> and reinsert them in the
1741 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001742 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001743static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001744 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001745 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001746{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001747 struct quic_frame *frm, *frmbak;
1748 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001749 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001750 uint64_t pn = pkt->pn_node.key;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001751
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001752 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1753
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001754 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001755 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001756 LIST_DELETE(&frm->list);
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001757 quic_tx_packet_refdec(pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001758 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001759 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001760 /* Remove any reference to this frame */
1761 qc_frm_unref(qc, frm);
1762 switch (frm->type) {
1763 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1764 {
1765 struct quic_stream *strm_frm = &frm->stream;
1766 struct eb64_node *node = NULL;
1767 struct qc_stream_desc *stream_desc;
1768
1769 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1770 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001771 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1772 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001773 qc, frm, &pn);
1774 pool_free(pool_head_quic_frame, frm);
1775 continue;
1776 }
1777
1778 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1779 /* Do not resend this frame if in the "already acked range" */
1780 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001781 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001782 QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillebccbad22022-08-31 15:02:53 +02001783 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001784 continue;
1785 }
1786 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1787 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001788 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001789 QUIC_EV_CONN_PRSAFRM, qc, frm);
1790 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001791 break;
1792 }
1793
1794 default:
1795 break;
1796 }
1797
1798 /* Do not resend probing packet with old data */
1799 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001800 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1801 qc, frm, &pn);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001802 if (frm->origin)
1803 LIST_DELETE(&frm->ref);
1804 pool_free(pool_head_quic_frame, frm);
1805 continue;
1806 }
1807
1808 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001809 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1810 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001811 qc, frm, &pn);
1812 pool_free(pool_head_quic_frame, frm);
1813 }
1814 else {
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001815 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1816 /* Mark this STREAM frame as lost. A look up their stream descriptor
1817 * will be performed to check the stream is not consumed or released.
1818 */
Amaury Denoyellef372e742022-08-16 14:41:57 +02001819 frm->flags |= QUIC_FL_TX_FRAME_LOST;
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001820 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001821 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001822 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001823 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001824 }
1825
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001826 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001827
1828 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001829}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001830
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001831/* Free <pkt> TX packet and its attached frames.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001832 * This is the responsibility of the caller to remove this packet of
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001833 * any data structure it was possibly attached to.
1834 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001835static inline void free_quic_tx_packet(struct quic_conn *qc,
1836 struct quic_tx_packet *pkt)
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001837{
1838 struct quic_frame *frm, *frmbak;
1839
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001840 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1841
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001842 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001843 goto leave;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001844
1845 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1846 LIST_DELETE(&frm->list);
1847 pool_free(pool_head_quic_frame, frm);
1848 }
1849 pool_free(pool_head_quic_tx_packet, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001850
1851 leave:
1852 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001853}
1854
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001855/* Free the TX packets of <pkts> list */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001856static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001857{
1858 struct quic_tx_packet *pkt, *tmp;
1859
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001860 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1861
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001862 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001863 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001864 eb64_delete(&pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001865 free_quic_tx_packet(qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001866 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001867
1868 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001869}
1870
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001871/* Remove already sent ranges of acknowledged packet numbers from
1872 * <pktns> packet number space tree below <largest_acked_pn> possibly
1873 * updating the range which contains <largest_acked_pn>.
1874 * Never fails.
1875 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001876static void qc_treat_ack_of_ack(struct quic_conn *qc,
1877 struct quic_pktns *pktns,
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001878 int64_t largest_acked_pn)
1879{
1880 struct eb64_node *ar, *next_ar;
1881 struct quic_arngs *arngs = &pktns->rx.arngs;
1882
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001883 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1884
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001885 ar = eb64_first(&arngs->root);
1886 while (ar) {
1887 struct quic_arng_node *ar_node;
1888
1889 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001890 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001891
1892 if ((int64_t)ar_node->first.key > largest_acked_pn) {
1893 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001894 break;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001895 }
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001896
1897 if (largest_acked_pn < ar_node->last) {
1898 eb64_delete(ar);
1899 ar_node->first.key = largest_acked_pn + 1;
1900 eb64_insert(&arngs->root, ar);
1901 break;
1902 }
1903
1904 eb64_delete(ar);
1905 pool_free(pool_head_quic_arng, ar_node);
1906 arngs->sz--;
1907 ar = next_ar;
1908 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001909
1910 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001911}
1912
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001913/* Send a packet ack event nofication for each newly acked packet of
1914 * <newly_acked_pkts> list and free them.
1915 * Always succeeds.
1916 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001917static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001918 struct list *newly_acked_pkts)
1919{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001920 struct quic_tx_packet *pkt, *tmp;
1921 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1922
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001923 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1924
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001925 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1926 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001927 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001928 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001929 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001930 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001931 /* If this packet contained an ACK frame, proceed to the
1932 * acknowledging of range of acks from the largest acknowledged
1933 * packet number which was sent in an ACK frame by this packet.
1934 */
1935 if (pkt->largest_acked_pn != -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001936 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001937 ev.ack.acked = pkt->in_flight_len;
1938 ev.ack.time_sent = pkt->time_sent;
1939 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001940 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001941 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001942 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001943 }
1944
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001945 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1946
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001947}
1948
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001949/* Release all the frames attached to <pktns> packet number space */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001950static inline void qc_release_pktns_frms(struct quic_conn *qc,
1951 struct quic_pktns *pktns)
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001952{
1953 struct quic_frame *frm, *frmbak;
1954
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001955 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
1956
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001957 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1958 LIST_DELETE(&frm->list);
1959 pool_free(pool_head_quic_frame, frm);
1960 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001961
1962 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001963}
1964
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001965/* Handle <pkts> list of lost packets detected at <now_us> handling
1966 * their TX frames.
1967 * Send a packet loss event to the congestion controller if
1968 * in flight packet have been lost.
1969 * Also frees the packet in <pkts> list.
1970 * Never fails.
1971 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001972static inline void qc_release_lost_pkts(struct quic_conn *qc,
1973 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001974 struct list *pkts,
1975 uint64_t now_us)
1976{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001977 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001978
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001979 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1980
Frédéric Lécaille277c4622022-08-24 17:06:04 +02001981 if (LIST_ISEMPTY(pkts))
1982 goto leave;
1983
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001984 oldest_lost = newest_lost = NULL;
1985 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001986 struct list tmp = LIST_HEAD_INIT(tmp);
1987
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001988 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001989 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001990 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001991 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001992 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001993 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001994 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001995 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001996 if (!oldest_lost) {
1997 oldest_lost = newest_lost = pkt;
1998 }
1999 else {
2000 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002001 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002002 newest_lost = pkt;
2003 }
2004 }
2005
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01002006 if (newest_lost) {
2007 /* Sent a congestion event to the controller */
Benoit DOLEZ69e3f052022-06-08 09:28:56 +02002008 struct quic_cc_event ev = { };
2009
2010 ev.type = QUIC_CC_EVT_LOSS;
2011 ev.loss.time_sent = newest_lost->time_sent;
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01002012
2013 quic_cc_event(&qc->path->cc, &ev);
2014 }
2015
2016 /* If an RTT have been already sampled, <rtt_min> has been set.
2017 * We must check if we are experiencing a persistent congestion.
2018 * If this is the case, the congestion controller must re-enter
2019 * slow start state.
2020 */
2021 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2022 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
2023
2024 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2025 now_ms, qc->max_ack_delay))
2026 qc->path->cc.algo->slow_start(&qc->path->cc);
2027 }
2028
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002029 quic_tx_packet_refdec(oldest_lost);
2030 if (newest_lost != oldest_lost)
2031 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002032
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002033 leave:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002034 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002035}
2036
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002037/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2038 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002039 * 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 +01002040 * acked ack-eliciting packet.
2041 * Return 1, if succeeded, 0 if not.
2042 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002043static inline int qc_parse_ack_frm(struct quic_conn *qc,
2044 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002045 struct quic_enc_level *qel,
2046 unsigned int *rtt_sample,
2047 const unsigned char **pos, const unsigned char *end)
2048{
2049 struct quic_ack *ack = &frm->ack;
2050 uint64_t smallest, largest;
2051 struct eb_root *pkts;
2052 struct eb64_node *largest_node;
2053 unsigned int time_sent, pkt_flags;
2054 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2055 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002056 int ret = 0;
2057
2058 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002059
2060 if (ack->largest_ack > qel->pktns->tx.next_pn) {
2061 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002062 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002063 goto err;
2064 }
2065
2066 if (ack->first_ack_range > ack->largest_ack) {
2067 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002068 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002069 goto err;
2070 }
2071
2072 largest = ack->largest_ack;
2073 smallest = largest - ack->first_ack_range;
2074 pkts = &qel->pktns->tx.pkts;
2075 pkt_flags = 0;
2076 largest_node = NULL;
2077 time_sent = 0;
2078
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002079 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002080 largest_node = eb64_lookup(pkts, largest);
2081 if (!largest_node) {
2082 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002083 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002084 }
2085 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002086 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002087 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002088 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002089 }
2090
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002091 TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002092 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002093 do {
2094 uint64_t gap, ack_range;
2095
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002096 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2097 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002098 if (!ack->ack_range_num--)
2099 break;
2100
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002101 if (!quic_dec_int(&gap, pos, end)) {
2102 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002103 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002104 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002105
2106 if (smallest < gap + 2) {
2107 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002108 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002109 goto err;
2110 }
2111
2112 largest = smallest - gap - 2;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002113 if (!quic_dec_int(&ack_range, pos, end)) {
2114 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002115 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002116 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002117
2118 if (largest < ack_range) {
2119 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002120 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002121 goto err;
2122 }
2123
2124 /* Do not use this node anymore. */
2125 largest_node = NULL;
2126 /* Next range */
2127 smallest = largest - ack_range;
2128
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002129 TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002130 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002131 } while (1);
2132
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002133 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2134 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002135 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002136 }
2137
2138 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2139 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002140 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002141 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002142 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002143 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2144 if (quic_peer_validated_addr(qc))
2145 qc->path->loss.pto_count = 0;
2146 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002147 }
2148
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002149 ret = 1;
2150 leave:
2151 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2152 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002153
2154 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002155 free_quic_tx_pkts(qc, &newly_acked_pkts);
2156 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002157}
2158
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002159/* This function gives the detail of the SSL error. It is used only
2160 * if the debug mode and the verbose mode are activated. It dump all
2161 * the SSL error until the stack was empty.
2162 */
2163static forceinline void qc_ssl_dump_errors(struct connection *conn)
2164{
2165 if (unlikely(global.mode & MODE_DEBUG)) {
2166 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002167 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002168 unsigned long ret;
2169
Willy Tarreau325fc632022-04-11 18:47:38 +02002170 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002171 ret = ERR_get_error();
2172 if (!ret)
2173 return;
2174
2175 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002176 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002177 }
2178 }
2179}
2180
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002181int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2182 const char **str, int *len);
2183
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002184/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2185 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002186 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002187 * Return 1 if succeeded, 0 if not.
2188 */
2189static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002190 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002191 const unsigned char *data, size_t len,
2192 struct quic_rx_packet *pkt,
2193 struct quic_rx_crypto_frm *cf)
2194{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002195 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002196 struct quic_conn *qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002197 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002198
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002199 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002200 qc = ctx->qc;
2201
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002202 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2203
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002204 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002205 TRACE_ERROR("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002206 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002207 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002208 }
2209
2210 el->rx.crypto.offset += len;
2211 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002212 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002213
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002214 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002215 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002216 ssl_err = SSL_do_handshake(ctx->ssl);
2217 if (ssl_err != 1) {
2218 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2219 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002220 TRACE_PROTO("SSL handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002221 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002222 goto out;
2223 }
2224
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002225 /* TODO: Should close the connection asap */
2226 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2227 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2228 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2229 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2230 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002231 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002232 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002233 ERR_clear_error();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002234 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002235 }
2236
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002237 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002238
2239 /* Check the alpn could be negotiated */
2240 if (!qc->app_ops) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002241 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002242 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002243 goto leave;
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002244 }
2245
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002246 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002247 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002248 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2249 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2250 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002251 /* I/O callback switch */
2252 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002253 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002254 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002255 /* The connection is ready to be accepted. */
2256 quic_accept_push_qc(qc);
2257 }
2258 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002259 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002260 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002261 } else {
2262 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2263 if (ssl_err != 1) {
2264 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2265 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002266 TRACE_PROTO("SSL post handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002267 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002268 goto out;
2269 }
2270
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002271 TRACE_ERROR("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002272 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002273 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002274 }
2275
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002276 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002277 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002278
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002279 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002280 ret = 1;
2281 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002282 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002283 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002284}
2285
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002286/* Parse a STREAM frame <strm_frm>
2287 *
2288 * Return 1 on success. On error, 0 is returned. In this case, the packet
2289 * containing the frame must not be acknowledged.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002290 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002291static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2292 struct quic_stream *strm_frm,
2293 struct quic_conn *qc)
2294{
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002295 int ret;
2296
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002297 /* RFC9000 13.1. Packet Processing
2298 *
2299 * A packet MUST NOT be acknowledged until packet protection has been
2300 * successfully removed and all frames contained in the packet have
2301 * been processed. For STREAM frames, this means the data has been
2302 * enqueued in preparation to be received by the application protocol,
2303 * but it does not require that data be delivered and consumed.
2304 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002305 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2306
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002307 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2308 strm_frm->offset.key, strm_frm->fin,
2309 (char *)strm_frm->data);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002310
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002311 /* frame rejected - packet must not be acknowledeged */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002312 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2313 return !ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002314}
2315
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002316/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2317 * for <qc> QUIC connection.
2318 * This is a best effort function which never fails even if no memory could be
2319 * allocated to duplicate these frames.
2320 */
2321static void qc_dup_pkt_frms(struct quic_conn *qc,
2322 struct list *pkt_frm_list, struct list *out_frm_list)
2323{
2324 struct quic_frame *frm, *frmbak;
2325 struct list tmp = LIST_HEAD_INIT(tmp);
2326
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002327 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2328
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002329 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2330 struct quic_frame *dup_frm, *origin;
2331
2332 switch (frm->type) {
2333 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2334 {
2335 struct quic_stream *strm_frm = &frm->stream;
2336 struct eb64_node *node = NULL;
2337 struct qc_stream_desc *stream_desc;
2338
2339 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2340 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002341 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002342 continue;
2343 }
2344
2345 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2346 /* Do not resend this frame if in the "already acked range" */
2347 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002348 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002349 QUIC_EV_CONN_PRSAFRM, qc, frm);
2350 continue;
2351 }
2352 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2353 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002354 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002355 QUIC_EV_CONN_PRSAFRM, qc, frm);
2356 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002357 break;
2358 }
2359
2360 default:
2361 break;
2362 }
2363
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002364 dup_frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002365 if (!dup_frm) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002366 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002367 break;
2368 }
2369
2370 /* If <frm> is already a copy of another frame, we must take
2371 * its original frame as source for the copy.
2372 */
2373 origin = frm->origin ? frm->origin : frm;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002374 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Frédéric Lécaillee35463c2022-08-27 10:19:42 +02002375 if (origin->pkt)
2376 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
2377 qc, NULL, &origin->pkt->pn_node.key);
2378 else {
2379 /* <origin> is a frame which was sent from a packet detected as lost. */
2380 TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
2381 }
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002382 *dup_frm = *origin;
2383 dup_frm->pkt = NULL;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002384 dup_frm->origin = origin;
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002385 dup_frm->flags = 0;
2386 LIST_INIT(&dup_frm->reflist);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002387 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2388 LIST_APPEND(&tmp, &dup_frm->list);
2389 }
2390
2391 LIST_SPLICE(out_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002392
2393 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002394}
2395
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002396/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002397static void qc_prep_fast_retrans(struct quic_conn *qc,
2398 struct quic_enc_level *qel,
2399 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002400{
2401 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002402 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002403 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002404 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002405
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002406 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2407
2408 BUG_ON(frms1 == frms2);
2409
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002410 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002411 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002412 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002413 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002414 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2415 node = eb64_next(node);
2416 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002417 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002418 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002419 }
2420
2421 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002422 goto leave;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002423
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002424 /* When building a packet from another one, the field which may increase the
2425 * packet size is the packet number. And the maximum increase is 4 bytes.
2426 */
2427 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2428 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +02002429 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002430 goto leave;
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002431 }
2432
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +02002433 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002434 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2435 if (frms == frms1 && frms2) {
2436 frms = frms2;
2437 goto start;
2438 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002439 leave:
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +02002440 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002441}
2442
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002443/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002444 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002445 * Initial packets send them coalescing with others (Handshake here).
2446 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002447 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002448static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2449 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002450{
2451 struct list itmp = LIST_HEAD_INIT(itmp);
2452 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002453
2454 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2455 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2456 struct quic_enc_level *qel = iqel;
2457 struct eb_root *pkts;
2458 struct eb64_node *node;
2459 struct quic_tx_packet *pkt;
2460 struct list *tmp = &itmp;
2461
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002462 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002463 start:
2464 pkt = NULL;
2465 pkts = &qel->pktns->tx.pkts;
2466 node = eb64_first(pkts);
2467 /* Skip the empty packet (they have already been retransmitted) */
2468 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002469 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002470 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002471 break;
2472 node = eb64_next(node);
2473 }
2474
2475 if (!pkt)
2476 goto end;
2477
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002478 /* When building a packet from another one, the field which may increase the
2479 * packet size is the packet number. And the maximum increase is 4 bytes.
2480 */
2481 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2482 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2483 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2484 goto end;
2485 }
2486
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002487 qel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002488
2489 /* No risk to loop here, #packet per datagram is bounded */
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002490 requeue:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002491 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002492 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002493 if (qel == iqel) {
2494 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2495 pkt = pkt->next;
2496 tmp = &htmp;
2497 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002498 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002499 goto requeue;
2500 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002501 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002502
2503 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002504 LIST_SPLICE(ifrms, &itmp);
2505 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002506
2507 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002508}
2509
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002510static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002511{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002512 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2513
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002514 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2515 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2516 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2517 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002518 goto out;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002519
2520 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2521 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002522
2523 out:
2524 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002525}
2526
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002527/* Enqueue a STOP_SENDING frame to send into 1RTT packet number space
2528 * frame list to send.
2529 * Return 1 if succeeded, 0 if not.
2530 */
2531static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id)
2532{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002533 int ret = 0;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002534 struct quic_frame *frm;
2535 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2536 uint64_t app_error_code;
2537
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002538 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2539
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002540 /* TODO: the mux may be released, we cannot have more
2541 * information about the application error code to send
2542 * at this time.
2543 */
2544 app_error_code = H3_REQUEST_REJECTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002545 // fixme: zalloc
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002546 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002547 if (!frm) {
2548 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2549 goto out;
2550 }
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002551
2552 frm->type = QUIC_FT_STOP_SENDING;
2553 frm->stop_sending.id = id;
2554 frm->stop_sending.app_error_code = app_error_code;
2555 LIST_INIT(&frm->reflist);
2556 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002557 ret = 1;
2558 out:
2559 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2560 return ret;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002561}
2562
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002563/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2564 * as I/O handler context and <qel> as encryption level.
2565 * Returns 1 if succeeded, 0 if failed.
2566 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002567static 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 +01002568 struct quic_enc_level *qel)
2569{
2570 struct quic_frame frm;
2571 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002572 struct quic_conn *qc = ctx->qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002573 int fast_retrans = 0, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002574
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002575 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002576 /* Skip the AAD */
2577 pos = pkt->data + pkt->aad_len;
2578 end = pkt->data + pkt->len;
2579
2580 while (pos < end) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002581 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
2582 // trace already emitted by function above
2583 goto leave;
2584 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002585
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002586 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002587 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002588 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002589 break;
2590 case QUIC_FT_PING:
2591 break;
2592 case QUIC_FT_ACK:
2593 {
2594 unsigned int rtt_sample;
2595
2596 rtt_sample = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002597 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
2598 // trace already emitted by function above
2599 goto leave;
2600 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002601
2602 if (rtt_sample) {
2603 unsigned int ack_delay;
2604
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002605 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002606 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002607 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2608 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002609 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002610 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002611 break;
2612 }
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002613 case QUIC_FT_RESET_STREAM:
2614 /* TODO: handle this frame at STREAM level */
2615 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002616 case QUIC_FT_STOP_SENDING:
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002617 {
2618 struct quic_stop_sending *stop_sending = &frm.stop_sending;
2619 if (qc->mux_state == QC_MUX_READY) {
2620 if (qcc_recv_stop_sending(qc->qcc, stop_sending->id,
2621 stop_sending->app_error_code)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002622 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
2623 goto leave;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002624 }
2625 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002626 break;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002627 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002628 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002629 {
2630 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002632 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002633 /* XXX TO DO: <cfdebug> is used only for the traces. */
2634 struct quic_rx_crypto_frm cfdebug = { };
2635
2636 cfdebug.offset_node.key = frm.crypto.offset;
2637 cfdebug.len = frm.crypto.len;
2638 TRACE_PROTO("CRYPTO data discarded",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002639 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002640 break;
2641 }
2642
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002643 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2644 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2645 /* XXX TO DO: <cfdebug> is used only for the traces. */
2646 struct quic_rx_crypto_frm cfdebug = { };
2647
2648 cfdebug.offset_node.key = frm.crypto.offset;
2649 cfdebug.len = frm.crypto.len;
2650 /* Nothing to do */
2651 TRACE_PROTO("Already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002652 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002653 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002654 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2655 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002656 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002657 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002658 else {
2659 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2660 /* XXX TO DO: <cfdebug> is used only for the traces. */
2661 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002662
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002663 cfdebug.offset_node.key = frm.crypto.offset;
2664 cfdebug.len = frm.crypto.len;
2665 TRACE_PROTO("Partially already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002666 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002667 frm.crypto.len -= diff;
2668 frm.crypto.data += diff;
2669 frm.crypto.offset = qel->rx.crypto.offset;
2670 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002671 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002672
2673 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002674 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002675 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002676
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002677 cfdebug.offset_node.key = frm.crypto.offset;
2678 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002679 if (!qc_provide_cdata(qel, ctx,
2680 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002681 pkt, &cfdebug)) {
2682 // trace already emitted by function above
2683 goto leave;
2684 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002685
2686 break;
2687 }
2688
2689 /* frm.crypto.offset > qel->rx.crypto.offset */
2690 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2691 if (!cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002692 TRACE_ERROR("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002693 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002694 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002695 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002696
2697 cf->offset_node.key = frm.crypto.offset;
2698 cf->len = frm.crypto.len;
2699 cf->data = frm.crypto.data;
2700 cf->pkt = pkt;
2701 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2702 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002703 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002704 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002705 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002706 {
2707 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002708 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002709
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002710 /* The upper layer may not be allocated. */
2711 if (qc->mux_state != QC_MUX_READY) {
2712 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002713 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002714 break;
2715 }
2716 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002717 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002718 if (!qc_stop_sending_frm_enqueue(qc, stream->id))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002719 TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc);
2720 /* This packet will not be acknowledged */
2721 goto leave;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002722 }
2723 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002724
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002725 if (!qc_handle_strm_frm(pkt, stream, qc)) {
2726 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
2727 goto leave;
2728 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002729
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002730 break;
2731 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002732 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002733 if (qc->mux_state == QC_MUX_READY) {
2734 struct quic_max_data *data = &frm.max_data;
2735 qcc_recv_max_data(qc->qcc, data->max_data);
2736 }
2737 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002738 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002739 if (qc->mux_state == QC_MUX_READY) {
2740 struct quic_max_stream_data *data = &frm.max_stream_data;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002741 if (qcc_recv_max_stream_data(qc->qcc, data->id,
2742 data->max_stream_data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002743 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
2744 goto leave;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002745 }
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002746 }
2747 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002748 case QUIC_FT_MAX_STREAMS_BIDI:
2749 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002750 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002751 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002752 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2753 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002754 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002755 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2756 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002757 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002758 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2759 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002760 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002761 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002762 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002763 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002764 case QUIC_FT_RETIRE_CONNECTION_ID:
2765 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002766 break;
2767 case QUIC_FT_CONNECTION_CLOSE:
2768 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002769 /* Increment the error counters */
2770 qc_cc_err_count_inc(qc, &frm);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002771 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002772 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2773 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002774 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002775 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002776 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002777 /* RFC 9000 10.2. Immediate Close:
2778 * The closing and draining connection states exist to ensure
2779 * that connections close cleanly and that delayed or reordered
2780 * packets are properly discarded. These states SHOULD persist
2781 * for at least three times the current PTO interval...
2782 *
2783 * Rearm the idle timeout only one time when entering draining
2784 * state.
2785 */
2786 qc_idle_timer_do_rearm(qc);
2787 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002788 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002789 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002790 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002791 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002792 if (qc_is_listener(ctx->qc)) {
2793 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
2794 QUIC_EV_CONN_PRSHPKT, qc);
2795 goto leave;
2796 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002797
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002798 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002799 break;
2800 default:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002801 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
2802 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002803 }
2804 }
2805
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002806 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002807 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002808
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002809 if (fast_retrans) {
2810 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2811 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2812
2813 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2814 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002815
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002816 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2817 * has successfully parse a Handshake packet. The Initial encryption must also
2818 * be discarded.
2819 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002820 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002821 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002822 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2823 QUIC_FL_TLS_SECRETS_DCD)) {
2824 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2825 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2826 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2827 qc_set_timer(ctx->qc);
2828 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002829 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002830 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002831 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2832 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002833 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002834 }
2835
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002836 ret = 1;
2837 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002838 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002839 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002840}
2841
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002842/* Allocate Tx buffer from <qc> quic-conn if needed.
2843 *
2844 * Returns allocated buffer or NULL on error.
2845 */
2846static struct buffer *qc_txb_alloc(struct quic_conn *qc)
2847{
2848 struct buffer *buf = &qc->tx.buf;
2849 if (!b_alloc(buf))
2850 return NULL;
2851
2852 return buf;
2853}
2854
2855/* Free Tx buffer from <qc> if it is empty. */
2856static void qc_txb_release(struct quic_conn *qc)
2857{
2858 struct buffer *buf = &qc->tx.buf;
2859
2860 /* For the moment sending function is responsible to purge the buffer
2861 * entirely. It may change in the future but this requires to be able
2862 * to reuse old data.
2863 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002864 BUG_ON_HOT(buf && b_data(buf));
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002865
2866 if (!b_data(buf)) {
2867 b_free(buf);
2868 offer_buffers(NULL, 1);
2869 }
2870}
2871
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002872/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
2873 * must contains the address of the first packet stored in the payload.
2874 *
2875 * Caller is responsible that there is enough space in the buffer.
2876 */
2877static void qc_txb_store(struct buffer *buf, uint16_t length,
2878 struct quic_tx_packet *first_pkt)
2879{
2880 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
2881 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
2882
2883 write_u16(b_tail(buf), length);
2884 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
2885 b_add(buf, hdlen + length);
2886}
2887
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002888/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2889 * with <frms> as ack-eliciting frame list to send, 0 if not.
2890 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2891 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002892 * <force_ack> may be set to 1 if you want to force an ack.
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002893 */
2894static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002895 struct quic_enc_level *qel, int cc, int probe, int force_ack)
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002896{
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002897 unsigned int must_ack = force_ack ||
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002898 (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED));
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002899
2900 /* Do not build any more packet if the TX secrets are not available or
2901 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2902 * and if there is no more packets to send upon PTO expiration
2903 * and if there is no more ack-eliciting frames to send or in flight
2904 * congestion control limit is reached for prepared data
2905 */
2906 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2907 (!cc && !probe && !must_ack &&
2908 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002909 return 0;
2910 }
2911
2912 return 1;
2913}
2914
Amaury Denoyellef2476052022-08-04 16:19:57 +02002915/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2916 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
2917 *
2918 * Each datagram is prepended by a two fields header : the datagram length and
2919 * the address of the packet contained in the datagram.
2920 *
2921 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2922 * -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002923 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002924static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002925 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002926{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002927 int ret = -1;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002928 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002929 unsigned char *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002930 struct quic_tx_packet *pkt;
2931 size_t total;
Amaury Denoyellef2476052022-08-04 16:19:57 +02002932 /* Each datagram is prepended with its length followed by the address
2933 * of the first packet in the datagram.
2934 */
2935 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002936
2937 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002938
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002939 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2940 total = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002941 pos = (unsigned char *)b_tail(buf);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002942 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002943 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002944
2945 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002946 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002947 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002948 /* We do not probe if an immediate close was asked */
2949 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002950 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002951
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002952 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002953 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002954
2955 /* Leave room for the datagram header */
2956 pos += dg_headlen;
2957 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002958 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002959 }
2960 else {
2961 end = pos + qc->path->mtu;
2962 }
2963
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002964 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
2965 QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002966 switch (err) {
2967 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002968 // trace already emitted by function above
2969 goto leave;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002970 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002971 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2972 * MTU, we are here because of the congestion control window. There is
2973 * no need to try to reuse this buffer.
2974 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002975 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002976 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002977 default:
2978 break;
2979 }
2980
2981 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002982 BUG_ON(!pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002983
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002984 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2985 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2986
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002987 total += pkt->len;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002988
Amaury Denoyellef2476052022-08-04 16:19:57 +02002989 /* Write datagram header. */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002990 qc_txb_store(buf, pkt->len, pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002991 }
2992
2993 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002994 ret = total;
2995 leave:
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002996 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002997 return ret;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002998}
2999
Amaury Denoyellef2476052022-08-04 16:19:57 +02003000/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3001 * <frms>. Several packets can be regrouped in a single datagram. The result is
3002 * written into <buf>.
3003 *
3004 * Each datagram is prepended by a two fields header : the datagram length and
3005 * the address of first packet in the datagram.
3006 *
3007 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3008 * -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003009 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003010static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003011 enum quic_tls_enc_level tel, struct list *tel_frms,
3012 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003013{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003014 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003015 unsigned char *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003016 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3017 /* length of datagrams */
3018 uint16_t dglen;
3019 size_t total;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003020 int ret = -1, padding;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003021 /* Each datagram is prepended with its length followed by the address
3022 * of the first packet in the datagram.
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003023 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003024 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003025 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003027 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3028
Amaury Denoyellef2476052022-08-04 16:19:57 +02003029 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
3030 * caller must ensure that buf is resetted.
3031 */
3032 BUG_ON_HOT(buf->head || buf->data);
3033
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003034 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003035 qel = &qc->els[tel];
3036 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003037 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003038 padding = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003039 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003040 first_pkt = prv_pkt = NULL;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003041 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003042 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003043 enum quic_pkt_type pkt_type;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003044 struct quic_tls_ctx *tls_ctx;
3045 const struct quic_version *ver;
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003046 int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3047 (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3048 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003049
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003050 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003051 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003052 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003053 /* We do not probe if an immediate close was asked */
3054 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01003055 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003056
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003057 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003058 if (prv_pkt)
3059 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003060 /* Let's select the next encryption level */
3061 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3062 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003063 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003064 qel = &qc->els[tel];
3065 /* Build a new datagram */
3066 prv_pkt = NULL;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003067 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003068 continue;
3069 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003070 break;
3071 }
3072
3073 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003074 if (!prv_pkt) {
3075 /* Leave room for the datagram header */
3076 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003077 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02003078 end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01003079 }
3080 else {
3081 end = pos + qc->path->mtu;
3082 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003083 }
3084
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003085 if (qc->negotiated_version) {
3086 ver = qc->negotiated_version;
3087 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3088 tls_ctx = &qc->negotiated_ictx;
3089 else
3090 tls_ctx = &qel->tls_ctx;
3091 }
3092 else {
3093 ver = qc->original_version;
3094 tls_ctx = &qel->tls_ctx;
3095 }
3096
3097 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02003098 qc, ver, dglen, pkt_type,
3099 force_ack, padding, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003100 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003101 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003102 // trace already emitted by function above
3103 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003104 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003105 /* If there was already a correct packet present, set the
3106 * current datagram as prepared into <cbuf>.
3107 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003108 if (prv_pkt)
3109 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003110 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003111 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003112 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01003113 break;
3114 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02003115
Frédéric Lécaille63556772021-12-29 17:18:21 +01003116 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003117 BUG_ON(!cur_pkt);
Frédéric Lécaille63556772021-12-29 17:18:21 +01003118
Frédéric Lécaille1809c332022-04-25 10:24:12 +02003119 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3120 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3121
Frédéric Lécaille63556772021-12-29 17:18:21 +01003122 total += cur_pkt->len;
3123 /* keep trace of the first packet in the datagram */
3124 if (!first_pkt)
3125 first_pkt = cur_pkt;
3126 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003127 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003128 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003129 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3130 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01003131 /* Let's say we have to build a new dgram */
3132 prv_pkt = NULL;
3133 dglen += cur_pkt->len;
3134 /* Client: discard the Initial encryption keys as soon as
3135 * a handshake packet could be built.
3136 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003137 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01003138 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3139 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3140 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3141 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3142 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01003143 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003144 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003145 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003146 }
3147 /* If the data for the current encryption level have all been sent,
3148 * select the next level.
3149 */
3150 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003151 next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003152 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3153 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3154 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3155 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003156 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3157 frms = &qc->els[tel].pktns->tx.frms;
3158 else
3159 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003160 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003161 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003162 /* If there is data for the next level, do not
3163 * consume a datagram.
3164 */
3165 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003166 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003167 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02003168
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003169 /* If we have to build a new datagram, set the current datagram as
3170 * prepared into <cbuf>.
3171 */
3172 if (!prv_pkt) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003173 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003174 first_pkt = NULL;
3175 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003176 padding = 0;
3177 }
3178 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003179 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003180 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3181 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003182 }
3183 }
3184
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003185 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003186 ret = total;
3187 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003188 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003189 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003190}
3191
Amaury Denoyellef2476052022-08-04 16:19:57 +02003192/* Send datagrams stored in <buf>.
3193 *
3194 * This function always returns 1 for success. Even if sendto() syscall failed,
3195 * buffer is drained and packets are considered as emitted. QUIC loss detection
3196 * mechanism is used as a back door way to retry sending.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003197 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003198int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003199{
3200 struct quic_conn *qc;
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003201 char skip_sendto = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003202
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003203 qc = ctx->qc;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003204 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003205 while (b_contig_data(buf, 0)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003206 unsigned char *pos;
3207 struct buffer tmpbuf = { };
3208 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3209 uint16_t dglen;
3210 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003211 unsigned int time_sent;
3212
Amaury Denoyellef2476052022-08-04 16:19:57 +02003213 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003214 dglen = read_u16(pos);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003215 BUG_ON_HOT(!dglen); /* this should not happen */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003216
3217 pos += sizeof dglen;
3218 first_pkt = read_ptr(pos);
3219 pos += sizeof first_pkt;
3220 tmpbuf.area = (char *)pos;
3221 tmpbuf.size = tmpbuf.data = dglen;
3222
Frédéric Lécaille3a561372022-08-29 18:05:44 +02003223 TRACE_DATA("send dgram", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003224 /* If sendto is on error just skip the call to it for the rest
3225 * of the loop but continue to purge the buffer. Data will be
3226 * transmitted when QUIC packets are detected as lost on our
3227 * side.
3228 *
3229 * TODO use fd-monitoring to detect when send operation can be
3230 * retry. This should improve the bandwidth without relying on
3231 * retransmission timer. However, it requires a major rework on
3232 * quic-conn fd management.
3233 */
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003234 if (!skip_sendto) {
3235 if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) {
3236 skip_sendto = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003237 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003238 }
3239 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003240
Amaury Denoyellef2476052022-08-04 16:19:57 +02003241 b_del(buf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003242 qc->tx.bytes += tmpbuf.data;
3243 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003244
3245 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3246 pkt->time_sent = time_sent;
3247 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3248 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003249 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003250 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3251 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003252 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003253 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3254 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3255 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003256 qc_notify_close(qc);
3257
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003258 /* RFC 9000 10.2. Immediate Close:
3259 * The closing and draining connection states exist to ensure
3260 * that connections close cleanly and that delayed or reordered
3261 * packets are properly discarded. These states SHOULD persist
3262 * for at least three times the current PTO interval...
3263 *
3264 * Rearm the idle timeout only one time when entering closing
3265 * state.
3266 */
3267 qc_idle_timer_do_rearm(qc);
3268 if (qc->timer_task) {
3269 task_destroy(qc->timer_task);
3270 qc->timer_task = NULL;
3271 }
3272 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003273 qc->path->in_flight += pkt->in_flight_len;
3274 pkt->pktns->tx.in_flight += pkt->in_flight_len;
Frédéric Lécaille5f6c25e2022-08-24 16:23:44 +02003275 if (pkt->in_flight_len)
Frédéric Lécailleda9c4412022-08-22 18:47:51 +02003276 qc_set_timer(qc);
Frédéric Lécaille5f6c25e2022-08-24 16:23:44 +02003277 TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
3278 next_pkt = pkt->next;
3279 quic_tx_packet_refinc(pkt);
3280 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003281 }
3282 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003283
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003284 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3285
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003286 return 1;
3287}
3288
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003289/* Copy into <buf> buffer a stateless reset token depending on the
3290 * <salt> salt input. This is the cluster secret which will be derived
3291 * as HKDF input secret to generate this token.
3292 * Return 1 if succeeded, 0 if not.
3293 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003294static int quic_stateless_reset_token_cpy(struct quic_conn *qc,
3295 unsigned char *buf, size_t len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003296 const unsigned char *salt, size_t saltlen)
3297{
3298 /* Input secret */
3299 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3300 size_t keylen = strlen(global.cluster_secret);
3301 /* Info */
3302 const unsigned char label[] = "stateless token";
3303 size_t labellen = sizeof label - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003304 int ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003305
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003306 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3307
3308 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003309 key, keylen, salt, saltlen, label, labellen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003310 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3311 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003312}
3313
3314/* Initialize the stateless reset token attached to <cid> connection ID.
3315 * Returns 1 if succeeded, 0 if not.
3316 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003317static int quic_stateless_reset_token_init(struct quic_conn *qc,
3318 struct quic_connection_id *quic_cid)
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003319{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003320 int ret;
3321
3322 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3323
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003324 if (global.cluster_secret) {
3325 /* Output secret */
3326 unsigned char *token = quic_cid->stateless_reset_token;
3327 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3328 /* Salt */
3329 const unsigned char *cid = quic_cid->cid.data;
3330 size_t cidlen = quic_cid->cid.len;
3331
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003332 ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003333 }
3334 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003335 /* TODO: RAND_bytes() should be replaced */
3336 ret = RAND_bytes(quic_cid->stateless_reset_token,
3337 sizeof quic_cid->stateless_reset_token) == 1;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003338 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003339
3340 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3341 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003342}
3343
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003344/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3345 * ebtree.
3346 *
3347 * The CID is randomly generated in part with the result altered to be
3348 * associated with the current thread ID. This means this function must only
3349 * be called by the quic_conn thread.
3350 *
3351 * Returns the new CID if succeeded, NULL if not.
3352 */
3353static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3354 struct quic_conn *qc,
3355 int seq_num)
3356{
3357 struct quic_connection_id *cid;
3358
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003359 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3360
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003361 cid = pool_alloc(pool_head_quic_connection_id);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003362 if (!cid) {
3363 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
3364 goto err;
3365 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003366
3367 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003368 /* TODO: RAND_bytes() should be replaced */
3369 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) {
3370 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003371 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003372 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003373
3374 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003375 if (quic_stateless_reset_token_init(qc, cid) != 1) {
3376 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003377 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003378 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003379
3380 cid->qc = qc;
3381
3382 cid->seq_num.key = seq_num;
3383 cid->retire_prior_to = 0;
3384 /* insert the allocated CID in the quic_conn tree */
3385 eb64_insert(root, &cid->seq_num);
3386
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003387 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003388 return cid;
3389
3390 err:
3391 pool_free(pool_head_quic_connection_id, cid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003392 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003393 return NULL;
3394}
3395
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003396/* Build all the frames which must be sent just after the handshake have succeeded.
3397 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3398 * a HANDSHAKE_DONE frame.
3399 * Return 1 if succeeded, 0 if not.
3400 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003401static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003402{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003403 int ret = 0, i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003404 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003405 struct quic_frame *frm, *frmbak;
3406 struct list frm_list = LIST_HEAD_INIT(frm_list);
3407 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003408
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003409 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
3410
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003411 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003412 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003413 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003414 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003415 if (!frm) {
3416 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3417 goto leave;
3418 }
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003419
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003420 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003421 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003422 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003423 }
3424
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003425 /* Initialize <max> connection IDs minus one: there is
3426 * already one connection ID used for the current connection.
3427 */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003428 first = 1;
3429 max = qc->tx.params.active_connection_id_limit;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003430
3431 /* TODO: check limit */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003432 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003433 struct quic_connection_id *cid;
3434
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003435 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003436 if (!frm) {
3437 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003438 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003439 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003440
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003441 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003442 cid = new_quic_cid(&qc->cids, qc, i);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003443 if (!cid) {
Frédéric Lécaillebccbad22022-08-31 15:02:53 +02003444 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003445 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003446 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003447 }
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003448
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003449 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003450 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003451
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003452 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003453 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003454 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003455
3456 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003457 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003458
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003459 ret = 1;
3460 leave:
3461 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
3462 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003463
3464 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003465 /* free the frames */
3466 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3467 pool_free(pool_head_quic_frame, frm);
3468
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003469 node = eb64_lookup_ge(&qc->cids, first);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003470 while (node) {
3471 struct quic_connection_id *cid;
3472
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003473 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003474 if (cid->seq_num.key >= max)
3475 break;
3476
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003477 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003478 ebmb_delete(&cid->node);
3479 eb64_delete(&cid->seq_num);
3480 pool_free(pool_head_quic_connection_id, cid);
3481 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003482 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003483}
3484
3485/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003486void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003487{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003488 struct eb64_node *n;
3489 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003491 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
3492
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003493 n = eb64_first(&arngs->root);
3494 while (n) {
3495 struct eb64_node *next;
3496
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003497 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003498 next = eb64_next(n);
3499 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003500 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003501 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003502 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003503
3504 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003505}
3506
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003507/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3508 * descending order.
3509 */
3510static inline size_t sack_gap(struct quic_arng_node *p,
3511 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003512{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003513 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003514}
3515
3516
3517/* Remove the last elements of <ack_ranges> list of ack range updating its
3518 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003519 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003520 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003521static int quic_rm_last_ack_ranges(struct quic_conn *qc,
3522 struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003523{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003524 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003525 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003526
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003527 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3528
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003529 last = eb64_last(&arngs->root);
3530 while (last && arngs->enc_sz > limit) {
3531 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003532
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003533 prev = eb64_prev(last);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003534 if (!prev) {
3535 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
3536 goto out;
3537 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003538
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003539 last_node = eb64_entry(last, struct quic_arng_node, first);
3540 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003541 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3542 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3543 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3544 --arngs->sz;
3545 eb64_delete(last);
3546 pool_free(pool_head_quic_arng, last);
3547 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003548 }
3549
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003550 ret = 1;
3551 out:
3552 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3553 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003554}
3555
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003556/* Set the encoded size of <arngs> QUIC ack ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003557static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003558{
3559 struct eb64_node *node, *next;
3560 struct quic_arng_node *ar, *ar_next;
3561
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003562 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3563
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003564 node = eb64_last(&arngs->root);
3565 if (!node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003566 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003567
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003568 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003569 arngs->enc_sz = quic_int_getsize(ar->last) +
3570 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3571
3572 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003573 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003574 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3575 quic_int_getsize(ar_next->last - ar_next->first.key);
3576 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003577 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003578 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003579
3580 leave:
3581 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003582}
3583
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003584/* Insert <ar> ack range into <argns> tree of ack ranges.
3585 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003586 */
3587static inline
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003588struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
3589 struct quic_arngs *arngs,
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003590 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003591{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003592 struct quic_arng_node *new_ar;
3593
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003594 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3595
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003596 new_ar = pool_alloc(pool_head_quic_arng);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003597 if (!new_ar) {
3598 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
3599 goto leave;
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003600 }
3601
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003602 new_ar->first.key = ar->first;
3603 new_ar->last = ar->last;
3604 eb64_insert(&arngs->root, &new_ar->first);
3605 arngs->sz++;
3606
3607 leave:
3608 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003609 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003610}
3611
3612/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003613 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003614 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003615 *
3616 * Descending order
3617 * ------------->
3618 * range1 range2
3619 * ..........|--------|..............|--------|
3620 * ^ ^ ^ ^
3621 * | | | |
3622 * last1 first1 last2 first2
3623 * ..........+--------+--------------+--------+......
3624 * diff1 gap12 diff2
3625 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003626 * To encode the previous list of ranges we must encode integers as follows in
3627 * descending order:
3628 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003629 * with diff1 = last1 - first1
3630 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003631 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003632 *
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003633
3634returns 0 on error
3635
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003636 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003637int quic_update_ack_ranges_list(struct quic_conn *qc,
3638 struct quic_arngs *arngs,
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003639 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003640{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003641 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003642 struct eb64_node *le;
3643 struct quic_arng_node *new_node;
3644 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003645
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003646 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3647
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003648 new = NULL;
3649 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003650 new_node = quic_insert_new_range(qc, arngs, ar);
3651 if (new_node)
3652 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003653
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003654 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003655 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003656
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003657 le = eb64_lookup_le(&arngs->root, ar->first);
3658 if (!le) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003659 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003660 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003661 goto leave;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003662
3663 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003664 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003665 else {
3666 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003667 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003668
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003669 /* Already existing range */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003670 if (le_ar->last >= ar->last) {
3671 ret = 1;
3672 }
3673 else if (le_ar->last + 1 >= ar->first) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003674 le_ar->last = ar->last;
3675 new = le;
3676 new_node = le_ar;
3677 }
3678 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003679 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003680 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003681 goto leave;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003682
3683 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003684 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003685 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003686
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003687 /* Verify that the new inserted node does not overlap the nodes
3688 * which follow it.
3689 */
3690 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003691 struct eb64_node *next;
3692 struct quic_arng_node *next_node;
3693
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003694 while ((next = eb64_next(new))) {
3695 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003696 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003697 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003698 break;
3699
3700 if (next_node->last > new_node->last)
3701 new_node->last = next_node->last;
3702 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003703 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003704 /* Decrement the size of these ranges. */
3705 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003706 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707 }
3708
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003709 ret = 1;
3710 leave:
3711 quic_arngs_set_enc_sz(qc, arngs);
3712 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
3713 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003714}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003715/* Remove the header protection of packets at <el> encryption level.
3716 * Always succeeds.
3717 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003718static 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 +01003719{
3720 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003721 struct quic_rx_packet *pqpkt, *pkttmp;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003722 struct quic_enc_level *app_qel;
3723
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003724 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3725 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003726 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003727 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003728 TRACE_DEVEL("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003729 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003730 goto out;
3731 }
3732 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003733 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003734 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02003735 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003736 TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003737 }
3738 else {
3739 /* The AAD includes the packet number field */
3740 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3741 /* Store the packet into the tree of packets to decrypt. */
3742 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003743 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3744 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003745 TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003746 }
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003747 LIST_DELETE(&pqpkt->list);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003748 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003749 }
3750
3751 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003752 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003753}
3754
3755/* Process all the CRYPTO frame at <el> encryption level.
3756 * Return 1 if succeeded, 0 if not.
3757 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003758static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
3759 struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003760 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003761{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003762 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003763 struct eb64_node *node;
3764
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003765 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3766
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003767 node = eb64_first(&el->rx.crypto.frms);
3768 while (node) {
3769 struct quic_rx_crypto_frm *cf;
3770
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003771 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003772 if (cf->offset_node.key != el->rx.crypto.offset)
3773 break;
3774
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003775 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) {
3776 TRACE_ERROR("qc_provide_cdata() failed", QUIC_EV_CONN_TRMHP);
3777 goto leave;
3778 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003779
3780 node = eb64_next(node);
3781 quic_rx_packet_refdec(cf->pkt);
3782 eb64_delete(&cf->offset_node);
3783 pool_free(pool_head_quic_rx_crypto_frm, cf);
3784 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003785
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003786 ret = 1;
3787 leave:
3788 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3789 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003790}
3791
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003792/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003793 * 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 +02003794 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003795 * Return 1 if succeeded, 0 if not.
3796 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003797int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3798 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003799{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003800 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003801 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003802 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003803 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003804 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003805 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003806
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003807 TRACE_ENTER(QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003808 qel = cur_el;
3809 next_tel:
3810 if (!qel)
3811 goto out;
3812
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003813 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814 while (node) {
3815 struct quic_rx_packet *pkt;
3816
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003817 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003818 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003819 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003820 if (!qc_pkt_decrypt(pkt, qel, qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003821 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003822 TRACE_ERROR("packet decryption failed -> dropped",
3823 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003824 }
3825 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003826 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003827 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003828 TRACE_ERROR("packet parsing failed -> dropped",
3829 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02003830 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003831 }
3832 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003833 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3834
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003835 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3836 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3837 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003838 qc_idle_timer_rearm(qc, 1);
3839 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003840 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003841 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003842 largest_pn_time_received = pkt->time_received;
3843 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003844 /* Update the list of ranges to acknowledge. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003845 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
3846 TRACE_ERROR("Could not update ack range list",
3847 QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003848 }
3849 }
3850 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003851 eb64_delete(&pkt->pn_node);
3852 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003853 }
3854
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003855 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3856 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003857 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003858 /* Update the largest acknowledged packet timestamps */
3859 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3860 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3861 }
3862
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003863 if (!qc_treat_rx_crypto_frms(qc, qel, ctx)) {
3864 // trace already emitted by function above
3865 goto leave;
3866 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003867
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003868 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003869 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003870 qel = next_el;
Frédéric Lécaille91a211f2022-05-24 10:54:42 +02003871 largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003872 goto next_tel;
3873 }
3874
3875 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003876 ret = 1;
3877 leave:
3878 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, ctx->qc);
3879 return ret;
3880}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003881
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003882/* Check if it's possible to remove header protection for packets related to
3883 * encryption level <qel>. If <qel> is NULL, assume it's false.
3884 *
3885 * Return true if the operation is possible else false.
3886 */
3887static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3888{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003889 int ret = 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003890 enum quic_tls_enc_level tel;
3891
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003892 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3893
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003894 if (!qel)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003895 goto cant_rm_hp;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003896
3897 tel = ssl_to_quic_enc_level(qel->level);
3898
3899 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003900 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003901 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003902 goto cant_rm_hp;
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003903 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003904
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003905 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3906 TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc);
3907 goto cant_rm_hp;
3908 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003909
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003910 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003911 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003912 qc->mux_state == QC_MUX_NULL) {
3913 TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
3914 goto cant_rm_hp;
3915 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003916
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003917 ret = 1;
3918 cant_rm_hp:
3919 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3920 return ret;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003921}
3922
Amaury Denoyellecc130472022-08-17 10:08:16 +02003923/* Try to send application frames from list <frms> on connection <qc>.
3924 *
Amaury Denoyelle70467562022-08-17 16:33:13 +02003925 * Use qc_send_app_probing wrapper when probing with old data.
Amaury Denoyellecc130472022-08-17 10:08:16 +02003926 *
3927 * Returns 1 on success. Some data might not have been sent due to congestion,
3928 * in this case they are left in <frms> input list. The caller may subscribe on
3929 * quic-conn to retry later.
3930 *
3931 * Returns 0 on critical error.
3932 * TODO review and classify more distinctly transient from definitive errors to
3933 * allow callers to properly handle it.
3934 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003935static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003936{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003937 int status = 0;
3938 struct buffer *buf;
3939
3940 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3941
3942 buf = qc_txb_alloc(qc);
3943 if (!buf) {
3944 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
3945 goto leave;
3946 }
3947
Frédéric Lécailledc077512022-07-26 09:17:19 +02003948 /* Prepare and send packets until we could not further prepare packets. */
3949 while (1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003950 int ret;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003951 /* Currently buf cannot be non-empty at this stage. Even if a
3952 * previous sendto() has failed it is emptied to simulate
3953 * packet emission and rely on QUIC lost detection to try to
3954 * emit it.
3955 */
3956 BUG_ON_HOT(b_data(buf));
3957 b_reset(buf);
3958
Amaury Denoyellef2476052022-08-04 16:19:57 +02003959 ret = qc_prep_app_pkts(qc, buf, frms);
Frédéric Lécailledc077512022-07-26 09:17:19 +02003960 if (ret == -1)
3961 goto err;
3962 else if (ret == 0)
3963 goto out;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003964
Amaury Denoyellef2476052022-08-04 16:19:57 +02003965 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécailledc077512022-07-26 09:17:19 +02003966 goto err;
3967 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003968
3969 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003970 status = 1;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003971 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003972 leave:
3973 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3974 return status;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003975
3976 err:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003977 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003978 goto leave;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003979}
3980
Amaury Denoyellecc130472022-08-17 10:08:16 +02003981/* Try to send application frames from list <frms> on connection <qc>. Use this
Amaury Denoyelle70467562022-08-17 16:33:13 +02003982 * function when probing is required.
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003983 *
3984 * Returns the result from qc_send_app_pkts function.
3985 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003986static forceinline int qc_send_app_probing(struct quic_conn *qc,
3987 struct list *frms)
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003988{
3989 int ret;
3990
3991 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3992
Amaury Denoyelle70467562022-08-17 16:33:13 +02003993 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
3994 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003995 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003996 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003997
3998 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3999 return ret;
4000}
4001
Amaury Denoyelle70467562022-08-17 16:33:13 +02004002/* Try to send application frames from list <frms> on connection <qc>. This
4003 * function is provided for MUX upper layer usage only.
Amaury Denoyellecc130472022-08-17 10:08:16 +02004004 *
4005 * Returns the result from qc_send_app_pkts function.
4006 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02004007int qc_send_mux(struct quic_conn *qc, struct list *frms)
Amaury Denoyellecc130472022-08-17 10:08:16 +02004008{
4009 int ret;
4010
4011 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle70467562022-08-17 16:33:13 +02004012 BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
Amaury Denoyellecc130472022-08-17 10:08:16 +02004013
Amaury Denoyelle70467562022-08-17 16:33:13 +02004014 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4015 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004016 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02004017 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004018
4019 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4020 return ret;
4021}
4022
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004023/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4024 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004025 * QUIC connection. <old_data> is used as boolean to send data already sent but
4026 * not already acknowledged (in flight).
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004027 * Returns 1 if succeeded, 0 if not.
4028 */
4029int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4030 enum quic_tls_enc_level tel, struct list *tel_frms,
4031 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4032{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004033 int ret, status = 0;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004034 struct buffer *buf = qc_txb_alloc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004035
4036 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4037
4038 if (!buf) {
4039 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4040 goto leave;
4041 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004042
Amaury Denoyellef2476052022-08-04 16:19:57 +02004043 /* Currently buf cannot be non-empty at this stage. Even if a previous
4044 * sendto() has failed it is emptied to simulate packet emission and
4045 * rely on QUIC lost detection to try to emit it.
4046 */
4047 BUG_ON_HOT(b_data(buf));
4048 b_reset(buf);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004049
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004050 if (old_data) {
4051 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004052 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004053 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02004054
4055 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004056 if (ret == -1)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004057 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004058 else if (ret == 0)
4059 goto skip_send;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004060
Amaury Denoyellef2476052022-08-04 16:19:57 +02004061 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004062 goto out;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004063
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004064 skip_send:
4065 status = 1;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004066 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004067 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004068 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004069 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004070 leave:
4071 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4072 return status;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004073}
4074
4075/* Retransmit up to two datagrams depending on packet number space */
4076static void qc_dgrams_retransmit(struct quic_conn *qc)
4077{
4078 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4079 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4080 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4081
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004082 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4083
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004084 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4085 struct list ifrms = LIST_HEAD_INIT(ifrms);
4086 struct list hfrms = LIST_HEAD_INIT(hfrms);
4087
4088 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004089 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4090 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004091 if (!LIST_ISEMPTY(&ifrms)) {
4092 iqel->pktns->tx.pto_probe = 1;
4093 if (!LIST_ISEMPTY(&hfrms)) {
4094 hqel->pktns->tx.pto_probe = 1;
4095 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4096 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004097 /* Put back unsent frames in their packet number spaces */
4098 LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms);
4099 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004100 }
4101 }
4102 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004103 /* This list has potentially been already used and spliced
4104 * to another one attached to the connection. We must reinitialize it.
4105 */
4106 LIST_INIT(&hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004107 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004108 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004109 if (!LIST_ISEMPTY(&hfrms)) {
4110 hqel->pktns->tx.pto_probe = 1;
4111 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
4112 QUIC_TLS_ENC_LEVEL_NONE, NULL);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004113 /* Put back unsent frames into their packet number spaces */
4114 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004115 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004116 TRACE_STATE("no more need to probe Handshake packet number space",
4117 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004118 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4119 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004120 TRACE_STATE("no more need to probe Initial packet number space",
4121 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004122 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4123 }
4124 else {
4125 int i;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004126
4127 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004128 struct list frms1 = LIST_HEAD_INIT(frms1);
4129
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004130 hqel->pktns->tx.pto_probe = 0;
4131 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4132 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004133 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004134 if (!LIST_ISEMPTY(&frms1)) {
4135 hqel->pktns->tx.pto_probe = 1;
4136 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4137 QUIC_TLS_ENC_LEVEL_NONE, NULL);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004138 /* Put back unsent frames into their packet number spaces */
4139 LIST_SPLICE(&hqel->pktns->tx.frms, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004140 }
4141 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004142 TRACE_STATE("no more need to probe Handshake packet number space",
4143 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004144 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4145 }
4146 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004147 struct list frms2 = LIST_HEAD_INIT(frms2);
4148 struct list frms1 = LIST_HEAD_INIT(frms1);
4149
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004150 aqel->pktns->tx.pto_probe = 0;
4151 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4152 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4153 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4154 if (!LIST_ISEMPTY(&frms1)) {
4155 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004156 qc_send_app_probing(qc, &frms1);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004157 /* Put back unsent frames into their packet number spaces */
4158 LIST_SPLICE(&aqel->pktns->tx.frms, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004159 }
4160 if (!LIST_ISEMPTY(&frms2)) {
4161 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004162 qc_send_app_probing(qc, &frms2);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004163 /* Put back unsent frames into their packet number spaces */
4164 LIST_SPLICE(&aqel->pktns->tx.frms, &frms2);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004165 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004166 TRACE_STATE("no more need to probe 01RTT packet number space",
4167 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004168 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4169 }
4170 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004171 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004172}
4173
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004174/* QUIC connection packet handler task (post handshake) */
4175static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4176{
4177 struct ssl_sock_ctx *ctx;
4178 struct quic_conn *qc;
4179 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004180
4181
4182 ctx = context;
4183 qc = ctx->qc;
4184 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004185
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004186 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4187 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004188
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004189 /* Retranmissions */
4190 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004191 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004192 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4193 qc_dgrams_retransmit(qc);
4194 }
4195
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004196 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004197 qc_rm_hp_pkts(qc, qel);
4198
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004199 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) {
4200 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4201 goto out;
4202 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004203
Frédéric Lécaille47756802022-03-25 09:12:16 +01004204 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004205 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4206 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01004207 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004208 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004209
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004210 /* XXX TODO: how to limit the list frames to send */
Amaury Denoyelle70467562022-08-17 16:33:13 +02004211 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
4212 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004213 goto out;
4214 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004215
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004216 out:
4217 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004218 return t;
4219}
4220
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004221/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4222static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4223{
4224 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
4225 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
4226 qel->pktns->tx.pto_probe ||
4227 !LIST_ISEMPTY(&qel->pktns->tx.frms);
4228}
4229
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004230/* QUIC connection packet handler task. */
4231struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004232{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004233 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004234 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004235 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004236 enum quic_tls_enc_level tel, next_tel;
4237 struct quic_enc_level *qel, *next_qel;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004238 struct buffer *buf = NULL;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004239 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004240
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004241 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004242 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004243 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004244 st = qc->state;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004245 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004246
4247 /* Retranmissions */
4248 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004249 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004250 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4251 qc_dgrams_retransmit(qc);
4252 }
4253
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004254 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
4255 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004256 TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached",
4257 QUIC_EV_CONN_IO_CB, qc);
4258 /* The I/O handler has been woken up by the dgram parser (qc_lstnr_pkt_rcv())
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004259 * after the anti-amplification was reached.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004260 *
4261 * TODO: this part should be removed. This was there because the
4262 * datagram parser was not executed by only one thread.
Frédéric Lécaille59507de2022-08-11 12:14:07 +02004263 */
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004264 qc_set_timer(qc);
4265 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
4266 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
4267 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004268 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004269 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004270 (!LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004271 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004272 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004273 if (st >= QUIC_HS_ST_COMPLETE &&
4274 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004275 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004276 /* There may be remaining Handshake packets to treat and acknowledge. */
4277 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4278 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4279 }
4280 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004281 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004282
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004283 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02004284 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004285
4286 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004287 /* Treat packets waiting for header packet protection decryption */
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004288 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004289 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004290
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004291 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
4292 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4293 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004294 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004295
Frédéric Lécaille47756802022-03-25 09:12:16 +01004296 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4297 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4298 goto out;
4299
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004300 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004301 !LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004302 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
4303 qel = next_qel;
4304 next_qel = NULL;
4305 goto next_level;
4306 }
4307 else {
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004308 struct quic_rx_packet *pkt, *pkttmp;
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004309 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
4310
4311 /* Drop these 0-RTT packets */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004312 TRACE_DEVEL("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004313 list_for_each_entry_safe(pkt, pkttmp, &aqel->rx.pqpkts, list) {
4314 LIST_DELETE(&pkt->list);
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004315 quic_rx_packet_refdec(pkt);
4316 }
4317 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004318 }
4319
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004320 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004321 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004322 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004323 !quic_build_post_handshake_frames(qc))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004324 goto out;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01004325
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004326 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004327 QUIC_FL_TLS_SECRETS_DCD)) {
4328 /* Discard the Handshake keys. */
4329 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
4330 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
4331 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
4332 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004333 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004334 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004335 }
4336
4337 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
4338 /* There may be remaining handshake to build (acks) */
4339 st = QUIC_HS_ST_SERVER_HANDSHAKE;
4340 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004341 }
4342
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01004343 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
4344 * be considered.
4345 */
4346 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004347 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004348
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004349 if (!qc_need_sending(qc, qel) &&
4350 (!next_qel || !qc_need_sending(qc, next_qel))) {
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004351 goto skip_send;
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004352 }
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004353
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004354 buf = qc_txb_alloc(qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02004355 if (!buf)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004356 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004357
4358 /* Currently buf cannot be non-empty at this stage. Even if a previous
4359 * sendto() has failed it is emptied to simulate packet emission and
4360 * rely on QUIC lost detection to try to emit it.
4361 */
4362 BUG_ON_HOT(b_data(buf));
4363 b_reset(buf);
4364
4365 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02004366 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004367 if (ret == -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004368 goto out;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004369 else if (ret == 0)
4370 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004371
Amaury Denoyellef2476052022-08-04 16:19:57 +02004372 if (!qc_send_ppkts(buf, ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004373 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004374
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004375 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004376 /* Check if there is something to do for the next level.
4377 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004378 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004379 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004380 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004381 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004382 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004383 goto next_level;
4384 }
4385
Frédéric Lécaille47756802022-03-25 09:12:16 +01004386 out:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004387 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004388 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004389 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004390}
4391
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004392/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004393static void quic_conn_enc_level_uninit(struct quic_conn *qc, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004394{
4395 int i;
4396
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004397 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4398
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004399 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4400 if (qel->tx.crypto.bufs[i]) {
4401 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4402 qel->tx.crypto.bufs[i] = NULL;
4403 }
4404 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004405 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004406
4407 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004408}
4409
4410/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004411 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004412 * Returns 1 if succeeded, 0 if not.
4413 */
4414static int quic_conn_enc_level_init(struct quic_conn *qc,
4415 enum quic_tls_enc_level level)
4416{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004417 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004418 struct quic_enc_level *qel;
4419
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004420 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4421
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004422 qel = &qc->els[level];
4423 qel->level = quic_to_ssl_enc_level(level);
4424 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4425 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4426 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004427 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004428
4429 qel->rx.pkts = EB_ROOT;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004430 LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004431 qel->rx.crypto.offset = 0;
4432 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004433
4434 /* Allocate only one buffer. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004435 /* TODO: use a pool */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004436 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4437 if (!qel->tx.crypto.bufs)
4438 goto err;
4439
4440 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4441 if (!qel->tx.crypto.bufs[0])
4442 goto err;
4443
4444 qel->tx.crypto.bufs[0]->sz = 0;
4445 qel->tx.crypto.nb_buf = 1;
4446
4447 qel->tx.crypto.sz = 0;
4448 qel->tx.crypto.offset = 0;
4449
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004450 ret = 1;
4451 leave:
4452 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4453 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004454
4455 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004456 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004457 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004458}
4459
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004460/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4461 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004462 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004463 * This function must only be called by the thread responsible of the quic_conn
4464 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004465 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004466static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004467{
4468 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004469 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004470 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004471 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004472 struct quic_rx_packet *pkt, *pktback;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004473
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004474 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4475
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004476 /* We must not free the quic-conn if the MUX is still allocated. */
4477 BUG_ON(qc->mux_state == QC_MUX_READY);
4478
Willy Tarreau54bc7862022-08-10 07:26:27 +02004479 /* in the unlikely (but possible) case the connection was just added to
4480 * the accept_list we must delete it from there.
4481 */
4482 MT_LIST_DELETE(&qc->accept_list);
4483
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004484 /* free remaining stream descriptors */
4485 node = eb64_first(&qc->streams_by_id);
4486 while (node) {
4487 struct qc_stream_desc *stream;
4488
4489 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4490 node = eb64_next(node);
4491
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004492 /* all streams attached to the quic-conn are released, so
4493 * qc_stream_desc_free will liberate the stream instance.
4494 */
4495 BUG_ON(!stream->release);
Frédéric Lécailleea4a5cb2022-08-20 18:59:36 +02004496 qc_stream_desc_free(stream, 1);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004497 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004498
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004499 /* Purge Rx packet list. */
4500 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
4501 LIST_DELETE(&pkt->qc_rx_pkt_list);
4502 pool_free(pool_head_quic_rx_packet, pkt);
4503 }
4504
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004505 if (qc->idle_timer_task) {
4506 task_destroy(qc->idle_timer_task);
4507 qc->idle_timer_task = NULL;
4508 }
4509
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004510 if (qc->timer_task) {
4511 task_destroy(qc->timer_task);
4512 qc->timer_task = NULL;
4513 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004514
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004515 /* remove the connection from receiver cids trees */
4516 ebmb_delete(&qc->odcid_node);
4517 ebmb_delete(&qc->scid_node);
4518 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004519
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004520 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004521 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004522 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004523 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004524 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004525 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004526
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004527 quic_tls_ku_free(qc);
4528 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4529 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004530 quic_conn_enc_level_uninit(qc, &qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004531 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004532 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004533
4534 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4535 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4536 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004537
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004538 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
Frédéric Lécaille8ddde4f2022-08-01 14:07:50 +02004539 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004540 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004541 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004542
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004543 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4544 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004545 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004546
4547 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004548}
4549
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004550static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004551{
4552 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004553 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004554
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004555 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004556
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004557 /* Next application data can be dropped. */
4558 qc->mux_state = QC_MUX_RELEASED;
4559
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004560 /* If the quic-conn timer has already expired free the quic-conn. */
4561 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4562 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004563 goto leave;
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004564 }
4565
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02004566 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004567 leave:
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004568 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004569}
4570
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004571/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004572static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004573{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004574 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004575 struct quic_conn *qc;
4576 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004577
4578 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004579 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004580 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004581 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004582 task->expire = TICK_ETERNITY;
4583 pktns = quic_loss_pktns(qc);
4584 if (tick_isset(pktns->tx.loss_time)) {
4585 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4586
4587 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
Frédéric Lécaille15773f22022-08-24 17:57:09 +02004588 if (!LIST_ISEMPTY(&lost_pkts))
4589 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécaille277c4622022-08-24 17:06:04 +02004590 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004591 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004592 goto out;
4593 }
4594
4595 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004596 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004597 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4598 qc->qcc->subs->events & SUB_RETRY_SEND) {
4599 struct qcc *qcc = qc->qcc;
4600
4601 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4602 tasklet_wakeup(qcc->subs->tasklet);
4603 qcc->subs->events &= ~SUB_RETRY_SEND;
4604 if (!qcc->subs->events)
4605 qcc->subs = NULL;
4606 }
4607 else {
4608 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4609 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4610 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004611 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4612 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004613 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004614 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4615 }
4616 }
4617 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
4618 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4619 }
4620 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
4621 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004622 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004623 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004624 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004625 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004626 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4627 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4628
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004629 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004630 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004631 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004632 iel->pktns->tx.pto_probe = 1;
4633 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004634
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004635 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4636 qc->path->loss.pto_count++;
4637
4638 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004639 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004640
4641 return task;
4642}
4643
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004644/* Parse the Retry token from buffer <token> with <end> a pointer to
4645 * one byte past the end of this buffer. This will extract the ODCID
4646 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004647 *
4648 * Returns 0 on success else non-zero.
4649 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004650static int parse_retry_token(struct quic_conn *qc,
4651 const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004652 struct quic_cid *odcid)
4653{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004654 int ret = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004655 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004656 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004657
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004658 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
4659
4660 if (!quic_dec_int(&odcid_len, &token, end)) {
4661 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
4662 goto leave;
4663 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004664
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004665 /* RFC 9000 7.2. Negotiating Connection IDs:
4666 * When an Initial packet is sent by a client that has not previously
4667 * received an Initial or Retry packet from the server, the client
4668 * populates the Destination Connection ID field with an unpredictable
4669 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4670 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004671 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
4672 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
4673 goto leave;
4674 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004675
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004676 if (end - token < odcid_len + sizeof timestamp) {
4677 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
4678 goto leave;
4679 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004680
4681 timestamp = ntohl(read_u32(token + odcid_len));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004682 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
4683 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
4684 goto leave;
4685 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004686
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004687 ret = 1;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004688 memcpy(odcid->data, token, odcid_len);
4689 odcid->len = odcid_len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004690 leave:
4691 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
4692 return !ret;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004693}
4694
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004695/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4696 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4697 * for QUIC servers (or haproxy listeners).
4698 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4699 * <token> the token found to be used for this connection with <token_len> as
4700 * length. <saddr> is the source address.
4701 * Returns the connection if succeeded, NULL if not.
4702 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004703static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004704 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02004705 const struct quic_cid *token_odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004706 struct sockaddr_storage *saddr,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004707 int server, int token, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004708{
4709 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004710 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004711 /* Initial CID. */
4712 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004713 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004714 struct listener *l = NULL;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004715 struct quic_cc_algo *cc_algo = NULL;
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004716 struct quic_tls_ctx *ictx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004717 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004718 qc = pool_zalloc(pool_head_quic_conn);
4719 if (!qc) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004720 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004721 goto err;
4722 }
4723
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004724 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4725 if (!buf_area) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004726 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004727 goto err;
4728 }
4729
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004730 qc->cids = EB_ROOT;
4731 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004732 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004733 struct proxy *prx;
4734
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004735 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004736 prx = l->bind_conf->frontend;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004737 cc_algo = l->bind_conf->quic_cc_algo;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004738
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004739 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4740 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004741 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004742 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004743 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004744 qc->odcid.len = dcid->len;
4745 qc->odcid.addrlen = dcid->addrlen;
4746 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004747
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004748 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004749 if (scid->len)
4750 memcpy(qc->dcid.data, scid->data, scid->len);
4751 qc->dcid.len = scid->len;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004752 qc->tx.buf = BUF_NULL;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004753 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004754 }
4755 /* QUIC Client (outgoing connection to servers) */
4756 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004757 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004758 if (dcid->len)
4759 memcpy(qc->dcid.data, dcid->data, dcid->len);
4760 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004761 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004762 qc->mux_state = QC_MUX_NULL;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02004763 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004764
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004765 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004766 if (!icid) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004767 TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004768 goto err;
4769 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004770
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004771 /* insert the allocated CID in the receiver datagram handler tree */
4772 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004773 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004774
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004775 /* Select our SCID which is the first CID with 0 as sequence number. */
4776 qc->scid = icid->cid;
4777
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004778 /* Packet number spaces initialization. */
4779 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4780 quic_pktns_init(&qc->pktns[i]);
4781 /* QUIC encryption level context initialization. */
4782 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004783 if (!quic_conn_enc_level_init(qc, i)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004784 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004785 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004786 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004787 /* Initialize the packet number space. */
4788 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4789 }
4790
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004791 qc->original_version = qv;
4792 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004793 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4794 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004795 /* TX part. */
4796 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004797 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4798 qc->tx.wbuf = qc->tx.rbuf = 0;
4799 qc->tx.bytes = 0;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004800 qc->tx.buf = BUF_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004801 /* RX part. */
4802 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004803 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004804 for (i = 0; i < QCS_MAX_TYPES; i++)
4805 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004806
4807 qc->nb_pkt_for_cc = 1;
4808 qc->nb_pkt_since_cc = 0;
4809
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004810 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004811 if (!quic_tls_ku_init(qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004812 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004813 goto err;
4814 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004815
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004816 /* XXX TO DO: Only one path at this time. */
4817 qc->path = &qc->paths[0];
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004818 quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004819
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004820 /* required to use MTLIST_IN_LIST */
4821 MT_LIST_INIT(&qc->accept_list);
4822
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004823 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004824 qc->stream_buf_count = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004825 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4826
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004827 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
4828 icid->stateless_reset_token,
4829 dcid->data, dcid->len,
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02004830 qc->scid.data, qc->scid.len, token_odcid))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004831 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004832
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004833 if (qc_conn_alloc_ssl_ctx(qc) ||
4834 !quic_conn_init_timer(qc) ||
4835 !quic_conn_init_idle_timer_task(qc))
4836 goto err;
4837
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004838 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
4839 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004840 goto err;
4841
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004842 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004843
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004844 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004845
4846 err:
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004847 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4848 if (qc)
4849 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004850 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004851 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004852 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004853}
4854
4855/* Initialize the timer task of <qc> QUIC connection.
4856 * Returns 1 if succeeded, 0 if not.
4857 */
4858static int quic_conn_init_timer(struct quic_conn *qc)
4859{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004860 int ret = 0;
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004861 /* Attach this task to the same thread ID used for the connection */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004862 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4863
Willy Tarreau87168752022-06-13 16:31:53 +02004864 qc->timer_task = task_new_on(qc->tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004865 if (!qc->timer_task) {
4866 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4867 goto leave;
4868 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004869
4870 qc->timer = TICK_ETERNITY;
4871 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004872 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004873
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004874 ret = 1;
4875 leave:
4876 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4877 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004878}
4879
Frédéric Lécaille47756802022-03-25 09:12:16 +01004880/* Rearm the idle timer for <qc> QUIC connection. */
4881static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4882{
4883 unsigned int expire;
4884
4885 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4886 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4887}
4888
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004889/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4890 * which is set to 1 when receiving a packet , and 0 when sending packet
4891 */
4892static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4893{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004894 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4895
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004896 if (read) {
4897 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4898 }
4899 else {
4900 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4901 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004902 qc_idle_timer_do_rearm(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004903
4904 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004905}
4906
4907/* The task handling the idle timeout */
4908static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4909{
4910 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004911 struct quic_counters *prx_counters = qc->prx_counters;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004912 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004913
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004914 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4915
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004916 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4917 * might free the quic-conn too early via quic_close().
4918 */
4919 qc_notify_close(qc);
4920
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004921 /* If the MUX is still alive, keep the quic-conn. The MUX is
4922 * responsible to call quic_close to release it.
4923 */
4924 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4925 if (qc->mux_state != QC_MUX_READY)
4926 quic_conn_release(qc);
4927
4928 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4929 * least clean some parts of it such as the tasklet.
4930 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004931
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004932 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
4933 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004934 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004935 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004936 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004937
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004938 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004939 return NULL;
4940}
4941
4942/* Initialize the idle timeout task for <qc>.
4943 * Returns 1 if succeeded, 0 if not.
4944 */
4945static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4946{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004947 int ret = 0;
4948
4949 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4950
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004951 qc->idle_timer_task = task_new_here();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004952 if (!qc->idle_timer_task) {
4953 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4954 goto leave;
4955 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004956
4957 qc->idle_timer_task->process = qc_idle_timer_task;
4958 qc->idle_timer_task->context = qc;
4959 qc_idle_timer_rearm(qc, 1);
4960 task_queue(qc->idle_timer_task);
4961
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004962 ret = 1;
4963 leave:
4964 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4965 return ret;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004966}
4967
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004968/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4969 * past one byte of this buffer.
4970 */
4971static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4972 struct quic_rx_packet *pkt)
4973{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004974 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004975 unsigned char dcid_len, scid_len;
4976
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004977 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
4978
4979 if (end == *buf) {
4980 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
4981 goto leave;
4982 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004983
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004984 /* Destination Connection ID Length */
4985 dcid_len = *(*buf)++;
4986 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004987 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
4988 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
4989 goto leave;
4990 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004991
4992 if (dcid_len) {
4993 /* Check that the length of this received DCID matches the CID lengths
4994 * of our implementation for non Initials packets only.
4995 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004996 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4997 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004998 dcid_len != QUIC_HAP_CID_LEN) {
4999 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
5000 goto leave;
5001 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005002
5003 memcpy(pkt->dcid.data, *buf, dcid_len);
5004 }
5005
5006 pkt->dcid.len = dcid_len;
5007 *buf += dcid_len;
5008
5009 /* Source Connection ID Length */
5010 scid_len = *(*buf)++;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005011 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
5012 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
5013 goto leave;
5014 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005015
5016 if (scid_len)
5017 memcpy(pkt->scid.data, *buf, scid_len);
5018 pkt->scid.len = scid_len;
5019 *buf += scid_len;
5020
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005021 ret = 1;
5022 leave:
5023 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
5024 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005025}
5026
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005027/* Insert <pkt> RX packet in its <qel> RX packets tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005028static void qc_pkt_insert(struct quic_conn *qc,
5029 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005030{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005031 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
5032
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005033 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01005034 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005035 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005036
5037 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005038}
5039
5040/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005041 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
5042 * byte past the end of the buffer containing this packet and <beg> the address of
5043 * the packet first byte.
5044 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
5045 * Returns 1 if succeeded, 0 if not.
5046 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005047static inline int qc_try_rm_hp(struct quic_conn *qc,
5048 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005049 unsigned char *buf, unsigned char *beg,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005050 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005051{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005052 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005053 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005054 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005055 struct quic_enc_level *qel;
5056 /* Only for traces. */
5057 struct quic_rx_packet *qpkt_trace;
5058
5059 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005060 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005061 /* The packet number is here. This is also the start minus
5062 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5063 * protection.
5064 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005065 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005066
5067 tel = quic_packet_type_enc_level(pkt->type);
5068 qel = &qc->els[tel];
5069
5070 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005071 /* Note that the following function enables us to unprotect the packet
5072 * number and its length subsequently used to decrypt the entire
5073 * packets.
5074 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005075 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02005076 qel->pktns->rx.largest_pn, pn, beg)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005077 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005078 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005079 }
5080
5081 /* The AAD includes the packet number field found at <pn>. */
5082 pkt->aad_len = pn - beg + pkt->pnl;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005083 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
5084 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005085 goto out;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005086 }
5087
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005088 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005089 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01005090 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01005091 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005092 /* If the packet number space has been discarded, this packet
5093 * will be not parsed.
5094 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005095 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005096 goto out;
5097 }
5098
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005099 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005100 pkt->pn_offset = pn - beg;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02005101 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02005102 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005103 }
5104
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005105 *el = qel;
5106 /* No reference counter incrementation here!!! */
5107 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
5108 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
5109 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
5110 b_add(&qc->rx.buf, pkt->len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005111
5112 ret = 1;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005113 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005114 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005115 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005116}
5117
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005118/* Parse the header form from <byte0> first byte of <pkt> packet to set its type.
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005119 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
5120 * of this packet into <*version>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005121 */
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005122static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
5123 unsigned char **buf, const unsigned char *end,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005124 int *long_header, uint32_t *version)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005125{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005126 int ret = 0;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005127 const unsigned char byte0 = **buf;
5128
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005129 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5130
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005131 (*buf)++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005132 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005133 unsigned char type =
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005134 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005135
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005136 *long_header = 1;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005137 /* Version */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005138 if (!quic_read_uint32(version, (const unsigned char **)buf, end)) {
5139 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
5140 goto out;
5141 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005142
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005143 if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005144 pkt->type = type;
5145 }
5146 else {
5147 switch (type) {
5148 case 0:
5149 pkt->type = QUIC_PACKET_TYPE_RETRY;
5150 break;
5151 case 1:
5152 pkt->type = QUIC_PACKET_TYPE_INITIAL;
5153 break;
5154 case 2:
5155 pkt->type = QUIC_PACKET_TYPE_0RTT;
5156 break;
5157 case 3:
5158 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
5159 break;
5160 }
5161 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005162 }
5163 else {
5164 pkt->type = QUIC_PACKET_TYPE_SHORT;
5165 *long_header = 0;
5166 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005167
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005168 ret = 1;
5169 out:
5170 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005171 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005172}
5173
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005174/* Return the QUIC version (quic_version struct) with <version> as version number
5175 * if supported or NULL if not.
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005176 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005177static inline const struct quic_version *qc_supported_version(uint32_t version)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005178{
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005179 int i;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005180
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005181 for (i = 0; i < quic_versions_nb; i++)
5182 if (quic_versions[i].num == version)
5183 return &quic_versions[i];
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005184
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005185 return NULL;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005186}
5187
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005188/*
5189 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
5190 * address <addr>.
5191 * Implementation of RFC9000 6. Version Negotiation
5192 *
5193 * TODO implement a rate-limiting sending of Version Negotiation packets
5194 *
5195 * Returns 0 on success else non-zero
5196 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005197static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
5198 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005199{
5200 char buf[256];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005201 int ret = 0, i = 0, j;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005202 uint32_t version;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005203 const socklen_t addrlen = get_addr_len(addr);
5204
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005205 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005206 /*
5207 * header form
5208 * long header, fixed bit to 0 for Version Negotiation
5209 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005210 /* TODO: RAND_bytes() should be replaced? */
5211 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
5212 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
5213 goto out;
5214 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005215
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01005216 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005217 /* null version for Version Negotiation */
5218 buf[i++] = '\x00';
5219 buf[i++] = '\x00';
5220 buf[i++] = '\x00';
5221 buf[i++] = '\x00';
5222
5223 /* source connection id */
5224 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005225 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005226 i += pkt->scid.len;
5227
5228 /* destination connection id */
5229 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005230 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005231 i += pkt->dcid.len;
5232
5233 /* supported version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005234 for (j = 0; j < quic_versions_nb; j++) {
5235 version = htonl(quic_versions[j].num);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005236 memcpy(&buf[i], &version, sizeof(version));
5237 i += sizeof(version);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005238 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005239
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005240 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005241 goto out;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005242
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005243 ret = 1;
5244 out:
5245 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5246 return !ret;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005247}
5248
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005249/* Send a stateless reset packet depending on <pkt> RX packet information
5250 * from <fd> UDP socket to <dst>
5251 * Return 1 if succeeded, 0 if not.
5252 */
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005253static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005254 struct quic_rx_packet *rxpkt)
5255{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005256 int ret = 0, pktlen, rndlen;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005257 unsigned char pkt[64];
5258 const socklen_t addrlen = get_addr_len(dstaddr);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005259 struct proxy *prx;
5260 struct quic_counters *prx_counters;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005261
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005262 TRACE_ENTER(QUIC_EV_STATELESS_RST);
5263
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005264 prx = l->bind_conf->frontend;
5265 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005266 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
5267 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
5268 * Reset is difficult to distinguish from other packets if the recipient requires
5269 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
5270 * that all packets it sends are at least 22 bytes longer than the minimum
5271 * connection ID length that it requests the peer to include in its packets,
5272 * adding PADDING frames as necessary. This ensures that any Stateless Reset
5273 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
5274 * An endpoint that sends a Stateless Reset in response to a packet that is
5275 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
5276 * than the packet it responds to.
5277 */
5278
5279 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
5280 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
5281 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
5282 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005283
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005284 /* Put a header of random bytes */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005285 /* TODO: RAND_bytes() should be replaced */
5286 if (RAND_bytes(pkt, rndlen) != 1) {
5287 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
5288 goto leave;
5289 }
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005290
5291 /* Clear the most significant bit, and set the second one */
5292 *pkt = (*pkt & ~0x80) | 0x40;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005293 if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005294 rxpkt->dcid.data, rxpkt->dcid.len))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005295 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005296
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005297 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005298 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005299
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005300 ret = 1;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005301 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005302 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005303 leave:
5304 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
5305 return ret;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005306}
5307
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005308/* QUIC server only function.
5309 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005310 * This is the responsibility of the caller to check <aad> size is big enough
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005311 * to contain these data.
5312 * Return the number of bytes copied to <aad>.
5313 */
5314static int quic_generate_retry_token_aad(unsigned char *aad,
5315 uint32_t version,
5316 const struct quic_cid *cid,
5317 const struct sockaddr_storage *addr)
5318{
5319 unsigned char *p;
5320
5321 p = aad;
5322 memcpy(p, &version, sizeof version);
5323 p += sizeof version;
5324 p += quic_saddr_cpy(p, addr);
5325 memcpy(p, cid->data, cid->len);
5326 p += cid->len;
5327
5328 return p - aad;
5329}
5330
5331/* QUIC server only function.
5332 * Generate the token to be used in Retry packets. The token is written to
5333 * <buf> whith <len> as length. <odcid> is the original destination connection
5334 * ID and <dcid> is our side destination connection ID (or client source
5335 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005336 * Returns the length of the encoded token or 0 on error.
5337 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005338static int quic_generate_retry_token(unsigned char *buf, size_t len,
5339 const uint32_t version,
5340 const struct quic_cid *odcid,
5341 const struct quic_cid *dcid,
5342 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005343{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005344 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005345 unsigned char *p;
5346 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5347 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5348 size_t aadlen;
5349 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
5350 unsigned char key[QUIC_TLS_KEY_LEN];
5351 unsigned char iv[QUIC_TLS_IV_LEN];
5352 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5353 size_t seclen = strlen(global.cluster_secret);
5354 EVP_CIPHER_CTX *ctx = NULL;
5355 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5356 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005357
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005358 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5359
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005360 /* We copy the odcid into the token, prefixed by its one byte
5361 * length, the format token byte. It is followed by an AEAD TAG, and finally
5362 * the random bytes used to derive the secret to encrypt the token.
5363 */
5364 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005365 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005366
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005367 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005368 /* TODO: RAND_bytes() should be replaced */
5369 if (RAND_bytes(salt, sizeof salt) != 1) {
5370 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005371 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005372 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005373
5374 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005375 salt, sizeof salt, sec, seclen)) {
5376 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005377 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005378 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005379
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005380 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
5381 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005382 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005383 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005384
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005385 /* Token build */
5386 p = buf;
5387 *p++ = QUIC_TOKEN_FMT_RETRY,
5388 *p++ = odcid->len;
5389 memcpy(p, odcid->data, odcid->len);
5390 p += odcid->len;
5391 write_u32(p, htonl(timestamp));
5392 p += sizeof timestamp;
5393
5394 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005395 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
5396 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005397 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005398 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005399
5400 p += QUIC_TLS_TAG_LEN;
5401 memcpy(p, salt, sizeof salt);
5402 p += sizeof salt;
5403 EVP_CIPHER_CTX_free(ctx);
5404
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005405 ret = p - buf;
5406 leave:
5407 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5408 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005409
5410 err:
5411 if (ctx)
5412 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005413 goto leave;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005414}
5415
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005416/* QUIC server only function.
5417 * Check the validity of the Retry token from <token> buffer with <tokenlen>
5418 * as length. If valid, the ODCID of <qc> QUIC connection will be put
5419 * into <odcid> connection ID. <dcid> is our side destination connection ID
5420 * of client source connection ID.
5421 * Return 1 if succeeded, 0 if not.
5422 */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005423static int quic_retry_token_check(const unsigned char *token, size_t tokenlen,
5424 const struct quic_version *qv,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005425 struct quic_cid *odcid,
5426 const struct quic_cid *dcid,
5427 struct quic_conn *qc,
5428 struct sockaddr_storage *addr)
5429{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005430 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005431 unsigned char buf[128];
5432 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5433 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5434 size_t aadlen;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005435 const unsigned char *salt;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005436 unsigned char key[QUIC_TLS_KEY_LEN];
5437 unsigned char iv[QUIC_TLS_IV_LEN];
5438 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5439 size_t seclen = strlen(global.cluster_secret);
5440 EVP_CIPHER_CTX *ctx = NULL;
5441 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5442
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005443 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5444
5445 if (sizeof buf < tokenlen) {
5446 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
5447 goto err;
5448 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005449
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005450 aadlen = quic_generate_retry_token_aad(aad, qv->num, dcid, addr);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005451 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
5452 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005453 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005454 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
5455 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005456 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005457
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005458 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
5459 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005460 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005461 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005462
5463 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
5464 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005465 ctx, aead, key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005466 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005467 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005468 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005469
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005470 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
5471 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005472 goto err;
5473 }
5474
5475 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005476
5477 ret = 1;
5478 leave:
5479 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5480 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005481
5482 err:
5483 if (ctx)
5484 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005485 goto leave;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005486}
5487
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005488/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
5489 * the Initial <pkt> packet.
5490 *
5491 * Returns 0 on success else non-zero.
5492 */
5493static int send_retry(int fd, struct sockaddr_storage *addr,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005494 struct quic_rx_packet *pkt, const struct quic_version *qv)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005495{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005496 int ret = 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005497 unsigned char buf[128];
5498 int i = 0, token_len;
5499 const socklen_t addrlen = get_addr_len(addr);
5500 struct quic_cid scid;
5501
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005502 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5503
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005504 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
5505 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
5506 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005507 /* version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005508 buf[i++] = *((unsigned char *)&qv->num + 3);
5509 buf[i++] = *((unsigned char *)&qv->num + 2);
5510 buf[i++] = *((unsigned char *)&qv->num + 1);
5511 buf[i++] = *(unsigned char *)&qv->num;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005512
5513 /* Use the SCID from <pkt> for Retry DCID. */
5514 buf[i++] = pkt->scid.len;
5515 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
5516 i += pkt->scid.len;
5517
5518 /* Generate a new CID to be used as SCID for the Retry packet. */
5519 scid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005520 /* TODO: RAND_bytes() should be replaced */
5521 if (RAND_bytes(scid.data, scid.len) != 1) {
5522 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
5523 goto out;
5524 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005525
5526 buf[i++] = scid.len;
5527 memcpy(&buf[i], scid.data, scid.len);
5528 i += scid.len;
5529
5530 /* token */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005531 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005532 &pkt->dcid, &pkt->scid, addr))) {
5533 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
5534 goto out;
5535 }
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01005536
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005537 i += token_len;
5538
5539 /* token integrity tag */
5540 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
5541 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005542 pkt->dcid.len, buf, i, qv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005543 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5544 goto out;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005545 }
5546
5547 i += QUIC_TLS_TAG_LEN;
5548
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005549 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
5550 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5551 goto out;
5552 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005553
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005554 ret = 1;
5555 out:
5556 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5557 return !ret;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005558}
5559
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005560/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5561 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5562 * concatenated to the <pkt> DCID field.
5563 *
5564 * Returns the instance or NULL if not found.
5565 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005566static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005567 struct listener *l,
5568 struct sockaddr_storage *saddr)
5569{
5570 struct quic_conn *qc = NULL;
5571 struct ebmb_node *node;
5572 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005573 /* set if the quic_conn is found in the second DCID tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005574
5575 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005576
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005577 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5578 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5579 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5580 /* DCIDs of first packets coming from multiple clients may have
5581 * the same values. Let's distinguish them by concatenating the
5582 * socket addresses.
5583 */
5584 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005585 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005586 pkt->dcid.len + pkt->dcid.addrlen);
5587 if (node) {
5588 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5589 goto end;
5590 }
5591 }
5592
5593 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5594 * also for INITIAL/0-RTT non-first packets with the final DCID in
5595 * used.
5596 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005597 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005598 if (!node)
5599 goto end;
5600
5601 id = ebmb_entry(node, struct quic_connection_id, node);
5602 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005603
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005604 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5605 * If already done, this is a noop.
5606 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005607 if (qc)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005608 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005609
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005610 end:
Frédéric Lécaille614742b2022-09-07 12:15:43 +02005611 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005612 return qc;
5613}
5614
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005615/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5616 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5617 * parameters of this session.
5618 * This is the responsibility of the caller to check the validity of all the
5619 * pointers passed as parameter to this function.
5620 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5621 * CO_ER_SSL_NO_MEM.
5622 */
5623static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5624 unsigned char *params, size_t params_len)
5625{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005626 int retry, ret = -1;
5627
5628 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005629
5630 retry = 1;
5631 retry:
5632 *ssl = SSL_new(ssl_ctx);
5633 if (!*ssl) {
5634 if (!retry--)
5635 goto err;
5636
5637 pool_gc(NULL);
5638 goto retry;
5639 }
5640
5641 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005642 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005643 SSL_free(*ssl);
5644 *ssl = NULL;
5645 if (!retry--)
5646 goto err;
5647
5648 pool_gc(NULL);
5649 goto retry;
5650 }
5651
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005652 ret = 0;
5653 leave:
5654 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5655 return ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005656
5657 err:
5658 qc->conn->err_code = CO_ER_SSL_NO_MEM;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005659 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005660}
5661
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005662/* Finalize <qc> QUIC connection:
5663 * - initialize the Initial QUIC TLS context for negotiated version,
5664 * - derive the secrets for this context,
5665 * - encode the transport parameters to be sent,
5666 * - set them into the TLS stack,
5667 * - initialize ->max_ack_delay and max_idle_timeout,
5668 *
5669 * MUST be called after having received the remote transport parameters.
5670 * Return 1 if succeeded, 0 if not.
5671 */
5672int qc_conn_finalize(struct quic_conn *qc, int server)
5673{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005674 int ret = 0;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005675 struct quic_transport_params *tx_tp = &qc->tx.params;
5676 struct quic_transport_params *rx_tp = &qc->rx.params;
5677 const struct quic_version *ver;
5678
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005679 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5680
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005681 if (tx_tp->version_information.negotiated_version &&
5682 tx_tp->version_information.negotiated_version != qc->original_version) {
5683 qc->negotiated_version =
5684 qc->tx.params.version_information.negotiated_version;
5685 if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
5686 qc->odcid.data, qc->odcid.len, !server))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005687 goto out;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005688
5689 ver = qc->negotiated_version;
5690 }
5691 else {
5692 ver = qc->original_version;
5693 }
5694
5695 qc->enc_params_len =
5696 quic_transport_params_encode(qc->enc_params,
5697 qc->enc_params + sizeof qc->enc_params,
5698 &qc->rx.params, ver, 1);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005699 if (!qc->enc_params_len) {
5700 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_TXPKT);
5701 goto out;
5702 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005703
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005704 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
5705 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_TXPKT);
5706 goto out;
5707 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005708
5709 if (tx_tp->max_ack_delay)
5710 qc->max_ack_delay = tx_tp->max_ack_delay;
5711
5712 if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout)
5713 qc->max_idle_timeout =
5714 QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5715 else
5716 qc->max_idle_timeout =
5717 QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5718
5719 TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp);
5720
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005721 ret = 1;
5722 out:
5723 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5724 return ret;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005725}
5726
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005727/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5728 * used to process <qc> received packets. The allocated context is stored in
5729 * <qc.xprt_ctx>.
5730 *
5731 * Returns 0 on success else non-zero.
5732 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005733static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005734{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005735 int ret = 0;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005736 struct bind_conf *bc = qc->li->bind_conf;
5737 struct ssl_sock_ctx *ctx = NULL;
5738
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005739 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5740
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005741 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005742 if (!ctx) {
5743 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005744 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005745 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005746
5747 ctx->wait_event.tasklet = tasklet_new();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005748 if (!ctx->wait_event.tasklet) {
5749 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005750 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005751 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005752
5753 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5754 ctx->wait_event.tasklet->context = ctx;
5755 ctx->wait_event.events = 0;
5756 ctx->subs = NULL;
5757 ctx->xprt_ctx = NULL;
5758 ctx->qc = qc;
5759
5760 /* Set tasklet tid based on the SCID selected by us for this
5761 * connection. The upper layer will also be binded on the same thread.
5762 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005763 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005764
5765 if (qc_is_listener(qc)) {
5766 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5767 qc->enc_params, qc->enc_params_len) == -1) {
5768 goto err;
5769 }
William Lallemande6ec6262022-09-02 16:24:39 +02005770#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005771 /* Enabling 0-RTT */
5772 if (bc->ssl_conf.early_data)
5773 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
William Lallemande6ec6262022-09-02 16:24:39 +02005774#endif
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005775
5776 SSL_set_accept_state(ctx->ssl);
5777 }
5778
5779 ctx->xprt = xprt_get(XPRT_QUIC);
5780
5781 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005782 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005783
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005784 ret = 1;
5785 leave:
5786 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5787 return !ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005788
5789 err:
5790 if (ctx && ctx->wait_event.tasklet)
5791 tasklet_free(ctx->wait_event.tasklet);
5792 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005793 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005794}
5795
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005796/* Check that all the bytes between <buf> included and <end> address
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005797 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005798 * check that there is at least one byte between <buf> end <end>.
5799 * Return 1 if this all the bytes are null, 0 if not.
5800 */
5801static inline int quic_padding_check(const unsigned char *buf,
5802 const unsigned char *end)
5803{
5804 while (buf < end && !*buf)
5805 buf++;
5806
5807 return buf == end;
5808}
5809
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005810/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5811 * end of this buffer past one byte and populate <pkt> RX packet structure
5812 * with the information collected from the packet.
5813 * This function sets ->len <pkt> field value to the end of the packet past one
5814 * byte to enable the caller to run this function again to continue to parse
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005815 * the remaining QUIC packets carried by the datagram.
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005816 * Note that this function always sets this ->len value. If a paquet could
5817 * not be correctly found, ->len value will be set to the remaining number
5818 * bytes in the datagram to entirely consume this latter.
5819 */
5820static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5821 struct quic_rx_packet *pkt, int first_pkt,
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02005822 struct quic_dgram *dgram, struct list **tasklist_head)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005823{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005824 unsigned char *beg, *payload;
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02005825 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005826 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005827 struct proxy *prx;
5828 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005829 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005830 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005831 size_t b_cspace;
5832 struct quic_enc_level *qel;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005833 uint32_t version;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005834 const struct quic_version *qv = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005835
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005836 TRACE_ENTER(QUIC_EV_CONN_LPKT);
5837
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005838 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005839 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005840 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005841 qel = NULL;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005842 l = dgram->owner;
5843 prx = l->bind_conf->frontend;
5844 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005845 /* This ist only to please to traces and distinguish the
5846 * packet with parsed packet number from others.
5847 */
5848 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005849 if (end <= buf) {
5850 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5851 goto drop;
5852 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005853
5854 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005855 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005856 if (!first_pkt && quic_padding_check(buf, end)) {
5857 /* Some browsers may pad the remaining datagram space with null bytes.
5858 * That is what we called add padding out of QUIC packets. Such
5859 * datagrams must be considered as valid. But we can only consume
5860 * the remaining space.
5861 */
5862 pkt->len = end - buf;
5863 goto drop_no_conn;
5864 }
5865
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005866 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005867 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005868 }
5869
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005870 /* Header form */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005871 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005872 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5873 goto drop;
5874 }
5875
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005876 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005877 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005878 struct quic_cid odcid;
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02005879 const struct quic_cid *token_odcid = NULL; // ODCID received from client token
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005880
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005881 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005882 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005883 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005884 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005885 }
5886
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005887 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5888 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005889 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005890 }
5891 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5892 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005893 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02005894 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
Frédéric Lécaillee7df68a2022-08-05 09:34:44 +02005895 goto drop;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005896 }
5897
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005898 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5899 * they must have the same DCID.
5900 */
5901 if (!first_pkt &&
5902 (pkt->dcid.len != dgram->dcid_len ||
5903 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5904 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005905 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005906 }
5907
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005908 /* Retry of Version Negotiation packets are only sent by servers */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005909 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005910 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005911 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005912 }
5913
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005914 /* RFC9000 6. Version Negotiation */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005915 qv = qc_supported_version(version);
5916 if (!qv) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005917 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005918 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005919 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005920 goto err;
5921 }
5922
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005923 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005924 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005925 }
5926
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005927 /* For Initial packets, and for servers (QUIC clients connections),
5928 * there is no Initial connection IDs storage.
5929 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005930 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005931 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005932
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005933 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5934 end - buf < token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005935 TRACE_PROTO("Packet dropped",
5936 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005937 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005938 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005939
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005940 /* TODO Retry should be automatically activated if
5941 * suspect network usage is detected.
5942 */
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005943 if (global.cluster_secret && !token_len) {
5944 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
5945 TRACE_PROTO("Initial without token, sending retry",
5946 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
5947 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
5948 TRACE_PROTO("Error during Retry generation",
5949 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005950 goto err;
5951 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005952
5953 HA_ATOMIC_INC(&prx_counters->retry_sent);
5954 goto err;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005955 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005956 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005957 else if (!global.cluster_secret && token_len) {
5958 /* Impossible case: a token was received without configured
5959 * cluster secret.
5960 */
5961 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
5962 NULL, NULL, NULL, qv);
5963 goto drop;
5964 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005965
5966 pkt->token = buf;
5967 pkt->token_len = token_len;
5968 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005969 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005970 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005971 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005972 TRACE_PROTO("Packet dropped",
5973 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005974 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005975 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005976 }
5977
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005978 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5979 end - buf < len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005980 TRACE_PROTO("Packet dropped",
5981 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005982 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005983 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005984
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005985 payload = buf;
5986 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005987 if (drop_no_conn)
5988 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005989
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005990 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005991 if (global.cluster_secret && pkt->token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005992 if (*pkt->token == QUIC_TOKEN_FMT_RETRY) {
5993 const struct quic_version *ver = qc ? qc->original_version : qv;
5994 if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid,
5995 &pkt->scid, qc, &dgram->saddr)) {
5996 HA_ATOMIC_INC(&prx_counters->retry_error);
5997 TRACE_PROTO("Wrong retry token",
5998 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5999 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
6000 * with an INVALID_TOKEN error.
6001 */
6002 goto drop;
6003 }
6004
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02006005 token_odcid = &odcid;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006006 HA_ATOMIC_INC(&prx_counters->retry_validated);
6007 }
6008 else {
6009 /* TODO: New token check */
6010 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
6011 goto drop;
6012 }
6013 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01006014 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006015 int ipv4;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006016
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006017 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006018 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006019 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006020 }
6021
Willy Tarreau787e92a2022-05-20 16:06:01 +02006022 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006023 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006024 TRACE_PROTO("Initial without token, sending retry",
6025 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02006026 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille59507de2022-08-11 12:14:07 +02006027 TRACE_ERROR("Error during Retry generation",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006028 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02006029 goto err;
6030 }
6031
6032 HA_ATOMIC_INC(&prx_counters->retry_sent);
6033 goto err;
6034 }
6035
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02006036 /* RFC 9000 7.2. Negotiating Connection IDs:
6037 * When an Initial packet is sent by a client that has not previously
6038 * received an Initial or Retry packet from the server, the client
6039 * populates the Destination Connection ID field with an unpredictable
6040 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6041 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01006042 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006043 TRACE_PROTO("dropped packet",
6044 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006045 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01006046 }
6047
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006048 pkt->saddr = dgram->saddr;
6049 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006050
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02006051 qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, token_odcid,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02006052 &pkt->saddr, 1, !!pkt->token_len, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02006053 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006054 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006055
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006056 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02006057 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02006058 ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
6059 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006060 }
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02006061
6062 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006063 }
6064 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006065 TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006066 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006067 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006068 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006069 }
6070
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006071 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006072 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006073
6074 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6075 * they must have the same DCID.
6076 */
6077 if (!first_pkt &&
6078 (pkt->dcid.len != dgram->dcid_len ||
6079 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
6080 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006081 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006082 }
6083
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006084 buf += QUIC_HAP_CID_LEN;
6085
6086 /* A short packet is the last one of a UDP datagram. */
6087 payload = buf;
6088 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006089
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006090 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006091 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006092 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006093 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006094 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006095 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006096 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006097 }
6098
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006099 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006100 }
6101
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006102 if (qc->flags & QUIC_FL_CONN_CLOSING) {
6103 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
6104 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
6105 qc->nb_pkt_for_cc++;
6106 qc->nb_pkt_since_cc = 0;
6107 }
6108 /* Skip the entire datagram */
6109 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006110 TRACE_STATE("Closing state connection",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006111 QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006112 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006113 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006114
6115 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6116 * they must have the same DCID.
6117 *
6118 * This check must be done after the final update to pkt.len to
6119 * properly drop the packet on failure.
6120 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006121 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006122 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006123 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006124 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006125 /* Reset the anti-amplification bit. It will be set again
6126 * when sending the next packet if reached again.
6127 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006128 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
6129 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006130 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006131 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006132
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006133 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006134
Amaury Denoyelle72d86502022-07-13 15:08:23 +02006135 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006136 TRACE_PROTO("Connection error",
6137 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006138 goto out;
6139 }
6140
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006141 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006142 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006143 b_cspace = b_contig_space(&qc->rx.buf);
6144 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006145 /* Do not consume buf if space not at the end. */
6146 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006147 TRACE_PROTO("Packet dropped",
6148 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006149 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6150 goto drop_no_conn;
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006151 }
6152
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006153 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006154 if (b_cspace) {
6155 b_putchr(&qc->rx.buf, 0x00);
6156 b_cspace--;
6157 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006158 b_add(&qc->rx.buf, b_cspace);
6159 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006160 TRACE_PROTO("Too big packet",
6161 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006162 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6163 goto drop_no_conn;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006164 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006165 }
6166
Frédéric Lécaille99897d12022-08-08 10:28:07 +02006167 if (!qc_try_rm_hp(qc, pkt, payload, beg, &qel)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006168 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006169 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02006170 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006171
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006172 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006173 if (pkt->aad_len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006174 qc_pkt_insert(qc, pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006175 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02006176 /* Wake up the connection packet handler task from here only if all
6177 * the contexts have been initialized, especially the mux context
6178 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
6179 * will start it if these contexts for the connection are not already
6180 * initialized.
6181 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006182 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006183 if (conn_ctx)
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02006184 *tasklist_head = tasklet_wakeup_after(*tasklist_head, conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02006185
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006186 drop_no_conn:
6187 if (drop_no_conn)
6188 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006189 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006190
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006191 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006192
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006193 drop:
6194 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006195 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006196 /* Wakeup the I/O handler callback if the PTO timer must be armed.
6197 * This cannot be done by this thread.
6198 */
6199 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006200 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006201 if (conn_ctx && conn_ctx->wait_event.tasklet)
6202 tasklet_wakeup(conn_ctx->wait_event.tasklet);
6203 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01006204 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006205 if (!pkt->len)
6206 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006207 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006208}
6209
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006210/* This function builds into <buf> buffer a QUIC long packet header.
6211 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006212 */
6213static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006214 int type, size_t pn_len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006215 struct quic_conn *qc, const struct quic_version *ver)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006216{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006217 int ret = 0;
6218
6219 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6220
6221 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
6222 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6223 goto leave;
6224 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006225
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006226 type = quic_pkt_type(type, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006227 /* #0 byte flags */
6228 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
6229 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
6230 /* Version */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006231 quic_write_uint32(buf, end, ver->num);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006232 *(*buf)++ = qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006233 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006234 if (qc->dcid.len) {
6235 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6236 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006237 }
6238 /* Source connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006239 *(*buf)++ = qc->scid.len;
6240 if (qc->scid.len) {
6241 memcpy(*buf, qc->scid.data, qc->scid.len);
6242 *buf += qc->scid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006243 }
6244
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006245 ret = 1;
6246 leave:
6247 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6248 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006249}
6250
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006251/* This function builds into <buf> buffer a QUIC short packet header.
6252 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006253 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006254static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006255 size_t pn_len, struct quic_conn *qc,
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006256 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006257{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006258 int ret = 0;
6259
6260 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6261
6262 if (end - *buf < 1 + qc->dcid.len) {
6263 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6264 goto leave;
6265 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006266
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006267 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01006268 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
6269 ((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 +01006270 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006271 if (qc->dcid.len) {
6272 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6273 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006274 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006275
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006276 ret = 1;
6277 leave:
6278 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6279 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006280}
6281
6282/* Apply QUIC header protection to the packet with <buf> as first byte address,
6283 * <pn> as address of the Packet number field, <pnlen> being this field length
6284 * with <aead> as AEAD cipher and <key> as secret key.
6285 * Returns 1 if succeeded or 0 if failed.
6286 */
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006287static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf,
6288 unsigned char *pn, size_t pnlen,
6289 struct quic_tls_ctx *tls_ctx)
6290
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006291{
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006292 int i, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006293 /* We need an IV of at least 5 bytes: one byte for bytes #0
6294 * and at most 4 bytes for the packet number
6295 */
6296 unsigned char mask[5] = {0};
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006297 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006298
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006299 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6300
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006301 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
6302 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006303 goto out;
6304 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006305
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006306 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
6307 for (i = 0; i < pnlen; i++)
6308 pn[i] ^= mask[i + 1];
6309
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006310 ret = 1;
6311 out:
6312 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006313 return ret;
6314}
6315
6316/* Reduce the encoded size of <ack_frm> ACK frame removing the last
6317 * ACK ranges if needed to a value below <limit> in bytes.
6318 * Return 1 if succeeded, 0 if not.
6319 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006320static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
6321 struct quic_frame *ack_frm, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006322{
6323 size_t room, ack_delay_sz;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006324 int ret = 0;
6325
6326 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006327
6328 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
6329 /* A frame is made of 1 byte for the frame type. */
6330 room = limit - ack_delay_sz - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006331 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
6332 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006333
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006334 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
6335 leave:
6336 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6337 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006338}
6339
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006340/* Prepare into <outlist> as most as possible ack-eliciting frame from their
6341 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
6342 * with <room> as available room, and <*len> the packet Length field initialized
6343 * with the number of bytes already present in this buffer which must be taken
6344 * into an account for the Length packet field value. <headlen> is the number of
6345 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006346 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006347 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006348 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006349 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006350 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006351static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006352 size_t room, size_t *len, size_t headlen,
6353 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006354 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006355{
Frédéric Lécailleea604992020-12-24 13:01:37 +01006356 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006357 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006358
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006359 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
6360
Frédéric Lécailleea604992020-12-24 13:01:37 +01006361 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006362 if (*len > room)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006363 goto leave;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006364
Frédéric Lécailleea604992020-12-24 13:01:37 +01006365 /* If we are not probing we must take into an account the congestion
6366 * control window.
6367 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006368 if (!qel->pktns->tx.pto_probe) {
6369 size_t remain = quic_path_prep_data(qc->path);
6370
6371 if (headlen > remain)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006372 goto leave;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006373
6374 room = QUIC_MIN(room, remain - headlen);
6375 }
6376
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006377 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006378 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006379
6380 /* NOTE: switch/case block inside a loop, a successful status must be
6381 * returned by this function only if at least one frame could be built
6382 * in the switch/case block.
6383 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006384 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006385 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006386 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006387
Frédéric Lécailleea604992020-12-24 13:01:37 +01006388 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006389 break;
6390
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006391 switch (cf->type) {
6392 case QUIC_FT_CRYPTO:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006393 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006394 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006395 /* Compute the length of this CRYPTO frame header */
6396 hlen = 1 + quic_int_getsize(cf->crypto.offset);
6397 /* Compute the data length of this CRyPTO frame. */
6398 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006399 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006400 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006401 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006402 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006403
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006404 /* CRYPTO frame length. */
6405 flen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006406 TRACE_DEVEL(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006407 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006408 /* Add the CRYPTO data length and its encoded length to the packet
6409 * length and the length of this length.
6410 */
6411 *len += flen;
6412 room -= flen;
6413 if (dlen == cf->crypto.len) {
6414 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006415 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006416 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006417 }
6418 else {
6419 struct quic_frame *new_cf;
6420
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006421 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006422 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006423 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006424 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006425 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006426
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006427 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006428 new_cf->type = QUIC_FT_CRYPTO;
6429 new_cf->crypto.len = dlen;
6430 new_cf->crypto.offset = cf->crypto.offset;
6431 new_cf->crypto.qel = qel;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006432 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6433 if (cf->origin) {
6434 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6435 /* This <cf> frame was duplicated */
6436 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6437 new_cf->origin = cf->origin;
6438 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006439 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006440 /* Consume <dlen> bytes of the current frame. */
6441 cf->crypto.len -= dlen;
6442 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006443 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006444 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006445
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006446 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006447 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
6448 struct eb64_node *node = NULL;
6449 struct qc_stream_desc *stream_desc = NULL;
6450 struct quic_stream *strm = &cf->stream;
6451
6452 /* As this frame has been already lost, ensure the stream is always
6453 * available or the range of this frame is not consumed before
6454 * resending it.
6455 */
6456 node = eb64_lookup(&qc->streams_by_id, strm->id);
6457 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006458 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006459 LIST_DELETE(&cf->list);
6460 pool_free(pool_head_quic_frame, cf);
6461 continue;
6462 }
6463
6464 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
6465 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006466 TRACE_DEVEL("ignored frame frame in already acked range",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006467 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006468 LIST_DELETE(&cf->list);
6469 pool_free(pool_head_quic_frame, cf);
6470 continue;
6471 }
6472 else if (strm->offset.key < stream_desc->ack_offset) {
6473 strm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006474 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006475 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006476 }
6477 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006478 /* Note that these frames are accepted in short packets only without
6479 * "Length" packet field. Here, <*len> is used only to compute the
6480 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01006481 *
6482 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006483 * excepting the variable ones. Note that +1 is for the type of this frame.
6484 */
6485 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006486 ((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 +02006487 /* Compute the data length of this STREAM frame. */
6488 avail_room = room - hlen - *len;
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006489 if ((ssize_t)avail_room <= 0)
6490 continue;
6491
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006492 TRACE_DEVEL(" New STREAM frame build (room, len)",
Frédéric Lécailleadc76412022-08-08 16:09:46 +02006493 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006494 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
6495 dlen = max_available_room(avail_room, &dlen_sz);
6496 if (dlen > cf->stream.len) {
6497 dlen = cf->stream.len;
6498 }
6499 dlen_sz = quic_int_getsize(dlen);
6500 flen = hlen + dlen_sz + dlen;
6501 }
6502 else {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02006503 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006504 flen = hlen + dlen;
6505 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006506 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006507 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006508 TRACE_DEVEL(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006509 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006510 /* Add the STREAM data length and its encoded length to the packet
6511 * length and the length of this length.
6512 */
6513 *len += flen;
6514 room -= flen;
6515 if (dlen == cf->stream.len) {
6516 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006517 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006518 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006519
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006520 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006521 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006522 qcc_streams_sent_done(cf->stream.stream->ctx,
6523 cf->stream.len,
6524 cf->stream.offset.key);
6525 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006526 }
6527 else {
6528 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006529 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006530
6531 new_cf = pool_zalloc(pool_head_quic_frame);
6532 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006533 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006534 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006535 }
6536
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006537 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006538 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006539 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006540 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006541 new_cf->stream.id = cf->stream.id;
6542 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
6543 new_cf->stream.offset = cf->stream.offset;
6544 new_cf->stream.len = dlen;
6545 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
6546 /* FIN bit reset */
6547 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
6548 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006549 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6550 if (cf->origin) {
6551 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6552 /* This <cf> frame was duplicated */
6553 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6554 new_cf->origin = cf->origin;
6555 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006556 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006557 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
6558 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006559 cf_buf = b_make(b_orig(cf->stream.buf),
6560 b_size(cf->stream.buf),
6561 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006562 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006563 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006564 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006565
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006566 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006567 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006568 qcc_streams_sent_done(new_cf->stream.stream->ctx,
6569 new_cf->stream.len,
6570 new_cf->stream.offset.key);
6571 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006572 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006573
6574 /* TODO the MUX is notified about the frame sending via
6575 * previous qcc_streams_sent_done call. However, the
6576 * sending can fail later, for example if the sendto
6577 * system call returns an error. As the MUX has been
6578 * notified, the transport layer is responsible to
6579 * bufferize and resent the announced data later.
6580 */
6581
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006582 break;
6583
6584 default:
6585 flen = qc_frm_len(cf);
6586 BUG_ON(!flen);
6587 if (flen > room)
6588 continue;
6589
6590 *len += flen;
6591 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006592 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006593 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006594 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006595 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006596
6597 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01006598 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006599 }
6600
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006601 leave:
6602 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécailleea604992020-12-24 13:01:37 +01006603 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006604}
6605
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006606/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
6607 * is used as return parameter and should be zero'ed by the caller.
6608 */
6609static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
6610 struct quic_frame *out)
6611{
6612 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
6613 *
6614 * A CONNECTION_CLOSE frame should be sent in several packets with
6615 * different encryption levels depending on the client context. This is
6616 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
6617 * more details on how to implement it.
6618 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006619 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
6620
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006621
6622 if (qc->err.app) {
6623 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
6624 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
6625 /* RFC 9000 10.2.3. Immediate Close during the Handshake
6626 *
6627 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
6628 * packet could expose application state or be used to alter application
6629 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
6630 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
6631 * Handshake packets. Otherwise, information about the application
6632 * state might be revealed. Endpoints MUST clear the value of the
6633 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
6634 * converting to a CONNECTION_CLOSE of type 0x1c.
6635 */
6636 out->type = QUIC_FT_CONNECTION_CLOSE;
6637 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
6638 out->connection_close.reason_phrase_len = 0;
6639 }
6640 else {
6641 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
6642 out->connection_close.error_code = qc->err.code;
6643 }
6644 }
6645 else {
6646 out->type = QUIC_FT_CONNECTION_CLOSE;
6647 out->connection_close.error_code = qc->err.code;
6648 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006649 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
6650
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006651}
6652
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006653/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006654 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
6655 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006656 * filling the buffer with as much frames as possible from <frms> list of
6657 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006658 * 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 +02006659 * 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 +02006660 * having returned from this function.
6661 * 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 +01006662 * number field in this packet. <pn_len> will also have the packet number
6663 * length as value.
6664 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006665 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006666 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006667static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
6668 size_t dglen, struct quic_tx_packet *pkt,
6669 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006670 int force_ack, int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006671 struct quic_enc_level *qel, struct quic_conn *qc,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006672 const struct quic_version *ver, struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006673{
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006674 unsigned char *beg, *payload;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006675 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006676 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
6677 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006678 struct quic_frame cc_frm = { };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006679 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006680 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006681 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006682 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006683 struct quic_frame *cf;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006684 int must_ack, ret = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006685 int nb_aepkts_since_last_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006686
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006687 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6688
Frédéric Lécailleea604992020-12-24 13:01:37 +01006689 /* Length field value with CRYPTO frames if present. */
6690 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006691 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006692 /* When not probing, and no immediate close is required, reduce the size of this
6693 * buffer to respect the congestion controller window.
6694 * 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 +01006695 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006696 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006697 size_t path_room;
6698
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006699 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006700 if (end - beg > path_room)
6701 end = beg + path_room;
6702 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006703
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006704 /* Ensure there is enough room for the TLS encryption tag and a zero token
6705 * length field if any.
6706 */
6707 if (end - pos < QUIC_TLS_TAG_LEN +
6708 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
6709 goto no_room;
6710
6711 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006712 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006713 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006714 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006715 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006716 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006717 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006718 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006719 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006720 goto no_room;
6721
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006722 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006723 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006724 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006725 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006726 /* Build an ACK frame if required. */
6727 ack_frm_len = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006728 nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
6729 must_ack = !qel->pktns->tx.pto_probe &&
6730 (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
6731 (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK)));
Frédéric Lécailleb866c692022-08-30 16:24:54 +02006732 if (must_ack) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006733 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006734 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006735 ack_frm.tx_ack.arngs = arngs;
6736 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6737 qel->pktns->tx.ack_delay =
6738 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6739 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6740 }
6741 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006742 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6743 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6744 * that from here, we do not know if we will have to send a PING frame.
6745 * This will be decided after having computed the ack-eliciting frames
6746 * to be added to this packet.
6747 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006748 ack_frm_len = quic_ack_frm_reduce_sz(qc, &ack_frm, end - 1 - *pn_len - pos);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006749 if (!ack_frm_len)
6750 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006751 }
6752
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006753 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006754 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006755 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006756 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006757 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006758
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006759 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006760 /* Initialize the length of the frames built below to <len>.
6761 * If any frame could be successfully built by qc_build_frms(),
6762 * we will have len_frms > len.
6763 */
6764 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006765 if (!qc_build_frms(&frm_list, frms,
6766 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006767 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006768 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006769 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6770 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006771 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006772 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006773
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006774 /* Length (of the remaining data). Must not fail because, the buffer size
6775 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6776 * for the encryption tag. It must be taken into an account for the length
6777 * of this packet.
6778 */
6779 if (len_frms)
6780 len = len_frms + QUIC_TLS_TAG_LEN;
6781 else
6782 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006783 /* CONNECTION_CLOSE frame */
6784 if (cc) {
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006785 qc_build_cc_frm(qc, qel, &cc_frm);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006786 len += qc_frm_len(&cc_frm);
6787 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006788 add_ping_frm = 0;
6789 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006790 len_sz = quic_int_getsize(len);
6791 /* Add this packet size to <dglen> */
6792 dglen += head_len + len_sz + len;
6793 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6794 /* This is a maximum padding size */
6795 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6796 /* The length field value is of this packet is <len> + <padding_len>
6797 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006798 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006799 * sizes from <padding_len>.
6800 */
6801 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6802 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006803 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006804 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006805 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006806 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006807 add_ping_frm = 1;
6808 len += 1;
6809 }
6810 /* 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 +01006811 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006812 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6813 }
6814
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006815 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6816 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006817
6818 /* Packet number field address. */
6819 *buf_pn = pos;
6820
6821 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006822 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6823 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006824
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006825 /* payload building (ack-eliciting or not frames) */
6826 payload = pos;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006827 if (ack_frm_len) {
6828 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6829 goto no_room;
6830
6831 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006832 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006833 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006834
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006835 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006836 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillea8a60432022-08-23 11:42:48 +02006837 struct quic_frame *tmp_cf;
6838 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02006839 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006840 ssize_t room = end - pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006841 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006842 qc, NULL, NULL, &room);
Frédéric Lécaille149c5312022-08-27 15:51:30 +02006843 /* Note that <cf> was added from <frms> to <frm_list> list by
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006844 * qc_build_frms().
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006845 */
6846 LIST_DELETE(&cf->list);
6847 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille149c5312022-08-27 15:51:30 +02006848 continue;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006849 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006850
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006851 quic_tx_packet_refinc(pkt);
6852 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006853 }
6854 }
6855
6856 /* Build a PING frame if needed. */
6857 if (add_ping_frm) {
6858 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006859 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006860 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006861 }
6862
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006863 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006864 if (cc) {
6865 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6866 goto no_room;
6867
6868 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6869 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006870
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006871 /* Build a PADDING frame if needed. */
6872 if (padding_len) {
6873 frm.type = QUIC_FT_PADDING;
6874 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006875 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006876 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006877 }
6878
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006879 if (pos == payload) {
6880 /* No payload was built because of congestion control */
6881 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
6882 goto no_room;
6883 }
6884
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006885 /* If this packet is ack-eliciting and we are probing let's
6886 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006887 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006888 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6889 qel->pktns->tx.pto_probe)
6890 qel->pktns->tx.pto_probe--;
6891
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006892 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006893 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006894
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006895 ret = 1;
6896 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
6897 leave:
6898 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6899 return ret;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006900
6901 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006902 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006903 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006904 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
6905 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006906}
6907
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006908static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006909{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006910 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006911 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006912 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006913 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006914 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006915 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006916 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006917 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006918 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006919 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006920}
6921
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006922/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006923 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6924 * of prebuilt frames.
6925 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006926 * Return -2 if the packet could not be allocated or encrypted for any reason,
6927 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006928 * XXX NOTE XXX
6929 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6930 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6931 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006932 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006933static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6934 const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006935 struct quic_enc_level *qel,
6936 struct quic_tls_ctx *tls_ctx, struct list *frms,
6937 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006938 size_t dglen, int pkt_type, int force_ack,
6939 int padding, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006940{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006941 struct quic_tx_packet *ret_pkt = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006942 /* The pointer to the packet number field. */
6943 unsigned char *buf_pn;
6944 unsigned char *beg, *end, *payload;
6945 int64_t pn;
6946 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006947 struct quic_tx_packet *pkt;
6948
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006949 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006950 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006951 pkt = pool_alloc(pool_head_quic_tx_packet);
6952 if (!pkt) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006953 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006954 *err = -2;
6955 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006956 }
6957
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006958 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006959 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006960 pn_len = 0;
6961 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006962
6963 pn = qel->pktns->tx.next_pn + 1;
6964 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006965 force_ack, padding, cc, probe, qel, qc, ver, frms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006966 // trace already emitted by function above
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006967 *err = -1;
6968 goto err;
6969 }
6970
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006971 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006972 payload = buf_pn + pn_len;
6973 payload_len = end - payload;
6974 aad_len = payload - beg;
6975
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006976 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006977 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006978 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006979 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006980 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006981
6982 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006983 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006984 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006985 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006986 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006987 goto err;
6988 }
6989
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006990 /* Consume a packet number */
6991 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006992 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006993 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006994 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006995 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
6996 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006997 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6998 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006999 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02007000 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007001 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01007002 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02007003 pkt->in_flight_len = pkt->len;
7004 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01007005 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01007006 /* Always reset this flags */
7007 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02007008 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
7009 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
7010 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
7011 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02007012
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007013 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007014
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007015 ret_pkt = pkt;
7016 leave:
7017 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt);
7018 return ret_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007019
7020 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01007021 /* TODO: what about the frames which have been built
7022 * for this packet.
7023 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007024 free_quic_tx_packet(qc, pkt);
7025 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007026}
7027
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007028
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007029/* Called from the upper layer, to subscribe <es> to events <event_type>. The
7030 * event subscriber <es> is not allowed to change from a previous call as long
7031 * as at least one event is still subscribed. The <event_type> must only be a
7032 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
7033 */
7034static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7035{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007036 struct quic_conn *qc = conn->handle.qc;
7037 struct qcc *qcc = qc->qcc;
7038
7039 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007040
7041 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
7042 BUG_ON(qcc->subs && qcc->subs != es);
7043
7044 es->events |= event_type;
7045 qcc->subs = es;
7046
7047 if (event_type & SUB_RETRY_RECV)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007048 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007049
7050 if (event_type & SUB_RETRY_SEND)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007051 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7052
7053 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007054
7055 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007056}
7057
7058/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
7059 * The <es> pointer is not allowed to differ from the one passed to the
7060 * subscribe() call. It always returns zero.
7061 */
7062static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7063{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007064 int ret;
7065 struct quic_conn *qc = conn->handle.qc;
7066 struct qcc *qcc = qc->qcc;
7067
7068 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
7069
7070 if (event_type & SUB_RETRY_RECV)
7071 TRACE_DEVEL("unsubscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
7072 if (event_type & SUB_RETRY_SEND)
7073 TRACE_DEVEL("unsubscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7074
7075 ret = conn_unsubscribe(conn, xprt_ctx, event_type, es);
7076
7077 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
7078
7079 return ret;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007080}
7081
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007082/* Store in <xprt_ctx> the context attached to <conn>.
7083 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007084 */
7085static int qc_conn_init(struct connection *conn, void **xprt_ctx)
7086{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01007087 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007088
7089 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
7090
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007091 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007092 if (*xprt_ctx)
7093 goto out;
7094
Willy Tarreau784b8682022-04-11 14:18:10 +02007095 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007096
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007097 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01007098 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007099
7100 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007101}
7102
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007103/* Start the QUIC transport layer */
7104static int qc_xprt_start(struct connection *conn, void *ctx)
7105{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007106 int ret = 0;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007107 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02007108 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007109
Willy Tarreau784b8682022-04-11 14:18:10 +02007110 qc = conn->handle.qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007111 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007112 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
7113 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02007114 /* prepare a CONNECTION_CLOSE frame */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02007115 quic_set_connection_close(qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007116 goto out;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007117 }
7118
7119 /* mux-quic can now be considered ready. */
7120 qc->mux_state = QC_MUX_READY;
7121
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007122 tasklet_wakeup(qctx->wait_event.tasklet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007123
7124 ret = 1;
7125 out:
7126 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
7127 return ret;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007128}
7129
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007130static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
7131{
Willy Tarreau784b8682022-04-11 14:18:10 +02007132 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007133 return NULL;
7134
Willy Tarreau784b8682022-04-11 14:18:10 +02007135 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007136}
7137
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007138/* transport-layer operations for QUIC connections. */
7139static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02007140 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007141 .subscribe = quic_conn_subscribe,
7142 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007143 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007144 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007145 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
7146 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01007147 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007148 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007149 .name = "QUIC",
7150};
7151
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007152static void __quic_conn_init(void)
7153{
7154 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
7155 xprt_register(XPRT_QUIC, &ssl_quic);
7156}
Willy Tarreau79367f92022-04-25 19:18:24 +02007157INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007158
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007159static void __quic_conn_deinit(void)
7160{
7161 BIO_meth_free(ha_quic_meth);
7162}
Willy Tarreau79367f92022-04-25 19:18:24 +02007163REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007164
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01007165/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
7166 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05007167 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007168 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007169struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007170{
7171 unsigned char *pos;
7172 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007173 struct quic_dghdlr *dghdlr = ctx;
7174 struct quic_dgram *dgram;
7175 int first_pkt = 1;
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007176 struct list *tasklist_head = NULL;
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007177 int max_dgrams = global.tune.maxpollevents;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007178
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007179 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7180
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007181 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007182 pos = dgram->buf;
7183 end = pos + dgram->len;
7184 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007185 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007186
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007187 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007188 if (!pkt) {
7189 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
7190 goto leave;
7191 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007192
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007193 LIST_INIT(&pkt->qc_rx_pkt_list);
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02007194 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007195 quic_rx_packet_refinc(pkt);
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007196 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram, &tasklist_head);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007197 first_pkt = 0;
7198 pos += pkt->len;
7199 quic_rx_packet_refdec(pkt);
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007200
7201 /* Free rejected packets */
7202 if (!pkt->refcnt) {
7203 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
7204 pool_free(pool_head_quic_rx_packet, pkt);
7205 }
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007206 } while (pos < end);
7207
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007208 /* Increasing the received bytes counter by the UDP datagram length
7209 * if this datagram could be associated to a connection.
7210 */
7211 if (dgram->qc)
7212 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01007213
7214 /* Mark this datagram as consumed */
7215 HA_ATOMIC_STORE(&dgram->buf, NULL);
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007216
7217 if (--max_dgrams <= 0)
7218 goto stop_here;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007219 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007220
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007221 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
7222
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007223 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007224
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007225 stop_here:
7226 /* too much work done at once, come back here later */
7227 if (!MT_LIST_ISEMPTY(&dghdlr->dgrams))
7228 tasklet_wakeup((struct tasklet *)t);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007229 leave:
7230 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007231 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007232}
7233
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05007234/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007235 * Returns 1 if succeeded, 0 if not.
7236 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02007237int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
7238 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007239{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007240 int ret = 0, long_header;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007241 size_t minlen, skip;
7242
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007243 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
7244
7245 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
7246 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007247 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007248 }
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007249
7250 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02007251 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
7252 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007253 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Willy Tarreauaf5138f2022-08-05 10:09:32 +02007254 if (end - buf < minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007255 goto err;
7256
7257 buf += skip;
7258 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
7259 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
7260 goto err;
7261
7262 *dcid = buf;
7263
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007264 ret = 1;
7265 leave:
7266 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
7267 return ret;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007268
7269 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007270 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
7271 goto leave;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007272}
7273
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007274/* Notify the MUX layer if alive about an imminent close of <qc>. */
7275void qc_notify_close(struct quic_conn *qc)
7276{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007277 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
7278
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007279 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007280 goto leave;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007281
7282 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007283 /* wake up the MUX */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007284 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
7285 TRACE_STATE("connection closure notidfied to mux",
7286 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007287 qc->conn->mux->wake(qc->conn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007288 }
7289 else
7290 TRACE_STATE("connection closure not notidfied to mux",
7291 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
7292 leave:
7293 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007294}
7295
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007296/*
7297 * Local variables:
7298 * c-indent-level: 8
7299 * c-basic-offset: 8
7300 * End:
7301 */