blob: dded61af4f6893fb89a4b66f06e3798ad1928338 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01004 * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010015#include <stdio.h>
16#include <stdlib.h>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
22#include <netinet/tcp.h>
23
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020024#include <import/ebmbtree.h>
25
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010026#include <haproxy/buf-t.h>
27#include <haproxy/compat.h>
28#include <haproxy/api.h>
29#include <haproxy/debug.h>
30#include <haproxy/tools.h>
31#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010032
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010038#include <haproxy/hq_interop.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010039#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010040#include <haproxy/mux_quic.h>
Amaury Denoyelle3db98e92022-05-13 15:41:04 +020041#include <haproxy/ncbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010042#include <haproxy/pipe.h>
43#include <haproxy/proxy.h>
44#include <haproxy/quic_cc.h>
45#include <haproxy/quic_frame.h>
46#include <haproxy/quic_loss.h>
Amaury Denoyellecfa2d562022-01-19 16:01:05 +010047#include <haproxy/quic_sock.h>
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +020048#include <haproxy/quic_stats.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020049#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020050#include <haproxy/quic_tp.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020051#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010052#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010053#include <haproxy/quic_tls.h>
54#include <haproxy/ssl_sock.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010055#include <haproxy/task.h>
56#include <haproxy/trace.h>
57#include <haproxy/xprt_quic.h>
58
Amaury Denoyellea22d8602021-11-10 15:17:56 +010059/* list of supported QUIC versions by this implementation */
Frédéric Lécaille301425b2022-06-14 17:40:39 +020060const struct quic_version quic_versions[] = {
Frédéric Lécaille86845c52022-06-08 19:28:36 +020061 {
62 .num = QUIC_PROTOCOL_VERSION_DRAFT_29,
63 .initial_salt = initial_salt_draft_29,
64 .initial_salt_len = sizeof initial_salt_draft_29,
65 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020066 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020067 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020068 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020069 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020070 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020071 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020072 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020073 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT,
74 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT,
75 },
76 {
77 .num = QUIC_PROTOCOL_VERSION_1,
78 .initial_salt = initial_salt_v1,
79 .initial_salt_len = sizeof initial_salt_v1,
80 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020081 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020082 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020083 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020084 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020085 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020086 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020087 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020088 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1,
89 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1,
90 },
91 {
92 .num = QUIC_PROTOCOL_VERSION_2_DRAFT,
93 .initial_salt = initial_salt_v2_draft,
94 .initial_salt_len = sizeof initial_salt_v2_draft,
95 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020096 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020097 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020098 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020099 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +0200100 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200101 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +0200102 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200103 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2_DRAFT,
104 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2_DRAFT,
105 },
Amaury Denoyellea22d8602021-11-10 15:17:56 +0100106};
107
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200108/* The total number of supported versions */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200109const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions;
110/* Listener only preferred version */
111const struct quic_version *preferred_version;
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200112
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100113/* trace source and events */
114static void quic_trace(enum trace_level level, uint64_t mask, \
115 const struct trace_source *src,
116 const struct ist where, const struct ist func,
117 const void *a1, const void *a2, const void *a3, const void *a4);
118
119static const struct trace_event quic_trace_events[] = {
120 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
121 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
122 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
123 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
124 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
125 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
126 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500127 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200128 { .mask = QUIC_EV_CONN_TXPKT, .name = "tx_pkt", .desc = "TX packet" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100129 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
130 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200131 { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processing" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100132 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
133 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
134 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
135 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
136 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
137 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
138 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
139 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
140 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200141 { .mask = QUIC_EV_CONN_RXPKT, .name = "rx_pkt", .desc = "RX packet" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100142 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
143 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
144 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
145 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
146 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100147 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
148 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
149 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
150 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
151 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
152 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100153 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200154 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
155 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100156 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
157 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100158 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200159 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200160 { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"},
Frédéric Lécaillec7785b52022-05-23 09:08:54 +0200161 { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"},
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200162 { .mask = QUIC_EV_CONN_IDLE_TIMER, .name = "idle_timer", .desc = "idle timer task"},
163 { .mask = QUIC_EV_CONN_SUB, .name = "xprt_sub", .desc = "RX/TX subcription or unsubscription to QUIC xprt"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100164 { /* end */ }
165};
166
167static const struct name_desc quic_trace_lockon_args[4] = {
168 /* arg1 */ { /* already used by the connection */ },
169 /* arg2 */ { .name="quic", .desc="QUIC transport" },
170 /* arg3 */ { },
171 /* arg4 */ { }
172};
173
174static const struct name_desc quic_trace_decoding[] = {
175#define QUIC_VERB_CLEAN 1
176 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
177 { /* end */ }
178};
179
180
181struct trace_source trace_quic = {
182 .name = IST("quic"),
183 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100184 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100185 .default_cb = quic_trace,
186 .known_events = quic_trace_events,
187 .lockon_args = quic_trace_lockon_args,
188 .decoding = quic_trace_decoding,
189 .report_events = ~0, /* report everything by default */
190};
191
192#define TRACE_SOURCE &trace_quic
193INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
194
195static BIO_METHOD *ha_quic_meth;
196
Willy Tarreaub8dec4a2022-06-23 11:02:08 +0200197DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100198DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100199DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Willy Tarreaub8dec4a2022-06-23 11:02:08 +0200200 "quic_conn_ctx", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100201DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100202DECLARE_POOL(pool_head_quic_connection_id,
Willy Tarreaub8dec4a2022-06-23 11:02:08 +0200203 "quic_connnection_id", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100204DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Willy Tarreaub8dec4a2022-06-23 11:02:08 +0200205DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet));
206DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet));
207DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm", sizeof(struct quic_rx_crypto_frm));
208DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf", sizeof(struct quic_crypto_buf));
209DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame));
210DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100211
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200212static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200213 struct quic_enc_level *qel, struct quic_tls_ctx *ctx,
214 struct list *frms, struct quic_conn *qc,
215 const struct quic_version *ver, size_t dglen, int pkt_type,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +0200216 int force_ack, int padding, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100217static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécaille47756802022-03-25 09:12:16 +0100218static void qc_idle_timer_do_rearm(struct quic_conn *qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100219static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +0200220static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc);
221static int quic_conn_init_timer(struct quic_conn *qc);
222static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100223
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100224/* Only for debug purpose */
225struct enc_debug_info {
226 unsigned char *payload;
227 size_t payload_len;
228 unsigned char *aad;
229 size_t aad_len;
230 uint64_t pn;
231};
232
233/* Initializes a enc_debug_info struct (only for debug purpose) */
234static inline void enc_debug_info_init(struct enc_debug_info *edi,
235 unsigned char *payload, size_t payload_len,
236 unsigned char *aad, size_t aad_len, uint64_t pn)
237{
238 edi->payload = payload;
239 edi->payload_len = payload_len;
240 edi->aad = aad;
241 edi->aad_len = aad_len;
242 edi->pn = pn;
243}
244
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100245/* Trace callback for QUIC.
246 * These traces always expect that arg1, if non-null, is of type connection.
247 */
248static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
249 const struct ist where, const struct ist func,
250 const void *a1, const void *a2, const void *a3, const void *a4)
251{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100252 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100253
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100254 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100255 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100256
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100257 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200258 if (mask & QUIC_EV_CONN_INIT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100259 chunk_appendf(&trace_buf, "\n odcid");
260 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100261 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100262 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100263 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100264 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100265 }
266
Frédéric Lécaillec7785b52022-05-23 09:08:54 +0200267 if (mask & QUIC_EV_TRANSP_PARAMS) {
268 const struct quic_transport_params *p = a2;
Frédéric Lécaille4f5777a2022-06-20 19:39:26 +0200269 quic_transport_params_dump(&trace_buf, qc, p);
Frédéric Lécaillec7785b52022-05-23 09:08:54 +0200270 }
271
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100272 if (mask & QUIC_EV_CONN_ADDDATA) {
273 const enum ssl_encryption_level_t *level = a2;
274 const size_t *len = a3;
275
276 if (level) {
277 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
278
279 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
280 }
281 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100282 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100283 }
284 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
285 /* Initial read & write secrets. */
286 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
287 const unsigned char *rx_sec = a2;
288 const unsigned char *tx_sec = a3;
289
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100290 tls_ctx = &qc->els[level].tls_ctx;
291 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100292 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
293 if (rx_sec)
294 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100295 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100296 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
297 if (tx_sec)
298 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100299 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 }
301 }
302 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
303 const enum ssl_encryption_level_t *level = a2;
304 const unsigned char *secret = a3;
305 const size_t *secret_len = a4;
306
307 if (level) {
308 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
309
310 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
311 if (secret && secret_len)
312 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100313 tls_ctx = &qc->els[lvl].tls_ctx;
314 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
315 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100316 }
317 }
318
319 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
320 const enum ssl_encryption_level_t *level = a2;
321 const unsigned char *secret = a3;
322 const size_t *secret_len = a4;
323
324 if (level) {
325 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
326
327 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
328 if (secret && secret_len)
329 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100330 tls_ctx = &qc->els[lvl].tls_ctx;
331 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
332 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100333 }
334
335 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100336
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200337 if (mask & QUIC_EV_CONN_FRMLIST) {
338 const struct list *l = a2;
339
340 if (l) {
341 const struct quic_frame *frm;
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200342 list_for_each_entry(frm, l, list) {
343 chunk_appendf(&trace_buf, " frm@%p", frm);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200344 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200345 }
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200346 }
347 }
348
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200349 if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100350 const struct quic_tx_packet *pkt = a2;
351 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100352 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100353
354 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100355 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100356 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100357 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100358 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100359 (unsigned long long)qc->path->cwnd,
360 (unsigned long long)qc->path->prep_in_flight,
361 (unsigned long long)qc->path->in_flight,
362 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100363 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100364 }
365 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200366 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100367 if (pkt->pn_node.key != (uint64_t)-1)
368 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200369 list_for_each_entry(frm, &pkt->frms, list) {
370 chunk_appendf(&trace_buf, " frm@%p", frm);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100371 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200372 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100373 }
374
375 if (room) {
376 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
377 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
378 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100379 }
380 }
381
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100382 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100383 const enum quic_handshake_state *state = a2;
384 const int *err = a3;
385
386 if (state)
387 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
388 if (err)
389 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
390 }
391
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100392 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100393 const struct quic_rx_packet *pkt = a2;
394 const unsigned long *pktlen = a3;
395 const SSL *ssl = a4;
396
397 if (pkt) {
Frédéric Lécaille3dfd4c42022-04-05 15:29:14 +0200398 chunk_appendf(&trace_buf, " pkt@%p", pkt);
399 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
400 chunk_appendf(&trace_buf, " kp=%d",
401 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
402 chunk_appendf(&trace_buf, " el=%c",
403 quic_packet_type_enc_level_char(pkt->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100404 if (pkt->pnl)
405 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
406 (unsigned long long)pkt->pn);
407 if (pkt->token_len)
408 chunk_appendf(&trace_buf, " toklen=%llu",
409 (unsigned long long)pkt->token_len);
410 if (pkt->aad_len)
411 chunk_appendf(&trace_buf, " aadlen=%llu",
412 (unsigned long long)pkt->aad_len);
413 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
414 pkt->flags, (unsigned long long)pkt->len);
415 }
416 if (pktlen)
417 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
418 if (ssl) {
419 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
420 chunk_appendf(&trace_buf, " el=%c",
421 quic_enc_level_char(ssl_to_quic_enc_level(level)));
422 }
423 }
424
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200425 if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100426 const struct quic_rx_packet *pkt = a2;
427 const struct quic_rx_crypto_frm *cf = a3;
428 const SSL *ssl = a4;
429
430 if (pkt)
431 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
432 quic_packet_type_enc_level_char(pkt->type),
433 (unsigned long long)pkt->pn);
434 if (cf)
435 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
436 (unsigned long long)cf->offset_node.key,
437 (unsigned long long)cf->len);
438 if (ssl) {
439 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200440 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100441 quic_enc_level_char(ssl_to_quic_enc_level(level)));
442 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100443
Amaury Denoyelle57e6db72022-07-13 15:07:56 +0200444 if (qc->err.code)
445 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100446 }
447
448 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
449 const struct quic_frame *frm = a2;
450
451 if (frm)
452 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
453 }
454
455 if (mask & QUIC_EV_CONN_PHPKTS) {
456 const struct quic_enc_level *qel = a2;
457
458 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100459 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100460 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100461 " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100462 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200463 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200464 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100465 (unsigned long long)qc->path->cwnd,
466 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100467 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100468 (unsigned long long)pktns->tx.in_flight,
469 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100470 }
471 }
472
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100473 if (mask & QUIC_EV_CONN_ENCPKT) {
474 const struct enc_debug_info *edi = a2;
475
476 if (edi)
477 chunk_appendf(&trace_buf,
478 " payload=@%p payload_len=%llu"
479 " aad=@%p aad_len=%llu pn=%llu",
480 edi->payload, (unsigned long long)edi->payload_len,
481 edi->aad, (unsigned long long)edi->aad_len,
482 (unsigned long long)edi->pn);
483 }
484
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100485 if (mask & QUIC_EV_CONN_RMHP) {
486 const struct quic_rx_packet *pkt = a2;
487
488 if (pkt) {
489 const int *ret = a3;
490
491 chunk_appendf(&trace_buf, " pkt@%p", pkt);
492 if (ret && *ret)
493 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
494 pkt->pnl, (unsigned long long)pkt->pn);
495 }
496 }
497
498 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200499 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500 const unsigned long *val1 = a3;
501 const unsigned long *val2 = a4;
502
Frédéric Lécaille2f163482022-08-19 09:23:15 +0200503 if (frm) {
504 chunk_appendf(&trace_buf, " frm@%p", frm);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100505 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille2f163482022-08-19 09:23:15 +0200506 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100507 if (val1)
508 chunk_appendf(&trace_buf, " %lu", *val1);
509 if (val2)
510 chunk_appendf(&trace_buf, "..%lu", *val2);
511 }
512
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100513 if (mask & QUIC_EV_CONN_ACKSTRM) {
514 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200515 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100516
517 if (s)
518 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200519 if (stream)
520 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100521 }
522
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100523 if (mask & QUIC_EV_CONN_RTTUPDT) {
524 const unsigned int *rtt_sample = a2;
525 const unsigned int *ack_delay = a3;
526 const struct quic_loss *ql = a4;
527
528 if (rtt_sample)
529 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
530 if (ack_delay)
531 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
532 if (ql)
533 chunk_appendf(&trace_buf,
534 " srtt=%ums rttvar=%ums min_rtt=%ums",
535 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
536 }
537 if (mask & QUIC_EV_CONN_CC) {
538 const struct quic_cc_event *ev = a2;
539 const struct quic_cc *cc = a3;
540
541 if (a2)
542 quic_cc_event_trace(&trace_buf, ev);
543 if (a3)
544 quic_cc_state_trace(&trace_buf, cc);
545 }
546
547 if (mask & QUIC_EV_CONN_PKTLOSS) {
548 const struct quic_pktns *pktns = a2;
549 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100550
551 if (pktns) {
552 chunk_appendf(&trace_buf, " pktns=%s",
553 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
554 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
555 if (pktns->tx.loss_time)
556 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100557 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100558 }
559 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
560 struct quic_tx_packet *pkt;
561
562 chunk_appendf(&trace_buf, " lost_pkts:");
563 list_for_each_entry(pkt, lost_pkts, list)
564 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
565 }
566 }
567
568 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100569 const struct quic_pktns *pktns = a2;
570 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100571 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100572
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100573 if (ifae_pkts)
574 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
575 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100576 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100577 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100578 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100579 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
580 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100581 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
582 if (pktns->tx.in_flight)
583 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100584 if (pktns->tx.loss_time)
585 chunk_appendf(&trace_buf, " loss_time=%dms",
586 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
587 }
588 if (mask & QUIC_EV_CONN_SPTO) {
589 if (pktns->tx.time_of_last_eliciting)
590 chunk_appendf(&trace_buf, " tole=%dms",
591 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
592 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100593 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100594 }
595 }
596
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200597 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598 chunk_appendf(&trace_buf,
599 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
600 }
601 }
602
603 if (mask & QUIC_EV_CONN_SPPKTS) {
604 const struct quic_tx_packet *pkt = a2;
605
Frédéric Lécaille8ecb7362022-08-04 12:00:00 +0200606 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100607 (unsigned long long)qc->path->cwnd,
608 (unsigned long long)qc->path->prep_in_flight,
609 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100610 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100611 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100612 (unsigned long)pkt->pn_node.key,
613 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
614 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100615 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +0200616 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
617 (unsigned long long)qc->rx.bytes,
618 (unsigned long long)qc->tx.bytes);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100619 }
620 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100621
622 if (mask & QUIC_EV_CONN_SSLALERT) {
623 const uint8_t *alert = a2;
624 const enum ssl_encryption_level_t *level = a3;
625
626 if (alert)
627 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
628 if (level)
629 chunk_appendf(&trace_buf, " el=%c",
630 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
631 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100632
633 if (mask & QUIC_EV_CONN_BCFRMS) {
634 const size_t *sz1 = a2;
635 const size_t *sz2 = a3;
636 const size_t *sz3 = a4;
637
638 if (sz1)
639 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
640 if (sz2)
641 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
642 if (sz3)
643 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
644 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100645
646 if (mask & QUIC_EV_CONN_PSTRM) {
647 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100648
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200649 if (frm) {
650 chunk_appendf(&trace_buf, " frm@%p", frm);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100651 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille26236f52022-08-31 17:48:53 +0200652 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100653 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100654 }
655 if (mask & QUIC_EV_CONN_LPKT) {
656 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200657 const uint64_t *len = a3;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200658 const struct quic_version *ver = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100659
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100660 if (pkt) {
661 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
662 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
663 if (pkt->pn_node.key != (uint64_t)-1)
664 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
665 }
666
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200667 if (len)
668 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200669
670 if (ver)
671 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100672 }
673
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200674 if (mask & QUIC_EV_STATELESS_RST) {
675 const struct quic_cid *cid = a2;
676
677 if (cid)
678 quic_cid_dump(&trace_buf, cid);
679 }
680
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100681}
682
683/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100684static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100685{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200686 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100687
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100688 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100689 return 1;
690
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200691 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
692 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200693 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
694 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200695 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100696 return 1;
697
698 return 0;
699}
700
701/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
702 * both loss detection and PTO and schedule the task assiated to this timer if needed.
703 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100704static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100705{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100706 struct quic_pktns *pktns;
707 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200708 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100709
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100710 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100711 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100712
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100713 pktns = quic_loss_pktns(qc);
714 if (tick_isset(pktns->tx.loss_time)) {
715 qc->timer = pktns->tx.loss_time;
716 goto out;
717 }
718
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100719 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100720 * cancelled for a server which reached the anti-amplification limit.
721 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100722 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200723 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100724 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100725 qc->timer = TICK_ETERNITY;
726 goto out;
727 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100728
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100729 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100730 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100731 /* Timer cancellation. */
732 qc->timer = TICK_ETERNITY;
733 goto out;
734 }
735
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200736 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200737 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100738 if (tick_isset(pto))
739 qc->timer = pto;
740 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100741 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100742 if (tick_is_expired(qc->timer, now_ms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200743 TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100744 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100745 }
746 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200747 TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100748 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100749 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100750 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100751 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100752}
753
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100754/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
755 * connection.
756 * Return 1 if succeeded, 0 if not.
757 */
758static int quic_tls_key_update(struct quic_conn *qc)
759{
760 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
761 struct quic_tls_secrets *rx, *tx;
762 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
763 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200764 const struct quic_version *ver =
765 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200766 int ret = 0;
767
768 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100769
770 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
771 rx = &tls_ctx->rx;
772 tx = &tls_ctx->tx;
773 nxt_rx = &qc->ku.nxt_rx;
774 nxt_tx = &qc->ku.nxt_tx;
775
776 /* Prepare new RX secrets */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200777 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100778 rx->secret, rx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200779 TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
780 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100781 }
782
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200783 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100784 nxt_rx->key, nxt_rx->keylen,
785 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
786 nxt_rx->secret, nxt_rx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200787 TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
788 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100789 }
790
791 /* Prepare new TX secrets */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200792 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100793 tx->secret, tx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200794 TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
795 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100796 }
797
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200798 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100799 nxt_tx->key, nxt_tx->keylen,
800 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
801 nxt_tx->secret, nxt_tx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200802 TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
803 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100804 }
805
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200806 if (nxt_rx->ctx) {
807 EVP_CIPHER_CTX_free(nxt_rx->ctx);
808 nxt_rx->ctx = NULL;
809 }
810
811 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 +0200812 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
813 goto leave;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200814 }
815
816 if (nxt_tx->ctx) {
817 EVP_CIPHER_CTX_free(nxt_tx->ctx);
818 nxt_tx->ctx = NULL;
819 }
820
821 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 +0200822 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
823 goto leave;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200824 }
825
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200826 ret = 1;
827 leave:
828 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc);
829 return ret;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100830}
831
832/* Rotate the Key Update information for <qc> QUIC connection.
833 * Must be used after having updated them.
834 * Always succeeds.
835 */
836static void quic_tls_rotate_keys(struct quic_conn *qc)
837{
838 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
839 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200840 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100841
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200842 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
843
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100844 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200845 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100846 curr_secret = tls_ctx->rx.secret;
847 curr_iv = tls_ctx->rx.iv;
848 curr_key = tls_ctx->rx.key;
849
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200850 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100851 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
852 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
853 tls_ctx->rx.key = qc->ku.nxt_rx.key;
854
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200855 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100856 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
857 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
858 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
859
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200860 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100861 qc->ku.prv_rx.secret = curr_secret;
862 qc->ku.prv_rx.iv = curr_iv;
863 qc->ku.prv_rx.key = curr_key;
864 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
865
866 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200867 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100868 curr_secret = tls_ctx->tx.secret;
869 curr_iv = tls_ctx->tx.iv;
870 curr_key = tls_ctx->tx.key;
871
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200872 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100873 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
874 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
875 tls_ctx->tx.key = qc->ku.nxt_tx.key;
876
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200877 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100878 qc->ku.nxt_tx.secret = curr_secret;
879 qc->ku.nxt_tx.iv = curr_iv;
880 qc->ku.nxt_tx.key = curr_key;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200881
882 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100883}
884
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200885/* returns 0 on error, 1 on success */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100886int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
887 const uint8_t *read_secret,
888 const uint8_t *write_secret, size_t secret_len)
889{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100890 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
891 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 +0100892 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100893 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200894 const struct quic_version *ver =
895 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200896 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100897
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100898 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100899 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200900 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100901 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100902 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100903 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100904
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100905 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200906 TRACE_ERROR("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
907 goto leave;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100908 }
909
910 rx = &tls_ctx->rx;
911 tx = &tls_ctx->tx;
912
913 rx->aead = tx->aead = tls_aead(cipher);
914 rx->md = tx->md = tls_md(cipher);
915 rx->hp = tx->hp = tls_hp(cipher);
916
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200917 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 +0100918 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100919 read_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200920 TRACE_ERROR("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
921 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100922 }
923
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200924 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200925 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
926 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200927 }
928
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200929 if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) {
930 TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
931 goto leave;
932 }
933
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100934 /* Enqueue this connection asap if we could derive O-RTT secrets as
935 * listener. Note that a listener derives only RX secrets for this
936 * level.
937 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200938 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
939 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100940 quic_accept_push_qc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200941 }
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100942
943 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100944 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100945
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200946 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 +0100947 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100948 write_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200949 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
950 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100951 }
952
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200953 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200954 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
955 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200956 }
957
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200958 if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) {
959 TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
960 goto leave;
961 }
962
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100963 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100964 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
965 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
966 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
967
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200968 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100969 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
970 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200971 TRACE_ERROR("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
972 goto leave;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100973 }
974
975 memcpy(rx->secret, read_secret, secret_len);
976 rx->secretlen = secret_len;
977 memcpy(tx->secret, write_secret, secret_len);
978 tx->secretlen = secret_len;
979 /* Initialize all the secret keys lengths */
980 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
981 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200982 if (!quic_tls_key_update(qc)) {
983 // trace already emitted by function above
984 goto leave;
985 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100986 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100987
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100988 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100989 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100990 no_secret:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200991 ret = 1;
992 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100993 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200994 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100995}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100996
997/* This function copies the CRYPTO data provided by the TLS stack found at <data>
998 * with <len> as size in CRYPTO buffers dedicated to store the information about
999 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001000 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
1001 * buffers to store all the data.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001002 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1003 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001004static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001005 const unsigned char *data, size_t len)
1006{
1007 struct quic_crypto_buf **qcb;
1008 /* The remaining byte to store in CRYPTO buffers. */
1009 size_t cf_offset, cf_len, *nb_buf;
1010 unsigned char *pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001011 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001012
1013 nb_buf = &qel->tx.crypto.nb_buf;
1014 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1015 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1016 cf_len = len;
1017
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001018 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1019
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001020 while (len) {
1021 size_t to_copy, room;
1022
1023 pos = (*qcb)->data + (*qcb)->sz;
1024 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1025 to_copy = len > room ? room : len;
1026 if (to_copy) {
1027 memcpy(pos, data, to_copy);
1028 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1029 qel->tx.crypto.sz += to_copy;
1030 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001031 len -= to_copy;
1032 data += to_copy;
1033 }
1034 else {
1035 struct quic_crypto_buf **tmp;
1036
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001037 // FIXME: realloc!
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001038 tmp = realloc(qel->tx.crypto.bufs,
1039 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1040 if (tmp) {
1041 qel->tx.crypto.bufs = tmp;
1042 qcb = &qel->tx.crypto.bufs[*nb_buf];
1043 *qcb = pool_alloc(pool_head_quic_crypto_buf);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001044 if (!*qcb) {
1045 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1046 goto leave;
1047 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001048
1049 (*qcb)->sz = 0;
1050 ++*nb_buf;
1051 }
1052 else {
1053 break;
1054 }
1055 }
1056 }
1057
1058 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1059 * have been buffered.
1060 */
1061 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001062 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001063 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001064
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001065 /* There is at most one CRYPTO frame in this packet number
1066 * space. Let's look for it.
1067 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001068 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001069 if (frm->type != QUIC_FT_CRYPTO)
1070 continue;
1071
1072 /* Found */
1073 found = frm;
1074 break;
1075 }
1076
1077 if (found) {
1078 found->crypto.len += cf_len;
1079 }
1080 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001081 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001082 if (!frm) {
1083 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1084 goto leave;
1085 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001086
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001087 LIST_INIT(&frm->reflist);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001088 frm->type = QUIC_FT_CRYPTO;
1089 frm->crypto.offset = cf_offset;
1090 frm->crypto.len = cf_len;
1091 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001092 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001093 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001094 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001095 ret = len == 0;
1096 leave:
1097 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1098 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099}
1100
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001101/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1102 * activity for <qc> will be interrupted.
1103 */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001104void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001105{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001106 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001107 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001108 goto leave;
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001109
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001110 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001111 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001112 qc->err.code = err.code;
1113 qc->err.app = err.app;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001114 leave:
1115 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001116}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001117
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001118/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1119void quic_set_tls_alert(struct quic_conn *qc, int alert)
1120{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001121 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1122
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001123 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1124 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001125 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001126 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1127 }
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001128 quic_set_connection_close(qc, quic_err_tls(alert));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001129 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001130 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1131
1132 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001133}
1134
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001135/* Set the application for <qc> QUIC connection.
1136 * Return 1 if succeeded, 0 if not.
1137 */
1138int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1139{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001140 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1141 qc->app_ops = &h3_ops;
1142 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1143 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001144 else
1145 return 0;
1146
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001147 return 1;
1148}
1149
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001150/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1151 * wants to provide the QUIC layer with CRYPTO data.
1152 * Returns 1 if succeeded, 0 if not.
1153 */
1154int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1155 const uint8_t *data, size_t len)
1156{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001157 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001158 enum quic_tls_enc_level tel;
1159 struct quic_enc_level *qel;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001160 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001161
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001162 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1163 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001164
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001165 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001166 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001167 goto out;
1168 }
1169
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001170 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001171 if (tel == -1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001172 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1173 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001174 }
1175
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001176 qel = &qc->els[tel];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001177 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1178 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1179 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001180 }
1181
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001182 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001183 qc, &level, &len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001184 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001185 ret = 1;
1186 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001187 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001188 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001189}
1190
1191int ha_quic_flush_flight(SSL *ssl)
1192{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001193 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001194
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001195 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1196 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001197
1198 return 1;
1199}
1200
1201int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
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
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001205 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1206
1207 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1208
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001209 quic_set_tls_alert(qc, alert);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001210 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001211 return 1;
1212}
1213
1214/* QUIC TLS methods */
1215static SSL_QUIC_METHOD ha_quic_method = {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001216 .set_encryption_secrets = ha_quic_set_encryption_secrets,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001217 .add_handshake_data = ha_quic_add_handshake_data,
1218 .flush_flight = ha_quic_flush_flight,
1219 .send_alert = ha_quic_send_alert,
1220};
1221
1222/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1223 * Returns an error count.
1224 */
1225int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1226{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001227 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1228 int cfgerr = 0;
1229
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001230 long options =
1231 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1232 SSL_OP_SINGLE_ECDH_USE |
1233 SSL_OP_CIPHER_SERVER_PREFERENCE;
1234 SSL_CTX *ctx;
1235
1236 ctx = SSL_CTX_new(TLS_server_method());
1237 bind_conf->initial_ctx = ctx;
1238
1239 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001240 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1241 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1242 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001243
1244#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand373ce732022-09-07 11:11:59 +02001245# if defined(HAVE_SSL_CLIENT_HELLO_CB)
1246# if defined(SSL_OP_NO_ANTI_REPLAY)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001247 if (bind_conf->ssl_conf.early_data) {
1248 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001249 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001250 }
William Lallemand373ce732022-09-07 11:11:59 +02001251# endif /* !SSL_OP_NO_ANTI_REPLAY */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001252 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1253 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
William Lallemand373ce732022-09-07 11:11:59 +02001254# else /* ! HAVE_SSL_CLIENT_HELLO_CB */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001255 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
William Lallemand373ce732022-09-07 11:11:59 +02001256# endif
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001257 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1258#endif
1259 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1260
1261 return cfgerr;
1262}
1263
1264/* Decode an expected packet number from <truncated_on> its truncated value,
1265 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1266 * the number of bits used to encode this packet number (its length in bytes * 8).
1267 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1268 */
1269static uint64_t decode_packet_number(uint64_t largest_pn,
1270 uint32_t truncated_pn, unsigned int pn_nbits)
1271{
1272 uint64_t expected_pn = largest_pn + 1;
1273 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1274 uint64_t pn_hwin = pn_win / 2;
1275 uint64_t pn_mask = pn_win - 1;
1276 uint64_t candidate_pn;
1277
1278
1279 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1280 /* Note that <pn_win> > <pn_hwin>. */
1281 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1282 candidate_pn + pn_hwin <= expected_pn)
1283 return candidate_pn + pn_win;
1284
1285 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1286 return candidate_pn - pn_win;
1287
1288 return candidate_pn;
1289}
1290
1291/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1292 * cryptographic context.
1293 * <largest_pn> is the largest received packet number and <pn> the address of
1294 * the packet number field for this packet with <byte0> address of its first byte.
1295 * <end> points to one byte past the end of this packet.
1296 * Returns 1 if succeeded, 0 if not.
1297 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001298static int qc_do_rm_hp(struct quic_conn *qc,
1299 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001300 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001301{
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02001302 int ret, i, pnlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001303 uint64_t packet_number;
1304 uint32_t truncated_pn = 0;
1305 unsigned char mask[5] = {0};
1306 unsigned char *sample;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001307 EVP_CIPHER_CTX *cctx = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001309 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1310
1311 ret = 0;
1312
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001313 /* Check there is enough data in this packet. */
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001314 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001315 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1316 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001317 }
1318
1319 cctx = EVP_CIPHER_CTX_new();
1320 if (!cctx) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001321 TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
1322 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001323 }
1324
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001325 sample = pn + QUIC_PACKET_PN_MAXLEN;
1326
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02001327 if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
1328 TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001329 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001330 }
1331
1332 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1333 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1334 for (i = 0; i < pnlen; i++) {
1335 pn[i] ^= mask[i + 1];
1336 truncated_pn = (truncated_pn << 8) | pn[i];
1337 }
1338
1339 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1340 /* Store remaining information for this unprotected header */
1341 pkt->pn = packet_number;
1342 pkt->pnl = pnlen;
1343
1344 ret = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001345 leave:
1346 if (cctx)
1347 EVP_CIPHER_CTX_free(cctx);
1348 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001349 return ret;
1350}
1351
1352/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1353 * address, with <payload_len> as payload length, <aad> as address of
1354 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1355 * context.
1356 * Returns 1 if succeeded, 0 if not.
1357 */
1358static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1359 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001360 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001361{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001362 int ret = 0;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001363 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001364 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001365 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001366 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001367
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001368 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
1369
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001370 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001371 TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001372 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001373 }
1374
1375 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001376 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001377 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001378 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001379 }
1380
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001381 ret = 1;
1382 leave:
1383 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
1384 return ret;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001385
1386 err:
1387 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001388 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001389}
1390
1391/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1392 * Returns 1 if succeeded, 0 if not.
1393 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001394static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel,
1395 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001396{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001397 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001398 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001399 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001400 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001401 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001402 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1403 unsigned char *rx_key = tls_ctx->rx.key;
1404
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001405 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1406
1407 ret = 0;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001408 kp_changed = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001409
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001410 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1411 /* The two tested bits are not at the same position,
1412 * this is why they are first both inversed.
1413 */
1414 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1415 if (pkt->pn < tls_ctx->rx.pn) {
1416 /* The lowest packet number of a previous key phase
1417 * cannot be null if it really stores previous key phase
1418 * secrets.
1419 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001420 // TODO: check if BUG_ON() more suitable
1421 if (!pkt->qc->ku.prv_rx.pn) {
1422 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1423 goto leave;
1424 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001425
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001426 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1427 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001428 rx_key = pkt->qc->ku.prv_rx.key;
1429 }
1430 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1431 /* Next key phase */
1432 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001433 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1434 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001435 rx_key = pkt->qc->ku.nxt_rx.key;
1436 }
1437 }
1438 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001439
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001440 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) {
1441 TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc);
1442 goto leave;
1443 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001444
1445 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1446 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001447 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001448 if (!ret) {
1449 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1450 goto leave;
1451 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001452
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001453 /* Update the keys only if the packet decryption succeeded. */
1454 if (kp_changed) {
1455 quic_tls_rotate_keys(pkt->qc);
1456 /* Toggle the Key Phase bit */
1457 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1458 /* Store the lowest packet number received for the current key phase */
1459 tls_ctx->rx.pn = pkt->pn;
1460 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001461 if (!quic_tls_key_update(pkt->qc)) {
1462 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1463 goto leave;
1464 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001465 }
1466
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001467 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001468 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001469 ret = 1;
1470 leave:
1471 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille11a6f402022-08-16 14:48:59 +02001472 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001473}
1474
Frédéric Lécaille96367152022-04-25 09:40:19 +02001475
1476/* Remove references to <frm> frame */
1477static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1478{
Frédéric Lécaille96367152022-04-25 09:40:19 +02001479 struct quic_frame *f, *tmp;
1480
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001481 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1482
Frédéric Lécaille96367152022-04-25 09:40:19 +02001483 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001484 f->origin = NULL;
1485 LIST_DELETE(&f->ref);
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001486 if (f->pkt) {
1487 TRACE_DEVEL("remove frame reference",
1488 QUIC_EV_CONN_PRSAFRM, qc, f, &f->pkt->pn_node.key);
1489 }
1490 else {
1491 TRACE_DEVEL("remove frame reference for unsent frame",
1492 QUIC_EV_CONN_PRSAFRM, qc, f);
1493 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001494 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001495
1496 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001497}
1498
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001499/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001500void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001501{
1502 uint64_t pn;
1503 struct quic_frame *origin, *f, *tmp;
1504
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001505 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1506
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001507 /* Identify this frame: a frame copy or one of its copies */
1508 origin = frm->origin ? frm->origin : frm;
1509 /* Ensure the source of the copies is flagged as acked, <frm> being
1510 * possibly a copy of <origin>
1511 */
1512 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1513 /* Mark all the copy of <origin> as acknowledged. We must
1514 * not release the packets (releasing the frames) at this time as
1515 * they are possibly also to be acknowledged alongside the
1516 * the current one.
1517 */
1518 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001519 if (f->pkt) {
Frédéric Lécaille025945f2022-09-06 09:55:21 +02001520 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1521 f->origin = NULL;
1522 LIST_DELETE(&f->ref);
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001523 pn = f->pkt->pn_node.key;
1524 TRACE_DEVEL("mark frame as acked from packet",
1525 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1526 }
1527 else {
Frédéric Lécaille025945f2022-09-06 09:55:21 +02001528 TRACE_DEVEL("freeing unsent frame",
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001529 QUIC_EV_CONN_PRSAFRM, qc, f);
Frédéric Lécaille025945f2022-09-06 09:55:21 +02001530 LIST_DELETE(&f->ref);
1531 LIST_DELETE(&f->list);
1532 pool_free(pool_head_quic_frame, f);
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001533 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001534 }
1535 LIST_DELETE(&frm->list);
1536 pn = frm->pkt->pn_node.key;
1537 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001538 TRACE_DEVEL("freeing frame from packet",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001539 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1540 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001541
1542 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001543}
1544
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001545/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1546 * and all STREAM data are acknowledged. The MUX is responsible to have set
1547 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1548 *
1549 * TODO this should also be called on lost packet detection
1550 */
1551static void qc_check_close_on_released_mux(struct quic_conn *qc)
1552{
1553 struct ssl_sock_ctx *ctx = qc->xprt_ctx;
1554
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001555 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1556
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001557 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1558 /* Reuse errcode which should have been previously set by the MUX on release. */
1559 quic_set_connection_close(qc, qc->err);
1560 tasklet_wakeup(ctx->wait_event.tasklet);
1561 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001562
1563 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001564}
1565
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001566/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001567 *
1568 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001569 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001570static int quic_stream_try_to_consume(struct quic_conn *qc,
1571 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001572{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001573 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001574 struct eb64_node *frm_node;
1575
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001576 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1577
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001578 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001579 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001580 while (frm_node) {
1581 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001582 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001583 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001584
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001585 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001586 offset = strm->offset.key;
1587 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001588
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001589 if (offset > stream->ack_offset)
1590 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001591
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001592 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001593 /* cf. next comment : frame may be freed at this stage. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001594 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001595 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001596 ret = 1;
1597 }
1598
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001599 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1600 * has been freed. with the stream frames tree. Nothing to do
1601 * anymore in here.
1602 */
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001603 if (!stream) {
1604 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001605 ret = 1;
1606 goto leave;
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001607 }
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001608
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001609 frm_node = eb64_next(frm_node);
1610 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001611
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001612 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001613 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001614 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001615
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001616 leave:
1617 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001618 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001619}
1620
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001621/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001622static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1623 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001624{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001625 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001626
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001627 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm);
1628
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001629 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001630 switch (frm->type) {
1631 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1632 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001633 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001634 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001635 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001636 const size_t offset = strm_frm->offset.key;
1637 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001638
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001639 /* do not use strm_frm->stream as the qc_stream_desc instance
1640 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001641 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001642 *
1643 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001644 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001645 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001646 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1647 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001648 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001649 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001650 /* early return */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001651 goto leave;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001652 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001653 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001654
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001655 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001656 if (offset <= stream->ack_offset) {
1657 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001658 stream_acked = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001659 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001660 qc, strm_frm, stream);
1661 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001662
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001663 if (!stream) {
1664 /* no need to continue if stream freed. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001665 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001666 qc_release_frm(qc, frm);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001667 qc_check_close_on_released_mux(qc);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001668 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001669 }
1670
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001671 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Frédéric Lécailleda342552022-04-25 10:28:49 +02001672 qc, strm_frm, stream);
1673 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001674 }
1675 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001676 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001677 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001678
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001679 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001680 }
1681 break;
1682 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001683 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001684 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001685
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001686 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001687 struct qcc *qcc = qc->qcc;
1688
1689 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1690 tasklet_wakeup(qcc->subs->tasklet);
1691 qcc->subs->events &= ~SUB_RETRY_SEND;
1692 if (!qcc->subs->events)
1693 qcc->subs = NULL;
1694 }
1695 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001696 leave:
1697 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001698}
1699
1700/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1701 * deallocating them, and their TX frames.
1702 * Returns the last node reached to be used for the next range.
1703 * May be NULL if <largest> node could not be found.
1704 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001705static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1706 struct eb_root *pkts,
1707 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001708 struct list *newly_acked_pkts,
1709 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001710 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001711{
1712 struct eb64_node *node;
1713 struct quic_tx_packet *pkt;
1714
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001715 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1716
Frédéric Lécaille843399f2022-07-22 16:27:44 +02001717 node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001718 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001719 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001720
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001721 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001722 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001723 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001724 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001725 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001726 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001727 node = eb64_prev(node);
1728 eb64_delete(&pkt->pn_node);
1729 }
1730
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001731 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001732 return node;
1733}
1734
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001735/* Remove all frames from <pkt_frm_list> and reinsert them in the
1736 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001737 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001738static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001739 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001740 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001741{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001742 struct quic_frame *frm, *frmbak;
1743 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001744 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001745 uint64_t pn = pkt->pn_node.key;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001746
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001747 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1748
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001749 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001750 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001751 LIST_DELETE(&frm->list);
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001752 quic_tx_packet_refdec(pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001753 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001754 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001755 /* Remove any reference to this frame */
1756 qc_frm_unref(qc, frm);
1757 switch (frm->type) {
1758 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1759 {
1760 struct quic_stream *strm_frm = &frm->stream;
1761 struct eb64_node *node = NULL;
1762 struct qc_stream_desc *stream_desc;
1763
1764 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1765 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001766 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1767 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001768 qc, frm, &pn);
1769 pool_free(pool_head_quic_frame, frm);
1770 continue;
1771 }
1772
1773 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1774 /* Do not resend this frame if in the "already acked range" */
1775 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001776 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001777 QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillebccbad22022-08-31 15:02:53 +02001778 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001779 continue;
1780 }
1781 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1782 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001783 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001784 QUIC_EV_CONN_PRSAFRM, qc, frm);
1785 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001786 break;
1787 }
1788
1789 default:
1790 break;
1791 }
1792
1793 /* Do not resend probing packet with old data */
1794 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001795 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1796 qc, frm, &pn);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001797 if (frm->origin)
1798 LIST_DELETE(&frm->ref);
1799 pool_free(pool_head_quic_frame, frm);
1800 continue;
1801 }
1802
1803 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001804 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1805 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001806 qc, frm, &pn);
1807 pool_free(pool_head_quic_frame, frm);
1808 }
1809 else {
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001810 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1811 /* Mark this STREAM frame as lost. A look up their stream descriptor
1812 * will be performed to check the stream is not consumed or released.
1813 */
Amaury Denoyellef372e742022-08-16 14:41:57 +02001814 frm->flags |= QUIC_FL_TX_FRAME_LOST;
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001815 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001816 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001817 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001818 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001819 }
1820
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001821 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001822
1823 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001824}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001825
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001826/* Free <pkt> TX packet and its attached frames.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001827 * This is the responsibility of the caller to remove this packet of
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001828 * any data structure it was possibly attached to.
1829 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001830static inline void free_quic_tx_packet(struct quic_conn *qc,
1831 struct quic_tx_packet *pkt)
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001832{
1833 struct quic_frame *frm, *frmbak;
1834
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001835 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1836
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001837 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001838 goto leave;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001839
1840 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1841 LIST_DELETE(&frm->list);
1842 pool_free(pool_head_quic_frame, frm);
1843 }
1844 pool_free(pool_head_quic_tx_packet, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001845
1846 leave:
1847 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001848}
1849
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001850/* Free the TX packets of <pkts> list */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001851static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001852{
1853 struct quic_tx_packet *pkt, *tmp;
1854
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001855 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1856
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001857 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001858 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001859 eb64_delete(&pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001860 free_quic_tx_packet(qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001861 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001862
1863 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001864}
1865
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001866/* Remove already sent ranges of acknowledged packet numbers from
1867 * <pktns> packet number space tree below <largest_acked_pn> possibly
1868 * updating the range which contains <largest_acked_pn>.
1869 * Never fails.
1870 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001871static void qc_treat_ack_of_ack(struct quic_conn *qc,
1872 struct quic_pktns *pktns,
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001873 int64_t largest_acked_pn)
1874{
1875 struct eb64_node *ar, *next_ar;
1876 struct quic_arngs *arngs = &pktns->rx.arngs;
1877
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001878 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1879
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001880 ar = eb64_first(&arngs->root);
1881 while (ar) {
1882 struct quic_arng_node *ar_node;
1883
1884 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001885 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001886
1887 if ((int64_t)ar_node->first.key > largest_acked_pn) {
1888 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001889 break;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001890 }
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001891
1892 if (largest_acked_pn < ar_node->last) {
1893 eb64_delete(ar);
1894 ar_node->first.key = largest_acked_pn + 1;
1895 eb64_insert(&arngs->root, ar);
1896 break;
1897 }
1898
1899 eb64_delete(ar);
1900 pool_free(pool_head_quic_arng, ar_node);
1901 arngs->sz--;
1902 ar = next_ar;
1903 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001904
1905 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001906}
1907
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001908/* Send a packet ack event nofication for each newly acked packet of
1909 * <newly_acked_pkts> list and free them.
1910 * Always succeeds.
1911 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001912static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001913 struct list *newly_acked_pkts)
1914{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001915 struct quic_tx_packet *pkt, *tmp;
1916 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1917
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001918 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1919
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001920 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1921 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001922 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001923 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001924 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001925 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001926 /* If this packet contained an ACK frame, proceed to the
1927 * acknowledging of range of acks from the largest acknowledged
1928 * packet number which was sent in an ACK frame by this packet.
1929 */
1930 if (pkt->largest_acked_pn != -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001931 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001932 ev.ack.acked = pkt->in_flight_len;
1933 ev.ack.time_sent = pkt->time_sent;
1934 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001935 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001936 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001937 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001938 }
1939
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001940 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1941
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001942}
1943
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001944/* Release all the frames attached to <pktns> packet number space */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001945static inline void qc_release_pktns_frms(struct quic_conn *qc,
1946 struct quic_pktns *pktns)
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001947{
1948 struct quic_frame *frm, *frmbak;
1949
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001950 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
1951
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001952 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1953 LIST_DELETE(&frm->list);
1954 pool_free(pool_head_quic_frame, frm);
1955 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001956
1957 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001958}
1959
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001960/* Handle <pkts> list of lost packets detected at <now_us> handling
1961 * their TX frames.
1962 * Send a packet loss event to the congestion controller if
1963 * in flight packet have been lost.
1964 * Also frees the packet in <pkts> list.
1965 * Never fails.
1966 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001967static inline void qc_release_lost_pkts(struct quic_conn *qc,
1968 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001969 struct list *pkts,
1970 uint64_t now_us)
1971{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001972 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001973
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001974 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1975
Frédéric Lécaille277c4622022-08-24 17:06:04 +02001976 if (LIST_ISEMPTY(pkts))
1977 goto leave;
1978
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001979 oldest_lost = newest_lost = NULL;
1980 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001981 struct list tmp = LIST_HEAD_INIT(tmp);
1982
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001983 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001984 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001985 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001986 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001987 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001988 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001989 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001990 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001991 if (!oldest_lost) {
1992 oldest_lost = newest_lost = pkt;
1993 }
1994 else {
1995 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001996 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001997 newest_lost = pkt;
1998 }
1999 }
2000
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01002001 if (newest_lost) {
2002 /* Sent a congestion event to the controller */
Benoit DOLEZ69e3f052022-06-08 09:28:56 +02002003 struct quic_cc_event ev = { };
2004
2005 ev.type = QUIC_CC_EVT_LOSS;
2006 ev.loss.time_sent = newest_lost->time_sent;
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01002007
2008 quic_cc_event(&qc->path->cc, &ev);
2009 }
2010
2011 /* If an RTT have been already sampled, <rtt_min> has been set.
2012 * We must check if we are experiencing a persistent congestion.
2013 * If this is the case, the congestion controller must re-enter
2014 * slow start state.
2015 */
2016 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2017 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
2018
2019 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2020 now_ms, qc->max_ack_delay))
2021 qc->path->cc.algo->slow_start(&qc->path->cc);
2022 }
2023
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002024 quic_tx_packet_refdec(oldest_lost);
2025 if (newest_lost != oldest_lost)
2026 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002027
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002028 leave:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002029 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002030}
2031
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002032/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2033 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002034 * 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 +01002035 * acked ack-eliciting packet.
2036 * Return 1, if succeeded, 0 if not.
2037 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002038static inline int qc_parse_ack_frm(struct quic_conn *qc,
2039 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002040 struct quic_enc_level *qel,
2041 unsigned int *rtt_sample,
2042 const unsigned char **pos, const unsigned char *end)
2043{
2044 struct quic_ack *ack = &frm->ack;
2045 uint64_t smallest, largest;
2046 struct eb_root *pkts;
2047 struct eb64_node *largest_node;
2048 unsigned int time_sent, pkt_flags;
2049 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2050 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002051 int ret = 0;
2052
2053 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002054
2055 if (ack->largest_ack > qel->pktns->tx.next_pn) {
2056 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002057 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002058 goto err;
2059 }
2060
2061 if (ack->first_ack_range > ack->largest_ack) {
2062 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002063 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002064 goto err;
2065 }
2066
2067 largest = ack->largest_ack;
2068 smallest = largest - ack->first_ack_range;
2069 pkts = &qel->pktns->tx.pkts;
2070 pkt_flags = 0;
2071 largest_node = NULL;
2072 time_sent = 0;
2073
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002074 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002075 largest_node = eb64_lookup(pkts, largest);
2076 if (!largest_node) {
2077 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002078 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002079 }
2080 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002081 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002082 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002083 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002084 }
2085
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002086 TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002087 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002088 do {
2089 uint64_t gap, ack_range;
2090
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002091 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2092 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002093 if (!ack->ack_range_num--)
2094 break;
2095
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002096 if (!quic_dec_int(&gap, pos, end)) {
2097 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002098 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002099 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002100
2101 if (smallest < gap + 2) {
2102 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002103 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002104 goto err;
2105 }
2106
2107 largest = smallest - gap - 2;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002108 if (!quic_dec_int(&ack_range, pos, end)) {
2109 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002110 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002111 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002112
2113 if (largest < ack_range) {
2114 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002115 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002116 goto err;
2117 }
2118
2119 /* Do not use this node anymore. */
2120 largest_node = NULL;
2121 /* Next range */
2122 smallest = largest - ack_range;
2123
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002124 TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002125 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002126 } while (1);
2127
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002128 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2129 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002130 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002131 }
2132
2133 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2134 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002135 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002136 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002137 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002138 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2139 if (quic_peer_validated_addr(qc))
2140 qc->path->loss.pto_count = 0;
2141 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002142 }
2143
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002144 ret = 1;
2145 leave:
2146 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2147 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002148
2149 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002150 free_quic_tx_pkts(qc, &newly_acked_pkts);
2151 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002152}
2153
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002154/* This function gives the detail of the SSL error. It is used only
2155 * if the debug mode and the verbose mode are activated. It dump all
2156 * the SSL error until the stack was empty.
2157 */
2158static forceinline void qc_ssl_dump_errors(struct connection *conn)
2159{
2160 if (unlikely(global.mode & MODE_DEBUG)) {
2161 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002162 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002163 unsigned long ret;
2164
Willy Tarreau325fc632022-04-11 18:47:38 +02002165 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002166 ret = ERR_get_error();
2167 if (!ret)
2168 return;
2169
2170 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002171 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002172 }
2173 }
2174}
2175
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002176int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2177 const char **str, int *len);
2178
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002179/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2180 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002181 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002182 * Return 1 if succeeded, 0 if not.
2183 */
2184static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002185 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002186 const unsigned char *data, size_t len,
2187 struct quic_rx_packet *pkt,
2188 struct quic_rx_crypto_frm *cf)
2189{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002190 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002191 struct quic_conn *qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002192 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002193
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002194 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002195 qc = ctx->qc;
2196
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002197 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2198
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002199 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002200 TRACE_ERROR("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002201 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002202 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002203 }
2204
2205 el->rx.crypto.offset += len;
2206 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002207 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002208
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002209 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002210 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002211 ssl_err = SSL_do_handshake(ctx->ssl);
2212 if (ssl_err != 1) {
2213 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2214 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002215 TRACE_PROTO("SSL handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002216 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002217 goto out;
2218 }
2219
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002220 /* TODO: Should close the connection asap */
2221 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2222 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2223 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2224 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2225 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002226 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002227 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002228 ERR_clear_error();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002229 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002230 }
2231
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002232 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002233
2234 /* Check the alpn could be negotiated */
2235 if (!qc->app_ops) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002236 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002237 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002238 goto leave;
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002239 }
2240
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002241 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002242 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002243 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2244 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2245 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002246 /* I/O callback switch */
2247 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002248 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002249 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002250 /* The connection is ready to be accepted. */
2251 quic_accept_push_qc(qc);
2252 }
2253 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002254 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002255 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002256 } else {
2257 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2258 if (ssl_err != 1) {
2259 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2260 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002261 TRACE_PROTO("SSL post handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002262 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002263 goto out;
2264 }
2265
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002266 TRACE_ERROR("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002267 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002268 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002269 }
2270
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002271 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002272 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002273
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002274 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002275 ret = 1;
2276 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002277 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002278 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002279}
2280
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002281/* Parse a STREAM frame <strm_frm>
2282 *
2283 * Return 1 on success. On error, 0 is returned. In this case, the packet
2284 * containing the frame must not be acknowledged.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002285 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002286static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2287 struct quic_stream *strm_frm,
2288 struct quic_conn *qc)
2289{
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002290 int ret;
2291
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002292 /* RFC9000 13.1. Packet Processing
2293 *
2294 * A packet MUST NOT be acknowledged until packet protection has been
2295 * successfully removed and all frames contained in the packet have
2296 * been processed. For STREAM frames, this means the data has been
2297 * enqueued in preparation to be received by the application protocol,
2298 * but it does not require that data be delivered and consumed.
2299 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002300 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2301
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002302 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2303 strm_frm->offset.key, strm_frm->fin,
2304 (char *)strm_frm->data);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002305
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002306 /* frame rejected - packet must not be acknowledeged */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002307 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2308 return !ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002309}
2310
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002311/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2312 * for <qc> QUIC connection.
2313 * This is a best effort function which never fails even if no memory could be
2314 * allocated to duplicate these frames.
2315 */
2316static void qc_dup_pkt_frms(struct quic_conn *qc,
2317 struct list *pkt_frm_list, struct list *out_frm_list)
2318{
2319 struct quic_frame *frm, *frmbak;
2320 struct list tmp = LIST_HEAD_INIT(tmp);
2321
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002322 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2323
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002324 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2325 struct quic_frame *dup_frm, *origin;
2326
2327 switch (frm->type) {
2328 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2329 {
2330 struct quic_stream *strm_frm = &frm->stream;
2331 struct eb64_node *node = NULL;
2332 struct qc_stream_desc *stream_desc;
2333
2334 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2335 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002336 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002337 continue;
2338 }
2339
2340 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2341 /* Do not resend this frame if in the "already acked range" */
2342 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002343 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002344 QUIC_EV_CONN_PRSAFRM, qc, frm);
2345 continue;
2346 }
2347 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2348 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002349 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002350 QUIC_EV_CONN_PRSAFRM, qc, frm);
2351 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002352 break;
2353 }
2354
2355 default:
2356 break;
2357 }
2358
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002359 dup_frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002360 if (!dup_frm) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002361 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002362 break;
2363 }
2364
2365 /* If <frm> is already a copy of another frame, we must take
2366 * its original frame as source for the copy.
2367 */
2368 origin = frm->origin ? frm->origin : frm;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002369 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Frédéric Lécaillee35463c2022-08-27 10:19:42 +02002370 if (origin->pkt)
2371 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
2372 qc, NULL, &origin->pkt->pn_node.key);
2373 else {
2374 /* <origin> is a frame which was sent from a packet detected as lost. */
2375 TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
2376 }
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002377 *dup_frm = *origin;
2378 dup_frm->pkt = NULL;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002379 dup_frm->origin = origin;
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002380 dup_frm->flags = 0;
2381 LIST_INIT(&dup_frm->reflist);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002382 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2383 LIST_APPEND(&tmp, &dup_frm->list);
2384 }
2385
2386 LIST_SPLICE(out_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002387
2388 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002389}
2390
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002391/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002392static void qc_prep_fast_retrans(struct quic_conn *qc,
2393 struct quic_enc_level *qel,
2394 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002395{
2396 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002397 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002398 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002399 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002400
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002401 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2402
2403 BUG_ON(frms1 == frms2);
2404
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002405 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002406 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002407 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002408 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002409 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2410 node = eb64_next(node);
2411 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002412 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002413 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002414 }
2415
2416 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002417 goto leave;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002418
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002419 /* When building a packet from another one, the field which may increase the
2420 * packet size is the packet number. And the maximum increase is 4 bytes.
2421 */
2422 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2423 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +02002424 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002425 goto leave;
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002426 }
2427
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +02002428 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002429 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2430 if (frms == frms1 && frms2) {
2431 frms = frms2;
2432 goto start;
2433 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002434 leave:
Frédéric Lécaille3a9b9442022-08-30 15:25:47 +02002435 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002436}
2437
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002438/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002439 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002440 * Initial packets send them coalescing with others (Handshake here).
2441 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002442 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002443static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2444 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002445{
2446 struct list itmp = LIST_HEAD_INIT(itmp);
2447 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002448
2449 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2450 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2451 struct quic_enc_level *qel = iqel;
2452 struct eb_root *pkts;
2453 struct eb64_node *node;
2454 struct quic_tx_packet *pkt;
2455 struct list *tmp = &itmp;
2456
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002457 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002458 start:
2459 pkt = NULL;
2460 pkts = &qel->pktns->tx.pkts;
2461 node = eb64_first(pkts);
2462 /* Skip the empty packet (they have already been retransmitted) */
2463 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002464 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002465 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002466 break;
2467 node = eb64_next(node);
2468 }
2469
2470 if (!pkt)
2471 goto end;
2472
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002473 /* When building a packet from another one, the field which may increase the
2474 * packet size is the packet number. And the maximum increase is 4 bytes.
2475 */
2476 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2477 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2478 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2479 goto end;
2480 }
2481
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002482 qel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002483
2484 /* No risk to loop here, #packet per datagram is bounded */
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002485 requeue:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002486 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002487 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002488 if (qel == iqel) {
2489 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2490 pkt = pkt->next;
2491 tmp = &htmp;
2492 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002493 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002494 goto requeue;
2495 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002496 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002497
2498 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002499 LIST_SPLICE(ifrms, &itmp);
2500 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002501
2502 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002503}
2504
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002505static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002506{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002507 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2508
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002509 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2510 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2511 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2512 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002513 goto out;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002514
2515 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2516 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002517
2518 out:
2519 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002520}
2521
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002522/* Enqueue a STOP_SENDING frame to send into 1RTT packet number space
2523 * frame list to send.
2524 * Return 1 if succeeded, 0 if not.
2525 */
2526static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id)
2527{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002528 int ret = 0;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002529 struct quic_frame *frm;
2530 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2531 uint64_t app_error_code;
2532
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002533 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2534
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002535 /* TODO: the mux may be released, we cannot have more
2536 * information about the application error code to send
2537 * at this time.
2538 */
2539 app_error_code = H3_REQUEST_REJECTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002540 // fixme: zalloc
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002541 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002542 if (!frm) {
2543 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2544 goto out;
2545 }
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002546
2547 frm->type = QUIC_FT_STOP_SENDING;
2548 frm->stop_sending.id = id;
2549 frm->stop_sending.app_error_code = app_error_code;
2550 LIST_INIT(&frm->reflist);
2551 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002552 ret = 1;
2553 out:
2554 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2555 return ret;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002556}
2557
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002558/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2559 * as I/O handler context and <qel> as encryption level.
2560 * Returns 1 if succeeded, 0 if failed.
2561 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002562static 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 +01002563 struct quic_enc_level *qel)
2564{
2565 struct quic_frame frm;
2566 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002567 struct quic_conn *qc = ctx->qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002568 int fast_retrans = 0, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002569
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002570 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002571 /* Skip the AAD */
2572 pos = pkt->data + pkt->aad_len;
2573 end = pkt->data + pkt->len;
2574
2575 while (pos < end) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002576 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
2577 // trace already emitted by function above
2578 goto leave;
2579 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002580
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002581 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002582 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002583 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002584 break;
2585 case QUIC_FT_PING:
2586 break;
2587 case QUIC_FT_ACK:
2588 {
2589 unsigned int rtt_sample;
2590
2591 rtt_sample = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002592 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
2593 // trace already emitted by function above
2594 goto leave;
2595 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002596
2597 if (rtt_sample) {
2598 unsigned int ack_delay;
2599
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002600 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002601 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002602 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2603 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002604 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002605 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002606 break;
2607 }
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002608 case QUIC_FT_RESET_STREAM:
2609 /* TODO: handle this frame at STREAM level */
2610 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002611 case QUIC_FT_STOP_SENDING:
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002612 {
2613 struct quic_stop_sending *stop_sending = &frm.stop_sending;
2614 if (qc->mux_state == QC_MUX_READY) {
2615 if (qcc_recv_stop_sending(qc->qcc, stop_sending->id,
2616 stop_sending->app_error_code)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002617 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
2618 goto leave;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002619 }
2620 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002621 break;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002622 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002623 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002624 {
2625 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002626
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002627 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002628 /* XXX TO DO: <cfdebug> is used only for the traces. */
2629 struct quic_rx_crypto_frm cfdebug = { };
2630
2631 cfdebug.offset_node.key = frm.crypto.offset;
2632 cfdebug.len = frm.crypto.len;
2633 TRACE_PROTO("CRYPTO data discarded",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002634 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002635 break;
2636 }
2637
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002638 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2639 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2640 /* XXX TO DO: <cfdebug> is used only for the traces. */
2641 struct quic_rx_crypto_frm cfdebug = { };
2642
2643 cfdebug.offset_node.key = frm.crypto.offset;
2644 cfdebug.len = frm.crypto.len;
2645 /* Nothing to do */
2646 TRACE_PROTO("Already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002647 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002648 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002649 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2650 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002651 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002653 else {
2654 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2655 /* XXX TO DO: <cfdebug> is used only for the traces. */
2656 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002657
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002658 cfdebug.offset_node.key = frm.crypto.offset;
2659 cfdebug.len = frm.crypto.len;
2660 TRACE_PROTO("Partially already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002661 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002662 frm.crypto.len -= diff;
2663 frm.crypto.data += diff;
2664 frm.crypto.offset = qel->rx.crypto.offset;
2665 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002666 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002667
2668 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002669 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002670 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002671
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002672 cfdebug.offset_node.key = frm.crypto.offset;
2673 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002674 if (!qc_provide_cdata(qel, ctx,
2675 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002676 pkt, &cfdebug)) {
2677 // trace already emitted by function above
2678 goto leave;
2679 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002680
2681 break;
2682 }
2683
2684 /* frm.crypto.offset > qel->rx.crypto.offset */
2685 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2686 if (!cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002687 TRACE_ERROR("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002688 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002689 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002690 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002691
2692 cf->offset_node.key = frm.crypto.offset;
2693 cf->len = frm.crypto.len;
2694 cf->data = frm.crypto.data;
2695 cf->pkt = pkt;
2696 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2697 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002698 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002699 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002700 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002701 {
2702 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002703 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002704
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002705 /* The upper layer may not be allocated. */
2706 if (qc->mux_state != QC_MUX_READY) {
2707 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002708 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002709 break;
2710 }
2711 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002712 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002713 if (!qc_stop_sending_frm_enqueue(qc, stream->id))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002714 TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc);
2715 /* This packet will not be acknowledged */
2716 goto leave;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002717 }
2718 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002719
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002720 if (!qc_handle_strm_frm(pkt, stream, qc)) {
2721 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
2722 goto leave;
2723 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002724
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002725 break;
2726 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002727 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002728 if (qc->mux_state == QC_MUX_READY) {
2729 struct quic_max_data *data = &frm.max_data;
2730 qcc_recv_max_data(qc->qcc, data->max_data);
2731 }
2732 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002733 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002734 if (qc->mux_state == QC_MUX_READY) {
2735 struct quic_max_stream_data *data = &frm.max_stream_data;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002736 if (qcc_recv_max_stream_data(qc->qcc, data->id,
2737 data->max_stream_data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002738 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
2739 goto leave;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002740 }
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002741 }
2742 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002743 case QUIC_FT_MAX_STREAMS_BIDI:
2744 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002745 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002746 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002747 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2748 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002749 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002750 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2751 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002752 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002753 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2754 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002755 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002756 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002757 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002758 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002759 case QUIC_FT_RETIRE_CONNECTION_ID:
2760 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002761 break;
2762 case QUIC_FT_CONNECTION_CLOSE:
2763 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002764 /* Increment the error counters */
2765 qc_cc_err_count_inc(qc, &frm);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002766 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002767 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2768 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002769 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002770 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002771 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002772 /* RFC 9000 10.2. Immediate Close:
2773 * The closing and draining connection states exist to ensure
2774 * that connections close cleanly and that delayed or reordered
2775 * packets are properly discarded. These states SHOULD persist
2776 * for at least three times the current PTO interval...
2777 *
2778 * Rearm the idle timeout only one time when entering draining
2779 * state.
2780 */
2781 qc_idle_timer_do_rearm(qc);
2782 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002783 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002784 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002785 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002786 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002787 if (qc_is_listener(ctx->qc)) {
2788 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
2789 QUIC_EV_CONN_PRSHPKT, qc);
2790 goto leave;
2791 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002792
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002793 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002794 break;
2795 default:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002796 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
2797 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002798 }
2799 }
2800
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002801 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002802 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002803
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002804 if (fast_retrans) {
2805 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2806 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2807
2808 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2809 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002810
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002811 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2812 * has successfully parse a Handshake packet. The Initial encryption must also
2813 * be discarded.
2814 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002815 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002816 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002817 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2818 QUIC_FL_TLS_SECRETS_DCD)) {
2819 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2820 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2821 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2822 qc_set_timer(ctx->qc);
2823 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002824 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002825 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002826 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2827 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002828 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002829 }
2830
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002831 ret = 1;
2832 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002833 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002834 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002835}
2836
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002837/* Allocate Tx buffer from <qc> quic-conn if needed.
2838 *
2839 * Returns allocated buffer or NULL on error.
2840 */
2841static struct buffer *qc_txb_alloc(struct quic_conn *qc)
2842{
2843 struct buffer *buf = &qc->tx.buf;
2844 if (!b_alloc(buf))
2845 return NULL;
2846
2847 return buf;
2848}
2849
2850/* Free Tx buffer from <qc> if it is empty. */
2851static void qc_txb_release(struct quic_conn *qc)
2852{
2853 struct buffer *buf = &qc->tx.buf;
2854
2855 /* For the moment sending function is responsible to purge the buffer
2856 * entirely. It may change in the future but this requires to be able
2857 * to reuse old data.
2858 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002859 BUG_ON_HOT(buf && b_data(buf));
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002860
2861 if (!b_data(buf)) {
2862 b_free(buf);
2863 offer_buffers(NULL, 1);
2864 }
2865}
2866
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002867/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
2868 * must contains the address of the first packet stored in the payload.
2869 *
2870 * Caller is responsible that there is enough space in the buffer.
2871 */
2872static void qc_txb_store(struct buffer *buf, uint16_t length,
2873 struct quic_tx_packet *first_pkt)
2874{
2875 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
2876 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
2877
2878 write_u16(b_tail(buf), length);
2879 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
2880 b_add(buf, hdlen + length);
2881}
2882
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002883/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2884 * with <frms> as ack-eliciting frame list to send, 0 if not.
2885 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2886 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002887 * <force_ack> may be set to 1 if you want to force an ack.
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002888 */
2889static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002890 struct quic_enc_level *qel, int cc, int probe, int force_ack)
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002891{
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002892 unsigned int must_ack = force_ack ||
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002893 (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED));
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002894
2895 /* Do not build any more packet if the TX secrets are not available or
2896 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2897 * and if there is no more packets to send upon PTO expiration
2898 * and if there is no more ack-eliciting frames to send or in flight
2899 * congestion control limit is reached for prepared data
2900 */
2901 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2902 (!cc && !probe && !must_ack &&
2903 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002904 return 0;
2905 }
2906
2907 return 1;
2908}
2909
Amaury Denoyellef2476052022-08-04 16:19:57 +02002910/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2911 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
2912 *
2913 * Each datagram is prepended by a two fields header : the datagram length and
2914 * the address of the packet contained in the datagram.
2915 *
2916 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2917 * -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002918 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002919static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002920 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002921{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002922 int ret = -1;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002923 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002924 unsigned char *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002925 struct quic_tx_packet *pkt;
2926 size_t total;
Amaury Denoyellef2476052022-08-04 16:19:57 +02002927 /* Each datagram is prepended with its length followed by the address
2928 * of the first packet in the datagram.
2929 */
2930 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002931
2932 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002933
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002934 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2935 total = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002936 pos = (unsigned char *)b_tail(buf);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002937 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002938 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002939
2940 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002941 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002942 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002943 /* We do not probe if an immediate close was asked */
2944 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002945 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002946
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002947 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002948 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002949
2950 /* Leave room for the datagram header */
2951 pos += dg_headlen;
2952 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002953 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 +01002954 }
2955 else {
2956 end = pos + qc->path->mtu;
2957 }
2958
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002959 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
2960 QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002961 switch (err) {
2962 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002963 // trace already emitted by function above
2964 goto leave;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002965 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002966 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2967 * MTU, we are here because of the congestion control window. There is
2968 * no need to try to reuse this buffer.
2969 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002970 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002971 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002972 default:
2973 break;
2974 }
2975
2976 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002977 BUG_ON(!pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002978
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002979 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2980 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2981
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002982 total += pkt->len;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002983
Amaury Denoyellef2476052022-08-04 16:19:57 +02002984 /* Write datagram header. */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002985 qc_txb_store(buf, pkt->len, pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002986 }
2987
2988 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002989 ret = total;
2990 leave:
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002991 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002992 return ret;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002993}
2994
Amaury Denoyellef2476052022-08-04 16:19:57 +02002995/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2996 * <frms>. Several packets can be regrouped in a single datagram. The result is
2997 * written into <buf>.
2998 *
2999 * Each datagram is prepended by a two fields header : the datagram length and
3000 * the address of first packet in the datagram.
3001 *
3002 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
3003 * -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003004 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003005static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003006 enum quic_tls_enc_level tel, struct list *tel_frms,
3007 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003008{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003009 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003010 unsigned char *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003011 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3012 /* length of datagrams */
3013 uint16_t dglen;
3014 size_t total;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003015 int ret = -1, padding;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003016 /* Each datagram is prepended with its length followed by the address
3017 * of the first packet in the datagram.
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003018 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003019 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003020 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003021
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003022 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3023
Amaury Denoyellef2476052022-08-04 16:19:57 +02003024 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
3025 * caller must ensure that buf is resetted.
3026 */
3027 BUG_ON_HOT(buf->head || buf->data);
3028
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003029 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003030 qel = &qc->els[tel];
3031 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003032 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003033 padding = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003034 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003035 first_pkt = prv_pkt = NULL;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003036 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003037 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003038 enum quic_pkt_type pkt_type;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003039 struct quic_tls_ctx *tls_ctx;
3040 const struct quic_version *ver;
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003041 int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3042 (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3043 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003045 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003046 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003047 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003048 /* We do not probe if an immediate close was asked */
3049 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01003050 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003051
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003052 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003053 if (prv_pkt)
3054 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003055 /* Let's select the next encryption level */
3056 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3057 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003058 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003059 qel = &qc->els[tel];
3060 /* Build a new datagram */
3061 prv_pkt = NULL;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003062 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003063 continue;
3064 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003065 break;
3066 }
3067
3068 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003069 if (!prv_pkt) {
3070 /* Leave room for the datagram header */
3071 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003072 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02003073 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 +01003074 }
3075 else {
3076 end = pos + qc->path->mtu;
3077 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003078 }
3079
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003080 if (qc->negotiated_version) {
3081 ver = qc->negotiated_version;
3082 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3083 tls_ctx = &qc->negotiated_ictx;
3084 else
3085 tls_ctx = &qel->tls_ctx;
3086 }
3087 else {
3088 ver = qc->original_version;
3089 tls_ctx = &qel->tls_ctx;
3090 }
3091
3092 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02003093 qc, ver, dglen, pkt_type,
3094 force_ack, padding, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003095 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003096 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003097 // trace already emitted by function above
3098 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003099 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003100 /* If there was already a correct packet present, set the
3101 * current datagram as prepared into <cbuf>.
3102 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003103 if (prv_pkt)
3104 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003105 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003106 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003107 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01003108 break;
3109 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02003110
Frédéric Lécaille63556772021-12-29 17:18:21 +01003111 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003112 BUG_ON(!cur_pkt);
Frédéric Lécaille63556772021-12-29 17:18:21 +01003113
Frédéric Lécaille1809c332022-04-25 10:24:12 +02003114 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3115 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3116
Frédéric Lécaille63556772021-12-29 17:18:21 +01003117 total += cur_pkt->len;
3118 /* keep trace of the first packet in the datagram */
3119 if (!first_pkt)
3120 first_pkt = cur_pkt;
3121 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003122 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003123 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003124 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3125 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01003126 /* Let's say we have to build a new dgram */
3127 prv_pkt = NULL;
3128 dglen += cur_pkt->len;
3129 /* Client: discard the Initial encryption keys as soon as
3130 * a handshake packet could be built.
3131 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003132 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01003133 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3134 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3135 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3136 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3137 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01003138 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003139 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003140 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003141 }
3142 /* If the data for the current encryption level have all been sent,
3143 * select the next level.
3144 */
3145 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003146 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 +01003147 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3148 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3149 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3150 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003151 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3152 frms = &qc->els[tel].pktns->tx.frms;
3153 else
3154 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003155 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003156 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003157 /* If there is data for the next level, do not
3158 * consume a datagram.
3159 */
3160 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003161 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003162 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02003163
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003164 /* If we have to build a new datagram, set the current datagram as
3165 * prepared into <cbuf>.
3166 */
3167 if (!prv_pkt) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003168 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003169 first_pkt = NULL;
3170 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003171 padding = 0;
3172 }
3173 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003174 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003175 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3176 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003177 }
3178 }
3179
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003180 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003181 ret = total;
3182 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003183 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003184 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003185}
3186
Amaury Denoyellef2476052022-08-04 16:19:57 +02003187/* Send datagrams stored in <buf>.
3188 *
3189 * This function always returns 1 for success. Even if sendto() syscall failed,
3190 * buffer is drained and packets are considered as emitted. QUIC loss detection
3191 * mechanism is used as a back door way to retry sending.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003192 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003193int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003194{
3195 struct quic_conn *qc;
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003196 char skip_sendto = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003197
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003198 qc = ctx->qc;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003199 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003200 while (b_contig_data(buf, 0)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003201 unsigned char *pos;
3202 struct buffer tmpbuf = { };
3203 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3204 uint16_t dglen;
3205 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003206 unsigned int time_sent;
3207
Amaury Denoyellef2476052022-08-04 16:19:57 +02003208 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003209 dglen = read_u16(pos);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003210 BUG_ON_HOT(!dglen); /* this should not happen */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003211
3212 pos += sizeof dglen;
3213 first_pkt = read_ptr(pos);
3214 pos += sizeof first_pkt;
3215 tmpbuf.area = (char *)pos;
3216 tmpbuf.size = tmpbuf.data = dglen;
3217
Frédéric Lécaille3a561372022-08-29 18:05:44 +02003218 TRACE_DATA("send dgram", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003219 /* If sendto is on error just skip the call to it for the rest
3220 * of the loop but continue to purge the buffer. Data will be
3221 * transmitted when QUIC packets are detected as lost on our
3222 * side.
3223 *
3224 * TODO use fd-monitoring to detect when send operation can be
3225 * retry. This should improve the bandwidth without relying on
3226 * retransmission timer. However, it requires a major rework on
3227 * quic-conn fd management.
3228 */
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003229 if (!skip_sendto) {
3230 if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) {
3231 skip_sendto = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003232 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003233 }
3234 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003235
Amaury Denoyellef2476052022-08-04 16:19:57 +02003236 b_del(buf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003237 qc->tx.bytes += tmpbuf.data;
3238 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003239
3240 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3241 pkt->time_sent = time_sent;
3242 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3243 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003244 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003245 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3246 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003247 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003248 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3249 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3250 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003251 qc_notify_close(qc);
3252
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003253 /* RFC 9000 10.2. Immediate Close:
3254 * The closing and draining connection states exist to ensure
3255 * that connections close cleanly and that delayed or reordered
3256 * packets are properly discarded. These states SHOULD persist
3257 * for at least three times the current PTO interval...
3258 *
3259 * Rearm the idle timeout only one time when entering closing
3260 * state.
3261 */
3262 qc_idle_timer_do_rearm(qc);
3263 if (qc->timer_task) {
3264 task_destroy(qc->timer_task);
3265 qc->timer_task = NULL;
3266 }
3267 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003268 qc->path->in_flight += pkt->in_flight_len;
3269 pkt->pktns->tx.in_flight += pkt->in_flight_len;
Frédéric Lécaille5f6c25e2022-08-24 16:23:44 +02003270 if (pkt->in_flight_len)
Frédéric Lécailleda9c4412022-08-22 18:47:51 +02003271 qc_set_timer(qc);
Frédéric Lécaille5f6c25e2022-08-24 16:23:44 +02003272 TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
3273 next_pkt = pkt->next;
3274 quic_tx_packet_refinc(pkt);
3275 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003276 }
3277 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003278
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003279 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3280
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003281 return 1;
3282}
3283
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003284/* Copy into <buf> buffer a stateless reset token depending on the
3285 * <salt> salt input. This is the cluster secret which will be derived
3286 * as HKDF input secret to generate this token.
3287 * Return 1 if succeeded, 0 if not.
3288 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003289static int quic_stateless_reset_token_cpy(struct quic_conn *qc,
3290 unsigned char *buf, size_t len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003291 const unsigned char *salt, size_t saltlen)
3292{
3293 /* Input secret */
3294 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3295 size_t keylen = strlen(global.cluster_secret);
3296 /* Info */
3297 const unsigned char label[] = "stateless token";
3298 size_t labellen = sizeof label - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003299 int ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003300
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003301 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3302
3303 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003304 key, keylen, salt, saltlen, label, labellen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003305 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3306 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003307}
3308
3309/* Initialize the stateless reset token attached to <cid> connection ID.
3310 * Returns 1 if succeeded, 0 if not.
3311 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003312static int quic_stateless_reset_token_init(struct quic_conn *qc,
3313 struct quic_connection_id *quic_cid)
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003314{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003315 int ret;
3316
3317 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3318
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003319 if (global.cluster_secret) {
3320 /* Output secret */
3321 unsigned char *token = quic_cid->stateless_reset_token;
3322 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3323 /* Salt */
3324 const unsigned char *cid = quic_cid->cid.data;
3325 size_t cidlen = quic_cid->cid.len;
3326
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003327 ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003328 }
3329 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003330 /* TODO: RAND_bytes() should be replaced */
3331 ret = RAND_bytes(quic_cid->stateless_reset_token,
3332 sizeof quic_cid->stateless_reset_token) == 1;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003333 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003334
3335 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3336 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003337}
3338
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003339/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3340 * ebtree.
3341 *
3342 * The CID is randomly generated in part with the result altered to be
3343 * associated with the current thread ID. This means this function must only
3344 * be called by the quic_conn thread.
3345 *
3346 * Returns the new CID if succeeded, NULL if not.
3347 */
3348static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3349 struct quic_conn *qc,
3350 int seq_num)
3351{
3352 struct quic_connection_id *cid;
3353
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003354 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3355
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003356 cid = pool_alloc(pool_head_quic_connection_id);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003357 if (!cid) {
3358 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
3359 goto err;
3360 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003361
3362 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003363 /* TODO: RAND_bytes() should be replaced */
3364 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) {
3365 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003366 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003367 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003368
3369 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003370 if (quic_stateless_reset_token_init(qc, cid) != 1) {
3371 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003372 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003373 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003374
3375 cid->qc = qc;
3376
3377 cid->seq_num.key = seq_num;
3378 cid->retire_prior_to = 0;
3379 /* insert the allocated CID in the quic_conn tree */
3380 eb64_insert(root, &cid->seq_num);
3381
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003382 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003383 return cid;
3384
3385 err:
3386 pool_free(pool_head_quic_connection_id, cid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003387 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003388 return NULL;
3389}
3390
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003391/* Build all the frames which must be sent just after the handshake have succeeded.
3392 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3393 * a HANDSHAKE_DONE frame.
3394 * Return 1 if succeeded, 0 if not.
3395 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003396static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003397{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003398 int ret = 0, i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003399 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003400 struct quic_frame *frm, *frmbak;
3401 struct list frm_list = LIST_HEAD_INIT(frm_list);
3402 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003403
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003404 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
3405
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003406 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003407 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003408 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003409 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003410 if (!frm) {
3411 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3412 goto leave;
3413 }
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003414
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003415 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003416 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003417 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003418 }
3419
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003420 /* Initialize <max> connection IDs minus one: there is
3421 * already one connection ID used for the current connection.
3422 */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003423 first = 1;
3424 max = qc->tx.params.active_connection_id_limit;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003425
3426 /* TODO: check limit */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003427 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003428 struct quic_connection_id *cid;
3429
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003430 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003431 if (!frm) {
3432 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003433 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003434 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003435
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003436 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003437 cid = new_quic_cid(&qc->cids, qc, i);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003438 if (!cid) {
Frédéric Lécaillebccbad22022-08-31 15:02:53 +02003439 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003440 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003441 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003442 }
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003443
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003444 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003445 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003446
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003447 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003448 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003449 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003450
3451 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003452 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003453
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003454 ret = 1;
3455 leave:
3456 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
3457 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003458
3459 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003460 /* free the frames */
3461 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3462 pool_free(pool_head_quic_frame, frm);
3463
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003464 node = eb64_lookup_ge(&qc->cids, first);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003465 while (node) {
3466 struct quic_connection_id *cid;
3467
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003468 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003469 if (cid->seq_num.key >= max)
3470 break;
3471
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003472 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003473 ebmb_delete(&cid->node);
3474 eb64_delete(&cid->seq_num);
3475 pool_free(pool_head_quic_connection_id, cid);
3476 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003477 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003478}
3479
3480/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003481void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003482{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003483 struct eb64_node *n;
3484 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003485
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003486 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
3487
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003488 n = eb64_first(&arngs->root);
3489 while (n) {
3490 struct eb64_node *next;
3491
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003492 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003493 next = eb64_next(n);
3494 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003495 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003496 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003497 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003498
3499 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003500}
3501
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003502/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3503 * descending order.
3504 */
3505static inline size_t sack_gap(struct quic_arng_node *p,
3506 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003507{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003508 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003509}
3510
3511
3512/* Remove the last elements of <ack_ranges> list of ack range updating its
3513 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003514 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003515 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003516static int quic_rm_last_ack_ranges(struct quic_conn *qc,
3517 struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003518{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003519 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003520 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003522 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3523
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003524 last = eb64_last(&arngs->root);
3525 while (last && arngs->enc_sz > limit) {
3526 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003527
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003528 prev = eb64_prev(last);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003529 if (!prev) {
3530 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
3531 goto out;
3532 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003533
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003534 last_node = eb64_entry(last, struct quic_arng_node, first);
3535 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003536 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3537 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3538 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3539 --arngs->sz;
3540 eb64_delete(last);
3541 pool_free(pool_head_quic_arng, last);
3542 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003543 }
3544
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003545 ret = 1;
3546 out:
3547 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3548 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003549}
3550
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003551/* Set the encoded size of <arngs> QUIC ack ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003552static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003553{
3554 struct eb64_node *node, *next;
3555 struct quic_arng_node *ar, *ar_next;
3556
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003557 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3558
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003559 node = eb64_last(&arngs->root);
3560 if (!node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003561 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003562
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003563 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003564 arngs->enc_sz = quic_int_getsize(ar->last) +
3565 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3566
3567 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003568 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003569 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3570 quic_int_getsize(ar_next->last - ar_next->first.key);
3571 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003572 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003573 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003574
3575 leave:
3576 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003577}
3578
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003579/* Insert <ar> ack range into <argns> tree of ack ranges.
3580 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003581 */
3582static inline
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003583struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
3584 struct quic_arngs *arngs,
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003585 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003586{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003587 struct quic_arng_node *new_ar;
3588
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003589 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3590
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003591 new_ar = pool_alloc(pool_head_quic_arng);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003592 if (!new_ar) {
3593 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
3594 goto leave;
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003595 }
3596
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003597 new_ar->first.key = ar->first;
3598 new_ar->last = ar->last;
3599 eb64_insert(&arngs->root, &new_ar->first);
3600 arngs->sz++;
3601
3602 leave:
3603 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003604 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003605}
3606
3607/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003608 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003609 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003610 *
3611 * Descending order
3612 * ------------->
3613 * range1 range2
3614 * ..........|--------|..............|--------|
3615 * ^ ^ ^ ^
3616 * | | | |
3617 * last1 first1 last2 first2
3618 * ..........+--------+--------------+--------+......
3619 * diff1 gap12 diff2
3620 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003621 * To encode the previous list of ranges we must encode integers as follows in
3622 * descending order:
3623 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003624 * with diff1 = last1 - first1
3625 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003626 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003627 *
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003628
3629returns 0 on error
3630
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003631 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003632int quic_update_ack_ranges_list(struct quic_conn *qc,
3633 struct quic_arngs *arngs,
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003634 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003635{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003636 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003637 struct eb64_node *le;
3638 struct quic_arng_node *new_node;
3639 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003640
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003641 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3642
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003643 new = NULL;
3644 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003645 new_node = quic_insert_new_range(qc, arngs, ar);
3646 if (new_node)
3647 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003648
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003649 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003650 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003651
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003652 le = eb64_lookup_le(&arngs->root, ar->first);
3653 if (!le) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003654 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003655 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003656 goto leave;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003657
3658 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003659 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003660 else {
3661 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003662 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003663
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003664 /* Already existing range */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003665 if (le_ar->last >= ar->last) {
3666 ret = 1;
3667 }
3668 else if (le_ar->last + 1 >= ar->first) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003669 le_ar->last = ar->last;
3670 new = le;
3671 new_node = le_ar;
3672 }
3673 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003674 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003675 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003676 goto leave;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003677
3678 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003679 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003680 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003681
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003682 /* Verify that the new inserted node does not overlap the nodes
3683 * which follow it.
3684 */
3685 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003686 struct eb64_node *next;
3687 struct quic_arng_node *next_node;
3688
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003689 while ((next = eb64_next(new))) {
3690 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003691 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003692 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003693 break;
3694
3695 if (next_node->last > new_node->last)
3696 new_node->last = next_node->last;
3697 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003698 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003699 /* Decrement the size of these ranges. */
3700 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003701 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003702 }
3703
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003704 ret = 1;
3705 leave:
3706 quic_arngs_set_enc_sz(qc, arngs);
3707 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
3708 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003709}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003710/* Remove the header protection of packets at <el> encryption level.
3711 * Always succeeds.
3712 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003713static 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 +01003714{
3715 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003716 struct quic_rx_packet *pqpkt, *pkttmp;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003717 struct quic_enc_level *app_qel;
3718
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003719 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3720 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003721 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003722 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003723 TRACE_DEVEL("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003724 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003725 goto out;
3726 }
3727 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003728 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003729 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02003730 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003731 TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003732 }
3733 else {
3734 /* The AAD includes the packet number field */
3735 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3736 /* Store the packet into the tree of packets to decrypt. */
3737 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003738 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3739 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003740 TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003741 }
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003742 LIST_DELETE(&pqpkt->list);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003743 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003744 }
3745
3746 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003747 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003748}
3749
3750/* Process all the CRYPTO frame at <el> encryption level.
3751 * Return 1 if succeeded, 0 if not.
3752 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003753static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
3754 struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003755 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003756{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003757 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003758 struct eb64_node *node;
3759
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003760 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3761
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003762 node = eb64_first(&el->rx.crypto.frms);
3763 while (node) {
3764 struct quic_rx_crypto_frm *cf;
3765
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003766 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003767 if (cf->offset_node.key != el->rx.crypto.offset)
3768 break;
3769
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003770 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) {
3771 TRACE_ERROR("qc_provide_cdata() failed", QUIC_EV_CONN_TRMHP);
3772 goto leave;
3773 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774
3775 node = eb64_next(node);
3776 quic_rx_packet_refdec(cf->pkt);
3777 eb64_delete(&cf->offset_node);
3778 pool_free(pool_head_quic_rx_crypto_frm, cf);
3779 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003780
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003781 ret = 1;
3782 leave:
3783 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3784 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003785}
3786
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003787/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003788 * 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 +02003789 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003790 * Return 1 if succeeded, 0 if not.
3791 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003792int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3793 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003794{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003795 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003796 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003797 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003798 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003799 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003800 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003801
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003802 TRACE_ENTER(QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003803 qel = cur_el;
3804 next_tel:
3805 if (!qel)
3806 goto out;
3807
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003808 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003809 while (node) {
3810 struct quic_rx_packet *pkt;
3811
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003812 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003813 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003814 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003815 if (!qc_pkt_decrypt(pkt, qel, qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003816 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003817 TRACE_ERROR("packet decryption failed -> dropped",
3818 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003819 }
3820 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003821 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003822 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003823 TRACE_ERROR("packet parsing failed -> dropped",
3824 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02003825 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003826 }
3827 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003828 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3829
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003830 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3831 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3832 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003833 qc_idle_timer_rearm(qc, 1);
3834 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003835 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003836 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003837 largest_pn_time_received = pkt->time_received;
3838 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003839 /* Update the list of ranges to acknowledge. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003840 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
3841 TRACE_ERROR("Could not update ack range list",
3842 QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003843 }
3844 }
3845 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003846 eb64_delete(&pkt->pn_node);
3847 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003848 }
3849
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003850 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3851 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003852 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003853 /* Update the largest acknowledged packet timestamps */
3854 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3855 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3856 }
3857
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003858 if (!qc_treat_rx_crypto_frms(qc, qel, ctx)) {
3859 // trace already emitted by function above
3860 goto leave;
3861 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003862
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003863 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003864 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003865 qel = next_el;
Frédéric Lécaille91a211f2022-05-24 10:54:42 +02003866 largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003867 goto next_tel;
3868 }
3869
3870 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003871 ret = 1;
3872 leave:
3873 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, ctx->qc);
3874 return ret;
3875}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003876
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003877/* Check if it's possible to remove header protection for packets related to
3878 * encryption level <qel>. If <qel> is NULL, assume it's false.
3879 *
3880 * Return true if the operation is possible else false.
3881 */
3882static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3883{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003884 int ret = 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003885 enum quic_tls_enc_level tel;
3886
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003887 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3888
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003889 if (!qel)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003890 goto cant_rm_hp;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003891
3892 tel = ssl_to_quic_enc_level(qel->level);
3893
3894 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003895 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003896 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003897 goto cant_rm_hp;
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003898 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003899
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003900 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3901 TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc);
3902 goto cant_rm_hp;
3903 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003904
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003905 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003906 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003907 qc->mux_state == QC_MUX_NULL) {
3908 TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
3909 goto cant_rm_hp;
3910 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003911
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003912 ret = 1;
3913 cant_rm_hp:
3914 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3915 return ret;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003916}
3917
Amaury Denoyellecc130472022-08-17 10:08:16 +02003918/* Try to send application frames from list <frms> on connection <qc>.
3919 *
Amaury Denoyelle70467562022-08-17 16:33:13 +02003920 * Use qc_send_app_probing wrapper when probing with old data.
Amaury Denoyellecc130472022-08-17 10:08:16 +02003921 *
3922 * Returns 1 on success. Some data might not have been sent due to congestion,
3923 * in this case they are left in <frms> input list. The caller may subscribe on
3924 * quic-conn to retry later.
3925 *
3926 * Returns 0 on critical error.
3927 * TODO review and classify more distinctly transient from definitive errors to
3928 * allow callers to properly handle it.
3929 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003930static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003931{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003932 int status = 0;
3933 struct buffer *buf;
3934
3935 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3936
3937 buf = qc_txb_alloc(qc);
3938 if (!buf) {
3939 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
3940 goto leave;
3941 }
3942
Frédéric Lécailledc077512022-07-26 09:17:19 +02003943 /* Prepare and send packets until we could not further prepare packets. */
3944 while (1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003945 int ret;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003946 /* Currently buf cannot be non-empty at this stage. Even if a
3947 * previous sendto() has failed it is emptied to simulate
3948 * packet emission and rely on QUIC lost detection to try to
3949 * emit it.
3950 */
3951 BUG_ON_HOT(b_data(buf));
3952 b_reset(buf);
3953
Amaury Denoyellef2476052022-08-04 16:19:57 +02003954 ret = qc_prep_app_pkts(qc, buf, frms);
Frédéric Lécailledc077512022-07-26 09:17:19 +02003955 if (ret == -1)
3956 goto err;
3957 else if (ret == 0)
3958 goto out;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003959
Amaury Denoyellef2476052022-08-04 16:19:57 +02003960 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécailledc077512022-07-26 09:17:19 +02003961 goto err;
3962 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003963
3964 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003965 status = 1;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003966 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003967 leave:
3968 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3969 return status;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003970
3971 err:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003972 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003973 goto leave;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003974}
3975
Amaury Denoyellecc130472022-08-17 10:08:16 +02003976/* Try to send application frames from list <frms> on connection <qc>. Use this
Amaury Denoyelle70467562022-08-17 16:33:13 +02003977 * function when probing is required.
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003978 *
3979 * Returns the result from qc_send_app_pkts function.
3980 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003981static forceinline int qc_send_app_probing(struct quic_conn *qc,
3982 struct list *frms)
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003983{
3984 int ret;
3985
3986 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3987
Amaury Denoyelle70467562022-08-17 16:33:13 +02003988 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
3989 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003990 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003991 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003992
3993 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3994 return ret;
3995}
3996
Amaury Denoyelle70467562022-08-17 16:33:13 +02003997/* Try to send application frames from list <frms> on connection <qc>. This
3998 * function is provided for MUX upper layer usage only.
Amaury Denoyellecc130472022-08-17 10:08:16 +02003999 *
4000 * Returns the result from qc_send_app_pkts function.
4001 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02004002int qc_send_mux(struct quic_conn *qc, struct list *frms)
Amaury Denoyellecc130472022-08-17 10:08:16 +02004003{
4004 int ret;
4005
4006 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle70467562022-08-17 16:33:13 +02004007 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 +02004008
Amaury Denoyelle70467562022-08-17 16:33:13 +02004009 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
4010 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004011 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02004012 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004013
4014 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4015 return ret;
4016}
4017
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004018/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4019 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004020 * QUIC connection. <old_data> is used as boolean to send data already sent but
4021 * not already acknowledged (in flight).
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004022 * Returns 1 if succeeded, 0 if not.
4023 */
4024int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4025 enum quic_tls_enc_level tel, struct list *tel_frms,
4026 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4027{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004028 int ret, status = 0;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004029 struct buffer *buf = qc_txb_alloc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004030
4031 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4032
4033 if (!buf) {
4034 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4035 goto leave;
4036 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004037
Amaury Denoyellef2476052022-08-04 16:19:57 +02004038 /* Currently buf cannot be non-empty at this stage. Even if a previous
4039 * sendto() has failed it is emptied to simulate packet emission and
4040 * rely on QUIC lost detection to try to emit it.
4041 */
4042 BUG_ON_HOT(b_data(buf));
4043 b_reset(buf);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004044
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004045 if (old_data) {
4046 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004047 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004048 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02004049
4050 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004051 if (ret == -1)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004052 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004053 else if (ret == 0)
4054 goto skip_send;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004055
Amaury Denoyellef2476052022-08-04 16:19:57 +02004056 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004057 goto out;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004058
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004059 skip_send:
4060 status = 1;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004061 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004062 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004063 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004064 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004065 leave:
4066 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4067 return status;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004068}
4069
4070/* Retransmit up to two datagrams depending on packet number space */
4071static void qc_dgrams_retransmit(struct quic_conn *qc)
4072{
4073 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4074 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4075 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4076
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004077 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4078
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004079 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4080 struct list ifrms = LIST_HEAD_INIT(ifrms);
4081 struct list hfrms = LIST_HEAD_INIT(hfrms);
4082
4083 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004084 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4085 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004086 if (!LIST_ISEMPTY(&ifrms)) {
4087 iqel->pktns->tx.pto_probe = 1;
4088 if (!LIST_ISEMPTY(&hfrms)) {
4089 hqel->pktns->tx.pto_probe = 1;
4090 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4091 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004092 /* Put back unsent frames in their packet number spaces */
4093 LIST_SPLICE(&iqel->pktns->tx.frms, &ifrms);
4094 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004095 }
4096 }
4097 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004098 /* This list has potentially been already used and spliced
4099 * to another one attached to the connection. We must reinitialize it.
4100 */
4101 LIST_INIT(&hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004102 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004103 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(&hfrms)) {
4105 hqel->pktns->tx.pto_probe = 1;
4106 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
4107 QUIC_TLS_ENC_LEVEL_NONE, NULL);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004108 /* Put back unsent frames into their packet number spaces */
4109 LIST_SPLICE(&hqel->pktns->tx.frms, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004110 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004111 TRACE_STATE("no more need to probe Handshake packet number space",
4112 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004113 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4114 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004115 TRACE_STATE("no more need to probe Initial packet number space",
4116 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004117 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4118 }
4119 else {
4120 int i;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004121
4122 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004123 struct list frms1 = LIST_HEAD_INIT(frms1);
4124
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004125 hqel->pktns->tx.pto_probe = 0;
4126 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4127 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004128 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004129 if (!LIST_ISEMPTY(&frms1)) {
4130 hqel->pktns->tx.pto_probe = 1;
4131 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4132 QUIC_TLS_ENC_LEVEL_NONE, NULL);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004133 /* Put back unsent frames into their packet number spaces */
4134 LIST_SPLICE(&hqel->pktns->tx.frms, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004135 }
4136 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004137 TRACE_STATE("no more need to probe Handshake packet number space",
4138 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004139 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4140 }
4141 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004142 struct list frms2 = LIST_HEAD_INIT(frms2);
4143 struct list frms1 = LIST_HEAD_INIT(frms1);
4144
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004145 aqel->pktns->tx.pto_probe = 0;
4146 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4147 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4148 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4149 if (!LIST_ISEMPTY(&frms1)) {
4150 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004151 qc_send_app_probing(qc, &frms1);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004152 /* Put back unsent frames into their packet number spaces */
4153 LIST_SPLICE(&aqel->pktns->tx.frms, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004154 }
4155 if (!LIST_ISEMPTY(&frms2)) {
4156 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004157 qc_send_app_probing(qc, &frms2);
Frédéric Lécaillea1075202022-09-01 10:51:19 +02004158 /* Put back unsent frames into their packet number spaces */
4159 LIST_SPLICE(&aqel->pktns->tx.frms, &frms2);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004160 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004161 TRACE_STATE("no more need to probe 01RTT packet number space",
4162 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004163 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4164 }
4165 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004166 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004167}
4168
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004169/* QUIC connection packet handler task (post handshake) */
4170static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4171{
4172 struct ssl_sock_ctx *ctx;
4173 struct quic_conn *qc;
4174 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004175
4176
4177 ctx = context;
4178 qc = ctx->qc;
4179 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004180
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004181 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4182 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004183
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004184 /* Retranmissions */
4185 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004186 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004187 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4188 qc_dgrams_retransmit(qc);
4189 }
4190
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004191 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004192 qc_rm_hp_pkts(qc, qel);
4193
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004194 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) {
4195 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4196 goto out;
4197 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004198
Frédéric Lécaille47756802022-03-25 09:12:16 +01004199 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004200 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4201 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01004202 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004203 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004204
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004205 /* XXX TODO: how to limit the list frames to send */
Amaury Denoyelle70467562022-08-17 16:33:13 +02004206 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
4207 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004208 goto out;
4209 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004210
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004211 out:
4212 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004213 return t;
4214}
4215
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004216/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4217static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4218{
4219 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
4220 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
4221 qel->pktns->tx.pto_probe ||
4222 !LIST_ISEMPTY(&qel->pktns->tx.frms);
4223}
4224
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004225/* QUIC connection packet handler task. */
4226struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004227{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004228 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004229 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004230 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004231 enum quic_tls_enc_level tel, next_tel;
4232 struct quic_enc_level *qel, *next_qel;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004233 struct buffer *buf = NULL;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004234 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004235
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004236 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004237 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004238 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004239 st = qc->state;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004240 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004241
4242 /* Retranmissions */
4243 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004244 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004245 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4246 qc_dgrams_retransmit(qc);
4247 }
4248
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004249 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
4250 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004251 TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached",
4252 QUIC_EV_CONN_IO_CB, qc);
4253 /* 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 +01004254 * after the anti-amplification was reached.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004255 *
4256 * TODO: this part should be removed. This was there because the
4257 * datagram parser was not executed by only one thread.
Frédéric Lécaille59507de2022-08-11 12:14:07 +02004258 */
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004259 qc_set_timer(qc);
4260 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
4261 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
4262 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004263 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004264 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004265 (!LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004266 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004267 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004268 if (st >= QUIC_HS_ST_COMPLETE &&
4269 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004270 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004271 /* There may be remaining Handshake packets to treat and acknowledge. */
4272 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4273 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4274 }
4275 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004276 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004277
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004278 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02004279 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004280
4281 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004282 /* Treat packets waiting for header packet protection decryption */
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004283 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004284 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004285
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004286 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
4287 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4288 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004289 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004290
Frédéric Lécaille47756802022-03-25 09:12:16 +01004291 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4292 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4293 goto out;
4294
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004295 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004296 !LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004297 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
4298 qel = next_qel;
4299 next_qel = NULL;
4300 goto next_level;
4301 }
4302 else {
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004303 struct quic_rx_packet *pkt, *pkttmp;
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004304 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
4305
4306 /* Drop these 0-RTT packets */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004307 TRACE_DEVEL("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004308 list_for_each_entry_safe(pkt, pkttmp, &aqel->rx.pqpkts, list) {
4309 LIST_DELETE(&pkt->list);
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004310 quic_rx_packet_refdec(pkt);
4311 }
4312 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004313 }
4314
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004315 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004316 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004317 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004318 !quic_build_post_handshake_frames(qc))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004319 goto out;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01004320
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004321 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004322 QUIC_FL_TLS_SECRETS_DCD)) {
4323 /* Discard the Handshake keys. */
4324 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
4325 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
4326 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
4327 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004328 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004329 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004330 }
4331
4332 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
4333 /* There may be remaining handshake to build (acks) */
4334 st = QUIC_HS_ST_SERVER_HANDSHAKE;
4335 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004336 }
4337
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01004338 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
4339 * be considered.
4340 */
4341 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004342 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004343
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004344 if (!qc_need_sending(qc, qel) &&
4345 (!next_qel || !qc_need_sending(qc, next_qel))) {
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004346 goto skip_send;
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004347 }
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004348
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004349 buf = qc_txb_alloc(qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02004350 if (!buf)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004351 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004352
4353 /* Currently buf cannot be non-empty at this stage. Even if a previous
4354 * sendto() has failed it is emptied to simulate packet emission and
4355 * rely on QUIC lost detection to try to emit it.
4356 */
4357 BUG_ON_HOT(b_data(buf));
4358 b_reset(buf);
4359
4360 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02004361 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004362 if (ret == -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004363 goto out;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004364 else if (ret == 0)
4365 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004366
Amaury Denoyellef2476052022-08-04 16:19:57 +02004367 if (!qc_send_ppkts(buf, ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004368 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004369
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004370 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004371 /* Check if there is something to do for the next level.
4372 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004373 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004374 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004375 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004376 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004377 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004378 goto next_level;
4379 }
4380
Frédéric Lécaille47756802022-03-25 09:12:16 +01004381 out:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004382 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004383 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004384 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004385}
4386
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004387/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004388static 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 +01004389{
4390 int i;
4391
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004392 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4393
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004394 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4395 if (qel->tx.crypto.bufs[i]) {
4396 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4397 qel->tx.crypto.bufs[i] = NULL;
4398 }
4399 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004400 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004401
4402 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004403}
4404
4405/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004406 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004407 * Returns 1 if succeeded, 0 if not.
4408 */
4409static int quic_conn_enc_level_init(struct quic_conn *qc,
4410 enum quic_tls_enc_level level)
4411{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004412 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004413 struct quic_enc_level *qel;
4414
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004415 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4416
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004417 qel = &qc->els[level];
4418 qel->level = quic_to_ssl_enc_level(level);
4419 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4420 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4421 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004422 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004423
4424 qel->rx.pkts = EB_ROOT;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004425 LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004426 qel->rx.crypto.offset = 0;
4427 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004428
4429 /* Allocate only one buffer. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004430 /* TODO: use a pool */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004431 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4432 if (!qel->tx.crypto.bufs)
4433 goto err;
4434
4435 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4436 if (!qel->tx.crypto.bufs[0])
4437 goto err;
4438
4439 qel->tx.crypto.bufs[0]->sz = 0;
4440 qel->tx.crypto.nb_buf = 1;
4441
4442 qel->tx.crypto.sz = 0;
4443 qel->tx.crypto.offset = 0;
4444
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004445 ret = 1;
4446 leave:
4447 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4448 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004449
4450 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004451 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004452 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004453}
4454
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004455/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4456 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004457 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004458 * This function must only be called by the thread responsible of the quic_conn
4459 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004460 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004461static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004462{
4463 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004464 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004465 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004466 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004467 struct quic_rx_packet *pkt, *pktback;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004468
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004469 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4470
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004471 /* We must not free the quic-conn if the MUX is still allocated. */
4472 BUG_ON(qc->mux_state == QC_MUX_READY);
4473
Willy Tarreau54bc7862022-08-10 07:26:27 +02004474 /* in the unlikely (but possible) case the connection was just added to
4475 * the accept_list we must delete it from there.
4476 */
4477 MT_LIST_DELETE(&qc->accept_list);
4478
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004479 /* free remaining stream descriptors */
4480 node = eb64_first(&qc->streams_by_id);
4481 while (node) {
4482 struct qc_stream_desc *stream;
4483
4484 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4485 node = eb64_next(node);
4486
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004487 /* all streams attached to the quic-conn are released, so
4488 * qc_stream_desc_free will liberate the stream instance.
4489 */
4490 BUG_ON(!stream->release);
Frédéric Lécailleea4a5cb2022-08-20 18:59:36 +02004491 qc_stream_desc_free(stream, 1);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004492 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004493
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004494 /* Purge Rx packet list. */
4495 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
4496 LIST_DELETE(&pkt->qc_rx_pkt_list);
4497 pool_free(pool_head_quic_rx_packet, pkt);
4498 }
4499
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004500 if (qc->idle_timer_task) {
4501 task_destroy(qc->idle_timer_task);
4502 qc->idle_timer_task = NULL;
4503 }
4504
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004505 if (qc->timer_task) {
4506 task_destroy(qc->timer_task);
4507 qc->timer_task = NULL;
4508 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004509
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004510 /* remove the connection from receiver cids trees */
4511 ebmb_delete(&qc->odcid_node);
4512 ebmb_delete(&qc->scid_node);
4513 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004514
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004515 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004516 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004517 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004518 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004519 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004520 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004521
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004522 quic_tls_ku_free(qc);
4523 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4524 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004525 quic_conn_enc_level_uninit(qc, &qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004526 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004527 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004528
4529 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4530 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4531 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004532
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004533 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
Frédéric Lécaille8ddde4f2022-08-01 14:07:50 +02004534 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004535 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004536 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004537
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004538 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4539 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004540 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004541
4542 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004543}
4544
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004545static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004546{
4547 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004548 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004549
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004550 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004551
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004552 /* Next application data can be dropped. */
4553 qc->mux_state = QC_MUX_RELEASED;
4554
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004555 /* If the quic-conn timer has already expired free the quic-conn. */
4556 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4557 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004558 goto leave;
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004559 }
4560
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02004561 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004562 leave:
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004563 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004564}
4565
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004566/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004567static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004568{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004569 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004570 struct quic_conn *qc;
4571 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004572
4573 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004574 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004575 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004576 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004577 task->expire = TICK_ETERNITY;
4578 pktns = quic_loss_pktns(qc);
4579 if (tick_isset(pktns->tx.loss_time)) {
4580 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4581
4582 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
Frédéric Lécaille15773f22022-08-24 17:57:09 +02004583 if (!LIST_ISEMPTY(&lost_pkts))
4584 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécaille277c4622022-08-24 17:06:04 +02004585 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004586 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004587 goto out;
4588 }
4589
4590 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004591 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004592 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4593 qc->qcc->subs->events & SUB_RETRY_SEND) {
4594 struct qcc *qcc = qc->qcc;
4595
4596 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4597 tasklet_wakeup(qcc->subs->tasklet);
4598 qcc->subs->events &= ~SUB_RETRY_SEND;
4599 if (!qcc->subs->events)
4600 qcc->subs = NULL;
4601 }
4602 else {
4603 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4604 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4605 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004606 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4607 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004608 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004609 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4610 }
4611 }
4612 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
4613 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4614 }
4615 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
4616 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004617 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004618 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004619 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004620 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004621 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4622 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4623
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004624 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004625 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004626 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004627 iel->pktns->tx.pto_probe = 1;
4628 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004629
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004630 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4631 qc->path->loss.pto_count++;
4632
4633 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004634 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004635
4636 return task;
4637}
4638
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004639/* Parse the Retry token from buffer <token> with <end> a pointer to
4640 * one byte past the end of this buffer. This will extract the ODCID
4641 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004642 *
4643 * Returns 0 on success else non-zero.
4644 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004645static int parse_retry_token(struct quic_conn *qc,
4646 const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004647 struct quic_cid *odcid)
4648{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004649 int ret = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004650 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004651 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004652
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004653 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
4654
4655 if (!quic_dec_int(&odcid_len, &token, end)) {
4656 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
4657 goto leave;
4658 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004659
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004660 /* RFC 9000 7.2. Negotiating Connection IDs:
4661 * When an Initial packet is sent by a client that has not previously
4662 * received an Initial or Retry packet from the server, the client
4663 * populates the Destination Connection ID field with an unpredictable
4664 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4665 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004666 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
4667 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
4668 goto leave;
4669 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004670
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004671 if (end - token < odcid_len + sizeof timestamp) {
4672 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
4673 goto leave;
4674 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004675
4676 timestamp = ntohl(read_u32(token + odcid_len));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004677 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
4678 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
4679 goto leave;
4680 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004681
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004682 ret = 1;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004683 memcpy(odcid->data, token, odcid_len);
4684 odcid->len = odcid_len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004685 leave:
4686 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
4687 return !ret;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004688}
4689
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004690/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4691 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4692 * for QUIC servers (or haproxy listeners).
4693 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4694 * <token> the token found to be used for this connection with <token_len> as
4695 * length. <saddr> is the source address.
4696 * Returns the connection if succeeded, NULL if not.
4697 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004698static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004699 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02004700 const struct quic_cid *token_odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004701 struct sockaddr_storage *saddr,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004702 int server, int token, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004703{
4704 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004705 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004706 /* Initial CID. */
4707 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004708 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004709 struct listener *l = NULL;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004710 struct quic_cc_algo *cc_algo = NULL;
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004711 struct quic_tls_ctx *ictx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004712 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004713 qc = pool_zalloc(pool_head_quic_conn);
4714 if (!qc) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004715 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004716 goto err;
4717 }
4718
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004719 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4720 if (!buf_area) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004721 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004722 goto err;
4723 }
4724
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004725 qc->cids = EB_ROOT;
4726 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004727 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004728 struct proxy *prx;
4729
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004730 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004731 prx = l->bind_conf->frontend;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004732 cc_algo = l->bind_conf->quic_cc_algo;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004733
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004734 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4735 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004736 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004737 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004738 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004739 qc->odcid.len = dcid->len;
4740 qc->odcid.addrlen = dcid->addrlen;
4741 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004742
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004743 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004744 if (scid->len)
4745 memcpy(qc->dcid.data, scid->data, scid->len);
4746 qc->dcid.len = scid->len;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004747 qc->tx.buf = BUF_NULL;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004748 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004749 }
4750 /* QUIC Client (outgoing connection to servers) */
4751 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004752 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004753 if (dcid->len)
4754 memcpy(qc->dcid.data, dcid->data, dcid->len);
4755 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004756 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004757 qc->mux_state = QC_MUX_NULL;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02004758 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004759
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004760 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004761 if (!icid) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004762 TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004763 goto err;
4764 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004765
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004766 /* insert the allocated CID in the receiver datagram handler tree */
4767 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004768 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004769
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004770 /* Select our SCID which is the first CID with 0 as sequence number. */
4771 qc->scid = icid->cid;
4772
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004773 /* Packet number spaces initialization. */
4774 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4775 quic_pktns_init(&qc->pktns[i]);
4776 /* QUIC encryption level context initialization. */
4777 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004778 if (!quic_conn_enc_level_init(qc, i)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004779 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004780 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004781 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004782 /* Initialize the packet number space. */
4783 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4784 }
4785
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004786 qc->original_version = qv;
4787 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004788 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4789 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004790 /* TX part. */
4791 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004792 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4793 qc->tx.wbuf = qc->tx.rbuf = 0;
4794 qc->tx.bytes = 0;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004795 qc->tx.buf = BUF_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004796 /* RX part. */
4797 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004798 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004799 for (i = 0; i < QCS_MAX_TYPES; i++)
4800 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004801
4802 qc->nb_pkt_for_cc = 1;
4803 qc->nb_pkt_since_cc = 0;
4804
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004805 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004806 if (!quic_tls_ku_init(qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004807 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004808 goto err;
4809 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004810
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004811 /* XXX TO DO: Only one path at this time. */
4812 qc->path = &qc->paths[0];
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004813 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 +01004814
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004815 /* required to use MTLIST_IN_LIST */
4816 MT_LIST_INIT(&qc->accept_list);
4817
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004818 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004819 qc->stream_buf_count = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004820 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4821
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004822 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
4823 icid->stateless_reset_token,
4824 dcid->data, dcid->len,
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02004825 qc->scid.data, qc->scid.len, token_odcid))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004826 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004827
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004828 if (qc_conn_alloc_ssl_ctx(qc) ||
4829 !quic_conn_init_timer(qc) ||
4830 !quic_conn_init_idle_timer_task(qc))
4831 goto err;
4832
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004833 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
4834 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004835 goto err;
4836
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004837 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004838
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004839 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004840
4841 err:
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004842 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4843 if (qc)
4844 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004845 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004846 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004847 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004848}
4849
4850/* Initialize the timer task of <qc> QUIC connection.
4851 * Returns 1 if succeeded, 0 if not.
4852 */
4853static int quic_conn_init_timer(struct quic_conn *qc)
4854{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004855 int ret = 0;
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004856 /* Attach this task to the same thread ID used for the connection */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004857 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4858
Willy Tarreau87168752022-06-13 16:31:53 +02004859 qc->timer_task = task_new_on(qc->tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004860 if (!qc->timer_task) {
4861 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4862 goto leave;
4863 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004864
4865 qc->timer = TICK_ETERNITY;
4866 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004867 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004868
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004869 ret = 1;
4870 leave:
4871 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4872 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004873}
4874
Frédéric Lécaille47756802022-03-25 09:12:16 +01004875/* Rearm the idle timer for <qc> QUIC connection. */
4876static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4877{
4878 unsigned int expire;
4879
4880 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4881 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4882}
4883
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004884/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4885 * which is set to 1 when receiving a packet , and 0 when sending packet
4886 */
4887static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4888{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004889 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4890
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004891 if (read) {
4892 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4893 }
4894 else {
4895 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4896 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004897 qc_idle_timer_do_rearm(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004898
4899 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004900}
4901
4902/* The task handling the idle timeout */
4903static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4904{
4905 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004906 struct quic_counters *prx_counters = qc->prx_counters;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004907 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004908
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004909 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4910
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004911 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4912 * might free the quic-conn too early via quic_close().
4913 */
4914 qc_notify_close(qc);
4915
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004916 /* If the MUX is still alive, keep the quic-conn. The MUX is
4917 * responsible to call quic_close to release it.
4918 */
4919 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4920 if (qc->mux_state != QC_MUX_READY)
4921 quic_conn_release(qc);
4922
4923 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4924 * least clean some parts of it such as the tasklet.
4925 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004926
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004927 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
4928 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004929 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004930 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004931 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004932
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004933 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004934 return NULL;
4935}
4936
4937/* Initialize the idle timeout task for <qc>.
4938 * Returns 1 if succeeded, 0 if not.
4939 */
4940static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4941{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004942 int ret = 0;
4943
4944 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4945
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004946 qc->idle_timer_task = task_new_here();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004947 if (!qc->idle_timer_task) {
4948 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4949 goto leave;
4950 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004951
4952 qc->idle_timer_task->process = qc_idle_timer_task;
4953 qc->idle_timer_task->context = qc;
4954 qc_idle_timer_rearm(qc, 1);
4955 task_queue(qc->idle_timer_task);
4956
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004957 ret = 1;
4958 leave:
4959 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4960 return ret;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004961}
4962
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004963/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4964 * past one byte of this buffer.
4965 */
4966static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4967 struct quic_rx_packet *pkt)
4968{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004969 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004970 unsigned char dcid_len, scid_len;
4971
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004972 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
4973
4974 if (end == *buf) {
4975 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
4976 goto leave;
4977 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004978
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004979 /* Destination Connection ID Length */
4980 dcid_len = *(*buf)++;
4981 /* 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 +02004982 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
4983 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
4984 goto leave;
4985 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004986
4987 if (dcid_len) {
4988 /* Check that the length of this received DCID matches the CID lengths
4989 * of our implementation for non Initials packets only.
4990 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004991 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4992 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004993 dcid_len != QUIC_HAP_CID_LEN) {
4994 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
4995 goto leave;
4996 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004997
4998 memcpy(pkt->dcid.data, *buf, dcid_len);
4999 }
5000
5001 pkt->dcid.len = dcid_len;
5002 *buf += dcid_len;
5003
5004 /* Source Connection ID Length */
5005 scid_len = *(*buf)++;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005006 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
5007 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
5008 goto leave;
5009 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005010
5011 if (scid_len)
5012 memcpy(pkt->scid.data, *buf, scid_len);
5013 pkt->scid.len = scid_len;
5014 *buf += scid_len;
5015
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005016 ret = 1;
5017 leave:
5018 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
5019 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005020}
5021
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005022/* Insert <pkt> RX packet in its <qel> RX packets tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005023static void qc_pkt_insert(struct quic_conn *qc,
5024 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005025{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005026 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
5027
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005028 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01005029 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005030 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005031
5032 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005033}
5034
5035/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005036 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
5037 * byte past the end of the buffer containing this packet and <beg> the address of
5038 * the packet first byte.
5039 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
5040 * Returns 1 if succeeded, 0 if not.
5041 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005042static inline int qc_try_rm_hp(struct quic_conn *qc,
5043 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005044 unsigned char *buf, unsigned char *beg,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005045 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005046{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005047 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005048 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005049 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005050 struct quic_enc_level *qel;
5051 /* Only for traces. */
5052 struct quic_rx_packet *qpkt_trace;
5053
5054 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005055 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005056 /* The packet number is here. This is also the start minus
5057 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5058 * protection.
5059 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005060 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005061
5062 tel = quic_packet_type_enc_level(pkt->type);
5063 qel = &qc->els[tel];
5064
5065 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005066 /* Note that the following function enables us to unprotect the packet
5067 * number and its length subsequently used to decrypt the entire
5068 * packets.
5069 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005070 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02005071 qel->pktns->rx.largest_pn, pn, beg)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005072 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005073 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005074 }
5075
5076 /* The AAD includes the packet number field found at <pn>. */
5077 pkt->aad_len = pn - beg + pkt->pnl;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005078 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
5079 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005080 goto out;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005081 }
5082
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005083 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005084 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01005085 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01005086 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005087 /* If the packet number space has been discarded, this packet
5088 * will be not parsed.
5089 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005090 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005091 goto out;
5092 }
5093
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005094 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005095 pkt->pn_offset = pn - beg;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02005096 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02005097 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005098 }
5099
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005100 *el = qel;
5101 /* No reference counter incrementation here!!! */
5102 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
5103 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
5104 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
5105 b_add(&qc->rx.buf, pkt->len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005106
5107 ret = 1;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005108 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005109 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005110 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005111}
5112
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005113/* 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 +02005114 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
5115 * of this packet into <*version>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005116 */
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005117static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
5118 unsigned char **buf, const unsigned char *end,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005119 int *long_header, uint32_t *version)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005120{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005121 int ret = 0;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005122 const unsigned char byte0 = **buf;
5123
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005124 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5125
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005126 (*buf)++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005127 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005128 unsigned char type =
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005129 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005130
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005131 *long_header = 1;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005132 /* Version */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005133 if (!quic_read_uint32(version, (const unsigned char **)buf, end)) {
5134 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
5135 goto out;
5136 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005137
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005138 if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005139 pkt->type = type;
5140 }
5141 else {
5142 switch (type) {
5143 case 0:
5144 pkt->type = QUIC_PACKET_TYPE_RETRY;
5145 break;
5146 case 1:
5147 pkt->type = QUIC_PACKET_TYPE_INITIAL;
5148 break;
5149 case 2:
5150 pkt->type = QUIC_PACKET_TYPE_0RTT;
5151 break;
5152 case 3:
5153 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
5154 break;
5155 }
5156 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005157 }
5158 else {
5159 pkt->type = QUIC_PACKET_TYPE_SHORT;
5160 *long_header = 0;
5161 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005162
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005163 ret = 1;
5164 out:
5165 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005166 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005167}
5168
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005169/* Return the QUIC version (quic_version struct) with <version> as version number
5170 * if supported or NULL if not.
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005171 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005172static inline const struct quic_version *qc_supported_version(uint32_t version)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005173{
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005174 int i;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005175
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005176 for (i = 0; i < quic_versions_nb; i++)
5177 if (quic_versions[i].num == version)
5178 return &quic_versions[i];
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005179
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005180 return NULL;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005181}
5182
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005183/*
5184 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
5185 * address <addr>.
5186 * Implementation of RFC9000 6. Version Negotiation
5187 *
5188 * TODO implement a rate-limiting sending of Version Negotiation packets
5189 *
5190 * Returns 0 on success else non-zero
5191 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005192static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
5193 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005194{
5195 char buf[256];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005196 int ret = 0, i = 0, j;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005197 uint32_t version;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005198 const socklen_t addrlen = get_addr_len(addr);
5199
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005200 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005201 /*
5202 * header form
5203 * long header, fixed bit to 0 for Version Negotiation
5204 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005205 /* TODO: RAND_bytes() should be replaced? */
5206 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
5207 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
5208 goto out;
5209 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005210
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01005211 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005212 /* null version for Version Negotiation */
5213 buf[i++] = '\x00';
5214 buf[i++] = '\x00';
5215 buf[i++] = '\x00';
5216 buf[i++] = '\x00';
5217
5218 /* source connection id */
5219 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005220 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005221 i += pkt->scid.len;
5222
5223 /* destination connection id */
5224 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005225 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005226 i += pkt->dcid.len;
5227
5228 /* supported version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005229 for (j = 0; j < quic_versions_nb; j++) {
5230 version = htonl(quic_versions[j].num);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005231 memcpy(&buf[i], &version, sizeof(version));
5232 i += sizeof(version);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005233 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005234
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005235 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005236 goto out;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005237
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005238 ret = 1;
5239 out:
5240 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5241 return !ret;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005242}
5243
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005244/* Send a stateless reset packet depending on <pkt> RX packet information
5245 * from <fd> UDP socket to <dst>
5246 * Return 1 if succeeded, 0 if not.
5247 */
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005248static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005249 struct quic_rx_packet *rxpkt)
5250{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005251 int ret = 0, pktlen, rndlen;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005252 unsigned char pkt[64];
5253 const socklen_t addrlen = get_addr_len(dstaddr);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005254 struct proxy *prx;
5255 struct quic_counters *prx_counters;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005256
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005257 TRACE_ENTER(QUIC_EV_STATELESS_RST);
5258
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005259 prx = l->bind_conf->frontend;
5260 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005261 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
5262 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
5263 * Reset is difficult to distinguish from other packets if the recipient requires
5264 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
5265 * that all packets it sends are at least 22 bytes longer than the minimum
5266 * connection ID length that it requests the peer to include in its packets,
5267 * adding PADDING frames as necessary. This ensures that any Stateless Reset
5268 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
5269 * An endpoint that sends a Stateless Reset in response to a packet that is
5270 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
5271 * than the packet it responds to.
5272 */
5273
5274 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
5275 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
5276 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
5277 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005278
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005279 /* Put a header of random bytes */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005280 /* TODO: RAND_bytes() should be replaced */
5281 if (RAND_bytes(pkt, rndlen) != 1) {
5282 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
5283 goto leave;
5284 }
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005285
5286 /* Clear the most significant bit, and set the second one */
5287 *pkt = (*pkt & ~0x80) | 0x40;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005288 if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005289 rxpkt->dcid.data, rxpkt->dcid.len))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005290 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005291
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005292 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005293 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005294
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005295 ret = 1;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005296 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005297 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005298 leave:
5299 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
5300 return ret;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005301}
5302
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005303/* QUIC server only function.
5304 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005305 * This is the responsibility of the caller to check <aad> size is big enough
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005306 * to contain these data.
5307 * Return the number of bytes copied to <aad>.
5308 */
5309static int quic_generate_retry_token_aad(unsigned char *aad,
5310 uint32_t version,
5311 const struct quic_cid *cid,
5312 const struct sockaddr_storage *addr)
5313{
5314 unsigned char *p;
5315
5316 p = aad;
5317 memcpy(p, &version, sizeof version);
5318 p += sizeof version;
5319 p += quic_saddr_cpy(p, addr);
5320 memcpy(p, cid->data, cid->len);
5321 p += cid->len;
5322
5323 return p - aad;
5324}
5325
5326/* QUIC server only function.
5327 * Generate the token to be used in Retry packets. The token is written to
5328 * <buf> whith <len> as length. <odcid> is the original destination connection
5329 * ID and <dcid> is our side destination connection ID (or client source
5330 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005331 * Returns the length of the encoded token or 0 on error.
5332 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005333static int quic_generate_retry_token(unsigned char *buf, size_t len,
5334 const uint32_t version,
5335 const struct quic_cid *odcid,
5336 const struct quic_cid *dcid,
5337 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005338{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005339 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005340 unsigned char *p;
5341 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5342 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5343 size_t aadlen;
5344 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
5345 unsigned char key[QUIC_TLS_KEY_LEN];
5346 unsigned char iv[QUIC_TLS_IV_LEN];
5347 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5348 size_t seclen = strlen(global.cluster_secret);
5349 EVP_CIPHER_CTX *ctx = NULL;
5350 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5351 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005352
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005353 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5354
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005355 /* We copy the odcid into the token, prefixed by its one byte
5356 * length, the format token byte. It is followed by an AEAD TAG, and finally
5357 * the random bytes used to derive the secret to encrypt the token.
5358 */
5359 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005360 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005361
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005362 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005363 /* TODO: RAND_bytes() should be replaced */
5364 if (RAND_bytes(salt, sizeof salt) != 1) {
5365 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005366 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005367 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005368
5369 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 +02005370 salt, sizeof salt, sec, seclen)) {
5371 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005372 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005373 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005374
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005375 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
5376 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005377 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005378 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005379
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005380 /* Token build */
5381 p = buf;
5382 *p++ = QUIC_TOKEN_FMT_RETRY,
5383 *p++ = odcid->len;
5384 memcpy(p, odcid->data, odcid->len);
5385 p += odcid->len;
5386 write_u32(p, htonl(timestamp));
5387 p += sizeof timestamp;
5388
5389 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005390 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
5391 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005392 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005393 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005394
5395 p += QUIC_TLS_TAG_LEN;
5396 memcpy(p, salt, sizeof salt);
5397 p += sizeof salt;
5398 EVP_CIPHER_CTX_free(ctx);
5399
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005400 ret = p - buf;
5401 leave:
5402 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5403 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005404
5405 err:
5406 if (ctx)
5407 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005408 goto leave;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005409}
5410
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005411/* QUIC server only function.
5412 * Check the validity of the Retry token from <token> buffer with <tokenlen>
5413 * as length. If valid, the ODCID of <qc> QUIC connection will be put
5414 * into <odcid> connection ID. <dcid> is our side destination connection ID
5415 * of client source connection ID.
5416 * Return 1 if succeeded, 0 if not.
5417 */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005418static int quic_retry_token_check(const unsigned char *token, size_t tokenlen,
5419 const struct quic_version *qv,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005420 struct quic_cid *odcid,
5421 const struct quic_cid *dcid,
5422 struct quic_conn *qc,
5423 struct sockaddr_storage *addr)
5424{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005425 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005426 unsigned char buf[128];
5427 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5428 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5429 size_t aadlen;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005430 const unsigned char *salt;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005431 unsigned char key[QUIC_TLS_KEY_LEN];
5432 unsigned char iv[QUIC_TLS_IV_LEN];
5433 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5434 size_t seclen = strlen(global.cluster_secret);
5435 EVP_CIPHER_CTX *ctx = NULL;
5436 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5437
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005438 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5439
5440 if (sizeof buf < tokenlen) {
5441 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
5442 goto err;
5443 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005444
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005445 aadlen = quic_generate_retry_token_aad(aad, qv->num, dcid, addr);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005446 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
5447 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 +02005448 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005449 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
5450 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005451 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005452
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005453 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
5454 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005455 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005456 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005457
5458 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
5459 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 +02005460 ctx, aead, key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005461 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005462 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005463 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005464
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005465 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
5466 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005467 goto err;
5468 }
5469
5470 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005471
5472 ret = 1;
5473 leave:
5474 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5475 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005476
5477 err:
5478 if (ctx)
5479 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005480 goto leave;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005481}
5482
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005483/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
5484 * the Initial <pkt> packet.
5485 *
5486 * Returns 0 on success else non-zero.
5487 */
5488static int send_retry(int fd, struct sockaddr_storage *addr,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005489 struct quic_rx_packet *pkt, const struct quic_version *qv)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005490{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005491 int ret = 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005492 unsigned char buf[128];
5493 int i = 0, token_len;
5494 const socklen_t addrlen = get_addr_len(addr);
5495 struct quic_cid scid;
5496
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005497 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5498
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005499 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
5500 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
5501 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005502 /* version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005503 buf[i++] = *((unsigned char *)&qv->num + 3);
5504 buf[i++] = *((unsigned char *)&qv->num + 2);
5505 buf[i++] = *((unsigned char *)&qv->num + 1);
5506 buf[i++] = *(unsigned char *)&qv->num;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005507
5508 /* Use the SCID from <pkt> for Retry DCID. */
5509 buf[i++] = pkt->scid.len;
5510 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
5511 i += pkt->scid.len;
5512
5513 /* Generate a new CID to be used as SCID for the Retry packet. */
5514 scid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005515 /* TODO: RAND_bytes() should be replaced */
5516 if (RAND_bytes(scid.data, scid.len) != 1) {
5517 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
5518 goto out;
5519 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005520
5521 buf[i++] = scid.len;
5522 memcpy(&buf[i], scid.data, scid.len);
5523 i += scid.len;
5524
5525 /* token */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005526 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005527 &pkt->dcid, &pkt->scid, addr))) {
5528 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
5529 goto out;
5530 }
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01005531
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005532 i += token_len;
5533
5534 /* token integrity tag */
5535 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
5536 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005537 pkt->dcid.len, buf, i, qv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005538 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5539 goto out;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005540 }
5541
5542 i += QUIC_TLS_TAG_LEN;
5543
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005544 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
5545 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5546 goto out;
5547 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005548
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005549 ret = 1;
5550 out:
5551 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5552 return !ret;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005553}
5554
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005555/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5556 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5557 * concatenated to the <pkt> DCID field.
5558 *
5559 * Returns the instance or NULL if not found.
5560 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005561static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005562 struct listener *l,
5563 struct sockaddr_storage *saddr)
5564{
5565 struct quic_conn *qc = NULL;
5566 struct ebmb_node *node;
5567 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005568 /* set if the quic_conn is found in the second DCID tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005569
5570 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005571
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005572 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5573 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5574 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5575 /* DCIDs of first packets coming from multiple clients may have
5576 * the same values. Let's distinguish them by concatenating the
5577 * socket addresses.
5578 */
5579 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005580 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005581 pkt->dcid.len + pkt->dcid.addrlen);
5582 if (node) {
5583 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5584 goto end;
5585 }
5586 }
5587
5588 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5589 * also for INITIAL/0-RTT non-first packets with the final DCID in
5590 * used.
5591 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005592 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005593 if (!node)
5594 goto end;
5595
5596 id = ebmb_entry(node, struct quic_connection_id, node);
5597 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005598
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005599 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5600 * If already done, this is a noop.
5601 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005602 if (qc)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005603 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005604
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005605 end:
5606 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005607 return qc;
5608}
5609
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005610/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5611 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5612 * parameters of this session.
5613 * This is the responsibility of the caller to check the validity of all the
5614 * pointers passed as parameter to this function.
5615 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5616 * CO_ER_SSL_NO_MEM.
5617 */
5618static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5619 unsigned char *params, size_t params_len)
5620{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005621 int retry, ret = -1;
5622
5623 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005624
5625 retry = 1;
5626 retry:
5627 *ssl = SSL_new(ssl_ctx);
5628 if (!*ssl) {
5629 if (!retry--)
5630 goto err;
5631
5632 pool_gc(NULL);
5633 goto retry;
5634 }
5635
5636 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005637 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005638 SSL_free(*ssl);
5639 *ssl = NULL;
5640 if (!retry--)
5641 goto err;
5642
5643 pool_gc(NULL);
5644 goto retry;
5645 }
5646
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005647 ret = 0;
5648 leave:
5649 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5650 return ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005651
5652 err:
5653 qc->conn->err_code = CO_ER_SSL_NO_MEM;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005654 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005655}
5656
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005657/* Finalize <qc> QUIC connection:
5658 * - initialize the Initial QUIC TLS context for negotiated version,
5659 * - derive the secrets for this context,
5660 * - encode the transport parameters to be sent,
5661 * - set them into the TLS stack,
5662 * - initialize ->max_ack_delay and max_idle_timeout,
5663 *
5664 * MUST be called after having received the remote transport parameters.
5665 * Return 1 if succeeded, 0 if not.
5666 */
5667int qc_conn_finalize(struct quic_conn *qc, int server)
5668{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005669 int ret = 0;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005670 struct quic_transport_params *tx_tp = &qc->tx.params;
5671 struct quic_transport_params *rx_tp = &qc->rx.params;
5672 const struct quic_version *ver;
5673
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005674 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5675
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005676 if (tx_tp->version_information.negotiated_version &&
5677 tx_tp->version_information.negotiated_version != qc->original_version) {
5678 qc->negotiated_version =
5679 qc->tx.params.version_information.negotiated_version;
5680 if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
5681 qc->odcid.data, qc->odcid.len, !server))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005682 goto out;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005683
5684 ver = qc->negotiated_version;
5685 }
5686 else {
5687 ver = qc->original_version;
5688 }
5689
5690 qc->enc_params_len =
5691 quic_transport_params_encode(qc->enc_params,
5692 qc->enc_params + sizeof qc->enc_params,
5693 &qc->rx.params, ver, 1);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005694 if (!qc->enc_params_len) {
5695 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_TXPKT);
5696 goto out;
5697 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005698
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005699 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
5700 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_TXPKT);
5701 goto out;
5702 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005703
5704 if (tx_tp->max_ack_delay)
5705 qc->max_ack_delay = tx_tp->max_ack_delay;
5706
5707 if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout)
5708 qc->max_idle_timeout =
5709 QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5710 else
5711 qc->max_idle_timeout =
5712 QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5713
5714 TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp);
5715
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005716 ret = 1;
5717 out:
5718 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5719 return ret;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005720}
5721
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005722/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5723 * used to process <qc> received packets. The allocated context is stored in
5724 * <qc.xprt_ctx>.
5725 *
5726 * Returns 0 on success else non-zero.
5727 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005728static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005729{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005730 int ret = 0;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005731 struct bind_conf *bc = qc->li->bind_conf;
5732 struct ssl_sock_ctx *ctx = NULL;
5733
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005734 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5735
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005736 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005737 if (!ctx) {
5738 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005739 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005740 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005741
5742 ctx->wait_event.tasklet = tasklet_new();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005743 if (!ctx->wait_event.tasklet) {
5744 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005745 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005746 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005747
5748 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5749 ctx->wait_event.tasklet->context = ctx;
5750 ctx->wait_event.events = 0;
5751 ctx->subs = NULL;
5752 ctx->xprt_ctx = NULL;
5753 ctx->qc = qc;
5754
5755 /* Set tasklet tid based on the SCID selected by us for this
5756 * connection. The upper layer will also be binded on the same thread.
5757 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005758 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005759
5760 if (qc_is_listener(qc)) {
5761 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5762 qc->enc_params, qc->enc_params_len) == -1) {
5763 goto err;
5764 }
William Lallemande6ec6262022-09-02 16:24:39 +02005765#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005766 /* Enabling 0-RTT */
5767 if (bc->ssl_conf.early_data)
5768 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
William Lallemande6ec6262022-09-02 16:24:39 +02005769#endif
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005770
5771 SSL_set_accept_state(ctx->ssl);
5772 }
5773
5774 ctx->xprt = xprt_get(XPRT_QUIC);
5775
5776 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005777 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005778
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005779 ret = 1;
5780 leave:
5781 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5782 return !ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005783
5784 err:
5785 if (ctx && ctx->wait_event.tasklet)
5786 tasklet_free(ctx->wait_event.tasklet);
5787 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005788 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005789}
5790
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005791/* Check that all the bytes between <buf> included and <end> address
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005792 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005793 * check that there is at least one byte between <buf> end <end>.
5794 * Return 1 if this all the bytes are null, 0 if not.
5795 */
5796static inline int quic_padding_check(const unsigned char *buf,
5797 const unsigned char *end)
5798{
5799 while (buf < end && !*buf)
5800 buf++;
5801
5802 return buf == end;
5803}
5804
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005805/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5806 * end of this buffer past one byte and populate <pkt> RX packet structure
5807 * with the information collected from the packet.
5808 * This function sets ->len <pkt> field value to the end of the packet past one
5809 * byte to enable the caller to run this function again to continue to parse
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005810 * the remaining QUIC packets carried by the datagram.
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005811 * Note that this function always sets this ->len value. If a paquet could
5812 * not be correctly found, ->len value will be set to the remaining number
5813 * bytes in the datagram to entirely consume this latter.
5814 */
5815static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5816 struct quic_rx_packet *pkt, int first_pkt,
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02005817 struct quic_dgram *dgram, struct list **tasklist_head)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005818{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005819 unsigned char *beg, *payload;
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02005820 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005821 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005822 struct proxy *prx;
5823 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005824 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005825 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005826 size_t b_cspace;
5827 struct quic_enc_level *qel;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005828 uint32_t version;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005829 const struct quic_version *qv = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005830
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005831 TRACE_ENTER(QUIC_EV_CONN_LPKT);
5832
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005833 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005834 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005835 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005836 qel = NULL;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005837 l = dgram->owner;
5838 prx = l->bind_conf->frontend;
5839 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005840 /* This ist only to please to traces and distinguish the
5841 * packet with parsed packet number from others.
5842 */
5843 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005844 if (end <= buf) {
5845 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5846 goto drop;
5847 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005848
5849 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005850 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005851 if (!first_pkt && quic_padding_check(buf, end)) {
5852 /* Some browsers may pad the remaining datagram space with null bytes.
5853 * That is what we called add padding out of QUIC packets. Such
5854 * datagrams must be considered as valid. But we can only consume
5855 * the remaining space.
5856 */
5857 pkt->len = end - buf;
5858 goto drop_no_conn;
5859 }
5860
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005861 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005862 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005863 }
5864
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005865 /* Header form */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005866 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005867 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5868 goto drop;
5869 }
5870
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005871 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005872 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005873 struct quic_cid odcid;
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02005874 const struct quic_cid *token_odcid = NULL; // ODCID received from client token
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005875
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005876 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005877 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005878 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005879 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005880 }
5881
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005882 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5883 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005884 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005885 }
5886 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5887 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005888 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02005889 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
Frédéric Lécaillee7df68a2022-08-05 09:34:44 +02005890 goto drop;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005891 }
5892
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005893 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5894 * they must have the same DCID.
5895 */
5896 if (!first_pkt &&
5897 (pkt->dcid.len != dgram->dcid_len ||
5898 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5899 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005900 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005901 }
5902
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005903 /* Retry of Version Negotiation packets are only sent by servers */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005904 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005905 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005906 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005907 }
5908
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005909 /* RFC9000 6. Version Negotiation */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005910 qv = qc_supported_version(version);
5911 if (!qv) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005912 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005913 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005914 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005915 goto err;
5916 }
5917
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005918 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005919 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005920 }
5921
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005922 /* For Initial packets, and for servers (QUIC clients connections),
5923 * there is no Initial connection IDs storage.
5924 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005925 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005926 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005927
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005928 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5929 end - buf < token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005930 TRACE_PROTO("Packet dropped",
5931 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005932 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005933 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005934
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005935 /* TODO Retry should be automatically activated if
5936 * suspect network usage is detected.
5937 */
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005938 if (global.cluster_secret && !token_len) {
5939 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
5940 TRACE_PROTO("Initial without token, sending retry",
5941 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
5942 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
5943 TRACE_PROTO("Error during Retry generation",
5944 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005945 goto err;
5946 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005947
5948 HA_ATOMIC_INC(&prx_counters->retry_sent);
5949 goto err;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005950 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005951 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005952 else if (!global.cluster_secret && token_len) {
5953 /* Impossible case: a token was received without configured
5954 * cluster secret.
5955 */
5956 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
5957 NULL, NULL, NULL, qv);
5958 goto drop;
5959 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005960
5961 pkt->token = buf;
5962 pkt->token_len = token_len;
5963 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005964 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005965 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005966 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005967 TRACE_PROTO("Packet dropped",
5968 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005969 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005970 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005971 }
5972
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005973 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5974 end - buf < len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005975 TRACE_PROTO("Packet dropped",
5976 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005977 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005978 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005979
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005980 payload = buf;
5981 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005982 if (drop_no_conn)
5983 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005984
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005985 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005986 if (global.cluster_secret && pkt->token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005987 if (*pkt->token == QUIC_TOKEN_FMT_RETRY) {
5988 const struct quic_version *ver = qc ? qc->original_version : qv;
5989 if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid,
5990 &pkt->scid, qc, &dgram->saddr)) {
5991 HA_ATOMIC_INC(&prx_counters->retry_error);
5992 TRACE_PROTO("Wrong retry token",
5993 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5994 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
5995 * with an INVALID_TOKEN error.
5996 */
5997 goto drop;
5998 }
5999
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02006000 token_odcid = &odcid;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006001 HA_ATOMIC_INC(&prx_counters->retry_validated);
6002 }
6003 else {
6004 /* TODO: New token check */
6005 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
6006 goto drop;
6007 }
6008 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01006009 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006010 int ipv4;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006011
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006012 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006013 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006014 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006015 }
6016
Willy Tarreau787e92a2022-05-20 16:06:01 +02006017 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 +02006018 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006019 TRACE_PROTO("Initial without token, sending retry",
6020 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02006021 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille59507de2022-08-11 12:14:07 +02006022 TRACE_ERROR("Error during Retry generation",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006023 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02006024 goto err;
6025 }
6026
6027 HA_ATOMIC_INC(&prx_counters->retry_sent);
6028 goto err;
6029 }
6030
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02006031 /* RFC 9000 7.2. Negotiating Connection IDs:
6032 * When an Initial packet is sent by a client that has not previously
6033 * received an Initial or Retry packet from the server, the client
6034 * populates the Destination Connection ID field with an unpredictable
6035 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6036 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01006037 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006038 TRACE_PROTO("dropped packet",
6039 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006040 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01006041 }
6042
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006043 pkt->saddr = dgram->saddr;
6044 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006045
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02006046 qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, token_odcid,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02006047 &pkt->saddr, 1, !!pkt->token_len, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02006048 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006049 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006050
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006051 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02006052 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02006053 ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
6054 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006055 }
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02006056
6057 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006058 }
6059 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006060 TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006061 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006062 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006063 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006064 }
6065
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006066 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006067 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006068
6069 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6070 * they must have the same DCID.
6071 */
6072 if (!first_pkt &&
6073 (pkt->dcid.len != dgram->dcid_len ||
6074 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
6075 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006076 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006077 }
6078
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006079 buf += QUIC_HAP_CID_LEN;
6080
6081 /* A short packet is the last one of a UDP datagram. */
6082 payload = buf;
6083 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006084
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006085 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006086 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006087 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006088 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006089 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006090 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006091 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006092 }
6093
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006094 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006095 }
6096
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006097 if (qc->flags & QUIC_FL_CONN_CLOSING) {
6098 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
6099 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
6100 qc->nb_pkt_for_cc++;
6101 qc->nb_pkt_since_cc = 0;
6102 }
6103 /* Skip the entire datagram */
6104 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006105 TRACE_STATE("Closing state connection",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006106 QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006107 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006108 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006109
6110 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6111 * they must have the same DCID.
6112 *
6113 * This check must be done after the final update to pkt.len to
6114 * properly drop the packet on failure.
6115 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006116 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006117 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006118 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006119 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006120 /* Reset the anti-amplification bit. It will be set again
6121 * when sending the next packet if reached again.
6122 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006123 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
6124 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006125 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006126 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006127
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006128 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006129
Amaury Denoyelle72d86502022-07-13 15:08:23 +02006130 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006131 TRACE_PROTO("Connection error",
6132 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006133 goto out;
6134 }
6135
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006136 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006137 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006138 b_cspace = b_contig_space(&qc->rx.buf);
6139 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006140 /* Do not consume buf if space not at the end. */
6141 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006142 TRACE_PROTO("Packet dropped",
6143 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006144 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6145 goto drop_no_conn;
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006146 }
6147
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006148 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006149 if (b_cspace) {
6150 b_putchr(&qc->rx.buf, 0x00);
6151 b_cspace--;
6152 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006153 b_add(&qc->rx.buf, b_cspace);
6154 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006155 TRACE_PROTO("Too big packet",
6156 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006157 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6158 goto drop_no_conn;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006159 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006160 }
6161
Frédéric Lécaille99897d12022-08-08 10:28:07 +02006162 if (!qc_try_rm_hp(qc, pkt, payload, beg, &qel)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006163 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006164 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02006165 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006166
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006167 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006168 if (pkt->aad_len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006169 qc_pkt_insert(qc, pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006170 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02006171 /* Wake up the connection packet handler task from here only if all
6172 * the contexts have been initialized, especially the mux context
6173 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
6174 * will start it if these contexts for the connection are not already
6175 * initialized.
6176 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006177 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006178 if (conn_ctx)
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02006179 *tasklist_head = tasklet_wakeup_after(*tasklist_head, conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02006180
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006181 drop_no_conn:
6182 if (drop_no_conn)
6183 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006184 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006185
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006186 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006187
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006188 drop:
6189 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006190 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006191 /* Wakeup the I/O handler callback if the PTO timer must be armed.
6192 * This cannot be done by this thread.
6193 */
6194 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006195 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006196 if (conn_ctx && conn_ctx->wait_event.tasklet)
6197 tasklet_wakeup(conn_ctx->wait_event.tasklet);
6198 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01006199 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006200 if (!pkt->len)
6201 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006202 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006203}
6204
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006205/* This function builds into <buf> buffer a QUIC long packet header.
6206 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006207 */
6208static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006209 int type, size_t pn_len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006210 struct quic_conn *qc, const struct quic_version *ver)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006211{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006212 int ret = 0;
6213
6214 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6215
6216 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
6217 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6218 goto leave;
6219 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006220
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006221 type = quic_pkt_type(type, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006222 /* #0 byte flags */
6223 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
6224 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
6225 /* Version */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006226 quic_write_uint32(buf, end, ver->num);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006227 *(*buf)++ = qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006228 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006229 if (qc->dcid.len) {
6230 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6231 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006232 }
6233 /* Source connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006234 *(*buf)++ = qc->scid.len;
6235 if (qc->scid.len) {
6236 memcpy(*buf, qc->scid.data, qc->scid.len);
6237 *buf += qc->scid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006238 }
6239
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006240 ret = 1;
6241 leave:
6242 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6243 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006244}
6245
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006246/* This function builds into <buf> buffer a QUIC short packet header.
6247 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006248 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006249static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006250 size_t pn_len, struct quic_conn *qc,
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006251 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006252{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006253 int ret = 0;
6254
6255 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6256
6257 if (end - *buf < 1 + qc->dcid.len) {
6258 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6259 goto leave;
6260 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006261
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006262 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01006263 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
6264 ((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 +01006265 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006266 if (qc->dcid.len) {
6267 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6268 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006269 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006270
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006271 ret = 1;
6272 leave:
6273 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6274 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006275}
6276
6277/* Apply QUIC header protection to the packet with <buf> as first byte address,
6278 * <pn> as address of the Packet number field, <pnlen> being this field length
6279 * with <aead> as AEAD cipher and <key> as secret key.
6280 * Returns 1 if succeeded or 0 if failed.
6281 */
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006282static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf,
6283 unsigned char *pn, size_t pnlen,
6284 struct quic_tls_ctx *tls_ctx)
6285
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006286{
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006287 int i, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006288 /* We need an IV of at least 5 bytes: one byte for bytes #0
6289 * and at most 4 bytes for the packet number
6290 */
6291 unsigned char mask[5] = {0};
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006292 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006293
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006294 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6295
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006296 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
6297 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006298 goto out;
6299 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006300
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006301 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
6302 for (i = 0; i < pnlen; i++)
6303 pn[i] ^= mask[i + 1];
6304
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006305 ret = 1;
6306 out:
6307 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006308 return ret;
6309}
6310
6311/* Reduce the encoded size of <ack_frm> ACK frame removing the last
6312 * ACK ranges if needed to a value below <limit> in bytes.
6313 * Return 1 if succeeded, 0 if not.
6314 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006315static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
6316 struct quic_frame *ack_frm, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006317{
6318 size_t room, ack_delay_sz;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006319 int ret = 0;
6320
6321 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006322
6323 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
6324 /* A frame is made of 1 byte for the frame type. */
6325 room = limit - ack_delay_sz - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006326 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
6327 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006328
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006329 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
6330 leave:
6331 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6332 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006333}
6334
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006335/* Prepare into <outlist> as most as possible ack-eliciting frame from their
6336 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
6337 * with <room> as available room, and <*len> the packet Length field initialized
6338 * with the number of bytes already present in this buffer which must be taken
6339 * into an account for the Length packet field value. <headlen> is the number of
6340 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006341 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006342 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006343 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006344 * 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 +01006345 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006346static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006347 size_t room, size_t *len, size_t headlen,
6348 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006349 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006350{
Frédéric Lécailleea604992020-12-24 13:01:37 +01006351 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006352 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006353
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006354 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
6355
Frédéric Lécailleea604992020-12-24 13:01:37 +01006356 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006357 if (*len > room)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006358 goto leave;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006359
Frédéric Lécailleea604992020-12-24 13:01:37 +01006360 /* If we are not probing we must take into an account the congestion
6361 * control window.
6362 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006363 if (!qel->pktns->tx.pto_probe) {
6364 size_t remain = quic_path_prep_data(qc->path);
6365
6366 if (headlen > remain)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006367 goto leave;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006368
6369 room = QUIC_MIN(room, remain - headlen);
6370 }
6371
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006372 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006373 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006374
6375 /* NOTE: switch/case block inside a loop, a successful status must be
6376 * returned by this function only if at least one frame could be built
6377 * in the switch/case block.
6378 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006379 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006380 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006381 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006382
Frédéric Lécailleea604992020-12-24 13:01:37 +01006383 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006384 break;
6385
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006386 switch (cf->type) {
6387 case QUIC_FT_CRYPTO:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006388 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006389 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006390 /* Compute the length of this CRYPTO frame header */
6391 hlen = 1 + quic_int_getsize(cf->crypto.offset);
6392 /* Compute the data length of this CRyPTO frame. */
6393 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006394 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006395 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006396 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006397 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006398
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006399 /* CRYPTO frame length. */
6400 flen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006401 TRACE_DEVEL(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006402 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006403 /* Add the CRYPTO data length and its encoded length to the packet
6404 * length and the length of this length.
6405 */
6406 *len += flen;
6407 room -= flen;
6408 if (dlen == cf->crypto.len) {
6409 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006410 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006411 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006412 }
6413 else {
6414 struct quic_frame *new_cf;
6415
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006416 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006417 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006418 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006419 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006420 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006421
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006422 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006423 new_cf->type = QUIC_FT_CRYPTO;
6424 new_cf->crypto.len = dlen;
6425 new_cf->crypto.offset = cf->crypto.offset;
6426 new_cf->crypto.qel = qel;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006427 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6428 if (cf->origin) {
6429 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6430 /* This <cf> frame was duplicated */
6431 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6432 new_cf->origin = cf->origin;
6433 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006434 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006435 /* Consume <dlen> bytes of the current frame. */
6436 cf->crypto.len -= dlen;
6437 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006438 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006439 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006440
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006441 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006442 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
6443 struct eb64_node *node = NULL;
6444 struct qc_stream_desc *stream_desc = NULL;
6445 struct quic_stream *strm = &cf->stream;
6446
6447 /* As this frame has been already lost, ensure the stream is always
6448 * available or the range of this frame is not consumed before
6449 * resending it.
6450 */
6451 node = eb64_lookup(&qc->streams_by_id, strm->id);
6452 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006453 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006454 LIST_DELETE(&cf->list);
6455 pool_free(pool_head_quic_frame, cf);
6456 continue;
6457 }
6458
6459 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
6460 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006461 TRACE_DEVEL("ignored frame frame in already acked range",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006462 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006463 LIST_DELETE(&cf->list);
6464 pool_free(pool_head_quic_frame, cf);
6465 continue;
6466 }
6467 else if (strm->offset.key < stream_desc->ack_offset) {
6468 strm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006469 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006470 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006471 }
6472 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006473 /* Note that these frames are accepted in short packets only without
6474 * "Length" packet field. Here, <*len> is used only to compute the
6475 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01006476 *
6477 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006478 * excepting the variable ones. Note that +1 is for the type of this frame.
6479 */
6480 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006481 ((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 +02006482 /* Compute the data length of this STREAM frame. */
6483 avail_room = room - hlen - *len;
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006484 if ((ssize_t)avail_room <= 0)
6485 continue;
6486
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006487 TRACE_DEVEL(" New STREAM frame build (room, len)",
Frédéric Lécailleadc76412022-08-08 16:09:46 +02006488 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006489 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
6490 dlen = max_available_room(avail_room, &dlen_sz);
6491 if (dlen > cf->stream.len) {
6492 dlen = cf->stream.len;
6493 }
6494 dlen_sz = quic_int_getsize(dlen);
6495 flen = hlen + dlen_sz + dlen;
6496 }
6497 else {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02006498 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006499 flen = hlen + dlen;
6500 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006501 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006502 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006503 TRACE_DEVEL(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006504 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006505 /* Add the STREAM data length and its encoded length to the packet
6506 * length and the length of this length.
6507 */
6508 *len += flen;
6509 room -= flen;
6510 if (dlen == cf->stream.len) {
6511 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006512 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006513 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006514
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006515 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006516 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006517 qcc_streams_sent_done(cf->stream.stream->ctx,
6518 cf->stream.len,
6519 cf->stream.offset.key);
6520 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006521 }
6522 else {
6523 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006524 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006525
6526 new_cf = pool_zalloc(pool_head_quic_frame);
6527 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006528 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006529 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006530 }
6531
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006532 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006533 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006534 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006535 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006536 new_cf->stream.id = cf->stream.id;
6537 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
6538 new_cf->stream.offset = cf->stream.offset;
6539 new_cf->stream.len = dlen;
6540 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
6541 /* FIN bit reset */
6542 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
6543 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006544 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6545 if (cf->origin) {
6546 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6547 /* This <cf> frame was duplicated */
6548 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6549 new_cf->origin = cf->origin;
6550 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006551 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006552 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
6553 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006554 cf_buf = b_make(b_orig(cf->stream.buf),
6555 b_size(cf->stream.buf),
6556 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006557 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006558 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006559 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006560
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006561 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006562 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006563 qcc_streams_sent_done(new_cf->stream.stream->ctx,
6564 new_cf->stream.len,
6565 new_cf->stream.offset.key);
6566 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006567 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006568
6569 /* TODO the MUX is notified about the frame sending via
6570 * previous qcc_streams_sent_done call. However, the
6571 * sending can fail later, for example if the sendto
6572 * system call returns an error. As the MUX has been
6573 * notified, the transport layer is responsible to
6574 * bufferize and resent the announced data later.
6575 */
6576
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006577 break;
6578
6579 default:
6580 flen = qc_frm_len(cf);
6581 BUG_ON(!flen);
6582 if (flen > room)
6583 continue;
6584
6585 *len += flen;
6586 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006587 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006588 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006589 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006590 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006591
6592 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01006593 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006594 }
6595
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006596 leave:
6597 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécailleea604992020-12-24 13:01:37 +01006598 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006599}
6600
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006601/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
6602 * is used as return parameter and should be zero'ed by the caller.
6603 */
6604static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
6605 struct quic_frame *out)
6606{
6607 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
6608 *
6609 * A CONNECTION_CLOSE frame should be sent in several packets with
6610 * different encryption levels depending on the client context. This is
6611 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
6612 * more details on how to implement it.
6613 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006614 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
6615
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006616
6617 if (qc->err.app) {
6618 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
6619 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
6620 /* RFC 9000 10.2.3. Immediate Close during the Handshake
6621 *
6622 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
6623 * packet could expose application state or be used to alter application
6624 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
6625 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
6626 * Handshake packets. Otherwise, information about the application
6627 * state might be revealed. Endpoints MUST clear the value of the
6628 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
6629 * converting to a CONNECTION_CLOSE of type 0x1c.
6630 */
6631 out->type = QUIC_FT_CONNECTION_CLOSE;
6632 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
6633 out->connection_close.reason_phrase_len = 0;
6634 }
6635 else {
6636 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
6637 out->connection_close.error_code = qc->err.code;
6638 }
6639 }
6640 else {
6641 out->type = QUIC_FT_CONNECTION_CLOSE;
6642 out->connection_close.error_code = qc->err.code;
6643 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006644 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
6645
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006646}
6647
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006648/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006649 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
6650 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006651 * filling the buffer with as much frames as possible from <frms> list of
6652 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006653 * 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 +02006654 * 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 +02006655 * having returned from this function.
6656 * 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 +01006657 * number field in this packet. <pn_len> will also have the packet number
6658 * length as value.
6659 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006660 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006661 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006662static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
6663 size_t dglen, struct quic_tx_packet *pkt,
6664 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006665 int force_ack, int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006666 struct quic_enc_level *qel, struct quic_conn *qc,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006667 const struct quic_version *ver, struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006668{
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006669 unsigned char *beg, *payload;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006670 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006671 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
6672 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006673 struct quic_frame cc_frm = { };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006674 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006675 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006676 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006677 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006678 struct quic_frame *cf;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006679 int must_ack, ret = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006680 int nb_aepkts_since_last_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006681
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006682 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6683
Frédéric Lécailleea604992020-12-24 13:01:37 +01006684 /* Length field value with CRYPTO frames if present. */
6685 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006686 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006687 /* When not probing, and no immediate close is required, reduce the size of this
6688 * buffer to respect the congestion controller window.
6689 * 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 +01006690 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006691 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006692 size_t path_room;
6693
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006694 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006695 if (end - beg > path_room)
6696 end = beg + path_room;
6697 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006698
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006699 /* Ensure there is enough room for the TLS encryption tag and a zero token
6700 * length field if any.
6701 */
6702 if (end - pos < QUIC_TLS_TAG_LEN +
6703 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
6704 goto no_room;
6705
6706 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006707 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006708 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006709 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006710 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006711 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006712 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006713 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006714 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006715 goto no_room;
6716
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006717 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006718 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006719 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006720 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006721 /* Build an ACK frame if required. */
6722 ack_frm_len = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006723 nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
6724 must_ack = !qel->pktns->tx.pto_probe &&
6725 (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
6726 (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 +02006727 if (must_ack) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006728 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006729 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006730 ack_frm.tx_ack.arngs = arngs;
6731 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6732 qel->pktns->tx.ack_delay =
6733 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6734 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6735 }
6736 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006737 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6738 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6739 * that from here, we do not know if we will have to send a PING frame.
6740 * This will be decided after having computed the ack-eliciting frames
6741 * to be added to this packet.
6742 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006743 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 +01006744 if (!ack_frm_len)
6745 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006746 }
6747
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006748 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006749 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006750 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006751 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006752 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006753
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006754 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006755 /* Initialize the length of the frames built below to <len>.
6756 * If any frame could be successfully built by qc_build_frms(),
6757 * we will have len_frms > len.
6758 */
6759 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006760 if (!qc_build_frms(&frm_list, frms,
6761 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006762 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006763 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006764 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6765 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006766 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006767 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006768
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006769 /* Length (of the remaining data). Must not fail because, the buffer size
6770 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6771 * for the encryption tag. It must be taken into an account for the length
6772 * of this packet.
6773 */
6774 if (len_frms)
6775 len = len_frms + QUIC_TLS_TAG_LEN;
6776 else
6777 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006778 /* CONNECTION_CLOSE frame */
6779 if (cc) {
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006780 qc_build_cc_frm(qc, qel, &cc_frm);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006781 len += qc_frm_len(&cc_frm);
6782 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006783 add_ping_frm = 0;
6784 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006785 len_sz = quic_int_getsize(len);
6786 /* Add this packet size to <dglen> */
6787 dglen += head_len + len_sz + len;
6788 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6789 /* This is a maximum padding size */
6790 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6791 /* The length field value is of this packet is <len> + <padding_len>
6792 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006793 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006794 * sizes from <padding_len>.
6795 */
6796 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6797 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006798 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006799 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006800 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006801 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006802 add_ping_frm = 1;
6803 len += 1;
6804 }
6805 /* 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 +01006806 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006807 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6808 }
6809
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006810 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6811 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006812
6813 /* Packet number field address. */
6814 *buf_pn = pos;
6815
6816 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006817 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6818 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006819
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006820 /* payload building (ack-eliciting or not frames) */
6821 payload = pos;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006822 if (ack_frm_len) {
6823 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6824 goto no_room;
6825
6826 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006827 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006828 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006829
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006830 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006831 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillea8a60432022-08-23 11:42:48 +02006832 struct quic_frame *tmp_cf;
6833 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02006834 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006835 ssize_t room = end - pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006836 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006837 qc, NULL, NULL, &room);
Frédéric Lécaille149c5312022-08-27 15:51:30 +02006838 /* Note that <cf> was added from <frms> to <frm_list> list by
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006839 * qc_build_frms().
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006840 */
6841 LIST_DELETE(&cf->list);
6842 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille149c5312022-08-27 15:51:30 +02006843 continue;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006844 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006845
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006846 quic_tx_packet_refinc(pkt);
6847 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006848 }
6849 }
6850
6851 /* Build a PING frame if needed. */
6852 if (add_ping_frm) {
6853 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006854 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006855 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006856 }
6857
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006858 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006859 if (cc) {
6860 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6861 goto no_room;
6862
6863 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6864 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006865
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006866 /* Build a PADDING frame if needed. */
6867 if (padding_len) {
6868 frm.type = QUIC_FT_PADDING;
6869 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006870 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006871 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006872 }
6873
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006874 if (pos == payload) {
6875 /* No payload was built because of congestion control */
6876 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
6877 goto no_room;
6878 }
6879
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006880 /* If this packet is ack-eliciting and we are probing let's
6881 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006882 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006883 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6884 qel->pktns->tx.pto_probe)
6885 qel->pktns->tx.pto_probe--;
6886
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006887 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006888 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006889
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006890 ret = 1;
6891 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
6892 leave:
6893 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6894 return ret;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006895
6896 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006897 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006898 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006899 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
6900 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006901}
6902
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006903static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006904{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006905 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006906 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006907 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006908 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006909 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006910 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006911 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006912 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006913 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006914 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006915}
6916
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006917/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006918 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6919 * of prebuilt frames.
6920 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006921 * Return -2 if the packet could not be allocated or encrypted for any reason,
6922 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006923 * XXX NOTE XXX
6924 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6925 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6926 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006927 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006928static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6929 const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006930 struct quic_enc_level *qel,
6931 struct quic_tls_ctx *tls_ctx, struct list *frms,
6932 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006933 size_t dglen, int pkt_type, int force_ack,
6934 int padding, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006935{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006936 struct quic_tx_packet *ret_pkt = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006937 /* The pointer to the packet number field. */
6938 unsigned char *buf_pn;
6939 unsigned char *beg, *end, *payload;
6940 int64_t pn;
6941 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006942 struct quic_tx_packet *pkt;
6943
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006944 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006945 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006946 pkt = pool_alloc(pool_head_quic_tx_packet);
6947 if (!pkt) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006948 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006949 *err = -2;
6950 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006951 }
6952
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006953 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006954 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006955 pn_len = 0;
6956 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006957
6958 pn = qel->pktns->tx.next_pn + 1;
6959 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 +02006960 force_ack, padding, cc, probe, qel, qc, ver, frms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006961 // trace already emitted by function above
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006962 *err = -1;
6963 goto err;
6964 }
6965
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006966 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006967 payload = buf_pn + pn_len;
6968 payload_len = end - payload;
6969 aad_len = payload - beg;
6970
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006971 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006972 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006973 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006974 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006975 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006976
6977 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006978 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006979 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006980 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006981 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006982 goto err;
6983 }
6984
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006985 /* Consume a packet number */
6986 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006987 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006988 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006989 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006990 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
6991 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006992 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6993 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006994 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006995 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006996 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006997 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006998 pkt->in_flight_len = pkt->len;
6999 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01007000 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01007001 /* Always reset this flags */
7002 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02007003 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
7004 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
7005 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
7006 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02007007
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007008 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007009
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007010 ret_pkt = pkt;
7011 leave:
7012 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt);
7013 return ret_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007014
7015 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01007016 /* TODO: what about the frames which have been built
7017 * for this packet.
7018 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007019 free_quic_tx_packet(qc, pkt);
7020 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007021}
7022
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007023
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007024/* Called from the upper layer, to subscribe <es> to events <event_type>. The
7025 * event subscriber <es> is not allowed to change from a previous call as long
7026 * as at least one event is still subscribed. The <event_type> must only be a
7027 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
7028 */
7029static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7030{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007031 struct quic_conn *qc = conn->handle.qc;
7032 struct qcc *qcc = qc->qcc;
7033
7034 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007035
7036 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
7037 BUG_ON(qcc->subs && qcc->subs != es);
7038
7039 es->events |= event_type;
7040 qcc->subs = es;
7041
7042 if (event_type & SUB_RETRY_RECV)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007043 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007044
7045 if (event_type & SUB_RETRY_SEND)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007046 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7047
7048 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007049
7050 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007051}
7052
7053/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
7054 * The <es> pointer is not allowed to differ from the one passed to the
7055 * subscribe() call. It always returns zero.
7056 */
7057static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7058{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007059 int ret;
7060 struct quic_conn *qc = conn->handle.qc;
7061 struct qcc *qcc = qc->qcc;
7062
7063 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
7064
7065 if (event_type & SUB_RETRY_RECV)
7066 TRACE_DEVEL("unsubscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
7067 if (event_type & SUB_RETRY_SEND)
7068 TRACE_DEVEL("unsubscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7069
7070 ret = conn_unsubscribe(conn, xprt_ctx, event_type, es);
7071
7072 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
7073
7074 return ret;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007075}
7076
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007077/* Store in <xprt_ctx> the context attached to <conn>.
7078 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007079 */
7080static int qc_conn_init(struct connection *conn, void **xprt_ctx)
7081{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01007082 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007083
7084 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
7085
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007086 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007087 if (*xprt_ctx)
7088 goto out;
7089
Willy Tarreau784b8682022-04-11 14:18:10 +02007090 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007091
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007092 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01007093 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007094
7095 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007096}
7097
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007098/* Start the QUIC transport layer */
7099static int qc_xprt_start(struct connection *conn, void *ctx)
7100{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007101 int ret = 0;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007102 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02007103 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007104
Willy Tarreau784b8682022-04-11 14:18:10 +02007105 qc = conn->handle.qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007106 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007107 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
7108 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02007109 /* prepare a CONNECTION_CLOSE frame */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02007110 quic_set_connection_close(qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007111 goto out;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007112 }
7113
7114 /* mux-quic can now be considered ready. */
7115 qc->mux_state = QC_MUX_READY;
7116
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007117 tasklet_wakeup(qctx->wait_event.tasklet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007118
7119 ret = 1;
7120 out:
7121 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
7122 return ret;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007123}
7124
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007125static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
7126{
Willy Tarreau784b8682022-04-11 14:18:10 +02007127 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007128 return NULL;
7129
Willy Tarreau784b8682022-04-11 14:18:10 +02007130 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007131}
7132
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007133/* transport-layer operations for QUIC connections. */
7134static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02007135 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007136 .subscribe = quic_conn_subscribe,
7137 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007138 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007139 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007140 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
7141 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01007142 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007143 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007144 .name = "QUIC",
7145};
7146
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007147static void __quic_conn_init(void)
7148{
7149 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
7150 xprt_register(XPRT_QUIC, &ssl_quic);
7151}
Willy Tarreau79367f92022-04-25 19:18:24 +02007152INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007153
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007154static void __quic_conn_deinit(void)
7155{
7156 BIO_meth_free(ha_quic_meth);
7157}
Willy Tarreau79367f92022-04-25 19:18:24 +02007158REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007159
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01007160/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
7161 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05007162 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007163 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007164struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007165{
7166 unsigned char *pos;
7167 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007168 struct quic_dghdlr *dghdlr = ctx;
7169 struct quic_dgram *dgram;
7170 int first_pkt = 1;
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007171 struct list *tasklist_head = NULL;
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007172 int max_dgrams = global.tune.maxpollevents;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007173
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007174 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7175
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007176 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007177 pos = dgram->buf;
7178 end = pos + dgram->len;
7179 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007180 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007181
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007182 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007183 if (!pkt) {
7184 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
7185 goto leave;
7186 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007187
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007188 LIST_INIT(&pkt->qc_rx_pkt_list);
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02007189 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007190 quic_rx_packet_refinc(pkt);
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007191 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram, &tasklist_head);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007192 first_pkt = 0;
7193 pos += pkt->len;
7194 quic_rx_packet_refdec(pkt);
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007195
7196 /* Free rejected packets */
7197 if (!pkt->refcnt) {
7198 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
7199 pool_free(pool_head_quic_rx_packet, pkt);
7200 }
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007201 } while (pos < end);
7202
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007203 /* Increasing the received bytes counter by the UDP datagram length
7204 * if this datagram could be associated to a connection.
7205 */
7206 if (dgram->qc)
7207 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01007208
7209 /* Mark this datagram as consumed */
7210 HA_ATOMIC_STORE(&dgram->buf, NULL);
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007211
7212 if (--max_dgrams <= 0)
7213 goto stop_here;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007214 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007215
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007216 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
7217
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007218 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007219
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007220 stop_here:
7221 /* too much work done at once, come back here later */
7222 if (!MT_LIST_ISEMPTY(&dghdlr->dgrams))
7223 tasklet_wakeup((struct tasklet *)t);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007224 leave:
7225 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007226 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007227}
7228
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05007229/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007230 * Returns 1 if succeeded, 0 if not.
7231 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02007232int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
7233 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007234{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007235 int ret = 0, long_header;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007236 size_t minlen, skip;
7237
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007238 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
7239
7240 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
7241 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007242 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007243 }
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007244
7245 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02007246 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
7247 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007248 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Willy Tarreauaf5138f2022-08-05 10:09:32 +02007249 if (end - buf < minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007250 goto err;
7251
7252 buf += skip;
7253 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
7254 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
7255 goto err;
7256
7257 *dcid = buf;
7258
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007259 ret = 1;
7260 leave:
7261 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
7262 return ret;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007263
7264 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007265 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
7266 goto leave;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007267}
7268
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007269/* Notify the MUX layer if alive about an imminent close of <qc>. */
7270void qc_notify_close(struct quic_conn *qc)
7271{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007272 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
7273
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007274 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007275 goto leave;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007276
7277 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007278 /* wake up the MUX */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007279 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
7280 TRACE_STATE("connection closure notidfied to mux",
7281 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007282 qc->conn->mux->wake(qc->conn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007283 }
7284 else
7285 TRACE_STATE("connection closure not notidfied to mux",
7286 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
7287 leave:
7288 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007289}
7290
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007291/*
7292 * Local variables:
7293 * c-indent-level: 8
7294 * c-basic-offset: 8
7295 * End:
7296 */