blob: baee8e242b920671d99943797d3501ecc0daeef0 [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
Frédéric Lécaille2f163482022-08-19 09:23:15 +0200502 if (frm) {
503 chunk_appendf(&trace_buf, " frm@%p", frm);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100504 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille2f163482022-08-19 09:23:15 +0200505 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100506 if (val1)
507 chunk_appendf(&trace_buf, " %lu", *val1);
508 if (val2)
509 chunk_appendf(&trace_buf, "..%lu", *val2);
510 }
511
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100512 if (mask & QUIC_EV_CONN_ACKSTRM) {
513 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200514 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100515
516 if (s)
517 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200518 if (stream)
519 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100520 }
521
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100522 if (mask & QUIC_EV_CONN_RTTUPDT) {
523 const unsigned int *rtt_sample = a2;
524 const unsigned int *ack_delay = a3;
525 const struct quic_loss *ql = a4;
526
527 if (rtt_sample)
528 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
529 if (ack_delay)
530 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
531 if (ql)
532 chunk_appendf(&trace_buf,
533 " srtt=%ums rttvar=%ums min_rtt=%ums",
534 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
535 }
536 if (mask & QUIC_EV_CONN_CC) {
537 const struct quic_cc_event *ev = a2;
538 const struct quic_cc *cc = a3;
539
540 if (a2)
541 quic_cc_event_trace(&trace_buf, ev);
542 if (a3)
543 quic_cc_state_trace(&trace_buf, cc);
544 }
545
546 if (mask & QUIC_EV_CONN_PKTLOSS) {
547 const struct quic_pktns *pktns = a2;
548 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100549
550 if (pktns) {
551 chunk_appendf(&trace_buf, " pktns=%s",
552 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
553 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
554 if (pktns->tx.loss_time)
555 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100556 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100557 }
558 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
559 struct quic_tx_packet *pkt;
560
561 chunk_appendf(&trace_buf, " lost_pkts:");
562 list_for_each_entry(pkt, lost_pkts, list)
563 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
564 }
565 }
566
567 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100568 const struct quic_pktns *pktns = a2;
569 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100570 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100571
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100572 if (ifae_pkts)
573 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
574 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100575 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100576 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100577 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100578 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
579 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100580 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
581 if (pktns->tx.in_flight)
582 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100583 if (pktns->tx.loss_time)
584 chunk_appendf(&trace_buf, " loss_time=%dms",
585 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
586 }
587 if (mask & QUIC_EV_CONN_SPTO) {
588 if (pktns->tx.time_of_last_eliciting)
589 chunk_appendf(&trace_buf, " tole=%dms",
590 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
591 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100592 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593 }
594 }
595
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200596 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100597 chunk_appendf(&trace_buf,
598 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
599 }
600 }
601
602 if (mask & QUIC_EV_CONN_SPPKTS) {
603 const struct quic_tx_packet *pkt = a2;
604
Frédéric Lécaille8ecb7362022-08-04 12:00:00 +0200605 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100606 (unsigned long long)qc->path->cwnd,
607 (unsigned long long)qc->path->prep_in_flight,
608 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100609 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100610 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100611 (unsigned long)pkt->pn_node.key,
612 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
613 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100614 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615 }
616 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100617
618 if (mask & QUIC_EV_CONN_SSLALERT) {
619 const uint8_t *alert = a2;
620 const enum ssl_encryption_level_t *level = a3;
621
622 if (alert)
623 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
624 if (level)
625 chunk_appendf(&trace_buf, " el=%c",
626 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
627 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100628
629 if (mask & QUIC_EV_CONN_BCFRMS) {
630 const size_t *sz1 = a2;
631 const size_t *sz2 = a3;
632 const size_t *sz3 = a4;
633
634 if (sz1)
635 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
636 if (sz2)
637 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
638 if (sz3)
639 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
640 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100641
642 if (mask & QUIC_EV_CONN_PSTRM) {
643 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100644
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100645 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100646 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100647 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100648 }
649 if (mask & QUIC_EV_CONN_LPKT) {
650 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200651 const uint64_t *len = a3;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200652 const struct quic_version *ver = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100653
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100654 if (pkt) {
655 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
656 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
657 if (pkt->pn_node.key != (uint64_t)-1)
658 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
659 }
660
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200661 if (len)
662 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200663
664 if (ver)
665 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100666 }
667
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200668 if (mask & QUIC_EV_STATELESS_RST) {
669 const struct quic_cid *cid = a2;
670
671 if (cid)
672 quic_cid_dump(&trace_buf, cid);
673 }
674
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100675}
676
677/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100678static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100679{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200680 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100681
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100682 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100683 return 1;
684
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200685 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
686 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200687 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
688 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200689 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100690 return 1;
691
692 return 0;
693}
694
695/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
696 * both loss detection and PTO and schedule the task assiated to this timer if needed.
697 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100698static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100699{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100700 struct quic_pktns *pktns;
701 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200702 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100703
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100704 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100705 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100706
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100707 pktns = quic_loss_pktns(qc);
708 if (tick_isset(pktns->tx.loss_time)) {
709 qc->timer = pktns->tx.loss_time;
710 goto out;
711 }
712
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100713 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100714 * cancelled for a server which reached the anti-amplification limit.
715 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100716 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200717 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100718 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100719 qc->timer = TICK_ETERNITY;
720 goto out;
721 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100722
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100723 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100724 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100725 /* Timer cancellation. */
726 qc->timer = TICK_ETERNITY;
727 goto out;
728 }
729
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200730 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200731 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100732 if (tick_isset(pto))
733 qc->timer = pto;
734 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100735 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100736 if (tick_is_expired(qc->timer, now_ms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200737 TRACE_DEVEL("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100738 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100739 }
740 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200741 TRACE_DEVEL("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100742 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100743 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100744 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100745 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100746}
747
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100748/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
749 * connection.
750 * Return 1 if succeeded, 0 if not.
751 */
752static int quic_tls_key_update(struct quic_conn *qc)
753{
754 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
755 struct quic_tls_secrets *rx, *tx;
756 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
757 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200758 const struct quic_version *ver =
759 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200760 int ret = 0;
761
762 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100763
764 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
765 rx = &tls_ctx->rx;
766 tx = &tls_ctx->tx;
767 nxt_rx = &qc->ku.nxt_rx;
768 nxt_tx = &qc->ku.nxt_tx;
769
770 /* Prepare new RX secrets */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200771 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100772 rx->secret, rx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200773 TRACE_ERROR("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
774 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100775 }
776
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200777 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100778 nxt_rx->key, nxt_rx->keylen,
779 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
780 nxt_rx->secret, nxt_rx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200781 TRACE_ERROR("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
782 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100783 }
784
785 /* Prepare new TX secrets */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200786 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100787 tx->secret, tx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200788 TRACE_ERROR("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
789 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100790 }
791
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200792 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100793 nxt_tx->key, nxt_tx->keylen,
794 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
795 nxt_tx->secret, nxt_tx->secretlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200796 TRACE_ERROR("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
797 goto leave;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100798 }
799
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200800 if (nxt_rx->ctx) {
801 EVP_CIPHER_CTX_free(nxt_rx->ctx);
802 nxt_rx->ctx = NULL;
803 }
804
805 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 +0200806 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
807 goto leave;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200808 }
809
810 if (nxt_tx->ctx) {
811 EVP_CIPHER_CTX_free(nxt_tx->ctx);
812 nxt_tx->ctx = NULL;
813 }
814
815 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 +0200816 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
817 goto leave;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200818 }
819
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200820 ret = 1;
821 leave:
822 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc);
823 return ret;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100824}
825
826/* Rotate the Key Update information for <qc> QUIC connection.
827 * Must be used after having updated them.
828 * Always succeeds.
829 */
830static void quic_tls_rotate_keys(struct quic_conn *qc)
831{
832 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
833 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200834 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100835
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200836 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
837
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100838 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200839 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100840 curr_secret = tls_ctx->rx.secret;
841 curr_iv = tls_ctx->rx.iv;
842 curr_key = tls_ctx->rx.key;
843
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200844 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100845 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
846 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
847 tls_ctx->rx.key = qc->ku.nxt_rx.key;
848
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200849 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100850 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
851 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
852 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
853
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200854 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100855 qc->ku.prv_rx.secret = curr_secret;
856 qc->ku.prv_rx.iv = curr_iv;
857 qc->ku.prv_rx.key = curr_key;
858 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
859
860 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200861 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100862 curr_secret = tls_ctx->tx.secret;
863 curr_iv = tls_ctx->tx.iv;
864 curr_key = tls_ctx->tx.key;
865
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200866 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100867 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
868 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
869 tls_ctx->tx.key = qc->ku.nxt_tx.key;
870
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200871 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100872 qc->ku.nxt_tx.secret = curr_secret;
873 qc->ku.nxt_tx.iv = curr_iv;
874 qc->ku.nxt_tx.key = curr_key;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200875
876 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100877}
878
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200879/* returns 0 on error, 1 on success */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100880int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
881 const uint8_t *read_secret,
882 const uint8_t *write_secret, size_t secret_len)
883{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100884 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
885 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 +0100886 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100887 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200888 const struct quic_version *ver =
889 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200890 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100891
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100892 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100893 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200894 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100895 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100896 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100897 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100898
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100899 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200900 TRACE_ERROR("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
901 goto leave;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100902 }
903
904 rx = &tls_ctx->rx;
905 tx = &tls_ctx->tx;
906
907 rx->aead = tx->aead = tls_aead(cipher);
908 rx->md = tx->md = tls_md(cipher);
909 rx->hp = tx->hp = tls_hp(cipher);
910
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200911 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 +0100912 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100913 read_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200914 TRACE_ERROR("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
915 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100916 }
917
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200918 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200919 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
920 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200921 }
922
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100923 /* Enqueue this connection asap if we could derive O-RTT secrets as
924 * listener. Note that a listener derives only RX secrets for this
925 * level.
926 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200927 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
928 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100929 quic_accept_push_qc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200930 }
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100931
932 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100933 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100934
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200935 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 +0100936 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100937 write_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200938 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
939 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100940 }
941
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200942 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200943 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
944 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200945 }
946
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100947 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100948 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
949 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
950 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
951
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200952 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100953 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
954 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200955 TRACE_ERROR("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
956 goto leave;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100957 }
958
959 memcpy(rx->secret, read_secret, secret_len);
960 rx->secretlen = secret_len;
961 memcpy(tx->secret, write_secret, secret_len);
962 tx->secretlen = secret_len;
963 /* Initialize all the secret keys lengths */
964 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
965 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200966 if (!quic_tls_key_update(qc)) {
967 // trace already emitted by function above
968 goto leave;
969 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100970 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100971
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100972 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100973 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100974 no_secret:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200975 ret = 1;
976 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100977 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200978 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100979}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100980
981/* This function copies the CRYPTO data provided by the TLS stack found at <data>
982 * with <len> as size in CRYPTO buffers dedicated to store the information about
983 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200984 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
985 * buffers to store all the data.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100986 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
987 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200988static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100989 const unsigned char *data, size_t len)
990{
991 struct quic_crypto_buf **qcb;
992 /* The remaining byte to store in CRYPTO buffers. */
993 size_t cf_offset, cf_len, *nb_buf;
994 unsigned char *pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200995 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100996
997 nb_buf = &qel->tx.crypto.nb_buf;
998 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
999 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1000 cf_len = len;
1001
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001002 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1003
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001004 while (len) {
1005 size_t to_copy, room;
1006
1007 pos = (*qcb)->data + (*qcb)->sz;
1008 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1009 to_copy = len > room ? room : len;
1010 if (to_copy) {
1011 memcpy(pos, data, to_copy);
1012 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1013 qel->tx.crypto.sz += to_copy;
1014 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001015 len -= to_copy;
1016 data += to_copy;
1017 }
1018 else {
1019 struct quic_crypto_buf **tmp;
1020
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001021 // FIXME: realloc!
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001022 tmp = realloc(qel->tx.crypto.bufs,
1023 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1024 if (tmp) {
1025 qel->tx.crypto.bufs = tmp;
1026 qcb = &qel->tx.crypto.bufs[*nb_buf];
1027 *qcb = pool_alloc(pool_head_quic_crypto_buf);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001028 if (!*qcb) {
1029 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1030 goto leave;
1031 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001032
1033 (*qcb)->sz = 0;
1034 ++*nb_buf;
1035 }
1036 else {
1037 break;
1038 }
1039 }
1040 }
1041
1042 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1043 * have been buffered.
1044 */
1045 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001046 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001047 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001048
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001049 /* There is at most one CRYPTO frame in this packet number
1050 * space. Let's look for it.
1051 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001052 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001053 if (frm->type != QUIC_FT_CRYPTO)
1054 continue;
1055
1056 /* Found */
1057 found = frm;
1058 break;
1059 }
1060
1061 if (found) {
1062 found->crypto.len += cf_len;
1063 }
1064 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001065 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001066 if (!frm) {
1067 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1068 goto leave;
1069 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001070
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001071 LIST_INIT(&frm->reflist);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001072 frm->type = QUIC_FT_CRYPTO;
1073 frm->crypto.offset = cf_offset;
1074 frm->crypto.len = cf_len;
1075 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001076 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001077 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001078 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001079 ret = len == 0;
1080 leave:
1081 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1082 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001083}
1084
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001085/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1086 * activity for <qc> will be interrupted.
1087 */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001088void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001089{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001090 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001091 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001092 goto leave;
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001093
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001094 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001095 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001096 qc->err.code = err.code;
1097 qc->err.app = err.app;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001098 leave:
1099 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001100}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001101
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001102/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1103void quic_set_tls_alert(struct quic_conn *qc, int alert)
1104{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001105 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1106
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001107 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1108 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001109 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001110 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1111 }
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001112 quic_set_connection_close(qc, quic_err_tls(alert));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001113 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001114 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1115
1116 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001117}
1118
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001119/* Set the application for <qc> QUIC connection.
1120 * Return 1 if succeeded, 0 if not.
1121 */
1122int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1123{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001124 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1125 qc->app_ops = &h3_ops;
1126 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1127 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001128 else
1129 return 0;
1130
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001131 return 1;
1132}
1133
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001134/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1135 * wants to provide the QUIC layer with CRYPTO data.
1136 * Returns 1 if succeeded, 0 if not.
1137 */
1138int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1139 const uint8_t *data, size_t len)
1140{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001141 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001142 enum quic_tls_enc_level tel;
1143 struct quic_enc_level *qel;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001144 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001145
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001146 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1147 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001148
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001149 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001150 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001151 goto out;
1152 }
1153
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001154 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001155 if (tel == -1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001156 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1157 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001158 }
1159
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001160 qel = &qc->els[tel];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001161 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1162 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1163 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001164 }
1165
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001166 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001167 qc, &level, &len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001168 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001169 ret = 1;
1170 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001171 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001172 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001173}
1174
1175int ha_quic_flush_flight(SSL *ssl)
1176{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001177 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001178
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001179 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1180 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001181
1182 return 1;
1183}
1184
1185int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1186{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001187 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001188
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001189 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1190
1191 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1192
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001193 quic_set_tls_alert(qc, alert);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001194 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001195 return 1;
1196}
1197
1198/* QUIC TLS methods */
1199static SSL_QUIC_METHOD ha_quic_method = {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001200 .set_encryption_secrets = ha_quic_set_encryption_secrets,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001201 .add_handshake_data = ha_quic_add_handshake_data,
1202 .flush_flight = ha_quic_flush_flight,
1203 .send_alert = ha_quic_send_alert,
1204};
1205
1206/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1207 * Returns an error count.
1208 */
1209int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1210{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001211 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1212 int cfgerr = 0;
1213
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001214 long options =
1215 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1216 SSL_OP_SINGLE_ECDH_USE |
1217 SSL_OP_CIPHER_SERVER_PREFERENCE;
1218 SSL_CTX *ctx;
1219
1220 ctx = SSL_CTX_new(TLS_server_method());
1221 bind_conf->initial_ctx = ctx;
1222
1223 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001224 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1225 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1226 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001227
1228#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaillee06f7452022-06-16 15:06:44 +02001229#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001230 if (bind_conf->ssl_conf.early_data) {
1231 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001232 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001233 }
1234 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1235 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1236#else
1237 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1238#endif
1239 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1240#endif
1241 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1242
1243 return cfgerr;
1244}
1245
1246/* Decode an expected packet number from <truncated_on> its truncated value,
1247 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1248 * the number of bits used to encode this packet number (its length in bytes * 8).
1249 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1250 */
1251static uint64_t decode_packet_number(uint64_t largest_pn,
1252 uint32_t truncated_pn, unsigned int pn_nbits)
1253{
1254 uint64_t expected_pn = largest_pn + 1;
1255 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1256 uint64_t pn_hwin = pn_win / 2;
1257 uint64_t pn_mask = pn_win - 1;
1258 uint64_t candidate_pn;
1259
1260
1261 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1262 /* Note that <pn_win> > <pn_hwin>. */
1263 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1264 candidate_pn + pn_hwin <= expected_pn)
1265 return candidate_pn + pn_win;
1266
1267 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1268 return candidate_pn - pn_win;
1269
1270 return candidate_pn;
1271}
1272
1273/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1274 * cryptographic context.
1275 * <largest_pn> is the largest received packet number and <pn> the address of
1276 * the packet number field for this packet with <byte0> address of its first byte.
1277 * <end> points to one byte past the end of this packet.
1278 * Returns 1 if succeeded, 0 if not.
1279 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001280static int qc_do_rm_hp(struct quic_conn *qc,
1281 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001282 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001283{
1284 int ret, outlen, i, pnlen;
1285 uint64_t packet_number;
1286 uint32_t truncated_pn = 0;
1287 unsigned char mask[5] = {0};
1288 unsigned char *sample;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001289 EVP_CIPHER_CTX *cctx = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001290 unsigned char *hp_key;
1291
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001292 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1293
1294 ret = 0;
1295
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001296 /* Check there is enough data in this packet. */
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001297 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001298 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1299 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001300 }
1301
1302 cctx = EVP_CIPHER_CTX_new();
1303 if (!cctx) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001304 TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
1305 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001306 }
1307
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308 sample = pn + QUIC_PACKET_PN_MAXLEN;
1309
1310 hp_key = tls_ctx->rx.hp_key;
1311 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1312 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1313 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001314 TRACE_ERROR("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
1315 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001316 }
1317
1318 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1319 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1320 for (i = 0; i < pnlen; i++) {
1321 pn[i] ^= mask[i + 1];
1322 truncated_pn = (truncated_pn << 8) | pn[i];
1323 }
1324
1325 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1326 /* Store remaining information for this unprotected header */
1327 pkt->pn = packet_number;
1328 pkt->pnl = pnlen;
1329
1330 ret = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001331 leave:
1332 if (cctx)
1333 EVP_CIPHER_CTX_free(cctx);
1334 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001335 return ret;
1336}
1337
1338/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1339 * address, with <payload_len> as payload length, <aad> as address of
1340 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1341 * context.
1342 * Returns 1 if succeeded, 0 if not.
1343 */
1344static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1345 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001346 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001347{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001348 int ret = 0;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001349 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001350 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001351 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001352 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001353
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001354 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
1355
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001356 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001357 TRACE_ERROR("AEAD IV building for encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001358 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001359 }
1360
1361 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001362 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001363 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001364 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001365 }
1366
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001367 ret = 1;
1368 leave:
1369 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
1370 return ret;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001371
1372 err:
1373 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001374 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001375}
1376
1377/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1378 * Returns 1 if succeeded, 0 if not.
1379 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001380static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel,
1381 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001382{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001383 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001384 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001385 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001386 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001387 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001388 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1389 unsigned char *rx_key = tls_ctx->rx.key;
1390
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001391 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1392
1393 ret = 0;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001394 kp_changed = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001395
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001396 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1397 /* The two tested bits are not at the same position,
1398 * this is why they are first both inversed.
1399 */
1400 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1401 if (pkt->pn < tls_ctx->rx.pn) {
1402 /* The lowest packet number of a previous key phase
1403 * cannot be null if it really stores previous key phase
1404 * secrets.
1405 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001406 // TODO: check if BUG_ON() more suitable
1407 if (!pkt->qc->ku.prv_rx.pn) {
1408 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1409 goto leave;
1410 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001411
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001412 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1413 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001414 rx_key = pkt->qc->ku.prv_rx.key;
1415 }
1416 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1417 /* Next key phase */
1418 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001419 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1420 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001421 rx_key = pkt->qc->ku.nxt_rx.key;
1422 }
1423 }
1424 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001425
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001426 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) {
1427 TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc);
1428 goto leave;
1429 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001430
1431 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1432 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001433 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001434 if (!ret) {
1435 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1436 goto leave;
1437 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001438
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001439 /* Update the keys only if the packet decryption succeeded. */
1440 if (kp_changed) {
1441 quic_tls_rotate_keys(pkt->qc);
1442 /* Toggle the Key Phase bit */
1443 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1444 /* Store the lowest packet number received for the current key phase */
1445 tls_ctx->rx.pn = pkt->pn;
1446 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001447 if (!quic_tls_key_update(pkt->qc)) {
1448 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1449 goto leave;
1450 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001451 }
1452
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001453 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001454 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001455 ret = 1;
1456 leave:
1457 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille11a6f402022-08-16 14:48:59 +02001458 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001459}
1460
Frédéric Lécaille96367152022-04-25 09:40:19 +02001461
1462/* Remove references to <frm> frame */
1463static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1464{
Frédéric Lécaille96367152022-04-25 09:40:19 +02001465 struct quic_frame *f, *tmp;
1466
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001467 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1468
Frédéric Lécaille96367152022-04-25 09:40:19 +02001469 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001470 f->origin = NULL;
1471 LIST_DELETE(&f->ref);
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001472 if (f->pkt) {
1473 TRACE_DEVEL("remove frame reference",
1474 QUIC_EV_CONN_PRSAFRM, qc, f, &f->pkt->pn_node.key);
1475 }
1476 else {
1477 /* XXX TODO: what must be done for such a frame */
1478 TRACE_DEVEL("remove frame reference for unsent frame",
1479 QUIC_EV_CONN_PRSAFRM, qc, f);
1480 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001481 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001482
1483 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001484}
1485
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001486/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001487void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001488{
1489 uint64_t pn;
1490 struct quic_frame *origin, *f, *tmp;
1491
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001492 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1493
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001494 /* Identify this frame: a frame copy or one of its copies */
1495 origin = frm->origin ? frm->origin : frm;
1496 /* Ensure the source of the copies is flagged as acked, <frm> being
1497 * possibly a copy of <origin>
1498 */
1499 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1500 /* Mark all the copy of <origin> as acknowledged. We must
1501 * not release the packets (releasing the frames) at this time as
1502 * they are possibly also to be acknowledged alongside the
1503 * the current one.
1504 */
1505 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001506 if (f->pkt) {
1507 pn = f->pkt->pn_node.key;
1508 TRACE_DEVEL("mark frame as acked from packet",
1509 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1510 }
1511 else {
1512 /* XXX TODO: what must be done for such a frame */
1513 TRACE_DEVEL("remove frame reference for unsent frame",
1514 QUIC_EV_CONN_PRSAFRM, qc, f);
1515 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001516 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1517 f->origin = NULL;
1518 LIST_DELETE(&f->ref);
1519 }
1520 LIST_DELETE(&frm->list);
1521 pn = frm->pkt->pn_node.key;
1522 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001523 TRACE_DEVEL("freeing frame from packet",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001524 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1525 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001526
1527 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001528}
1529
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001530/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1531 * and all STREAM data are acknowledged. The MUX is responsible to have set
1532 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1533 *
1534 * TODO this should also be called on lost packet detection
1535 */
1536static void qc_check_close_on_released_mux(struct quic_conn *qc)
1537{
1538 struct ssl_sock_ctx *ctx = qc->xprt_ctx;
1539
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001540 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1541
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001542 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1543 /* Reuse errcode which should have been previously set by the MUX on release. */
1544 quic_set_connection_close(qc, qc->err);
1545 tasklet_wakeup(ctx->wait_event.tasklet);
1546 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001547
1548 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001549}
1550
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001551/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001552 *
1553 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001554 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001555static int quic_stream_try_to_consume(struct quic_conn *qc,
1556 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001557{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001558 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001559 struct eb64_node *frm_node;
1560
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001561 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1562
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001563 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001564 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001565 while (frm_node) {
1566 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001567 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001568 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001569
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001570 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001571 offset = strm->offset.key;
1572 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001573
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001574 if (offset > stream->ack_offset)
1575 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001576
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001577 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001578 /* cf. next comment : frame may be freed at this stage. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001579 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001580 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001581 ret = 1;
1582 }
1583
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001584 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1585 * has been freed. with the stream frames tree. Nothing to do
1586 * anymore in here.
1587 */
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001588 if (!stream) {
1589 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001590 ret = 1;
1591 goto leave;
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001592 }
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001593
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001594 frm_node = eb64_next(frm_node);
1595 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001596
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001597 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001598 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001599 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001600
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001601 leave:
1602 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001603 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001604}
1605
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001606/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001607static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1608 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001609{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001610 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001611
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001612 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm);
1613
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001614 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001615 switch (frm->type) {
1616 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1617 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001618 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001619 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001620 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001621 const size_t offset = strm_frm->offset.key;
1622 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001623
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001624 /* do not use strm_frm->stream as the qc_stream_desc instance
1625 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001626 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001627 *
1628 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001629 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001630 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001631 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1632 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001633 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001634 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001635 /* early return */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001636 goto leave;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001637 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001638 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001639
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001640 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001641 if (offset <= stream->ack_offset) {
1642 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001643 stream_acked = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001644 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001645 qc, strm_frm, stream);
1646 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001647
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001648 if (!stream) {
1649 /* no need to continue if stream freed. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001650 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001651 qc_release_frm(qc, frm);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001652 qc_check_close_on_released_mux(qc);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001653 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001654 }
1655
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001656 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Frédéric Lécailleda342552022-04-25 10:28:49 +02001657 qc, strm_frm, stream);
1658 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001659 }
1660 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001661 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001662 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001663
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001664 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001665 }
1666 break;
1667 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001668 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001669 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001670
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001671 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001672 struct qcc *qcc = qc->qcc;
1673
1674 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1675 tasklet_wakeup(qcc->subs->tasklet);
1676 qcc->subs->events &= ~SUB_RETRY_SEND;
1677 if (!qcc->subs->events)
1678 qcc->subs = NULL;
1679 }
1680 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001681 leave:
1682 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001683}
1684
1685/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1686 * deallocating them, and their TX frames.
1687 * Returns the last node reached to be used for the next range.
1688 * May be NULL if <largest> node could not be found.
1689 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001690static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1691 struct eb_root *pkts,
1692 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001693 struct list *newly_acked_pkts,
1694 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001695 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001696{
1697 struct eb64_node *node;
1698 struct quic_tx_packet *pkt;
1699
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001700 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1701
Frédéric Lécaille843399f2022-07-22 16:27:44 +02001702 node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001703 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001704 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001705
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001706 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001707 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001708 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001709 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001710 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001711 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001712 node = eb64_prev(node);
1713 eb64_delete(&pkt->pn_node);
1714 }
1715
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001716 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001717 return node;
1718}
1719
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001720/* Remove all frames from <pkt_frm_list> and reinsert them in the
1721 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001722 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001723static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001724 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001725 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001726{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001727 struct quic_frame *frm, *frmbak;
1728 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001729 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001730 uint64_t pn = pkt->pn_node.key;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001731
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001732 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1733
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001734 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001735 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001736 LIST_DELETE(&frm->list);
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001737 quic_tx_packet_refdec(pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001738 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001739 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001740 /* Remove any reference to this frame */
1741 qc_frm_unref(qc, frm);
1742 switch (frm->type) {
1743 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1744 {
1745 struct quic_stream *strm_frm = &frm->stream;
1746 struct eb64_node *node = NULL;
1747 struct qc_stream_desc *stream_desc;
1748
1749 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1750 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001751 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1752 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001753 qc, frm, &pn);
1754 pool_free(pool_head_quic_frame, frm);
1755 continue;
1756 }
1757
1758 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1759 /* Do not resend this frame if in the "already acked range" */
1760 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001761 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001762 QUIC_EV_CONN_PRSAFRM, qc, frm);
1763 continue;
1764 }
1765 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1766 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001767 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001768 QUIC_EV_CONN_PRSAFRM, qc, frm);
1769 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001770 break;
1771 }
1772
1773 default:
1774 break;
1775 }
1776
1777 /* Do not resend probing packet with old data */
1778 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001779 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1780 qc, frm, &pn);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001781 if (frm->origin)
1782 LIST_DELETE(&frm->ref);
1783 pool_free(pool_head_quic_frame, frm);
1784 continue;
1785 }
1786
1787 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001788 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1789 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001790 qc, frm, &pn);
1791 pool_free(pool_head_quic_frame, frm);
1792 }
1793 else {
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001794 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1795 /* Mark this STREAM frame as lost. A look up their stream descriptor
1796 * will be performed to check the stream is not consumed or released.
1797 */
Amaury Denoyellef372e742022-08-16 14:41:57 +02001798 frm->flags |= QUIC_FL_TX_FRAME_LOST;
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001799 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001800 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001801 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001802 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001803 }
1804
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001805 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001806
1807 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001808}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001809
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001810/* Free <pkt> TX packet and its attached frames.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001811 * This is the responsibility of the caller to remove this packet of
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001812 * any data structure it was possibly attached to.
1813 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001814static inline void free_quic_tx_packet(struct quic_conn *qc,
1815 struct quic_tx_packet *pkt)
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001816{
1817 struct quic_frame *frm, *frmbak;
1818
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001819 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1820
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001821 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001822 goto leave;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001823
1824 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1825 LIST_DELETE(&frm->list);
1826 pool_free(pool_head_quic_frame, frm);
1827 }
1828 pool_free(pool_head_quic_tx_packet, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001829
1830 leave:
1831 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001832}
1833
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001834/* Free the TX packets of <pkts> list */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001835static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001836{
1837 struct quic_tx_packet *pkt, *tmp;
1838
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001839 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1840
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001841 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001842 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001843 eb64_delete(&pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001844 free_quic_tx_packet(qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001845 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001846
1847 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001848}
1849
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001850/* Remove already sent ranges of acknowledged packet numbers from
1851 * <pktns> packet number space tree below <largest_acked_pn> possibly
1852 * updating the range which contains <largest_acked_pn>.
1853 * Never fails.
1854 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001855static void qc_treat_ack_of_ack(struct quic_conn *qc,
1856 struct quic_pktns *pktns,
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001857 int64_t largest_acked_pn)
1858{
1859 struct eb64_node *ar, *next_ar;
1860 struct quic_arngs *arngs = &pktns->rx.arngs;
1861
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001862 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1863
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001864 ar = eb64_first(&arngs->root);
1865 while (ar) {
1866 struct quic_arng_node *ar_node;
1867
1868 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001869 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001870
1871 if ((int64_t)ar_node->first.key > largest_acked_pn) {
1872 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001873 break;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001874 }
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001875
1876 if (largest_acked_pn < ar_node->last) {
1877 eb64_delete(ar);
1878 ar_node->first.key = largest_acked_pn + 1;
1879 eb64_insert(&arngs->root, ar);
1880 break;
1881 }
1882
1883 eb64_delete(ar);
1884 pool_free(pool_head_quic_arng, ar_node);
1885 arngs->sz--;
1886 ar = next_ar;
1887 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001888
1889 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001890}
1891
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001892/* Send a packet ack event nofication for each newly acked packet of
1893 * <newly_acked_pkts> list and free them.
1894 * Always succeeds.
1895 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001896static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001897 struct list *newly_acked_pkts)
1898{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001899 struct quic_tx_packet *pkt, *tmp;
1900 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1901
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001902 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1903
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001904 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1905 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001906 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001907 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001908 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001909 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001910 /* If this packet contained an ACK frame, proceed to the
1911 * acknowledging of range of acks from the largest acknowledged
1912 * packet number which was sent in an ACK frame by this packet.
1913 */
1914 if (pkt->largest_acked_pn != -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001915 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001916 ev.ack.acked = pkt->in_flight_len;
1917 ev.ack.time_sent = pkt->time_sent;
1918 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001919 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001920 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001921 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001922 }
1923
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001924 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1925
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001926}
1927
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001928/* Release all the frames attached to <pktns> packet number space */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001929static inline void qc_release_pktns_frms(struct quic_conn *qc,
1930 struct quic_pktns *pktns)
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001931{
1932 struct quic_frame *frm, *frmbak;
1933
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001934 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
1935
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001936 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1937 LIST_DELETE(&frm->list);
1938 pool_free(pool_head_quic_frame, frm);
1939 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001940
1941 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001942}
1943
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944/* Handle <pkts> list of lost packets detected at <now_us> handling
1945 * their TX frames.
1946 * Send a packet loss event to the congestion controller if
1947 * in flight packet have been lost.
1948 * Also frees the packet in <pkts> list.
1949 * Never fails.
1950 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001951static inline void qc_release_lost_pkts(struct quic_conn *qc,
1952 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001953 struct list *pkts,
1954 uint64_t now_us)
1955{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001956 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001957 uint64_t lost_bytes;
1958
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001959 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1960
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001961 lost_bytes = 0;
1962 oldest_lost = newest_lost = NULL;
1963 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001964 struct list tmp = LIST_HEAD_INIT(tmp);
1965
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001966 lost_bytes += pkt->in_flight_len;
1967 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001968 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001969 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001970 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001971 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001972 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001973 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001974 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001975 if (!oldest_lost) {
1976 oldest_lost = newest_lost = pkt;
1977 }
1978 else {
1979 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001980 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001981 newest_lost = pkt;
1982 }
1983 }
1984
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001985 if (newest_lost) {
1986 /* Sent a congestion event to the controller */
Benoit DOLEZ69e3f052022-06-08 09:28:56 +02001987 struct quic_cc_event ev = { };
1988
1989 ev.type = QUIC_CC_EVT_LOSS;
1990 ev.loss.time_sent = newest_lost->time_sent;
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001991
1992 quic_cc_event(&qc->path->cc, &ev);
1993 }
1994
1995 /* If an RTT have been already sampled, <rtt_min> has been set.
1996 * We must check if we are experiencing a persistent congestion.
1997 * If this is the case, the congestion controller must re-enter
1998 * slow start state.
1999 */
2000 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2001 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
2002
2003 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2004 now_ms, qc->max_ack_delay))
2005 qc->path->cc.algo->slow_start(&qc->path->cc);
2006 }
2007
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002008 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002009 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002010 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002011 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002012 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002013
2014 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002015}
2016
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002017/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2018 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002019 * 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 +01002020 * acked ack-eliciting packet.
2021 * Return 1, if succeeded, 0 if not.
2022 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002023static inline int qc_parse_ack_frm(struct quic_conn *qc,
2024 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002025 struct quic_enc_level *qel,
2026 unsigned int *rtt_sample,
2027 const unsigned char **pos, const unsigned char *end)
2028{
2029 struct quic_ack *ack = &frm->ack;
2030 uint64_t smallest, largest;
2031 struct eb_root *pkts;
2032 struct eb64_node *largest_node;
2033 unsigned int time_sent, pkt_flags;
2034 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2035 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002036 int ret = 0;
2037
2038 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002039
2040 if (ack->largest_ack > qel->pktns->tx.next_pn) {
2041 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002042 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002043 goto err;
2044 }
2045
2046 if (ack->first_ack_range > ack->largest_ack) {
2047 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002048 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002049 goto err;
2050 }
2051
2052 largest = ack->largest_ack;
2053 smallest = largest - ack->first_ack_range;
2054 pkts = &qel->pktns->tx.pkts;
2055 pkt_flags = 0;
2056 largest_node = NULL;
2057 time_sent = 0;
2058
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002059 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002060 largest_node = eb64_lookup(pkts, largest);
2061 if (!largest_node) {
2062 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002063 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002064 }
2065 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002066 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002067 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002068 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002069 }
2070
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002071 TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002072 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002073 do {
2074 uint64_t gap, ack_range;
2075
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002076 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2077 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002078 if (!ack->ack_range_num--)
2079 break;
2080
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002081 if (!quic_dec_int(&gap, pos, end)) {
2082 TRACE_ERROR("quic_dec_int(gap) 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 (smallest < gap + 2) {
2087 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002088 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002089 goto err;
2090 }
2091
2092 largest = smallest - gap - 2;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002093 if (!quic_dec_int(&ack_range, pos, end)) {
2094 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002095 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002096 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002097
2098 if (largest < ack_range) {
2099 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002100 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002101 goto err;
2102 }
2103
2104 /* Do not use this node anymore. */
2105 largest_node = NULL;
2106 /* Next range */
2107 smallest = largest - ack_range;
2108
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002109 TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002110 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002111 } while (1);
2112
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002113 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2114 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002115 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002116 }
2117
2118 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2119 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002120 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002121 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002122 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002123 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002124 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2125 if (quic_peer_validated_addr(qc))
2126 qc->path->loss.pto_count = 0;
2127 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002128 }
2129
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002130 ret = 1;
2131 leave:
2132 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2133 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002134
2135 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002136 free_quic_tx_pkts(qc, &newly_acked_pkts);
2137 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002138}
2139
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002140/* This function gives the detail of the SSL error. It is used only
2141 * if the debug mode and the verbose mode are activated. It dump all
2142 * the SSL error until the stack was empty.
2143 */
2144static forceinline void qc_ssl_dump_errors(struct connection *conn)
2145{
2146 if (unlikely(global.mode & MODE_DEBUG)) {
2147 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002148 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002149 unsigned long ret;
2150
Willy Tarreau325fc632022-04-11 18:47:38 +02002151 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002152 ret = ERR_get_error();
2153 if (!ret)
2154 return;
2155
2156 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002157 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002158 }
2159 }
2160}
2161
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002162int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2163 const char **str, int *len);
2164
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002165/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2166 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002167 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002168 * Return 1 if succeeded, 0 if not.
2169 */
2170static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002171 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002172 const unsigned char *data, size_t len,
2173 struct quic_rx_packet *pkt,
2174 struct quic_rx_crypto_frm *cf)
2175{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002176 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002177 struct quic_conn *qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002178 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002179
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002180 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002181 qc = ctx->qc;
2182
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002183 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2184
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002185 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002186 TRACE_ERROR("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002187 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002188 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002189 }
2190
2191 el->rx.crypto.offset += len;
2192 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002193 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002194
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002195 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002196 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002197 ssl_err = SSL_do_handshake(ctx->ssl);
2198 if (ssl_err != 1) {
2199 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2200 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002201 TRACE_PROTO("SSL handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002202 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002203 goto out;
2204 }
2205
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002206 /* TODO: Should close the connection asap */
2207 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2208 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2209 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2210 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2211 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002212 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002213 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002214 ERR_clear_error();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002215 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002216 }
2217
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002218 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002219
2220 /* Check the alpn could be negotiated */
2221 if (!qc->app_ops) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002222 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002223 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002224 goto leave;
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002225 }
2226
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002227 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002228 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002229 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2230 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2231 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002232 /* I/O callback switch */
2233 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002234 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002235 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002236 /* The connection is ready to be accepted. */
2237 quic_accept_push_qc(qc);
2238 }
2239 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002240 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002241 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002242 } else {
2243 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2244 if (ssl_err != 1) {
2245 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2246 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002247 TRACE_PROTO("SSL post handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002248 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002249 goto out;
2250 }
2251
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002252 TRACE_ERROR("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002253 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002254 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002255 }
2256
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002257 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002258 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002259
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002260 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002261 ret = 1;
2262 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002263 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002264 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002265}
2266
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002267/* Parse a STREAM frame <strm_frm>
2268 *
2269 * Return 1 on success. On error, 0 is returned. In this case, the packet
2270 * containing the frame must not be acknowledged.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002271 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002272static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2273 struct quic_stream *strm_frm,
2274 struct quic_conn *qc)
2275{
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002276 int ret;
2277
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002278 /* RFC9000 13.1. Packet Processing
2279 *
2280 * A packet MUST NOT be acknowledged until packet protection has been
2281 * successfully removed and all frames contained in the packet have
2282 * been processed. For STREAM frames, this means the data has been
2283 * enqueued in preparation to be received by the application protocol,
2284 * but it does not require that data be delivered and consumed.
2285 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002286 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2287
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002288 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2289 strm_frm->offset.key, strm_frm->fin,
2290 (char *)strm_frm->data);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002291
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002292 /* frame rejected - packet must not be acknowledeged */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002293 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2294 return !ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002295}
2296
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002297/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2298 * for <qc> QUIC connection.
2299 * This is a best effort function which never fails even if no memory could be
2300 * allocated to duplicate these frames.
2301 */
2302static void qc_dup_pkt_frms(struct quic_conn *qc,
2303 struct list *pkt_frm_list, struct list *out_frm_list)
2304{
2305 struct quic_frame *frm, *frmbak;
2306 struct list tmp = LIST_HEAD_INIT(tmp);
2307
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002308 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2309
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002310 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2311 struct quic_frame *dup_frm, *origin;
2312
2313 switch (frm->type) {
2314 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2315 {
2316 struct quic_stream *strm_frm = &frm->stream;
2317 struct eb64_node *node = NULL;
2318 struct qc_stream_desc *stream_desc;
2319
2320 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2321 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002322 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002323 continue;
2324 }
2325
2326 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2327 /* Do not resend this frame if in the "already acked range" */
2328 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002329 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002330 QUIC_EV_CONN_PRSAFRM, qc, frm);
2331 continue;
2332 }
2333 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2334 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002335 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002336 QUIC_EV_CONN_PRSAFRM, qc, frm);
2337 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002338 break;
2339 }
2340
2341 default:
2342 break;
2343 }
2344
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002345 dup_frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002346 if (!dup_frm) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002347 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002348 break;
2349 }
2350
2351 /* If <frm> is already a copy of another frame, we must take
2352 * its original frame as source for the copy.
2353 */
2354 origin = frm->origin ? frm->origin : frm;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002355 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002356 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002357 qc, NULL, &origin->pkt->pn_node.key);
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002358 *dup_frm = *origin;
2359 dup_frm->pkt = NULL;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002360 dup_frm->origin = origin;
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002361 dup_frm->flags = 0;
2362 LIST_INIT(&dup_frm->reflist);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002363 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2364 LIST_APPEND(&tmp, &dup_frm->list);
2365 }
2366
2367 LIST_SPLICE(out_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002368
2369 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002370}
2371
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002372/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002373static void qc_prep_fast_retrans(struct quic_conn *qc,
2374 struct quic_enc_level *qel,
2375 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002376{
2377 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002378 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002379 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002380 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002381
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002382 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2383
2384 BUG_ON(frms1 == frms2);
2385
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002386 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002387 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002388 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002389 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002390 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2391 node = eb64_next(node);
2392 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002393 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002394 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002395 }
2396
2397 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002398 goto leave;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002399
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002400 /* When building a packet from another one, the field which may increase the
2401 * packet size is the packet number. And the maximum increase is 4 bytes.
2402 */
2403 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2404 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2405 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002406 goto leave;
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002407 }
2408
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002409 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002410 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2411 if (frms == frms1 && frms2) {
2412 frms = frms2;
2413 goto start;
2414 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002415 leave:
2416 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002417}
2418
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002419/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002420 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002421 * Initial packets send them coalescing with others (Handshake here).
2422 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002423 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002424static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2425 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002426{
2427 struct list itmp = LIST_HEAD_INIT(itmp);
2428 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002429
2430 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2431 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2432 struct quic_enc_level *qel = iqel;
2433 struct eb_root *pkts;
2434 struct eb64_node *node;
2435 struct quic_tx_packet *pkt;
2436 struct list *tmp = &itmp;
2437
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002438 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002439 start:
2440 pkt = NULL;
2441 pkts = &qel->pktns->tx.pkts;
2442 node = eb64_first(pkts);
2443 /* Skip the empty packet (they have already been retransmitted) */
2444 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002445 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002446 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002447 break;
2448 node = eb64_next(node);
2449 }
2450
2451 if (!pkt)
2452 goto end;
2453
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002454 /* When building a packet from another one, the field which may increase the
2455 * packet size is the packet number. And the maximum increase is 4 bytes.
2456 */
2457 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2458 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2459 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2460 goto end;
2461 }
2462
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002463 qel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002464
2465 /* No risk to loop here, #packet per datagram is bounded */
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002466 requeue:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002467 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002468 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002469 if (qel == iqel) {
2470 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2471 pkt = pkt->next;
2472 tmp = &htmp;
2473 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002474 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002475 goto requeue;
2476 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002477 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002478
2479 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002480 LIST_SPLICE(ifrms, &itmp);
2481 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002482
2483 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002484}
2485
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002486static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002487{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002488 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2489
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002490 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2491 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2492 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2493 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002494 goto out;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002495
2496 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2497 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002498
2499 out:
2500 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002501}
2502
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002503/* Enqueue a STOP_SENDING frame to send into 1RTT packet number space
2504 * frame list to send.
2505 * Return 1 if succeeded, 0 if not.
2506 */
2507static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id)
2508{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002509 int ret = 0;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002510 struct quic_frame *frm;
2511 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2512 uint64_t app_error_code;
2513
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002514 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2515
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002516 /* TODO: the mux may be released, we cannot have more
2517 * information about the application error code to send
2518 * at this time.
2519 */
2520 app_error_code = H3_REQUEST_REJECTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002521 // fixme: zalloc
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002522 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002523 if (!frm) {
2524 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2525 goto out;
2526 }
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002527
2528 frm->type = QUIC_FT_STOP_SENDING;
2529 frm->stop_sending.id = id;
2530 frm->stop_sending.app_error_code = app_error_code;
2531 LIST_INIT(&frm->reflist);
2532 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002533 ret = 1;
2534 out:
2535 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2536 return ret;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002537}
2538
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002539/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2540 * as I/O handler context and <qel> as encryption level.
2541 * Returns 1 if succeeded, 0 if failed.
2542 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002543static 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 +01002544 struct quic_enc_level *qel)
2545{
2546 struct quic_frame frm;
2547 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002548 struct quic_conn *qc = ctx->qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002549 int fast_retrans = 0, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002550
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002551 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002552 /* Skip the AAD */
2553 pos = pkt->data + pkt->aad_len;
2554 end = pkt->data + pkt->len;
2555
2556 while (pos < end) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002557 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
2558 // trace already emitted by function above
2559 goto leave;
2560 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002561
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002562 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002563 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002564 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002565 break;
2566 case QUIC_FT_PING:
2567 break;
2568 case QUIC_FT_ACK:
2569 {
2570 unsigned int rtt_sample;
2571
2572 rtt_sample = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002573 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
2574 // trace already emitted by function above
2575 goto leave;
2576 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002577
2578 if (rtt_sample) {
2579 unsigned int ack_delay;
2580
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002581 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002582 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002583 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2584 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002585 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002586 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002587 break;
2588 }
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002589 case QUIC_FT_RESET_STREAM:
2590 /* TODO: handle this frame at STREAM level */
2591 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002592 case QUIC_FT_STOP_SENDING:
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002593 {
2594 struct quic_stop_sending *stop_sending = &frm.stop_sending;
2595 if (qc->mux_state == QC_MUX_READY) {
2596 if (qcc_recv_stop_sending(qc->qcc, stop_sending->id,
2597 stop_sending->app_error_code)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002598 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
2599 goto leave;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002600 }
2601 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002602 break;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002603 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002604 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002605 {
2606 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002607
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002608 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002609 /* 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 TRACE_PROTO("CRYPTO data discarded",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002615 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002616 break;
2617 }
2618
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002619 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2620 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2621 /* XXX TO DO: <cfdebug> is used only for the traces. */
2622 struct quic_rx_crypto_frm cfdebug = { };
2623
2624 cfdebug.offset_node.key = frm.crypto.offset;
2625 cfdebug.len = frm.crypto.len;
2626 /* Nothing to do */
2627 TRACE_PROTO("Already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002628 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002629 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002630 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2631 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002632 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002633 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002634 else {
2635 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2636 /* XXX TO DO: <cfdebug> is used only for the traces. */
2637 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002638
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002639 cfdebug.offset_node.key = frm.crypto.offset;
2640 cfdebug.len = frm.crypto.len;
2641 TRACE_PROTO("Partially already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002642 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002643 frm.crypto.len -= diff;
2644 frm.crypto.data += diff;
2645 frm.crypto.offset = qel->rx.crypto.offset;
2646 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002647 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002648
2649 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002650 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002651 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002653 cfdebug.offset_node.key = frm.crypto.offset;
2654 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002655 if (!qc_provide_cdata(qel, ctx,
2656 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002657 pkt, &cfdebug)) {
2658 // trace already emitted by function above
2659 goto leave;
2660 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002661
2662 break;
2663 }
2664
2665 /* frm.crypto.offset > qel->rx.crypto.offset */
2666 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2667 if (!cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002668 TRACE_ERROR("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002669 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002670 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002671 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002672
2673 cf->offset_node.key = frm.crypto.offset;
2674 cf->len = frm.crypto.len;
2675 cf->data = frm.crypto.data;
2676 cf->pkt = pkt;
2677 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2678 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002679 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002680 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002681 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002682 {
2683 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002684 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002685
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002686 /* The upper layer may not be allocated. */
2687 if (qc->mux_state != QC_MUX_READY) {
2688 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002689 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002690 break;
2691 }
2692 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002693 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002694 if (!qc_stop_sending_frm_enqueue(qc, stream->id))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002695 TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc);
2696 /* This packet will not be acknowledged */
2697 goto leave;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002698 }
2699 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002700
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002701 if (!qc_handle_strm_frm(pkt, stream, qc)) {
2702 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
2703 goto leave;
2704 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002705
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002706 break;
2707 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002708 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002709 if (qc->mux_state == QC_MUX_READY) {
2710 struct quic_max_data *data = &frm.max_data;
2711 qcc_recv_max_data(qc->qcc, data->max_data);
2712 }
2713 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002714 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002715 if (qc->mux_state == QC_MUX_READY) {
2716 struct quic_max_stream_data *data = &frm.max_stream_data;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002717 if (qcc_recv_max_stream_data(qc->qcc, data->id,
2718 data->max_stream_data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002719 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
2720 goto leave;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002721 }
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002722 }
2723 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002724 case QUIC_FT_MAX_STREAMS_BIDI:
2725 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002726 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002727 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002728 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2729 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002730 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002731 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2732 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002733 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002734 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2735 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002736 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002737 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002738 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002739 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002740 case QUIC_FT_RETIRE_CONNECTION_ID:
2741 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002742 break;
2743 case QUIC_FT_CONNECTION_CLOSE:
2744 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002745 /* Increment the error counters */
2746 qc_cc_err_count_inc(qc, &frm);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002747 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002748 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2749 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002750 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002751 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002752 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002753 /* RFC 9000 10.2. Immediate Close:
2754 * The closing and draining connection states exist to ensure
2755 * that connections close cleanly and that delayed or reordered
2756 * packets are properly discarded. These states SHOULD persist
2757 * for at least three times the current PTO interval...
2758 *
2759 * Rearm the idle timeout only one time when entering draining
2760 * state.
2761 */
2762 qc_idle_timer_do_rearm(qc);
2763 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002764 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002765 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002766 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002767 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002768 if (qc_is_listener(ctx->qc)) {
2769 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
2770 QUIC_EV_CONN_PRSHPKT, qc);
2771 goto leave;
2772 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002773
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002774 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002775 break;
2776 default:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002777 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
2778 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002779 }
2780 }
2781
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002782 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002783 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002784
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002785 if (fast_retrans) {
2786 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2787 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2788
2789 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2790 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002791
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002792 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2793 * has successfully parse a Handshake packet. The Initial encryption must also
2794 * be discarded.
2795 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002796 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002797 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002798 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2799 QUIC_FL_TLS_SECRETS_DCD)) {
2800 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2801 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2802 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2803 qc_set_timer(ctx->qc);
2804 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002805 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002806 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002807 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2808 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002809 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002810 }
2811
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002812 ret = 1;
2813 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002814 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002815 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002816}
2817
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002818/* Allocate Tx buffer from <qc> quic-conn if needed.
2819 *
2820 * Returns allocated buffer or NULL on error.
2821 */
2822static struct buffer *qc_txb_alloc(struct quic_conn *qc)
2823{
2824 struct buffer *buf = &qc->tx.buf;
2825 if (!b_alloc(buf))
2826 return NULL;
2827
2828 return buf;
2829}
2830
2831/* Free Tx buffer from <qc> if it is empty. */
2832static void qc_txb_release(struct quic_conn *qc)
2833{
2834 struct buffer *buf = &qc->tx.buf;
2835
2836 /* For the moment sending function is responsible to purge the buffer
2837 * entirely. It may change in the future but this requires to be able
2838 * to reuse old data.
2839 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002840 BUG_ON_HOT(buf && b_data(buf));
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002841
2842 if (!b_data(buf)) {
2843 b_free(buf);
2844 offer_buffers(NULL, 1);
2845 }
2846}
2847
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002848/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
2849 * must contains the address of the first packet stored in the payload.
2850 *
2851 * Caller is responsible that there is enough space in the buffer.
2852 */
2853static void qc_txb_store(struct buffer *buf, uint16_t length,
2854 struct quic_tx_packet *first_pkt)
2855{
2856 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
2857 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
2858
2859 write_u16(b_tail(buf), length);
2860 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
2861 b_add(buf, hdlen + length);
2862}
2863
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002864/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2865 * with <frms> as ack-eliciting frame list to send, 0 if not.
2866 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2867 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002868 * <force_ack> may be set to 1 if you want to force an ack.
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002869 */
2870static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002871 struct quic_enc_level *qel, int cc, int probe, int force_ack)
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002872{
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002873 unsigned int must_ack = force_ack ||
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002874 (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED));
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002875
2876 /* Do not build any more packet if the TX secrets are not available or
2877 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2878 * and if there is no more packets to send upon PTO expiration
2879 * and if there is no more ack-eliciting frames to send or in flight
2880 * congestion control limit is reached for prepared data
2881 */
2882 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2883 (!cc && !probe && !must_ack &&
2884 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002885 return 0;
2886 }
2887
2888 return 1;
2889}
2890
Amaury Denoyellef2476052022-08-04 16:19:57 +02002891/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2892 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
2893 *
2894 * Each datagram is prepended by a two fields header : the datagram length and
2895 * the address of the packet contained in the datagram.
2896 *
2897 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2898 * -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002899 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002900static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002901 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002902{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002903 int ret = -1;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002904 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002905 unsigned char *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002906 struct quic_tx_packet *pkt;
2907 size_t total;
Amaury Denoyellef2476052022-08-04 16:19:57 +02002908 /* Each datagram is prepended with its length followed by the address
2909 * of the first packet in the datagram.
2910 */
2911 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002912
2913 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002914
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002915 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2916 total = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002917 pos = (unsigned char *)b_tail(buf);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002918 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002919 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002920
2921 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002922 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002923 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002924 /* We do not probe if an immediate close was asked */
2925 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002926 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002927
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002928 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002929 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002930
2931 /* Leave room for the datagram header */
2932 pos += dg_headlen;
2933 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002934 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 +01002935 }
2936 else {
2937 end = pos + qc->path->mtu;
2938 }
2939
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002940 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
2941 QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002942 switch (err) {
2943 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002944 // trace already emitted by function above
2945 goto leave;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002946 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002947 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2948 * MTU, we are here because of the congestion control window. There is
2949 * no need to try to reuse this buffer.
2950 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002951 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002952 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002953 default:
2954 break;
2955 }
2956
2957 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002958 BUG_ON(!pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002959
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002960 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2961 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2962
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002963 total += pkt->len;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002964
Amaury Denoyellef2476052022-08-04 16:19:57 +02002965 /* Write datagram header. */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002966 qc_txb_store(buf, pkt->len, pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002967 }
2968
2969 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002970 ret = total;
2971 leave:
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002972 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002973 return ret;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002974}
2975
Amaury Denoyellef2476052022-08-04 16:19:57 +02002976/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2977 * <frms>. Several packets can be regrouped in a single datagram. The result is
2978 * written into <buf>.
2979 *
2980 * Each datagram is prepended by a two fields header : the datagram length and
2981 * the address of first packet in the datagram.
2982 *
2983 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2984 * -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002985 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002986static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002987 enum quic_tls_enc_level tel, struct list *tel_frms,
2988 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002989{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002990 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002991 unsigned char *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002992 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2993 /* length of datagrams */
2994 uint16_t dglen;
2995 size_t total;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002996 int ret = -1, padding;
Amaury Denoyellef2476052022-08-04 16:19:57 +02002997 /* Each datagram is prepended with its length followed by the address
2998 * of the first packet in the datagram.
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002999 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003000 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003001 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003002
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003003 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3004
Amaury Denoyellef2476052022-08-04 16:19:57 +02003005 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
3006 * caller must ensure that buf is resetted.
3007 */
3008 BUG_ON_HOT(buf->head || buf->data);
3009
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003010 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003011 qel = &qc->els[tel];
3012 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003013 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003014 padding = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003015 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003016 first_pkt = prv_pkt = NULL;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003017 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003018 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003019 enum quic_pkt_type pkt_type;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003020 struct quic_tls_ctx *tls_ctx;
3021 const struct quic_version *ver;
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003022 int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3023 (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3024 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003026 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003027 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003028 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003029 /* We do not probe if an immediate close was asked */
3030 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01003031 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003032
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003033 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003034 if (prv_pkt)
3035 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003036 /* Let's select the next encryption level */
3037 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3038 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003039 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003040 qel = &qc->els[tel];
3041 /* Build a new datagram */
3042 prv_pkt = NULL;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003043 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003044 continue;
3045 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 break;
3047 }
3048
3049 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003050 if (!prv_pkt) {
3051 /* Leave room for the datagram header */
3052 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003053 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02003054 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 +01003055 }
3056 else {
3057 end = pos + qc->path->mtu;
3058 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003059 }
3060
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003061 if (qc->negotiated_version) {
3062 ver = qc->negotiated_version;
3063 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3064 tls_ctx = &qc->negotiated_ictx;
3065 else
3066 tls_ctx = &qel->tls_ctx;
3067 }
3068 else {
3069 ver = qc->original_version;
3070 tls_ctx = &qel->tls_ctx;
3071 }
3072
3073 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02003074 qc, ver, dglen, pkt_type,
3075 force_ack, padding, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003076 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003077 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003078 // trace already emitted by function above
3079 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003080 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003081 /* If there was already a correct packet present, set the
3082 * current datagram as prepared into <cbuf>.
3083 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003084 if (prv_pkt)
3085 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003086 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003087 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003088 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01003089 break;
3090 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02003091
Frédéric Lécaille63556772021-12-29 17:18:21 +01003092 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003093 BUG_ON(!cur_pkt);
Frédéric Lécaille63556772021-12-29 17:18:21 +01003094
Frédéric Lécaille1809c332022-04-25 10:24:12 +02003095 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3096 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3097
Frédéric Lécaille63556772021-12-29 17:18:21 +01003098 total += cur_pkt->len;
3099 /* keep trace of the first packet in the datagram */
3100 if (!first_pkt)
3101 first_pkt = cur_pkt;
3102 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003103 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003104 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003105 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3106 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01003107 /* Let's say we have to build a new dgram */
3108 prv_pkt = NULL;
3109 dglen += cur_pkt->len;
3110 /* Client: discard the Initial encryption keys as soon as
3111 * a handshake packet could be built.
3112 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003113 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01003114 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3115 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3116 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3117 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3118 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01003119 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003120 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003121 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003122 }
3123 /* If the data for the current encryption level have all been sent,
3124 * select the next level.
3125 */
3126 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003127 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 +01003128 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3129 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3130 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3131 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003132 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3133 frms = &qc->els[tel].pktns->tx.frms;
3134 else
3135 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003136 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003137 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003138 /* If there is data for the next level, do not
3139 * consume a datagram.
3140 */
3141 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003142 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003143 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02003144
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003145 /* If we have to build a new datagram, set the current datagram as
3146 * prepared into <cbuf>.
3147 */
3148 if (!prv_pkt) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003149 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003150 first_pkt = NULL;
3151 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003152 padding = 0;
3153 }
3154 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003155 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003156 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3157 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003158 }
3159 }
3160
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003161 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003162 ret = total;
3163 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003164 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003165 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003166}
3167
Amaury Denoyellef2476052022-08-04 16:19:57 +02003168/* Send datagrams stored in <buf>.
3169 *
3170 * This function always returns 1 for success. Even if sendto() syscall failed,
3171 * buffer is drained and packets are considered as emitted. QUIC loss detection
3172 * mechanism is used as a back door way to retry sending.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003173 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003174int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003175{
3176 struct quic_conn *qc;
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003177 char skip_sendto = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003178
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003179 qc = ctx->qc;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003180 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003181 while (b_contig_data(buf, 0)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003182 unsigned char *pos;
3183 struct buffer tmpbuf = { };
3184 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3185 uint16_t dglen;
3186 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003187 unsigned int time_sent;
3188
Amaury Denoyellef2476052022-08-04 16:19:57 +02003189 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003190 dglen = read_u16(pos);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003191 BUG_ON_HOT(!dglen); /* this should not happen */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003192
3193 pos += sizeof dglen;
3194 first_pkt = read_ptr(pos);
3195 pos += sizeof first_pkt;
3196 tmpbuf.area = (char *)pos;
3197 tmpbuf.size = tmpbuf.data = dglen;
3198
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003199 /* If sendto is on error just skip the call to it for the rest
3200 * of the loop but continue to purge the buffer. Data will be
3201 * transmitted when QUIC packets are detected as lost on our
3202 * side.
3203 *
3204 * TODO use fd-monitoring to detect when send operation can be
3205 * retry. This should improve the bandwidth without relying on
3206 * retransmission timer. However, it requires a major rework on
3207 * quic-conn fd management.
3208 */
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003209 if (!skip_sendto) {
3210 if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) {
3211 skip_sendto = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003212 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003213 }
3214 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215
Amaury Denoyellef2476052022-08-04 16:19:57 +02003216 b_del(buf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003217 qc->tx.bytes += tmpbuf.data;
3218 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003219
3220 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3221 pkt->time_sent = time_sent;
3222 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3223 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003224 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003225 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3226 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003227 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003228 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3229 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3230 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003231 qc_notify_close(qc);
3232
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003233 /* RFC 9000 10.2. Immediate Close:
3234 * The closing and draining connection states exist to ensure
3235 * that connections close cleanly and that delayed or reordered
3236 * packets are properly discarded. These states SHOULD persist
3237 * for at least three times the current PTO interval...
3238 *
3239 * Rearm the idle timeout only one time when entering closing
3240 * state.
3241 */
3242 qc_idle_timer_do_rearm(qc);
3243 if (qc->timer_task) {
3244 task_destroy(qc->timer_task);
3245 qc->timer_task = NULL;
3246 }
3247 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003248 qc->path->in_flight += pkt->in_flight_len;
3249 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3250 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003251 qc_set_timer(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003252 TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003253 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003254 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003255 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003256 }
3257 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003258
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003259 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3260
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003261 return 1;
3262}
3263
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003264/* Copy into <buf> buffer a stateless reset token depending on the
3265 * <salt> salt input. This is the cluster secret which will be derived
3266 * as HKDF input secret to generate this token.
3267 * Return 1 if succeeded, 0 if not.
3268 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003269static int quic_stateless_reset_token_cpy(struct quic_conn *qc,
3270 unsigned char *buf, size_t len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003271 const unsigned char *salt, size_t saltlen)
3272{
3273 /* Input secret */
3274 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3275 size_t keylen = strlen(global.cluster_secret);
3276 /* Info */
3277 const unsigned char label[] = "stateless token";
3278 size_t labellen = sizeof label - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003279 int ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003280
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003281 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3282
3283 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003284 key, keylen, salt, saltlen, label, labellen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003285 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3286 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003287}
3288
3289/* Initialize the stateless reset token attached to <cid> connection ID.
3290 * Returns 1 if succeeded, 0 if not.
3291 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003292static int quic_stateless_reset_token_init(struct quic_conn *qc,
3293 struct quic_connection_id *quic_cid)
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003294{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003295 int ret;
3296
3297 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3298
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003299 if (global.cluster_secret) {
3300 /* Output secret */
3301 unsigned char *token = quic_cid->stateless_reset_token;
3302 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3303 /* Salt */
3304 const unsigned char *cid = quic_cid->cid.data;
3305 size_t cidlen = quic_cid->cid.len;
3306
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003307 ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003308 }
3309 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003310 /* TODO: RAND_bytes() should be replaced */
3311 ret = RAND_bytes(quic_cid->stateless_reset_token,
3312 sizeof quic_cid->stateless_reset_token) == 1;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003313 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003314
3315 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3316 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003317}
3318
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003319/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3320 * ebtree.
3321 *
3322 * The CID is randomly generated in part with the result altered to be
3323 * associated with the current thread ID. This means this function must only
3324 * be called by the quic_conn thread.
3325 *
3326 * Returns the new CID if succeeded, NULL if not.
3327 */
3328static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3329 struct quic_conn *qc,
3330 int seq_num)
3331{
3332 struct quic_connection_id *cid;
3333
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003334 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3335
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003336 cid = pool_alloc(pool_head_quic_connection_id);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003337 if (!cid) {
3338 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
3339 goto err;
3340 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003341
3342 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003343 /* TODO: RAND_bytes() should be replaced */
3344 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) {
3345 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003346 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003347 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003348
3349 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003350 if (quic_stateless_reset_token_init(qc, cid) != 1) {
3351 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003352 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003353 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003354
3355 cid->qc = qc;
3356
3357 cid->seq_num.key = seq_num;
3358 cid->retire_prior_to = 0;
3359 /* insert the allocated CID in the quic_conn tree */
3360 eb64_insert(root, &cid->seq_num);
3361
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003362 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003363 return cid;
3364
3365 err:
3366 pool_free(pool_head_quic_connection_id, cid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003367 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003368 return NULL;
3369}
3370
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003371/* Build all the frames which must be sent just after the handshake have succeeded.
3372 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3373 * a HANDSHAKE_DONE frame.
3374 * Return 1 if succeeded, 0 if not.
3375 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003376static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003377{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003378 int ret = 0, i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003379 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003380 struct quic_frame *frm, *frmbak;
3381 struct list frm_list = LIST_HEAD_INIT(frm_list);
3382 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003383
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003384 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
3385
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003386 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003387 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003388 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003389 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003390 if (!frm) {
3391 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3392 goto leave;
3393 }
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003394
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003395 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003396 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003397 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003398 }
3399
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003400 /* Initialize <max> connection IDs minus one: there is
3401 * already one connection ID used for the current connection.
3402 */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003403 first = 1;
3404 max = qc->tx.params.active_connection_id_limit;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003405
3406 /* TODO: check limit */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003407 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003408 struct quic_connection_id *cid;
3409
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003410 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003411 if (!frm) {
3412 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003413 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003414 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003415
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003416 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003417 cid = new_quic_cid(&qc->cids, qc, i);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003418 if (!cid) {
3419 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003420 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003421 }
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003422
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003423 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003424 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003425
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003426 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003427 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003428 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003429
3430 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003431 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003432
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003433 ret = 1;
3434 leave:
3435 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
3436 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003437
3438 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003439 /* free the frames */
3440 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3441 pool_free(pool_head_quic_frame, frm);
3442
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003443 node = eb64_lookup_ge(&qc->cids, first);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003444 while (node) {
3445 struct quic_connection_id *cid;
3446
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003447 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003448 if (cid->seq_num.key >= max)
3449 break;
3450
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003451 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003452 ebmb_delete(&cid->node);
3453 eb64_delete(&cid->seq_num);
3454 pool_free(pool_head_quic_connection_id, cid);
3455 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003456 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003457}
3458
3459/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003460void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003461{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003462 struct eb64_node *n;
3463 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003464
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003465 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
3466
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003467 n = eb64_first(&arngs->root);
3468 while (n) {
3469 struct eb64_node *next;
3470
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003471 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003472 next = eb64_next(n);
3473 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003474 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003475 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003476 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003477
3478 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003479}
3480
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003481/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3482 * descending order.
3483 */
3484static inline size_t sack_gap(struct quic_arng_node *p,
3485 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003486{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003487 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003488}
3489
3490
3491/* Remove the last elements of <ack_ranges> list of ack range updating its
3492 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003493 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003494 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003495static int quic_rm_last_ack_ranges(struct quic_conn *qc,
3496 struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003497{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003498 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003499 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003500
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003501 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3502
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003503 last = eb64_last(&arngs->root);
3504 while (last && arngs->enc_sz > limit) {
3505 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003506
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003507 prev = eb64_prev(last);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003508 if (!prev) {
3509 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
3510 goto out;
3511 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003512
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003513 last_node = eb64_entry(last, struct quic_arng_node, first);
3514 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003515 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3516 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3517 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3518 --arngs->sz;
3519 eb64_delete(last);
3520 pool_free(pool_head_quic_arng, last);
3521 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003522 }
3523
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003524 ret = 1;
3525 out:
3526 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3527 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003528}
3529
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003530/* Set the encoded size of <arngs> QUIC ack ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003531static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003532{
3533 struct eb64_node *node, *next;
3534 struct quic_arng_node *ar, *ar_next;
3535
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003536 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3537
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003538 node = eb64_last(&arngs->root);
3539 if (!node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003540 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003541
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003542 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003543 arngs->enc_sz = quic_int_getsize(ar->last) +
3544 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3545
3546 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003547 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003548 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3549 quic_int_getsize(ar_next->last - ar_next->first.key);
3550 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003551 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003552 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003553
3554 leave:
3555 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003556}
3557
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003558/* Insert <ar> ack range into <argns> tree of ack ranges.
3559 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003560 */
3561static inline
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003562struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
3563 struct quic_arngs *arngs,
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003564 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003565{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003566 struct quic_arng_node *new_ar;
3567
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003568 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3569
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003570 new_ar = pool_alloc(pool_head_quic_arng);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003571 if (!new_ar) {
3572 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
3573 goto leave;
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003574 }
3575
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003576 new_ar->first.key = ar->first;
3577 new_ar->last = ar->last;
3578 eb64_insert(&arngs->root, &new_ar->first);
3579 arngs->sz++;
3580
3581 leave:
3582 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003583 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003584}
3585
3586/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003587 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003588 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003589 *
3590 * Descending order
3591 * ------------->
3592 * range1 range2
3593 * ..........|--------|..............|--------|
3594 * ^ ^ ^ ^
3595 * | | | |
3596 * last1 first1 last2 first2
3597 * ..........+--------+--------------+--------+......
3598 * diff1 gap12 diff2
3599 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003600 * To encode the previous list of ranges we must encode integers as follows in
3601 * descending order:
3602 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003603 * with diff1 = last1 - first1
3604 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003605 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003606 *
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003607
3608returns 0 on error
3609
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003610 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003611int quic_update_ack_ranges_list(struct quic_conn *qc,
3612 struct quic_arngs *arngs,
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003613 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003614{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003615 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003616 struct eb64_node *le;
3617 struct quic_arng_node *new_node;
3618 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003619
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003620 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3621
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003622 new = NULL;
3623 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003624 new_node = quic_insert_new_range(qc, arngs, ar);
3625 if (new_node)
3626 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003627
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003628 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003629 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003630
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003631 le = eb64_lookup_le(&arngs->root, ar->first);
3632 if (!le) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003633 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003634 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003635 goto leave;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003636
3637 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003638 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003639 else {
3640 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003641 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003642
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003643 /* Already existing range */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003644 if (le_ar->last >= ar->last) {
3645 ret = 1;
3646 }
3647 else if (le_ar->last + 1 >= ar->first) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003648 le_ar->last = ar->last;
3649 new = le;
3650 new_node = le_ar;
3651 }
3652 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003653 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003654 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003655 goto leave;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003656
3657 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003658 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003659 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003660
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003661 /* Verify that the new inserted node does not overlap the nodes
3662 * which follow it.
3663 */
3664 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003665 struct eb64_node *next;
3666 struct quic_arng_node *next_node;
3667
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003668 while ((next = eb64_next(new))) {
3669 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003670 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003671 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003672 break;
3673
3674 if (next_node->last > new_node->last)
3675 new_node->last = next_node->last;
3676 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003677 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003678 /* Decrement the size of these ranges. */
3679 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003680 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003681 }
3682
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003683 ret = 1;
3684 leave:
3685 quic_arngs_set_enc_sz(qc, arngs);
3686 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
3687 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003688}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003689/* Remove the header protection of packets at <el> encryption level.
3690 * Always succeeds.
3691 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003692static 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 +01003693{
3694 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003695 struct quic_rx_packet *pqpkt;
3696 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003697 struct quic_enc_level *app_qel;
3698
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003699 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3700 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003701 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003702 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003703 TRACE_DEVEL("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003704 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003705 goto out;
3706 }
3707 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003708 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003709 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02003710 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003711 TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712 }
3713 else {
3714 /* The AAD includes the packet number field */
3715 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3716 /* Store the packet into the tree of packets to decrypt. */
3717 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003718 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3719 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003720 TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003721 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003722 MT_LIST_DELETE_SAFE(pkttmp1);
3723 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003724 }
3725
3726 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003727 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003728}
3729
3730/* Process all the CRYPTO frame at <el> encryption level.
3731 * Return 1 if succeeded, 0 if not.
3732 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003733static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
3734 struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003735 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003736{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003737 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003738 struct eb64_node *node;
3739
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003740 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3741
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003742 node = eb64_first(&el->rx.crypto.frms);
3743 while (node) {
3744 struct quic_rx_crypto_frm *cf;
3745
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003746 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003747 if (cf->offset_node.key != el->rx.crypto.offset)
3748 break;
3749
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003750 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) {
3751 TRACE_ERROR("qc_provide_cdata() failed", QUIC_EV_CONN_TRMHP);
3752 goto leave;
3753 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003754
3755 node = eb64_next(node);
3756 quic_rx_packet_refdec(cf->pkt);
3757 eb64_delete(&cf->offset_node);
3758 pool_free(pool_head_quic_rx_crypto_frm, cf);
3759 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003760
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003761 ret = 1;
3762 leave:
3763 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3764 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003765}
3766
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003767/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003768 * 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 +02003769 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003770 * Return 1 if succeeded, 0 if not.
3771 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003772int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3773 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003775 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003776 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003777 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003778 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003779 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003780 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003781
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003782 TRACE_ENTER(QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003783 qel = cur_el;
3784 next_tel:
3785 if (!qel)
3786 goto out;
3787
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003788 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003789 while (node) {
3790 struct quic_rx_packet *pkt;
3791
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003792 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003793 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003794 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003795 if (!qc_pkt_decrypt(pkt, qel, qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003796 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003797 TRACE_ERROR("packet decryption failed -> dropped",
3798 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003799 }
3800 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003801 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003802 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003803 TRACE_ERROR("packet parsing failed -> dropped",
3804 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02003805 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003806 }
3807 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003808 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3809
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003810 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3811 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3812 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003813 qc_idle_timer_rearm(qc, 1);
3814 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003815 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003816 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003817 largest_pn_time_received = pkt->time_received;
3818 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003819 /* Update the list of ranges to acknowledge. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003820 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
3821 TRACE_ERROR("Could not update ack range list",
3822 QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003823 }
3824 }
3825 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003826 eb64_delete(&pkt->pn_node);
3827 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003828 }
3829
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003830 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3831 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003832 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003833 /* Update the largest acknowledged packet timestamps */
3834 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3835 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3836 }
3837
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003838 if (!qc_treat_rx_crypto_frms(qc, qel, ctx)) {
3839 // trace already emitted by function above
3840 goto leave;
3841 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003842
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003843 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003844 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003845 qel = next_el;
Frédéric Lécaille91a211f2022-05-24 10:54:42 +02003846 largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003847 goto next_tel;
3848 }
3849
3850 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003851 ret = 1;
3852 leave:
3853 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, ctx->qc);
3854 return ret;
3855}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003856
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003857/* Check if it's possible to remove header protection for packets related to
3858 * encryption level <qel>. If <qel> is NULL, assume it's false.
3859 *
3860 * Return true if the operation is possible else false.
3861 */
3862static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3863{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003864 int ret = 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003865 enum quic_tls_enc_level tel;
3866
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003867 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3868
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003869 if (!qel)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003870 goto cant_rm_hp;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003871
3872 tel = ssl_to_quic_enc_level(qel->level);
3873
3874 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003875 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003876 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003877 goto cant_rm_hp;
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003878 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003879
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003880 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3881 TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc);
3882 goto cant_rm_hp;
3883 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003884
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003885 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003886 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003887 qc->mux_state == QC_MUX_NULL) {
3888 TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
3889 goto cant_rm_hp;
3890 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003891
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003892 ret = 1;
3893 cant_rm_hp:
3894 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3895 return ret;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003896}
3897
Amaury Denoyellecc130472022-08-17 10:08:16 +02003898/* Try to send application frames from list <frms> on connection <qc>.
3899 *
Amaury Denoyelle70467562022-08-17 16:33:13 +02003900 * Use qc_send_app_probing wrapper when probing with old data.
Amaury Denoyellecc130472022-08-17 10:08:16 +02003901 *
3902 * Returns 1 on success. Some data might not have been sent due to congestion,
3903 * in this case they are left in <frms> input list. The caller may subscribe on
3904 * quic-conn to retry later.
3905 *
3906 * Returns 0 on critical error.
3907 * TODO review and classify more distinctly transient from definitive errors to
3908 * allow callers to properly handle it.
3909 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003910static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003911{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003912 int status = 0;
3913 struct buffer *buf;
3914
3915 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3916
3917 buf = qc_txb_alloc(qc);
3918 if (!buf) {
3919 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
3920 goto leave;
3921 }
3922
Frédéric Lécailledc077512022-07-26 09:17:19 +02003923 /* Prepare and send packets until we could not further prepare packets. */
3924 while (1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003925 int ret;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003926 /* Currently buf cannot be non-empty at this stage. Even if a
3927 * previous sendto() has failed it is emptied to simulate
3928 * packet emission and rely on QUIC lost detection to try to
3929 * emit it.
3930 */
3931 BUG_ON_HOT(b_data(buf));
3932 b_reset(buf);
3933
Amaury Denoyellef2476052022-08-04 16:19:57 +02003934 ret = qc_prep_app_pkts(qc, buf, frms);
Frédéric Lécailledc077512022-07-26 09:17:19 +02003935 if (ret == -1)
3936 goto err;
3937 else if (ret == 0)
3938 goto out;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003939
Amaury Denoyellef2476052022-08-04 16:19:57 +02003940 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécailledc077512022-07-26 09:17:19 +02003941 goto err;
3942 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003943
3944 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003945 status = 1;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003946 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003947 leave:
3948 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3949 return status;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003950
3951 err:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003952 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003953 goto leave;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003954}
3955
Amaury Denoyellecc130472022-08-17 10:08:16 +02003956/* Try to send application frames from list <frms> on connection <qc>. Use this
Amaury Denoyelle70467562022-08-17 16:33:13 +02003957 * function when probing is required.
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003958 *
3959 * Returns the result from qc_send_app_pkts function.
3960 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003961static forceinline int qc_send_app_probing(struct quic_conn *qc,
3962 struct list *frms)
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003963{
3964 int ret;
3965
3966 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3967
Amaury Denoyelle70467562022-08-17 16:33:13 +02003968 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
3969 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003970 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003971 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003972
3973 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3974 return ret;
3975}
3976
Amaury Denoyelle70467562022-08-17 16:33:13 +02003977/* Try to send application frames from list <frms> on connection <qc>. This
3978 * function is provided for MUX upper layer usage only.
Amaury Denoyellecc130472022-08-17 10:08:16 +02003979 *
3980 * Returns the result from qc_send_app_pkts function.
3981 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003982int qc_send_mux(struct quic_conn *qc, struct list *frms)
Amaury Denoyellecc130472022-08-17 10:08:16 +02003983{
3984 int ret;
3985
3986 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003987 BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
Amaury Denoyellecc130472022-08-17 10:08:16 +02003988
Amaury Denoyelle70467562022-08-17 16:33:13 +02003989 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
3990 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02003991 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003992 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02003993
3994 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3995 return ret;
3996}
3997
Frédéric Lécaillea9568412022-04-25 08:58:04 +02003998/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
3999 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004000 * QUIC connection. <old_data> is used as boolean to send data already sent but
4001 * not already acknowledged (in flight).
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004002 * Returns 1 if succeeded, 0 if not.
4003 */
4004int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4005 enum quic_tls_enc_level tel, struct list *tel_frms,
4006 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4007{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004008 int ret, status = 0;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004009 struct buffer *buf = qc_txb_alloc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004010
4011 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4012
4013 if (!buf) {
4014 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4015 goto leave;
4016 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004017
Amaury Denoyellef2476052022-08-04 16:19:57 +02004018 /* Currently buf cannot be non-empty at this stage. Even if a previous
4019 * sendto() has failed it is emptied to simulate packet emission and
4020 * rely on QUIC lost detection to try to emit it.
4021 */
4022 BUG_ON_HOT(b_data(buf));
4023 b_reset(buf);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004024
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004025 if (old_data) {
4026 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004027 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004028 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02004029
4030 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004031 if (ret == -1)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004032 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004033 else if (ret == 0)
4034 goto skip_send;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004035
Amaury Denoyellef2476052022-08-04 16:19:57 +02004036 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004037 goto out;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004038
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004039 skip_send:
4040 status = 1;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004041 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004042 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004043 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004044 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004045 leave:
4046 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4047 return status;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004048}
4049
4050/* Retransmit up to two datagrams depending on packet number space */
4051static void qc_dgrams_retransmit(struct quic_conn *qc)
4052{
4053 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4054 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4055 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4056
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004057 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4058
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004059 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4060 struct list ifrms = LIST_HEAD_INIT(ifrms);
4061 struct list hfrms = LIST_HEAD_INIT(hfrms);
4062
4063 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004064 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4065 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004066 if (!LIST_ISEMPTY(&ifrms)) {
4067 iqel->pktns->tx.pto_probe = 1;
4068 if (!LIST_ISEMPTY(&hfrms)) {
4069 hqel->pktns->tx.pto_probe = 1;
4070 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4071 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
4072 }
4073 }
4074 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4075 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004076 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004077 if (!LIST_ISEMPTY(&hfrms)) {
4078 hqel->pktns->tx.pto_probe = 1;
4079 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
4080 QUIC_TLS_ENC_LEVEL_NONE, NULL);
4081 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004082 TRACE_STATE("no more need to probe Handshake packet number space",
4083 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004084 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4085 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004086 TRACE_STATE("no more need to probe Initial packet number space",
4087 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004088 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4089 }
4090 else {
4091 int i;
4092 struct list frms1 = LIST_HEAD_INIT(frms1);
4093 struct list frms2 = LIST_HEAD_INIT(frms2);
4094
4095 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4096 hqel->pktns->tx.pto_probe = 0;
4097 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4098 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004099 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004100 if (!LIST_ISEMPTY(&frms1)) {
4101 hqel->pktns->tx.pto_probe = 1;
4102 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4103 QUIC_TLS_ENC_LEVEL_NONE, NULL);
4104 }
4105 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004106 TRACE_STATE("no more need to probe Handshake packet number space",
4107 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004108 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4109 }
4110 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4111 aqel->pktns->tx.pto_probe = 0;
4112 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4113 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4114 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4115 if (!LIST_ISEMPTY(&frms1)) {
4116 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004117 qc_send_app_probing(qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004118 }
4119 if (!LIST_ISEMPTY(&frms2)) {
4120 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004121 qc_send_app_probing(qc, &frms2);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004122 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004123 TRACE_STATE("no more need to probe 01RTT packet number space",
4124 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004125 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4126 }
4127 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004128 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004129}
4130
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004131/* QUIC connection packet handler task (post handshake) */
4132static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4133{
4134 struct ssl_sock_ctx *ctx;
4135 struct quic_conn *qc;
4136 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004137
4138
4139 ctx = context;
4140 qc = ctx->qc;
4141 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004142
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004143 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4144 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004145
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004146 /* Retranmissions */
4147 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004148 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004149 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4150 qc_dgrams_retransmit(qc);
4151 }
4152
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004153 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
4154 qc_rm_hp_pkts(qc, qel);
4155
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004156 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) {
4157 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4158 goto out;
4159 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004160
Frédéric Lécaille47756802022-03-25 09:12:16 +01004161 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004162 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4163 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01004164 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004165 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004166
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004167 /* XXX TODO: how to limit the list frames to send */
Amaury Denoyelle70467562022-08-17 16:33:13 +02004168 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
4169 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004170 goto out;
4171 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004172
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004173 out:
4174 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004175 return t;
4176}
4177
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004178/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4179static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4180{
4181 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
4182 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
4183 qel->pktns->tx.pto_probe ||
4184 !LIST_ISEMPTY(&qel->pktns->tx.frms);
4185}
4186
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004187/* QUIC connection packet handler task. */
4188struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004189{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004190 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004191 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004192 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004193 enum quic_tls_enc_level tel, next_tel;
4194 struct quic_enc_level *qel, *next_qel;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004195 struct buffer *buf = NULL;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004196 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004197
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004198 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004199 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004200 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004201 st = qc->state;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004202 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004203
4204 /* Retranmissions */
4205 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004206 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004207 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4208 qc_dgrams_retransmit(qc);
4209 }
4210
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004211 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
4212 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004213 TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached",
4214 QUIC_EV_CONN_IO_CB, qc);
4215 /* 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 +01004216 * after the anti-amplification was reached.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004217 *
4218 * TODO: this part should be removed. This was there because the
4219 * datagram parser was not executed by only one thread.
Frédéric Lécaille59507de2022-08-11 12:14:07 +02004220 */
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004221 qc_set_timer(qc);
4222 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
4223 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
4224 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004225 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004226 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
4227 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
4228 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004229 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004230 if (st >= QUIC_HS_ST_COMPLETE &&
4231 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004232 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004233 /* There may be remaining Handshake packets to treat and acknowledge. */
4234 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4235 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4236 }
4237 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004238 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004239
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004240 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02004241 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004242
4243 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004244 /* Treat packets waiting for header packet protection decryption */
4245 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004246 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004247
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004248 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
4249 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4250 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004251 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004252
Frédéric Lécaille47756802022-03-25 09:12:16 +01004253 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4254 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4255 goto out;
4256
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004257 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
4258 !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
4259 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
4260 qel = next_qel;
4261 next_qel = NULL;
4262 goto next_level;
4263 }
4264 else {
4265 struct quic_rx_packet *pkt;
4266 struct mt_list *elt1, elt2;
4267 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
4268
4269 /* Drop these 0-RTT packets */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004270 TRACE_DEVEL("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004271 mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) {
4272 MT_LIST_DELETE_SAFE(elt1);
4273 quic_rx_packet_refdec(pkt);
4274 }
4275 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004276 }
4277
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004278 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004279 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004280 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004281 !quic_build_post_handshake_frames(qc))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004282 goto out;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01004283
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004284 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004285 QUIC_FL_TLS_SECRETS_DCD)) {
4286 /* Discard the Handshake keys. */
4287 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
4288 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
4289 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
4290 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004291 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004292 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004293 }
4294
4295 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
4296 /* There may be remaining handshake to build (acks) */
4297 st = QUIC_HS_ST_SERVER_HANDSHAKE;
4298 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004299 }
4300
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01004301 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
4302 * be considered.
4303 */
4304 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004305 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004306
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004307 if (!qc_need_sending(qc, qel) &&
4308 (!next_qel || !qc_need_sending(qc, next_qel))) {
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004309 goto skip_send;
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004310 }
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004311
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004312 buf = qc_txb_alloc(qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02004313 if (!buf)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004314 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004315
4316 /* Currently buf cannot be non-empty at this stage. Even if a previous
4317 * sendto() has failed it is emptied to simulate packet emission and
4318 * rely on QUIC lost detection to try to emit it.
4319 */
4320 BUG_ON_HOT(b_data(buf));
4321 b_reset(buf);
4322
4323 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02004324 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004325 if (ret == -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004326 goto out;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004327 else if (ret == 0)
4328 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004329
Amaury Denoyellef2476052022-08-04 16:19:57 +02004330 if (!qc_send_ppkts(buf, ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004331 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004332
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004333 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004334 /* Check if there is something to do for the next level.
4335 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004336 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004337 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01004338 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004339 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004340 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004341 goto next_level;
4342 }
4343
Frédéric Lécaille47756802022-03-25 09:12:16 +01004344 out:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004345 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004346 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004347 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004348}
4349
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004350/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004351static 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 +01004352{
4353 int i;
4354
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004355 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4356
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004357 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4358 if (qel->tx.crypto.bufs[i]) {
4359 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4360 qel->tx.crypto.bufs[i] = NULL;
4361 }
4362 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004363 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004364
4365 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004366}
4367
4368/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004369 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004370 * Returns 1 if succeeded, 0 if not.
4371 */
4372static int quic_conn_enc_level_init(struct quic_conn *qc,
4373 enum quic_tls_enc_level level)
4374{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004375 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004376 struct quic_enc_level *qel;
4377
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004378 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4379
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004380 qel = &qc->els[level];
4381 qel->level = quic_to_ssl_enc_level(level);
4382 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4383 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4384 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004385 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004386
4387 qel->rx.pkts = EB_ROOT;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02004388 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004389 qel->rx.crypto.offset = 0;
4390 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004391
4392 /* Allocate only one buffer. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004393 /* TODO: use a pool */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004394 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4395 if (!qel->tx.crypto.bufs)
4396 goto err;
4397
4398 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4399 if (!qel->tx.crypto.bufs[0])
4400 goto err;
4401
4402 qel->tx.crypto.bufs[0]->sz = 0;
4403 qel->tx.crypto.nb_buf = 1;
4404
4405 qel->tx.crypto.sz = 0;
4406 qel->tx.crypto.offset = 0;
4407
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004408 ret = 1;
4409 leave:
4410 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4411 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004412
4413 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004414 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004415 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004416}
4417
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004418/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4419 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004420 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004421 * This function must only be called by the thread responsible of the quic_conn
4422 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004423 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004424static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004425{
4426 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004427 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004428 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004429 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004430 struct quic_rx_packet *pkt, *pktback;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004431
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004432 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4433
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004434 /* We must not free the quic-conn if the MUX is still allocated. */
4435 BUG_ON(qc->mux_state == QC_MUX_READY);
4436
Willy Tarreau54bc7862022-08-10 07:26:27 +02004437 /* in the unlikely (but possible) case the connection was just added to
4438 * the accept_list we must delete it from there.
4439 */
4440 MT_LIST_DELETE(&qc->accept_list);
4441
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004442 /* free remaining stream descriptors */
4443 node = eb64_first(&qc->streams_by_id);
4444 while (node) {
4445 struct qc_stream_desc *stream;
4446
4447 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4448 node = eb64_next(node);
4449
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004450 /* all streams attached to the quic-conn are released, so
4451 * qc_stream_desc_free will liberate the stream instance.
4452 */
4453 BUG_ON(!stream->release);
4454 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004455 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004456
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004457 /* Purge Rx packet list. */
4458 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
4459 LIST_DELETE(&pkt->qc_rx_pkt_list);
4460 pool_free(pool_head_quic_rx_packet, pkt);
4461 }
4462
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004463 if (qc->idle_timer_task) {
4464 task_destroy(qc->idle_timer_task);
4465 qc->idle_timer_task = NULL;
4466 }
4467
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004468 if (qc->timer_task) {
4469 task_destroy(qc->timer_task);
4470 qc->timer_task = NULL;
4471 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004472
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004473 /* remove the connection from receiver cids trees */
4474 ebmb_delete(&qc->odcid_node);
4475 ebmb_delete(&qc->scid_node);
4476 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004477
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004478 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004479 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004480 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004481 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004482 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004483 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004484
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004485 quic_tls_ku_free(qc);
4486 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4487 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004488 quic_conn_enc_level_uninit(qc, &qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004489 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004490 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004491
4492 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4493 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4494 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004495
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004496 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
Frédéric Lécaille8ddde4f2022-08-01 14:07:50 +02004497 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004498 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004499 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004500
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004501 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4502 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004503 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004504
4505 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004506}
4507
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004508static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004509{
4510 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004511 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004512
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004513 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004514
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004515 /* Next application data can be dropped. */
4516 qc->mux_state = QC_MUX_RELEASED;
4517
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004518 /* If the quic-conn timer has already expired free the quic-conn. */
4519 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4520 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004521 goto leave;
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004522 }
4523
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02004524 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004525 leave:
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004526 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004527}
4528
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004529/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004530static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004531{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004532 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004533 struct quic_conn *qc;
4534 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004535
4536 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004537 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004538 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004539 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004540 task->expire = TICK_ETERNITY;
4541 pktns = quic_loss_pktns(qc);
4542 if (tick_isset(pktns->tx.loss_time)) {
4543 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4544
4545 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
4546 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004547 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
4548 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004549 goto out;
4550 }
4551
4552 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004553 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004554 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4555 qc->qcc->subs->events & SUB_RETRY_SEND) {
4556 struct qcc *qcc = qc->qcc;
4557
4558 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4559 tasklet_wakeup(qcc->subs->tasklet);
4560 qcc->subs->events &= ~SUB_RETRY_SEND;
4561 if (!qcc->subs->events)
4562 qcc->subs = NULL;
4563 }
4564 else {
4565 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4566 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4567 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004568 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4569 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004570 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004571 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4572 }
4573 }
4574 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
4575 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4576 }
4577 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
4578 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004579 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004580 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004581 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004582 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004583 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4584 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4585
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004586 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004587 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004588 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004589 iel->pktns->tx.pto_probe = 1;
4590 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004591
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004592 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4593 qc->path->loss.pto_count++;
4594
4595 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004596 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004597
4598 return task;
4599}
4600
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004601/* Parse the Retry token from buffer <token> with <end> a pointer to
4602 * one byte past the end of this buffer. This will extract the ODCID
4603 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004604 *
4605 * Returns 0 on success else non-zero.
4606 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004607static int parse_retry_token(struct quic_conn *qc,
4608 const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004609 struct quic_cid *odcid)
4610{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004611 int ret = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004612 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004613 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004614
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004615 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
4616
4617 if (!quic_dec_int(&odcid_len, &token, end)) {
4618 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
4619 goto leave;
4620 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004621
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004622 /* RFC 9000 7.2. Negotiating Connection IDs:
4623 * When an Initial packet is sent by a client that has not previously
4624 * received an Initial or Retry packet from the server, the client
4625 * populates the Destination Connection ID field with an unpredictable
4626 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4627 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004628 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
4629 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
4630 goto leave;
4631 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004632
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004633 if (end - token < odcid_len + sizeof timestamp) {
4634 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
4635 goto leave;
4636 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004637
4638 timestamp = ntohl(read_u32(token + odcid_len));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004639 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
4640 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
4641 goto leave;
4642 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004643
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004644 ret = 1;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004645 memcpy(odcid->data, token, odcid_len);
4646 odcid->len = odcid_len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004647 leave:
4648 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
4649 return !ret;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004650}
4651
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004652/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4653 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4654 * for QUIC servers (or haproxy listeners).
4655 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4656 * <token> the token found to be used for this connection with <token_len> as
4657 * length. <saddr> is the source address.
4658 * Returns the connection if succeeded, NULL if not.
4659 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004660static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004661 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02004662 const struct quic_cid *token_odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004663 struct sockaddr_storage *saddr,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004664 int server, int token, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004665{
4666 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004667 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004668 /* Initial CID. */
4669 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004670 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004671 struct listener *l = NULL;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004672 struct quic_cc_algo *cc_algo = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004673
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004674 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004675 qc = pool_zalloc(pool_head_quic_conn);
4676 if (!qc) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004677 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004678 goto err;
4679 }
4680
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004681 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4682 if (!buf_area) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004683 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004684 goto err;
4685 }
4686
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004687 qc->cids = EB_ROOT;
4688 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004689 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004690 struct proxy *prx;
4691
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004692 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004693 prx = l->bind_conf->frontend;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004694 cc_algo = l->bind_conf->quic_cc_algo;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004695
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004696 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4697 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004698 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004699 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004700 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004701 qc->odcid.len = dcid->len;
4702 qc->odcid.addrlen = dcid->addrlen;
4703 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004704
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004705 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004706 if (scid->len)
4707 memcpy(qc->dcid.data, scid->data, scid->len);
4708 qc->dcid.len = scid->len;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004709 qc->tx.buf = BUF_NULL;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004710 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004711 }
4712 /* QUIC Client (outgoing connection to servers) */
4713 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004714 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004715 if (dcid->len)
4716 memcpy(qc->dcid.data, dcid->data, dcid->len);
4717 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004718 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004719 qc->mux_state = QC_MUX_NULL;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02004720 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004722 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004723 if (!icid) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004724 TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004725 goto err;
4726 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004727
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004728 /* insert the allocated CID in the receiver datagram handler tree */
4729 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004730 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004731
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004732 /* Select our SCID which is the first CID with 0 as sequence number. */
4733 qc->scid = icid->cid;
4734
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004735 /* Packet number spaces initialization. */
4736 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4737 quic_pktns_init(&qc->pktns[i]);
4738 /* QUIC encryption level context initialization. */
4739 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004740 if (!quic_conn_enc_level_init(qc, i)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004741 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004742 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004743 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004744 /* Initialize the packet number space. */
4745 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4746 }
4747
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004748 qc->original_version = qv;
4749 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004750 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4751 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004752 /* TX part. */
4753 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004754 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4755 qc->tx.wbuf = qc->tx.rbuf = 0;
4756 qc->tx.bytes = 0;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004757 qc->tx.buf = BUF_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004758 /* RX part. */
4759 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004760 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004761 for (i = 0; i < QCS_MAX_TYPES; i++)
4762 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004763
4764 qc->nb_pkt_for_cc = 1;
4765 qc->nb_pkt_since_cc = 0;
4766
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004767 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004768 if (!quic_tls_ku_init(qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004769 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004770 goto err;
4771 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004772
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004773 /* XXX TO DO: Only one path at this time. */
4774 qc->path = &qc->paths[0];
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004775 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 +01004776
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004777 /* required to use MTLIST_IN_LIST */
4778 MT_LIST_INIT(&qc->accept_list);
4779
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004780 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004781 qc->stream_buf_count = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004782 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4783
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004784 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
4785 icid->stateless_reset_token,
4786 dcid->data, dcid->len,
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02004787 qc->scid.data, qc->scid.len, token_odcid))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004788 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004789
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004790 if (qc_conn_alloc_ssl_ctx(qc) ||
4791 !quic_conn_init_timer(qc) ||
4792 !quic_conn_init_idle_timer_task(qc))
4793 goto err;
4794
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004795 if (!qc_new_isecs(qc, &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx,
4796 qc->original_version, dcid->data, dcid->len, 1))
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004797 goto err;
4798
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004799 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004800
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004801 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004802
4803 err:
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004804 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4805 if (qc)
4806 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004807 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004808 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004809 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004810}
4811
4812/* Initialize the timer task of <qc> QUIC connection.
4813 * Returns 1 if succeeded, 0 if not.
4814 */
4815static int quic_conn_init_timer(struct quic_conn *qc)
4816{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004817 int ret = 0;
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004818 /* Attach this task to the same thread ID used for the connection */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004819 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4820
Willy Tarreau87168752022-06-13 16:31:53 +02004821 qc->timer_task = task_new_on(qc->tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004822 if (!qc->timer_task) {
4823 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4824 goto leave;
4825 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004826
4827 qc->timer = TICK_ETERNITY;
4828 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004829 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004830
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004831 ret = 1;
4832 leave:
4833 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4834 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004835}
4836
Frédéric Lécaille47756802022-03-25 09:12:16 +01004837/* Rearm the idle timer for <qc> QUIC connection. */
4838static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4839{
4840 unsigned int expire;
4841
4842 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4843 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4844}
4845
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004846/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4847 * which is set to 1 when receiving a packet , and 0 when sending packet
4848 */
4849static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4850{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004851 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4852
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004853 if (read) {
4854 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4855 }
4856 else {
4857 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4858 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004859 qc_idle_timer_do_rearm(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004860
4861 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004862}
4863
4864/* The task handling the idle timeout */
4865static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4866{
4867 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004868 struct quic_counters *prx_counters = qc->prx_counters;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004869 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004870
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004871 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4872
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004873 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4874 * might free the quic-conn too early via quic_close().
4875 */
4876 qc_notify_close(qc);
4877
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004878 /* If the MUX is still alive, keep the quic-conn. The MUX is
4879 * responsible to call quic_close to release it.
4880 */
4881 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4882 if (qc->mux_state != QC_MUX_READY)
4883 quic_conn_release(qc);
4884
4885 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4886 * least clean some parts of it such as the tasklet.
4887 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004888
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004889 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
4890 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004891 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004892 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004893 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004894
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004895 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004896 return NULL;
4897}
4898
4899/* Initialize the idle timeout task for <qc>.
4900 * Returns 1 if succeeded, 0 if not.
4901 */
4902static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4903{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004904 int ret = 0;
4905
4906 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4907
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004908 qc->idle_timer_task = task_new_here();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004909 if (!qc->idle_timer_task) {
4910 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4911 goto leave;
4912 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004913
4914 qc->idle_timer_task->process = qc_idle_timer_task;
4915 qc->idle_timer_task->context = qc;
4916 qc_idle_timer_rearm(qc, 1);
4917 task_queue(qc->idle_timer_task);
4918
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004919 ret = 1;
4920 leave:
4921 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4922 return ret;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004923}
4924
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004925/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4926 * past one byte of this buffer.
4927 */
4928static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4929 struct quic_rx_packet *pkt)
4930{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004931 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004932 unsigned char dcid_len, scid_len;
4933
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004934 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
4935
4936 if (end == *buf) {
4937 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
4938 goto leave;
4939 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004940
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004941 /* Destination Connection ID Length */
4942 dcid_len = *(*buf)++;
4943 /* 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 +02004944 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
4945 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
4946 goto leave;
4947 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004948
4949 if (dcid_len) {
4950 /* Check that the length of this received DCID matches the CID lengths
4951 * of our implementation for non Initials packets only.
4952 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004953 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4954 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004955 dcid_len != QUIC_HAP_CID_LEN) {
4956 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
4957 goto leave;
4958 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004959
4960 memcpy(pkt->dcid.data, *buf, dcid_len);
4961 }
4962
4963 pkt->dcid.len = dcid_len;
4964 *buf += dcid_len;
4965
4966 /* Source Connection ID Length */
4967 scid_len = *(*buf)++;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004968 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
4969 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
4970 goto leave;
4971 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004972
4973 if (scid_len)
4974 memcpy(pkt->scid.data, *buf, scid_len);
4975 pkt->scid.len = scid_len;
4976 *buf += scid_len;
4977
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004978 ret = 1;
4979 leave:
4980 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
4981 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004982}
4983
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004984/* Insert <pkt> RX packet in its <qel> RX packets tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004985static void qc_pkt_insert(struct quic_conn *qc,
4986 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004987{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004988 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4989
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004990 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004991 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004992 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004993
4994 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004995}
4996
4997/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004998 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4999 * byte past the end of the buffer containing this packet and <beg> the address of
5000 * the packet first byte.
5001 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
5002 * Returns 1 if succeeded, 0 if not.
5003 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005004static inline int qc_try_rm_hp(struct quic_conn *qc,
5005 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005006 unsigned char *buf, unsigned char *beg,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005007 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005008{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005009 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005010 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005011 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005012 struct quic_enc_level *qel;
5013 /* Only for traces. */
5014 struct quic_rx_packet *qpkt_trace;
5015
5016 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005017 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005018 /* The packet number is here. This is also the start minus
5019 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5020 * protection.
5021 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005022 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005023
5024 tel = quic_packet_type_enc_level(pkt->type);
5025 qel = &qc->els[tel];
5026
5027 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005028 /* Note that the following function enables us to unprotect the packet
5029 * number and its length subsequently used to decrypt the entire
5030 * packets.
5031 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005032 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02005033 qel->pktns->rx.largest_pn, pn, beg)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005034 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005035 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005036 }
5037
5038 /* The AAD includes the packet number field found at <pn>. */
5039 pkt->aad_len = pn - beg + pkt->pnl;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005040 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
5041 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005042 goto out;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005043 }
5044
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005045 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005046 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01005047 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01005048 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005049 /* If the packet number space has been discarded, this packet
5050 * will be not parsed.
5051 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005052 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005053 goto out;
5054 }
5055
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005056 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005057 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02005058 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
5059 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005060 }
5061
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005062 *el = qel;
5063 /* No reference counter incrementation here!!! */
5064 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
5065 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
5066 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
5067 b_add(&qc->rx.buf, pkt->len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005068
5069 ret = 1;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005070 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005071 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005072 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005073}
5074
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005075/* 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 +02005076 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
5077 * of this packet into <*version>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005078 */
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005079static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
5080 unsigned char **buf, const unsigned char *end,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005081 int *long_header, uint32_t *version)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005082{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005083 int ret = 0;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005084 const unsigned char byte0 = **buf;
5085
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005086 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5087
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005088 (*buf)++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005089 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005090 unsigned char type =
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005091 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005092
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005093 *long_header = 1;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005094 /* Version */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005095 if (!quic_read_uint32(version, (const unsigned char **)buf, end)) {
5096 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
5097 goto out;
5098 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005099
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005100 if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005101 pkt->type = type;
5102 }
5103 else {
5104 switch (type) {
5105 case 0:
5106 pkt->type = QUIC_PACKET_TYPE_RETRY;
5107 break;
5108 case 1:
5109 pkt->type = QUIC_PACKET_TYPE_INITIAL;
5110 break;
5111 case 2:
5112 pkt->type = QUIC_PACKET_TYPE_0RTT;
5113 break;
5114 case 3:
5115 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
5116 break;
5117 }
5118 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005119 }
5120 else {
5121 pkt->type = QUIC_PACKET_TYPE_SHORT;
5122 *long_header = 0;
5123 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005124
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005125 ret = 1;
5126 out:
5127 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005128 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005129}
5130
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005131/* Return the QUIC version (quic_version struct) with <version> as version number
5132 * if supported or NULL if not.
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005133 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005134static inline const struct quic_version *qc_supported_version(uint32_t version)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005135{
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005136 int i;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005137
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005138 for (i = 0; i < quic_versions_nb; i++)
5139 if (quic_versions[i].num == version)
5140 return &quic_versions[i];
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005141
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005142 return NULL;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005143}
5144
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005145/*
5146 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
5147 * address <addr>.
5148 * Implementation of RFC9000 6. Version Negotiation
5149 *
5150 * TODO implement a rate-limiting sending of Version Negotiation packets
5151 *
5152 * Returns 0 on success else non-zero
5153 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005154static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
5155 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005156{
5157 char buf[256];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005158 int ret = 0, i = 0, j;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005159 uint32_t version;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005160 const socklen_t addrlen = get_addr_len(addr);
5161
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005162 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005163 /*
5164 * header form
5165 * long header, fixed bit to 0 for Version Negotiation
5166 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005167 /* TODO: RAND_bytes() should be replaced? */
5168 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
5169 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
5170 goto out;
5171 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005172
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01005173 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005174 /* null version for Version Negotiation */
5175 buf[i++] = '\x00';
5176 buf[i++] = '\x00';
5177 buf[i++] = '\x00';
5178 buf[i++] = '\x00';
5179
5180 /* source connection id */
5181 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005182 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005183 i += pkt->scid.len;
5184
5185 /* destination connection id */
5186 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005187 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005188 i += pkt->dcid.len;
5189
5190 /* supported version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005191 for (j = 0; j < quic_versions_nb; j++) {
5192 version = htonl(quic_versions[j].num);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005193 memcpy(&buf[i], &version, sizeof(version));
5194 i += sizeof(version);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005195 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005196
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005197 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005198 goto out;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005199
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005200 ret = 1;
5201 out:
5202 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5203 return !ret;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005204}
5205
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005206/* Send a stateless reset packet depending on <pkt> RX packet information
5207 * from <fd> UDP socket to <dst>
5208 * Return 1 if succeeded, 0 if not.
5209 */
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005210static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005211 struct quic_rx_packet *rxpkt)
5212{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005213 int ret = 0, pktlen, rndlen;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005214 unsigned char pkt[64];
5215 const socklen_t addrlen = get_addr_len(dstaddr);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005216 struct proxy *prx;
5217 struct quic_counters *prx_counters;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005218
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005219 TRACE_ENTER(QUIC_EV_STATELESS_RST);
5220
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005221 prx = l->bind_conf->frontend;
5222 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005223 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
5224 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
5225 * Reset is difficult to distinguish from other packets if the recipient requires
5226 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
5227 * that all packets it sends are at least 22 bytes longer than the minimum
5228 * connection ID length that it requests the peer to include in its packets,
5229 * adding PADDING frames as necessary. This ensures that any Stateless Reset
5230 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
5231 * An endpoint that sends a Stateless Reset in response to a packet that is
5232 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
5233 * than the packet it responds to.
5234 */
5235
5236 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
5237 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
5238 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
5239 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005240
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005241 /* Put a header of random bytes */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005242 /* TODO: RAND_bytes() should be replaced */
5243 if (RAND_bytes(pkt, rndlen) != 1) {
5244 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
5245 goto leave;
5246 }
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005247
5248 /* Clear the most significant bit, and set the second one */
5249 *pkt = (*pkt & ~0x80) | 0x40;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005250 if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005251 rxpkt->dcid.data, rxpkt->dcid.len))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005252 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005253
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005254 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005255 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005256
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005257 ret = 1;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005258 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005259 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005260 leave:
5261 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
5262 return ret;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005263}
5264
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005265/* QUIC server only function.
5266 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005267 * This is the responsibility of the caller to check <aad> size is big enough
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005268 * to contain these data.
5269 * Return the number of bytes copied to <aad>.
5270 */
5271static int quic_generate_retry_token_aad(unsigned char *aad,
5272 uint32_t version,
5273 const struct quic_cid *cid,
5274 const struct sockaddr_storage *addr)
5275{
5276 unsigned char *p;
5277
5278 p = aad;
5279 memcpy(p, &version, sizeof version);
5280 p += sizeof version;
5281 p += quic_saddr_cpy(p, addr);
5282 memcpy(p, cid->data, cid->len);
5283 p += cid->len;
5284
5285 return p - aad;
5286}
5287
5288/* QUIC server only function.
5289 * Generate the token to be used in Retry packets. The token is written to
5290 * <buf> whith <len> as length. <odcid> is the original destination connection
5291 * ID and <dcid> is our side destination connection ID (or client source
5292 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005293 * Returns the length of the encoded token or 0 on error.
5294 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005295static int quic_generate_retry_token(unsigned char *buf, size_t len,
5296 const uint32_t version,
5297 const struct quic_cid *odcid,
5298 const struct quic_cid *dcid,
5299 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005300{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005301 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005302 unsigned char *p;
5303 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5304 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5305 size_t aadlen;
5306 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
5307 unsigned char key[QUIC_TLS_KEY_LEN];
5308 unsigned char iv[QUIC_TLS_IV_LEN];
5309 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5310 size_t seclen = strlen(global.cluster_secret);
5311 EVP_CIPHER_CTX *ctx = NULL;
5312 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5313 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005314
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005315 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5316
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005317 /* We copy the odcid into the token, prefixed by its one byte
5318 * length, the format token byte. It is followed by an AEAD TAG, and finally
5319 * the random bytes used to derive the secret to encrypt the token.
5320 */
5321 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005322 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005323
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005324 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005325 /* TODO: RAND_bytes() should be replaced */
5326 if (RAND_bytes(salt, sizeof salt) != 1) {
5327 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005328 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005329 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005330
5331 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 +02005332 salt, sizeof salt, sec, seclen)) {
5333 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005334 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005335 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005336
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005337 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
5338 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005339 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005340 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005341
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005342 /* Token build */
5343 p = buf;
5344 *p++ = QUIC_TOKEN_FMT_RETRY,
5345 *p++ = odcid->len;
5346 memcpy(p, odcid->data, odcid->len);
5347 p += odcid->len;
5348 write_u32(p, htonl(timestamp));
5349 p += sizeof timestamp;
5350
5351 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005352 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
5353 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005354 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005355 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005356
5357 p += QUIC_TLS_TAG_LEN;
5358 memcpy(p, salt, sizeof salt);
5359 p += sizeof salt;
5360 EVP_CIPHER_CTX_free(ctx);
5361
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005362 ret = p - buf;
5363 leave:
5364 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5365 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005366
5367 err:
5368 if (ctx)
5369 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005370 goto leave;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005371}
5372
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005373/* QUIC server only function.
5374 * Check the validity of the Retry token from <token> buffer with <tokenlen>
5375 * as length. If valid, the ODCID of <qc> QUIC connection will be put
5376 * into <odcid> connection ID. <dcid> is our side destination connection ID
5377 * of client source connection ID.
5378 * Return 1 if succeeded, 0 if not.
5379 */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005380static int quic_retry_token_check(const unsigned char *token, size_t tokenlen,
5381 const struct quic_version *qv,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005382 struct quic_cid *odcid,
5383 const struct quic_cid *dcid,
5384 struct quic_conn *qc,
5385 struct sockaddr_storage *addr)
5386{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005387 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005388 unsigned char buf[128];
5389 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5390 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5391 size_t aadlen;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005392 const unsigned char *salt;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005393 unsigned char key[QUIC_TLS_KEY_LEN];
5394 unsigned char iv[QUIC_TLS_IV_LEN];
5395 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5396 size_t seclen = strlen(global.cluster_secret);
5397 EVP_CIPHER_CTX *ctx = NULL;
5398 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5399
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005400 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5401
5402 if (sizeof buf < tokenlen) {
5403 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
5404 goto err;
5405 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005406
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005407 aadlen = quic_generate_retry_token_aad(aad, qv->num, dcid, addr);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005408 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
5409 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 +02005410 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005411 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
5412 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005413 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005414
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005415 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
5416 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005417 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005418 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005419
5420 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
5421 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 +02005422 ctx, aead, key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005423 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005424 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005425 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005426
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005427 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
5428 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005429 goto err;
5430 }
5431
5432 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005433
5434 ret = 1;
5435 leave:
5436 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5437 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005438
5439 err:
5440 if (ctx)
5441 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005442 goto leave;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005443}
5444
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005445/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
5446 * the Initial <pkt> packet.
5447 *
5448 * Returns 0 on success else non-zero.
5449 */
5450static int send_retry(int fd, struct sockaddr_storage *addr,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005451 struct quic_rx_packet *pkt, const struct quic_version *qv)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005452{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005453 int ret = 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005454 unsigned char buf[128];
5455 int i = 0, token_len;
5456 const socklen_t addrlen = get_addr_len(addr);
5457 struct quic_cid scid;
5458
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005459 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5460
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005461 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
5462 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
5463 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005464 /* version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005465 buf[i++] = *((unsigned char *)&qv->num + 3);
5466 buf[i++] = *((unsigned char *)&qv->num + 2);
5467 buf[i++] = *((unsigned char *)&qv->num + 1);
5468 buf[i++] = *(unsigned char *)&qv->num;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005469
5470 /* Use the SCID from <pkt> for Retry DCID. */
5471 buf[i++] = pkt->scid.len;
5472 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
5473 i += pkt->scid.len;
5474
5475 /* Generate a new CID to be used as SCID for the Retry packet. */
5476 scid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005477 /* TODO: RAND_bytes() should be replaced */
5478 if (RAND_bytes(scid.data, scid.len) != 1) {
5479 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
5480 goto out;
5481 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005482
5483 buf[i++] = scid.len;
5484 memcpy(&buf[i], scid.data, scid.len);
5485 i += scid.len;
5486
5487 /* token */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005488 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005489 &pkt->dcid, &pkt->scid, addr))) {
5490 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
5491 goto out;
5492 }
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01005493
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005494 i += token_len;
5495
5496 /* token integrity tag */
5497 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
5498 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005499 pkt->dcid.len, buf, i, qv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005500 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5501 goto out;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005502 }
5503
5504 i += QUIC_TLS_TAG_LEN;
5505
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005506 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
5507 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5508 goto out;
5509 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005510
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005511 ret = 1;
5512 out:
5513 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5514 return !ret;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005515}
5516
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005517/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5518 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5519 * concatenated to the <pkt> DCID field.
5520 *
5521 * Returns the instance or NULL if not found.
5522 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005523static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005524 struct listener *l,
5525 struct sockaddr_storage *saddr)
5526{
5527 struct quic_conn *qc = NULL;
5528 struct ebmb_node *node;
5529 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005530 /* set if the quic_conn is found in the second DCID tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005531
5532 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005533
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005534 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5535 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5536 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5537 /* DCIDs of first packets coming from multiple clients may have
5538 * the same values. Let's distinguish them by concatenating the
5539 * socket addresses.
5540 */
5541 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005542 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005543 pkt->dcid.len + pkt->dcid.addrlen);
5544 if (node) {
5545 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5546 goto end;
5547 }
5548 }
5549
5550 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5551 * also for INITIAL/0-RTT non-first packets with the final DCID in
5552 * used.
5553 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005554 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005555 if (!node)
5556 goto end;
5557
5558 id = ebmb_entry(node, struct quic_connection_id, node);
5559 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005560
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005561 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5562 * If already done, this is a noop.
5563 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005564 if (qc)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005565 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005566
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005567 end:
5568 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005569 return qc;
5570}
5571
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005572/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5573 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5574 * parameters of this session.
5575 * This is the responsibility of the caller to check the validity of all the
5576 * pointers passed as parameter to this function.
5577 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5578 * CO_ER_SSL_NO_MEM.
5579 */
5580static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5581 unsigned char *params, size_t params_len)
5582{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005583 int retry, ret = -1;
5584
5585 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005586
5587 retry = 1;
5588 retry:
5589 *ssl = SSL_new(ssl_ctx);
5590 if (!*ssl) {
5591 if (!retry--)
5592 goto err;
5593
5594 pool_gc(NULL);
5595 goto retry;
5596 }
5597
5598 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005599 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005600 SSL_free(*ssl);
5601 *ssl = NULL;
5602 if (!retry--)
5603 goto err;
5604
5605 pool_gc(NULL);
5606 goto retry;
5607 }
5608
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005609 ret = 0;
5610 leave:
5611 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5612 return ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005613
5614 err:
5615 qc->conn->err_code = CO_ER_SSL_NO_MEM;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005616 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005617}
5618
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005619/* Finalize <qc> QUIC connection:
5620 * - initialize the Initial QUIC TLS context for negotiated version,
5621 * - derive the secrets for this context,
5622 * - encode the transport parameters to be sent,
5623 * - set them into the TLS stack,
5624 * - initialize ->max_ack_delay and max_idle_timeout,
5625 *
5626 * MUST be called after having received the remote transport parameters.
5627 * Return 1 if succeeded, 0 if not.
5628 */
5629int qc_conn_finalize(struct quic_conn *qc, int server)
5630{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005631 int ret = 0;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005632 struct quic_transport_params *tx_tp = &qc->tx.params;
5633 struct quic_transport_params *rx_tp = &qc->rx.params;
5634 const struct quic_version *ver;
5635
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005636 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5637
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005638 if (tx_tp->version_information.negotiated_version &&
5639 tx_tp->version_information.negotiated_version != qc->original_version) {
5640 qc->negotiated_version =
5641 qc->tx.params.version_information.negotiated_version;
5642 if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
5643 qc->odcid.data, qc->odcid.len, !server))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005644 goto out;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005645
5646 ver = qc->negotiated_version;
5647 }
5648 else {
5649 ver = qc->original_version;
5650 }
5651
5652 qc->enc_params_len =
5653 quic_transport_params_encode(qc->enc_params,
5654 qc->enc_params + sizeof qc->enc_params,
5655 &qc->rx.params, ver, 1);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005656 if (!qc->enc_params_len) {
5657 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_TXPKT);
5658 goto out;
5659 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005660
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005661 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
5662 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_TXPKT);
5663 goto out;
5664 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005665
5666 if (tx_tp->max_ack_delay)
5667 qc->max_ack_delay = tx_tp->max_ack_delay;
5668
5669 if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout)
5670 qc->max_idle_timeout =
5671 QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5672 else
5673 qc->max_idle_timeout =
5674 QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5675
5676 TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp);
5677
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005678 ret = 1;
5679 out:
5680 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5681 return ret;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005682}
5683
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005684/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5685 * used to process <qc> received packets. The allocated context is stored in
5686 * <qc.xprt_ctx>.
5687 *
5688 * Returns 0 on success else non-zero.
5689 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005690static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005691{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005692 int ret = 0;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005693 struct bind_conf *bc = qc->li->bind_conf;
5694 struct ssl_sock_ctx *ctx = NULL;
5695
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005696 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5697
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005698 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005699 if (!ctx) {
5700 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005701 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005702 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005703
5704 ctx->wait_event.tasklet = tasklet_new();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005705 if (!ctx->wait_event.tasklet) {
5706 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005707 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005708 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005709
5710 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5711 ctx->wait_event.tasklet->context = ctx;
5712 ctx->wait_event.events = 0;
5713 ctx->subs = NULL;
5714 ctx->xprt_ctx = NULL;
5715 ctx->qc = qc;
5716
5717 /* Set tasklet tid based on the SCID selected by us for this
5718 * connection. The upper layer will also be binded on the same thread.
5719 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005720 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005721
5722 if (qc_is_listener(qc)) {
5723 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5724 qc->enc_params, qc->enc_params_len) == -1) {
5725 goto err;
5726 }
5727
5728 /* Enabling 0-RTT */
5729 if (bc->ssl_conf.early_data)
5730 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5731
5732 SSL_set_accept_state(ctx->ssl);
5733 }
5734
5735 ctx->xprt = xprt_get(XPRT_QUIC);
5736
5737 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005738 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005739
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005740 ret = 1;
5741 leave:
5742 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5743 return !ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005744
5745 err:
5746 if (ctx && ctx->wait_event.tasklet)
5747 tasklet_free(ctx->wait_event.tasklet);
5748 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005749 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005750}
5751
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005752/* Check that all the bytes between <buf> included and <end> address
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005753 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005754 * check that there is at least one byte between <buf> end <end>.
5755 * Return 1 if this all the bytes are null, 0 if not.
5756 */
5757static inline int quic_padding_check(const unsigned char *buf,
5758 const unsigned char *end)
5759{
5760 while (buf < end && !*buf)
5761 buf++;
5762
5763 return buf == end;
5764}
5765
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005766/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5767 * end of this buffer past one byte and populate <pkt> RX packet structure
5768 * with the information collected from the packet.
5769 * This function sets ->len <pkt> field value to the end of the packet past one
5770 * byte to enable the caller to run this function again to continue to parse
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005771 * the remaining QUIC packets carried by the datagram.
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005772 * Note that this function always sets this ->len value. If a paquet could
5773 * not be correctly found, ->len value will be set to the remaining number
5774 * bytes in the datagram to entirely consume this latter.
5775 */
5776static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5777 struct quic_rx_packet *pkt, int first_pkt,
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02005778 struct quic_dgram *dgram, struct list **tasklist_head)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005779{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005780 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005781 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005782 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005783 struct proxy *prx;
5784 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005785 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005786 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005787 size_t b_cspace;
5788 struct quic_enc_level *qel;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005789 uint32_t version;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005790 const struct quic_version *qv = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005791
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005792 TRACE_ENTER(QUIC_EV_CONN_LPKT);
5793
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005794 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005795 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005796 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005797 qel = NULL;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005798 l = dgram->owner;
5799 prx = l->bind_conf->frontend;
5800 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005801 /* This ist only to please to traces and distinguish the
5802 * packet with parsed packet number from others.
5803 */
5804 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005805 if (end <= buf) {
5806 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5807 goto drop;
5808 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005809
5810 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005811 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005812 if (!first_pkt && quic_padding_check(buf, end)) {
5813 /* Some browsers may pad the remaining datagram space with null bytes.
5814 * That is what we called add padding out of QUIC packets. Such
5815 * datagrams must be considered as valid. But we can only consume
5816 * the remaining space.
5817 */
5818 pkt->len = end - buf;
5819 goto drop_no_conn;
5820 }
5821
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005822 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005823 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005824 }
5825
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005826 /* Header form */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005827 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005828 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5829 goto drop;
5830 }
5831
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005832 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005833 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005834 struct quic_cid odcid;
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02005835 const struct quic_cid *token_odcid = NULL; // ODCID received from client token
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005836
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005837 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005838 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005839 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005840 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005841 }
5842
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005843 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5844 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005845 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005846 }
5847 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5848 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005849 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02005850 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
Frédéric Lécaillee7df68a2022-08-05 09:34:44 +02005851 goto drop;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005852 }
5853
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005854 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5855 * they must have the same DCID.
5856 */
5857 if (!first_pkt &&
5858 (pkt->dcid.len != dgram->dcid_len ||
5859 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5860 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005861 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005862 }
5863
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005864 /* Retry of Version Negotiation packets are only sent by servers */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005865 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005866 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005867 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005868 }
5869
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005870 /* RFC9000 6. Version Negotiation */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005871 qv = qc_supported_version(version);
5872 if (!qv) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005873 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005874 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005875 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005876 goto err;
5877 }
5878
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005879 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005880 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005881 }
5882
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005883 /* For Initial packets, and for servers (QUIC clients connections),
5884 * there is no Initial connection IDs storage.
5885 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005886 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005887 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005888
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005889 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5890 end - buf < token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005891 TRACE_PROTO("Packet dropped",
5892 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005893 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005894 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005895
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005896 /* TODO Retry should be automatically activated if
5897 * suspect network usage is detected.
5898 */
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005899 if (global.cluster_secret && !token_len) {
5900 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
5901 TRACE_PROTO("Initial without token, sending retry",
5902 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
5903 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
5904 TRACE_PROTO("Error during Retry generation",
5905 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005906 goto err;
5907 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005908
5909 HA_ATOMIC_INC(&prx_counters->retry_sent);
5910 goto err;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005911 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005912 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005913 else if (!global.cluster_secret && token_len) {
5914 /* Impossible case: a token was received without configured
5915 * cluster secret.
5916 */
5917 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
5918 NULL, NULL, NULL, qv);
5919 goto drop;
5920 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005921
5922 pkt->token = buf;
5923 pkt->token_len = token_len;
5924 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005925 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005926 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005927 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005928 TRACE_PROTO("Packet dropped",
5929 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005930 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005931 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005932 }
5933
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005934 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5935 end - buf < len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005936 TRACE_PROTO("Packet dropped",
5937 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005938 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005939 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005940
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005941 payload = buf;
5942 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005943 if (drop_no_conn)
5944 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005945
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005946 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005947 if (global.cluster_secret && pkt->token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005948 if (*pkt->token == QUIC_TOKEN_FMT_RETRY) {
5949 const struct quic_version *ver = qc ? qc->original_version : qv;
5950 if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid,
5951 &pkt->scid, qc, &dgram->saddr)) {
5952 HA_ATOMIC_INC(&prx_counters->retry_error);
5953 TRACE_PROTO("Wrong retry token",
5954 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5955 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
5956 * with an INVALID_TOKEN error.
5957 */
5958 goto drop;
5959 }
5960
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005961 token_odcid = &odcid;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005962 HA_ATOMIC_INC(&prx_counters->retry_validated);
5963 }
5964 else {
5965 /* TODO: New token check */
5966 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5967 goto drop;
5968 }
5969 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005970 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005971 int ipv4;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005972 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005973
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005974 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005975 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005976 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005977 }
5978
Willy Tarreau787e92a2022-05-20 16:06:01 +02005979 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 +02005980 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005981 TRACE_PROTO("Initial without token, sending retry",
5982 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005983 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005984 TRACE_ERROR("Error during Retry generation",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005985 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005986 goto err;
5987 }
5988
5989 HA_ATOMIC_INC(&prx_counters->retry_sent);
5990 goto err;
5991 }
5992
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005993 /* RFC 9000 7.2. Negotiating Connection IDs:
5994 * When an Initial packet is sent by a client that has not previously
5995 * received an Initial or Retry packet from the server, the client
5996 * populates the Destination Connection ID field with an unpredictable
5997 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5998 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01005999 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006000 TRACE_PROTO("dropped packet",
6001 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006002 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01006003 }
6004
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006005 pkt->saddr = dgram->saddr;
6006 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006007
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02006008 qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, token_odcid,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02006009 &pkt->saddr, 1, !!pkt->token_len, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02006010 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006011 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006012
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006013 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02006014 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01006015 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01006016 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006017
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01006018 /* If the insertion failed, it means that another
6019 * thread has already allocated a QUIC connection for
6020 * the same CID. Liberate our allocated connection.
6021 */
6022 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01006023 qc_to_purge = qc;
6024
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01006025 qc = ebmb_entry(n, struct quic_conn, odcid_node);
6026 pkt->qc = qc;
6027 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01006028
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01006029 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006030 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01006031 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006032 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01006033 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01006034 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01006035 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006036 }
6037 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006038 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006039 }
6040 }
6041 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006042 TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006043 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006044 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006045 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006046 }
6047
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006048 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006049 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006050
6051 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6052 * they must have the same DCID.
6053 */
6054 if (!first_pkt &&
6055 (pkt->dcid.len != dgram->dcid_len ||
6056 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
6057 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006058 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006059 }
6060
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006061 buf += QUIC_HAP_CID_LEN;
6062
6063 /* A short packet is the last one of a UDP datagram. */
6064 payload = buf;
6065 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006066
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006067 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006068 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006069 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006070 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006071 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006072 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006073 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006074 }
6075
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006076 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006077 }
6078
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006079 if (qc->flags & QUIC_FL_CONN_CLOSING) {
6080 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
6081 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
6082 qc->nb_pkt_for_cc++;
6083 qc->nb_pkt_since_cc = 0;
6084 }
6085 /* Skip the entire datagram */
6086 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006087 TRACE_STATE("Closing state connection",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006088 QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006089 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006090 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006091
6092 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6093 * they must have the same DCID.
6094 *
6095 * This check must be done after the final update to pkt.len to
6096 * properly drop the packet on failure.
6097 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006098 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006099 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006100 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006101 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006102 /* Reset the anti-amplification bit. It will be set again
6103 * when sending the next packet if reached again.
6104 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006105 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
6106 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006107 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006108 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006109
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006110 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006111
Amaury Denoyelle72d86502022-07-13 15:08:23 +02006112 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006113 TRACE_PROTO("Connection error",
6114 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006115 goto out;
6116 }
6117
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006118 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006119 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006120 b_cspace = b_contig_space(&qc->rx.buf);
6121 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006122 /* Do not consume buf if space not at the end. */
6123 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006124 TRACE_PROTO("Packet dropped",
6125 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006126 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6127 goto drop_no_conn;
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006128 }
6129
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006130 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006131 if (b_cspace) {
6132 b_putchr(&qc->rx.buf, 0x00);
6133 b_cspace--;
6134 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006135 b_add(&qc->rx.buf, b_cspace);
6136 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006137 TRACE_PROTO("Too big packet",
6138 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006139 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6140 goto drop_no_conn;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006141 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006142 }
6143
Frédéric Lécaille99897d12022-08-08 10:28:07 +02006144 if (!qc_try_rm_hp(qc, pkt, payload, beg, &qel)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006145 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006146 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02006147 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006148
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006149 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006150 if (pkt->aad_len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006151 qc_pkt_insert(qc, pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006152 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02006153 /* Wake up the connection packet handler task from here only if all
6154 * the contexts have been initialized, especially the mux context
6155 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
6156 * will start it if these contexts for the connection are not already
6157 * initialized.
6158 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006159 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006160 if (conn_ctx)
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02006161 *tasklist_head = tasklet_wakeup_after(*tasklist_head, conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02006162
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006163 drop_no_conn:
6164 if (drop_no_conn)
6165 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006166 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006167
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006168 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006169
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006170 drop:
6171 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006172 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006173 /* Wakeup the I/O handler callback if the PTO timer must be armed.
6174 * This cannot be done by this thread.
6175 */
6176 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006177 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006178 if (conn_ctx && conn_ctx->wait_event.tasklet)
6179 tasklet_wakeup(conn_ctx->wait_event.tasklet);
6180 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01006181 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006182 if (!pkt->len)
6183 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006184 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006185}
6186
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006187/* This function builds into <buf> buffer a QUIC long packet header.
6188 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006189 */
6190static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006191 int type, size_t pn_len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006192 struct quic_conn *qc, const struct quic_version *ver)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006193{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006194 int ret = 0;
6195
6196 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6197
6198 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
6199 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6200 goto leave;
6201 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006202
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006203 type = quic_pkt_type(type, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006204 /* #0 byte flags */
6205 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
6206 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
6207 /* Version */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006208 quic_write_uint32(buf, end, ver->num);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006209 *(*buf)++ = qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006210 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006211 if (qc->dcid.len) {
6212 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6213 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006214 }
6215 /* Source connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006216 *(*buf)++ = qc->scid.len;
6217 if (qc->scid.len) {
6218 memcpy(*buf, qc->scid.data, qc->scid.len);
6219 *buf += qc->scid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006220 }
6221
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006222 ret = 1;
6223 leave:
6224 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6225 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006226}
6227
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006228/* This function builds into <buf> buffer a QUIC short packet header.
6229 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006230 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006231static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006232 size_t pn_len, struct quic_conn *qc,
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006233 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006234{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006235 int ret = 0;
6236
6237 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6238
6239 if (end - *buf < 1 + qc->dcid.len) {
6240 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6241 goto leave;
6242 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006243
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006244 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01006245 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
6246 ((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 +01006247 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006248 if (qc->dcid.len) {
6249 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6250 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006251 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006252
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006253 ret = 1;
6254 leave:
6255 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6256 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006257}
6258
6259/* Apply QUIC header protection to the packet with <buf> as first byte address,
6260 * <pn> as address of the Packet number field, <pnlen> being this field length
6261 * with <aead> as AEAD cipher and <key> as secret key.
6262 * Returns 1 if succeeded or 0 if failed.
6263 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006264static int quic_apply_header_protection(struct quic_conn *qc,
6265 unsigned char *buf, unsigned char *pn, size_t pnlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006266 const EVP_CIPHER *aead, const unsigned char *key)
6267{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006268 int i, outlen, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006269 EVP_CIPHER_CTX *ctx;
6270 /* We need an IV of at least 5 bytes: one byte for bytes #0
6271 * and at most 4 bytes for the packet number
6272 */
6273 unsigned char mask[5] = {0};
6274
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006275 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6276
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006277 ctx = EVP_CIPHER_CTX_new();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006278 if (!ctx) {
6279 TRACE_ERROR("cipher context allocation failed", QUIC_EV_CONN_TXPKT, qc);
6280 goto out;
6281 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006282
6283 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
6284 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006285 !EVP_EncryptFinal_ex(ctx, mask, &outlen)) {
6286 TRACE_ERROR("cipher context allocation failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006287 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006288 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006289
6290 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
6291 for (i = 0; i < pnlen; i++)
6292 pn[i] ^= mask[i + 1];
6293
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006294 EVP_CIPHER_CTX_free(ctx);
6295
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006296 ret = 1;
6297 out:
6298 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006299 return ret;
6300}
6301
6302/* Reduce the encoded size of <ack_frm> ACK frame removing the last
6303 * ACK ranges if needed to a value below <limit> in bytes.
6304 * Return 1 if succeeded, 0 if not.
6305 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006306static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
6307 struct quic_frame *ack_frm, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006308{
6309 size_t room, ack_delay_sz;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006310 int ret = 0;
6311
6312 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006313
6314 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
6315 /* A frame is made of 1 byte for the frame type. */
6316 room = limit - ack_delay_sz - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006317 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
6318 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006319
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006320 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
6321 leave:
6322 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6323 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006324}
6325
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006326/* Prepare into <outlist> as most as possible ack-eliciting frame from their
6327 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
6328 * with <room> as available room, and <*len> the packet Length field initialized
6329 * with the number of bytes already present in this buffer which must be taken
6330 * into an account for the Length packet field value. <headlen> is the number of
6331 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006332 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006333 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006334 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006335 * 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 +01006336 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006337static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006338 size_t room, size_t *len, size_t headlen,
6339 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006340 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006341{
Frédéric Lécailleea604992020-12-24 13:01:37 +01006342 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006343 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006344
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006345 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
6346
Frédéric Lécailleea604992020-12-24 13:01:37 +01006347 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006348 if (*len > room)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006349 goto leave;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006350
Frédéric Lécailleea604992020-12-24 13:01:37 +01006351 /* If we are not probing we must take into an account the congestion
6352 * control window.
6353 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006354 if (!qel->pktns->tx.pto_probe) {
6355 size_t remain = quic_path_prep_data(qc->path);
6356
6357 if (headlen > remain)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006358 goto leave;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006359
6360 room = QUIC_MIN(room, remain - headlen);
6361 }
6362
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006363 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006364 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006365
6366 /* NOTE: switch/case block inside a loop, a successful status must be
6367 * returned by this function only if at least one frame could be built
6368 * in the switch/case block.
6369 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006370 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006371 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006372 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006373
Frédéric Lécailleea604992020-12-24 13:01:37 +01006374 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006375 break;
6376
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006377 switch (cf->type) {
6378 case QUIC_FT_CRYPTO:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006379 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006380 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006381 /* Compute the length of this CRYPTO frame header */
6382 hlen = 1 + quic_int_getsize(cf->crypto.offset);
6383 /* Compute the data length of this CRyPTO frame. */
6384 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006385 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006386 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006387 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006388 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006389
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006390 /* CRYPTO frame length. */
6391 flen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006392 TRACE_DEVEL(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006393 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006394 /* Add the CRYPTO data length and its encoded length to the packet
6395 * length and the length of this length.
6396 */
6397 *len += flen;
6398 room -= flen;
6399 if (dlen == cf->crypto.len) {
6400 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006401 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006402 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006403 }
6404 else {
6405 struct quic_frame *new_cf;
6406
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006407 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006408 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006409 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006410 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006411 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006412
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006413 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006414 new_cf->type = QUIC_FT_CRYPTO;
6415 new_cf->crypto.len = dlen;
6416 new_cf->crypto.offset = cf->crypto.offset;
6417 new_cf->crypto.qel = qel;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006418 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6419 if (cf->origin) {
6420 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6421 /* This <cf> frame was duplicated */
6422 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6423 new_cf->origin = cf->origin;
6424 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006425 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006426 /* Consume <dlen> bytes of the current frame. */
6427 cf->crypto.len -= dlen;
6428 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006429 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006430 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006431
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006432 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006433 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
6434 struct eb64_node *node = NULL;
6435 struct qc_stream_desc *stream_desc = NULL;
6436 struct quic_stream *strm = &cf->stream;
6437
6438 /* As this frame has been already lost, ensure the stream is always
6439 * available or the range of this frame is not consumed before
6440 * resending it.
6441 */
6442 node = eb64_lookup(&qc->streams_by_id, strm->id);
6443 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006444 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006445 LIST_DELETE(&cf->list);
6446 pool_free(pool_head_quic_frame, cf);
6447 continue;
6448 }
6449
6450 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
6451 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006452 TRACE_DEVEL("ignored frame frame in already acked range",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006453 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006454 LIST_DELETE(&cf->list);
6455 pool_free(pool_head_quic_frame, cf);
6456 continue;
6457 }
6458 else if (strm->offset.key < stream_desc->ack_offset) {
6459 strm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006460 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006461 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006462 }
6463 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006464 /* Note that these frames are accepted in short packets only without
6465 * "Length" packet field. Here, <*len> is used only to compute the
6466 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01006467 *
6468 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006469 * excepting the variable ones. Note that +1 is for the type of this frame.
6470 */
6471 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006472 ((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 +02006473 /* Compute the data length of this STREAM frame. */
6474 avail_room = room - hlen - *len;
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006475 if ((ssize_t)avail_room <= 0)
6476 continue;
6477
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006478 TRACE_DEVEL(" New STREAM frame build (room, len)",
Frédéric Lécailleadc76412022-08-08 16:09:46 +02006479 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006480 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
6481 dlen = max_available_room(avail_room, &dlen_sz);
6482 if (dlen > cf->stream.len) {
6483 dlen = cf->stream.len;
6484 }
6485 dlen_sz = quic_int_getsize(dlen);
6486 flen = hlen + dlen_sz + dlen;
6487 }
6488 else {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02006489 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006490 flen = hlen + dlen;
6491 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006492 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006493 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006494 TRACE_DEVEL(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006495 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006496 /* Add the STREAM data length and its encoded length to the packet
6497 * length and the length of this length.
6498 */
6499 *len += flen;
6500 room -= flen;
6501 if (dlen == cf->stream.len) {
6502 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006503 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006504 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006505
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006506 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006507 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006508 qcc_streams_sent_done(cf->stream.stream->ctx,
6509 cf->stream.len,
6510 cf->stream.offset.key);
6511 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006512 }
6513 else {
6514 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006515 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006516
6517 new_cf = pool_zalloc(pool_head_quic_frame);
6518 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006519 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006520 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006521 }
6522
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006523 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006524 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006525 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006526 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006527 new_cf->stream.id = cf->stream.id;
6528 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
6529 new_cf->stream.offset = cf->stream.offset;
6530 new_cf->stream.len = dlen;
6531 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
6532 /* FIN bit reset */
6533 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
6534 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006535 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6536 if (cf->origin) {
6537 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6538 /* This <cf> frame was duplicated */
6539 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6540 new_cf->origin = cf->origin;
6541 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006542 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006543 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
6544 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006545 cf_buf = b_make(b_orig(cf->stream.buf),
6546 b_size(cf->stream.buf),
6547 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006548 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006549 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006550 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006551
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006552 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006553 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006554 qcc_streams_sent_done(new_cf->stream.stream->ctx,
6555 new_cf->stream.len,
6556 new_cf->stream.offset.key);
6557 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006558 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006559
6560 /* TODO the MUX is notified about the frame sending via
6561 * previous qcc_streams_sent_done call. However, the
6562 * sending can fail later, for example if the sendto
6563 * system call returns an error. As the MUX has been
6564 * notified, the transport layer is responsible to
6565 * bufferize and resent the announced data later.
6566 */
6567
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006568 break;
6569
6570 default:
6571 flen = qc_frm_len(cf);
6572 BUG_ON(!flen);
6573 if (flen > room)
6574 continue;
6575
6576 *len += flen;
6577 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006578 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006579 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006580 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006581 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006582
6583 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01006584 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006585 }
6586
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006587 leave:
6588 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécailleea604992020-12-24 13:01:37 +01006589 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006590}
6591
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006592/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
6593 * is used as return parameter and should be zero'ed by the caller.
6594 */
6595static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
6596 struct quic_frame *out)
6597{
6598 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
6599 *
6600 * A CONNECTION_CLOSE frame should be sent in several packets with
6601 * different encryption levels depending on the client context. This is
6602 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
6603 * more details on how to implement it.
6604 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006605 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
6606
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006607
6608 if (qc->err.app) {
6609 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
6610 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
6611 /* RFC 9000 10.2.3. Immediate Close during the Handshake
6612 *
6613 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
6614 * packet could expose application state or be used to alter application
6615 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
6616 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
6617 * Handshake packets. Otherwise, information about the application
6618 * state might be revealed. Endpoints MUST clear the value of the
6619 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
6620 * converting to a CONNECTION_CLOSE of type 0x1c.
6621 */
6622 out->type = QUIC_FT_CONNECTION_CLOSE;
6623 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
6624 out->connection_close.reason_phrase_len = 0;
6625 }
6626 else {
6627 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
6628 out->connection_close.error_code = qc->err.code;
6629 }
6630 }
6631 else {
6632 out->type = QUIC_FT_CONNECTION_CLOSE;
6633 out->connection_close.error_code = qc->err.code;
6634 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006635 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
6636
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006637}
6638
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006639/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006640 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
6641 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006642 * filling the buffer with as much frames as possible from <frms> list of
6643 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006644 * 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 +02006645 * 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 +02006646 * having returned from this function.
6647 * 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 +01006648 * number field in this packet. <pn_len> will also have the packet number
6649 * length as value.
6650 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006651 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006652 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006653static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
6654 size_t dglen, struct quic_tx_packet *pkt,
6655 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006656 int force_ack, int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006657 struct quic_enc_level *qel, struct quic_conn *qc,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006658 const struct quic_version *ver, struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006659{
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006660 unsigned char *beg, *payload;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006661 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006662 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
6663 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006664 struct quic_frame cc_frm = { };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006665 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006666 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006667 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006668 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006669 struct quic_frame *cf;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006670 int must_ack, ret = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006671 int nb_aepkts_since_last_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006672
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006673 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6674
Frédéric Lécailleea604992020-12-24 13:01:37 +01006675 /* Length field value with CRYPTO frames if present. */
6676 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006677 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006678 /* When not probing, and no immediate close is required, reduce the size of this
6679 * buffer to respect the congestion controller window.
6680 * 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 +01006681 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006682 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006683 size_t path_room;
6684
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006685 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006686 if (end - beg > path_room)
6687 end = beg + path_room;
6688 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006689
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006690 /* Ensure there is enough room for the TLS encryption tag and a zero token
6691 * length field if any.
6692 */
6693 if (end - pos < QUIC_TLS_TAG_LEN +
6694 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
6695 goto no_room;
6696
6697 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006698 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006699 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006700 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006701 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006702 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006703 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006704 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006705 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006706 goto no_room;
6707
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006708 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006709 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006710 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006711 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006712 /* Build an ACK frame if required. */
6713 ack_frm_len = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006714 nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
6715 must_ack = !qel->pktns->tx.pto_probe &&
6716 (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
6717 (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK)));
6718 if (force_ack || must_ack) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006719 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006720 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006721 ack_frm.tx_ack.arngs = arngs;
6722 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6723 qel->pktns->tx.ack_delay =
6724 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6725 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6726 }
6727 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006728 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6729 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6730 * that from here, we do not know if we will have to send a PING frame.
6731 * This will be decided after having computed the ack-eliciting frames
6732 * to be added to this packet.
6733 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006734 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 +01006735 if (!ack_frm_len)
6736 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006737 }
6738
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006739 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006740 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006741 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006742 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006743 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006744
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006745 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006746 /* Initialize the length of the frames built below to <len>.
6747 * If any frame could be successfully built by qc_build_frms(),
6748 * we will have len_frms > len.
6749 */
6750 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006751 if (!qc_build_frms(&frm_list, frms,
6752 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006753 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006754 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006755 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6756 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006757 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006758 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006759
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006760 /* Length (of the remaining data). Must not fail because, the buffer size
6761 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6762 * for the encryption tag. It must be taken into an account for the length
6763 * of this packet.
6764 */
6765 if (len_frms)
6766 len = len_frms + QUIC_TLS_TAG_LEN;
6767 else
6768 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006769 /* CONNECTION_CLOSE frame */
6770 if (cc) {
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006771 qc_build_cc_frm(qc, qel, &cc_frm);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006772 len += qc_frm_len(&cc_frm);
6773 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006774 add_ping_frm = 0;
6775 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006776 len_sz = quic_int_getsize(len);
6777 /* Add this packet size to <dglen> */
6778 dglen += head_len + len_sz + len;
6779 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6780 /* This is a maximum padding size */
6781 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6782 /* The length field value is of this packet is <len> + <padding_len>
6783 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006784 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006785 * sizes from <padding_len>.
6786 */
6787 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6788 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006789 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006790 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006791 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006792 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006793 add_ping_frm = 1;
6794 len += 1;
6795 }
6796 /* 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 +01006797 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006798 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6799 }
6800
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006801 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6802 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006803
6804 /* Packet number field address. */
6805 *buf_pn = pos;
6806
6807 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006808 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6809 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006810
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006811 /* payload building (ack-eliciting or not frames) */
6812 payload = pos;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006813 if (ack_frm_len) {
6814 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6815 goto no_room;
6816
6817 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006818 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006819 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006820
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006821 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006822 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006823 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006824 unsigned char *spos = pos;
6825
6826 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006827 ssize_t room = end - pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006828 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006829 qc, NULL, NULL, &room);
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006830 /* TODO: this should not have happened except if we
6831 * are limited by the congestion control.
6832 * Note that <cf> was added from <frm_list> to <frms> list by
6833 * qc_build_frms().
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006834 */
6835 LIST_DELETE(&cf->list);
6836 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006837 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006838 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006839
6840 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006841 quic_tx_packet_refinc(pkt);
6842 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006843 }
6844 }
6845
6846 /* Build a PING frame if needed. */
6847 if (add_ping_frm) {
6848 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006849 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006850 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006851 }
6852
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006853 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006854 if (cc) {
6855 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6856 goto no_room;
6857
6858 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6859 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006860
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006861 /* Build a PADDING frame if needed. */
6862 if (padding_len) {
6863 frm.type = QUIC_FT_PADDING;
6864 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006865 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006866 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006867 }
6868
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006869 if (pos == payload) {
6870 /* No payload was built because of congestion control */
6871 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
6872 goto no_room;
6873 }
6874
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006875 /* If this packet is ack-eliciting and we are probing let's
6876 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006877 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006878 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6879 qel->pktns->tx.pto_probe)
6880 qel->pktns->tx.pto_probe--;
6881
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006882 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006883 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006884
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006885 ret = 1;
6886 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
6887 leave:
6888 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6889 return ret;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006890
6891 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006892 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006893 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006894 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
6895 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006896}
6897
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006898static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006899{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006900 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006901 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006902 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006903 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006904 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006905 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006906 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006907 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006908 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006909 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006910}
6911
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006912/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006913 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6914 * of prebuilt frames.
6915 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006916 * Return -2 if the packet could not be allocated or encrypted for any reason,
6917 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006918 * XXX NOTE XXX
6919 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6920 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6921 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006922 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006923static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6924 const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006925 struct quic_enc_level *qel,
6926 struct quic_tls_ctx *tls_ctx, struct list *frms,
6927 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006928 size_t dglen, int pkt_type, int force_ack,
6929 int padding, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006930{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006931 struct quic_tx_packet *ret_pkt = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006932 /* The pointer to the packet number field. */
6933 unsigned char *buf_pn;
6934 unsigned char *beg, *end, *payload;
6935 int64_t pn;
6936 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006937 struct quic_tx_packet *pkt;
6938
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006939 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006940 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006941 pkt = pool_alloc(pool_head_quic_tx_packet);
6942 if (!pkt) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006943 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006944 *err = -2;
6945 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006946 }
6947
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006948 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006949 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006950 pn_len = 0;
6951 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006952
6953 pn = qel->pktns->tx.next_pn + 1;
6954 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 +02006955 force_ack, padding, cc, probe, qel, qc, ver, frms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006956 // trace already emitted by function above
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006957 *err = -1;
6958 goto err;
6959 }
6960
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006961 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006962 payload = buf_pn + pn_len;
6963 payload_len = end - payload;
6964 aad_len = payload - beg;
6965
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006966 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006967 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006968 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006969 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006970 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006971
6972 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006973 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006974 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006975 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006976 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006977 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006978 goto err;
6979 }
6980
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006981 /* Consume a packet number */
6982 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006983 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006984 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006985 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006986 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
6987 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006988 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6989 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006990 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006991 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006992 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006993 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006994 pkt->in_flight_len = pkt->len;
6995 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006996 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01006997 /* Always reset this flags */
6998 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006999 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
7000 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
7001 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
7002 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02007003
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007004 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007005
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007006 ret_pkt = pkt;
7007 leave:
7008 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt);
7009 return ret_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007010
7011 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01007012 /* TODO: what about the frames which have been built
7013 * for this packet.
7014 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007015 free_quic_tx_packet(qc, pkt);
7016 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007017}
7018
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007019
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007020/* Called from the upper layer, to subscribe <es> to events <event_type>. The
7021 * event subscriber <es> is not allowed to change from a previous call as long
7022 * as at least one event is still subscribed. The <event_type> must only be a
7023 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
7024 */
7025static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7026{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007027 struct quic_conn *qc = conn->handle.qc;
7028 struct qcc *qcc = qc->qcc;
7029
7030 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007031
7032 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
7033 BUG_ON(qcc->subs && qcc->subs != es);
7034
7035 es->events |= event_type;
7036 qcc->subs = es;
7037
7038 if (event_type & SUB_RETRY_RECV)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007039 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007040
7041 if (event_type & SUB_RETRY_SEND)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007042 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7043
7044 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007045
7046 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007047}
7048
7049/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
7050 * The <es> pointer is not allowed to differ from the one passed to the
7051 * subscribe() call. It always returns zero.
7052 */
7053static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7054{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007055 int ret;
7056 struct quic_conn *qc = conn->handle.qc;
7057 struct qcc *qcc = qc->qcc;
7058
7059 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
7060
7061 if (event_type & SUB_RETRY_RECV)
7062 TRACE_DEVEL("unsubscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
7063 if (event_type & SUB_RETRY_SEND)
7064 TRACE_DEVEL("unsubscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7065
7066 ret = conn_unsubscribe(conn, xprt_ctx, event_type, es);
7067
7068 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
7069
7070 return ret;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007071}
7072
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007073/* Store in <xprt_ctx> the context attached to <conn>.
7074 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007075 */
7076static int qc_conn_init(struct connection *conn, void **xprt_ctx)
7077{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01007078 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007079
7080 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
7081
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007082 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007083 if (*xprt_ctx)
7084 goto out;
7085
Willy Tarreau784b8682022-04-11 14:18:10 +02007086 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007087
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007088 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01007089 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007090
7091 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007092}
7093
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007094/* Start the QUIC transport layer */
7095static int qc_xprt_start(struct connection *conn, void *ctx)
7096{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007097 int ret = 0;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007098 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02007099 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007100
Willy Tarreau784b8682022-04-11 14:18:10 +02007101 qc = conn->handle.qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007102 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007103 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
7104 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02007105 /* prepare a CONNECTION_CLOSE frame */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02007106 quic_set_connection_close(qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007107 goto out;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007108 }
7109
7110 /* mux-quic can now be considered ready. */
7111 qc->mux_state = QC_MUX_READY;
7112
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007113 tasklet_wakeup(qctx->wait_event.tasklet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007114
7115 ret = 1;
7116 out:
7117 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
7118 return ret;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007119}
7120
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007121static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
7122{
Willy Tarreau784b8682022-04-11 14:18:10 +02007123 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007124 return NULL;
7125
Willy Tarreau784b8682022-04-11 14:18:10 +02007126 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007127}
7128
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007129/* transport-layer operations for QUIC connections. */
7130static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02007131 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007132 .subscribe = quic_conn_subscribe,
7133 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007134 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007135 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007136 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
7137 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01007138 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007139 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007140 .name = "QUIC",
7141};
7142
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007143static void __quic_conn_init(void)
7144{
7145 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
7146 xprt_register(XPRT_QUIC, &ssl_quic);
7147}
Willy Tarreau79367f92022-04-25 19:18:24 +02007148INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007149
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007150static void __quic_conn_deinit(void)
7151{
7152 BIO_meth_free(ha_quic_meth);
7153}
Willy Tarreau79367f92022-04-25 19:18:24 +02007154REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007155
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01007156/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
7157 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05007158 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007159 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007160struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007161{
7162 unsigned char *pos;
7163 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007164 struct quic_dghdlr *dghdlr = ctx;
7165 struct quic_dgram *dgram;
7166 int first_pkt = 1;
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007167 struct list *tasklist_head = NULL;
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007168 int max_dgrams = global.tune.maxpollevents;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007169
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007170 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7171
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007172 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007173 pos = dgram->buf;
7174 end = pos + dgram->len;
7175 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007176 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007177
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007178 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007179 if (!pkt) {
7180 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
7181 goto leave;
7182 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007183
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007184 LIST_INIT(&pkt->qc_rx_pkt_list);
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02007185 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007186 quic_rx_packet_refinc(pkt);
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007187 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram, &tasklist_head);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007188 first_pkt = 0;
7189 pos += pkt->len;
7190 quic_rx_packet_refdec(pkt);
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007191
7192 /* Free rejected packets */
7193 if (!pkt->refcnt) {
7194 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
7195 pool_free(pool_head_quic_rx_packet, pkt);
7196 }
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007197 } while (pos < end);
7198
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007199 /* Increasing the received bytes counter by the UDP datagram length
7200 * if this datagram could be associated to a connection.
7201 */
7202 if (dgram->qc)
7203 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01007204
7205 /* Mark this datagram as consumed */
7206 HA_ATOMIC_STORE(&dgram->buf, NULL);
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007207
7208 if (--max_dgrams <= 0)
7209 goto stop_here;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007210 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007211
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007212 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
7213
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007214 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007215
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007216 stop_here:
7217 /* too much work done at once, come back here later */
7218 if (!MT_LIST_ISEMPTY(&dghdlr->dgrams))
7219 tasklet_wakeup((struct tasklet *)t);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007220 leave:
7221 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007222 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007223}
7224
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05007225/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007226 * Returns 1 if succeeded, 0 if not.
7227 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02007228int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
7229 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007230{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007231 int ret = 0, long_header;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007232 size_t minlen, skip;
7233
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007234 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
7235
7236 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
7237 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007238 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007239 }
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007240
7241 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02007242 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
7243 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007244 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Willy Tarreauaf5138f2022-08-05 10:09:32 +02007245 if (end - buf < minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007246 goto err;
7247
7248 buf += skip;
7249 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
7250 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
7251 goto err;
7252
7253 *dcid = buf;
7254
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007255 ret = 1;
7256 leave:
7257 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
7258 return ret;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007259
7260 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007261 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
7262 goto leave;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007263}
7264
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007265/* Notify the MUX layer if alive about an imminent close of <qc>. */
7266void qc_notify_close(struct quic_conn *qc)
7267{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007268 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
7269
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007270 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007271 goto leave;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007272
7273 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007274 /* wake up the MUX */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007275 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
7276 TRACE_STATE("connection closure notidfied to mux",
7277 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007278 qc->conn->mux->wake(qc->conn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007279 }
7280 else
7281 TRACE_STATE("connection closure not notidfied to mux",
7282 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
7283 leave:
7284 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007285}
7286
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007287/*
7288 * Local variables:
7289 * c-indent-level: 8
7290 * c-basic-offset: 8
7291 * End:
7292 */