blob: e4cc35bc9a750134d1a3fa50a2bad8100fb4358c [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;
342 list_for_each_entry(frm, l, list)
343 chunk_frm_appendf(&trace_buf, frm);
344 }
345 }
346
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200347 if (mask & (QUIC_EV_CONN_TXPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100348 const struct quic_tx_packet *pkt = a2;
349 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100350 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100351
352 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100353 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100354 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100355 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100356 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100357 (unsigned long long)qc->path->cwnd,
358 (unsigned long long)qc->path->prep_in_flight,
359 (unsigned long long)qc->path->in_flight,
360 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100361 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100362 }
363 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200364 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100365 if (pkt->pn_node.key != (uint64_t)-1)
366 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100367 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100368 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100369 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
370 (unsigned long long)qc->rx.bytes,
371 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100372 }
373
374 if (room) {
375 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
376 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
377 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100378 }
379 }
380
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100381 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100382 const enum quic_handshake_state *state = a2;
383 const int *err = a3;
384
385 if (state)
386 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
387 if (err)
388 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
389 }
390
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100391 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100392 const struct quic_rx_packet *pkt = a2;
393 const unsigned long *pktlen = a3;
394 const SSL *ssl = a4;
395
396 if (pkt) {
Frédéric Lécaille3dfd4c42022-04-05 15:29:14 +0200397 chunk_appendf(&trace_buf, " pkt@%p", pkt);
398 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
399 chunk_appendf(&trace_buf, " kp=%d",
400 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
401 chunk_appendf(&trace_buf, " el=%c",
402 quic_packet_type_enc_level_char(pkt->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100403 if (pkt->pnl)
404 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
405 (unsigned long long)pkt->pn);
406 if (pkt->token_len)
407 chunk_appendf(&trace_buf, " toklen=%llu",
408 (unsigned long long)pkt->token_len);
409 if (pkt->aad_len)
410 chunk_appendf(&trace_buf, " aadlen=%llu",
411 (unsigned long long)pkt->aad_len);
412 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
413 pkt->flags, (unsigned long long)pkt->len);
414 }
415 if (pktlen)
416 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
417 if (ssl) {
418 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
419 chunk_appendf(&trace_buf, " el=%c",
420 quic_enc_level_char(ssl_to_quic_enc_level(level)));
421 }
422 }
423
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200424 if (mask & (QUIC_EV_CONN_RXPKT|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100425 const struct quic_rx_packet *pkt = a2;
426 const struct quic_rx_crypto_frm *cf = a3;
427 const SSL *ssl = a4;
428
429 if (pkt)
430 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
431 quic_packet_type_enc_level_char(pkt->type),
432 (unsigned long long)pkt->pn);
433 if (cf)
434 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
435 (unsigned long long)cf->offset_node.key,
436 (unsigned long long)cf->len);
437 if (ssl) {
438 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200439 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100440 quic_enc_level_char(ssl_to_quic_enc_level(level)));
441 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100442
Amaury Denoyelle57e6db72022-07-13 15:07:56 +0200443 if (qc->err.code)
444 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err.code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100445 }
446
447 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
448 const struct quic_frame *frm = a2;
449
450 if (frm)
451 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
452 }
453
454 if (mask & QUIC_EV_CONN_PHPKTS) {
455 const struct quic_enc_level *qel = a2;
456
457 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100458 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100459 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100460 " 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 +0100461 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200462 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200463 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100464 (unsigned long long)qc->path->cwnd,
465 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100466 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100467 (unsigned long long)pktns->tx.in_flight,
468 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100469 }
470 }
471
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100472 if (mask & QUIC_EV_CONN_ENCPKT) {
473 const struct enc_debug_info *edi = a2;
474
475 if (edi)
476 chunk_appendf(&trace_buf,
477 " payload=@%p payload_len=%llu"
478 " aad=@%p aad_len=%llu pn=%llu",
479 edi->payload, (unsigned long long)edi->payload_len,
480 edi->aad, (unsigned long long)edi->aad_len,
481 (unsigned long long)edi->pn);
482 }
483
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100484 if (mask & QUIC_EV_CONN_RMHP) {
485 const struct quic_rx_packet *pkt = a2;
486
487 if (pkt) {
488 const int *ret = a3;
489
490 chunk_appendf(&trace_buf, " pkt@%p", pkt);
491 if (ret && *ret)
492 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
493 pkt->pnl, (unsigned long long)pkt->pn);
494 }
495 }
496
497 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200498 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100499 const unsigned long *val1 = a3;
500 const unsigned long *val2 = a4;
501
502 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100503 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100504 if (val1)
505 chunk_appendf(&trace_buf, " %lu", *val1);
506 if (val2)
507 chunk_appendf(&trace_buf, "..%lu", *val2);
508 }
509
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100510 if (mask & QUIC_EV_CONN_ACKSTRM) {
511 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200512 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100513
514 if (s)
515 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200516 if (stream)
517 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100518 }
519
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100520 if (mask & QUIC_EV_CONN_RTTUPDT) {
521 const unsigned int *rtt_sample = a2;
522 const unsigned int *ack_delay = a3;
523 const struct quic_loss *ql = a4;
524
525 if (rtt_sample)
526 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
527 if (ack_delay)
528 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
529 if (ql)
530 chunk_appendf(&trace_buf,
531 " srtt=%ums rttvar=%ums min_rtt=%ums",
532 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
533 }
534 if (mask & QUIC_EV_CONN_CC) {
535 const struct quic_cc_event *ev = a2;
536 const struct quic_cc *cc = a3;
537
538 if (a2)
539 quic_cc_event_trace(&trace_buf, ev);
540 if (a3)
541 quic_cc_state_trace(&trace_buf, cc);
542 }
543
544 if (mask & QUIC_EV_CONN_PKTLOSS) {
545 const struct quic_pktns *pktns = a2;
546 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100547
548 if (pktns) {
549 chunk_appendf(&trace_buf, " pktns=%s",
550 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
551 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
552 if (pktns->tx.loss_time)
553 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100554 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100555 }
556 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
557 struct quic_tx_packet *pkt;
558
559 chunk_appendf(&trace_buf, " lost_pkts:");
560 list_for_each_entry(pkt, lost_pkts, list)
561 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
562 }
563 }
564
565 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100566 const struct quic_pktns *pktns = a2;
567 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100568 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100569
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100570 if (ifae_pkts)
571 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
572 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100573 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100574 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100575 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100576 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
577 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100578 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
579 if (pktns->tx.in_flight)
580 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100581 if (pktns->tx.loss_time)
582 chunk_appendf(&trace_buf, " loss_time=%dms",
583 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
584 }
585 if (mask & QUIC_EV_CONN_SPTO) {
586 if (pktns->tx.time_of_last_eliciting)
587 chunk_appendf(&trace_buf, " tole=%dms",
588 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
589 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100590 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100591 }
592 }
593
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200594 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100595 chunk_appendf(&trace_buf,
596 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
597 }
598 }
599
600 if (mask & QUIC_EV_CONN_SPPKTS) {
601 const struct quic_tx_packet *pkt = a2;
602
Frédéric Lécaille8ecb7362022-08-04 12:00:00 +0200603 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100604 (unsigned long long)qc->path->cwnd,
605 (unsigned long long)qc->path->prep_in_flight,
606 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100607 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100608 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100609 (unsigned long)pkt->pn_node.key,
610 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
611 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100612 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613 }
614 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100615
616 if (mask & QUIC_EV_CONN_SSLALERT) {
617 const uint8_t *alert = a2;
618 const enum ssl_encryption_level_t *level = a3;
619
620 if (alert)
621 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
622 if (level)
623 chunk_appendf(&trace_buf, " el=%c",
624 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
625 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100626
627 if (mask & QUIC_EV_CONN_BCFRMS) {
628 const size_t *sz1 = a2;
629 const size_t *sz2 = a3;
630 const size_t *sz3 = a4;
631
632 if (sz1)
633 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
634 if (sz2)
635 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
636 if (sz3)
637 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
638 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100639
640 if (mask & QUIC_EV_CONN_PSTRM) {
641 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100642
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100643 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100644 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100645 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100646 }
647 if (mask & QUIC_EV_CONN_LPKT) {
648 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200649 const uint64_t *len = a3;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200650 const struct quic_version *ver = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100651
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100652 if (pkt) {
653 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
654 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
655 if (pkt->pn_node.key != (uint64_t)-1)
656 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
657 }
658
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200659 if (len)
660 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200661
662 if (ver)
663 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100664 }
665
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200666 if (mask & QUIC_EV_STATELESS_RST) {
667 const struct quic_cid *cid = a2;
668
669 if (cid)
670 quic_cid_dump(&trace_buf, cid);
671 }
672
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100673}
674
675/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100676static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100677{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200678 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100679
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100680 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100681 return 1;
682
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200683 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
684 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200685 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
686 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200687 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100688 return 1;
689
690 return 0;
691}
692
693/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
694 * both loss detection and PTO and schedule the task assiated to this timer if needed.
695 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100696static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100697{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100698 struct quic_pktns *pktns;
699 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200700 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100701
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100702 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100703 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100704
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100705 pktns = quic_loss_pktns(qc);
706 if (tick_isset(pktns->tx.loss_time)) {
707 qc->timer = pktns->tx.loss_time;
708 goto out;
709 }
710
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100711 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100712 * cancelled for a server which reached the anti-amplification limit.
713 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100714 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200715 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100716 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100717 qc->timer = TICK_ETERNITY;
718 goto out;
719 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100720
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100721 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100722 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100723 /* Timer cancellation. */
724 qc->timer = TICK_ETERNITY;
725 goto out;
726 }
727
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200728 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200729 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100730 if (tick_isset(pto))
731 qc->timer = pto;
732 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100733 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100734 if (tick_is_expired(qc->timer, now_ms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200735 TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100736 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100737 }
738 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200739 TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100740 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100741 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100742 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100743 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100744}
745
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100746/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
747 * connection.
748 * Return 1 if succeeded, 0 if not.
749 */
750static int quic_tls_key_update(struct quic_conn *qc)
751{
752 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
753 struct quic_tls_secrets *rx, *tx;
754 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
755 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200756 const struct quic_version *ver =
757 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200758 int ret = 0;
759
760 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100761
762 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
763 rx = &tls_ctx->rx;
764 tx = &tls_ctx->tx;
765 nxt_rx = &qc->ku.nxt_rx;
766 nxt_tx = &qc->ku.nxt_tx;
767
768 /* Prepare new RX secrets */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200769 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100770 rx->secret, rx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200771 TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
772 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100773 }
774
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200775 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100776 nxt_rx->key, nxt_rx->keylen,
777 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
778 nxt_rx->secret, nxt_rx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200779 TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
780 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100781 }
782
783 /* Prepare new TX secrets */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200784 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100785 tx->secret, tx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200786 TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
787 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100788 }
789
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200790 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100791 nxt_tx->key, nxt_tx->keylen,
792 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
793 nxt_tx->secret, nxt_tx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200794 TRACE_ERROR("New TX key derivation 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écaille8c7927c2022-04-05 16:28:38 +0200798 if (nxt_rx->ctx) {
799 EVP_CIPHER_CTX_free(nxt_rx->ctx);
800 nxt_rx->ctx = NULL;
801 }
802
803 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 +0200804 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
805 goto leave;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200806 }
807
808 if (nxt_tx->ctx) {
809 EVP_CIPHER_CTX_free(nxt_tx->ctx);
810 nxt_tx->ctx = NULL;
811 }
812
813 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 +0200814 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
815 goto leave;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200816 }
817
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200818 ret = 1;
819 leave:
820 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc);
821 return ret;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100822}
823
824/* Rotate the Key Update information for <qc> QUIC connection.
825 * Must be used after having updated them.
826 * Always succeeds.
827 */
828static void quic_tls_rotate_keys(struct quic_conn *qc)
829{
830 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
831 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200832 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100833
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200834 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
835
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100836 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200837 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100838 curr_secret = tls_ctx->rx.secret;
839 curr_iv = tls_ctx->rx.iv;
840 curr_key = tls_ctx->rx.key;
841
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200842 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100843 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
844 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
845 tls_ctx->rx.key = qc->ku.nxt_rx.key;
846
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200847 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100848 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
849 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
850 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
851
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200852 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100853 qc->ku.prv_rx.secret = curr_secret;
854 qc->ku.prv_rx.iv = curr_iv;
855 qc->ku.prv_rx.key = curr_key;
856 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
857
858 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200859 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100860 curr_secret = tls_ctx->tx.secret;
861 curr_iv = tls_ctx->tx.iv;
862 curr_key = tls_ctx->tx.key;
863
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200864 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100865 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
866 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
867 tls_ctx->tx.key = qc->ku.nxt_tx.key;
868
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200869 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100870 qc->ku.nxt_tx.secret = curr_secret;
871 qc->ku.nxt_tx.iv = curr_iv;
872 qc->ku.nxt_tx.key = curr_key;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200873
874 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100875}
876
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200877/* returns 0 on error, 1 on success */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100878int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
879 const uint8_t *read_secret,
880 const uint8_t *write_secret, size_t secret_len)
881{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100882 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
883 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 +0100884 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100885 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200886 const struct quic_version *ver =
887 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200888 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100889
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100890 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100891 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200892 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100893 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100894 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100895 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100896
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100897 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200898 TRACE_ERROR("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
899 goto leave;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100900 }
901
902 rx = &tls_ctx->rx;
903 tx = &tls_ctx->tx;
904
905 rx->aead = tx->aead = tls_aead(cipher);
906 rx->md = tx->md = tls_md(cipher);
907 rx->hp = tx->hp = tls_hp(cipher);
908
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200909 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 +0100910 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100911 read_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200912 TRACE_ERROR("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
913 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100914 }
915
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200916 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200917 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
918 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200919 }
920
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100921 /* Enqueue this connection asap if we could derive O-RTT secrets as
922 * listener. Note that a listener derives only RX secrets for this
923 * level.
924 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200925 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
926 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100927 quic_accept_push_qc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200928 }
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100929
930 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100931 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100932
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200933 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 +0100934 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100935 write_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200936 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
937 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100938 }
939
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200940 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200941 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
942 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200943 }
944
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100945 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100946 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
947 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
948 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
949
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200950 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100951 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
952 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200953 TRACE_ERROR("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
954 goto leave;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100955 }
956
957 memcpy(rx->secret, read_secret, secret_len);
958 rx->secretlen = secret_len;
959 memcpy(tx->secret, write_secret, secret_len);
960 tx->secretlen = secret_len;
961 /* Initialize all the secret keys lengths */
962 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
963 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200964 if (!quic_tls_key_update(qc)) {
965 // trace already emitted by function above
966 goto leave;
967 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100968 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100969
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100970 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100971 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100972 no_secret:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200973 ret = 1;
974 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100975 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200976 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100977}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100978
979/* This function copies the CRYPTO data provided by the TLS stack found at <data>
980 * with <len> as size in CRYPTO buffers dedicated to store the information about
981 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200982 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
983 * buffers to store all the data.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100984 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
985 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200986static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100987 const unsigned char *data, size_t len)
988{
989 struct quic_crypto_buf **qcb;
990 /* The remaining byte to store in CRYPTO buffers. */
991 size_t cf_offset, cf_len, *nb_buf;
992 unsigned char *pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200993 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100994
995 nb_buf = &qel->tx.crypto.nb_buf;
996 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
997 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
998 cf_len = len;
999
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001000 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1001
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001002 while (len) {
1003 size_t to_copy, room;
1004
1005 pos = (*qcb)->data + (*qcb)->sz;
1006 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1007 to_copy = len > room ? room : len;
1008 if (to_copy) {
1009 memcpy(pos, data, to_copy);
1010 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1011 qel->tx.crypto.sz += to_copy;
1012 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001013 len -= to_copy;
1014 data += to_copy;
1015 }
1016 else {
1017 struct quic_crypto_buf **tmp;
1018
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001019 // FIXME: realloc!
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001020 tmp = realloc(qel->tx.crypto.bufs,
1021 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1022 if (tmp) {
1023 qel->tx.crypto.bufs = tmp;
1024 qcb = &qel->tx.crypto.bufs[*nb_buf];
1025 *qcb = pool_alloc(pool_head_quic_crypto_buf);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001026 if (!*qcb) {
1027 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1028 goto leave;
1029 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001030
1031 (*qcb)->sz = 0;
1032 ++*nb_buf;
1033 }
1034 else {
1035 break;
1036 }
1037 }
1038 }
1039
1040 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1041 * have been buffered.
1042 */
1043 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001044 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001045 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001046
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001047 /* There is at most one CRYPTO frame in this packet number
1048 * space. Let's look for it.
1049 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001050 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001051 if (frm->type != QUIC_FT_CRYPTO)
1052 continue;
1053
1054 /* Found */
1055 found = frm;
1056 break;
1057 }
1058
1059 if (found) {
1060 found->crypto.len += cf_len;
1061 }
1062 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001063 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001064 if (!frm) {
1065 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1066 goto leave;
1067 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001068
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001069 LIST_INIT(&frm->reflist);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001070 frm->type = QUIC_FT_CRYPTO;
1071 frm->crypto.offset = cf_offset;
1072 frm->crypto.len = cf_len;
1073 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001074 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001075 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001076 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001077 ret = len == 0;
1078 leave:
1079 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1080 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001081}
1082
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001083/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1084 * activity for <qc> will be interrupted.
1085 */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001086void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001087{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001088 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001089 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001090 goto leave;
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001091
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001092 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001093 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001094 qc->err.code = err.code;
1095 qc->err.app = err.app;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001096 leave:
1097 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001098}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001100/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1101void quic_set_tls_alert(struct quic_conn *qc, int alert)
1102{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001103 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1104
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001105 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1106 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001107 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001108 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1109 }
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001110 quic_set_connection_close(qc, quic_err_tls(alert));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001111 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001112 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1113
1114 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001115}
1116
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001117/* Set the application for <qc> QUIC connection.
1118 * Return 1 if succeeded, 0 if not.
1119 */
1120int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1121{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001122 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1123 qc->app_ops = &h3_ops;
1124 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1125 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001126 else
1127 return 0;
1128
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001129 return 1;
1130}
1131
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001132/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1133 * wants to provide the QUIC layer with CRYPTO data.
1134 * Returns 1 if succeeded, 0 if not.
1135 */
1136int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1137 const uint8_t *data, size_t len)
1138{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001139 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001140 enum quic_tls_enc_level tel;
1141 struct quic_enc_level *qel;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001142 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001143
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001144 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1145 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001146
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001147 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001148 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001149 goto out;
1150 }
1151
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001152 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001153 if (tel == -1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001154 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1155 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001156 }
1157
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001158 qel = &qc->els[tel];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001159 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1160 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1161 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001162 }
1163
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001164 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001165 qc, &level, &len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001166 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001167 ret = 1;
1168 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001169 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001170 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001171}
1172
1173int ha_quic_flush_flight(SSL *ssl)
1174{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001175 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001176
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001177 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1178 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001179
1180 return 1;
1181}
1182
1183int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1184{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001185 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001186
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001187 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1188
1189 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1190
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001191 quic_set_tls_alert(qc, alert);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001192 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001193 return 1;
1194}
1195
1196/* QUIC TLS methods */
1197static SSL_QUIC_METHOD ha_quic_method = {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001198 .set_encryption_secrets = ha_quic_set_encryption_secrets,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001199 .add_handshake_data = ha_quic_add_handshake_data,
1200 .flush_flight = ha_quic_flush_flight,
1201 .send_alert = ha_quic_send_alert,
1202};
1203
1204/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1205 * Returns an error count.
1206 */
1207int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1208{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001209 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1210 int cfgerr = 0;
1211
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001212 long options =
1213 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1214 SSL_OP_SINGLE_ECDH_USE |
1215 SSL_OP_CIPHER_SERVER_PREFERENCE;
1216 SSL_CTX *ctx;
1217
1218 ctx = SSL_CTX_new(TLS_server_method());
1219 bind_conf->initial_ctx = ctx;
1220
1221 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001222 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1223 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1224 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001225
1226#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaillee06f7452022-06-16 15:06:44 +02001227#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001228 if (bind_conf->ssl_conf.early_data) {
1229 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001230 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001231 }
1232 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1233 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1234#else
1235 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1236#endif
1237 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1238#endif
1239 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1240
1241 return cfgerr;
1242}
1243
1244/* Decode an expected packet number from <truncated_on> its truncated value,
1245 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1246 * the number of bits used to encode this packet number (its length in bytes * 8).
1247 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1248 */
1249static uint64_t decode_packet_number(uint64_t largest_pn,
1250 uint32_t truncated_pn, unsigned int pn_nbits)
1251{
1252 uint64_t expected_pn = largest_pn + 1;
1253 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1254 uint64_t pn_hwin = pn_win / 2;
1255 uint64_t pn_mask = pn_win - 1;
1256 uint64_t candidate_pn;
1257
1258
1259 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1260 /* Note that <pn_win> > <pn_hwin>. */
1261 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1262 candidate_pn + pn_hwin <= expected_pn)
1263 return candidate_pn + pn_win;
1264
1265 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1266 return candidate_pn - pn_win;
1267
1268 return candidate_pn;
1269}
1270
1271/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1272 * cryptographic context.
1273 * <largest_pn> is the largest received packet number and <pn> the address of
1274 * the packet number field for this packet with <byte0> address of its first byte.
1275 * <end> points to one byte past the end of this packet.
1276 * Returns 1 if succeeded, 0 if not.
1277 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001278static int qc_do_rm_hp(struct quic_conn *qc,
1279 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001280 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001281{
1282 int ret, outlen, i, pnlen;
1283 uint64_t packet_number;
1284 uint32_t truncated_pn = 0;
1285 unsigned char mask[5] = {0};
1286 unsigned char *sample;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001287 EVP_CIPHER_CTX *cctx = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001288 unsigned char *hp_key;
1289
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001290 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1291
1292 ret = 0;
1293
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001294 /* Check there is enough data in this packet. */
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001295 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001296 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1297 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001298 }
1299
1300 cctx = EVP_CIPHER_CTX_new();
1301 if (!cctx) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001302 TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
1303 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001304 }
1305
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001306 sample = pn + QUIC_PACKET_PN_MAXLEN;
1307
1308 hp_key = tls_ctx->rx.hp_key;
1309 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1310 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1311 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001312 TRACE_ERROR("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
1313 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001314 }
1315
1316 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1317 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1318 for (i = 0; i < pnlen; i++) {
1319 pn[i] ^= mask[i + 1];
1320 truncated_pn = (truncated_pn << 8) | pn[i];
1321 }
1322
1323 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1324 /* Store remaining information for this unprotected header */
1325 pkt->pn = packet_number;
1326 pkt->pnl = pnlen;
1327
1328 ret = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001329 leave:
1330 if (cctx)
1331 EVP_CIPHER_CTX_free(cctx);
1332 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001333 return ret;
1334}
1335
1336/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1337 * address, with <payload_len> as payload length, <aad> as address of
1338 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1339 * context.
1340 * Returns 1 if succeeded, 0 if not.
1341 */
1342static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1343 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001344 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001345{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001346 int ret = 0;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001347 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001348 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001349 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001350 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001351
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001352 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
1353
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001354 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001355 TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001356 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001357 }
1358
1359 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001360 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001361 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001362 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001363 }
1364
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001365 ret = 1;
1366 leave:
1367 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
1368 return ret;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001369
1370 err:
1371 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001372 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001373}
1374
1375/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1376 * Returns 1 if succeeded, 0 if not.
1377 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001378static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel,
1379 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001380{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001381 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001382 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001383 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001384 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001385 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001386 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1387 unsigned char *rx_key = tls_ctx->rx.key;
1388
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001389 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1390
1391 ret = 0;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001392 kp_changed = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001393
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001394 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1395 /* The two tested bits are not at the same position,
1396 * this is why they are first both inversed.
1397 */
1398 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1399 if (pkt->pn < tls_ctx->rx.pn) {
1400 /* The lowest packet number of a previous key phase
1401 * cannot be null if it really stores previous key phase
1402 * secrets.
1403 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001404 // TODO: check if BUG_ON() more suitable
1405 if (!pkt->qc->ku.prv_rx.pn) {
1406 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1407 goto leave;
1408 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001409
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001410 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1411 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001412 rx_key = pkt->qc->ku.prv_rx.key;
1413 }
1414 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1415 /* Next key phase */
1416 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001417 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1418 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001419 rx_key = pkt->qc->ku.nxt_rx.key;
1420 }
1421 }
1422 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001423
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001424 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) {
1425 TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc);
1426 goto leave;
1427 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001428
1429 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1430 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001431 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001432 if (!ret) {
1433 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1434 goto leave;
1435 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001436
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001437 /* Update the keys only if the packet decryption succeeded. */
1438 if (kp_changed) {
1439 quic_tls_rotate_keys(pkt->qc);
1440 /* Toggle the Key Phase bit */
1441 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1442 /* Store the lowest packet number received for the current key phase */
1443 tls_ctx->rx.pn = pkt->pn;
1444 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001445 if (!quic_tls_key_update(pkt->qc)) {
1446 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1447 goto leave;
1448 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001449 }
1450
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001451 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001452 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001453 ret = 1;
1454 leave:
1455 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille11a6f402022-08-16 14:48:59 +02001456 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001457}
1458
Frédéric Lécaille96367152022-04-25 09:40:19 +02001459
1460/* Remove references to <frm> frame */
1461static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1462{
1463 uint64_t pn;
1464 struct quic_frame *f, *tmp;
1465
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001466 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1467
Frédéric Lécaille96367152022-04-25 09:40:19 +02001468 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
1469 pn = f->pkt->pn_node.key;
1470 f->origin = NULL;
1471 LIST_DELETE(&f->ref);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001472 TRACE_DEVEL("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001473 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001474
1475 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001476}
1477
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001478/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001479void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001480{
1481 uint64_t pn;
1482 struct quic_frame *origin, *f, *tmp;
1483
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001484 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1485
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001486 /* Identify this frame: a frame copy or one of its copies */
1487 origin = frm->origin ? frm->origin : frm;
1488 /* Ensure the source of the copies is flagged as acked, <frm> being
1489 * possibly a copy of <origin>
1490 */
1491 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1492 /* Mark all the copy of <origin> as acknowledged. We must
1493 * not release the packets (releasing the frames) at this time as
1494 * they are possibly also to be acknowledged alongside the
1495 * the current one.
1496 */
1497 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1498 pn = f->pkt->pn_node.key;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001499 TRACE_DEVEL("mark frame as acked from packet",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001500 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1501 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1502 f->origin = NULL;
1503 LIST_DELETE(&f->ref);
1504 }
1505 LIST_DELETE(&frm->list);
1506 pn = frm->pkt->pn_node.key;
1507 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001508 TRACE_DEVEL("freeing frame from packet",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001509 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1510 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001511
1512 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001513}
1514
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001515/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1516 * and all STREAM data are acknowledged. The MUX is responsible to have set
1517 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1518 *
1519 * TODO this should also be called on lost packet detection
1520 */
1521static void qc_check_close_on_released_mux(struct quic_conn *qc)
1522{
1523 struct ssl_sock_ctx *ctx = qc->xprt_ctx;
1524
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001525 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1526
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001527 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1528 /* Reuse errcode which should have been previously set by the MUX on release. */
1529 quic_set_connection_close(qc, qc->err);
1530 tasklet_wakeup(ctx->wait_event.tasklet);
1531 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001532
1533 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001534}
1535
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001536/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001537 *
1538 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001539 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001540static int quic_stream_try_to_consume(struct quic_conn *qc,
1541 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001542{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001543 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001544 struct eb64_node *frm_node;
1545
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001546 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1547
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001548 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001549 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001550 while (frm_node) {
1551 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001552 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001553 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001554
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001555 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001556 offset = strm->offset.key;
1557 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001558
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001559 if (offset > stream->ack_offset)
1560 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001561
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001562 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001563 /* cf. next comment : frame may be freed at this stage. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001564 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001565 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001566 ret = 1;
1567 }
1568
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001569 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1570 * has been freed. with the stream frames tree. Nothing to do
1571 * anymore in here.
1572 */
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001573 if (!stream) {
1574 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001575 ret = 1;
1576 goto leave;
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001577 }
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001578
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001579 frm_node = eb64_next(frm_node);
1580 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001581
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001582 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001583 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001584 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001585
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001586 leave:
1587 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001588 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001589}
1590
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001591/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001592static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1593 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001594{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001595 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001596
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001597 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm);
1598
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001599 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001600 switch (frm->type) {
1601 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1602 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001603 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001604 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001605 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001606 const size_t offset = strm_frm->offset.key;
1607 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001608
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001609 /* do not use strm_frm->stream as the qc_stream_desc instance
1610 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001611 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001612 *
1613 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001614 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001615 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001616 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1617 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001618 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001619 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001620 /* early return */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001621 goto leave;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001622 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001623 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001624
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001625 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001626 if (offset <= stream->ack_offset) {
1627 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001628 stream_acked = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001629 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001630 qc, strm_frm, stream);
1631 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001632
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001633 if (!stream) {
1634 /* no need to continue if stream freed. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001635 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001636 qc_release_frm(qc, frm);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001637 qc_check_close_on_released_mux(qc);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001638 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001639 }
1640
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001641 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Frédéric Lécailleda342552022-04-25 10:28:49 +02001642 qc, strm_frm, stream);
1643 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001644 }
1645 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001646 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001647 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001648
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001649 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001650 }
1651 break;
1652 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001653 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001654 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001655
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001656 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001657 struct qcc *qcc = qc->qcc;
1658
1659 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1660 tasklet_wakeup(qcc->subs->tasklet);
1661 qcc->subs->events &= ~SUB_RETRY_SEND;
1662 if (!qcc->subs->events)
1663 qcc->subs = NULL;
1664 }
1665 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001666 leave:
1667 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001668}
1669
1670/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1671 * deallocating them, and their TX frames.
1672 * Returns the last node reached to be used for the next range.
1673 * May be NULL if <largest> node could not be found.
1674 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001675static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1676 struct eb_root *pkts,
1677 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001678 struct list *newly_acked_pkts,
1679 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001680 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001681{
1682 struct eb64_node *node;
1683 struct quic_tx_packet *pkt;
1684
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001685 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1686
Frédéric Lécaille843399f2022-07-22 16:27:44 +02001687 node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001688 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001689 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001690
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001691 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001692 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001693 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001694 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001695 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001696 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001697 node = eb64_prev(node);
1698 eb64_delete(&pkt->pn_node);
1699 }
1700
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001701 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001702 return node;
1703}
1704
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001705/* Remove all frames from <pkt_frm_list> and reinsert them in the
1706 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001707 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001708static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001709 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001710 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001711{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001712 struct quic_frame *frm, *frmbak;
1713 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001714 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001715
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001716 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1717
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001718 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001719 /* Only for debug */
1720 uint64_t pn;
1721
1722 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001723 LIST_DELETE(&frm->list);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001724 pn = frm->pkt->pn_node.key;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001725 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001726 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001727 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001728 /* Remove any reference to this frame */
1729 qc_frm_unref(qc, frm);
1730 switch (frm->type) {
1731 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1732 {
1733 struct quic_stream *strm_frm = &frm->stream;
1734 struct eb64_node *node = NULL;
1735 struct qc_stream_desc *stream_desc;
1736
1737 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1738 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001739 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1740 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001741 qc, frm, &pn);
1742 pool_free(pool_head_quic_frame, frm);
1743 continue;
1744 }
1745
1746 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1747 /* Do not resend this frame if in the "already acked range" */
1748 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001749 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001750 QUIC_EV_CONN_PRSAFRM, qc, frm);
1751 continue;
1752 }
1753 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1754 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001755 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001756 QUIC_EV_CONN_PRSAFRM, qc, frm);
1757 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001758 break;
1759 }
1760
1761 default:
1762 break;
1763 }
1764
1765 /* Do not resend probing packet with old data */
1766 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001767 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1768 qc, frm, &pn);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001769 if (frm->origin)
1770 LIST_DELETE(&frm->ref);
1771 pool_free(pool_head_quic_frame, frm);
1772 continue;
1773 }
1774
1775 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001776 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1777 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001778 qc, frm, &pn);
1779 pool_free(pool_head_quic_frame, frm);
1780 }
1781 else {
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001782 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1783 /* Mark this STREAM frame as lost. A look up their stream descriptor
1784 * will be performed to check the stream is not consumed or released.
1785 */
Amaury Denoyellef372e742022-08-16 14:41:57 +02001786 frm->flags |= QUIC_FL_TX_FRAME_LOST;
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001787 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001788 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001789 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001790 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001791 }
1792
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001793 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001794
1795 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001796}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001797
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001798/* Free <pkt> TX packet and its attached frames.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001799 * This is the responsibility of the caller to remove this packet of
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001800 * any data structure it was possibly attached to.
1801 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001802static inline void free_quic_tx_packet(struct quic_conn *qc,
1803 struct quic_tx_packet *pkt)
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001804{
1805 struct quic_frame *frm, *frmbak;
1806
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001807 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1808
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001809 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001810 goto leave;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001811
1812 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1813 LIST_DELETE(&frm->list);
1814 pool_free(pool_head_quic_frame, frm);
1815 }
1816 pool_free(pool_head_quic_tx_packet, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001817
1818 leave:
1819 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001820}
1821
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001822/* Free the TX packets of <pkts> list */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001823static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001824{
1825 struct quic_tx_packet *pkt, *tmp;
1826
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001827 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1828
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001829 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001830 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001831 eb64_delete(&pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001832 free_quic_tx_packet(qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001833 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001834
1835 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001836}
1837
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001838/* Remove already sent ranges of acknowledged packet numbers from
1839 * <pktns> packet number space tree below <largest_acked_pn> possibly
1840 * updating the range which contains <largest_acked_pn>.
1841 * Never fails.
1842 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001843static void qc_treat_ack_of_ack(struct quic_conn *qc,
1844 struct quic_pktns *pktns,
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001845 int64_t largest_acked_pn)
1846{
1847 struct eb64_node *ar, *next_ar;
1848 struct quic_arngs *arngs = &pktns->rx.arngs;
1849
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001850 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1851
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001852 ar = eb64_first(&arngs->root);
1853 while (ar) {
1854 struct quic_arng_node *ar_node;
1855
1856 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001857 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001858
1859 if ((int64_t)ar_node->first.key > largest_acked_pn) {
1860 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001861 break;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001862 }
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001863
1864 if (largest_acked_pn < ar_node->last) {
1865 eb64_delete(ar);
1866 ar_node->first.key = largest_acked_pn + 1;
1867 eb64_insert(&arngs->root, ar);
1868 break;
1869 }
1870
1871 eb64_delete(ar);
1872 pool_free(pool_head_quic_arng, ar_node);
1873 arngs->sz--;
1874 ar = next_ar;
1875 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001876
1877 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001878}
1879
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001880/* Send a packet ack event nofication for each newly acked packet of
1881 * <newly_acked_pkts> list and free them.
1882 * Always succeeds.
1883 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001884static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001885 struct list *newly_acked_pkts)
1886{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001887 struct quic_tx_packet *pkt, *tmp;
1888 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1889
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001890 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1891
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001892 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1893 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001894 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001895 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001896 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001897 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001898 /* If this packet contained an ACK frame, proceed to the
1899 * acknowledging of range of acks from the largest acknowledged
1900 * packet number which was sent in an ACK frame by this packet.
1901 */
1902 if (pkt->largest_acked_pn != -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001903 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001904 ev.ack.acked = pkt->in_flight_len;
1905 ev.ack.time_sent = pkt->time_sent;
1906 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001907 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001908 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001909 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001910 }
1911
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001912 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1913
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001914}
1915
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001916/* Release all the frames attached to <pktns> packet number space */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001917static inline void qc_release_pktns_frms(struct quic_conn *qc,
1918 struct quic_pktns *pktns)
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001919{
1920 struct quic_frame *frm, *frmbak;
1921
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001922 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
1923
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001924 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1925 LIST_DELETE(&frm->list);
1926 pool_free(pool_head_quic_frame, frm);
1927 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001928
1929 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001930}
1931
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001932/* Handle <pkts> list of lost packets detected at <now_us> handling
1933 * their TX frames.
1934 * Send a packet loss event to the congestion controller if
1935 * in flight packet have been lost.
1936 * Also frees the packet in <pkts> list.
1937 * Never fails.
1938 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001939static inline void qc_release_lost_pkts(struct quic_conn *qc,
1940 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001941 struct list *pkts,
1942 uint64_t now_us)
1943{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001945 uint64_t lost_bytes;
1946
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001947 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1948
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001949 lost_bytes = 0;
1950 oldest_lost = newest_lost = NULL;
1951 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001952 struct list tmp = LIST_HEAD_INIT(tmp);
1953
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001954 lost_bytes += pkt->in_flight_len;
1955 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001956 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001957 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001958 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001959 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001960 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001961 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001962 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001963 if (!oldest_lost) {
1964 oldest_lost = newest_lost = pkt;
1965 }
1966 else {
1967 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001968 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001969 newest_lost = pkt;
1970 }
1971 }
1972
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001973 if (newest_lost) {
1974 /* Sent a congestion event to the controller */
Benoit DOLEZ69e3f052022-06-08 09:28:56 +02001975 struct quic_cc_event ev = { };
1976
1977 ev.type = QUIC_CC_EVT_LOSS;
1978 ev.loss.time_sent = newest_lost->time_sent;
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001979
1980 quic_cc_event(&qc->path->cc, &ev);
1981 }
1982
1983 /* If an RTT have been already sampled, <rtt_min> has been set.
1984 * We must check if we are experiencing a persistent congestion.
1985 * If this is the case, the congestion controller must re-enter
1986 * slow start state.
1987 */
1988 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1989 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1990
1991 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1992 now_ms, qc->max_ack_delay))
1993 qc->path->cc.algo->slow_start(&qc->path->cc);
1994 }
1995
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001996 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001997 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001998 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001999 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002000 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002001
2002 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002003}
2004
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002005/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2006 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002007 * 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 +01002008 * acked ack-eliciting packet.
2009 * Return 1, if succeeded, 0 if not.
2010 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002011static inline int qc_parse_ack_frm(struct quic_conn *qc,
2012 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002013 struct quic_enc_level *qel,
2014 unsigned int *rtt_sample,
2015 const unsigned char **pos, const unsigned char *end)
2016{
2017 struct quic_ack *ack = &frm->ack;
2018 uint64_t smallest, largest;
2019 struct eb_root *pkts;
2020 struct eb64_node *largest_node;
2021 unsigned int time_sent, pkt_flags;
2022 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2023 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002024 int ret = 0;
2025
2026 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002027
2028 if (ack->largest_ack > qel->pktns->tx.next_pn) {
2029 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002030 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002031 goto err;
2032 }
2033
2034 if (ack->first_ack_range > ack->largest_ack) {
2035 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002036 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002037 goto err;
2038 }
2039
2040 largest = ack->largest_ack;
2041 smallest = largest - ack->first_ack_range;
2042 pkts = &qel->pktns->tx.pkts;
2043 pkt_flags = 0;
2044 largest_node = NULL;
2045 time_sent = 0;
2046
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002047 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002048 largest_node = eb64_lookup(pkts, largest);
2049 if (!largest_node) {
2050 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002051 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002052 }
2053 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002054 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002055 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002056 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002057 }
2058
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002059 TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002060 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002061 do {
2062 uint64_t gap, ack_range;
2063
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002064 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2065 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002066 if (!ack->ack_range_num--)
2067 break;
2068
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002069 if (!quic_dec_int(&gap, pos, end)) {
2070 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002071 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002072 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002073
2074 if (smallest < gap + 2) {
2075 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002076 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002077 goto err;
2078 }
2079
2080 largest = smallest - gap - 2;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002081 if (!quic_dec_int(&ack_range, pos, end)) {
2082 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002083 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002084 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002085
2086 if (largest < ack_range) {
2087 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002088 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002089 goto err;
2090 }
2091
2092 /* Do not use this node anymore. */
2093 largest_node = NULL;
2094 /* Next range */
2095 smallest = largest - ack_range;
2096
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002097 TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002098 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002099 } while (1);
2100
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002101 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2102 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002103 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002104 }
2105
2106 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2107 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002108 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002109 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002110 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002111 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002112 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2113 if (quic_peer_validated_addr(qc))
2114 qc->path->loss.pto_count = 0;
2115 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002116 }
2117
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002118 ret = 1;
2119 leave:
2120 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2121 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002122
2123 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002124 free_quic_tx_pkts(qc, &newly_acked_pkts);
2125 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002126}
2127
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002128/* This function gives the detail of the SSL error. It is used only
2129 * if the debug mode and the verbose mode are activated. It dump all
2130 * the SSL error until the stack was empty.
2131 */
2132static forceinline void qc_ssl_dump_errors(struct connection *conn)
2133{
2134 if (unlikely(global.mode & MODE_DEBUG)) {
2135 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002136 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002137 unsigned long ret;
2138
Willy Tarreau325fc632022-04-11 18:47:38 +02002139 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002140 ret = ERR_get_error();
2141 if (!ret)
2142 return;
2143
2144 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002145 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002146 }
2147 }
2148}
2149
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002150int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2151 const char **str, int *len);
2152
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002153/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2154 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002155 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002156 * Return 1 if succeeded, 0 if not.
2157 */
2158static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002159 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002160 const unsigned char *data, size_t len,
2161 struct quic_rx_packet *pkt,
2162 struct quic_rx_crypto_frm *cf)
2163{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002164 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002165 struct quic_conn *qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002166 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002167
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002168 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002169 qc = ctx->qc;
2170
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002171 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2172
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002173 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002174 TRACE_ERROR("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002175 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002176 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002177 }
2178
2179 el->rx.crypto.offset += len;
2180 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002181 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002182
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002183 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002184 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002185 ssl_err = SSL_do_handshake(ctx->ssl);
2186 if (ssl_err != 1) {
2187 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2188 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002189 TRACE_PROTO("SSL handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002190 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002191 goto out;
2192 }
2193
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002194 /* TODO: Should close the connection asap */
2195 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2196 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2197 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2198 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2199 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002200 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002201 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002202 ERR_clear_error();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002203 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002204 }
2205
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002206 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002207
2208 /* Check the alpn could be negotiated */
2209 if (!qc->app_ops) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002210 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002211 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002212 goto leave;
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002213 }
2214
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002215 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002216 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002217 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2218 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2219 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002220 /* I/O callback switch */
2221 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002222 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002223 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002224 /* The connection is ready to be accepted. */
2225 quic_accept_push_qc(qc);
2226 }
2227 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002228 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002229 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002230 } else {
2231 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2232 if (ssl_err != 1) {
2233 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2234 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002235 TRACE_PROTO("SSL post handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002236 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002237 goto out;
2238 }
2239
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002240 TRACE_ERROR("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002241 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002242 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002243 }
2244
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002245 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002246 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002247
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002248 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002249 ret = 1;
2250 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002251 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002252 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002253}
2254
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002255/* Parse a STREAM frame <strm_frm>
2256 *
2257 * Return 1 on success. On error, 0 is returned. In this case, the packet
2258 * containing the frame must not be acknowledged.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002259 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002260static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2261 struct quic_stream *strm_frm,
2262 struct quic_conn *qc)
2263{
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002264 int ret;
2265
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002266 /* RFC9000 13.1. Packet Processing
2267 *
2268 * A packet MUST NOT be acknowledged until packet protection has been
2269 * successfully removed and all frames contained in the packet have
2270 * been processed. For STREAM frames, this means the data has been
2271 * enqueued in preparation to be received by the application protocol,
2272 * but it does not require that data be delivered and consumed.
2273 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002274 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2275
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002276 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2277 strm_frm->offset.key, strm_frm->fin,
2278 (char *)strm_frm->data);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002279
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002280 /* frame rejected - packet must not be acknowledeged */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002281 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2282 return !ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002283}
2284
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002285/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2286 * for <qc> QUIC connection.
2287 * This is a best effort function which never fails even if no memory could be
2288 * allocated to duplicate these frames.
2289 */
2290static void qc_dup_pkt_frms(struct quic_conn *qc,
2291 struct list *pkt_frm_list, struct list *out_frm_list)
2292{
2293 struct quic_frame *frm, *frmbak;
2294 struct list tmp = LIST_HEAD_INIT(tmp);
2295
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002296 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2297
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002298 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2299 struct quic_frame *dup_frm, *origin;
2300
2301 switch (frm->type) {
2302 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2303 {
2304 struct quic_stream *strm_frm = &frm->stream;
2305 struct eb64_node *node = NULL;
2306 struct qc_stream_desc *stream_desc;
2307
2308 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2309 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002310 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002311 continue;
2312 }
2313
2314 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2315 /* Do not resend this frame if in the "already acked range" */
2316 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002317 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002318 QUIC_EV_CONN_PRSAFRM, qc, frm);
2319 continue;
2320 }
2321 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2322 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002323 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002324 QUIC_EV_CONN_PRSAFRM, qc, frm);
2325 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002326 break;
2327 }
2328
2329 default:
2330 break;
2331 }
2332
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002333 dup_frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002334 if (!dup_frm) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002335 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002336 break;
2337 }
2338
2339 /* If <frm> is already a copy of another frame, we must take
2340 * its original frame as source for the copy.
2341 */
2342 origin = frm->origin ? frm->origin : frm;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002343 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002344 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002345 qc, NULL, &origin->pkt->pn_node.key);
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002346 *dup_frm = *origin;
2347 dup_frm->pkt = NULL;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002348 dup_frm->origin = origin;
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002349 dup_frm->flags = 0;
2350 LIST_INIT(&dup_frm->reflist);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002351 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2352 LIST_APPEND(&tmp, &dup_frm->list);
2353 }
2354
2355 LIST_SPLICE(out_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002356
2357 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002358}
2359
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002360/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002361static void qc_prep_fast_retrans(struct quic_conn *qc,
2362 struct quic_enc_level *qel,
2363 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002364{
2365 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002366 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002367 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002368 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002369
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002370 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2371
2372 BUG_ON(frms1 == frms2);
2373
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002374 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002375 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002376 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002377 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002378 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2379 node = eb64_next(node);
2380 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002381 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002382 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002383 }
2384
2385 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002386 goto leave;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002387
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002388 /* When building a packet from another one, the field which may increase the
2389 * packet size is the packet number. And the maximum increase is 4 bytes.
2390 */
2391 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2392 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2393 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002394 goto leave;
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002395 }
2396
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002397 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002398 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2399 if (frms == frms1 && frms2) {
2400 frms = frms2;
2401 goto start;
2402 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002403 leave:
2404 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002405}
2406
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002407/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002408 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002409 * Initial packets send them coalescing with others (Handshake here).
2410 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002411 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002412static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2413 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002414{
2415 struct list itmp = LIST_HEAD_INIT(itmp);
2416 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002417
2418 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2419 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2420 struct quic_enc_level *qel = iqel;
2421 struct eb_root *pkts;
2422 struct eb64_node *node;
2423 struct quic_tx_packet *pkt;
2424 struct list *tmp = &itmp;
2425
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002426 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002427 start:
2428 pkt = NULL;
2429 pkts = &qel->pktns->tx.pkts;
2430 node = eb64_first(pkts);
2431 /* Skip the empty packet (they have already been retransmitted) */
2432 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002433 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002434 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002435 break;
2436 node = eb64_next(node);
2437 }
2438
2439 if (!pkt)
2440 goto end;
2441
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002442 /* When building a packet from another one, the field which may increase the
2443 * packet size is the packet number. And the maximum increase is 4 bytes.
2444 */
2445 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2446 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2447 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2448 goto end;
2449 }
2450
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002451 qel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002452
2453 /* No risk to loop here, #packet per datagram is bounded */
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002454 requeue:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002455 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002456 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002457 if (qel == iqel) {
2458 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2459 pkt = pkt->next;
2460 tmp = &htmp;
2461 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002462 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002463 goto requeue;
2464 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002465 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002466
2467 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002468 LIST_SPLICE(ifrms, &itmp);
2469 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002470
2471 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002472}
2473
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002474static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002475{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002476 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2477
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002478 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2479 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2480 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2481 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002482 goto out;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002483
2484 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2485 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002486
2487 out:
2488 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002489}
2490
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002491/* Enqueue a STOP_SENDING frame to send into 1RTT packet number space
2492 * frame list to send.
2493 * Return 1 if succeeded, 0 if not.
2494 */
2495static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id)
2496{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002497 int ret = 0;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002498 struct quic_frame *frm;
2499 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2500 uint64_t app_error_code;
2501
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002502 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2503
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002504 /* TODO: the mux may be released, we cannot have more
2505 * information about the application error code to send
2506 * at this time.
2507 */
2508 app_error_code = H3_REQUEST_REJECTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002509 // fixme: zalloc
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002510 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002511 if (!frm) {
2512 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2513 goto out;
2514 }
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002515
2516 frm->type = QUIC_FT_STOP_SENDING;
2517 frm->stop_sending.id = id;
2518 frm->stop_sending.app_error_code = app_error_code;
2519 LIST_INIT(&frm->reflist);
2520 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002521 ret = 1;
2522 out:
2523 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2524 return ret;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002525}
2526
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002527/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2528 * as I/O handler context and <qel> as encryption level.
2529 * Returns 1 if succeeded, 0 if failed.
2530 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002531static 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 +01002532 struct quic_enc_level *qel)
2533{
2534 struct quic_frame frm;
2535 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002536 struct quic_conn *qc = ctx->qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002537 int fast_retrans = 0, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002538
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002539 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002540 /* Skip the AAD */
2541 pos = pkt->data + pkt->aad_len;
2542 end = pkt->data + pkt->len;
2543
2544 while (pos < end) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002545 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
2546 // trace already emitted by function above
2547 goto leave;
2548 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002549
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002550 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002551 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002552 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002553 break;
2554 case QUIC_FT_PING:
2555 break;
2556 case QUIC_FT_ACK:
2557 {
2558 unsigned int rtt_sample;
2559
2560 rtt_sample = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002561 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
2562 // trace already emitted by function above
2563 goto leave;
2564 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002565
2566 if (rtt_sample) {
2567 unsigned int ack_delay;
2568
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002569 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002570 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002571 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2572 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002573 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002574 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002575 break;
2576 }
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002577 case QUIC_FT_RESET_STREAM:
2578 /* TODO: handle this frame at STREAM level */
2579 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002580 case QUIC_FT_STOP_SENDING:
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002581 {
2582 struct quic_stop_sending *stop_sending = &frm.stop_sending;
2583 if (qc->mux_state == QC_MUX_READY) {
2584 if (qcc_recv_stop_sending(qc->qcc, stop_sending->id,
2585 stop_sending->app_error_code)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002586 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
2587 goto leave;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002588 }
2589 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002590 break;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002591 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002592 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002593 {
2594 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002595
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002596 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002597 /* XXX TO DO: <cfdebug> is used only for the traces. */
2598 struct quic_rx_crypto_frm cfdebug = { };
2599
2600 cfdebug.offset_node.key = frm.crypto.offset;
2601 cfdebug.len = frm.crypto.len;
2602 TRACE_PROTO("CRYPTO data discarded",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002603 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002604 break;
2605 }
2606
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002607 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2608 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2609 /* XXX TO DO: <cfdebug> is used only for the traces. */
2610 struct quic_rx_crypto_frm cfdebug = { };
2611
2612 cfdebug.offset_node.key = frm.crypto.offset;
2613 cfdebug.len = frm.crypto.len;
2614 /* Nothing to do */
2615 TRACE_PROTO("Already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002616 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002617 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002618 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2619 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002620 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002621 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002622 else {
2623 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2624 /* XXX TO DO: <cfdebug> is used only for the traces. */
2625 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002626
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002627 cfdebug.offset_node.key = frm.crypto.offset;
2628 cfdebug.len = frm.crypto.len;
2629 TRACE_PROTO("Partially already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002630 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002631 frm.crypto.len -= diff;
2632 frm.crypto.data += diff;
2633 frm.crypto.offset = qel->rx.crypto.offset;
2634 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002635 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002636
2637 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002638 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002639 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002640
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002641 cfdebug.offset_node.key = frm.crypto.offset;
2642 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002643 if (!qc_provide_cdata(qel, ctx,
2644 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002645 pkt, &cfdebug)) {
2646 // trace already emitted by function above
2647 goto leave;
2648 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002649
2650 break;
2651 }
2652
2653 /* frm.crypto.offset > qel->rx.crypto.offset */
2654 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2655 if (!cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002656 TRACE_ERROR("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002657 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002658 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002659 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002660
2661 cf->offset_node.key = frm.crypto.offset;
2662 cf->len = frm.crypto.len;
2663 cf->data = frm.crypto.data;
2664 cf->pkt = pkt;
2665 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2666 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002667 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002668 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002669 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002670 {
2671 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002672 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002673
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002674 /* The upper layer may not be allocated. */
2675 if (qc->mux_state != QC_MUX_READY) {
2676 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002677 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002678 break;
2679 }
2680 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002681 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002682 if (!qc_stop_sending_frm_enqueue(qc, stream->id))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002683 TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc);
2684 /* This packet will not be acknowledged */
2685 goto leave;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002686 }
2687 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002688
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002689 if (!qc_handle_strm_frm(pkt, stream, qc)) {
2690 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
2691 goto leave;
2692 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002693
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002694 break;
2695 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002696 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002697 if (qc->mux_state == QC_MUX_READY) {
2698 struct quic_max_data *data = &frm.max_data;
2699 qcc_recv_max_data(qc->qcc, data->max_data);
2700 }
2701 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002702 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002703 if (qc->mux_state == QC_MUX_READY) {
2704 struct quic_max_stream_data *data = &frm.max_stream_data;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002705 if (qcc_recv_max_stream_data(qc->qcc, data->id,
2706 data->max_stream_data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002707 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
2708 goto leave;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002709 }
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002710 }
2711 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002712 case QUIC_FT_MAX_STREAMS_BIDI:
2713 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002714 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002715 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002716 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2717 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002718 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002719 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2720 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002721 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002722 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2723 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002724 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002725 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002726 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002727 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002728 case QUIC_FT_RETIRE_CONNECTION_ID:
2729 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002730 break;
2731 case QUIC_FT_CONNECTION_CLOSE:
2732 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002733 /* Increment the error counters */
2734 qc_cc_err_count_inc(qc, &frm);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002735 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002736 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2737 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002738 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002739 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002740 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002741 /* RFC 9000 10.2. Immediate Close:
2742 * The closing and draining connection states exist to ensure
2743 * that connections close cleanly and that delayed or reordered
2744 * packets are properly discarded. These states SHOULD persist
2745 * for at least three times the current PTO interval...
2746 *
2747 * Rearm the idle timeout only one time when entering draining
2748 * state.
2749 */
2750 qc_idle_timer_do_rearm(qc);
2751 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002752 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002753 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002754 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002755 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002756 if (qc_is_listener(ctx->qc)) {
2757 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
2758 QUIC_EV_CONN_PRSHPKT, qc);
2759 goto leave;
2760 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002761
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002762 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002763 break;
2764 default:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002765 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
2766 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002767 }
2768 }
2769
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002770 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002771 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002772
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002773 if (fast_retrans) {
2774 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2775 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2776
2777 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2778 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002779
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002780 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2781 * has successfully parse a Handshake packet. The Initial encryption must also
2782 * be discarded.
2783 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002784 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002785 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002786 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2787 QUIC_FL_TLS_SECRETS_DCD)) {
2788 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2789 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2790 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2791 qc_set_timer(ctx->qc);
2792 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002793 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002794 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002795 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2796 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002797 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002798 }
2799
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002800 ret = 1;
2801 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002802 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002803 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002804}
2805
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002806/* Allocate Tx buffer from <qc> quic-conn if needed.
2807 *
2808 * Returns allocated buffer or NULL on error.
2809 */
2810static struct buffer *qc_txb_alloc(struct quic_conn *qc)
2811{
2812 struct buffer *buf = &qc->tx.buf;
2813 if (!b_alloc(buf))
2814 return NULL;
2815
2816 return buf;
2817}
2818
2819/* Free Tx buffer from <qc> if it is empty. */
2820static void qc_txb_release(struct quic_conn *qc)
2821{
2822 struct buffer *buf = &qc->tx.buf;
2823
2824 /* For the moment sending function is responsible to purge the buffer
2825 * entirely. It may change in the future but this requires to be able
2826 * to reuse old data.
2827 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002828 BUG_ON_HOT(buf && b_data(buf));
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002829
2830 if (!b_data(buf)) {
2831 b_free(buf);
2832 offer_buffers(NULL, 1);
2833 }
2834}
2835
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002836/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
2837 * must contains the address of the first packet stored in the payload.
2838 *
2839 * Caller is responsible that there is enough space in the buffer.
2840 */
2841static void qc_txb_store(struct buffer *buf, uint16_t length,
2842 struct quic_tx_packet *first_pkt)
2843{
2844 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
2845 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
2846
2847 write_u16(b_tail(buf), length);
2848 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
2849 b_add(buf, hdlen + length);
2850}
2851
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002852/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2853 * with <frms> as ack-eliciting frame list to send, 0 if not.
2854 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2855 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002856 * <force_ack> may be set to 1 if you want to force an ack.
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002857 */
2858static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002859 struct quic_enc_level *qel, int cc, int probe, int force_ack)
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002860{
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002861 unsigned int must_ack = force_ack ||
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002862 (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED));
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002863
2864 /* Do not build any more packet if the TX secrets are not available or
2865 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2866 * and if there is no more packets to send upon PTO expiration
2867 * and if there is no more ack-eliciting frames to send or in flight
2868 * congestion control limit is reached for prepared data
2869 */
2870 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2871 (!cc && !probe && !must_ack &&
2872 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002873 return 0;
2874 }
2875
2876 return 1;
2877}
2878
Amaury Denoyellef2476052022-08-04 16:19:57 +02002879/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2880 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
2881 *
2882 * Each datagram is prepended by a two fields header : the datagram length and
2883 * the address of the packet contained in the datagram.
2884 *
2885 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2886 * -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002887 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002888static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002889 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002890{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002891 int ret = -1;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002892 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002893 unsigned char *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002894 struct quic_tx_packet *pkt;
2895 size_t total;
Amaury Denoyellef2476052022-08-04 16:19:57 +02002896 /* Each datagram is prepended with its length followed by the address
2897 * of the first packet in the datagram.
2898 */
2899 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002900
2901 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002902
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002903 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2904 total = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002905 pos = (unsigned char *)b_tail(buf);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002906 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002907 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002908
2909 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002910 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002911 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002912 /* We do not probe if an immediate close was asked */
2913 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002914 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002915
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002916 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002917 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002918
2919 /* Leave room for the datagram header */
2920 pos += dg_headlen;
2921 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002922 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 +01002923 }
2924 else {
2925 end = pos + qc->path->mtu;
2926 }
2927
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002928 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
2929 QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002930 switch (err) {
2931 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002932 // trace already emitted by function above
2933 goto leave;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002934 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002935 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2936 * MTU, we are here because of the congestion control window. There is
2937 * no need to try to reuse this buffer.
2938 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002939 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002940 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002941 default:
2942 break;
2943 }
2944
2945 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002946 BUG_ON(!pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002947
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002948 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2949 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2950
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002951 total += pkt->len;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002952
Amaury Denoyellef2476052022-08-04 16:19:57 +02002953 /* Write datagram header. */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002954 qc_txb_store(buf, pkt->len, pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002955 }
2956
2957 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002958 ret = total;
2959 leave:
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002960 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002961 return ret;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002962}
2963
Amaury Denoyellef2476052022-08-04 16:19:57 +02002964/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2965 * <frms>. Several packets can be regrouped in a single datagram. The result is
2966 * written into <buf>.
2967 *
2968 * Each datagram is prepended by a two fields header : the datagram length and
2969 * the address of first packet in the datagram.
2970 *
2971 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2972 * -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002973 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002974static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002975 enum quic_tls_enc_level tel, struct list *tel_frms,
2976 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002977{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002978 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002979 unsigned char *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002980 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2981 /* length of datagrams */
2982 uint16_t dglen;
2983 size_t total;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002984 int ret = -1, padding;
Amaury Denoyellef2476052022-08-04 16:19:57 +02002985 /* Each datagram is prepended with its length followed by the address
2986 * of the first packet in the datagram.
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002987 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002988 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002989 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002990
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002991 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2992
Amaury Denoyellef2476052022-08-04 16:19:57 +02002993 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
2994 * caller must ensure that buf is resetted.
2995 */
2996 BUG_ON_HOT(buf->head || buf->data);
2997
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002998 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002999 qel = &qc->els[tel];
3000 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003001 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003002 padding = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003003 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003004 first_pkt = prv_pkt = NULL;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003005 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003006 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003007 enum quic_pkt_type pkt_type;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003008 struct quic_tls_ctx *tls_ctx;
3009 const struct quic_version *ver;
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003010 int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3011 (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3012 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003013
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003014 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003015 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003016 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003017 /* We do not probe if an immediate close was asked */
3018 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01003019 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003020
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003021 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003022 if (prv_pkt)
3023 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003024 /* Let's select the next encryption level */
3025 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3026 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003027 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003028 qel = &qc->els[tel];
3029 /* Build a new datagram */
3030 prv_pkt = NULL;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003031 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003032 continue;
3033 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003034 break;
3035 }
3036
3037 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003038 if (!prv_pkt) {
3039 /* Leave room for the datagram header */
3040 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003041 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02003042 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 +01003043 }
3044 else {
3045 end = pos + qc->path->mtu;
3046 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003047 }
3048
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003049 if (qc->negotiated_version) {
3050 ver = qc->negotiated_version;
3051 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3052 tls_ctx = &qc->negotiated_ictx;
3053 else
3054 tls_ctx = &qel->tls_ctx;
3055 }
3056 else {
3057 ver = qc->original_version;
3058 tls_ctx = &qel->tls_ctx;
3059 }
3060
3061 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02003062 qc, ver, dglen, pkt_type,
3063 force_ack, padding, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003064 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003065 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003066 // trace already emitted by function above
3067 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003068 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003069 /* If there was already a correct packet present, set the
3070 * current datagram as prepared into <cbuf>.
3071 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003072 if (prv_pkt)
3073 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003074 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003075 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003076 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01003077 break;
3078 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02003079
Frédéric Lécaille63556772021-12-29 17:18:21 +01003080 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003081 BUG_ON(!cur_pkt);
Frédéric Lécaille63556772021-12-29 17:18:21 +01003082
Frédéric Lécaille1809c332022-04-25 10:24:12 +02003083 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3084 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3085
Frédéric Lécaille63556772021-12-29 17:18:21 +01003086 total += cur_pkt->len;
3087 /* keep trace of the first packet in the datagram */
3088 if (!first_pkt)
3089 first_pkt = cur_pkt;
3090 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003091 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003092 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003093 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3094 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01003095 /* Let's say we have to build a new dgram */
3096 prv_pkt = NULL;
3097 dglen += cur_pkt->len;
3098 /* Client: discard the Initial encryption keys as soon as
3099 * a handshake packet could be built.
3100 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003101 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01003102 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3103 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3104 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3105 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3106 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01003107 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003108 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003109 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003110 }
3111 /* If the data for the current encryption level have all been sent,
3112 * select the next level.
3113 */
3114 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003115 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 +01003116 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3117 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3118 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3119 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003120 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3121 frms = &qc->els[tel].pktns->tx.frms;
3122 else
3123 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003124 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003125 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003126 /* If there is data for the next level, do not
3127 * consume a datagram.
3128 */
3129 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003130 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003131 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02003132
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003133 /* If we have to build a new datagram, set the current datagram as
3134 * prepared into <cbuf>.
3135 */
3136 if (!prv_pkt) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003137 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003138 first_pkt = NULL;
3139 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003140 padding = 0;
3141 }
3142 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003143 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003144 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3145 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003146 }
3147 }
3148
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003149 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003150 ret = total;
3151 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003152 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003153 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003154}
3155
Amaury Denoyellef2476052022-08-04 16:19:57 +02003156/* Send datagrams stored in <buf>.
3157 *
3158 * This function always returns 1 for success. Even if sendto() syscall failed,
3159 * buffer is drained and packets are considered as emitted. QUIC loss detection
3160 * mechanism is used as a back door way to retry sending.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003161 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003162int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003163{
3164 struct quic_conn *qc;
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003165 char skip_sendto = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003166
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003167 qc = ctx->qc;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003168 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003169 while (b_contig_data(buf, 0)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003170 unsigned char *pos;
3171 struct buffer tmpbuf = { };
3172 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3173 uint16_t dglen;
3174 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003175 unsigned int time_sent;
3176
Amaury Denoyellef2476052022-08-04 16:19:57 +02003177 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003178 dglen = read_u16(pos);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003179 BUG_ON_HOT(!dglen); /* this should not happen */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003180
3181 pos += sizeof dglen;
3182 first_pkt = read_ptr(pos);
3183 pos += sizeof first_pkt;
3184 tmpbuf.area = (char *)pos;
3185 tmpbuf.size = tmpbuf.data = dglen;
3186
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003187 /* If sendto is on error just skip the call to it for the rest
3188 * of the loop but continue to purge the buffer. Data will be
3189 * transmitted when QUIC packets are detected as lost on our
3190 * side.
3191 *
3192 * TODO use fd-monitoring to detect when send operation can be
3193 * retry. This should improve the bandwidth without relying on
3194 * retransmission timer. However, it requires a major rework on
3195 * quic-conn fd management.
3196 */
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003197 if (!skip_sendto) {
3198 if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) {
3199 skip_sendto = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003200 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003201 }
3202 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003203
Amaury Denoyellef2476052022-08-04 16:19:57 +02003204 b_del(buf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003205 qc->tx.bytes += tmpbuf.data;
3206 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003207
3208 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3209 pkt->time_sent = time_sent;
3210 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3211 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003212 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003213 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3214 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003216 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3217 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3218 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003219 qc_notify_close(qc);
3220
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003221 /* RFC 9000 10.2. Immediate Close:
3222 * The closing and draining connection states exist to ensure
3223 * that connections close cleanly and that delayed or reordered
3224 * packets are properly discarded. These states SHOULD persist
3225 * for at least three times the current PTO interval...
3226 *
3227 * Rearm the idle timeout only one time when entering closing
3228 * state.
3229 */
3230 qc_idle_timer_do_rearm(qc);
3231 if (qc->timer_task) {
3232 task_destroy(qc->timer_task);
3233 qc->timer_task = NULL;
3234 }
3235 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003236 qc->path->in_flight += pkt->in_flight_len;
3237 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3238 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003239 qc_set_timer(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003240 TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003241 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003242 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003243 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003244 }
3245 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003246
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003247 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3248
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003249 return 1;
3250}
3251
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003252/* Copy into <buf> buffer a stateless reset token depending on the
3253 * <salt> salt input. This is the cluster secret which will be derived
3254 * as HKDF input secret to generate this token.
3255 * Return 1 if succeeded, 0 if not.
3256 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003257static int quic_stateless_reset_token_cpy(struct quic_conn *qc,
3258 unsigned char *buf, size_t len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003259 const unsigned char *salt, size_t saltlen)
3260{
3261 /* Input secret */
3262 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3263 size_t keylen = strlen(global.cluster_secret);
3264 /* Info */
3265 const unsigned char label[] = "stateless token";
3266 size_t labellen = sizeof label - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003267 int ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003268
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003269 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3270
3271 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003272 key, keylen, salt, saltlen, label, labellen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003273 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3274 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003275}
3276
3277/* Initialize the stateless reset token attached to <cid> connection ID.
3278 * Returns 1 if succeeded, 0 if not.
3279 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003280static int quic_stateless_reset_token_init(struct quic_conn *qc,
3281 struct quic_connection_id *quic_cid)
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003282{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003283 int ret;
3284
3285 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3286
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003287 if (global.cluster_secret) {
3288 /* Output secret */
3289 unsigned char *token = quic_cid->stateless_reset_token;
3290 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3291 /* Salt */
3292 const unsigned char *cid = quic_cid->cid.data;
3293 size_t cidlen = quic_cid->cid.len;
3294
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003295 ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003296 }
3297 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003298 /* TODO: RAND_bytes() should be replaced */
3299 ret = RAND_bytes(quic_cid->stateless_reset_token,
3300 sizeof quic_cid->stateless_reset_token) == 1;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003301 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003302
3303 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3304 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003305}
3306
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003307/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3308 * ebtree.
3309 *
3310 * The CID is randomly generated in part with the result altered to be
3311 * associated with the current thread ID. This means this function must only
3312 * be called by the quic_conn thread.
3313 *
3314 * Returns the new CID if succeeded, NULL if not.
3315 */
3316static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3317 struct quic_conn *qc,
3318 int seq_num)
3319{
3320 struct quic_connection_id *cid;
3321
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003322 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3323
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003324 cid = pool_alloc(pool_head_quic_connection_id);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003325 if (!cid) {
3326 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
3327 goto err;
3328 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003329
3330 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003331 /* TODO: RAND_bytes() should be replaced */
3332 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) {
3333 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003334 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003335 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003336
3337 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003338 if (quic_stateless_reset_token_init(qc, cid) != 1) {
3339 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003340 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003341 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003342
3343 cid->qc = qc;
3344
3345 cid->seq_num.key = seq_num;
3346 cid->retire_prior_to = 0;
3347 /* insert the allocated CID in the quic_conn tree */
3348 eb64_insert(root, &cid->seq_num);
3349
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003350 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003351 return cid;
3352
3353 err:
3354 pool_free(pool_head_quic_connection_id, cid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003355 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003356 return NULL;
3357}
3358
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003359/* Build all the frames which must be sent just after the handshake have succeeded.
3360 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3361 * a HANDSHAKE_DONE frame.
3362 * Return 1 if succeeded, 0 if not.
3363 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003364static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003366 int ret = 0, i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003367 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003368 struct quic_frame *frm, *frmbak;
3369 struct list frm_list = LIST_HEAD_INIT(frm_list);
3370 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003371
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003372 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
3373
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003374 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003375 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003376 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003377 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003378 if (!frm) {
3379 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3380 goto leave;
3381 }
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003382
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003383 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003384 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003385 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003386 }
3387
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003388 /* Initialize <max> connection IDs minus one: there is
3389 * already one connection ID used for the current connection.
3390 */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003391 first = 1;
3392 max = qc->tx.params.active_connection_id_limit;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003393
3394 /* TODO: check limit */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003395 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003396 struct quic_connection_id *cid;
3397
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003398 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003399 if (!frm) {
3400 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003401 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003402 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003403
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003404 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003405 cid = new_quic_cid(&qc->cids, qc, i);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003406 if (!cid) {
3407 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003408 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003409 }
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003410
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003411 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003412 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003413
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003414 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003415 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003416 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003417
3418 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003419 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003420
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003421 ret = 1;
3422 leave:
3423 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
3424 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003425
3426 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003427 /* free the frames */
3428 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3429 pool_free(pool_head_quic_frame, frm);
3430
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003431 node = eb64_lookup_ge(&qc->cids, first);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003432 while (node) {
3433 struct quic_connection_id *cid;
3434
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003435 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003436 if (cid->seq_num.key >= max)
3437 break;
3438
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003439 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003440 ebmb_delete(&cid->node);
3441 eb64_delete(&cid->seq_num);
3442 pool_free(pool_head_quic_connection_id, cid);
3443 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003444 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003445}
3446
3447/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003448void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003449{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003450 struct eb64_node *n;
3451 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003452
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003453 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
3454
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003455 n = eb64_first(&arngs->root);
3456 while (n) {
3457 struct eb64_node *next;
3458
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003459 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003460 next = eb64_next(n);
3461 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003462 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003463 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003464 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003465
3466 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003467}
3468
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003469/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3470 * descending order.
3471 */
3472static inline size_t sack_gap(struct quic_arng_node *p,
3473 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003474{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003475 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003476}
3477
3478
3479/* Remove the last elements of <ack_ranges> list of ack range updating its
3480 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003481 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003482 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003483static int quic_rm_last_ack_ranges(struct quic_conn *qc,
3484 struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003485{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003486 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003487 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003488
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003489 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3490
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003491 last = eb64_last(&arngs->root);
3492 while (last && arngs->enc_sz > limit) {
3493 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003494
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003495 prev = eb64_prev(last);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003496 if (!prev) {
3497 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
3498 goto out;
3499 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003500
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003501 last_node = eb64_entry(last, struct quic_arng_node, first);
3502 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003503 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3504 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3505 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3506 --arngs->sz;
3507 eb64_delete(last);
3508 pool_free(pool_head_quic_arng, last);
3509 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003510 }
3511
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003512 ret = 1;
3513 out:
3514 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3515 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003516}
3517
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003518/* Set the encoded size of <arngs> QUIC ack ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003519static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003520{
3521 struct eb64_node *node, *next;
3522 struct quic_arng_node *ar, *ar_next;
3523
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003524 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3525
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003526 node = eb64_last(&arngs->root);
3527 if (!node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003528 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003529
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003530 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003531 arngs->enc_sz = quic_int_getsize(ar->last) +
3532 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3533
3534 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003535 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003536 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3537 quic_int_getsize(ar_next->last - ar_next->first.key);
3538 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003539 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003540 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003541
3542 leave:
3543 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003544}
3545
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003546/* Insert <ar> ack range into <argns> tree of ack ranges.
3547 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003548 */
3549static inline
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003550struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
3551 struct quic_arngs *arngs,
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003552 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003553{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003554 struct quic_arng_node *new_ar;
3555
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003556 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3557
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003558 new_ar = pool_alloc(pool_head_quic_arng);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003559 if (!new_ar) {
3560 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
3561 goto leave;
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003562 }
3563
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003564 new_ar->first.key = ar->first;
3565 new_ar->last = ar->last;
3566 eb64_insert(&arngs->root, &new_ar->first);
3567 arngs->sz++;
3568
3569 leave:
3570 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003571 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003572}
3573
3574/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003575 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003576 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003577 *
3578 * Descending order
3579 * ------------->
3580 * range1 range2
3581 * ..........|--------|..............|--------|
3582 * ^ ^ ^ ^
3583 * | | | |
3584 * last1 first1 last2 first2
3585 * ..........+--------+--------------+--------+......
3586 * diff1 gap12 diff2
3587 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003588 * To encode the previous list of ranges we must encode integers as follows in
3589 * descending order:
3590 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003591 * with diff1 = last1 - first1
3592 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003593 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003594 *
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003595
3596returns 0 on error
3597
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003598 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003599int quic_update_ack_ranges_list(struct quic_conn *qc,
3600 struct quic_arngs *arngs,
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003601 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003602{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003603 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003604 struct eb64_node *le;
3605 struct quic_arng_node *new_node;
3606 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003607
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003608 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3609
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003610 new = NULL;
3611 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003612 new_node = quic_insert_new_range(qc, arngs, ar);
3613 if (new_node)
3614 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003615
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003616 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003617 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003618
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003619 le = eb64_lookup_le(&arngs->root, ar->first);
3620 if (!le) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003621 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003622 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003623 goto leave;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003624
3625 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003626 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003627 else {
3628 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003629 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003630
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003631 /* Already existing range */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003632 if (le_ar->last >= ar->last) {
3633 ret = 1;
3634 }
3635 else if (le_ar->last + 1 >= ar->first) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003636 le_ar->last = ar->last;
3637 new = le;
3638 new_node = le_ar;
3639 }
3640 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003641 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003642 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003643 goto leave;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003644
3645 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003646 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003647 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003648
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003649 /* Verify that the new inserted node does not overlap the nodes
3650 * which follow it.
3651 */
3652 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003653 struct eb64_node *next;
3654 struct quic_arng_node *next_node;
3655
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003656 while ((next = eb64_next(new))) {
3657 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003658 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003659 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003660 break;
3661
3662 if (next_node->last > new_node->last)
3663 new_node->last = next_node->last;
3664 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003665 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003666 /* Decrement the size of these ranges. */
3667 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003668 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003669 }
3670
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003671 ret = 1;
3672 leave:
3673 quic_arngs_set_enc_sz(qc, arngs);
3674 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
3675 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003676}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003677/* Remove the header protection of packets at <el> encryption level.
3678 * Always succeeds.
3679 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003680static 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 +01003681{
3682 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003683 struct quic_rx_packet *pqpkt;
3684 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003685 struct quic_enc_level *app_qel;
3686
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003687 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3688 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003689 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003690 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003691 TRACE_DEVEL("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003692 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003693 goto out;
3694 }
3695 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003696 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003697 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02003698 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003699 TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003700 }
3701 else {
3702 /* The AAD includes the packet number field */
3703 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3704 /* Store the packet into the tree of packets to decrypt. */
3705 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003706 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3707 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003708 TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003709 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003710 MT_LIST_DELETE_SAFE(pkttmp1);
3711 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712 }
3713
3714 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003715 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003716}
3717
3718/* Process all the CRYPTO frame at <el> encryption level.
3719 * Return 1 if succeeded, 0 if not.
3720 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003721static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
3722 struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003723 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003724{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003725 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003726 struct eb64_node *node;
3727
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003728 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3729
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003730 node = eb64_first(&el->rx.crypto.frms);
3731 while (node) {
3732 struct quic_rx_crypto_frm *cf;
3733
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003734 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003735 if (cf->offset_node.key != el->rx.crypto.offset)
3736 break;
3737
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003738 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) {
3739 TRACE_ERROR("qc_provide_cdata() failed", QUIC_EV_CONN_TRMHP);
3740 goto leave;
3741 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003742
3743 node = eb64_next(node);
3744 quic_rx_packet_refdec(cf->pkt);
3745 eb64_delete(&cf->offset_node);
3746 pool_free(pool_head_quic_rx_crypto_frm, cf);
3747 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003748
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003749 ret = 1;
3750 leave:
3751 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3752 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003753}
3754
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003755/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003756 * 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 +02003757 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003758 * Return 1 if succeeded, 0 if not.
3759 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003760int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3761 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003762{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003763 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003764 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003765 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003766 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003767 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003768 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003769
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003770 TRACE_ENTER(QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003771 qel = cur_el;
3772 next_tel:
3773 if (!qel)
3774 goto out;
3775
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003776 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003777 while (node) {
3778 struct quic_rx_packet *pkt;
3779
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003780 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003781 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003782 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003783 if (!qc_pkt_decrypt(pkt, qel, qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003784 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003785 TRACE_ERROR("packet decryption failed -> dropped",
3786 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003787 }
3788 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003789 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003790 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003791 TRACE_ERROR("packet parsing failed -> dropped",
3792 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02003793 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003794 }
3795 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003796 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3797
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003798 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3799 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3800 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003801 qc_idle_timer_rearm(qc, 1);
3802 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003803 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003804 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003805 largest_pn_time_received = pkt->time_received;
3806 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003807 /* Update the list of ranges to acknowledge. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003808 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
3809 TRACE_ERROR("Could not update ack range list",
3810 QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003811 }
3812 }
3813 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003814 eb64_delete(&pkt->pn_node);
3815 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003816 }
3817
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003818 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3819 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003820 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003821 /* Update the largest acknowledged packet timestamps */
3822 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3823 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3824 }
3825
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003826 if (!qc_treat_rx_crypto_frms(qc, qel, ctx)) {
3827 // trace already emitted by function above
3828 goto leave;
3829 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003830
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003831 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003832 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003833 qel = next_el;
Frédéric Lécaille91a211f2022-05-24 10:54:42 +02003834 largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003835 goto next_tel;
3836 }
3837
3838 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003839 ret = 1;
3840 leave:
3841 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, ctx->qc);
3842 return ret;
3843}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003844
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003845/* Check if it's possible to remove header protection for packets related to
3846 * encryption level <qel>. If <qel> is NULL, assume it's false.
3847 *
3848 * Return true if the operation is possible else false.
3849 */
3850static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3851{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003852 int ret = 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003853 enum quic_tls_enc_level tel;
3854
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003855 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3856
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003857 if (!qel)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003858 goto cant_rm_hp;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003859
3860 tel = ssl_to_quic_enc_level(qel->level);
3861
3862 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003863 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003864 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003865 goto cant_rm_hp;
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003866 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003867
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003868 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3869 TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc);
3870 goto cant_rm_hp;
3871 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003872
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003873 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003874 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003875 qc->mux_state == QC_MUX_NULL) {
3876 TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
3877 goto cant_rm_hp;
3878 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003879
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003880 ret = 1;
3881 cant_rm_hp:
3882 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3883 return ret;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003884}
3885
Amaury Denoyellecc130472022-08-17 10:08:16 +02003886/* Try to send application frames from list <frms> on connection <qc>.
3887 *
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003888 * For retransmission you must use wrapper depending on the sending condition :
3889 * - use qc_send_app_retransmit to send data detected as lost
3890 * - use qc_send_app_probing when probing with already sent data
Amaury Denoyellecc130472022-08-17 10:08:16 +02003891 *
3892 * Returns 1 on success. Some data might not have been sent due to congestion,
3893 * in this case they are left in <frms> input list. The caller may subscribe on
3894 * quic-conn to retry later.
3895 *
3896 * Returns 0 on critical error.
3897 * TODO review and classify more distinctly transient from definitive errors to
3898 * allow callers to properly handle it.
3899 */
3900int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003901{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003902 int status = 0;
3903 struct buffer *buf;
3904
3905 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3906
3907 buf = qc_txb_alloc(qc);
3908 if (!buf) {
3909 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
3910 goto leave;
3911 }
3912
Frédéric Lécailledc077512022-07-26 09:17:19 +02003913 /* Prepare and send packets until we could not further prepare packets. */
3914 while (1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003915 int ret;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003916 /* Currently buf cannot be non-empty at this stage. Even if a
3917 * previous sendto() has failed it is emptied to simulate
3918 * packet emission and rely on QUIC lost detection to try to
3919 * emit it.
3920 */
3921 BUG_ON_HOT(b_data(buf));
3922 b_reset(buf);
3923
Amaury Denoyellef2476052022-08-04 16:19:57 +02003924 ret = qc_prep_app_pkts(qc, buf, frms);
Frédéric Lécailledc077512022-07-26 09:17:19 +02003925 if (ret == -1)
3926 goto err;
3927 else if (ret == 0)
3928 goto out;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003929
Amaury Denoyellef2476052022-08-04 16:19:57 +02003930 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécailledc077512022-07-26 09:17:19 +02003931 goto err;
3932 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003933
3934 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003935 status = 1;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003936 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003937 leave:
3938 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3939 return status;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003940
3941 err:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003942 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003943 goto leave;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003944}
3945
Amaury Denoyellecc130472022-08-17 10:08:16 +02003946/* Try to send application frames from list <frms> on connection <qc>. Use this
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003947 * function when retransmitting lost frames.
3948 *
3949 * Returns the result from qc_send_app_pkts function.
3950 */
3951static forceinline int qc_send_app_retransmit(struct quic_conn *qc,
3952 struct list *frms)
3953{
3954 int ret;
3955
3956 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3957
3958 TRACE_STATE("preparing lost data (retransmission)", QUIC_EV_CONN_TXPKT, qc);
3959 qc->flags |= QUIC_FL_CONN_RETRANS_LOST_DATA;
3960 ret = qc_send_app_pkts(qc, frms);
3961 qc->flags &= ~QUIC_FL_CONN_RETRANS_LOST_DATA;
3962
3963 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3964 return ret;
3965}
3966
3967/* Try to send application frames from list <frms> on connection <qc>. Use this
Amaury Denoyellecc130472022-08-17 10:08:16 +02003968 * function when probing is required.
3969 *
3970 * Returns the result from qc_send_app_pkts function.
3971 */
3972static forceinline int qc_send_app_probing(struct quic_conn *qc,
3973 struct list *frms)
3974{
3975 int ret;
3976
3977 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3978
3979 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
3980 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
3981 ret = qc_send_app_pkts(qc, frms);
3982 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3983
3984 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3985 return ret;
3986}
3987
Frédéric Lécaillea9568412022-04-25 08:58:04 +02003988/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
3989 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003990 * QUIC connection. <old_data> is used as boolean to send data already sent but
3991 * not already acknowledged (in flight).
Frédéric Lécaillea9568412022-04-25 08:58:04 +02003992 * Returns 1 if succeeded, 0 if not.
3993 */
3994int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
3995 enum quic_tls_enc_level tel, struct list *tel_frms,
3996 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3997{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003998 int ret, status = 0;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003999 struct buffer *buf = qc_txb_alloc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004000
4001 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4002
4003 if (!buf) {
4004 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4005 goto leave;
4006 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004007
Amaury Denoyellef2476052022-08-04 16:19:57 +02004008 /* Currently buf cannot be non-empty at this stage. Even if a previous
4009 * sendto() has failed it is emptied to simulate packet emission and
4010 * rely on QUIC lost detection to try to emit it.
4011 */
4012 BUG_ON_HOT(b_data(buf));
4013 b_reset(buf);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004014
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004015 if (old_data) {
4016 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004017 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004018 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02004019
4020 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004021 if (ret == -1)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004022 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004023 else if (ret == 0)
4024 goto skip_send;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004025
Amaury Denoyellef2476052022-08-04 16:19:57 +02004026 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004027 goto out;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004028
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004029 skip_send:
4030 status = 1;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004031 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004032 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004033 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004034 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004035 leave:
4036 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4037 return status;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004038}
4039
4040/* Retransmit up to two datagrams depending on packet number space */
4041static void qc_dgrams_retransmit(struct quic_conn *qc)
4042{
4043 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4044 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4045 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4046
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004047 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4048
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004049 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4050 struct list ifrms = LIST_HEAD_INIT(ifrms);
4051 struct list hfrms = LIST_HEAD_INIT(hfrms);
4052
4053 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004054 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4055 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004056 if (!LIST_ISEMPTY(&ifrms)) {
4057 iqel->pktns->tx.pto_probe = 1;
4058 if (!LIST_ISEMPTY(&hfrms)) {
4059 hqel->pktns->tx.pto_probe = 1;
4060 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4061 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
4062 }
4063 }
4064 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4065 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004066 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004067 if (!LIST_ISEMPTY(&hfrms)) {
4068 hqel->pktns->tx.pto_probe = 1;
4069 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
4070 QUIC_TLS_ENC_LEVEL_NONE, NULL);
4071 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004072 TRACE_STATE("no more need to probe Handshake packet number space",
4073 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004074 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4075 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004076 TRACE_STATE("no more need to probe Initial packet number space",
4077 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004078 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4079 }
4080 else {
4081 int i;
4082 struct list frms1 = LIST_HEAD_INIT(frms1);
4083 struct list frms2 = LIST_HEAD_INIT(frms2);
4084
4085 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4086 hqel->pktns->tx.pto_probe = 0;
4087 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4088 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004089 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004090 if (!LIST_ISEMPTY(&frms1)) {
4091 hqel->pktns->tx.pto_probe = 1;
4092 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4093 QUIC_TLS_ENC_LEVEL_NONE, NULL);
4094 }
4095 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004096 TRACE_STATE("no more need to probe Handshake packet number space",
4097 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004098 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4099 }
4100 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4101 aqel->pktns->tx.pto_probe = 0;
4102 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4103 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4104 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4105 if (!LIST_ISEMPTY(&frms1)) {
4106 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004107 qc_send_app_probing(qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004108 }
4109 if (!LIST_ISEMPTY(&frms2)) {
4110 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004111 qc_send_app_probing(qc, &frms2);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004112 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004113 TRACE_STATE("no more need to probe 01RTT packet number space",
4114 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004115 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4116 }
4117 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004118 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004119}
4120
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004121/* QUIC connection packet handler task (post handshake) */
4122static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4123{
4124 struct ssl_sock_ctx *ctx;
4125 struct quic_conn *qc;
4126 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004127
4128
4129 ctx = context;
4130 qc = ctx->qc;
4131 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004132
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004133 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4134 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004135
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004136 /* Retranmissions */
4137 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004138 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004139 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4140 qc_dgrams_retransmit(qc);
4141 }
4142
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004143 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
4144 qc_rm_hp_pkts(qc, qel);
4145
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004146 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) {
4147 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4148 goto out;
4149 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004150
Frédéric Lécaille47756802022-03-25 09:12:16 +01004151 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004152 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4153 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01004154 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004155 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004156
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004157 /* XXX TODO: how to limit the list frames to send */
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02004158 if (!qc_send_app_retransmit(qc, &qel->pktns->tx.frms)) {
4159 TRACE_DEVEL("qc_send_app_retransmit() failed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004160 goto out;
4161 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004162
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004163 out:
4164 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004165 return t;
4166}
4167
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004168/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4169static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4170{
4171 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
4172 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
4173 qel->pktns->tx.pto_probe ||
4174 !LIST_ISEMPTY(&qel->pktns->tx.frms);
4175}
4176
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004177/* QUIC connection packet handler task. */
4178struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004179{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004180 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004181 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004182 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004183 enum quic_tls_enc_level tel, next_tel;
4184 struct quic_enc_level *qel, *next_qel;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004185 struct buffer *buf = NULL;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004186 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004187
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004188 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004189 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004190 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004191 st = qc->state;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004192 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004193
4194 /* Retranmissions */
4195 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004196 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004197 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4198 qc_dgrams_retransmit(qc);
4199 }
4200
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004201 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
4202 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004203 TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached",
4204 QUIC_EV_CONN_IO_CB, qc);
4205 /* 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 +01004206 * after the anti-amplification was reached.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004207 *
4208 * TODO: this part should be removed. This was there because the
4209 * datagram parser was not executed by only one thread.
Frédéric Lécaille59507de2022-08-11 12:14:07 +02004210 */
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004211 qc_set_timer(qc);
4212 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
4213 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
4214 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004216 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
4217 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
4218 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004219 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004220 if (st >= QUIC_HS_ST_COMPLETE &&
4221 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004222 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004223 /* There may be remaining Handshake packets to treat and acknowledge. */
4224 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4225 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4226 }
4227 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004228 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004229
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004230 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02004231 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004232
4233 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004234 /* Treat packets waiting for header packet protection decryption */
4235 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004236 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004237
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004238 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
4239 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4240 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004241 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004242
Frédéric Lécaille47756802022-03-25 09:12:16 +01004243 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4244 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4245 goto out;
4246
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004247 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
4248 !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
4249 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
4250 qel = next_qel;
4251 next_qel = NULL;
4252 goto next_level;
4253 }
4254 else {
4255 struct quic_rx_packet *pkt;
4256 struct mt_list *elt1, elt2;
4257 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
4258
4259 /* Drop these 0-RTT packets */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004260 TRACE_DEVEL("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004261 mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) {
4262 MT_LIST_DELETE_SAFE(elt1);
4263 quic_rx_packet_refdec(pkt);
4264 }
4265 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004266 }
4267
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004268 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004269 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004270 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004271 !quic_build_post_handshake_frames(qc))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004272 goto out;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01004273
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004274 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004275 QUIC_FL_TLS_SECRETS_DCD)) {
4276 /* Discard the Handshake keys. */
4277 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
4278 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
4279 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
4280 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004281 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004282 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004283 }
4284
4285 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
4286 /* There may be remaining handshake to build (acks) */
4287 st = QUIC_HS_ST_SERVER_HANDSHAKE;
4288 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004289 }
4290
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01004291 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
4292 * be considered.
4293 */
4294 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004295 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004296
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004297 if (!qc_need_sending(qc, qel) &&
4298 (!next_qel || !qc_need_sending(qc, next_qel))) {
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004299 goto skip_send;
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004300 }
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004301
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004302 buf = qc_txb_alloc(qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02004303 if (!buf)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004304 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004305
4306 /* Currently buf cannot be non-empty at this stage. Even if a previous
4307 * sendto() has failed it is emptied to simulate packet emission and
4308 * rely on QUIC lost detection to try to emit it.
4309 */
4310 BUG_ON_HOT(b_data(buf));
4311 b_reset(buf);
4312
4313 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02004314 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004315 if (ret == -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004316 goto out;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004317 else if (ret == 0)
4318 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004319
Amaury Denoyellef2476052022-08-04 16:19:57 +02004320 if (!qc_send_ppkts(buf, ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004321 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004322
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004323 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004324 /* Check if there is something to do for the next level.
4325 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004326 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004327 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01004328 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004329 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004330 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004331 goto next_level;
4332 }
4333
Frédéric Lécaille47756802022-03-25 09:12:16 +01004334 out:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004335 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004336 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004337 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004338}
4339
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004340/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004341static 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 +01004342{
4343 int i;
4344
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004345 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4346
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004347 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4348 if (qel->tx.crypto.bufs[i]) {
4349 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4350 qel->tx.crypto.bufs[i] = NULL;
4351 }
4352 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004353 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004354
4355 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004356}
4357
4358/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004359 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004360 * Returns 1 if succeeded, 0 if not.
4361 */
4362static int quic_conn_enc_level_init(struct quic_conn *qc,
4363 enum quic_tls_enc_level level)
4364{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004365 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004366 struct quic_enc_level *qel;
4367
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004368 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4369
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004370 qel = &qc->els[level];
4371 qel->level = quic_to_ssl_enc_level(level);
4372 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4373 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4374 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004375 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004376
4377 qel->rx.pkts = EB_ROOT;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02004378 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004379 qel->rx.crypto.offset = 0;
4380 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004381
4382 /* Allocate only one buffer. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004383 /* TODO: use a pool */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004384 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4385 if (!qel->tx.crypto.bufs)
4386 goto err;
4387
4388 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4389 if (!qel->tx.crypto.bufs[0])
4390 goto err;
4391
4392 qel->tx.crypto.bufs[0]->sz = 0;
4393 qel->tx.crypto.nb_buf = 1;
4394
4395 qel->tx.crypto.sz = 0;
4396 qel->tx.crypto.offset = 0;
4397
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004398 ret = 1;
4399 leave:
4400 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4401 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004402
4403 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004404 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004405 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004406}
4407
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004408/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4409 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004410 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004411 * This function must only be called by the thread responsible of the quic_conn
4412 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004413 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004414static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004415{
4416 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004417 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004418 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004419 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004420 struct quic_rx_packet *pkt, *pktback;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004421
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004422 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4423
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004424 /* We must not free the quic-conn if the MUX is still allocated. */
4425 BUG_ON(qc->mux_state == QC_MUX_READY);
4426
Willy Tarreau54bc7862022-08-10 07:26:27 +02004427 /* in the unlikely (but possible) case the connection was just added to
4428 * the accept_list we must delete it from there.
4429 */
4430 MT_LIST_DELETE(&qc->accept_list);
4431
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004432 /* free remaining stream descriptors */
4433 node = eb64_first(&qc->streams_by_id);
4434 while (node) {
4435 struct qc_stream_desc *stream;
4436
4437 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4438 node = eb64_next(node);
4439
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004440 /* all streams attached to the quic-conn are released, so
4441 * qc_stream_desc_free will liberate the stream instance.
4442 */
4443 BUG_ON(!stream->release);
4444 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004445 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004446
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004447 /* Purge Rx packet list. */
4448 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
4449 LIST_DELETE(&pkt->qc_rx_pkt_list);
4450 pool_free(pool_head_quic_rx_packet, pkt);
4451 }
4452
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004453 if (qc->idle_timer_task) {
4454 task_destroy(qc->idle_timer_task);
4455 qc->idle_timer_task = NULL;
4456 }
4457
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004458 if (qc->timer_task) {
4459 task_destroy(qc->timer_task);
4460 qc->timer_task = NULL;
4461 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004462
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004463 /* remove the connection from receiver cids trees */
4464 ebmb_delete(&qc->odcid_node);
4465 ebmb_delete(&qc->scid_node);
4466 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004467
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004468 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004469 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004470 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004471 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004472 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004473 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004474
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004475 quic_tls_ku_free(qc);
4476 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4477 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004478 quic_conn_enc_level_uninit(qc, &qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004479 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004480 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004481
4482 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4483 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4484 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004485
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004486 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
Frédéric Lécaille8ddde4f2022-08-01 14:07:50 +02004487 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004488 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004489 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004490
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004491 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4492 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004493 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004494
4495 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004496}
4497
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004498static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004499{
4500 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004501 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004502
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004503 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004504
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004505 /* Next application data can be dropped. */
4506 qc->mux_state = QC_MUX_RELEASED;
4507
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004508 /* If the quic-conn timer has already expired free the quic-conn. */
4509 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4510 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004511 goto leave;
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004512 }
4513
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02004514 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004515 leave:
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004516 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004517}
4518
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004519/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004520static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004521{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004522 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004523 struct quic_conn *qc;
4524 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004525
4526 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004527 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004528 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004529 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004530 task->expire = TICK_ETERNITY;
4531 pktns = quic_loss_pktns(qc);
4532 if (tick_isset(pktns->tx.loss_time)) {
4533 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4534
4535 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
4536 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004537 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
4538 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004539 goto out;
4540 }
4541
4542 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004543 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004544 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4545 qc->qcc->subs->events & SUB_RETRY_SEND) {
4546 struct qcc *qcc = qc->qcc;
4547
4548 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4549 tasklet_wakeup(qcc->subs->tasklet);
4550 qcc->subs->events &= ~SUB_RETRY_SEND;
4551 if (!qcc->subs->events)
4552 qcc->subs = NULL;
4553 }
4554 else {
4555 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4556 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4557 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004558 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4559 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004560 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004561 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4562 }
4563 }
4564 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
4565 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4566 }
4567 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
4568 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004569 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004570 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004571 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004572 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004573 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4574 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4575
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004576 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004577 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004578 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004579 iel->pktns->tx.pto_probe = 1;
4580 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004581
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004582 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4583 qc->path->loss.pto_count++;
4584
4585 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004586 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004587
4588 return task;
4589}
4590
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004591/* Parse the Retry token from buffer <token> with <end> a pointer to
4592 * one byte past the end of this buffer. This will extract the ODCID
4593 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004594 *
4595 * Returns 0 on success else non-zero.
4596 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004597static int parse_retry_token(struct quic_conn *qc,
4598 const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004599 struct quic_cid *odcid)
4600{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004601 int ret = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004602 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004603 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004604
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004605 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
4606
4607 if (!quic_dec_int(&odcid_len, &token, end)) {
4608 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
4609 goto leave;
4610 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004611
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004612 /* RFC 9000 7.2. Negotiating Connection IDs:
4613 * When an Initial packet is sent by a client that has not previously
4614 * received an Initial or Retry packet from the server, the client
4615 * populates the Destination Connection ID field with an unpredictable
4616 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4617 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004618 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
4619 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
4620 goto leave;
4621 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004622
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004623 if (end - token < odcid_len + sizeof timestamp) {
4624 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
4625 goto leave;
4626 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004627
4628 timestamp = ntohl(read_u32(token + odcid_len));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004629 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
4630 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
4631 goto leave;
4632 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004633
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004634 ret = 1;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004635 memcpy(odcid->data, token, odcid_len);
4636 odcid->len = odcid_len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004637 leave:
4638 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
4639 return !ret;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004640}
4641
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004642/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4643 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4644 * for QUIC servers (or haproxy listeners).
4645 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4646 * <token> the token found to be used for this connection with <token_len> as
4647 * length. <saddr> is the source address.
4648 * Returns the connection if succeeded, NULL if not.
4649 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004650static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004651 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02004652 const struct quic_cid *token_odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004653 struct sockaddr_storage *saddr,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004654 int server, int token, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004655{
4656 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004657 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004658 /* Initial CID. */
4659 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004660 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004661 struct listener *l = NULL;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004662 struct quic_cc_algo *cc_algo = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004663
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004664 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004665 qc = pool_zalloc(pool_head_quic_conn);
4666 if (!qc) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004667 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004668 goto err;
4669 }
4670
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004671 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4672 if (!buf_area) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004673 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004674 goto err;
4675 }
4676
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004677 qc->cids = EB_ROOT;
4678 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004679 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004680 struct proxy *prx;
4681
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004682 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004683 prx = l->bind_conf->frontend;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004684 cc_algo = l->bind_conf->quic_cc_algo;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004685
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004686 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4687 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004688 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004689 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004690 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004691 qc->odcid.len = dcid->len;
4692 qc->odcid.addrlen = dcid->addrlen;
4693 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004694
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004695 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004696 if (scid->len)
4697 memcpy(qc->dcid.data, scid->data, scid->len);
4698 qc->dcid.len = scid->len;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004699 qc->tx.buf = BUF_NULL;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004700 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004701 }
4702 /* QUIC Client (outgoing connection to servers) */
4703 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004704 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004705 if (dcid->len)
4706 memcpy(qc->dcid.data, dcid->data, dcid->len);
4707 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004708 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004709 qc->mux_state = QC_MUX_NULL;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02004710 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004711
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004712 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004713 if (!icid) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004714 TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004715 goto err;
4716 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004717
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004718 /* insert the allocated CID in the receiver datagram handler tree */
4719 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004720 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004721
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004722 /* Select our SCID which is the first CID with 0 as sequence number. */
4723 qc->scid = icid->cid;
4724
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004725 /* Packet number spaces initialization. */
4726 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4727 quic_pktns_init(&qc->pktns[i]);
4728 /* QUIC encryption level context initialization. */
4729 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004730 if (!quic_conn_enc_level_init(qc, i)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004731 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004732 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004733 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004734 /* Initialize the packet number space. */
4735 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4736 }
4737
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004738 qc->original_version = qv;
4739 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004740 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4741 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004742 /* TX part. */
4743 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004744 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4745 qc->tx.wbuf = qc->tx.rbuf = 0;
4746 qc->tx.bytes = 0;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004747 qc->tx.buf = BUF_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004748 /* RX part. */
4749 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004750 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004751 for (i = 0; i < QCS_MAX_TYPES; i++)
4752 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004753
4754 qc->nb_pkt_for_cc = 1;
4755 qc->nb_pkt_since_cc = 0;
4756
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004757 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004758 if (!quic_tls_ku_init(qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004759 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004760 goto err;
4761 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004762
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004763 /* XXX TO DO: Only one path at this time. */
4764 qc->path = &qc->paths[0];
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004765 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 +01004766
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004767 /* required to use MTLIST_IN_LIST */
4768 MT_LIST_INIT(&qc->accept_list);
4769
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004770 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004771 qc->stream_buf_count = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004772 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4773
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004774 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
4775 icid->stateless_reset_token,
4776 dcid->data, dcid->len,
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02004777 qc->scid.data, qc->scid.len, token_odcid))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004778 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004779
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004780 if (qc_conn_alloc_ssl_ctx(qc) ||
4781 !quic_conn_init_timer(qc) ||
4782 !quic_conn_init_idle_timer_task(qc))
4783 goto err;
4784
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004785 if (!qc_new_isecs(qc, &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx,
4786 qc->original_version, dcid->data, dcid->len, 1))
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004787 goto err;
4788
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004789 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004790
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004791 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004792
4793 err:
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004794 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4795 if (qc)
4796 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004797 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004798 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004799 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004800}
4801
4802/* Initialize the timer task of <qc> QUIC connection.
4803 * Returns 1 if succeeded, 0 if not.
4804 */
4805static int quic_conn_init_timer(struct quic_conn *qc)
4806{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004807 int ret = 0;
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004808 /* Attach this task to the same thread ID used for the connection */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004809 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4810
Willy Tarreau87168752022-06-13 16:31:53 +02004811 qc->timer_task = task_new_on(qc->tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004812 if (!qc->timer_task) {
4813 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4814 goto leave;
4815 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004816
4817 qc->timer = TICK_ETERNITY;
4818 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004819 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004820
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004821 ret = 1;
4822 leave:
4823 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4824 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004825}
4826
Frédéric Lécaille47756802022-03-25 09:12:16 +01004827/* Rearm the idle timer for <qc> QUIC connection. */
4828static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4829{
4830 unsigned int expire;
4831
4832 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4833 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4834}
4835
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004836/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4837 * which is set to 1 when receiving a packet , and 0 when sending packet
4838 */
4839static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4840{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004841 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4842
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004843 if (read) {
4844 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4845 }
4846 else {
4847 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4848 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004849 qc_idle_timer_do_rearm(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004850
4851 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004852}
4853
4854/* The task handling the idle timeout */
4855static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4856{
4857 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004858 struct quic_counters *prx_counters = qc->prx_counters;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004859 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004860
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004861 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4862
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004863 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4864 * might free the quic-conn too early via quic_close().
4865 */
4866 qc_notify_close(qc);
4867
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004868 /* If the MUX is still alive, keep the quic-conn. The MUX is
4869 * responsible to call quic_close to release it.
4870 */
4871 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4872 if (qc->mux_state != QC_MUX_READY)
4873 quic_conn_release(qc);
4874
4875 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4876 * least clean some parts of it such as the tasklet.
4877 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004878
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004879 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
4880 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004881 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004882 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004883 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004884
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004885 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004886 return NULL;
4887}
4888
4889/* Initialize the idle timeout task for <qc>.
4890 * Returns 1 if succeeded, 0 if not.
4891 */
4892static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4893{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004894 int ret = 0;
4895
4896 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4897
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004898 qc->idle_timer_task = task_new_here();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004899 if (!qc->idle_timer_task) {
4900 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4901 goto leave;
4902 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004903
4904 qc->idle_timer_task->process = qc_idle_timer_task;
4905 qc->idle_timer_task->context = qc;
4906 qc_idle_timer_rearm(qc, 1);
4907 task_queue(qc->idle_timer_task);
4908
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004909 ret = 1;
4910 leave:
4911 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4912 return ret;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004913}
4914
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004915/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4916 * past one byte of this buffer.
4917 */
4918static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4919 struct quic_rx_packet *pkt)
4920{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004921 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004922 unsigned char dcid_len, scid_len;
4923
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004924 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
4925
4926 if (end == *buf) {
4927 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
4928 goto leave;
4929 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004930
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004931 /* Destination Connection ID Length */
4932 dcid_len = *(*buf)++;
4933 /* 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 +02004934 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
4935 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
4936 goto leave;
4937 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004938
4939 if (dcid_len) {
4940 /* Check that the length of this received DCID matches the CID lengths
4941 * of our implementation for non Initials packets only.
4942 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004943 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4944 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004945 dcid_len != QUIC_HAP_CID_LEN) {
4946 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
4947 goto leave;
4948 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004949
4950 memcpy(pkt->dcid.data, *buf, dcid_len);
4951 }
4952
4953 pkt->dcid.len = dcid_len;
4954 *buf += dcid_len;
4955
4956 /* Source Connection ID Length */
4957 scid_len = *(*buf)++;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004958 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
4959 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
4960 goto leave;
4961 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004962
4963 if (scid_len)
4964 memcpy(pkt->scid.data, *buf, scid_len);
4965 pkt->scid.len = scid_len;
4966 *buf += scid_len;
4967
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004968 ret = 1;
4969 leave:
4970 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
4971 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004972}
4973
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004974/* Insert <pkt> RX packet in its <qel> RX packets tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004975static void qc_pkt_insert(struct quic_conn *qc,
4976 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004977{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004978 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4979
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004980 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004981 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004982 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004983
4984 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004985}
4986
4987/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004988 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4989 * byte past the end of the buffer containing this packet and <beg> the address of
4990 * the packet first byte.
4991 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4992 * Returns 1 if succeeded, 0 if not.
4993 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004994static inline int qc_try_rm_hp(struct quic_conn *qc,
4995 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004996 unsigned char *buf, unsigned char *beg,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004997 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004998{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004999 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005000 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005001 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005002 struct quic_enc_level *qel;
5003 /* Only for traces. */
5004 struct quic_rx_packet *qpkt_trace;
5005
5006 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005007 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005008 /* The packet number is here. This is also the start minus
5009 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5010 * protection.
5011 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005012 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005013
5014 tel = quic_packet_type_enc_level(pkt->type);
5015 qel = &qc->els[tel];
5016
5017 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005018 /* Note that the following function enables us to unprotect the packet
5019 * number and its length subsequently used to decrypt the entire
5020 * packets.
5021 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005022 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02005023 qel->pktns->rx.largest_pn, pn, beg)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005024 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005025 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005026 }
5027
5028 /* The AAD includes the packet number field found at <pn>. */
5029 pkt->aad_len = pn - beg + pkt->pnl;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005030 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
5031 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005032 goto out;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005033 }
5034
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005035 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005036 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01005037 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01005038 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005039 /* If the packet number space has been discarded, this packet
5040 * will be not parsed.
5041 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005042 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005043 goto out;
5044 }
5045
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005046 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005047 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02005048 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
5049 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005050 }
5051
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005052 *el = qel;
5053 /* No reference counter incrementation here!!! */
5054 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
5055 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
5056 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
5057 b_add(&qc->rx.buf, pkt->len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005058
5059 ret = 1;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005060 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005061 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005062 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005063}
5064
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005065/* 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 +02005066 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
5067 * of this packet into <*version>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005068 */
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005069static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
5070 unsigned char **buf, const unsigned char *end,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005071 int *long_header, uint32_t *version)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005072{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005073 int ret = 0;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005074 const unsigned char byte0 = **buf;
5075
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005076 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5077
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005078 (*buf)++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005079 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005080 unsigned char type =
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005081 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005082
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005083 *long_header = 1;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005084 /* Version */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005085 if (!quic_read_uint32(version, (const unsigned char **)buf, end)) {
5086 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
5087 goto out;
5088 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005089
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005090 if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005091 pkt->type = type;
5092 }
5093 else {
5094 switch (type) {
5095 case 0:
5096 pkt->type = QUIC_PACKET_TYPE_RETRY;
5097 break;
5098 case 1:
5099 pkt->type = QUIC_PACKET_TYPE_INITIAL;
5100 break;
5101 case 2:
5102 pkt->type = QUIC_PACKET_TYPE_0RTT;
5103 break;
5104 case 3:
5105 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
5106 break;
5107 }
5108 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005109 }
5110 else {
5111 pkt->type = QUIC_PACKET_TYPE_SHORT;
5112 *long_header = 0;
5113 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005114
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005115 ret = 1;
5116 out:
5117 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005118 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005119}
5120
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005121/* Return the QUIC version (quic_version struct) with <version> as version number
5122 * if supported or NULL if not.
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005123 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005124static inline const struct quic_version *qc_supported_version(uint32_t version)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005125{
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005126 int i;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005127
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005128 for (i = 0; i < quic_versions_nb; i++)
5129 if (quic_versions[i].num == version)
5130 return &quic_versions[i];
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005131
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005132 return NULL;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005133}
5134
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005135/*
5136 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
5137 * address <addr>.
5138 * Implementation of RFC9000 6. Version Negotiation
5139 *
5140 * TODO implement a rate-limiting sending of Version Negotiation packets
5141 *
5142 * Returns 0 on success else non-zero
5143 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005144static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
5145 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005146{
5147 char buf[256];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005148 int ret = 0, i = 0, j;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005149 uint32_t version;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005150 const socklen_t addrlen = get_addr_len(addr);
5151
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005152 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005153 /*
5154 * header form
5155 * long header, fixed bit to 0 for Version Negotiation
5156 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005157 /* TODO: RAND_bytes() should be replaced? */
5158 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
5159 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
5160 goto out;
5161 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005162
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01005163 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005164 /* null version for Version Negotiation */
5165 buf[i++] = '\x00';
5166 buf[i++] = '\x00';
5167 buf[i++] = '\x00';
5168 buf[i++] = '\x00';
5169
5170 /* source connection id */
5171 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005172 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005173 i += pkt->scid.len;
5174
5175 /* destination connection id */
5176 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005177 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005178 i += pkt->dcid.len;
5179
5180 /* supported version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005181 for (j = 0; j < quic_versions_nb; j++) {
5182 version = htonl(quic_versions[j].num);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005183 memcpy(&buf[i], &version, sizeof(version));
5184 i += sizeof(version);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005185 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005186
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005187 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005188 goto out;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005189
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005190 ret = 1;
5191 out:
5192 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5193 return !ret;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005194}
5195
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005196/* Send a stateless reset packet depending on <pkt> RX packet information
5197 * from <fd> UDP socket to <dst>
5198 * Return 1 if succeeded, 0 if not.
5199 */
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005200static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005201 struct quic_rx_packet *rxpkt)
5202{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005203 int ret = 0, pktlen, rndlen;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005204 unsigned char pkt[64];
5205 const socklen_t addrlen = get_addr_len(dstaddr);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005206 struct proxy *prx;
5207 struct quic_counters *prx_counters;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005208
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005209 TRACE_ENTER(QUIC_EV_STATELESS_RST);
5210
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005211 prx = l->bind_conf->frontend;
5212 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005213 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
5214 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
5215 * Reset is difficult to distinguish from other packets if the recipient requires
5216 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
5217 * that all packets it sends are at least 22 bytes longer than the minimum
5218 * connection ID length that it requests the peer to include in its packets,
5219 * adding PADDING frames as necessary. This ensures that any Stateless Reset
5220 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
5221 * An endpoint that sends a Stateless Reset in response to a packet that is
5222 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
5223 * than the packet it responds to.
5224 */
5225
5226 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
5227 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
5228 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
5229 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005230
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005231 /* Put a header of random bytes */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005232 /* TODO: RAND_bytes() should be replaced */
5233 if (RAND_bytes(pkt, rndlen) != 1) {
5234 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
5235 goto leave;
5236 }
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005237
5238 /* Clear the most significant bit, and set the second one */
5239 *pkt = (*pkt & ~0x80) | 0x40;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005240 if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005241 rxpkt->dcid.data, rxpkt->dcid.len))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005242 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005243
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005244 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005245 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005246
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005247 ret = 1;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005248 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005249 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005250 leave:
5251 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
5252 return ret;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005253}
5254
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005255/* QUIC server only function.
5256 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005257 * This is the responsibility of the caller to check <aad> size is big enough
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005258 * to contain these data.
5259 * Return the number of bytes copied to <aad>.
5260 */
5261static int quic_generate_retry_token_aad(unsigned char *aad,
5262 uint32_t version,
5263 const struct quic_cid *cid,
5264 const struct sockaddr_storage *addr)
5265{
5266 unsigned char *p;
5267
5268 p = aad;
5269 memcpy(p, &version, sizeof version);
5270 p += sizeof version;
5271 p += quic_saddr_cpy(p, addr);
5272 memcpy(p, cid->data, cid->len);
5273 p += cid->len;
5274
5275 return p - aad;
5276}
5277
5278/* QUIC server only function.
5279 * Generate the token to be used in Retry packets. The token is written to
5280 * <buf> whith <len> as length. <odcid> is the original destination connection
5281 * ID and <dcid> is our side destination connection ID (or client source
5282 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005283 * Returns the length of the encoded token or 0 on error.
5284 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005285static int quic_generate_retry_token(unsigned char *buf, size_t len,
5286 const uint32_t version,
5287 const struct quic_cid *odcid,
5288 const struct quic_cid *dcid,
5289 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005290{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005291 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005292 unsigned char *p;
5293 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5294 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5295 size_t aadlen;
5296 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
5297 unsigned char key[QUIC_TLS_KEY_LEN];
5298 unsigned char iv[QUIC_TLS_IV_LEN];
5299 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5300 size_t seclen = strlen(global.cluster_secret);
5301 EVP_CIPHER_CTX *ctx = NULL;
5302 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5303 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005304
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005305 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5306
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005307 /* We copy the odcid into the token, prefixed by its one byte
5308 * length, the format token byte. It is followed by an AEAD TAG, and finally
5309 * the random bytes used to derive the secret to encrypt the token.
5310 */
5311 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005312 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005313
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005314 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005315 /* TODO: RAND_bytes() should be replaced */
5316 if (RAND_bytes(salt, sizeof salt) != 1) {
5317 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005318 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005319 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005320
5321 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 +02005322 salt, sizeof salt, sec, seclen)) {
5323 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005324 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005325 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005326
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005327 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
5328 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005329 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005330 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005331
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005332 /* Token build */
5333 p = buf;
5334 *p++ = QUIC_TOKEN_FMT_RETRY,
5335 *p++ = odcid->len;
5336 memcpy(p, odcid->data, odcid->len);
5337 p += odcid->len;
5338 write_u32(p, htonl(timestamp));
5339 p += sizeof timestamp;
5340
5341 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005342 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
5343 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005344 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005345 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005346
5347 p += QUIC_TLS_TAG_LEN;
5348 memcpy(p, salt, sizeof salt);
5349 p += sizeof salt;
5350 EVP_CIPHER_CTX_free(ctx);
5351
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005352 ret = p - buf;
5353 leave:
5354 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5355 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005356
5357 err:
5358 if (ctx)
5359 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005360 goto leave;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005361}
5362
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005363/* QUIC server only function.
5364 * Check the validity of the Retry token from <token> buffer with <tokenlen>
5365 * as length. If valid, the ODCID of <qc> QUIC connection will be put
5366 * into <odcid> connection ID. <dcid> is our side destination connection ID
5367 * of client source connection ID.
5368 * Return 1 if succeeded, 0 if not.
5369 */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005370static int quic_retry_token_check(const unsigned char *token, size_t tokenlen,
5371 const struct quic_version *qv,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005372 struct quic_cid *odcid,
5373 const struct quic_cid *dcid,
5374 struct quic_conn *qc,
5375 struct sockaddr_storage *addr)
5376{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005377 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005378 unsigned char buf[128];
5379 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5380 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5381 size_t aadlen;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005382 const unsigned char *salt;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005383 unsigned char key[QUIC_TLS_KEY_LEN];
5384 unsigned char iv[QUIC_TLS_IV_LEN];
5385 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5386 size_t seclen = strlen(global.cluster_secret);
5387 EVP_CIPHER_CTX *ctx = NULL;
5388 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5389
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005390 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5391
5392 if (sizeof buf < tokenlen) {
5393 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
5394 goto err;
5395 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005396
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005397 aadlen = quic_generate_retry_token_aad(aad, qv->num, dcid, addr);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005398 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
5399 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 +02005400 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005401 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
5402 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005403 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005404
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005405 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
5406 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005407 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005408 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005409
5410 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
5411 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 +02005412 ctx, aead, key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005413 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005414 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005415 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005416
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005417 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
5418 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005419 goto err;
5420 }
5421
5422 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005423
5424 ret = 1;
5425 leave:
5426 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5427 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005428
5429 err:
5430 if (ctx)
5431 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005432 goto leave;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005433}
5434
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005435/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
5436 * the Initial <pkt> packet.
5437 *
5438 * Returns 0 on success else non-zero.
5439 */
5440static int send_retry(int fd, struct sockaddr_storage *addr,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005441 struct quic_rx_packet *pkt, const struct quic_version *qv)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005442{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005443 int ret = 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005444 unsigned char buf[128];
5445 int i = 0, token_len;
5446 const socklen_t addrlen = get_addr_len(addr);
5447 struct quic_cid scid;
5448
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005449 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5450
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005451 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
5452 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
5453 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005454 /* version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005455 buf[i++] = *((unsigned char *)&qv->num + 3);
5456 buf[i++] = *((unsigned char *)&qv->num + 2);
5457 buf[i++] = *((unsigned char *)&qv->num + 1);
5458 buf[i++] = *(unsigned char *)&qv->num;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005459
5460 /* Use the SCID from <pkt> for Retry DCID. */
5461 buf[i++] = pkt->scid.len;
5462 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
5463 i += pkt->scid.len;
5464
5465 /* Generate a new CID to be used as SCID for the Retry packet. */
5466 scid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005467 /* TODO: RAND_bytes() should be replaced */
5468 if (RAND_bytes(scid.data, scid.len) != 1) {
5469 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
5470 goto out;
5471 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005472
5473 buf[i++] = scid.len;
5474 memcpy(&buf[i], scid.data, scid.len);
5475 i += scid.len;
5476
5477 /* token */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005478 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005479 &pkt->dcid, &pkt->scid, addr))) {
5480 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
5481 goto out;
5482 }
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01005483
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005484 i += token_len;
5485
5486 /* token integrity tag */
5487 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
5488 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005489 pkt->dcid.len, buf, i, qv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005490 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5491 goto out;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005492 }
5493
5494 i += QUIC_TLS_TAG_LEN;
5495
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005496 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
5497 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5498 goto out;
5499 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005500
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005501 ret = 1;
5502 out:
5503 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5504 return !ret;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005505}
5506
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005507/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5508 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5509 * concatenated to the <pkt> DCID field.
5510 *
5511 * Returns the instance or NULL if not found.
5512 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005513static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005514 struct listener *l,
5515 struct sockaddr_storage *saddr)
5516{
5517 struct quic_conn *qc = NULL;
5518 struct ebmb_node *node;
5519 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005520 /* set if the quic_conn is found in the second DCID tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005521
5522 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005523
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005524 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5525 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5526 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5527 /* DCIDs of first packets coming from multiple clients may have
5528 * the same values. Let's distinguish them by concatenating the
5529 * socket addresses.
5530 */
5531 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005532 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005533 pkt->dcid.len + pkt->dcid.addrlen);
5534 if (node) {
5535 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5536 goto end;
5537 }
5538 }
5539
5540 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5541 * also for INITIAL/0-RTT non-first packets with the final DCID in
5542 * used.
5543 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005544 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005545 if (!node)
5546 goto end;
5547
5548 id = ebmb_entry(node, struct quic_connection_id, node);
5549 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005550
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005551 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5552 * If already done, this is a noop.
5553 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005554 if (qc)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005555 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005556
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005557 end:
5558 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005559 return qc;
5560}
5561
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005562/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5563 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5564 * parameters of this session.
5565 * This is the responsibility of the caller to check the validity of all the
5566 * pointers passed as parameter to this function.
5567 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5568 * CO_ER_SSL_NO_MEM.
5569 */
5570static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5571 unsigned char *params, size_t params_len)
5572{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005573 int retry, ret = -1;
5574
5575 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005576
5577 retry = 1;
5578 retry:
5579 *ssl = SSL_new(ssl_ctx);
5580 if (!*ssl) {
5581 if (!retry--)
5582 goto err;
5583
5584 pool_gc(NULL);
5585 goto retry;
5586 }
5587
5588 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005589 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005590 SSL_free(*ssl);
5591 *ssl = NULL;
5592 if (!retry--)
5593 goto err;
5594
5595 pool_gc(NULL);
5596 goto retry;
5597 }
5598
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005599 ret = 0;
5600 leave:
5601 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5602 return ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005603
5604 err:
5605 qc->conn->err_code = CO_ER_SSL_NO_MEM;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005606 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005607}
5608
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005609/* Finalize <qc> QUIC connection:
5610 * - initialize the Initial QUIC TLS context for negotiated version,
5611 * - derive the secrets for this context,
5612 * - encode the transport parameters to be sent,
5613 * - set them into the TLS stack,
5614 * - initialize ->max_ack_delay and max_idle_timeout,
5615 *
5616 * MUST be called after having received the remote transport parameters.
5617 * Return 1 if succeeded, 0 if not.
5618 */
5619int qc_conn_finalize(struct quic_conn *qc, int server)
5620{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005621 int ret = 0;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005622 struct quic_transport_params *tx_tp = &qc->tx.params;
5623 struct quic_transport_params *rx_tp = &qc->rx.params;
5624 const struct quic_version *ver;
5625
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005626 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5627
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005628 if (tx_tp->version_information.negotiated_version &&
5629 tx_tp->version_information.negotiated_version != qc->original_version) {
5630 qc->negotiated_version =
5631 qc->tx.params.version_information.negotiated_version;
5632 if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
5633 qc->odcid.data, qc->odcid.len, !server))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005634 goto out;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005635
5636 ver = qc->negotiated_version;
5637 }
5638 else {
5639 ver = qc->original_version;
5640 }
5641
5642 qc->enc_params_len =
5643 quic_transport_params_encode(qc->enc_params,
5644 qc->enc_params + sizeof qc->enc_params,
5645 &qc->rx.params, ver, 1);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005646 if (!qc->enc_params_len) {
5647 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_TXPKT);
5648 goto out;
5649 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005650
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005651 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
5652 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_TXPKT);
5653 goto out;
5654 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005655
5656 if (tx_tp->max_ack_delay)
5657 qc->max_ack_delay = tx_tp->max_ack_delay;
5658
5659 if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout)
5660 qc->max_idle_timeout =
5661 QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5662 else
5663 qc->max_idle_timeout =
5664 QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5665
5666 TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp);
5667
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005668 ret = 1;
5669 out:
5670 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5671 return ret;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005672}
5673
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005674/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5675 * used to process <qc> received packets. The allocated context is stored in
5676 * <qc.xprt_ctx>.
5677 *
5678 * Returns 0 on success else non-zero.
5679 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005680static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005681{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005682 int ret = 0;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005683 struct bind_conf *bc = qc->li->bind_conf;
5684 struct ssl_sock_ctx *ctx = NULL;
5685
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005686 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5687
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005688 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005689 if (!ctx) {
5690 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005691 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005692 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005693
5694 ctx->wait_event.tasklet = tasklet_new();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005695 if (!ctx->wait_event.tasklet) {
5696 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005697 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005698 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005699
5700 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5701 ctx->wait_event.tasklet->context = ctx;
5702 ctx->wait_event.events = 0;
5703 ctx->subs = NULL;
5704 ctx->xprt_ctx = NULL;
5705 ctx->qc = qc;
5706
5707 /* Set tasklet tid based on the SCID selected by us for this
5708 * connection. The upper layer will also be binded on the same thread.
5709 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005710 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005711
5712 if (qc_is_listener(qc)) {
5713 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5714 qc->enc_params, qc->enc_params_len) == -1) {
5715 goto err;
5716 }
5717
5718 /* Enabling 0-RTT */
5719 if (bc->ssl_conf.early_data)
5720 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5721
5722 SSL_set_accept_state(ctx->ssl);
5723 }
5724
5725 ctx->xprt = xprt_get(XPRT_QUIC);
5726
5727 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005728 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005729
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005730 ret = 1;
5731 leave:
5732 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5733 return !ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005734
5735 err:
5736 if (ctx && ctx->wait_event.tasklet)
5737 tasklet_free(ctx->wait_event.tasklet);
5738 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005739 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005740}
5741
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005742/* Check that all the bytes between <buf> included and <end> address
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005743 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005744 * check that there is at least one byte between <buf> end <end>.
5745 * Return 1 if this all the bytes are null, 0 if not.
5746 */
5747static inline int quic_padding_check(const unsigned char *buf,
5748 const unsigned char *end)
5749{
5750 while (buf < end && !*buf)
5751 buf++;
5752
5753 return buf == end;
5754}
5755
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005756/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5757 * end of this buffer past one byte and populate <pkt> RX packet structure
5758 * with the information collected from the packet.
5759 * This function sets ->len <pkt> field value to the end of the packet past one
5760 * byte to enable the caller to run this function again to continue to parse
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005761 * the remaining QUIC packets carried by the datagram.
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005762 * Note that this function always sets this ->len value. If a paquet could
5763 * not be correctly found, ->len value will be set to the remaining number
5764 * bytes in the datagram to entirely consume this latter.
5765 */
5766static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5767 struct quic_rx_packet *pkt, int first_pkt,
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02005768 struct quic_dgram *dgram, struct list **tasklist_head)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005769{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005770 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005771 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005772 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005773 struct proxy *prx;
5774 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005775 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005776 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005777 size_t b_cspace;
5778 struct quic_enc_level *qel;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005779 uint32_t version;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005780 const struct quic_version *qv = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005781
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005782 TRACE_ENTER(QUIC_EV_CONN_LPKT);
5783
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005784 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005785 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005786 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005787 qel = NULL;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005788 l = dgram->owner;
5789 prx = l->bind_conf->frontend;
5790 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005791 /* This ist only to please to traces and distinguish the
5792 * packet with parsed packet number from others.
5793 */
5794 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005795 if (end <= buf) {
5796 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5797 goto drop;
5798 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005799
5800 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005801 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005802 if (!first_pkt && quic_padding_check(buf, end)) {
5803 /* Some browsers may pad the remaining datagram space with null bytes.
5804 * That is what we called add padding out of QUIC packets. Such
5805 * datagrams must be considered as valid. But we can only consume
5806 * the remaining space.
5807 */
5808 pkt->len = end - buf;
5809 goto drop_no_conn;
5810 }
5811
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005812 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005813 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005814 }
5815
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005816 /* Header form */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005817 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005818 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5819 goto drop;
5820 }
5821
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005822 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005823 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005824 struct quic_cid odcid;
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02005825 const struct quic_cid *token_odcid = NULL; // ODCID received from client token
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005826
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005827 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005828 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005829 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005830 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005831 }
5832
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005833 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5834 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005835 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005836 }
5837 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5838 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005839 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02005840 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
Frédéric Lécaillee7df68a2022-08-05 09:34:44 +02005841 goto drop;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005842 }
5843
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005844 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5845 * they must have the same DCID.
5846 */
5847 if (!first_pkt &&
5848 (pkt->dcid.len != dgram->dcid_len ||
5849 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5850 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005851 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005852 }
5853
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005854 /* Retry of Version Negotiation packets are only sent by servers */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005855 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005856 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005857 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005858 }
5859
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005860 /* RFC9000 6. Version Negotiation */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005861 qv = qc_supported_version(version);
5862 if (!qv) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005863 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005864 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005865 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005866 goto err;
5867 }
5868
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005869 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005870 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005871 }
5872
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005873 /* For Initial packets, and for servers (QUIC clients connections),
5874 * there is no Initial connection IDs storage.
5875 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005876 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005877 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005878
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005879 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5880 end - buf < token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005881 TRACE_PROTO("Packet dropped",
5882 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005883 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005884 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005885
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005886 /* TODO Retry should be automatically activated if
5887 * suspect network usage is detected.
5888 */
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005889 if (global.cluster_secret && !token_len) {
5890 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
5891 TRACE_PROTO("Initial without token, sending retry",
5892 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
5893 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
5894 TRACE_PROTO("Error during Retry generation",
5895 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005896 goto err;
5897 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005898
5899 HA_ATOMIC_INC(&prx_counters->retry_sent);
5900 goto err;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005901 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005902 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005903 else if (!global.cluster_secret && token_len) {
5904 /* Impossible case: a token was received without configured
5905 * cluster secret.
5906 */
5907 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
5908 NULL, NULL, NULL, qv);
5909 goto drop;
5910 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005911
5912 pkt->token = buf;
5913 pkt->token_len = token_len;
5914 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005915 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005916 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005917 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005918 TRACE_PROTO("Packet dropped",
5919 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005920 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005921 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005922 }
5923
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005924 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5925 end - buf < len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005926 TRACE_PROTO("Packet dropped",
5927 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005928 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005929 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005930
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005931 payload = buf;
5932 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005933 if (drop_no_conn)
5934 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005935
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005936 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005937 if (global.cluster_secret && pkt->token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005938 if (*pkt->token == QUIC_TOKEN_FMT_RETRY) {
5939 const struct quic_version *ver = qc ? qc->original_version : qv;
5940 if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid,
5941 &pkt->scid, qc, &dgram->saddr)) {
5942 HA_ATOMIC_INC(&prx_counters->retry_error);
5943 TRACE_PROTO("Wrong retry token",
5944 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5945 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
5946 * with an INVALID_TOKEN error.
5947 */
5948 goto drop;
5949 }
5950
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005951 token_odcid = &odcid;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005952 HA_ATOMIC_INC(&prx_counters->retry_validated);
5953 }
5954 else {
5955 /* TODO: New token check */
5956 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5957 goto drop;
5958 }
5959 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005960 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005961 int ipv4;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005962 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005963
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005964 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005965 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005966 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005967 }
5968
Willy Tarreau787e92a2022-05-20 16:06:01 +02005969 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 +02005970 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005971 TRACE_PROTO("Initial without token, sending retry",
5972 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005973 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005974 TRACE_ERROR("Error during Retry generation",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005975 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005976 goto err;
5977 }
5978
5979 HA_ATOMIC_INC(&prx_counters->retry_sent);
5980 goto err;
5981 }
5982
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005983 /* RFC 9000 7.2. Negotiating Connection IDs:
5984 * When an Initial packet is sent by a client that has not previously
5985 * received an Initial or Retry packet from the server, the client
5986 * populates the Destination Connection ID field with an unpredictable
5987 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5988 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01005989 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005990 TRACE_PROTO("dropped packet",
5991 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005992 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01005993 }
5994
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005995 pkt->saddr = dgram->saddr;
5996 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005997
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005998 qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, token_odcid,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02005999 &pkt->saddr, 1, !!pkt->token_len, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02006000 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006001 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006002
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006003 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02006004 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01006005 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01006006 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006007
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01006008 /* If the insertion failed, it means that another
6009 * thread has already allocated a QUIC connection for
6010 * the same CID. Liberate our allocated connection.
6011 */
6012 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01006013 qc_to_purge = qc;
6014
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01006015 qc = ebmb_entry(n, struct quic_conn, odcid_node);
6016 pkt->qc = qc;
6017 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01006018
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01006019 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006020 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01006021 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006022 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01006023 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01006024 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01006025 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006026 }
6027 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006028 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006029 }
6030 }
6031 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006032 TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006033 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006034 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006035 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006036 }
6037
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006038 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006039 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006040
6041 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6042 * they must have the same DCID.
6043 */
6044 if (!first_pkt &&
6045 (pkt->dcid.len != dgram->dcid_len ||
6046 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
6047 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006048 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006049 }
6050
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006051 buf += QUIC_HAP_CID_LEN;
6052
6053 /* A short packet is the last one of a UDP datagram. */
6054 payload = buf;
6055 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006056
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006057 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006058 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006059 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006060 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006061 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006062 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006063 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006064 }
6065
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006066 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006067 }
6068
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006069 if (qc->flags & QUIC_FL_CONN_CLOSING) {
6070 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
6071 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
6072 qc->nb_pkt_for_cc++;
6073 qc->nb_pkt_since_cc = 0;
6074 }
6075 /* Skip the entire datagram */
6076 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006077 TRACE_STATE("Closing state connection",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006078 QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006079 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006080 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006081
6082 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6083 * they must have the same DCID.
6084 *
6085 * This check must be done after the final update to pkt.len to
6086 * properly drop the packet on failure.
6087 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006088 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006089 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006090 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006091 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006092 /* Reset the anti-amplification bit. It will be set again
6093 * when sending the next packet if reached again.
6094 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006095 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
6096 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006097 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006098 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006099
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006100 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006101
Amaury Denoyelle72d86502022-07-13 15:08:23 +02006102 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006103 TRACE_PROTO("Connection error",
6104 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006105 goto out;
6106 }
6107
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006108 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006109 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006110 b_cspace = b_contig_space(&qc->rx.buf);
6111 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006112 /* Do not consume buf if space not at the end. */
6113 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006114 TRACE_PROTO("Packet dropped",
6115 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006116 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6117 goto drop_no_conn;
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006118 }
6119
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006120 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006121 if (b_cspace) {
6122 b_putchr(&qc->rx.buf, 0x00);
6123 b_cspace--;
6124 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006125 b_add(&qc->rx.buf, b_cspace);
6126 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006127 TRACE_PROTO("Too big packet",
6128 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006129 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6130 goto drop_no_conn;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006131 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006132 }
6133
Frédéric Lécaille99897d12022-08-08 10:28:07 +02006134 if (!qc_try_rm_hp(qc, pkt, payload, beg, &qel)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006135 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006136 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02006137 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006138
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006139 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006140 if (pkt->aad_len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006141 qc_pkt_insert(qc, pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006142 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02006143 /* Wake up the connection packet handler task from here only if all
6144 * the contexts have been initialized, especially the mux context
6145 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
6146 * will start it if these contexts for the connection are not already
6147 * initialized.
6148 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006149 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006150 if (conn_ctx)
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02006151 *tasklist_head = tasklet_wakeup_after(*tasklist_head, conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02006152
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006153 drop_no_conn:
6154 if (drop_no_conn)
6155 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006156 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006157
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006158 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006159
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006160 drop:
6161 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006162 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006163 /* Wakeup the I/O handler callback if the PTO timer must be armed.
6164 * This cannot be done by this thread.
6165 */
6166 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006167 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006168 if (conn_ctx && conn_ctx->wait_event.tasklet)
6169 tasklet_wakeup(conn_ctx->wait_event.tasklet);
6170 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01006171 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006172 if (!pkt->len)
6173 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006174 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006175}
6176
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006177/* This function builds into <buf> buffer a QUIC long packet header.
6178 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006179 */
6180static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006181 int type, size_t pn_len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006182 struct quic_conn *qc, const struct quic_version *ver)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006183{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006184 int ret = 0;
6185
6186 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6187
6188 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
6189 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6190 goto leave;
6191 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006192
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006193 type = quic_pkt_type(type, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006194 /* #0 byte flags */
6195 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
6196 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
6197 /* Version */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006198 quic_write_uint32(buf, end, ver->num);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006199 *(*buf)++ = qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006200 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006201 if (qc->dcid.len) {
6202 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6203 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006204 }
6205 /* Source connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006206 *(*buf)++ = qc->scid.len;
6207 if (qc->scid.len) {
6208 memcpy(*buf, qc->scid.data, qc->scid.len);
6209 *buf += qc->scid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006210 }
6211
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006212 ret = 1;
6213 leave:
6214 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6215 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006216}
6217
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006218/* This function builds into <buf> buffer a QUIC short packet header.
6219 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006220 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006221static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006222 size_t pn_len, struct quic_conn *qc,
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006223 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006224{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006225 int ret = 0;
6226
6227 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6228
6229 if (end - *buf < 1 + qc->dcid.len) {
6230 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6231 goto leave;
6232 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006233
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006234 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01006235 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
6236 ((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 +01006237 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006238 if (qc->dcid.len) {
6239 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6240 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006241 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006242
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006243 ret = 1;
6244 leave:
6245 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6246 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006247}
6248
6249/* Apply QUIC header protection to the packet with <buf> as first byte address,
6250 * <pn> as address of the Packet number field, <pnlen> being this field length
6251 * with <aead> as AEAD cipher and <key> as secret key.
6252 * Returns 1 if succeeded or 0 if failed.
6253 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006254static int quic_apply_header_protection(struct quic_conn *qc,
6255 unsigned char *buf, unsigned char *pn, size_t pnlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006256 const EVP_CIPHER *aead, const unsigned char *key)
6257{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006258 int i, outlen, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006259 EVP_CIPHER_CTX *ctx;
6260 /* We need an IV of at least 5 bytes: one byte for bytes #0
6261 * and at most 4 bytes for the packet number
6262 */
6263 unsigned char mask[5] = {0};
6264
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006265 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6266
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006267 ctx = EVP_CIPHER_CTX_new();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006268 if (!ctx) {
6269 TRACE_ERROR("cipher context allocation failed", QUIC_EV_CONN_TXPKT, qc);
6270 goto out;
6271 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006272
6273 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
6274 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006275 !EVP_EncryptFinal_ex(ctx, mask, &outlen)) {
6276 TRACE_ERROR("cipher context allocation failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006277 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006278 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006279
6280 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
6281 for (i = 0; i < pnlen; i++)
6282 pn[i] ^= mask[i + 1];
6283
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006284 EVP_CIPHER_CTX_free(ctx);
6285
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006286 ret = 1;
6287 out:
6288 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006289 return ret;
6290}
6291
6292/* Reduce the encoded size of <ack_frm> ACK frame removing the last
6293 * ACK ranges if needed to a value below <limit> in bytes.
6294 * Return 1 if succeeded, 0 if not.
6295 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006296static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
6297 struct quic_frame *ack_frm, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006298{
6299 size_t room, ack_delay_sz;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006300 int ret = 0;
6301
6302 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006303
6304 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
6305 /* A frame is made of 1 byte for the frame type. */
6306 room = limit - ack_delay_sz - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006307 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
6308 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006309
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006310 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
6311 leave:
6312 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6313 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006314}
6315
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006316/* Prepare into <outlist> as most as possible ack-eliciting frame from their
6317 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
6318 * with <room> as available room, and <*len> the packet Length field initialized
6319 * with the number of bytes already present in this buffer which must be taken
6320 * into an account for the Length packet field value. <headlen> is the number of
6321 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006322 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006323 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006324 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006325 * 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 +01006326 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006327static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006328 size_t room, size_t *len, size_t headlen,
6329 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006330 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006331{
Frédéric Lécailleea604992020-12-24 13:01:37 +01006332 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006333 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006334
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006335 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
6336
Frédéric Lécailleea604992020-12-24 13:01:37 +01006337 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006338 if (*len > room)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006339 goto leave;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006340
Frédéric Lécailleea604992020-12-24 13:01:37 +01006341 /* If we are not probing we must take into an account the congestion
6342 * control window.
6343 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006344 if (!qel->pktns->tx.pto_probe) {
6345 size_t remain = quic_path_prep_data(qc->path);
6346
6347 if (headlen > remain)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006348 goto leave;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006349
6350 room = QUIC_MIN(room, remain - headlen);
6351 }
6352
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006353 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006354 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006355
6356 /* NOTE: switch/case block inside a loop, a successful status must be
6357 * returned by this function only if at least one frame could be built
6358 * in the switch/case block.
6359 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006360 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006361 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006362 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006363
Frédéric Lécailleea604992020-12-24 13:01:37 +01006364 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006365 break;
6366
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006367 switch (cf->type) {
6368 case QUIC_FT_CRYPTO:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006369 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006370 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006371 /* Compute the length of this CRYPTO frame header */
6372 hlen = 1 + quic_int_getsize(cf->crypto.offset);
6373 /* Compute the data length of this CRyPTO frame. */
6374 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006375 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006376 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006377 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006378 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006379
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006380 /* CRYPTO frame length. */
6381 flen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006382 TRACE_DEVEL(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006383 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006384 /* Add the CRYPTO data length and its encoded length to the packet
6385 * length and the length of this length.
6386 */
6387 *len += flen;
6388 room -= flen;
6389 if (dlen == cf->crypto.len) {
6390 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006391 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006392 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006393 }
6394 else {
6395 struct quic_frame *new_cf;
6396
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006397 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006398 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006399 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006400 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006401 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006402
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006403 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006404 new_cf->type = QUIC_FT_CRYPTO;
6405 new_cf->crypto.len = dlen;
6406 new_cf->crypto.offset = cf->crypto.offset;
6407 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006408 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006409 /* Consume <dlen> bytes of the current frame. */
6410 cf->crypto.len -= dlen;
6411 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006412 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006413 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006414
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006415 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006416 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
6417 struct eb64_node *node = NULL;
6418 struct qc_stream_desc *stream_desc = NULL;
6419 struct quic_stream *strm = &cf->stream;
6420
6421 /* As this frame has been already lost, ensure the stream is always
6422 * available or the range of this frame is not consumed before
6423 * resending it.
6424 */
6425 node = eb64_lookup(&qc->streams_by_id, strm->id);
6426 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006427 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006428 LIST_DELETE(&cf->list);
6429 pool_free(pool_head_quic_frame, cf);
6430 continue;
6431 }
6432
6433 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
6434 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006435 TRACE_DEVEL("ignored frame frame in already acked range",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006436 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006437 LIST_DELETE(&cf->list);
6438 pool_free(pool_head_quic_frame, cf);
6439 continue;
6440 }
6441 else if (strm->offset.key < stream_desc->ack_offset) {
6442 strm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006443 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006444 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006445 }
6446 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006447 /* Note that these frames are accepted in short packets only without
6448 * "Length" packet field. Here, <*len> is used only to compute the
6449 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01006450 *
6451 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006452 * excepting the variable ones. Note that +1 is for the type of this frame.
6453 */
6454 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006455 ((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 +02006456 /* Compute the data length of this STREAM frame. */
6457 avail_room = room - hlen - *len;
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006458 if ((ssize_t)avail_room <= 0)
6459 continue;
6460
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006461 TRACE_DEVEL(" New STREAM frame build (room, len)",
Frédéric Lécailleadc76412022-08-08 16:09:46 +02006462 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006463 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
6464 dlen = max_available_room(avail_room, &dlen_sz);
6465 if (dlen > cf->stream.len) {
6466 dlen = cf->stream.len;
6467 }
6468 dlen_sz = quic_int_getsize(dlen);
6469 flen = hlen + dlen_sz + dlen;
6470 }
6471 else {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02006472 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006473 flen = hlen + dlen;
6474 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006475 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006476 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006477 TRACE_DEVEL(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006478 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006479 /* Add the STREAM data length and its encoded length to the packet
6480 * length and the length of this length.
6481 */
6482 *len += flen;
6483 room -= flen;
6484 if (dlen == cf->stream.len) {
6485 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006486 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006487 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006488
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006489 /* Do not notify MUX on retransmission. */
6490 if (!(qc->flags & (QUIC_FL_CONN_RETRANS_LOST_DATA|QUIC_FL_CONN_RETRANS_OLD_DATA))) {
6491 BUG_ON(qc->mux_state != QC_MUX_READY); /* MUX must be the caller if not on retransmission. */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006492 qcc_streams_sent_done(cf->stream.stream->ctx,
6493 cf->stream.len,
6494 cf->stream.offset.key);
6495 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006496 }
6497 else {
6498 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006499 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006500
6501 new_cf = pool_zalloc(pool_head_quic_frame);
6502 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006503 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006504 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006505 }
6506
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006507 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006508 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006509 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006510 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006511 new_cf->stream.id = cf->stream.id;
6512 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
6513 new_cf->stream.offset = cf->stream.offset;
6514 new_cf->stream.len = dlen;
6515 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
6516 /* FIN bit reset */
6517 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
6518 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006519 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006520 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
6521 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006522 cf_buf = b_make(b_orig(cf->stream.buf),
6523 b_size(cf->stream.buf),
6524 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006525 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006526 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006527 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006528
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006529 /* Do not notify MUX on retransmission. */
6530 if (!(qc->flags & (QUIC_FL_CONN_RETRANS_LOST_DATA|QUIC_FL_CONN_RETRANS_OLD_DATA))) {
6531 BUG_ON(qc->mux_state != QC_MUX_READY); /* MUX must be the caller if not on retransmission. */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006532 qcc_streams_sent_done(new_cf->stream.stream->ctx,
6533 new_cf->stream.len,
6534 new_cf->stream.offset.key);
6535 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006536 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006537
6538 /* TODO the MUX is notified about the frame sending via
6539 * previous qcc_streams_sent_done call. However, the
6540 * sending can fail later, for example if the sendto
6541 * system call returns an error. As the MUX has been
6542 * notified, the transport layer is responsible to
6543 * bufferize and resent the announced data later.
6544 */
6545
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006546 break;
6547
6548 default:
6549 flen = qc_frm_len(cf);
6550 BUG_ON(!flen);
6551 if (flen > room)
6552 continue;
6553
6554 *len += flen;
6555 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006556 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006557 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006558 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006559 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006560
6561 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01006562 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006563 }
6564
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006565 leave:
6566 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécailleea604992020-12-24 13:01:37 +01006567 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006568}
6569
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006570/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
6571 * is used as return parameter and should be zero'ed by the caller.
6572 */
6573static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
6574 struct quic_frame *out)
6575{
6576 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
6577 *
6578 * A CONNECTION_CLOSE frame should be sent in several packets with
6579 * different encryption levels depending on the client context. This is
6580 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
6581 * more details on how to implement it.
6582 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006583 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
6584
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006585
6586 if (qc->err.app) {
6587 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
6588 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
6589 /* RFC 9000 10.2.3. Immediate Close during the Handshake
6590 *
6591 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
6592 * packet could expose application state or be used to alter application
6593 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
6594 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
6595 * Handshake packets. Otherwise, information about the application
6596 * state might be revealed. Endpoints MUST clear the value of the
6597 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
6598 * converting to a CONNECTION_CLOSE of type 0x1c.
6599 */
6600 out->type = QUIC_FT_CONNECTION_CLOSE;
6601 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
6602 out->connection_close.reason_phrase_len = 0;
6603 }
6604 else {
6605 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
6606 out->connection_close.error_code = qc->err.code;
6607 }
6608 }
6609 else {
6610 out->type = QUIC_FT_CONNECTION_CLOSE;
6611 out->connection_close.error_code = qc->err.code;
6612 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006613 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
6614
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006615}
6616
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006617/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006618 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
6619 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006620 * filling the buffer with as much frames as possible from <frms> list of
6621 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006622 * 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 +02006623 * 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 +02006624 * having returned from this function.
6625 * 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 +01006626 * number field in this packet. <pn_len> will also have the packet number
6627 * length as value.
6628 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006629 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006630 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006631static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
6632 size_t dglen, struct quic_tx_packet *pkt,
6633 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006634 int force_ack, int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006635 struct quic_enc_level *qel, struct quic_conn *qc,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006636 const struct quic_version *ver, struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006637{
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006638 unsigned char *beg, *payload;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006639 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006640 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
6641 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006642 struct quic_frame cc_frm = { };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006643 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006644 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006645 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006646 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006647 struct quic_frame *cf;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006648 int must_ack, ret = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006649 int nb_aepkts_since_last_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006650
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006651 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6652
Frédéric Lécailleea604992020-12-24 13:01:37 +01006653 /* Length field value with CRYPTO frames if present. */
6654 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006655 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006656 /* When not probing, and no immediate close is required, reduce the size of this
6657 * buffer to respect the congestion controller window.
6658 * 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 +01006659 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006660 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006661 size_t path_room;
6662
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006663 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006664 if (end - beg > path_room)
6665 end = beg + path_room;
6666 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006667
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006668 /* Ensure there is enough room for the TLS encryption tag and a zero token
6669 * length field if any.
6670 */
6671 if (end - pos < QUIC_TLS_TAG_LEN +
6672 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
6673 goto no_room;
6674
6675 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006676 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006677 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006678 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006679 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006680 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006681 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006682 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006683 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006684 goto no_room;
6685
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006686 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006687 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006688 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006689 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006690 /* Build an ACK frame if required. */
6691 ack_frm_len = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006692 nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
6693 must_ack = !qel->pktns->tx.pto_probe &&
6694 (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
6695 (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK)));
6696 if (force_ack || must_ack) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006697 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006698 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006699 ack_frm.tx_ack.arngs = arngs;
6700 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6701 qel->pktns->tx.ack_delay =
6702 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6703 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6704 }
6705 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006706 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6707 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6708 * that from here, we do not know if we will have to send a PING frame.
6709 * This will be decided after having computed the ack-eliciting frames
6710 * to be added to this packet.
6711 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006712 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 +01006713 if (!ack_frm_len)
6714 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006715 }
6716
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006717 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006718 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006719 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006720 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006721 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006722
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006723 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006724 /* Initialize the length of the frames built below to <len>.
6725 * If any frame could be successfully built by qc_build_frms(),
6726 * we will have len_frms > len.
6727 */
6728 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006729 if (!qc_build_frms(&frm_list, frms,
6730 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006731 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006732 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006733 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6734 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006735 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006736 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006737
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006738 /* Length (of the remaining data). Must not fail because, the buffer size
6739 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6740 * for the encryption tag. It must be taken into an account for the length
6741 * of this packet.
6742 */
6743 if (len_frms)
6744 len = len_frms + QUIC_TLS_TAG_LEN;
6745 else
6746 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006747 /* CONNECTION_CLOSE frame */
6748 if (cc) {
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006749 qc_build_cc_frm(qc, qel, &cc_frm);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006750 len += qc_frm_len(&cc_frm);
6751 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006752 add_ping_frm = 0;
6753 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006754 len_sz = quic_int_getsize(len);
6755 /* Add this packet size to <dglen> */
6756 dglen += head_len + len_sz + len;
6757 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6758 /* This is a maximum padding size */
6759 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6760 /* The length field value is of this packet is <len> + <padding_len>
6761 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006762 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006763 * sizes from <padding_len>.
6764 */
6765 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6766 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006767 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006768 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006769 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006770 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006771 add_ping_frm = 1;
6772 len += 1;
6773 }
6774 /* 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 +01006775 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006776 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6777 }
6778
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006779 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6780 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006781
6782 /* Packet number field address. */
6783 *buf_pn = pos;
6784
6785 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006786 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6787 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006788
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006789 /* payload building (ack-eliciting or not frames) */
6790 payload = pos;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006791 if (ack_frm_len) {
6792 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6793 goto no_room;
6794
6795 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006796 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006797 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006798
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006799 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006800 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006801 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006802 unsigned char *spos = pos;
6803
6804 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006805 ssize_t room = end - pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006806 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006807 qc, NULL, NULL, &room);
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006808 /* TODO: this should not have happened except if we
6809 * are limited by the congestion control.
6810 * Note that <cf> was added from <frm_list> to <frms> list by
6811 * qc_build_frms().
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006812 */
6813 LIST_DELETE(&cf->list);
6814 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006815 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006816 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006817
6818 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006819 quic_tx_packet_refinc(pkt);
6820 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006821 }
6822 }
6823
6824 /* Build a PING frame if needed. */
6825 if (add_ping_frm) {
6826 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006827 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006828 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006829 }
6830
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006831 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006832 if (cc) {
6833 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6834 goto no_room;
6835
6836 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6837 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006838
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006839 /* Build a PADDING frame if needed. */
6840 if (padding_len) {
6841 frm.type = QUIC_FT_PADDING;
6842 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006843 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006844 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006845 }
6846
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006847 if (pos == payload) {
6848 /* No payload was built because of congestion control */
6849 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
6850 goto no_room;
6851 }
6852
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006853 /* If this packet is ack-eliciting and we are probing let's
6854 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006855 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006856 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6857 qel->pktns->tx.pto_probe)
6858 qel->pktns->tx.pto_probe--;
6859
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006860 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006861 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006862
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006863 ret = 1;
6864 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
6865 leave:
6866 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6867 return ret;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006868
6869 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006870 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006871 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006872 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
6873 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006874}
6875
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006876static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006877{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006878 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006879 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006880 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006881 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006882 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006883 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006884 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006885 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006886 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006887 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006888}
6889
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006890/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006891 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6892 * of prebuilt frames.
6893 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006894 * Return -2 if the packet could not be allocated or encrypted for any reason,
6895 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006896 * XXX NOTE XXX
6897 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6898 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6899 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006900 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006901static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6902 const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006903 struct quic_enc_level *qel,
6904 struct quic_tls_ctx *tls_ctx, struct list *frms,
6905 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006906 size_t dglen, int pkt_type, int force_ack,
6907 int padding, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006908{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006909 struct quic_tx_packet *ret_pkt = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006910 /* The pointer to the packet number field. */
6911 unsigned char *buf_pn;
6912 unsigned char *beg, *end, *payload;
6913 int64_t pn;
6914 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006915 struct quic_tx_packet *pkt;
6916
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006917 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006918 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006919 pkt = pool_alloc(pool_head_quic_tx_packet);
6920 if (!pkt) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006921 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006922 *err = -2;
6923 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006924 }
6925
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006926 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006927 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006928 pn_len = 0;
6929 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006930
6931 pn = qel->pktns->tx.next_pn + 1;
6932 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 +02006933 force_ack, padding, cc, probe, qel, qc, ver, frms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006934 // trace already emitted by function above
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006935 *err = -1;
6936 goto err;
6937 }
6938
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006939 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006940 payload = buf_pn + pn_len;
6941 payload_len = end - payload;
6942 aad_len = payload - beg;
6943
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006944 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006945 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006946 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006947 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006948 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006949
6950 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006951 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006952 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006953 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006954 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006955 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006956 goto err;
6957 }
6958
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006959 /* Consume a packet number */
6960 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006961 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006962 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006963 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006964 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
6965 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006966 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6967 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006968 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006969 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006970 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006971 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006972 pkt->in_flight_len = pkt->len;
6973 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006974 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01006975 /* Always reset this flags */
6976 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006977 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
6978 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
6979 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
6980 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006981
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006982 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006983
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006984 ret_pkt = pkt;
6985 leave:
6986 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt);
6987 return ret_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006988
6989 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006990 /* TODO: what about the frames which have been built
6991 * for this packet.
6992 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006993 free_quic_tx_packet(qc, pkt);
6994 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006995}
6996
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006997
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006998/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6999 * event subscriber <es> is not allowed to change from a previous call as long
7000 * as at least one event is still subscribed. The <event_type> must only be a
7001 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
7002 */
7003static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7004{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007005 struct quic_conn *qc = conn->handle.qc;
7006 struct qcc *qcc = qc->qcc;
7007
7008 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007009
7010 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
7011 BUG_ON(qcc->subs && qcc->subs != es);
7012
7013 es->events |= event_type;
7014 qcc->subs = es;
7015
7016 if (event_type & SUB_RETRY_RECV)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007017 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007018
7019 if (event_type & SUB_RETRY_SEND)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007020 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7021
7022 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007023
7024 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007025}
7026
7027/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
7028 * The <es> pointer is not allowed to differ from the one passed to the
7029 * subscribe() call. It always returns zero.
7030 */
7031static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7032{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007033 int ret;
7034 struct quic_conn *qc = conn->handle.qc;
7035 struct qcc *qcc = qc->qcc;
7036
7037 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
7038
7039 if (event_type & SUB_RETRY_RECV)
7040 TRACE_DEVEL("unsubscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
7041 if (event_type & SUB_RETRY_SEND)
7042 TRACE_DEVEL("unsubscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7043
7044 ret = conn_unsubscribe(conn, xprt_ctx, event_type, es);
7045
7046 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
7047
7048 return ret;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007049}
7050
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007051/* Store in <xprt_ctx> the context attached to <conn>.
7052 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007053 */
7054static int qc_conn_init(struct connection *conn, void **xprt_ctx)
7055{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01007056 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007057
7058 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
7059
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007060 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007061 if (*xprt_ctx)
7062 goto out;
7063
Willy Tarreau784b8682022-04-11 14:18:10 +02007064 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007065
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007066 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01007067 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007068
7069 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007070}
7071
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007072/* Start the QUIC transport layer */
7073static int qc_xprt_start(struct connection *conn, void *ctx)
7074{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007075 int ret = 0;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007076 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02007077 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007078
Willy Tarreau784b8682022-04-11 14:18:10 +02007079 qc = conn->handle.qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007080 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007081 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
7082 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02007083 /* prepare a CONNECTION_CLOSE frame */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02007084 quic_set_connection_close(qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007085 goto out;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007086 }
7087
7088 /* mux-quic can now be considered ready. */
7089 qc->mux_state = QC_MUX_READY;
7090
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007091 tasklet_wakeup(qctx->wait_event.tasklet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007092
7093 ret = 1;
7094 out:
7095 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
7096 return ret;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007097}
7098
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007099static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
7100{
Willy Tarreau784b8682022-04-11 14:18:10 +02007101 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007102 return NULL;
7103
Willy Tarreau784b8682022-04-11 14:18:10 +02007104 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007105}
7106
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007107/* transport-layer operations for QUIC connections. */
7108static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02007109 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007110 .subscribe = quic_conn_subscribe,
7111 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007112 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007113 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007114 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
7115 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01007116 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007117 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007118 .name = "QUIC",
7119};
7120
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007121static void __quic_conn_init(void)
7122{
7123 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
7124 xprt_register(XPRT_QUIC, &ssl_quic);
7125}
Willy Tarreau79367f92022-04-25 19:18:24 +02007126INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007127
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007128static void __quic_conn_deinit(void)
7129{
7130 BIO_meth_free(ha_quic_meth);
7131}
Willy Tarreau79367f92022-04-25 19:18:24 +02007132REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007133
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01007134/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
7135 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05007136 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007137 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007138struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007139{
7140 unsigned char *pos;
7141 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007142 struct quic_dghdlr *dghdlr = ctx;
7143 struct quic_dgram *dgram;
7144 int first_pkt = 1;
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007145 struct list *tasklist_head = NULL;
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007146 int max_dgrams = global.tune.maxpollevents;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007147
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007148 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7149
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007150 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007151 pos = dgram->buf;
7152 end = pos + dgram->len;
7153 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007154 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007155
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007156 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007157 if (!pkt) {
7158 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
7159 goto leave;
7160 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007161
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007162 LIST_INIT(&pkt->qc_rx_pkt_list);
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02007163 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007164 quic_rx_packet_refinc(pkt);
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007165 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram, &tasklist_head);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007166 first_pkt = 0;
7167 pos += pkt->len;
7168 quic_rx_packet_refdec(pkt);
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007169
7170 /* Free rejected packets */
7171 if (!pkt->refcnt) {
7172 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
7173 pool_free(pool_head_quic_rx_packet, pkt);
7174 }
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007175 } while (pos < end);
7176
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007177 /* Increasing the received bytes counter by the UDP datagram length
7178 * if this datagram could be associated to a connection.
7179 */
7180 if (dgram->qc)
7181 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01007182
7183 /* Mark this datagram as consumed */
7184 HA_ATOMIC_STORE(&dgram->buf, NULL);
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007185
7186 if (--max_dgrams <= 0)
7187 goto stop_here;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007188 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007189
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007190 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
7191
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007192 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007193
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007194 stop_here:
7195 /* too much work done at once, come back here later */
7196 if (!MT_LIST_ISEMPTY(&dghdlr->dgrams))
7197 tasklet_wakeup((struct tasklet *)t);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007198 leave:
7199 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007200 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007201}
7202
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05007203/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007204 * Returns 1 if succeeded, 0 if not.
7205 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02007206int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
7207 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007208{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007209 int ret = 0, long_header;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007210 size_t minlen, skip;
7211
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007212 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
7213
7214 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
7215 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007216 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007217 }
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007218
7219 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02007220 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
7221 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007222 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Willy Tarreauaf5138f2022-08-05 10:09:32 +02007223 if (end - buf < minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007224 goto err;
7225
7226 buf += skip;
7227 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
7228 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
7229 goto err;
7230
7231 *dcid = buf;
7232
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007233 ret = 1;
7234 leave:
7235 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
7236 return ret;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007237
7238 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007239 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
7240 goto leave;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007241}
7242
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007243/* Notify the MUX layer if alive about an imminent close of <qc>. */
7244void qc_notify_close(struct quic_conn *qc)
7245{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007246 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
7247
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007248 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007249 goto leave;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007250
7251 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007252 /* wake up the MUX */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007253 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
7254 TRACE_STATE("connection closure notidfied to mux",
7255 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007256 qc->conn->mux->wake(qc->conn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007257 }
7258 else
7259 TRACE_STATE("connection closure not notidfied to mux",
7260 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
7261 leave:
7262 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007263}
7264
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007265/*
7266 * Local variables:
7267 * c-indent-level: 8
7268 * c-basic-offset: 8
7269 * End:
7270 */