blob: 042bd17aec4b391732e46cf25618fa2c0a428468 [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);
Willy Tarreau41e701e2022-09-08 15:12:59 +0200217struct 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) {
Frédéric Lécaille3c1b81f2022-09-08 09:52:14 +0200355 const struct quic_pktns *pktns = qel->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
William Lallemand95fc7372022-09-14 17:54:05 +0200922 if (!read_secret)
923 goto write;
924
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200925 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 +0100926 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100927 read_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200928 TRACE_ERROR("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
929 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100930 }
931
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200932 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200933 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
934 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200935 }
936
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200937 if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) {
938 TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
939 goto leave;
940 }
941
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100942 /* Enqueue this connection asap if we could derive O-RTT secrets as
943 * listener. Note that a listener derives only RX secrets for this
944 * level.
945 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200946 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
947 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100948 quic_accept_push_qc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200949 }
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100950
William Lallemand95fc7372022-09-14 17:54:05 +0200951write:
952
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100953 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100954 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100955
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200956 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 +0100957 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100958 write_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200959 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
960 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100961 }
962
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200963 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200964 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
965 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200966 }
967
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200968 if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) {
969 TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
970 goto leave;
971 }
972
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100973 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100974 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
975 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
976 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
977
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200978 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100979 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
980 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200981 TRACE_ERROR("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
982 goto leave;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100983 }
984
William Lallemand95fc7372022-09-14 17:54:05 +0200985 if (read_secret) {
986 memcpy(rx->secret, read_secret, secret_len);
987 rx->secretlen = secret_len;
988 }
989 if (write_secret) {
990 memcpy(tx->secret, write_secret, secret_len);
991 tx->secretlen = secret_len;
992 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100993 /* Initialize all the secret keys lengths */
994 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
995 /* Prepare the next key update */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100996 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100997
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100998 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100999 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +01001000 no_secret:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001001 ret = 1;
1002 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001003 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001004 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001005}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001006
1007/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1008 * with <len> as size in CRYPTO buffers dedicated to store the information about
1009 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001010 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
1011 * buffers to store all the data.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001012 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1013 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001014static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001015 const unsigned char *data, size_t len)
1016{
1017 struct quic_crypto_buf **qcb;
1018 /* The remaining byte to store in CRYPTO buffers. */
1019 size_t cf_offset, cf_len, *nb_buf;
1020 unsigned char *pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001021 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001022
1023 nb_buf = &qel->tx.crypto.nb_buf;
1024 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1025 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1026 cf_len = len;
1027
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001028 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1029
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001030 while (len) {
1031 size_t to_copy, room;
1032
1033 pos = (*qcb)->data + (*qcb)->sz;
1034 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1035 to_copy = len > room ? room : len;
1036 if (to_copy) {
1037 memcpy(pos, data, to_copy);
1038 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1039 qel->tx.crypto.sz += to_copy;
1040 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001041 len -= to_copy;
1042 data += to_copy;
1043 }
1044 else {
1045 struct quic_crypto_buf **tmp;
1046
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001047 // FIXME: realloc!
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001048 tmp = realloc(qel->tx.crypto.bufs,
1049 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1050 if (tmp) {
1051 qel->tx.crypto.bufs = tmp;
1052 qcb = &qel->tx.crypto.bufs[*nb_buf];
1053 *qcb = pool_alloc(pool_head_quic_crypto_buf);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001054 if (!*qcb) {
1055 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1056 goto leave;
1057 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001058
1059 (*qcb)->sz = 0;
1060 ++*nb_buf;
1061 }
1062 else {
1063 break;
1064 }
1065 }
1066 }
1067
1068 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1069 * have been buffered.
1070 */
1071 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001072 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001073 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001074
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001075 /* There is at most one CRYPTO frame in this packet number
1076 * space. Let's look for it.
1077 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001078 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001079 if (frm->type != QUIC_FT_CRYPTO)
1080 continue;
1081
1082 /* Found */
1083 found = frm;
1084 break;
1085 }
1086
1087 if (found) {
1088 found->crypto.len += cf_len;
1089 }
1090 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001091 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001092 if (!frm) {
1093 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1094 goto leave;
1095 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001096
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001097 LIST_INIT(&frm->reflist);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001098 frm->type = QUIC_FT_CRYPTO;
1099 frm->crypto.offset = cf_offset;
1100 frm->crypto.len = cf_len;
1101 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001102 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001103 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001104 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001105 ret = len == 0;
1106 leave:
1107 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1108 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001109}
1110
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001111/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1112 * activity for <qc> will be interrupted.
1113 */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001114void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001115{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001116 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001117 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001118 goto leave;
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001119
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001120 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001121 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001122 qc->err.code = err.code;
1123 qc->err.app = err.app;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001124 leave:
1125 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001126}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001127
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001128/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1129void quic_set_tls_alert(struct quic_conn *qc, int alert)
1130{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001131 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1132
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001133 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1134 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001135 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001136 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1137 }
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001138 quic_set_connection_close(qc, quic_err_tls(alert));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001139 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001140 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1141
1142 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001143}
1144
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001145/* Set the application for <qc> QUIC connection.
1146 * Return 1 if succeeded, 0 if not.
1147 */
1148int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1149{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001150 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1151 qc->app_ops = &h3_ops;
1152 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1153 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001154 else
1155 return 0;
1156
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001157 return 1;
1158}
1159
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001160/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1161 * wants to provide the QUIC layer with CRYPTO data.
1162 * Returns 1 if succeeded, 0 if not.
1163 */
1164int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1165 const uint8_t *data, size_t len)
1166{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001167 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001168 enum quic_tls_enc_level tel;
1169 struct quic_enc_level *qel;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001170 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001171
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001172 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1173 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001174
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001175 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001176 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001177 goto out;
1178 }
1179
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001180 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001181 if (tel == -1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001182 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1183 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001184 }
1185
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001186 qel = &qc->els[tel];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001187 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1188 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1189 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001190 }
1191
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001192 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001193 qc, &level, &len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001194 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001195 ret = 1;
1196 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001197 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001198 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001199}
1200
1201int ha_quic_flush_flight(SSL *ssl)
1202{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001203 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001204
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001205 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1206 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001207
1208 return 1;
1209}
1210
1211int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1212{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001213 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001214
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001215 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1216
1217 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1218
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001219 quic_set_tls_alert(qc, alert);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001220 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001221 return 1;
1222}
1223
1224/* QUIC TLS methods */
1225static SSL_QUIC_METHOD ha_quic_method = {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001226 .set_encryption_secrets = ha_quic_set_encryption_secrets,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001227 .add_handshake_data = ha_quic_add_handshake_data,
1228 .flush_flight = ha_quic_flush_flight,
1229 .send_alert = ha_quic_send_alert,
1230};
1231
1232/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1233 * Returns an error count.
1234 */
1235int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1236{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001237 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1238 int cfgerr = 0;
1239
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001240 long options =
1241 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1242 SSL_OP_SINGLE_ECDH_USE |
1243 SSL_OP_CIPHER_SERVER_PREFERENCE;
1244 SSL_CTX *ctx;
1245
1246 ctx = SSL_CTX_new(TLS_server_method());
1247 bind_conf->initial_ctx = ctx;
1248
1249 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001250 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1251 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1252 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001253
1254#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand373ce732022-09-07 11:11:59 +02001255# if defined(HAVE_SSL_CLIENT_HELLO_CB)
1256# if defined(SSL_OP_NO_ANTI_REPLAY)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001257 if (bind_conf->ssl_conf.early_data) {
1258 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001259 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001260 }
William Lallemand373ce732022-09-07 11:11:59 +02001261# endif /* !SSL_OP_NO_ANTI_REPLAY */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001262 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1263 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
William Lallemand373ce732022-09-07 11:11:59 +02001264# else /* ! HAVE_SSL_CLIENT_HELLO_CB */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
William Lallemand373ce732022-09-07 11:11:59 +02001266# endif
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001267 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1268#endif
1269 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1270
1271 return cfgerr;
1272}
1273
1274/* Decode an expected packet number from <truncated_on> its truncated value,
1275 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1276 * the number of bits used to encode this packet number (its length in bytes * 8).
1277 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1278 */
1279static uint64_t decode_packet_number(uint64_t largest_pn,
1280 uint32_t truncated_pn, unsigned int pn_nbits)
1281{
1282 uint64_t expected_pn = largest_pn + 1;
1283 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1284 uint64_t pn_hwin = pn_win / 2;
1285 uint64_t pn_mask = pn_win - 1;
1286 uint64_t candidate_pn;
1287
1288
1289 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1290 /* Note that <pn_win> > <pn_hwin>. */
1291 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1292 candidate_pn + pn_hwin <= expected_pn)
1293 return candidate_pn + pn_win;
1294
1295 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1296 return candidate_pn - pn_win;
1297
1298 return candidate_pn;
1299}
1300
1301/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1302 * cryptographic context.
1303 * <largest_pn> is the largest received packet number and <pn> the address of
1304 * the packet number field for this packet with <byte0> address of its first byte.
1305 * <end> points to one byte past the end of this packet.
1306 * Returns 1 if succeeded, 0 if not.
1307 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001308static int qc_do_rm_hp(struct quic_conn *qc,
1309 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001310 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001311{
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02001312 int ret, i, pnlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001313 uint64_t packet_number;
1314 uint32_t truncated_pn = 0;
1315 unsigned char mask[5] = {0};
1316 unsigned char *sample;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001317 EVP_CIPHER_CTX *cctx = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001318
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001319 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1320
1321 ret = 0;
1322
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001323 /* Check there is enough data in this packet. */
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001324 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001325 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1326 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001327 }
1328
1329 cctx = EVP_CIPHER_CTX_new();
1330 if (!cctx) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001331 TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
1332 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001333 }
1334
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001335 sample = pn + QUIC_PACKET_PN_MAXLEN;
1336
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02001337 if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
1338 TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001339 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001340 }
1341
1342 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1343 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1344 for (i = 0; i < pnlen; i++) {
1345 pn[i] ^= mask[i + 1];
1346 truncated_pn = (truncated_pn << 8) | pn[i];
1347 }
1348
1349 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1350 /* Store remaining information for this unprotected header */
1351 pkt->pn = packet_number;
1352 pkt->pnl = pnlen;
1353
1354 ret = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001355 leave:
1356 if (cctx)
1357 EVP_CIPHER_CTX_free(cctx);
1358 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001359 return ret;
1360}
1361
1362/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1363 * address, with <payload_len> as payload length, <aad> as address of
1364 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1365 * context.
1366 * Returns 1 if succeeded, 0 if not.
1367 */
1368static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1369 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001370 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001371{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001372 int ret = 0;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001373 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001374 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001375 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001376 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001377
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001378 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
1379
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001380 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001381 TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001382 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001383 }
1384
1385 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001386 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001387 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001388 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001389 }
1390
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001391 ret = 1;
1392 leave:
1393 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
1394 return ret;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001395
1396 err:
1397 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001398 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001399}
1400
1401/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1402 * Returns 1 if succeeded, 0 if not.
1403 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001404static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel,
1405 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001406{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001407 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001408 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001409 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001410 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001411 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001412 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1413 unsigned char *rx_key = tls_ctx->rx.key;
1414
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001415 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1416
1417 ret = 0;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001418 kp_changed = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001419
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001420 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1421 /* The two tested bits are not at the same position,
1422 * this is why they are first both inversed.
1423 */
1424 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1425 if (pkt->pn < tls_ctx->rx.pn) {
1426 /* The lowest packet number of a previous key phase
1427 * cannot be null if it really stores previous key phase
1428 * secrets.
1429 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001430 // TODO: check if BUG_ON() more suitable
1431 if (!pkt->qc->ku.prv_rx.pn) {
1432 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1433 goto leave;
1434 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001435
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001436 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1437 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001438 rx_key = pkt->qc->ku.prv_rx.key;
1439 }
1440 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1441 /* Next key phase */
1442 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001443 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1444 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001445 rx_key = pkt->qc->ku.nxt_rx.key;
1446 }
1447 }
1448 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001449
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001450 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) {
1451 TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc);
1452 goto leave;
1453 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001454
1455 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1456 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001457 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001458 if (!ret) {
1459 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1460 goto leave;
1461 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001462
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001463 /* Update the keys only if the packet decryption succeeded. */
1464 if (kp_changed) {
1465 quic_tls_rotate_keys(pkt->qc);
1466 /* Toggle the Key Phase bit */
1467 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1468 /* Store the lowest packet number received for the current key phase */
1469 tls_ctx->rx.pn = pkt->pn;
1470 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001471 if (!quic_tls_key_update(pkt->qc)) {
1472 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1473 goto leave;
1474 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001475 }
1476
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001477 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001478 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001479 ret = 1;
1480 leave:
1481 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille11a6f402022-08-16 14:48:59 +02001482 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001483}
1484
Frédéric Lécaille96367152022-04-25 09:40:19 +02001485
1486/* Remove references to <frm> frame */
1487static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1488{
Frédéric Lécaille96367152022-04-25 09:40:19 +02001489 struct quic_frame *f, *tmp;
1490
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001491 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1492
Frédéric Lécaille96367152022-04-25 09:40:19 +02001493 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001494 f->origin = NULL;
1495 LIST_DELETE(&f->ref);
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001496 if (f->pkt) {
1497 TRACE_DEVEL("remove frame reference",
1498 QUIC_EV_CONN_PRSAFRM, qc, f, &f->pkt->pn_node.key);
1499 }
1500 else {
1501 TRACE_DEVEL("remove frame reference for unsent frame",
1502 QUIC_EV_CONN_PRSAFRM, qc, f);
1503 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001504 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001505
1506 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001507}
1508
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001509/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001510void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001511{
1512 uint64_t pn;
1513 struct quic_frame *origin, *f, *tmp;
1514
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001515 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1516
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001517 /* Identify this frame: a frame copy or one of its copies */
1518 origin = frm->origin ? frm->origin : frm;
1519 /* Ensure the source of the copies is flagged as acked, <frm> being
1520 * possibly a copy of <origin>
1521 */
1522 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1523 /* Mark all the copy of <origin> as acknowledged. We must
1524 * not release the packets (releasing the frames) at this time as
1525 * they are possibly also to be acknowledged alongside the
1526 * the current one.
1527 */
1528 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001529 if (f->pkt) {
Frédéric Lécaille025945f2022-09-06 09:55:21 +02001530 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1531 f->origin = NULL;
1532 LIST_DELETE(&f->ref);
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001533 pn = f->pkt->pn_node.key;
1534 TRACE_DEVEL("mark frame as acked from packet",
1535 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1536 }
1537 else {
Frédéric Lécaille025945f2022-09-06 09:55:21 +02001538 TRACE_DEVEL("freeing unsent frame",
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001539 QUIC_EV_CONN_PRSAFRM, qc, f);
Frédéric Lécaille025945f2022-09-06 09:55:21 +02001540 LIST_DELETE(&f->ref);
1541 LIST_DELETE(&f->list);
1542 pool_free(pool_head_quic_frame, f);
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001543 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001544 }
1545 LIST_DELETE(&frm->list);
1546 pn = frm->pkt->pn_node.key;
1547 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001548 TRACE_DEVEL("freeing frame from packet",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001549 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1550 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001551
1552 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001553}
1554
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001555/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1556 * and all STREAM data are acknowledged. The MUX is responsible to have set
1557 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1558 *
1559 * TODO this should also be called on lost packet detection
1560 */
1561static void qc_check_close_on_released_mux(struct quic_conn *qc)
1562{
1563 struct ssl_sock_ctx *ctx = qc->xprt_ctx;
1564
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001565 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1566
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001567 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1568 /* Reuse errcode which should have been previously set by the MUX on release. */
1569 quic_set_connection_close(qc, qc->err);
1570 tasklet_wakeup(ctx->wait_event.tasklet);
1571 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001572
1573 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001574}
1575
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001576/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001577 *
1578 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001579 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001580static int quic_stream_try_to_consume(struct quic_conn *qc,
1581 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001582{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001583 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001584 struct eb64_node *frm_node;
1585
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001586 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1587
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001588 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001589 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001590 while (frm_node) {
1591 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001592 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001593 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001594
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001595 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001596 offset = strm->offset.key;
1597 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001598
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001599 if (offset > stream->ack_offset)
1600 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001601
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001602 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001603 /* cf. next comment : frame may be freed at this stage. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001604 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001605 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001606 ret = 1;
1607 }
1608
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001609 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1610 * has been freed. with the stream frames tree. Nothing to do
1611 * anymore in here.
1612 */
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001613 if (!stream) {
1614 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001615 ret = 1;
1616 goto leave;
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001617 }
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001618
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001619 frm_node = eb64_next(frm_node);
1620 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001621
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001622 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001623 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001624 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001625
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001626 leave:
1627 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001628 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001629}
1630
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001631/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001632static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1633 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001634{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001635 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001636
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001637 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm);
1638
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001639 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001640 switch (frm->type) {
1641 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1642 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001643 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001644 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001645 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001646 const size_t offset = strm_frm->offset.key;
1647 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001648
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001649 /* do not use strm_frm->stream as the qc_stream_desc instance
1650 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001651 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001652 *
1653 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001654 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001655 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001656 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1657 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001658 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001659 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001660 /* early return */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001661 goto leave;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001662 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001663 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001664
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001665 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001666 if (offset <= stream->ack_offset) {
1667 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001668 stream_acked = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001669 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001670 qc, strm_frm, stream);
1671 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001672
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001673 if (!stream) {
1674 /* no need to continue if stream freed. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001675 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001676 qc_release_frm(qc, frm);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001677 qc_check_close_on_released_mux(qc);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001678 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001679 }
1680
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001681 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Frédéric Lécailleda342552022-04-25 10:28:49 +02001682 qc, strm_frm, stream);
1683 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001684 }
1685 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001686 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001687 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001688
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001689 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001690 }
1691 break;
1692 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001693 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001694 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001695
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001696 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001697 struct qcc *qcc = qc->qcc;
1698
1699 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1700 tasklet_wakeup(qcc->subs->tasklet);
1701 qcc->subs->events &= ~SUB_RETRY_SEND;
1702 if (!qcc->subs->events)
1703 qcc->subs = NULL;
1704 }
1705 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001706 leave:
1707 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001708}
1709
1710/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1711 * deallocating them, and their TX frames.
1712 * Returns the last node reached to be used for the next range.
1713 * May be NULL if <largest> node could not be found.
1714 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001715static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1716 struct eb_root *pkts,
1717 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001718 struct list *newly_acked_pkts,
1719 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001720 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001721{
1722 struct eb64_node *node;
1723 struct quic_tx_packet *pkt;
1724
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001725 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1726
Frédéric Lécaille843399f2022-07-22 16:27:44 +02001727 node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001728 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001729 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001730
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001731 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001732 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001733 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001734 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001735 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001736 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001737 node = eb64_prev(node);
1738 eb64_delete(&pkt->pn_node);
1739 }
1740
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001741 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001742 return node;
1743}
1744
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001745/* Remove all frames from <pkt_frm_list> and reinsert them in the
1746 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001747 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001748static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001749 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001750 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001751{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001752 struct quic_frame *frm, *frmbak;
1753 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001754 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001755 uint64_t pn = pkt->pn_node.key;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001756
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001757 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1758
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001759 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001760 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001761 LIST_DELETE(&frm->list);
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001762 quic_tx_packet_refdec(pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001763 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001764 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001765 /* Remove any reference to this frame */
1766 qc_frm_unref(qc, frm);
1767 switch (frm->type) {
1768 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1769 {
1770 struct quic_stream *strm_frm = &frm->stream;
1771 struct eb64_node *node = NULL;
1772 struct qc_stream_desc *stream_desc;
1773
1774 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1775 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001776 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1777 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001778 qc, frm, &pn);
1779 pool_free(pool_head_quic_frame, frm);
1780 continue;
1781 }
1782
1783 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1784 /* Do not resend this frame if in the "already acked range" */
1785 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001786 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001787 QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillebccbad22022-08-31 15:02:53 +02001788 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001789 continue;
1790 }
1791 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1792 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001793 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001794 QUIC_EV_CONN_PRSAFRM, qc, frm);
1795 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001796 break;
1797 }
1798
1799 default:
1800 break;
1801 }
1802
1803 /* Do not resend probing packet with old data */
1804 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001805 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1806 qc, frm, &pn);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001807 if (frm->origin)
1808 LIST_DELETE(&frm->ref);
1809 pool_free(pool_head_quic_frame, frm);
1810 continue;
1811 }
1812
1813 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001814 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1815 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001816 qc, frm, &pn);
1817 pool_free(pool_head_quic_frame, frm);
1818 }
1819 else {
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001820 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1821 /* Mark this STREAM frame as lost. A look up their stream descriptor
1822 * will be performed to check the stream is not consumed or released.
1823 */
Amaury Denoyellef372e742022-08-16 14:41:57 +02001824 frm->flags |= QUIC_FL_TX_FRAME_LOST;
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001825 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001826 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001827 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001828 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001829 }
1830
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001831 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001832
1833 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001834}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001835
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001836/* Free <pkt> TX packet and its attached frames.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001837 * This is the responsibility of the caller to remove this packet of
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001838 * any data structure it was possibly attached to.
1839 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001840static inline void free_quic_tx_packet(struct quic_conn *qc,
1841 struct quic_tx_packet *pkt)
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001842{
1843 struct quic_frame *frm, *frmbak;
1844
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001845 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1846
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001847 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001848 goto leave;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001849
1850 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1851 LIST_DELETE(&frm->list);
1852 pool_free(pool_head_quic_frame, frm);
1853 }
1854 pool_free(pool_head_quic_tx_packet, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001855
1856 leave:
1857 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001858}
1859
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001860/* Free the TX packets of <pkts> list */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001861static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001862{
1863 struct quic_tx_packet *pkt, *tmp;
1864
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001865 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1866
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001867 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001868 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001869 eb64_delete(&pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001870 free_quic_tx_packet(qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001871 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001872
1873 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001874}
1875
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001876/* Remove already sent ranges of acknowledged packet numbers from
1877 * <pktns> packet number space tree below <largest_acked_pn> possibly
1878 * updating the range which contains <largest_acked_pn>.
1879 * Never fails.
1880 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001881static void qc_treat_ack_of_ack(struct quic_conn *qc,
1882 struct quic_pktns *pktns,
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001883 int64_t largest_acked_pn)
1884{
1885 struct eb64_node *ar, *next_ar;
1886 struct quic_arngs *arngs = &pktns->rx.arngs;
1887
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001888 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1889
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001890 ar = eb64_first(&arngs->root);
1891 while (ar) {
1892 struct quic_arng_node *ar_node;
1893
1894 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001895 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001896
1897 if ((int64_t)ar_node->first.key > largest_acked_pn) {
1898 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001899 break;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001900 }
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001901
1902 if (largest_acked_pn < ar_node->last) {
1903 eb64_delete(ar);
1904 ar_node->first.key = largest_acked_pn + 1;
1905 eb64_insert(&arngs->root, ar);
1906 break;
1907 }
1908
1909 eb64_delete(ar);
1910 pool_free(pool_head_quic_arng, ar_node);
1911 arngs->sz--;
1912 ar = next_ar;
1913 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001914
1915 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001916}
1917
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001918/* Send a packet ack event nofication for each newly acked packet of
1919 * <newly_acked_pkts> list and free them.
1920 * Always succeeds.
1921 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001922static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001923 struct list *newly_acked_pkts)
1924{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001925 struct quic_tx_packet *pkt, *tmp;
1926 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1927
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001928 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1929
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001930 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1931 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001932 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001933 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001934 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001935 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001936 /* If this packet contained an ACK frame, proceed to the
1937 * acknowledging of range of acks from the largest acknowledged
1938 * packet number which was sent in an ACK frame by this packet.
1939 */
1940 if (pkt->largest_acked_pn != -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001941 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001942 ev.ack.acked = pkt->in_flight_len;
1943 ev.ack.time_sent = pkt->time_sent;
1944 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001945 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001946 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001947 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001948 }
1949
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001950 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1951
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001952}
1953
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001954/* Release all the frames attached to <pktns> packet number space */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001955static inline void qc_release_pktns_frms(struct quic_conn *qc,
1956 struct quic_pktns *pktns)
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001957{
1958 struct quic_frame *frm, *frmbak;
1959
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001960 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
1961
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001962 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1963 LIST_DELETE(&frm->list);
1964 pool_free(pool_head_quic_frame, frm);
1965 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001966
1967 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001968}
1969
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001970/* Handle <pkts> list of lost packets detected at <now_us> handling
1971 * their TX frames.
1972 * Send a packet loss event to the congestion controller if
1973 * in flight packet have been lost.
1974 * Also frees the packet in <pkts> list.
1975 * Never fails.
1976 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001977static inline void qc_release_lost_pkts(struct quic_conn *qc,
1978 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001979 struct list *pkts,
1980 uint64_t now_us)
1981{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001982 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001983
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001984 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1985
Frédéric Lécaille277c4622022-08-24 17:06:04 +02001986 if (LIST_ISEMPTY(pkts))
1987 goto leave;
1988
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001989 oldest_lost = newest_lost = NULL;
1990 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001991 struct list tmp = LIST_HEAD_INIT(tmp);
1992
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001993 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001994 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001995 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001996 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001997 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001998 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001999 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02002000 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002001 if (!oldest_lost) {
2002 oldest_lost = newest_lost = pkt;
2003 }
2004 else {
2005 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002006 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002007 newest_lost = pkt;
2008 }
2009 }
2010
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01002011 if (newest_lost) {
2012 /* Sent a congestion event to the controller */
Benoit DOLEZ69e3f052022-06-08 09:28:56 +02002013 struct quic_cc_event ev = { };
2014
2015 ev.type = QUIC_CC_EVT_LOSS;
2016 ev.loss.time_sent = newest_lost->time_sent;
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01002017
2018 quic_cc_event(&qc->path->cc, &ev);
2019 }
2020
2021 /* If an RTT have been already sampled, <rtt_min> has been set.
2022 * We must check if we are experiencing a persistent congestion.
2023 * If this is the case, the congestion controller must re-enter
2024 * slow start state.
2025 */
2026 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2027 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
2028
2029 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2030 now_ms, qc->max_ack_delay))
2031 qc->path->cc.algo->slow_start(&qc->path->cc);
2032 }
2033
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002034 quic_tx_packet_refdec(oldest_lost);
2035 if (newest_lost != oldest_lost)
2036 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002037
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002038 leave:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002039 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002040}
2041
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002042/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2043 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002044 * 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 +01002045 * acked ack-eliciting packet.
2046 * Return 1, if succeeded, 0 if not.
2047 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002048static inline int qc_parse_ack_frm(struct quic_conn *qc,
2049 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002050 struct quic_enc_level *qel,
2051 unsigned int *rtt_sample,
2052 const unsigned char **pos, const unsigned char *end)
2053{
2054 struct quic_ack *ack = &frm->ack;
2055 uint64_t smallest, largest;
2056 struct eb_root *pkts;
2057 struct eb64_node *largest_node;
2058 unsigned int time_sent, pkt_flags;
2059 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2060 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002061 int ret = 0;
2062
2063 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002064
2065 if (ack->largest_ack > qel->pktns->tx.next_pn) {
2066 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002067 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002068 goto err;
2069 }
2070
2071 if (ack->first_ack_range > ack->largest_ack) {
2072 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002073 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002074 goto err;
2075 }
2076
2077 largest = ack->largest_ack;
2078 smallest = largest - ack->first_ack_range;
2079 pkts = &qel->pktns->tx.pkts;
2080 pkt_flags = 0;
2081 largest_node = NULL;
2082 time_sent = 0;
2083
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002084 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002085 largest_node = eb64_lookup(pkts, largest);
2086 if (!largest_node) {
2087 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002088 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002089 }
2090 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002091 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002092 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002093 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002094 }
2095
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002096 TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002097 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002098 do {
2099 uint64_t gap, ack_range;
2100
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002101 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2102 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002103 if (!ack->ack_range_num--)
2104 break;
2105
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002106 if (!quic_dec_int(&gap, pos, end)) {
2107 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002108 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002109 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002110
2111 if (smallest < gap + 2) {
2112 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002113 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002114 goto err;
2115 }
2116
2117 largest = smallest - gap - 2;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002118 if (!quic_dec_int(&ack_range, pos, end)) {
2119 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002120 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002121 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002122
2123 if (largest < ack_range) {
2124 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002125 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002126 goto err;
2127 }
2128
2129 /* Do not use this node anymore. */
2130 largest_node = NULL;
2131 /* Next range */
2132 smallest = largest - ack_range;
2133
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002134 TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002135 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002136 } while (1);
2137
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002138 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2139 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002140 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002141 }
2142
2143 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2144 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002145 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002146 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002147 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002148 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2149 if (quic_peer_validated_addr(qc))
2150 qc->path->loss.pto_count = 0;
2151 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002152 }
2153
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002154 ret = 1;
2155 leave:
2156 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2157 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002158
2159 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002160 free_quic_tx_pkts(qc, &newly_acked_pkts);
2161 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002162}
2163
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002164/* This function gives the detail of the SSL error. It is used only
2165 * if the debug mode and the verbose mode are activated. It dump all
2166 * the SSL error until the stack was empty.
2167 */
2168static forceinline void qc_ssl_dump_errors(struct connection *conn)
2169{
2170 if (unlikely(global.mode & MODE_DEBUG)) {
2171 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002172 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002173 unsigned long ret;
2174
Willy Tarreau325fc632022-04-11 18:47:38 +02002175 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002176 ret = ERR_get_error();
2177 if (!ret)
2178 return;
2179
2180 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002181 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002182 }
2183 }
2184}
2185
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002186int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2187 const char **str, int *len);
2188
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002189/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2190 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002191 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002192 * Return 1 if succeeded, 0 if not.
2193 */
2194static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002195 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002196 const unsigned char *data, size_t len,
2197 struct quic_rx_packet *pkt,
2198 struct quic_rx_crypto_frm *cf)
2199{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002200 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002201 struct quic_conn *qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002202 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002203
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002204 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002205 qc = ctx->qc;
2206
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002207 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2208
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002209 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002210 TRACE_ERROR("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002211 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002212 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002213 }
2214
2215 el->rx.crypto.offset += len;
2216 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002217 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002218
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002219 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002220 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002221 ssl_err = SSL_do_handshake(ctx->ssl);
2222 if (ssl_err != 1) {
2223 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2224 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002225 TRACE_PROTO("SSL handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002226 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002227 goto out;
2228 }
2229
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002230 /* TODO: Should close the connection asap */
2231 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2232 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2233 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2234 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2235 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002236 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002237 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002238 ERR_clear_error();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002239 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002240 }
2241
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002242 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002243
2244 /* Check the alpn could be negotiated */
2245 if (!qc->app_ops) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002246 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002247 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002248 goto leave;
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002249 }
2250
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002251 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002252 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002253 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2254 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2255 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002256 /* I/O callback switch */
2257 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002258 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002259 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002260 /* The connection is ready to be accepted. */
2261 quic_accept_push_qc(qc);
2262 }
2263 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002264 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002265 }
William Lallemand95fc7372022-09-14 17:54:05 +02002266
2267 if (!quic_tls_key_update(qc)) {
2268 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_IO_CB, qc);
2269 goto leave;
2270 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002271 } else {
2272 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2273 if (ssl_err != 1) {
2274 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2275 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002276 TRACE_PROTO("SSL post handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002277 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002278 goto out;
2279 }
2280
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002281 TRACE_ERROR("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002282 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002283 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002284 }
2285
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002286 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002287 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002288
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002289 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002290 ret = 1;
2291 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002292 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002293 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002294}
2295
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002296/* Parse a STREAM frame <strm_frm>
2297 *
2298 * Return 1 on success. On error, 0 is returned. In this case, the packet
2299 * containing the frame must not be acknowledged.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002300 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002301static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2302 struct quic_stream *strm_frm,
2303 struct quic_conn *qc)
2304{
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002305 int ret;
2306
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002307 /* RFC9000 13.1. Packet Processing
2308 *
2309 * A packet MUST NOT be acknowledged until packet protection has been
2310 * successfully removed and all frames contained in the packet have
2311 * been processed. For STREAM frames, this means the data has been
2312 * enqueued in preparation to be received by the application protocol,
2313 * but it does not require that data be delivered and consumed.
2314 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002315 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2316
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002317 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2318 strm_frm->offset.key, strm_frm->fin,
2319 (char *)strm_frm->data);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002320
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002321 /* frame rejected - packet must not be acknowledeged */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002322 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2323 return !ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002324}
2325
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002326/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2327 * for <qc> QUIC connection.
2328 * This is a best effort function which never fails even if no memory could be
2329 * allocated to duplicate these frames.
2330 */
2331static void qc_dup_pkt_frms(struct quic_conn *qc,
2332 struct list *pkt_frm_list, struct list *out_frm_list)
2333{
2334 struct quic_frame *frm, *frmbak;
2335 struct list tmp = LIST_HEAD_INIT(tmp);
2336
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002337 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2338
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002339 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2340 struct quic_frame *dup_frm, *origin;
2341
2342 switch (frm->type) {
2343 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2344 {
2345 struct quic_stream *strm_frm = &frm->stream;
2346 struct eb64_node *node = NULL;
2347 struct qc_stream_desc *stream_desc;
2348
2349 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2350 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002351 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002352 continue;
2353 }
2354
2355 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2356 /* Do not resend this frame if in the "already acked range" */
2357 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002358 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002359 QUIC_EV_CONN_PRSAFRM, qc, frm);
2360 continue;
2361 }
2362 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2363 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002364 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002365 QUIC_EV_CONN_PRSAFRM, qc, frm);
2366 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002367 break;
2368 }
2369
2370 default:
2371 break;
2372 }
2373
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002374 dup_frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002375 if (!dup_frm) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002376 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002377 break;
2378 }
2379
2380 /* If <frm> is already a copy of another frame, we must take
2381 * its original frame as source for the copy.
2382 */
2383 origin = frm->origin ? frm->origin : frm;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002384 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Frédéric Lécaillee35463c2022-08-27 10:19:42 +02002385 if (origin->pkt)
2386 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
2387 qc, NULL, &origin->pkt->pn_node.key);
2388 else {
2389 /* <origin> is a frame which was sent from a packet detected as lost. */
2390 TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
2391 }
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002392 *dup_frm = *origin;
2393 dup_frm->pkt = NULL;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002394 dup_frm->origin = origin;
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002395 dup_frm->flags = 0;
2396 LIST_INIT(&dup_frm->reflist);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002397 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2398 LIST_APPEND(&tmp, &dup_frm->list);
2399 }
2400
2401 LIST_SPLICE(out_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002402
2403 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002404}
2405
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002406/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002407static void qc_prep_fast_retrans(struct quic_conn *qc,
2408 struct quic_enc_level *qel,
2409 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002410{
2411 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002412 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002413 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002414 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002415
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002416 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2417
2418 BUG_ON(frms1 == frms2);
2419
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002420 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002421 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002422 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002423 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002424 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2425 node = eb64_next(node);
2426 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002427 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002428 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002429 }
2430
2431 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002432 goto leave;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002433
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002434 /* When building a packet from another one, the field which may increase the
2435 * packet size is the packet number. And the maximum increase is 4 bytes.
2436 */
2437 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2438 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +02002439 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002440 goto leave;
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002441 }
2442
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +02002443 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002444 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2445 if (frms == frms1 && frms2) {
2446 frms = frms2;
2447 goto start;
2448 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002449 leave:
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +02002450 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002451}
2452
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002453/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002454 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002455 * Initial packets send them coalescing with others (Handshake here).
2456 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002457 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002458static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2459 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002460{
2461 struct list itmp = LIST_HEAD_INIT(itmp);
2462 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002463
2464 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2465 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2466 struct quic_enc_level *qel = iqel;
2467 struct eb_root *pkts;
2468 struct eb64_node *node;
2469 struct quic_tx_packet *pkt;
2470 struct list *tmp = &itmp;
2471
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002472 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002473 start:
2474 pkt = NULL;
2475 pkts = &qel->pktns->tx.pkts;
2476 node = eb64_first(pkts);
2477 /* Skip the empty packet (they have already been retransmitted) */
2478 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002479 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002480 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002481 break;
2482 node = eb64_next(node);
2483 }
2484
2485 if (!pkt)
2486 goto end;
2487
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002488 /* When building a packet from another one, the field which may increase the
2489 * packet size is the packet number. And the maximum increase is 4 bytes.
2490 */
2491 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2492 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2493 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2494 goto end;
2495 }
2496
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002497 qel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002498
2499 /* No risk to loop here, #packet per datagram is bounded */
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002500 requeue:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002501 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002502 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002503 if (qel == iqel) {
2504 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2505 pkt = pkt->next;
2506 tmp = &htmp;
2507 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002508 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002509 goto requeue;
2510 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002511 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002512
2513 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002514 LIST_SPLICE(ifrms, &itmp);
2515 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002516
2517 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002518}
2519
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002520static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002521{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002522 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2523
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002524 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2525 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2526 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2527 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002528 goto out;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002529
2530 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2531 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002532
2533 out:
2534 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002535}
2536
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002537/* Enqueue a STOP_SENDING frame to send into 1RTT packet number space
2538 * frame list to send.
2539 * Return 1 if succeeded, 0 if not.
2540 */
2541static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id)
2542{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002543 int ret = 0;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002544 struct quic_frame *frm;
2545 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2546 uint64_t app_error_code;
2547
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002548 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2549
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002550 /* TODO: the mux may be released, we cannot have more
2551 * information about the application error code to send
2552 * at this time.
2553 */
2554 app_error_code = H3_REQUEST_REJECTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002555 // fixme: zalloc
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002556 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002557 if (!frm) {
2558 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2559 goto out;
2560 }
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002561
2562 frm->type = QUIC_FT_STOP_SENDING;
2563 frm->stop_sending.id = id;
2564 frm->stop_sending.app_error_code = app_error_code;
2565 LIST_INIT(&frm->reflist);
2566 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002567 ret = 1;
2568 out:
2569 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2570 return ret;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002571}
2572
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002573/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2574 * as I/O handler context and <qel> as encryption level.
2575 * Returns 1 if succeeded, 0 if failed.
2576 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002577static 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 +01002578 struct quic_enc_level *qel)
2579{
2580 struct quic_frame frm;
2581 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002582 struct quic_conn *qc = ctx->qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002583 int fast_retrans = 0, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002584
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002585 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002586 /* Skip the AAD */
2587 pos = pkt->data + pkt->aad_len;
2588 end = pkt->data + pkt->len;
2589
2590 while (pos < end) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002591 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
2592 // trace already emitted by function above
2593 goto leave;
2594 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002595
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002596 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002597 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002598 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002599 break;
2600 case QUIC_FT_PING:
2601 break;
2602 case QUIC_FT_ACK:
2603 {
2604 unsigned int rtt_sample;
2605
2606 rtt_sample = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002607 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
2608 // trace already emitted by function above
2609 goto leave;
2610 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002611
2612 if (rtt_sample) {
2613 unsigned int ack_delay;
2614
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002615 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002616 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002617 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2618 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002619 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002620 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002621 break;
2622 }
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002623 case QUIC_FT_RESET_STREAM:
2624 /* TODO: handle this frame at STREAM level */
2625 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002626 case QUIC_FT_STOP_SENDING:
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002627 {
2628 struct quic_stop_sending *stop_sending = &frm.stop_sending;
2629 if (qc->mux_state == QC_MUX_READY) {
2630 if (qcc_recv_stop_sending(qc->qcc, stop_sending->id,
2631 stop_sending->app_error_code)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002632 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
2633 goto leave;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002634 }
2635 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002636 break;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002637 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002638 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002639 {
2640 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002641
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002642 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002643 /* XXX TO DO: <cfdebug> is used only for the traces. */
2644 struct quic_rx_crypto_frm cfdebug = { };
2645
2646 cfdebug.offset_node.key = frm.crypto.offset;
2647 cfdebug.len = frm.crypto.len;
2648 TRACE_PROTO("CRYPTO data discarded",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002649 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002650 break;
2651 }
2652
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002653 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2654 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2655 /* XXX TO DO: <cfdebug> is used only for the traces. */
2656 struct quic_rx_crypto_frm cfdebug = { };
2657
2658 cfdebug.offset_node.key = frm.crypto.offset;
2659 cfdebug.len = frm.crypto.len;
2660 /* Nothing to do */
2661 TRACE_PROTO("Already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002662 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002663 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaillebb995ea2022-09-07 17:34:18 +02002664 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] &&
2665 !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP))
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002666 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002667 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002669 else {
2670 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2671 /* XXX TO DO: <cfdebug> is used only for the traces. */
2672 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002674 cfdebug.offset_node.key = frm.crypto.offset;
2675 cfdebug.len = frm.crypto.len;
2676 TRACE_PROTO("Partially already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002677 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002678 frm.crypto.len -= diff;
2679 frm.crypto.data += diff;
2680 frm.crypto.offset = qel->rx.crypto.offset;
2681 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002682 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002683
2684 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002685 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002686 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002687
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002688 cfdebug.offset_node.key = frm.crypto.offset;
2689 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002690 if (!qc_provide_cdata(qel, ctx,
2691 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002692 pkt, &cfdebug)) {
2693 // trace already emitted by function above
2694 goto leave;
2695 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002696
2697 break;
2698 }
2699
2700 /* frm.crypto.offset > qel->rx.crypto.offset */
2701 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2702 if (!cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002703 TRACE_ERROR("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002704 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002705 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002706 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002707
2708 cf->offset_node.key = frm.crypto.offset;
2709 cf->len = frm.crypto.len;
2710 cf->data = frm.crypto.data;
2711 cf->pkt = pkt;
2712 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2713 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002714 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002715 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002716 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002717 {
2718 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002719 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002720
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002721 /* The upper layer may not be allocated. */
2722 if (qc->mux_state != QC_MUX_READY) {
2723 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002724 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002725 break;
2726 }
2727 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002728 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002729 if (!qc_stop_sending_frm_enqueue(qc, stream->id))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002730 TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc);
2731 /* This packet will not be acknowledged */
2732 goto leave;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002733 }
2734 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002735
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002736 if (!qc_handle_strm_frm(pkt, stream, qc)) {
2737 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
2738 goto leave;
2739 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002740
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002741 break;
2742 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002743 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002744 if (qc->mux_state == QC_MUX_READY) {
2745 struct quic_max_data *data = &frm.max_data;
2746 qcc_recv_max_data(qc->qcc, data->max_data);
2747 }
2748 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002749 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002750 if (qc->mux_state == QC_MUX_READY) {
2751 struct quic_max_stream_data *data = &frm.max_stream_data;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002752 if (qcc_recv_max_stream_data(qc->qcc, data->id,
2753 data->max_stream_data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002754 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
2755 goto leave;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002756 }
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002757 }
2758 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002759 case QUIC_FT_MAX_STREAMS_BIDI:
2760 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002761 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002762 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002763 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2764 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002765 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002766 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2767 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002768 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002769 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2770 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002771 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002772 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002773 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002774 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002775 case QUIC_FT_RETIRE_CONNECTION_ID:
2776 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002777 break;
2778 case QUIC_FT_CONNECTION_CLOSE:
2779 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002780 /* Increment the error counters */
2781 qc_cc_err_count_inc(qc, &frm);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002782 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002783 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2784 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002785 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002786 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002787 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002788 /* RFC 9000 10.2. Immediate Close:
2789 * The closing and draining connection states exist to ensure
2790 * that connections close cleanly and that delayed or reordered
2791 * packets are properly discarded. These states SHOULD persist
2792 * for at least three times the current PTO interval...
2793 *
2794 * Rearm the idle timeout only one time when entering draining
2795 * state.
2796 */
2797 qc_idle_timer_do_rearm(qc);
2798 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002799 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002800 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002801 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002802 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002803 if (qc_is_listener(ctx->qc)) {
2804 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
2805 QUIC_EV_CONN_PRSHPKT, qc);
2806 goto leave;
2807 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002808
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002809 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002810 break;
2811 default:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002812 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
2813 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002814 }
2815 }
2816
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002817 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002818 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002819
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002820 if (fast_retrans) {
2821 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2822 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2823
Frédéric Lécaillebb995ea2022-09-07 17:34:18 +02002824 TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002825 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
Frédéric Lécaillebb995ea2022-09-07 17:34:18 +02002826 qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002827 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002828
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002829 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2830 * has successfully parse a Handshake packet. The Initial encryption must also
2831 * be discarded.
2832 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002833 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002834 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002835 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2836 QUIC_FL_TLS_SECRETS_DCD)) {
2837 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2838 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2839 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2840 qc_set_timer(ctx->qc);
2841 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002842 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002843 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002844 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2845 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002846 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002847 }
2848
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002849 ret = 1;
2850 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002851 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002852 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002853}
2854
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002855/* Allocate Tx buffer from <qc> quic-conn if needed.
2856 *
2857 * Returns allocated buffer or NULL on error.
2858 */
2859static struct buffer *qc_txb_alloc(struct quic_conn *qc)
2860{
2861 struct buffer *buf = &qc->tx.buf;
2862 if (!b_alloc(buf))
2863 return NULL;
2864
2865 return buf;
2866}
2867
2868/* Free Tx buffer from <qc> if it is empty. */
2869static void qc_txb_release(struct quic_conn *qc)
2870{
2871 struct buffer *buf = &qc->tx.buf;
2872
2873 /* For the moment sending function is responsible to purge the buffer
2874 * entirely. It may change in the future but this requires to be able
2875 * to reuse old data.
2876 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002877 BUG_ON_HOT(buf && b_data(buf));
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002878
2879 if (!b_data(buf)) {
2880 b_free(buf);
2881 offer_buffers(NULL, 1);
2882 }
2883}
2884
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002885/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
2886 * must contains the address of the first packet stored in the payload.
2887 *
2888 * Caller is responsible that there is enough space in the buffer.
2889 */
2890static void qc_txb_store(struct buffer *buf, uint16_t length,
2891 struct quic_tx_packet *first_pkt)
2892{
2893 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
2894 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
2895
2896 write_u16(b_tail(buf), length);
2897 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
2898 b_add(buf, hdlen + length);
2899}
2900
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002901/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2902 * with <frms> as ack-eliciting frame list to send, 0 if not.
2903 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2904 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002905 * <force_ack> may be set to 1 if you want to force an ack.
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002906 */
2907static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002908 struct quic_enc_level *qel, int cc, int probe, int force_ack)
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002909{
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002910 unsigned int must_ack = force_ack ||
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002911 (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED));
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002912
2913 /* Do not build any more packet if the TX secrets are not available or
2914 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2915 * and if there is no more packets to send upon PTO expiration
2916 * and if there is no more ack-eliciting frames to send or in flight
2917 * congestion control limit is reached for prepared data
2918 */
2919 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2920 (!cc && !probe && !must_ack &&
2921 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002922 return 0;
2923 }
2924
2925 return 1;
2926}
2927
Amaury Denoyellef2476052022-08-04 16:19:57 +02002928/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2929 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
2930 *
2931 * Each datagram is prepended by a two fields header : the datagram length and
2932 * the address of the packet contained in the datagram.
2933 *
2934 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2935 * -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002936 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002937static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002938 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002939{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002940 int ret = -1;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002941 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002942 unsigned char *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002943 struct quic_tx_packet *pkt;
2944 size_t total;
Amaury Denoyellef2476052022-08-04 16:19:57 +02002945 /* Each datagram is prepended with its length followed by the address
2946 * of the first packet in the datagram.
2947 */
2948 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002949
2950 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002951
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002952 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2953 total = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002954 pos = (unsigned char *)b_tail(buf);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002955 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002956 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002957
2958 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002959 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002960 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002961 /* We do not probe if an immediate close was asked */
2962 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002963 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002964
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002965 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002966 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002967
2968 /* Leave room for the datagram header */
2969 pos += dg_headlen;
2970 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002971 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 +01002972 }
2973 else {
2974 end = pos + qc->path->mtu;
2975 }
2976
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002977 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
2978 QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002979 switch (err) {
2980 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002981 // trace already emitted by function above
2982 goto leave;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002983 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002984 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2985 * MTU, we are here because of the congestion control window. There is
2986 * no need to try to reuse this buffer.
2987 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002988 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002989 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002990 default:
2991 break;
2992 }
2993
2994 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002995 BUG_ON(!pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002996
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002997 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2998 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2999
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003000 total += pkt->len;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003001
Amaury Denoyellef2476052022-08-04 16:19:57 +02003002 /* Write datagram header. */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003003 qc_txb_store(buf, pkt->len, pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003004 }
3005
3006 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003007 ret = total;
3008 leave:
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003009 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003010 return ret;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003011}
3012
Amaury Denoyellef2476052022-08-04 16:19:57 +02003013/* Prepare as much as possible QUIC packets for sending from prebuilt frames
3014 * <frms>. Several packets can be regrouped in a single datagram. The result is
3015 * written into <buf>.
3016 *
3017 * Each datagram is prepended by a two fields header : the datagram length and
3018 * the address of first packet in the datagram.
3019 *
3020 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3021 * -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003022 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003023static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003024 enum quic_tls_enc_level tel, struct list *tel_frms,
3025 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003027 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003028 unsigned char *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003029 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3030 /* length of datagrams */
3031 uint16_t dglen;
3032 size_t total;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003033 int ret = -1, padding;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003034 /* Each datagram is prepended with its length followed by the address
3035 * of the first packet in the datagram.
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003036 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003037 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003038 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003039
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003040 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3041
Amaury Denoyellef2476052022-08-04 16:19:57 +02003042 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
3043 * caller must ensure that buf is resetted.
3044 */
3045 BUG_ON_HOT(buf->head || buf->data);
3046
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003047 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003048 qel = &qc->els[tel];
3049 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003050 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003051 padding = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003052 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003053 first_pkt = prv_pkt = NULL;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003054 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003055 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003056 enum quic_pkt_type pkt_type;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003057 struct quic_tls_ctx *tls_ctx;
3058 const struct quic_version *ver;
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003059 int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3060 (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3061 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003062
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003063 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003064 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003065 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003066 /* We do not probe if an immediate close was asked */
3067 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01003068 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003069
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003070 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003071 if (prv_pkt)
3072 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003073 /* Let's select the next encryption level */
3074 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3075 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003076 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003077 qel = &qc->els[tel];
3078 /* Build a new datagram */
3079 prv_pkt = NULL;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003080 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003081 continue;
3082 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003083 break;
3084 }
3085
3086 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003087 if (!prv_pkt) {
3088 /* Leave room for the datagram header */
3089 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003090 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02003091 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 +01003092 }
3093 else {
3094 end = pos + qc->path->mtu;
3095 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003096 }
3097
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003098 if (qc->negotiated_version) {
3099 ver = qc->negotiated_version;
3100 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3101 tls_ctx = &qc->negotiated_ictx;
3102 else
3103 tls_ctx = &qel->tls_ctx;
3104 }
3105 else {
3106 ver = qc->original_version;
3107 tls_ctx = &qel->tls_ctx;
3108 }
3109
3110 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02003111 qc, ver, dglen, pkt_type,
3112 force_ack, padding, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003113 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003114 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003115 // trace already emitted by function above
3116 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003117 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003118 /* If there was already a correct packet present, set the
3119 * current datagram as prepared into <cbuf>.
3120 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003121 if (prv_pkt)
3122 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003123 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003124 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003125 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01003126 break;
3127 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02003128
Frédéric Lécaille63556772021-12-29 17:18:21 +01003129 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003130 BUG_ON(!cur_pkt);
Frédéric Lécaille63556772021-12-29 17:18:21 +01003131
Frédéric Lécaille1809c332022-04-25 10:24:12 +02003132 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3133 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3134
Frédéric Lécaille63556772021-12-29 17:18:21 +01003135 total += cur_pkt->len;
3136 /* keep trace of the first packet in the datagram */
3137 if (!first_pkt)
3138 first_pkt = cur_pkt;
3139 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003140 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003141 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003142 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3143 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01003144 /* Let's say we have to build a new dgram */
3145 prv_pkt = NULL;
3146 dglen += cur_pkt->len;
3147 /* Client: discard the Initial encryption keys as soon as
3148 * a handshake packet could be built.
3149 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003150 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01003151 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3152 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3153 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3154 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3155 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01003156 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003157 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003158 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003159 }
3160 /* If the data for the current encryption level have all been sent,
3161 * select the next level.
3162 */
3163 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003164 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 +01003165 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3166 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3167 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3168 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003169 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3170 frms = &qc->els[tel].pktns->tx.frms;
3171 else
3172 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003173 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003174 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003175 /* If there is data for the next level, do not
3176 * consume a datagram.
3177 */
3178 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003179 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003180 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02003181
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003182 /* If we have to build a new datagram, set the current datagram as
3183 * prepared into <cbuf>.
3184 */
3185 if (!prv_pkt) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003186 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003187 first_pkt = NULL;
3188 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003189 padding = 0;
3190 }
3191 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003192 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003193 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3194 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003195 }
3196 }
3197
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003198 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003199 ret = total;
3200 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003201 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003202 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003203}
3204
Amaury Denoyellef2476052022-08-04 16:19:57 +02003205/* Send datagrams stored in <buf>.
3206 *
3207 * This function always returns 1 for success. Even if sendto() syscall failed,
3208 * buffer is drained and packets are considered as emitted. QUIC loss detection
3209 * mechanism is used as a back door way to retry sending.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003210 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003211int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003212{
3213 struct quic_conn *qc;
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003214 char skip_sendto = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003216 qc = ctx->qc;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003217 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003218 while (b_contig_data(buf, 0)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003219 unsigned char *pos;
3220 struct buffer tmpbuf = { };
3221 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3222 uint16_t dglen;
3223 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003224 unsigned int time_sent;
3225
Amaury Denoyellef2476052022-08-04 16:19:57 +02003226 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003227 dglen = read_u16(pos);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003228 BUG_ON_HOT(!dglen); /* this should not happen */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003229
3230 pos += sizeof dglen;
3231 first_pkt = read_ptr(pos);
3232 pos += sizeof first_pkt;
3233 tmpbuf.area = (char *)pos;
3234 tmpbuf.size = tmpbuf.data = dglen;
3235
Frédéric Lécaille3a561372022-08-29 18:05:44 +02003236 TRACE_DATA("send dgram", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003237 /* If sendto is on error just skip the call to it for the rest
3238 * of the loop but continue to purge the buffer. Data will be
3239 * transmitted when QUIC packets are detected as lost on our
3240 * side.
3241 *
3242 * TODO use fd-monitoring to detect when send operation can be
3243 * retry. This should improve the bandwidth without relying on
3244 * retransmission timer. However, it requires a major rework on
3245 * quic-conn fd management.
3246 */
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003247 if (!skip_sendto) {
3248 if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) {
3249 skip_sendto = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003250 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003251 }
3252 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003253
Amaury Denoyellef2476052022-08-04 16:19:57 +02003254 b_del(buf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003255 qc->tx.bytes += tmpbuf.data;
3256 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003257
3258 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3259 pkt->time_sent = time_sent;
3260 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3261 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003262 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003263 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3264 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003265 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003266 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3267 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3268 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003269 qc_notify_close(qc);
3270
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003271 /* RFC 9000 10.2. Immediate Close:
3272 * The closing and draining connection states exist to ensure
3273 * that connections close cleanly and that delayed or reordered
3274 * packets are properly discarded. These states SHOULD persist
3275 * for at least three times the current PTO interval...
3276 *
3277 * Rearm the idle timeout only one time when entering closing
3278 * state.
3279 */
3280 qc_idle_timer_do_rearm(qc);
3281 if (qc->timer_task) {
3282 task_destroy(qc->timer_task);
3283 qc->timer_task = NULL;
3284 }
3285 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003286 qc->path->in_flight += pkt->in_flight_len;
3287 pkt->pktns->tx.in_flight += pkt->in_flight_len;
Frédéric Lécaille5f6c25e2022-08-24 16:23:44 +02003288 if (pkt->in_flight_len)
Frédéric Lécailleda9c4412022-08-22 18:47:51 +02003289 qc_set_timer(qc);
Frédéric Lécaille5f6c25e2022-08-24 16:23:44 +02003290 TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
3291 next_pkt = pkt->next;
3292 quic_tx_packet_refinc(pkt);
3293 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003294 }
3295 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003296
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003297 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3298
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003299 return 1;
3300}
3301
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003302/* Copy into <buf> buffer a stateless reset token depending on the
3303 * <salt> salt input. This is the cluster secret which will be derived
3304 * as HKDF input secret to generate this token.
3305 * Return 1 if succeeded, 0 if not.
3306 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003307static int quic_stateless_reset_token_cpy(struct quic_conn *qc,
3308 unsigned char *buf, size_t len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003309 const unsigned char *salt, size_t saltlen)
3310{
3311 /* Input secret */
3312 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3313 size_t keylen = strlen(global.cluster_secret);
3314 /* Info */
3315 const unsigned char label[] = "stateless token";
3316 size_t labellen = sizeof label - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003317 int ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003318
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003319 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3320
3321 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003322 key, keylen, salt, saltlen, label, labellen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003323 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3324 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003325}
3326
3327/* Initialize the stateless reset token attached to <cid> connection ID.
3328 * Returns 1 if succeeded, 0 if not.
3329 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003330static int quic_stateless_reset_token_init(struct quic_conn *qc,
3331 struct quic_connection_id *quic_cid)
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003332{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003333 int ret;
3334
3335 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3336
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003337 if (global.cluster_secret) {
3338 /* Output secret */
3339 unsigned char *token = quic_cid->stateless_reset_token;
3340 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3341 /* Salt */
3342 const unsigned char *cid = quic_cid->cid.data;
3343 size_t cidlen = quic_cid->cid.len;
3344
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003345 ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003346 }
3347 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003348 /* TODO: RAND_bytes() should be replaced */
3349 ret = RAND_bytes(quic_cid->stateless_reset_token,
3350 sizeof quic_cid->stateless_reset_token) == 1;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003351 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003352
3353 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3354 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003355}
3356
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003357/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3358 * ebtree.
3359 *
3360 * The CID is randomly generated in part with the result altered to be
3361 * associated with the current thread ID. This means this function must only
3362 * be called by the quic_conn thread.
3363 *
3364 * Returns the new CID if succeeded, NULL if not.
3365 */
3366static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3367 struct quic_conn *qc,
3368 int seq_num)
3369{
3370 struct quic_connection_id *cid;
3371
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003372 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3373
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003374 cid = pool_alloc(pool_head_quic_connection_id);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003375 if (!cid) {
3376 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
3377 goto err;
3378 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003379
3380 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003381 /* TODO: RAND_bytes() should be replaced */
3382 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) {
3383 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003384 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003385 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003386
3387 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003388 if (quic_stateless_reset_token_init(qc, cid) != 1) {
3389 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003390 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003391 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003392
3393 cid->qc = qc;
3394
3395 cid->seq_num.key = seq_num;
3396 cid->retire_prior_to = 0;
3397 /* insert the allocated CID in the quic_conn tree */
3398 eb64_insert(root, &cid->seq_num);
3399
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003400 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003401 return cid;
3402
3403 err:
3404 pool_free(pool_head_quic_connection_id, cid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003405 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003406 return NULL;
3407}
3408
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003409/* Build all the frames which must be sent just after the handshake have succeeded.
3410 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3411 * a HANDSHAKE_DONE frame.
3412 * Return 1 if succeeded, 0 if not.
3413 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003414static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003415{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003416 int ret = 0, i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003417 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003418 struct quic_frame *frm, *frmbak;
3419 struct list frm_list = LIST_HEAD_INIT(frm_list);
3420 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003421
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003422 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
3423
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003424 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003425 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003426 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003427 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003428 if (!frm) {
3429 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3430 goto leave;
3431 }
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003432
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003433 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003434 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003435 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003436 }
3437
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003438 /* Initialize <max> connection IDs minus one: there is
3439 * already one connection ID used for the current connection.
3440 */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003441 first = 1;
3442 max = qc->tx.params.active_connection_id_limit;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003443
3444 /* TODO: check limit */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003445 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003446 struct quic_connection_id *cid;
3447
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003448 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003449 if (!frm) {
3450 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003451 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003452 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003453
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003454 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003455 cid = new_quic_cid(&qc->cids, qc, i);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003456 if (!cid) {
Frédéric Lécaillebccbad22022-08-31 15:02:53 +02003457 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003458 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003459 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003460 }
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003461
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003462 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003463 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003464
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003465 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003466 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003467 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003468
3469 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003470 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003471
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003472 ret = 1;
3473 leave:
3474 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
3475 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003476
3477 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003478 /* free the frames */
3479 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3480 pool_free(pool_head_quic_frame, frm);
3481
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003482 node = eb64_lookup_ge(&qc->cids, first);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003483 while (node) {
3484 struct quic_connection_id *cid;
3485
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003486 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003487 if (cid->seq_num.key >= max)
3488 break;
3489
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003490 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003491 ebmb_delete(&cid->node);
3492 eb64_delete(&cid->seq_num);
3493 pool_free(pool_head_quic_connection_id, cid);
3494 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003495 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003496}
3497
3498/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003499void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003500{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003501 struct eb64_node *n;
3502 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003503
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003504 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
3505
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003506 n = eb64_first(&arngs->root);
3507 while (n) {
3508 struct eb64_node *next;
3509
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003510 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003511 next = eb64_next(n);
3512 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003513 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003514 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003515 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003516
3517 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003518}
3519
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003520/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3521 * descending order.
3522 */
3523static inline size_t sack_gap(struct quic_arng_node *p,
3524 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003525{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003526 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003527}
3528
3529
3530/* Remove the last elements of <ack_ranges> list of ack range updating its
3531 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003532 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003533 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003534static int quic_rm_last_ack_ranges(struct quic_conn *qc,
3535 struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003536{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003537 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003538 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003539
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003540 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3541
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003542 last = eb64_last(&arngs->root);
3543 while (last && arngs->enc_sz > limit) {
3544 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003545
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003546 prev = eb64_prev(last);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003547 if (!prev) {
3548 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
3549 goto out;
3550 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003551
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003552 last_node = eb64_entry(last, struct quic_arng_node, first);
3553 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003554 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3555 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3556 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3557 --arngs->sz;
3558 eb64_delete(last);
3559 pool_free(pool_head_quic_arng, last);
3560 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003561 }
3562
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003563 ret = 1;
3564 out:
3565 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3566 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003567}
3568
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003569/* Set the encoded size of <arngs> QUIC ack ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003570static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003571{
3572 struct eb64_node *node, *next;
3573 struct quic_arng_node *ar, *ar_next;
3574
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003575 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3576
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003577 node = eb64_last(&arngs->root);
3578 if (!node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003579 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003580
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003581 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003582 arngs->enc_sz = quic_int_getsize(ar->last) +
3583 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3584
3585 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003586 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003587 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3588 quic_int_getsize(ar_next->last - ar_next->first.key);
3589 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003590 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003591 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003592
3593 leave:
3594 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003595}
3596
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003597/* Insert <ar> ack range into <argns> tree of ack ranges.
3598 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003599 */
3600static inline
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003601struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
3602 struct quic_arngs *arngs,
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003603 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003604{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003605 struct quic_arng_node *new_ar;
3606
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003607 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3608
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003609 new_ar = pool_alloc(pool_head_quic_arng);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003610 if (!new_ar) {
3611 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
3612 goto leave;
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003613 }
3614
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003615 new_ar->first.key = ar->first;
3616 new_ar->last = ar->last;
3617 eb64_insert(&arngs->root, &new_ar->first);
3618 arngs->sz++;
3619
3620 leave:
3621 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003622 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003623}
3624
3625/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003626 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003627 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003628 *
3629 * Descending order
3630 * ------------->
3631 * range1 range2
3632 * ..........|--------|..............|--------|
3633 * ^ ^ ^ ^
3634 * | | | |
3635 * last1 first1 last2 first2
3636 * ..........+--------+--------------+--------+......
3637 * diff1 gap12 diff2
3638 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003639 * To encode the previous list of ranges we must encode integers as follows in
3640 * descending order:
3641 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003642 * with diff1 = last1 - first1
3643 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003644 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003645 *
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003646
3647returns 0 on error
3648
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003649 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003650int quic_update_ack_ranges_list(struct quic_conn *qc,
3651 struct quic_arngs *arngs,
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003652 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003653{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003654 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003655 struct eb64_node *le;
3656 struct quic_arng_node *new_node;
3657 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003658
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003659 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3660
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003661 new = NULL;
3662 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003663 new_node = quic_insert_new_range(qc, arngs, ar);
3664 if (new_node)
3665 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003666
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003667 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003668 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003669
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003670 le = eb64_lookup_le(&arngs->root, ar->first);
3671 if (!le) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003672 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003673 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003674 goto leave;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003675
3676 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003677 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003678 else {
3679 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003680 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003681
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003682 /* Already existing range */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003683 if (le_ar->last >= ar->last) {
3684 ret = 1;
3685 }
3686 else if (le_ar->last + 1 >= ar->first) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003687 le_ar->last = ar->last;
3688 new = le;
3689 new_node = le_ar;
3690 }
3691 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003692 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003693 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003694 goto leave;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003695
3696 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003697 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003698 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003699
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003700 /* Verify that the new inserted node does not overlap the nodes
3701 * which follow it.
3702 */
3703 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003704 struct eb64_node *next;
3705 struct quic_arng_node *next_node;
3706
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003707 while ((next = eb64_next(new))) {
3708 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003709 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003710 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003711 break;
3712
3713 if (next_node->last > new_node->last)
3714 new_node->last = next_node->last;
3715 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003716 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003717 /* Decrement the size of these ranges. */
3718 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003719 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003720 }
3721
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003722 ret = 1;
3723 leave:
3724 quic_arngs_set_enc_sz(qc, arngs);
3725 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
3726 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003727}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003728/* Remove the header protection of packets at <el> encryption level.
3729 * Always succeeds.
3730 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003731static 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 +01003732{
3733 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003734 struct quic_rx_packet *pqpkt, *pkttmp;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003735 struct quic_enc_level *app_qel;
3736
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003737 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3738 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003739 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003740 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003741 TRACE_DEVEL("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003742 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003743 goto out;
3744 }
3745 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003746 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003747 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02003748 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003749 TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003750 }
3751 else {
3752 /* The AAD includes the packet number field */
3753 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3754 /* Store the packet into the tree of packets to decrypt. */
3755 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003756 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3757 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003758 TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003759 }
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003760 LIST_DELETE(&pqpkt->list);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003761 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003762 }
3763
3764 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003765 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003766}
3767
3768/* Process all the CRYPTO frame at <el> encryption level.
3769 * Return 1 if succeeded, 0 if not.
3770 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003771static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
3772 struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003773 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003775 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003776 struct eb64_node *node;
3777
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003778 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3779
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003780 node = eb64_first(&el->rx.crypto.frms);
3781 while (node) {
3782 struct quic_rx_crypto_frm *cf;
3783
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003784 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003785 if (cf->offset_node.key != el->rx.crypto.offset)
3786 break;
3787
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003788 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) {
3789 TRACE_ERROR("qc_provide_cdata() failed", QUIC_EV_CONN_TRMHP);
3790 goto leave;
3791 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792
3793 node = eb64_next(node);
3794 quic_rx_packet_refdec(cf->pkt);
3795 eb64_delete(&cf->offset_node);
3796 pool_free(pool_head_quic_rx_crypto_frm, cf);
3797 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003798
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003799 ret = 1;
3800 leave:
3801 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3802 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003803}
3804
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003805/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003806 * 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 +02003807 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003808 * Return 1 if succeeded, 0 if not.
3809 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003810int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3811 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003812{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003813 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003815 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003816 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003817 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003818 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003819
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003820 TRACE_ENTER(QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003821 qel = cur_el;
3822 next_tel:
3823 if (!qel)
3824 goto out;
3825
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003826 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003827 while (node) {
3828 struct quic_rx_packet *pkt;
3829
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003830 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003831 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003832 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003833 if (!qc_pkt_decrypt(pkt, qel, qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003834 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003835 TRACE_ERROR("packet decryption failed -> dropped",
3836 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003837 }
3838 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003839 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003840 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003841 TRACE_ERROR("packet parsing failed -> dropped",
3842 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02003843 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003844 }
3845 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003846 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3847
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003848 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3849 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3850 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003851 qc_idle_timer_rearm(qc, 1);
3852 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003853 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003854 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003855 largest_pn_time_received = pkt->time_received;
3856 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003857 /* Update the list of ranges to acknowledge. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003858 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
3859 TRACE_ERROR("Could not update ack range list",
3860 QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003861 }
3862 }
3863 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003864 eb64_delete(&pkt->pn_node);
3865 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003866 }
3867
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003868 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3869 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003870 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003871 /* Update the largest acknowledged packet timestamps */
3872 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3873 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3874 }
3875
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003876 if (!qc_treat_rx_crypto_frms(qc, qel, ctx)) {
3877 // trace already emitted by function above
3878 goto leave;
3879 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003880
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003881 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003882 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003883 qel = next_el;
Frédéric Lécaille91a211f2022-05-24 10:54:42 +02003884 largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003885 goto next_tel;
3886 }
3887
3888 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003889 ret = 1;
3890 leave:
3891 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, ctx->qc);
3892 return ret;
3893}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003894
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003895/* Check if it's possible to remove header protection for packets related to
3896 * encryption level <qel>. If <qel> is NULL, assume it's false.
3897 *
3898 * Return true if the operation is possible else false.
3899 */
3900static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3901{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003902 int ret = 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003903 enum quic_tls_enc_level tel;
3904
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003905 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3906
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003907 if (!qel)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003908 goto cant_rm_hp;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003909
3910 tel = ssl_to_quic_enc_level(qel->level);
3911
3912 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003913 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003914 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003915 goto cant_rm_hp;
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003916 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003917
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003918 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3919 TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc);
3920 goto cant_rm_hp;
3921 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003922
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003923 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003924 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003925 qc->mux_state == QC_MUX_NULL) {
3926 TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
3927 goto cant_rm_hp;
3928 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003929
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003930 ret = 1;
3931 cant_rm_hp:
3932 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3933 return ret;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003934}
3935
Amaury Denoyellecc130472022-08-17 10:08:16 +02003936/* Try to send application frames from list <frms> on connection <qc>.
3937 *
Amaury Denoyelle70467562022-08-17 16:33:13 +02003938 * Use qc_send_app_probing wrapper when probing with old data.
Amaury Denoyellecc130472022-08-17 10:08:16 +02003939 *
3940 * Returns 1 on success. Some data might not have been sent due to congestion,
3941 * in this case they are left in <frms> input list. The caller may subscribe on
3942 * quic-conn to retry later.
3943 *
3944 * Returns 0 on critical error.
3945 * TODO review and classify more distinctly transient from definitive errors to
3946 * allow callers to properly handle it.
3947 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003948static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003949{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003950 int status = 0;
3951 struct buffer *buf;
3952
3953 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3954
3955 buf = qc_txb_alloc(qc);
3956 if (!buf) {
3957 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
3958 goto leave;
3959 }
3960
Frédéric Lécailledc077512022-07-26 09:17:19 +02003961 /* Prepare and send packets until we could not further prepare packets. */
3962 while (1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003963 int ret;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003964 /* Currently buf cannot be non-empty at this stage. Even if a
3965 * previous sendto() has failed it is emptied to simulate
3966 * packet emission and rely on QUIC lost detection to try to
3967 * emit it.
3968 */
3969 BUG_ON_HOT(b_data(buf));
3970 b_reset(buf);
3971
Amaury Denoyellef2476052022-08-04 16:19:57 +02003972 ret = qc_prep_app_pkts(qc, buf, frms);
Frédéric Lécailledc077512022-07-26 09:17:19 +02003973 if (ret == -1)
3974 goto err;
3975 else if (ret == 0)
3976 goto out;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003977
Amaury Denoyellef2476052022-08-04 16:19:57 +02003978 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécailledc077512022-07-26 09:17:19 +02003979 goto err;
3980 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003981
3982 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003983 status = 1;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003984 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003985 leave:
3986 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3987 return status;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003988
3989 err:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003990 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003991 goto leave;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003992}
3993
Amaury Denoyellecc130472022-08-17 10:08:16 +02003994/* Try to send application frames from list <frms> on connection <qc>. Use this
Amaury Denoyelle70467562022-08-17 16:33:13 +02003995 * function when probing is required.
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003996 *
3997 * Returns the result from qc_send_app_pkts function.
3998 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003999static forceinline int qc_send_app_probing(struct quic_conn *qc,
4000 struct list *frms)
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02004001{
4002 int ret;
4003
4004 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4005
Amaury Denoyelle70467562022-08-17 16:33:13 +02004006 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
4007 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02004008 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02004009 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02004010
4011 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4012 return ret;
4013}
4014
Amaury Denoyelle70467562022-08-17 16:33:13 +02004015/* Try to send application frames from list <frms> on connection <qc>. This
4016 * function is provided for MUX upper layer usage only.
Amaury Denoyellecc130472022-08-17 10:08:16 +02004017 *
4018 * Returns the result from qc_send_app_pkts function.
4019 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02004020int qc_send_mux(struct quic_conn *qc, struct list *frms)
Amaury Denoyellecc130472022-08-17 10:08:16 +02004021{
4022 int ret;
4023
4024 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle70467562022-08-17 16:33:13 +02004025 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 +02004026
Amaury Denoyelle70467562022-08-17 16:33:13 +02004027 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4028 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004029 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02004030 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004031
4032 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4033 return ret;
4034}
4035
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004036/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4037 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004038 * QUIC connection. <old_data> is used as boolean to send data already sent but
4039 * not already acknowledged (in flight).
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004040 * Returns 1 if succeeded, 0 if not.
4041 */
4042int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4043 enum quic_tls_enc_level tel, struct list *tel_frms,
4044 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4045{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004046 int ret, status = 0;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004047 struct buffer *buf = qc_txb_alloc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004048
4049 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4050
4051 if (!buf) {
4052 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4053 goto leave;
4054 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004055
Amaury Denoyellef2476052022-08-04 16:19:57 +02004056 /* Currently buf cannot be non-empty at this stage. Even if a previous
4057 * sendto() has failed it is emptied to simulate packet emission and
4058 * rely on QUIC lost detection to try to emit it.
4059 */
4060 BUG_ON_HOT(b_data(buf));
4061 b_reset(buf);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004062
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004063 if (old_data) {
4064 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004065 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004066 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02004067
4068 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004069 if (ret == -1)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004070 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004071 else if (ret == 0)
4072 goto skip_send;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004073
Amaury Denoyellef2476052022-08-04 16:19:57 +02004074 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004075 goto out;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004076
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004077 skip_send:
4078 status = 1;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004079 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004080 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004081 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004082 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004083 leave:
4084 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4085 return status;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004086}
4087
4088/* Retransmit up to two datagrams depending on packet number space */
4089static void qc_dgrams_retransmit(struct quic_conn *qc)
4090{
4091 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4092 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4093 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4094
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004095 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4096
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004097 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4098 struct list ifrms = LIST_HEAD_INIT(ifrms);
4099 struct list hfrms = LIST_HEAD_INIT(hfrms);
4100
4101 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004102 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4103 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004104 if (!LIST_ISEMPTY(&ifrms)) {
4105 iqel->pktns->tx.pto_probe = 1;
4106 if (!LIST_ISEMPTY(&hfrms)) {
4107 hqel->pktns->tx.pto_probe = 1;
4108 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4109 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004110 /* Put back unsent frames in their packet number spaces */
4111 LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms);
4112 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004113 }
4114 }
4115 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004116 /* This list has potentially been already used and spliced
4117 * to another one attached to the connection. We must reinitialize it.
4118 */
4119 LIST_INIT(&hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004120 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004121 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004122 if (!LIST_ISEMPTY(&hfrms)) {
4123 hqel->pktns->tx.pto_probe = 1;
4124 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
4125 QUIC_TLS_ENC_LEVEL_NONE, NULL);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004126 /* Put back unsent frames into their packet number spaces */
4127 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004128 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004129 TRACE_STATE("no more need to probe Handshake packet number space",
4130 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004131 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4132 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004133 TRACE_STATE("no more need to probe Initial packet number space",
4134 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004135 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4136 }
4137 else {
4138 int i;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004139
4140 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004141 struct list frms1 = LIST_HEAD_INIT(frms1);
4142
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004143 hqel->pktns->tx.pto_probe = 0;
4144 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4145 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004146 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004147 if (!LIST_ISEMPTY(&frms1)) {
4148 hqel->pktns->tx.pto_probe = 1;
4149 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4150 QUIC_TLS_ENC_LEVEL_NONE, NULL);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004151 /* Put back unsent frames into their packet number spaces */
4152 LIST_SPLICE(&hqel->pktns->tx.frms, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004153 }
4154 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004155 TRACE_STATE("no more need to probe Handshake packet number space",
4156 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004157 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4158 }
4159 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004160 struct list frms2 = LIST_HEAD_INIT(frms2);
4161 struct list frms1 = LIST_HEAD_INIT(frms1);
4162
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004163 aqel->pktns->tx.pto_probe = 0;
4164 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4165 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4166 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4167 if (!LIST_ISEMPTY(&frms1)) {
4168 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004169 qc_send_app_probing(qc, &frms1);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004170 /* Put back unsent frames into their packet number spaces */
4171 LIST_SPLICE(&aqel->pktns->tx.frms, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004172 }
4173 if (!LIST_ISEMPTY(&frms2)) {
4174 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004175 qc_send_app_probing(qc, &frms2);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004176 /* Put back unsent frames into their packet number spaces */
4177 LIST_SPLICE(&aqel->pktns->tx.frms, &frms2);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004178 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004179 TRACE_STATE("no more need to probe 01RTT packet number space",
4180 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004181 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4182 }
4183 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004184 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004185}
4186
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004187/* QUIC connection packet handler task (post handshake) */
Willy Tarreau41e701e2022-09-08 15:12:59 +02004188struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004189{
4190 struct ssl_sock_ctx *ctx;
4191 struct quic_conn *qc;
4192 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004193
4194
4195 ctx = context;
4196 qc = ctx->qc;
4197 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004198
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004199 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4200 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004201
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004202 /* Retranmissions */
4203 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004204 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004205 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4206 qc_dgrams_retransmit(qc);
4207 }
4208
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004209 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004210 qc_rm_hp_pkts(qc, qel);
4211
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004212 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) {
4213 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4214 goto out;
4215 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004216
Frédéric Lécaille47756802022-03-25 09:12:16 +01004217 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004218 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4219 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01004220 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004221 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004222
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004223 /* XXX TODO: how to limit the list frames to send */
Amaury Denoyelle70467562022-08-17 16:33:13 +02004224 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
4225 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004226 goto out;
4227 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004228
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004229 out:
4230 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004231 return t;
4232}
4233
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004234/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4235static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4236{
4237 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
4238 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
4239 qel->pktns->tx.pto_probe ||
4240 !LIST_ISEMPTY(&qel->pktns->tx.frms);
4241}
4242
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004243/* QUIC connection packet handler task. */
4244struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004245{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004246 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004247 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004248 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004249 enum quic_tls_enc_level tel, next_tel;
4250 struct quic_enc_level *qel, *next_qel;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004251 struct buffer *buf = NULL;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004252 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004253
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004254 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004255 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004256 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004257 st = qc->state;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004258 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004259
4260 /* Retranmissions */
4261 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004262 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004263 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4264 qc_dgrams_retransmit(qc);
4265 }
4266
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004267 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
4268 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004269 TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached",
4270 QUIC_EV_CONN_IO_CB, qc);
4271 /* 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 +01004272 * after the anti-amplification was reached.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004273 *
4274 * TODO: this part should be removed. This was there because the
4275 * datagram parser was not executed by only one thread.
Frédéric Lécaille59507de2022-08-11 12:14:07 +02004276 */
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004277 qc_set_timer(qc);
4278 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
4279 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
4280 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004281 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004282 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004283 (!LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004284 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004285 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004286 if (st >= QUIC_HS_ST_COMPLETE &&
4287 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004288 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004289 /* There may be remaining Handshake packets to treat and acknowledge. */
4290 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4291 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4292 }
4293 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
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écaillea5fe49f2021-06-04 11:52:35 +02004296 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02004297 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004298
4299 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004300 /* Treat packets waiting for header packet protection decryption */
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004301 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004302 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004303
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004304 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
4305 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4306 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004307 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004308
Frédéric Lécaille47756802022-03-25 09:12:16 +01004309 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4310 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4311 goto out;
4312
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004313 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004314 !LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004315 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
4316 qel = next_qel;
4317 next_qel = NULL;
4318 goto next_level;
4319 }
4320 else {
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004321 struct quic_rx_packet *pkt, *pkttmp;
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004322 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
4323
4324 /* Drop these 0-RTT packets */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004325 TRACE_DEVEL("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004326 list_for_each_entry_safe(pkt, pkttmp, &aqel->rx.pqpkts, list) {
4327 LIST_DELETE(&pkt->list);
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004328 quic_rx_packet_refdec(pkt);
4329 }
4330 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004331 }
4332
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004333 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004334 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004335 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004336 !quic_build_post_handshake_frames(qc))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004337 goto out;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01004338
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004339 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004340 QUIC_FL_TLS_SECRETS_DCD)) {
4341 /* Discard the Handshake keys. */
4342 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
4343 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
4344 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
4345 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004346 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004347 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004348 }
4349
4350 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
4351 /* There may be remaining handshake to build (acks) */
4352 st = QUIC_HS_ST_SERVER_HANDSHAKE;
4353 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004354 }
4355
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01004356 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
4357 * be considered.
4358 */
4359 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004360 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004361
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004362 if (!qc_need_sending(qc, qel) &&
4363 (!next_qel || !qc_need_sending(qc, next_qel))) {
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004364 goto skip_send;
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004365 }
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004366
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004367 buf = qc_txb_alloc(qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02004368 if (!buf)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004369 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004370
4371 /* Currently buf cannot be non-empty at this stage. Even if a previous
4372 * sendto() has failed it is emptied to simulate packet emission and
4373 * rely on QUIC lost detection to try to emit it.
4374 */
4375 BUG_ON_HOT(b_data(buf));
4376 b_reset(buf);
4377
4378 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02004379 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004380 if (ret == -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004381 goto out;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004382 else if (ret == 0)
4383 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004384
Amaury Denoyellef2476052022-08-04 16:19:57 +02004385 if (!qc_send_ppkts(buf, ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004386 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004387
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004388 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004389 /* Check if there is something to do for the next level.
4390 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004391 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004392 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004393 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004394 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004395 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004396 goto next_level;
4397 }
4398
Frédéric Lécaille47756802022-03-25 09:12:16 +01004399 out:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004400 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004401 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004402 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004403}
4404
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004405/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004406static 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 +01004407{
4408 int i;
4409
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004410 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4411
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004412 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4413 if (qel->tx.crypto.bufs[i]) {
4414 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4415 qel->tx.crypto.bufs[i] = NULL;
4416 }
4417 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004418 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004419
4420 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004421}
4422
4423/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004424 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004425 * Returns 1 if succeeded, 0 if not.
4426 */
4427static int quic_conn_enc_level_init(struct quic_conn *qc,
4428 enum quic_tls_enc_level level)
4429{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004430 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004431 struct quic_enc_level *qel;
4432
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004433 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4434
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004435 qel = &qc->els[level];
4436 qel->level = quic_to_ssl_enc_level(level);
4437 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4438 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4439 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004440 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004441
4442 qel->rx.pkts = EB_ROOT;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004443 LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004444 qel->rx.crypto.offset = 0;
4445 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004446
4447 /* Allocate only one buffer. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004448 /* TODO: use a pool */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004449 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4450 if (!qel->tx.crypto.bufs)
4451 goto err;
4452
4453 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4454 if (!qel->tx.crypto.bufs[0])
4455 goto err;
4456
4457 qel->tx.crypto.bufs[0]->sz = 0;
4458 qel->tx.crypto.nb_buf = 1;
4459
4460 qel->tx.crypto.sz = 0;
4461 qel->tx.crypto.offset = 0;
4462
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004463 ret = 1;
4464 leave:
4465 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4466 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004467
4468 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004469 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004470 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004471}
4472
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004473/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4474 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004475 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004476 * This function must only be called by the thread responsible of the quic_conn
4477 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004478 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004479static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004480{
4481 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004482 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004483 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004484 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004485 struct quic_rx_packet *pkt, *pktback;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004486
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004487 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4488
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004489 /* We must not free the quic-conn if the MUX is still allocated. */
4490 BUG_ON(qc->mux_state == QC_MUX_READY);
4491
Willy Tarreau54bc7862022-08-10 07:26:27 +02004492 /* in the unlikely (but possible) case the connection was just added to
4493 * the accept_list we must delete it from there.
4494 */
4495 MT_LIST_DELETE(&qc->accept_list);
4496
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004497 /* free remaining stream descriptors */
4498 node = eb64_first(&qc->streams_by_id);
4499 while (node) {
4500 struct qc_stream_desc *stream;
4501
4502 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4503 node = eb64_next(node);
4504
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004505 /* all streams attached to the quic-conn are released, so
4506 * qc_stream_desc_free will liberate the stream instance.
4507 */
4508 BUG_ON(!stream->release);
Frédéric Lécailleea4a5cb2022-08-20 18:59:36 +02004509 qc_stream_desc_free(stream, 1);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004510 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004511
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004512 /* Purge Rx packet list. */
4513 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
4514 LIST_DELETE(&pkt->qc_rx_pkt_list);
4515 pool_free(pool_head_quic_rx_packet, pkt);
4516 }
4517
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004518 if (qc->idle_timer_task) {
4519 task_destroy(qc->idle_timer_task);
4520 qc->idle_timer_task = NULL;
4521 }
4522
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004523 if (qc->timer_task) {
4524 task_destroy(qc->timer_task);
4525 qc->timer_task = NULL;
4526 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004527
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004528 /* remove the connection from receiver cids trees */
4529 ebmb_delete(&qc->odcid_node);
4530 ebmb_delete(&qc->scid_node);
4531 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004532
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004533 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004534 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004535 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004536 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004537 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004538 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004539
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004540 quic_tls_ku_free(qc);
4541 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4542 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004543 quic_conn_enc_level_uninit(qc, &qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004544 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004545 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004546
4547 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4548 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4549 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004550
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004551 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
Frédéric Lécaille8ddde4f2022-08-01 14:07:50 +02004552 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004553 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004554 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004555
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004556 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4557 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004558 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004559
4560 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004561}
4562
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004563static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004564{
4565 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004566 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004567
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004568 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004569
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004570 /* Next application data can be dropped. */
4571 qc->mux_state = QC_MUX_RELEASED;
4572
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004573 /* If the quic-conn timer has already expired free the quic-conn. */
4574 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4575 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004576 goto leave;
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004577 }
4578
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02004579 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004580 leave:
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004581 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004582}
4583
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004584/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau41e701e2022-09-08 15:12:59 +02004585struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004586{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004587 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004588 struct quic_conn *qc;
4589 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004590
4591 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004592 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004593 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004594 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004595 task->expire = TICK_ETERNITY;
4596 pktns = quic_loss_pktns(qc);
4597 if (tick_isset(pktns->tx.loss_time)) {
4598 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4599
4600 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
Frédéric Lécaille15773f22022-08-24 17:57:09 +02004601 if (!LIST_ISEMPTY(&lost_pkts))
4602 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécaille277c4622022-08-24 17:06:04 +02004603 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004604 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004605 goto out;
4606 }
4607
4608 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004609 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004610 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4611 qc->qcc->subs->events & SUB_RETRY_SEND) {
4612 struct qcc *qcc = qc->qcc;
4613
4614 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4615 tasklet_wakeup(qcc->subs->tasklet);
4616 qcc->subs->events &= ~SUB_RETRY_SEND;
4617 if (!qcc->subs->events)
4618 qcc->subs = NULL;
4619 }
4620 else {
4621 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4622 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4623 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004624 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4625 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004626 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004627 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4628 }
4629 }
4630 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
4631 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4632 }
4633 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
4634 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004635 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004636 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004637 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004638 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004639 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4640 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4641
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004642 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004643 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004644 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004645 iel->pktns->tx.pto_probe = 1;
4646 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004647
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004648 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4649 qc->path->loss.pto_count++;
4650
4651 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004652 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004653
4654 return task;
4655}
4656
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004657/* Parse the Retry token from buffer <token> with <end> a pointer to
4658 * one byte past the end of this buffer. This will extract the ODCID
4659 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004660 *
4661 * Returns 0 on success else non-zero.
4662 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004663static int parse_retry_token(struct quic_conn *qc,
4664 const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004665 struct quic_cid *odcid)
4666{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004667 int ret = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004668 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004669 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004670
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004671 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
4672
4673 if (!quic_dec_int(&odcid_len, &token, end)) {
4674 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
4675 goto leave;
4676 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004677
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004678 /* RFC 9000 7.2. Negotiating Connection IDs:
4679 * When an Initial packet is sent by a client that has not previously
4680 * received an Initial or Retry packet from the server, the client
4681 * populates the Destination Connection ID field with an unpredictable
4682 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4683 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004684 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
4685 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
4686 goto leave;
4687 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004688
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004689 if (end - token < odcid_len + sizeof timestamp) {
4690 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
4691 goto leave;
4692 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004693
4694 timestamp = ntohl(read_u32(token + odcid_len));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004695 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
4696 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
4697 goto leave;
4698 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004699
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004700 ret = 1;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004701 memcpy(odcid->data, token, odcid_len);
4702 odcid->len = odcid_len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004703 leave:
4704 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
4705 return !ret;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004706}
4707
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004708/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4709 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4710 * for QUIC servers (or haproxy listeners).
4711 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4712 * <token> the token found to be used for this connection with <token_len> as
4713 * length. <saddr> is the source address.
4714 * Returns the connection if succeeded, NULL if not.
4715 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004716static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004717 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02004718 const struct quic_cid *token_odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004719 struct sockaddr_storage *saddr,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004720 int server, int token, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721{
4722 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004723 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004724 /* Initial CID. */
4725 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004726 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004727 struct listener *l = NULL;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004728 struct quic_cc_algo *cc_algo = NULL;
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004729 struct quic_tls_ctx *ictx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004730 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004731 qc = pool_zalloc(pool_head_quic_conn);
4732 if (!qc) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004733 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004734 goto err;
4735 }
4736
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004737 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4738 if (!buf_area) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004739 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004740 goto err;
4741 }
4742
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004743 qc->cids = EB_ROOT;
4744 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004745 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004746 struct proxy *prx;
4747
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004748 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004749 prx = l->bind_conf->frontend;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004750 cc_algo = l->bind_conf->quic_cc_algo;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004751
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004752 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4753 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004754 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004755 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004756 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004757 qc->odcid.len = dcid->len;
4758 qc->odcid.addrlen = dcid->addrlen;
4759 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004760
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004761 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004762 if (scid->len)
4763 memcpy(qc->dcid.data, scid->data, scid->len);
4764 qc->dcid.len = scid->len;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004765 qc->tx.buf = BUF_NULL;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004766 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004767 }
4768 /* QUIC Client (outgoing connection to servers) */
4769 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004770 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004771 if (dcid->len)
4772 memcpy(qc->dcid.data, dcid->data, dcid->len);
4773 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004774 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004775 qc->mux_state = QC_MUX_NULL;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02004776 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004777
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004778 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004779 if (!icid) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004780 TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004781 goto err;
4782 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004783
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004784 /* insert the allocated CID in the receiver datagram handler tree */
4785 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004786 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004787
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004788 /* Select our SCID which is the first CID with 0 as sequence number. */
4789 qc->scid = icid->cid;
4790
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004791 /* Packet number spaces initialization. */
4792 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4793 quic_pktns_init(&qc->pktns[i]);
4794 /* QUIC encryption level context initialization. */
4795 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004796 if (!quic_conn_enc_level_init(qc, i)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004797 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004798 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004799 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004800 /* Initialize the packet number space. */
4801 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4802 }
4803
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004804 qc->original_version = qv;
4805 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004806 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4807 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004808 /* TX part. */
4809 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004810 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4811 qc->tx.wbuf = qc->tx.rbuf = 0;
4812 qc->tx.bytes = 0;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004813 qc->tx.buf = BUF_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004814 /* RX part. */
4815 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004816 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004817 for (i = 0; i < QCS_MAX_TYPES; i++)
4818 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004819
4820 qc->nb_pkt_for_cc = 1;
4821 qc->nb_pkt_since_cc = 0;
4822
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004823 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004824 if (!quic_tls_ku_init(qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004825 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004826 goto err;
4827 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004828
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004829 /* XXX TO DO: Only one path at this time. */
4830 qc->path = &qc->paths[0];
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004831 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 +01004832
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004833 /* required to use MTLIST_IN_LIST */
4834 MT_LIST_INIT(&qc->accept_list);
4835
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004836 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004837 qc->stream_buf_count = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004838 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4839
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004840 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
4841 icid->stateless_reset_token,
4842 dcid->data, dcid->len,
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02004843 qc->scid.data, qc->scid.len, token_odcid))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004844 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004845
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004846 if (qc_conn_alloc_ssl_ctx(qc) ||
4847 !quic_conn_init_timer(qc) ||
4848 !quic_conn_init_idle_timer_task(qc))
4849 goto err;
4850
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004851 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
4852 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004853 goto err;
4854
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004855 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004856
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004857 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004858
4859 err:
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004860 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4861 if (qc)
4862 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004863 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004864 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004865 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004866}
4867
4868/* Initialize the timer task of <qc> QUIC connection.
4869 * Returns 1 if succeeded, 0 if not.
4870 */
4871static int quic_conn_init_timer(struct quic_conn *qc)
4872{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004873 int ret = 0;
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004874 /* Attach this task to the same thread ID used for the connection */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004875 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4876
Willy Tarreau87168752022-06-13 16:31:53 +02004877 qc->timer_task = task_new_on(qc->tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004878 if (!qc->timer_task) {
4879 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4880 goto leave;
4881 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004882
4883 qc->timer = TICK_ETERNITY;
Willy Tarreau41e701e2022-09-08 15:12:59 +02004884 qc->timer_task->process = qc_process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004885 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004886
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004887 ret = 1;
4888 leave:
4889 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4890 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004891}
4892
Frédéric Lécaille47756802022-03-25 09:12:16 +01004893/* Rearm the idle timer for <qc> QUIC connection. */
4894static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4895{
4896 unsigned int expire;
4897
4898 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4899 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4900}
4901
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004902/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4903 * which is set to 1 when receiving a packet , and 0 when sending packet
4904 */
4905static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4906{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004907 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4908
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004909 if (read) {
4910 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4911 }
4912 else {
4913 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4914 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004915 qc_idle_timer_do_rearm(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004916
4917 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004918}
4919
4920/* The task handling the idle timeout */
Willy Tarreau41e701e2022-09-08 15:12:59 +02004921struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004922{
4923 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004924 struct quic_counters *prx_counters = qc->prx_counters;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004925 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004926
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004927 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4928
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004929 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4930 * might free the quic-conn too early via quic_close().
4931 */
4932 qc_notify_close(qc);
4933
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004934 /* If the MUX is still alive, keep the quic-conn. The MUX is
4935 * responsible to call quic_close to release it.
4936 */
4937 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4938 if (qc->mux_state != QC_MUX_READY)
4939 quic_conn_release(qc);
4940
4941 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4942 * least clean some parts of it such as the tasklet.
4943 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004944
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004945 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
4946 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004947 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004948 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004949 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004950
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004951 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004952 return NULL;
4953}
4954
4955/* Initialize the idle timeout task for <qc>.
4956 * Returns 1 if succeeded, 0 if not.
4957 */
4958static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4959{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004960 int ret = 0;
4961
4962 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4963
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004964 qc->idle_timer_task = task_new_here();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004965 if (!qc->idle_timer_task) {
4966 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4967 goto leave;
4968 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004969
4970 qc->idle_timer_task->process = qc_idle_timer_task;
4971 qc->idle_timer_task->context = qc;
4972 qc_idle_timer_rearm(qc, 1);
4973 task_queue(qc->idle_timer_task);
4974
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004975 ret = 1;
4976 leave:
4977 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4978 return ret;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004979}
4980
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004981/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4982 * past one byte of this buffer.
4983 */
4984static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4985 struct quic_rx_packet *pkt)
4986{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004987 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004988 unsigned char dcid_len, scid_len;
4989
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004990 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
4991
4992 if (end == *buf) {
4993 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
4994 goto leave;
4995 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004996
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004997 /* Destination Connection ID Length */
4998 dcid_len = *(*buf)++;
4999 /* 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 +02005000 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
5001 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
5002 goto leave;
5003 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005004
5005 if (dcid_len) {
5006 /* Check that the length of this received DCID matches the CID lengths
5007 * of our implementation for non Initials packets only.
5008 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005009 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
5010 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005011 dcid_len != QUIC_HAP_CID_LEN) {
5012 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
5013 goto leave;
5014 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005015
5016 memcpy(pkt->dcid.data, *buf, dcid_len);
5017 }
5018
5019 pkt->dcid.len = dcid_len;
5020 *buf += dcid_len;
5021
5022 /* Source Connection ID Length */
5023 scid_len = *(*buf)++;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005024 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
5025 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
5026 goto leave;
5027 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005028
5029 if (scid_len)
5030 memcpy(pkt->scid.data, *buf, scid_len);
5031 pkt->scid.len = scid_len;
5032 *buf += scid_len;
5033
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005034 ret = 1;
5035 leave:
5036 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
5037 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005038}
5039
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005040/* Insert <pkt> RX packet in its <qel> RX packets tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005041static void qc_pkt_insert(struct quic_conn *qc,
5042 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005043{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005044 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
5045
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005046 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01005047 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005048 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005049
5050 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005051}
5052
5053/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005054 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
5055 * byte past the end of the buffer containing this packet and <beg> the address of
5056 * the packet first byte.
5057 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
5058 * Returns 1 if succeeded, 0 if not.
5059 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005060static inline int qc_try_rm_hp(struct quic_conn *qc,
5061 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005062 unsigned char *buf, unsigned char *beg,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005063 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005064{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005065 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005066 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005067 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005068 struct quic_enc_level *qel;
5069 /* Only for traces. */
5070 struct quic_rx_packet *qpkt_trace;
5071
5072 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005073 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005074 /* The packet number is here. This is also the start minus
5075 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5076 * protection.
5077 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005078 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005079
5080 tel = quic_packet_type_enc_level(pkt->type);
5081 qel = &qc->els[tel];
5082
5083 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005084 /* Note that the following function enables us to unprotect the packet
5085 * number and its length subsequently used to decrypt the entire
5086 * packets.
5087 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005088 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02005089 qel->pktns->rx.largest_pn, pn, beg)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005090 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005091 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005092 }
5093
5094 /* The AAD includes the packet number field found at <pn>. */
5095 pkt->aad_len = pn - beg + pkt->pnl;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005096 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
5097 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005098 goto out;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005099 }
5100
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005101 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005102 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01005103 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01005104 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005105 /* If the packet number space has been discarded, this packet
5106 * will be not parsed.
5107 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005108 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005109 goto out;
5110 }
5111
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005112 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005113 pkt->pn_offset = pn - beg;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02005114 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02005115 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005116 }
5117
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005118 *el = qel;
5119 /* No reference counter incrementation here!!! */
5120 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
5121 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
5122 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
5123 b_add(&qc->rx.buf, pkt->len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005124
5125 ret = 1;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005126 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005127 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005128 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005129}
5130
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005131/* 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 +02005132 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
5133 * of this packet into <*version>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005134 */
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005135static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
5136 unsigned char **buf, const unsigned char *end,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005137 int *long_header, uint32_t *version)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005138{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005139 int ret = 0;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005140 const unsigned char byte0 = **buf;
5141
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005142 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5143
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005144 (*buf)++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005145 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005146 unsigned char type =
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005147 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005148
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005149 *long_header = 1;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005150 /* Version */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005151 if (!quic_read_uint32(version, (const unsigned char **)buf, end)) {
5152 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
5153 goto out;
5154 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005155
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005156 if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005157 pkt->type = type;
5158 }
5159 else {
5160 switch (type) {
5161 case 0:
5162 pkt->type = QUIC_PACKET_TYPE_RETRY;
5163 break;
5164 case 1:
5165 pkt->type = QUIC_PACKET_TYPE_INITIAL;
5166 break;
5167 case 2:
5168 pkt->type = QUIC_PACKET_TYPE_0RTT;
5169 break;
5170 case 3:
5171 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
5172 break;
5173 }
5174 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005175 }
5176 else {
5177 pkt->type = QUIC_PACKET_TYPE_SHORT;
5178 *long_header = 0;
5179 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005180
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005181 ret = 1;
5182 out:
5183 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005184 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005185}
5186
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005187/* Return the QUIC version (quic_version struct) with <version> as version number
5188 * if supported or NULL if not.
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005189 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005190static inline const struct quic_version *qc_supported_version(uint32_t version)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005191{
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005192 int i;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005193
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005194 for (i = 0; i < quic_versions_nb; i++)
5195 if (quic_versions[i].num == version)
5196 return &quic_versions[i];
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005197
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005198 return NULL;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005199}
5200
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005201/*
5202 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
5203 * address <addr>.
5204 * Implementation of RFC9000 6. Version Negotiation
5205 *
5206 * TODO implement a rate-limiting sending of Version Negotiation packets
5207 *
5208 * Returns 0 on success else non-zero
5209 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005210static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
5211 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005212{
5213 char buf[256];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005214 int ret = 0, i = 0, j;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005215 uint32_t version;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005216 const socklen_t addrlen = get_addr_len(addr);
5217
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005218 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005219 /*
5220 * header form
5221 * long header, fixed bit to 0 for Version Negotiation
5222 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005223 /* TODO: RAND_bytes() should be replaced? */
5224 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
5225 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
5226 goto out;
5227 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005228
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01005229 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005230 /* null version for Version Negotiation */
5231 buf[i++] = '\x00';
5232 buf[i++] = '\x00';
5233 buf[i++] = '\x00';
5234 buf[i++] = '\x00';
5235
5236 /* source connection id */
5237 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005238 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005239 i += pkt->scid.len;
5240
5241 /* destination connection id */
5242 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005243 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005244 i += pkt->dcid.len;
5245
5246 /* supported version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005247 for (j = 0; j < quic_versions_nb; j++) {
5248 version = htonl(quic_versions[j].num);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005249 memcpy(&buf[i], &version, sizeof(version));
5250 i += sizeof(version);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005251 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005252
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005253 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005254 goto out;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005255
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005256 ret = 1;
5257 out:
5258 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5259 return !ret;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005260}
5261
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005262/* Send a stateless reset packet depending on <pkt> RX packet information
5263 * from <fd> UDP socket to <dst>
5264 * Return 1 if succeeded, 0 if not.
5265 */
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005266static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005267 struct quic_rx_packet *rxpkt)
5268{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005269 int ret = 0, pktlen, rndlen;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005270 unsigned char pkt[64];
5271 const socklen_t addrlen = get_addr_len(dstaddr);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005272 struct proxy *prx;
5273 struct quic_counters *prx_counters;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005274
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005275 TRACE_ENTER(QUIC_EV_STATELESS_RST);
5276
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005277 prx = l->bind_conf->frontend;
5278 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005279 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
5280 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
5281 * Reset is difficult to distinguish from other packets if the recipient requires
5282 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
5283 * that all packets it sends are at least 22 bytes longer than the minimum
5284 * connection ID length that it requests the peer to include in its packets,
5285 * adding PADDING frames as necessary. This ensures that any Stateless Reset
5286 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
5287 * An endpoint that sends a Stateless Reset in response to a packet that is
5288 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
5289 * than the packet it responds to.
5290 */
5291
5292 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
5293 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
5294 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
5295 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005296
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005297 /* Put a header of random bytes */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005298 /* TODO: RAND_bytes() should be replaced */
5299 if (RAND_bytes(pkt, rndlen) != 1) {
5300 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
5301 goto leave;
5302 }
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005303
5304 /* Clear the most significant bit, and set the second one */
5305 *pkt = (*pkt & ~0x80) | 0x40;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005306 if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005307 rxpkt->dcid.data, rxpkt->dcid.len))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005308 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005309
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005310 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005311 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005312
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005313 ret = 1;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005314 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005315 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005316 leave:
5317 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
5318 return ret;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005319}
5320
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005321/* QUIC server only function.
5322 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005323 * This is the responsibility of the caller to check <aad> size is big enough
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005324 * to contain these data.
5325 * Return the number of bytes copied to <aad>.
5326 */
5327static int quic_generate_retry_token_aad(unsigned char *aad,
5328 uint32_t version,
5329 const struct quic_cid *cid,
5330 const struct sockaddr_storage *addr)
5331{
5332 unsigned char *p;
5333
5334 p = aad;
5335 memcpy(p, &version, sizeof version);
5336 p += sizeof version;
5337 p += quic_saddr_cpy(p, addr);
5338 memcpy(p, cid->data, cid->len);
5339 p += cid->len;
5340
5341 return p - aad;
5342}
5343
5344/* QUIC server only function.
5345 * Generate the token to be used in Retry packets. The token is written to
5346 * <buf> whith <len> as length. <odcid> is the original destination connection
5347 * ID and <dcid> is our side destination connection ID (or client source
5348 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005349 * Returns the length of the encoded token or 0 on error.
5350 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005351static int quic_generate_retry_token(unsigned char *buf, size_t len,
5352 const uint32_t version,
5353 const struct quic_cid *odcid,
5354 const struct quic_cid *dcid,
5355 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005356{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005357 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005358 unsigned char *p;
5359 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5360 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5361 size_t aadlen;
5362 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
5363 unsigned char key[QUIC_TLS_KEY_LEN];
5364 unsigned char iv[QUIC_TLS_IV_LEN];
5365 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5366 size_t seclen = strlen(global.cluster_secret);
5367 EVP_CIPHER_CTX *ctx = NULL;
5368 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5369 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005370
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005371 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5372
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005373 /* We copy the odcid into the token, prefixed by its one byte
5374 * length, the format token byte. It is followed by an AEAD TAG, and finally
5375 * the random bytes used to derive the secret to encrypt the token.
5376 */
5377 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005378 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005379
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005380 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005381 /* TODO: RAND_bytes() should be replaced */
5382 if (RAND_bytes(salt, sizeof salt) != 1) {
5383 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005384 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005385 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005386
5387 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 +02005388 salt, sizeof salt, sec, seclen)) {
5389 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005390 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005391 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005392
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005393 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
5394 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005395 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005396 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005397
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005398 /* Token build */
5399 p = buf;
5400 *p++ = QUIC_TOKEN_FMT_RETRY,
5401 *p++ = odcid->len;
5402 memcpy(p, odcid->data, odcid->len);
5403 p += odcid->len;
5404 write_u32(p, htonl(timestamp));
5405 p += sizeof timestamp;
5406
5407 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005408 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
5409 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005410 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005411 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005412
5413 p += QUIC_TLS_TAG_LEN;
5414 memcpy(p, salt, sizeof salt);
5415 p += sizeof salt;
5416 EVP_CIPHER_CTX_free(ctx);
5417
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005418 ret = p - buf;
5419 leave:
5420 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5421 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005422
5423 err:
5424 if (ctx)
5425 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005426 goto leave;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005427}
5428
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005429/* QUIC server only function.
5430 * Check the validity of the Retry token from <token> buffer with <tokenlen>
5431 * as length. If valid, the ODCID of <qc> QUIC connection will be put
5432 * into <odcid> connection ID. <dcid> is our side destination connection ID
5433 * of client source connection ID.
5434 * Return 1 if succeeded, 0 if not.
5435 */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005436static int quic_retry_token_check(const unsigned char *token, size_t tokenlen,
5437 const struct quic_version *qv,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005438 struct quic_cid *odcid,
5439 const struct quic_cid *dcid,
5440 struct quic_conn *qc,
5441 struct sockaddr_storage *addr)
5442{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005443 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005444 unsigned char buf[128];
5445 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5446 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5447 size_t aadlen;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005448 const unsigned char *salt;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005449 unsigned char key[QUIC_TLS_KEY_LEN];
5450 unsigned char iv[QUIC_TLS_IV_LEN];
5451 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5452 size_t seclen = strlen(global.cluster_secret);
5453 EVP_CIPHER_CTX *ctx = NULL;
5454 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5455
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005456 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5457
5458 if (sizeof buf < tokenlen) {
5459 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
5460 goto err;
5461 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005462
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005463 aadlen = quic_generate_retry_token_aad(aad, qv->num, dcid, addr);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005464 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
5465 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 +02005466 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005467 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
5468 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005469 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005470
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005471 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
5472 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005473 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005474 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005475
5476 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
5477 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 +02005478 ctx, aead, key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005479 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005480 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005481 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005482
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005483 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
5484 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005485 goto err;
5486 }
5487
5488 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005489
5490 ret = 1;
5491 leave:
5492 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5493 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005494
5495 err:
5496 if (ctx)
5497 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005498 goto leave;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005499}
5500
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005501/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
5502 * the Initial <pkt> packet.
5503 *
5504 * Returns 0 on success else non-zero.
5505 */
5506static int send_retry(int fd, struct sockaddr_storage *addr,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005507 struct quic_rx_packet *pkt, const struct quic_version *qv)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005508{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005509 int ret = 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005510 unsigned char buf[128];
5511 int i = 0, token_len;
5512 const socklen_t addrlen = get_addr_len(addr);
5513 struct quic_cid scid;
5514
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005515 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5516
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005517 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
5518 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
5519 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005520 /* version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005521 buf[i++] = *((unsigned char *)&qv->num + 3);
5522 buf[i++] = *((unsigned char *)&qv->num + 2);
5523 buf[i++] = *((unsigned char *)&qv->num + 1);
5524 buf[i++] = *(unsigned char *)&qv->num;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005525
5526 /* Use the SCID from <pkt> for Retry DCID. */
5527 buf[i++] = pkt->scid.len;
5528 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
5529 i += pkt->scid.len;
5530
5531 /* Generate a new CID to be used as SCID for the Retry packet. */
5532 scid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005533 /* TODO: RAND_bytes() should be replaced */
5534 if (RAND_bytes(scid.data, scid.len) != 1) {
5535 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
5536 goto out;
5537 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005538
5539 buf[i++] = scid.len;
5540 memcpy(&buf[i], scid.data, scid.len);
5541 i += scid.len;
5542
5543 /* token */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005544 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005545 &pkt->dcid, &pkt->scid, addr))) {
5546 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
5547 goto out;
5548 }
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01005549
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005550 i += token_len;
5551
5552 /* token integrity tag */
5553 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
5554 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005555 pkt->dcid.len, buf, i, qv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005556 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5557 goto out;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005558 }
5559
5560 i += QUIC_TLS_TAG_LEN;
5561
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005562 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
5563 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5564 goto out;
5565 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005566
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005567 ret = 1;
5568 out:
5569 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5570 return !ret;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005571}
5572
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005573/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5574 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5575 * concatenated to the <pkt> DCID field.
5576 *
5577 * Returns the instance or NULL if not found.
5578 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005579static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005580 struct listener *l,
5581 struct sockaddr_storage *saddr)
5582{
5583 struct quic_conn *qc = NULL;
5584 struct ebmb_node *node;
5585 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005586 /* set if the quic_conn is found in the second DCID tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005587
5588 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005589
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005590 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5591 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5592 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5593 /* DCIDs of first packets coming from multiple clients may have
5594 * the same values. Let's distinguish them by concatenating the
5595 * socket addresses.
5596 */
5597 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005598 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005599 pkt->dcid.len + pkt->dcid.addrlen);
5600 if (node) {
5601 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5602 goto end;
5603 }
5604 }
5605
5606 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5607 * also for INITIAL/0-RTT non-first packets with the final DCID in
5608 * used.
5609 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005610 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005611 if (!node)
5612 goto end;
5613
5614 id = ebmb_entry(node, struct quic_connection_id, node);
5615 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005616
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005617 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5618 * If already done, this is a noop.
5619 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005620 if (qc)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005621 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005622
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005623 end:
Frédéric Lécaille614742b2022-09-07 12:15:43 +02005624 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005625 return qc;
5626}
5627
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005628/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5629 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5630 * parameters of this session.
5631 * This is the responsibility of the caller to check the validity of all the
5632 * pointers passed as parameter to this function.
5633 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5634 * CO_ER_SSL_NO_MEM.
5635 */
5636static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5637 unsigned char *params, size_t params_len)
5638{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005639 int retry, ret = -1;
5640
5641 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005642
5643 retry = 1;
5644 retry:
5645 *ssl = SSL_new(ssl_ctx);
5646 if (!*ssl) {
5647 if (!retry--)
5648 goto err;
5649
5650 pool_gc(NULL);
5651 goto retry;
5652 }
5653
5654 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005655 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005656 SSL_free(*ssl);
5657 *ssl = NULL;
5658 if (!retry--)
5659 goto err;
5660
5661 pool_gc(NULL);
5662 goto retry;
5663 }
5664
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005665 ret = 0;
5666 leave:
5667 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5668 return ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005669
5670 err:
5671 qc->conn->err_code = CO_ER_SSL_NO_MEM;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005672 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005673}
5674
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005675/* Finalize <qc> QUIC connection:
5676 * - initialize the Initial QUIC TLS context for negotiated version,
5677 * - derive the secrets for this context,
5678 * - encode the transport parameters to be sent,
5679 * - set them into the TLS stack,
5680 * - initialize ->max_ack_delay and max_idle_timeout,
5681 *
5682 * MUST be called after having received the remote transport parameters.
5683 * Return 1 if succeeded, 0 if not.
5684 */
5685int qc_conn_finalize(struct quic_conn *qc, int server)
5686{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005687 int ret = 0;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005688 struct quic_transport_params *tx_tp = &qc->tx.params;
5689 struct quic_transport_params *rx_tp = &qc->rx.params;
5690 const struct quic_version *ver;
5691
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005692 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5693
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005694 if (tx_tp->version_information.negotiated_version &&
5695 tx_tp->version_information.negotiated_version != qc->original_version) {
5696 qc->negotiated_version =
5697 qc->tx.params.version_information.negotiated_version;
5698 if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
5699 qc->odcid.data, qc->odcid.len, !server))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005700 goto out;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005701
5702 ver = qc->negotiated_version;
5703 }
5704 else {
5705 ver = qc->original_version;
5706 }
5707
5708 qc->enc_params_len =
5709 quic_transport_params_encode(qc->enc_params,
5710 qc->enc_params + sizeof qc->enc_params,
5711 &qc->rx.params, ver, 1);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005712 if (!qc->enc_params_len) {
5713 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_TXPKT);
5714 goto out;
5715 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005716
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005717 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
5718 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_TXPKT);
5719 goto out;
5720 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005721
5722 if (tx_tp->max_ack_delay)
5723 qc->max_ack_delay = tx_tp->max_ack_delay;
5724
5725 if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout)
5726 qc->max_idle_timeout =
5727 QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5728 else
5729 qc->max_idle_timeout =
5730 QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5731
5732 TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp);
5733
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005734 ret = 1;
5735 out:
5736 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5737 return ret;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005738}
5739
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005740/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5741 * used to process <qc> received packets. The allocated context is stored in
5742 * <qc.xprt_ctx>.
5743 *
5744 * Returns 0 on success else non-zero.
5745 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005746static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005747{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005748 int ret = 0;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005749 struct bind_conf *bc = qc->li->bind_conf;
5750 struct ssl_sock_ctx *ctx = NULL;
5751
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005752 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5753
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005754 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005755 if (!ctx) {
5756 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005757 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005758 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005759
5760 ctx->wait_event.tasklet = tasklet_new();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005761 if (!ctx->wait_event.tasklet) {
5762 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005763 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005764 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005765
5766 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5767 ctx->wait_event.tasklet->context = ctx;
5768 ctx->wait_event.events = 0;
5769 ctx->subs = NULL;
5770 ctx->xprt_ctx = NULL;
5771 ctx->qc = qc;
5772
5773 /* Set tasklet tid based on the SCID selected by us for this
5774 * connection. The upper layer will also be binded on the same thread.
5775 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005776 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005777
5778 if (qc_is_listener(qc)) {
5779 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5780 qc->enc_params, qc->enc_params_len) == -1) {
5781 goto err;
5782 }
William Lallemande6ec6262022-09-02 16:24:39 +02005783#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005784 /* Enabling 0-RTT */
5785 if (bc->ssl_conf.early_data)
5786 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
William Lallemande6ec6262022-09-02 16:24:39 +02005787#endif
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005788
5789 SSL_set_accept_state(ctx->ssl);
5790 }
5791
5792 ctx->xprt = xprt_get(XPRT_QUIC);
5793
5794 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005795 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005796
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005797 ret = 1;
5798 leave:
5799 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5800 return !ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005801
5802 err:
5803 if (ctx && ctx->wait_event.tasklet)
5804 tasklet_free(ctx->wait_event.tasklet);
5805 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005806 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005807}
5808
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005809/* Check that all the bytes between <buf> included and <end> address
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005810 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005811 * check that there is at least one byte between <buf> end <end>.
5812 * Return 1 if this all the bytes are null, 0 if not.
5813 */
5814static inline int quic_padding_check(const unsigned char *buf,
5815 const unsigned char *end)
5816{
5817 while (buf < end && !*buf)
5818 buf++;
5819
5820 return buf == end;
5821}
5822
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005823/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5824 * end of this buffer past one byte and populate <pkt> RX packet structure
5825 * with the information collected from the packet.
5826 * This function sets ->len <pkt> field value to the end of the packet past one
5827 * byte to enable the caller to run this function again to continue to parse
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005828 * the remaining QUIC packets carried by the datagram.
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005829 * Note that this function always sets this ->len value. If a paquet could
5830 * not be correctly found, ->len value will be set to the remaining number
5831 * bytes in the datagram to entirely consume this latter.
5832 */
5833static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5834 struct quic_rx_packet *pkt, int first_pkt,
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02005835 struct quic_dgram *dgram, struct list **tasklist_head)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005836{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005837 unsigned char *beg, *payload;
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02005838 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005839 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005840 struct proxy *prx;
5841 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005842 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005843 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005844 size_t b_cspace;
5845 struct quic_enc_level *qel;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005846 uint32_t version;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005847 const struct quic_version *qv = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005848
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005849 TRACE_ENTER(QUIC_EV_CONN_LPKT);
5850
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005851 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005852 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005853 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005854 qel = NULL;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005855 l = dgram->owner;
5856 prx = l->bind_conf->frontend;
5857 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005858 /* This ist only to please to traces and distinguish the
5859 * packet with parsed packet number from others.
5860 */
5861 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005862 if (end <= buf) {
5863 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5864 goto drop;
5865 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005866
5867 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005868 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005869 if (!first_pkt && quic_padding_check(buf, end)) {
5870 /* Some browsers may pad the remaining datagram space with null bytes.
5871 * That is what we called add padding out of QUIC packets. Such
5872 * datagrams must be considered as valid. But we can only consume
5873 * the remaining space.
5874 */
5875 pkt->len = end - buf;
5876 goto drop_no_conn;
5877 }
5878
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005879 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005880 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005881 }
5882
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005883 /* Header form */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005884 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005885 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5886 goto drop;
5887 }
5888
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005889 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005890 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005891 struct quic_cid odcid;
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02005892 const struct quic_cid *token_odcid = NULL; // ODCID received from client token
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005893
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005894 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005895 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005896 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005897 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005898 }
5899
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005900 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5901 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005902 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005903 }
5904 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5905 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005906 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02005907 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
Frédéric Lécaillee7df68a2022-08-05 09:34:44 +02005908 goto drop;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005909 }
5910
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005911 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5912 * they must have the same DCID.
5913 */
5914 if (!first_pkt &&
5915 (pkt->dcid.len != dgram->dcid_len ||
5916 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5917 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005918 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005919 }
5920
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005921 /* Retry of Version Negotiation packets are only sent by servers */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005922 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005923 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005924 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005925 }
5926
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005927 /* RFC9000 6. Version Negotiation */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005928 qv = qc_supported_version(version);
5929 if (!qv) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005930 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005931 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005932 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005933 goto err;
5934 }
5935
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005936 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005937 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005938 }
5939
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005940 /* For Initial packets, and for servers (QUIC clients connections),
5941 * there is no Initial connection IDs storage.
5942 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005943 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005944 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005945
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005946 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5947 end - buf < token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005948 TRACE_PROTO("Packet dropped",
5949 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005950 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005951 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005952
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005953 /* TODO Retry should be automatically activated if
5954 * suspect network usage is detected.
5955 */
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005956 if (global.cluster_secret && !token_len) {
5957 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
5958 TRACE_PROTO("Initial without token, sending retry",
5959 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
5960 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
5961 TRACE_PROTO("Error during Retry generation",
5962 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005963 goto err;
5964 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005965
5966 HA_ATOMIC_INC(&prx_counters->retry_sent);
5967 goto err;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005968 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005969 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005970 else if (!global.cluster_secret && token_len) {
5971 /* Impossible case: a token was received without configured
5972 * cluster secret.
5973 */
5974 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
5975 NULL, NULL, NULL, qv);
5976 goto drop;
5977 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005978
5979 pkt->token = buf;
5980 pkt->token_len = token_len;
5981 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005982 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005983 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005984 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005985 TRACE_PROTO("Packet dropped",
5986 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005987 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005988 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005989 }
5990
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005991 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5992 end - buf < len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005993 TRACE_PROTO("Packet dropped",
5994 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005995 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005996 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005997
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005998 payload = buf;
5999 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006000 if (drop_no_conn)
6001 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01006002
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006003 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02006004 if (global.cluster_secret && pkt->token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006005 if (*pkt->token == QUIC_TOKEN_FMT_RETRY) {
6006 const struct quic_version *ver = qc ? qc->original_version : qv;
6007 if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid,
6008 &pkt->scid, qc, &dgram->saddr)) {
6009 HA_ATOMIC_INC(&prx_counters->retry_error);
6010 TRACE_PROTO("Wrong retry token",
6011 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
6012 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
6013 * with an INVALID_TOKEN error.
6014 */
6015 goto drop;
6016 }
6017
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02006018 token_odcid = &odcid;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006019 HA_ATOMIC_INC(&prx_counters->retry_validated);
6020 }
6021 else {
6022 /* TODO: New token check */
6023 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
6024 goto drop;
6025 }
6026 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01006027 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006028 int ipv4;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006029
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006030 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006031 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006032 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006033 }
6034
Willy Tarreau787e92a2022-05-20 16:06:01 +02006035 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 +02006036 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006037 TRACE_PROTO("Initial without token, sending retry",
6038 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02006039 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille59507de2022-08-11 12:14:07 +02006040 TRACE_ERROR("Error during Retry generation",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006041 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02006042 goto err;
6043 }
6044
6045 HA_ATOMIC_INC(&prx_counters->retry_sent);
6046 goto err;
6047 }
6048
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02006049 /* RFC 9000 7.2. Negotiating Connection IDs:
6050 * When an Initial packet is sent by a client that has not previously
6051 * received an Initial or Retry packet from the server, the client
6052 * populates the Destination Connection ID field with an unpredictable
6053 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6054 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01006055 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006056 TRACE_PROTO("dropped packet",
6057 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006058 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01006059 }
6060
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006061 pkt->saddr = dgram->saddr;
6062 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006063
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02006064 qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, token_odcid,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02006065 &pkt->saddr, 1, !!pkt->token_len, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02006066 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006067 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006068
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006069 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02006070 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02006071 ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
6072 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006073 }
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02006074
6075 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006076 }
6077 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006078 TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006079 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006080 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006081 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006082 }
6083
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006084 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006085 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006086
6087 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6088 * they must have the same DCID.
6089 */
6090 if (!first_pkt &&
6091 (pkt->dcid.len != dgram->dcid_len ||
6092 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
6093 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006094 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006095 }
6096
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006097 buf += QUIC_HAP_CID_LEN;
6098
6099 /* A short packet is the last one of a UDP datagram. */
6100 payload = buf;
6101 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006102
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006103 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006104 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006105 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006106 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006107 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006108 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006109 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006110 }
6111
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006112 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006113 }
6114
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006115 if (qc->flags & QUIC_FL_CONN_CLOSING) {
6116 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
6117 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
6118 qc->nb_pkt_for_cc++;
6119 qc->nb_pkt_since_cc = 0;
6120 }
6121 /* Skip the entire datagram */
6122 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006123 TRACE_STATE("Closing state connection",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006124 QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006125 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006126 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006127
6128 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6129 * they must have the same DCID.
6130 *
6131 * This check must be done after the final update to pkt.len to
6132 * properly drop the packet on failure.
6133 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006134 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006135 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006136 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006137 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006138 /* Reset the anti-amplification bit. It will be set again
6139 * when sending the next packet if reached again.
6140 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006141 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
6142 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006143 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006144 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006145
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006146 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006147
Amaury Denoyelle72d86502022-07-13 15:08:23 +02006148 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006149 TRACE_PROTO("Connection error",
6150 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006151 goto out;
6152 }
6153
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006154 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006155 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006156 b_cspace = b_contig_space(&qc->rx.buf);
6157 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006158 /* Do not consume buf if space not at the end. */
6159 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006160 TRACE_PROTO("Packet dropped",
6161 QUIC_EV_CONN_LPKT, qc, NULL, NULL, 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;
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006164 }
6165
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006166 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006167 if (b_cspace) {
6168 b_putchr(&qc->rx.buf, 0x00);
6169 b_cspace--;
6170 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006171 b_add(&qc->rx.buf, b_cspace);
6172 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006173 TRACE_PROTO("Too big packet",
6174 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006175 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6176 goto drop_no_conn;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006177 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006178 }
6179
Frédéric Lécaille99897d12022-08-08 10:28:07 +02006180 if (!qc_try_rm_hp(qc, pkt, payload, beg, &qel)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006181 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006182 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02006183 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006184
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006185 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006186 if (pkt->aad_len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006187 qc_pkt_insert(qc, pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006188 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02006189 /* Wake up the connection packet handler task from here only if all
6190 * the contexts have been initialized, especially the mux context
6191 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
6192 * will start it if these contexts for the connection are not already
6193 * initialized.
6194 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006195 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006196 if (conn_ctx)
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02006197 *tasklist_head = tasklet_wakeup_after(*tasklist_head, conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02006198
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006199 drop_no_conn:
6200 if (drop_no_conn)
6201 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006202 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006203
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006204 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006205
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006206 drop:
6207 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006208 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006209 /* Wakeup the I/O handler callback if the PTO timer must be armed.
6210 * This cannot be done by this thread.
6211 */
6212 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006213 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006214 if (conn_ctx && conn_ctx->wait_event.tasklet)
6215 tasklet_wakeup(conn_ctx->wait_event.tasklet);
6216 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01006217 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006218 if (!pkt->len)
6219 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006220 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006221}
6222
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006223/* This function builds into <buf> buffer a QUIC long packet header.
6224 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006225 */
6226static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006227 int type, size_t pn_len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006228 struct quic_conn *qc, const struct quic_version *ver)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006229{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006230 int ret = 0;
6231
6232 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6233
6234 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
6235 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6236 goto leave;
6237 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006238
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006239 type = quic_pkt_type(type, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006240 /* #0 byte flags */
6241 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
6242 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
6243 /* Version */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006244 quic_write_uint32(buf, end, ver->num);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006245 *(*buf)++ = qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006246 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006247 if (qc->dcid.len) {
6248 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6249 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006250 }
6251 /* Source connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006252 *(*buf)++ = qc->scid.len;
6253 if (qc->scid.len) {
6254 memcpy(*buf, qc->scid.data, qc->scid.len);
6255 *buf += qc->scid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006256 }
6257
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006258 ret = 1;
6259 leave:
6260 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6261 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006262}
6263
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006264/* This function builds into <buf> buffer a QUIC short packet header.
6265 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006266 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006267static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006268 size_t pn_len, struct quic_conn *qc,
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006269 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006270{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006271 int ret = 0;
6272
6273 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6274
6275 if (end - *buf < 1 + qc->dcid.len) {
6276 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6277 goto leave;
6278 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006279
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006280 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01006281 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
6282 ((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 +01006283 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006284 if (qc->dcid.len) {
6285 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6286 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006287 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006288
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006289 ret = 1;
6290 leave:
6291 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6292 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006293}
6294
6295/* Apply QUIC header protection to the packet with <buf> as first byte address,
6296 * <pn> as address of the Packet number field, <pnlen> being this field length
6297 * with <aead> as AEAD cipher and <key> as secret key.
6298 * Returns 1 if succeeded or 0 if failed.
6299 */
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006300static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf,
6301 unsigned char *pn, size_t pnlen,
6302 struct quic_tls_ctx *tls_ctx)
6303
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006304{
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006305 int i, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006306 /* We need an IV of at least 5 bytes: one byte for bytes #0
6307 * and at most 4 bytes for the packet number
6308 */
6309 unsigned char mask[5] = {0};
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006310 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006311
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006312 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6313
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006314 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
6315 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006316 goto out;
6317 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006318
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006319 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
6320 for (i = 0; i < pnlen; i++)
6321 pn[i] ^= mask[i + 1];
6322
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006323 ret = 1;
6324 out:
6325 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006326 return ret;
6327}
6328
6329/* Reduce the encoded size of <ack_frm> ACK frame removing the last
6330 * ACK ranges if needed to a value below <limit> in bytes.
6331 * Return 1 if succeeded, 0 if not.
6332 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006333static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
6334 struct quic_frame *ack_frm, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006335{
6336 size_t room, ack_delay_sz;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006337 int ret = 0;
6338
6339 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006340
6341 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
6342 /* A frame is made of 1 byte for the frame type. */
6343 room = limit - ack_delay_sz - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006344 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
6345 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006346
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006347 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
6348 leave:
6349 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6350 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006351}
6352
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006353/* Prepare into <outlist> as most as possible ack-eliciting frame from their
6354 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
6355 * with <room> as available room, and <*len> the packet Length field initialized
6356 * with the number of bytes already present in this buffer which must be taken
6357 * into an account for the Length packet field value. <headlen> is the number of
6358 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006359 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006360 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006361 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006362 * 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 +01006363 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006364static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006365 size_t room, size_t *len, size_t headlen,
6366 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006367 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006368{
Frédéric Lécailleea604992020-12-24 13:01:37 +01006369 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006370 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006371
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006372 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
6373
Frédéric Lécailleea604992020-12-24 13:01:37 +01006374 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006375 if (*len > room)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006376 goto leave;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006377
Frédéric Lécailleea604992020-12-24 13:01:37 +01006378 /* If we are not probing we must take into an account the congestion
6379 * control window.
6380 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006381 if (!qel->pktns->tx.pto_probe) {
6382 size_t remain = quic_path_prep_data(qc->path);
6383
6384 if (headlen > remain)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006385 goto leave;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006386
6387 room = QUIC_MIN(room, remain - headlen);
6388 }
6389
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006390 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006391 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006392
6393 /* NOTE: switch/case block inside a loop, a successful status must be
6394 * returned by this function only if at least one frame could be built
6395 * in the switch/case block.
6396 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006397 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006398 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006399 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006400
Frédéric Lécailleea604992020-12-24 13:01:37 +01006401 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006402 break;
6403
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006404 switch (cf->type) {
6405 case QUIC_FT_CRYPTO:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006406 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006407 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006408 /* Compute the length of this CRYPTO frame header */
6409 hlen = 1 + quic_int_getsize(cf->crypto.offset);
6410 /* Compute the data length of this CRyPTO frame. */
6411 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006412 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006413 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006414 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006415 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006416
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006417 /* CRYPTO frame length. */
6418 flen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006419 TRACE_DEVEL(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006420 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006421 /* Add the CRYPTO data length and its encoded length to the packet
6422 * length and the length of this length.
6423 */
6424 *len += flen;
6425 room -= flen;
6426 if (dlen == cf->crypto.len) {
6427 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006428 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006429 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006430 }
6431 else {
6432 struct quic_frame *new_cf;
6433
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006434 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006435 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006436 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006437 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006438 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006439
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006440 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006441 new_cf->type = QUIC_FT_CRYPTO;
6442 new_cf->crypto.len = dlen;
6443 new_cf->crypto.offset = cf->crypto.offset;
6444 new_cf->crypto.qel = qel;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006445 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6446 if (cf->origin) {
6447 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6448 /* This <cf> frame was duplicated */
6449 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6450 new_cf->origin = cf->origin;
6451 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006452 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006453 /* Consume <dlen> bytes of the current frame. */
6454 cf->crypto.len -= dlen;
6455 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006456 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006457 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006458
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006459 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006460 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
6461 struct eb64_node *node = NULL;
6462 struct qc_stream_desc *stream_desc = NULL;
6463 struct quic_stream *strm = &cf->stream;
6464
6465 /* As this frame has been already lost, ensure the stream is always
6466 * available or the range of this frame is not consumed before
6467 * resending it.
6468 */
6469 node = eb64_lookup(&qc->streams_by_id, strm->id);
6470 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006471 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006472 LIST_DELETE(&cf->list);
6473 pool_free(pool_head_quic_frame, cf);
6474 continue;
6475 }
6476
6477 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
6478 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006479 TRACE_DEVEL("ignored frame frame in already acked range",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006480 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006481 LIST_DELETE(&cf->list);
6482 pool_free(pool_head_quic_frame, cf);
6483 continue;
6484 }
6485 else if (strm->offset.key < stream_desc->ack_offset) {
6486 strm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006487 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006488 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006489 }
6490 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006491 /* Note that these frames are accepted in short packets only without
6492 * "Length" packet field. Here, <*len> is used only to compute the
6493 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01006494 *
6495 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006496 * excepting the variable ones. Note that +1 is for the type of this frame.
6497 */
6498 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006499 ((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 +02006500 /* Compute the data length of this STREAM frame. */
6501 avail_room = room - hlen - *len;
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006502 if ((ssize_t)avail_room <= 0)
6503 continue;
6504
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006505 TRACE_DEVEL(" New STREAM frame build (room, len)",
Frédéric Lécailleadc76412022-08-08 16:09:46 +02006506 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006507 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
6508 dlen = max_available_room(avail_room, &dlen_sz);
6509 if (dlen > cf->stream.len) {
6510 dlen = cf->stream.len;
6511 }
6512 dlen_sz = quic_int_getsize(dlen);
6513 flen = hlen + dlen_sz + dlen;
6514 }
6515 else {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02006516 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006517 flen = hlen + dlen;
6518 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006519 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006520 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006521 TRACE_DEVEL(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006522 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006523 /* Add the STREAM data length and its encoded length to the packet
6524 * length and the length of this length.
6525 */
6526 *len += flen;
6527 room -= flen;
6528 if (dlen == cf->stream.len) {
6529 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006530 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006531 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006532
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006533 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006534 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006535 qcc_streams_sent_done(cf->stream.stream->ctx,
6536 cf->stream.len,
6537 cf->stream.offset.key);
6538 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006539 }
6540 else {
6541 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006542 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006543
6544 new_cf = pool_zalloc(pool_head_quic_frame);
6545 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006546 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006547 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006548 }
6549
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006550 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006551 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006552 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006553 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006554 new_cf->stream.id = cf->stream.id;
6555 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
6556 new_cf->stream.offset = cf->stream.offset;
6557 new_cf->stream.len = dlen;
6558 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
6559 /* FIN bit reset */
6560 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
6561 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006562 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6563 if (cf->origin) {
6564 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6565 /* This <cf> frame was duplicated */
6566 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6567 new_cf->origin = cf->origin;
6568 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006569 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006570 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
6571 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006572 cf_buf = b_make(b_orig(cf->stream.buf),
6573 b_size(cf->stream.buf),
6574 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006575 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006576 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006577 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006578
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006579 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006580 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006581 qcc_streams_sent_done(new_cf->stream.stream->ctx,
6582 new_cf->stream.len,
6583 new_cf->stream.offset.key);
6584 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006585 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006586
6587 /* TODO the MUX is notified about the frame sending via
6588 * previous qcc_streams_sent_done call. However, the
6589 * sending can fail later, for example if the sendto
6590 * system call returns an error. As the MUX has been
6591 * notified, the transport layer is responsible to
6592 * bufferize and resent the announced data later.
6593 */
6594
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006595 break;
6596
6597 default:
6598 flen = qc_frm_len(cf);
6599 BUG_ON(!flen);
6600 if (flen > room)
6601 continue;
6602
6603 *len += flen;
6604 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006605 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006606 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006607 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006608 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006609
6610 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01006611 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006612 }
6613
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006614 leave:
6615 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécailleea604992020-12-24 13:01:37 +01006616 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006617}
6618
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006619/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
6620 * is used as return parameter and should be zero'ed by the caller.
6621 */
6622static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
6623 struct quic_frame *out)
6624{
6625 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
6626 *
6627 * A CONNECTION_CLOSE frame should be sent in several packets with
6628 * different encryption levels depending on the client context. This is
6629 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
6630 * more details on how to implement it.
6631 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006632 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
6633
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006634
6635 if (qc->err.app) {
6636 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
6637 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
6638 /* RFC 9000 10.2.3. Immediate Close during the Handshake
6639 *
6640 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
6641 * packet could expose application state or be used to alter application
6642 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
6643 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
6644 * Handshake packets. Otherwise, information about the application
6645 * state might be revealed. Endpoints MUST clear the value of the
6646 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
6647 * converting to a CONNECTION_CLOSE of type 0x1c.
6648 */
6649 out->type = QUIC_FT_CONNECTION_CLOSE;
6650 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
6651 out->connection_close.reason_phrase_len = 0;
6652 }
6653 else {
6654 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
6655 out->connection_close.error_code = qc->err.code;
6656 }
6657 }
6658 else {
6659 out->type = QUIC_FT_CONNECTION_CLOSE;
6660 out->connection_close.error_code = qc->err.code;
6661 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006662 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
6663
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006664}
6665
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006666/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006667 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
6668 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006669 * filling the buffer with as much frames as possible from <frms> list of
6670 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006671 * 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 +02006672 * 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 +02006673 * having returned from this function.
6674 * 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 +01006675 * number field in this packet. <pn_len> will also have the packet number
6676 * length as value.
6677 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006678 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006679 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006680static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
6681 size_t dglen, struct quic_tx_packet *pkt,
6682 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006683 int force_ack, int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006684 struct quic_enc_level *qel, struct quic_conn *qc,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006685 const struct quic_version *ver, struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006686{
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006687 unsigned char *beg, *payload;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006688 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006689 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
6690 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006691 struct quic_frame cc_frm = { };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006692 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006693 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006694 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006695 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006696 struct quic_frame *cf;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006697 int must_ack, ret = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006698 int nb_aepkts_since_last_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006699
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006700 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6701
Frédéric Lécailleea604992020-12-24 13:01:37 +01006702 /* Length field value with CRYPTO frames if present. */
6703 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006704 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006705 /* When not probing, and no immediate close is required, reduce the size of this
6706 * buffer to respect the congestion controller window.
6707 * 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 +01006708 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006709 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006710 size_t path_room;
6711
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006712 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006713 if (end - beg > path_room)
6714 end = beg + path_room;
6715 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006716
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006717 /* Ensure there is enough room for the TLS encryption tag and a zero token
6718 * length field if any.
6719 */
6720 if (end - pos < QUIC_TLS_TAG_LEN +
6721 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
6722 goto no_room;
6723
6724 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006725 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006726 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006727 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006728 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006729 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006730 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006731 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006732 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006733 goto no_room;
6734
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006735 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006736 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006737 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006738 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006739 /* Build an ACK frame if required. */
6740 ack_frm_len = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006741 nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
6742 must_ack = !qel->pktns->tx.pto_probe &&
6743 (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
6744 (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 +02006745 if (must_ack) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006746 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006747 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006748 ack_frm.tx_ack.arngs = arngs;
6749 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6750 qel->pktns->tx.ack_delay =
6751 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6752 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6753 }
6754 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006755 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6756 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6757 * that from here, we do not know if we will have to send a PING frame.
6758 * This will be decided after having computed the ack-eliciting frames
6759 * to be added to this packet.
6760 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006761 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 +01006762 if (!ack_frm_len)
6763 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006764 }
6765
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006766 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006767 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006768 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006769 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006770 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006771
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006772 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006773 /* Initialize the length of the frames built below to <len>.
6774 * If any frame could be successfully built by qc_build_frms(),
6775 * we will have len_frms > len.
6776 */
6777 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006778 if (!qc_build_frms(&frm_list, frms,
6779 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006780 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006781 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006782 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6783 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006784 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006785 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006786
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006787 /* Length (of the remaining data). Must not fail because, the buffer size
6788 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6789 * for the encryption tag. It must be taken into an account for the length
6790 * of this packet.
6791 */
6792 if (len_frms)
6793 len = len_frms + QUIC_TLS_TAG_LEN;
6794 else
6795 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006796 /* CONNECTION_CLOSE frame */
6797 if (cc) {
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006798 qc_build_cc_frm(qc, qel, &cc_frm);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006799 len += qc_frm_len(&cc_frm);
6800 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006801 add_ping_frm = 0;
6802 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006803 len_sz = quic_int_getsize(len);
6804 /* Add this packet size to <dglen> */
6805 dglen += head_len + len_sz + len;
6806 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6807 /* This is a maximum padding size */
6808 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6809 /* The length field value is of this packet is <len> + <padding_len>
6810 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006811 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006812 * sizes from <padding_len>.
6813 */
6814 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6815 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006816 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006817 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006818 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006819 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006820 add_ping_frm = 1;
6821 len += 1;
6822 }
6823 /* 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 +01006824 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006825 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6826 }
6827
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006828 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6829 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006830
6831 /* Packet number field address. */
6832 *buf_pn = pos;
6833
6834 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006835 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6836 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006837
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006838 /* payload building (ack-eliciting or not frames) */
6839 payload = pos;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006840 if (ack_frm_len) {
6841 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6842 goto no_room;
6843
6844 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006845 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006846 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006847
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006848 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006849 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillea8a60432022-08-23 11:42:48 +02006850 struct quic_frame *tmp_cf;
6851 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02006852 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006853 ssize_t room = end - pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006854 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006855 qc, NULL, NULL, &room);
Frédéric Lécaille149c5312022-08-27 15:51:30 +02006856 /* Note that <cf> was added from <frms> to <frm_list> list by
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006857 * qc_build_frms().
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006858 */
6859 LIST_DELETE(&cf->list);
6860 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille149c5312022-08-27 15:51:30 +02006861 continue;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006862 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006863
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006864 quic_tx_packet_refinc(pkt);
6865 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006866 }
6867 }
6868
6869 /* Build a PING frame if needed. */
6870 if (add_ping_frm) {
6871 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006872 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006873 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006874 }
6875
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006876 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006877 if (cc) {
6878 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6879 goto no_room;
6880
6881 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6882 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006883
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006884 /* Build a PADDING frame if needed. */
6885 if (padding_len) {
6886 frm.type = QUIC_FT_PADDING;
6887 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006888 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006889 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006890 }
6891
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006892 if (pos == payload) {
6893 /* No payload was built because of congestion control */
6894 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
6895 goto no_room;
6896 }
6897
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006898 /* If this packet is ack-eliciting and we are probing let's
6899 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006900 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006901 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6902 qel->pktns->tx.pto_probe)
6903 qel->pktns->tx.pto_probe--;
6904
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006905 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006906 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006907
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006908 ret = 1;
6909 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
6910 leave:
6911 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6912 return ret;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006913
6914 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006915 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006916 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006917 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
6918 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006919}
6920
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006921static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006922{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006923 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006924 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006925 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006926 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006927 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006928 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006929 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006930 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006931 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006932 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006933}
6934
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006935/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006936 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6937 * of prebuilt frames.
6938 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006939 * Return -2 if the packet could not be allocated or encrypted for any reason,
6940 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006941 * XXX NOTE XXX
6942 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6943 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6944 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006945 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006946static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6947 const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006948 struct quic_enc_level *qel,
6949 struct quic_tls_ctx *tls_ctx, struct list *frms,
6950 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006951 size_t dglen, int pkt_type, int force_ack,
6952 int padding, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006953{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006954 struct quic_tx_packet *ret_pkt = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006955 /* The pointer to the packet number field. */
6956 unsigned char *buf_pn;
6957 unsigned char *beg, *end, *payload;
6958 int64_t pn;
6959 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006960 struct quic_tx_packet *pkt;
6961
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006962 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006963 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006964 pkt = pool_alloc(pool_head_quic_tx_packet);
6965 if (!pkt) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006966 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006967 *err = -2;
6968 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006969 }
6970
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006971 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006972 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006973 pn_len = 0;
6974 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006975
6976 pn = qel->pktns->tx.next_pn + 1;
6977 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 +02006978 force_ack, padding, cc, probe, qel, qc, ver, frms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006979 // trace already emitted by function above
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006980 *err = -1;
6981 goto err;
6982 }
6983
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006984 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006985 payload = buf_pn + pn_len;
6986 payload_len = end - payload;
6987 aad_len = payload - beg;
6988
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006989 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006990 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006991 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006992 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006993 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006994
6995 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006996 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006997 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006998 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006999 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007000 goto err;
7001 }
7002
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01007003 /* Consume a packet number */
7004 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01007005 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007006 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02007007 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007008 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
7009 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02007010 /* Now that a correct packet is built, let us consume <*pos> buffer. */
7011 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007012 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02007013 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007014 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01007015 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02007016 pkt->in_flight_len = pkt->len;
7017 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01007018 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01007019 /* Always reset this flags */
7020 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02007021 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
7022 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
7023 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
7024 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02007025
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007026 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007027
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007028 ret_pkt = pkt;
7029 leave:
7030 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt);
7031 return ret_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007032
7033 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01007034 /* TODO: what about the frames which have been built
7035 * for this packet.
7036 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007037 free_quic_tx_packet(qc, pkt);
7038 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007039}
7040
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007041
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007042/* Called from the upper layer, to subscribe <es> to events <event_type>. The
7043 * event subscriber <es> is not allowed to change from a previous call as long
7044 * as at least one event is still subscribed. The <event_type> must only be a
7045 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
7046 */
7047static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7048{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007049 struct quic_conn *qc = conn->handle.qc;
7050 struct qcc *qcc = qc->qcc;
7051
7052 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007053
7054 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
7055 BUG_ON(qcc->subs && qcc->subs != es);
7056
7057 es->events |= event_type;
7058 qcc->subs = es;
7059
7060 if (event_type & SUB_RETRY_RECV)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007061 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007062
7063 if (event_type & SUB_RETRY_SEND)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007064 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7065
7066 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007067
7068 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007069}
7070
7071/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
7072 * The <es> pointer is not allowed to differ from the one passed to the
7073 * subscribe() call. It always returns zero.
7074 */
7075static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7076{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007077 int ret;
7078 struct quic_conn *qc = conn->handle.qc;
7079 struct qcc *qcc = qc->qcc;
7080
7081 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
7082
7083 if (event_type & SUB_RETRY_RECV)
7084 TRACE_DEVEL("unsubscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
7085 if (event_type & SUB_RETRY_SEND)
7086 TRACE_DEVEL("unsubscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7087
7088 ret = conn_unsubscribe(conn, xprt_ctx, event_type, es);
7089
7090 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
7091
7092 return ret;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007093}
7094
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007095/* Store in <xprt_ctx> the context attached to <conn>.
7096 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007097 */
7098static int qc_conn_init(struct connection *conn, void **xprt_ctx)
7099{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01007100 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007101
7102 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
7103
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007104 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007105 if (*xprt_ctx)
7106 goto out;
7107
Willy Tarreau784b8682022-04-11 14:18:10 +02007108 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007109
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007110 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01007111 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007112
7113 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007114}
7115
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007116/* Start the QUIC transport layer */
7117static int qc_xprt_start(struct connection *conn, void *ctx)
7118{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007119 int ret = 0;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007120 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02007121 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007122
Willy Tarreau784b8682022-04-11 14:18:10 +02007123 qc = conn->handle.qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007124 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007125 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
7126 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02007127 /* prepare a CONNECTION_CLOSE frame */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02007128 quic_set_connection_close(qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007129 goto out;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007130 }
7131
7132 /* mux-quic can now be considered ready. */
7133 qc->mux_state = QC_MUX_READY;
7134
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007135 tasklet_wakeup(qctx->wait_event.tasklet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007136
7137 ret = 1;
7138 out:
7139 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
7140 return ret;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007141}
7142
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007143static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
7144{
Willy Tarreau784b8682022-04-11 14:18:10 +02007145 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007146 return NULL;
7147
Willy Tarreau784b8682022-04-11 14:18:10 +02007148 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007149}
7150
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007151/* transport-layer operations for QUIC connections. */
7152static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02007153 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007154 .subscribe = quic_conn_subscribe,
7155 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007156 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007157 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007158 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
7159 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01007160 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007161 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007162 .name = "QUIC",
7163};
7164
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007165static void __quic_conn_init(void)
7166{
7167 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
7168 xprt_register(XPRT_QUIC, &ssl_quic);
7169}
Willy Tarreau79367f92022-04-25 19:18:24 +02007170INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007171
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007172static void __quic_conn_deinit(void)
7173{
7174 BIO_meth_free(ha_quic_meth);
7175}
Willy Tarreau79367f92022-04-25 19:18:24 +02007176REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007177
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01007178/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
7179 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05007180 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007181 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007182struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007183{
7184 unsigned char *pos;
7185 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007186 struct quic_dghdlr *dghdlr = ctx;
7187 struct quic_dgram *dgram;
7188 int first_pkt = 1;
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007189 struct list *tasklist_head = NULL;
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007190 int max_dgrams = global.tune.maxpollevents;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007191
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007192 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7193
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007194 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007195 pos = dgram->buf;
7196 end = pos + dgram->len;
7197 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007198 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007199
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007200 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007201 if (!pkt) {
7202 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
7203 goto leave;
7204 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007205
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007206 LIST_INIT(&pkt->qc_rx_pkt_list);
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02007207 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007208 quic_rx_packet_refinc(pkt);
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007209 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram, &tasklist_head);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007210 first_pkt = 0;
7211 pos += pkt->len;
7212 quic_rx_packet_refdec(pkt);
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007213
7214 /* Free rejected packets */
7215 if (!pkt->refcnt) {
7216 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
7217 pool_free(pool_head_quic_rx_packet, pkt);
7218 }
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007219 } while (pos < end);
7220
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007221 /* Increasing the received bytes counter by the UDP datagram length
7222 * if this datagram could be associated to a connection.
7223 */
7224 if (dgram->qc)
7225 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01007226
7227 /* Mark this datagram as consumed */
7228 HA_ATOMIC_STORE(&dgram->buf, NULL);
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007229
7230 if (--max_dgrams <= 0)
7231 goto stop_here;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007232 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007233
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007234 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
7235
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007236 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007237
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007238 stop_here:
7239 /* too much work done at once, come back here later */
7240 if (!MT_LIST_ISEMPTY(&dghdlr->dgrams))
7241 tasklet_wakeup((struct tasklet *)t);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007242 leave:
7243 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007244 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007245}
7246
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05007247/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007248 * Returns 1 if succeeded, 0 if not.
7249 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02007250int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
7251 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007252{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007253 int ret = 0, long_header;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007254 size_t minlen, skip;
7255
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007256 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
7257
7258 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
7259 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007260 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007261 }
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007262
7263 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02007264 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
7265 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007266 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Willy Tarreauaf5138f2022-08-05 10:09:32 +02007267 if (end - buf < minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007268 goto err;
7269
7270 buf += skip;
7271 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
7272 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
7273 goto err;
7274
7275 *dcid = buf;
7276
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007277 ret = 1;
7278 leave:
7279 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
7280 return ret;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007281
7282 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007283 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
7284 goto leave;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007285}
7286
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007287/* Notify the MUX layer if alive about an imminent close of <qc>. */
7288void qc_notify_close(struct quic_conn *qc)
7289{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007290 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
7291
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007292 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007293 goto leave;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007294
7295 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007296 /* wake up the MUX */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007297 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
7298 TRACE_STATE("connection closure notidfied to mux",
7299 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007300 qc->conn->mux->wake(qc->conn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007301 }
7302 else
7303 TRACE_STATE("connection closure not notidfied to mux",
7304 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
7305 leave:
7306 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007307}
7308
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007309/*
7310 * Local variables:
7311 * c-indent-level: 8
7312 * c-basic-offset: 8
7313 * End:
7314 */