blob: 0aad272138771ccd4f11b35344206b2f468d793a [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écaille86a53c52022-08-19 18:18:13 +0200923 if (!quic_tls_dec_aes_ctx_init(&rx->hp_ctx, rx->hp, rx->hp_key)) {
924 TRACE_ERROR("could not initial RX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
925 goto leave;
926 }
927
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100928 /* Enqueue this connection asap if we could derive O-RTT secrets as
929 * listener. Note that a listener derives only RX secrets for this
930 * level.
931 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200932 if (qc_is_listener(qc) && level == ssl_encryption_early_data) {
933 TRACE_DEVEL("pushing connection into accept queue", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100934 quic_accept_push_qc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200935 }
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100936
937 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100938 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100939
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200940 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 +0100941 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100942 write_secret, secret_len)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200943 TRACE_ERROR("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
944 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100945 }
946
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200947 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200948 TRACE_ERROR("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
949 goto leave;
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200950 }
951
Frédéric Lécaille86a53c52022-08-19 18:18:13 +0200952 if (!quic_tls_enc_aes_ctx_init(&tx->hp_ctx, tx->hp, tx->hp_key)) {
953 TRACE_ERROR("could not initial TX TLS cipher context for HP", QUIC_EV_CONN_RWSEC, qc);
954 goto leave;
955 }
956
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100957 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100958 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
959 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
960 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
961
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200962 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100963 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
964 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200965 TRACE_ERROR("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
966 goto leave;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100967 }
968
969 memcpy(rx->secret, read_secret, secret_len);
970 rx->secretlen = secret_len;
971 memcpy(tx->secret, write_secret, secret_len);
972 tx->secretlen = secret_len;
973 /* Initialize all the secret keys lengths */
974 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
975 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200976 if (!quic_tls_key_update(qc)) {
977 // trace already emitted by function above
978 goto leave;
979 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100980 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100981
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100982 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100983 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100984 no_secret:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200985 ret = 1;
986 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100987 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200988 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100989}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100990
991/* This function copies the CRYPTO data provided by the TLS stack found at <data>
992 * with <len> as size in CRYPTO buffers dedicated to store the information about
993 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200994 * It fails (returns 0) only if it could not managed to allocate enough CRYPTO
995 * buffers to store all the data.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100996 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
997 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200998static int quic_crypto_data_cpy(struct quic_conn *qc, struct quic_enc_level *qel,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100999 const unsigned char *data, size_t len)
1000{
1001 struct quic_crypto_buf **qcb;
1002 /* The remaining byte to store in CRYPTO buffers. */
1003 size_t cf_offset, cf_len, *nb_buf;
1004 unsigned char *pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001005 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001006
1007 nb_buf = &qel->tx.crypto.nb_buf;
1008 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1009 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1010 cf_len = len;
1011
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001012 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1013
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001014 while (len) {
1015 size_t to_copy, room;
1016
1017 pos = (*qcb)->data + (*qcb)->sz;
1018 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1019 to_copy = len > room ? room : len;
1020 if (to_copy) {
1021 memcpy(pos, data, to_copy);
1022 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1023 qel->tx.crypto.sz += to_copy;
1024 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001025 len -= to_copy;
1026 data += to_copy;
1027 }
1028 else {
1029 struct quic_crypto_buf **tmp;
1030
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001031 // FIXME: realloc!
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001032 tmp = realloc(qel->tx.crypto.bufs,
1033 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1034 if (tmp) {
1035 qel->tx.crypto.bufs = tmp;
1036 qcb = &qel->tx.crypto.bufs[*nb_buf];
1037 *qcb = pool_alloc(pool_head_quic_crypto_buf);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001038 if (!*qcb) {
1039 TRACE_ERROR("Could not allocate crypto buf", QUIC_EV_CONN_ADDDATA, qc);
1040 goto leave;
1041 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001042
1043 (*qcb)->sz = 0;
1044 ++*nb_buf;
1045 }
1046 else {
1047 break;
1048 }
1049 }
1050 }
1051
1052 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1053 * have been buffered.
1054 */
1055 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001056 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001057 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001058
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001059 /* There is at most one CRYPTO frame in this packet number
1060 * space. Let's look for it.
1061 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001062 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001063 if (frm->type != QUIC_FT_CRYPTO)
1064 continue;
1065
1066 /* Found */
1067 found = frm;
1068 break;
1069 }
1070
1071 if (found) {
1072 found->crypto.len += cf_len;
1073 }
1074 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001075 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001076 if (!frm) {
1077 TRACE_ERROR("Could not allocate quic frame", QUIC_EV_CONN_ADDDATA, qc);
1078 goto leave;
1079 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001080
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001081 LIST_INIT(&frm->reflist);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001082 frm->type = QUIC_FT_CRYPTO;
1083 frm->crypto.offset = cf_offset;
1084 frm->crypto.len = cf_len;
1085 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001086 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001087 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001088 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001089 ret = len == 0;
1090 leave:
1091 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
1092 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001093}
1094
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001095/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1096 * activity for <qc> will be interrupted.
1097 */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001098void quic_set_connection_close(struct quic_conn *qc, const struct quic_err err)
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001099{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001100 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001101 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001102 goto leave;
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001103
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001104 TRACE_STATE("setting immediate close", QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001105 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001106 qc->err.code = err.code;
1107 qc->err.app = err.app;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001108 leave:
1109 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001110}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001111
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001112/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1113void quic_set_tls_alert(struct quic_conn *qc, int alert)
1114{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001115 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1116
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001117 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1118 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001119 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02001120 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1121 }
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02001122 quic_set_connection_close(qc, quic_err_tls(alert));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001123 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001124 TRACE_STATE("Alert set", QUIC_EV_CONN_SSLALERT, qc);
1125
1126 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001127}
1128
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001129/* Set the application for <qc> QUIC connection.
1130 * Return 1 if succeeded, 0 if not.
1131 */
1132int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1133{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001134 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1135 qc->app_ops = &h3_ops;
1136 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1137 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001138 else
1139 return 0;
1140
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001141 return 1;
1142}
1143
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001144/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1145 * wants to provide the QUIC layer with CRYPTO data.
1146 * Returns 1 if succeeded, 0 if not.
1147 */
1148int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1149 const uint8_t *data, size_t len)
1150{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001151 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001152 enum quic_tls_enc_level tel;
1153 struct quic_enc_level *qel;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001154 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001155
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001156 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1157 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001158
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001159 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001160 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001161 goto out;
1162 }
1163
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001164 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001165 if (tel == -1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001166 TRACE_ERROR("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
1167 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001168 }
1169
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001170 qel = &qc->els[tel];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001171 if (!quic_crypto_data_cpy(qc, qel, data, len)) {
1172 TRACE_ERROR("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
1173 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001174 }
1175
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001176 TRACE_DEVEL("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001177 qc, &level, &len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001178 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001179 ret = 1;
1180 leave:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001181 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001182 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001183}
1184
1185int ha_quic_flush_flight(SSL *ssl)
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
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001189 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1190 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001191
1192 return 1;
1193}
1194
1195int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1196{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001197 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001198
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001199 TRACE_ENTER(QUIC_EV_CONN_SSLALERT, qc);
1200
1201 TRACE_PROTO("Received TLS alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1202
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001203 quic_set_tls_alert(qc, alert);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001204 TRACE_LEAVE(QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001205 return 1;
1206}
1207
1208/* QUIC TLS methods */
1209static SSL_QUIC_METHOD ha_quic_method = {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001210 .set_encryption_secrets = ha_quic_set_encryption_secrets,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001211 .add_handshake_data = ha_quic_add_handshake_data,
1212 .flush_flight = ha_quic_flush_flight,
1213 .send_alert = ha_quic_send_alert,
1214};
1215
1216/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1217 * Returns an error count.
1218 */
1219int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1220{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001221 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1222 int cfgerr = 0;
1223
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001224 long options =
1225 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1226 SSL_OP_SINGLE_ECDH_USE |
1227 SSL_OP_CIPHER_SERVER_PREFERENCE;
1228 SSL_CTX *ctx;
1229
1230 ctx = SSL_CTX_new(TLS_server_method());
1231 bind_conf->initial_ctx = ctx;
1232
1233 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001234 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1235 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1236 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001237
1238#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaillee06f7452022-06-16 15:06:44 +02001239#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001240 if (bind_conf->ssl_conf.early_data) {
1241 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001242 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001243 }
1244 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1245 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1246#else
1247 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1248#endif
1249 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1250#endif
1251 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1252
1253 return cfgerr;
1254}
1255
1256/* Decode an expected packet number from <truncated_on> its truncated value,
1257 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1258 * the number of bits used to encode this packet number (its length in bytes * 8).
1259 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1260 */
1261static uint64_t decode_packet_number(uint64_t largest_pn,
1262 uint32_t truncated_pn, unsigned int pn_nbits)
1263{
1264 uint64_t expected_pn = largest_pn + 1;
1265 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1266 uint64_t pn_hwin = pn_win / 2;
1267 uint64_t pn_mask = pn_win - 1;
1268 uint64_t candidate_pn;
1269
1270
1271 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1272 /* Note that <pn_win> > <pn_hwin>. */
1273 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1274 candidate_pn + pn_hwin <= expected_pn)
1275 return candidate_pn + pn_win;
1276
1277 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1278 return candidate_pn - pn_win;
1279
1280 return candidate_pn;
1281}
1282
1283/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1284 * cryptographic context.
1285 * <largest_pn> is the largest received packet number and <pn> the address of
1286 * the packet number field for this packet with <byte0> address of its first byte.
1287 * <end> points to one byte past the end of this packet.
1288 * Returns 1 if succeeded, 0 if not.
1289 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001290static int qc_do_rm_hp(struct quic_conn *qc,
1291 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001292 int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001293{
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02001294 int ret, i, pnlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001295 uint64_t packet_number;
1296 uint32_t truncated_pn = 0;
1297 unsigned char mask[5] = {0};
1298 unsigned char *sample;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001299 EVP_CIPHER_CTX *cctx = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001300
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001301 TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
1302
1303 ret = 0;
1304
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001305 /* Check there is enough data in this packet. */
Frédéric Lécaille99897d12022-08-08 10:28:07 +02001306 if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001307 TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
1308 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001309 }
1310
1311 cctx = EVP_CIPHER_CTX_new();
1312 if (!cctx) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001313 TRACE_ERROR("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
1314 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001315 }
1316
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001317 sample = pn + QUIC_PACKET_PN_MAXLEN;
1318
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02001319 if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
1320 TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001321 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001322 }
1323
1324 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1325 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1326 for (i = 0; i < pnlen; i++) {
1327 pn[i] ^= mask[i + 1];
1328 truncated_pn = (truncated_pn << 8) | pn[i];
1329 }
1330
1331 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1332 /* Store remaining information for this unprotected header */
1333 pkt->pn = packet_number;
1334 pkt->pnl = pnlen;
1335
1336 ret = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001337 leave:
1338 if (cctx)
1339 EVP_CIPHER_CTX_free(cctx);
1340 TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001341 return ret;
1342}
1343
1344/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1345 * address, with <payload_len> as payload length, <aad> as address of
1346 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1347 * context.
1348 * Returns 1 if succeeded, 0 if not.
1349 */
1350static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1351 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001352 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001353{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001354 int ret = 0;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001355 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001356 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001357 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001358 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001359
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001360 TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
1361
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001362 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001363 TRACE_ERROR("AEAD IV building for 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
1367 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001368 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001369 TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001370 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001371 }
1372
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001373 ret = 1;
1374 leave:
1375 TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
1376 return ret;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001377
1378 err:
1379 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001380 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001381}
1382
1383/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1384 * Returns 1 if succeeded, 0 if not.
1385 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001386static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel,
1387 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001388{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001389 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001390 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001391 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001392 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001393 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001394 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1395 unsigned char *rx_key = tls_ctx->rx.key;
1396
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001397 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
1398
1399 ret = 0;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001400 kp_changed = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001401
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001402 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1403 /* The two tested bits are not at the same position,
1404 * this is why they are first both inversed.
1405 */
1406 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1407 if (pkt->pn < tls_ctx->rx.pn) {
1408 /* The lowest packet number of a previous key phase
1409 * cannot be null if it really stores previous key phase
1410 * secrets.
1411 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001412 // TODO: check if BUG_ON() more suitable
1413 if (!pkt->qc->ku.prv_rx.pn) {
1414 TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
1415 goto leave;
1416 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001417
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001418 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1419 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001420 rx_key = pkt->qc->ku.prv_rx.key;
1421 }
1422 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1423 /* Next key phase */
1424 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001425 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1426 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001427 rx_key = pkt->qc->ku.nxt_rx.key;
1428 }
1429 }
1430 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001431
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001432 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn)) {
1433 TRACE_ERROR("quic_aead_iv_build() failed", QUIC_EV_CONN_RXPKT, qc);
1434 goto leave;
1435 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001436
1437 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1438 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001439 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001440 if (!ret) {
1441 TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
1442 goto leave;
1443 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001444
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001445 /* Update the keys only if the packet decryption succeeded. */
1446 if (kp_changed) {
1447 quic_tls_rotate_keys(pkt->qc);
1448 /* Toggle the Key Phase bit */
1449 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1450 /* Store the lowest packet number received for the current key phase */
1451 tls_ctx->rx.pn = pkt->pn;
1452 /* Prepare the next key update */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001453 if (!quic_tls_key_update(pkt->qc)) {
1454 TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
1455 goto leave;
1456 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001457 }
1458
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001459 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001460 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001461 ret = 1;
1462 leave:
1463 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille11a6f402022-08-16 14:48:59 +02001464 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001465}
1466
Frédéric Lécaille96367152022-04-25 09:40:19 +02001467
1468/* Remove references to <frm> frame */
1469static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1470{
Frédéric Lécaille96367152022-04-25 09:40:19 +02001471 struct quic_frame *f, *tmp;
1472
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001473 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1474
Frédéric Lécaille96367152022-04-25 09:40:19 +02001475 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001476 f->origin = NULL;
1477 LIST_DELETE(&f->ref);
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001478 if (f->pkt) {
1479 TRACE_DEVEL("remove frame reference",
1480 QUIC_EV_CONN_PRSAFRM, qc, f, &f->pkt->pn_node.key);
1481 }
1482 else {
1483 TRACE_DEVEL("remove frame reference for unsent frame",
1484 QUIC_EV_CONN_PRSAFRM, qc, f);
1485 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001486 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001487
1488 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001489}
1490
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001491/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001492void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001493{
1494 uint64_t pn;
1495 struct quic_frame *origin, *f, *tmp;
1496
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001497 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1498
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001499 /* Identify this frame: a frame copy or one of its copies */
1500 origin = frm->origin ? frm->origin : frm;
1501 /* Ensure the source of the copies is flagged as acked, <frm> being
1502 * possibly a copy of <origin>
1503 */
1504 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1505 /* Mark all the copy of <origin> as acknowledged. We must
1506 * not release the packets (releasing the frames) at this time as
1507 * they are possibly also to be acknowledged alongside the
1508 * the current one.
1509 */
1510 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
Frédéric Lécailleeba90882022-08-25 16:06:48 +02001511 if (f->pkt) {
1512 pn = f->pkt->pn_node.key;
1513 TRACE_DEVEL("mark frame as acked from packet",
1514 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1515 }
1516 else {
1517 TRACE_DEVEL("mark unsend frame as acked",
1518 QUIC_EV_CONN_PRSAFRM, qc, f);
1519 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001520 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1521 f->origin = NULL;
1522 LIST_DELETE(&f->ref);
1523 }
1524 LIST_DELETE(&frm->list);
1525 pn = frm->pkt->pn_node.key;
1526 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001527 TRACE_DEVEL("freeing frame from packet",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001528 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1529 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001530
1531 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001532}
1533
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001534/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1535 * and all STREAM data are acknowledged. The MUX is responsible to have set
1536 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1537 *
1538 * TODO this should also be called on lost packet detection
1539 */
1540static void qc_check_close_on_released_mux(struct quic_conn *qc)
1541{
1542 struct ssl_sock_ctx *ctx = qc->xprt_ctx;
1543
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001544 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1545
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001546 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1547 /* Reuse errcode which should have been previously set by the MUX on release. */
1548 quic_set_connection_close(qc, qc->err);
1549 tasklet_wakeup(ctx->wait_event.tasklet);
1550 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001551
1552 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001553}
1554
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001555/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001556 *
1557 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001558 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001559static int quic_stream_try_to_consume(struct quic_conn *qc,
1560 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001561{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001562 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001563 struct eb64_node *frm_node;
1564
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001565 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1566
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001567 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001568 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001569 while (frm_node) {
1570 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001571 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001572 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001573
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001574 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001575 offset = strm->offset.key;
1576 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001577
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001578 if (offset > stream->ack_offset)
1579 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001580
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001581 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001582 /* cf. next comment : frame may be freed at this stage. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001583 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001584 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001585 ret = 1;
1586 }
1587
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001588 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1589 * has been freed. with the stream frames tree. Nothing to do
1590 * anymore in here.
1591 */
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001592 if (!stream) {
1593 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001594 ret = 1;
1595 goto leave;
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001596 }
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001597
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001598 frm_node = eb64_next(frm_node);
1599 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001600
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001601 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001602 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001603 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001604
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001605 leave:
1606 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001607 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001608}
1609
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001610/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001611static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1612 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001613{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001614 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001615
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001616 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm);
1617
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001618 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001619 switch (frm->type) {
1620 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1621 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001622 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001623 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001624 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001625 const size_t offset = strm_frm->offset.key;
1626 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001627
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001628 /* do not use strm_frm->stream as the qc_stream_desc instance
1629 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001630 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001631 *
1632 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001633 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001634 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001635 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1636 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001637 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001638 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001639 /* early return */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001640 goto leave;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001641 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001642 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001643
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001644 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001645 if (offset <= stream->ack_offset) {
1646 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001647 stream_acked = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001648 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001649 qc, strm_frm, stream);
1650 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001651
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001652 if (!stream) {
1653 /* no need to continue if stream freed. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001654 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001655 qc_release_frm(qc, frm);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001656 qc_check_close_on_released_mux(qc);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001657 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001658 }
1659
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001660 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Frédéric Lécailleda342552022-04-25 10:28:49 +02001661 qc, strm_frm, stream);
1662 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001663 }
1664 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001665 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001666 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001667
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001668 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001669 }
1670 break;
1671 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001672 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001673 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001674
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001675 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001676 struct qcc *qcc = qc->qcc;
1677
1678 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1679 tasklet_wakeup(qcc->subs->tasklet);
1680 qcc->subs->events &= ~SUB_RETRY_SEND;
1681 if (!qcc->subs->events)
1682 qcc->subs = NULL;
1683 }
1684 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001685 leave:
1686 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001687}
1688
1689/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1690 * deallocating them, and their TX frames.
1691 * Returns the last node reached to be used for the next range.
1692 * May be NULL if <largest> node could not be found.
1693 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001694static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1695 struct eb_root *pkts,
1696 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001697 struct list *newly_acked_pkts,
1698 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001699 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001700{
1701 struct eb64_node *node;
1702 struct quic_tx_packet *pkt;
1703
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001704 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1705
Frédéric Lécaille843399f2022-07-22 16:27:44 +02001706 node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001707 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001708 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001709
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001710 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001711 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001712 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001713 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001714 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001715 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001716 node = eb64_prev(node);
1717 eb64_delete(&pkt->pn_node);
1718 }
1719
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001720 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001721 return node;
1722}
1723
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001724/* Remove all frames from <pkt_frm_list> and reinsert them in the
1725 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001726 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001727static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001728 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001729 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001730{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001731 struct quic_frame *frm, *frmbak;
1732 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001733 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001734 uint64_t pn = pkt->pn_node.key;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001735
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001736 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1737
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001738 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001739 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001740 LIST_DELETE(&frm->list);
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001741 quic_tx_packet_refdec(pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001742 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001743 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001744 /* Remove any reference to this frame */
1745 qc_frm_unref(qc, frm);
1746 switch (frm->type) {
1747 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1748 {
1749 struct quic_stream *strm_frm = &frm->stream;
1750 struct eb64_node *node = NULL;
1751 struct qc_stream_desc *stream_desc;
1752
1753 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1754 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001755 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1756 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001757 qc, frm, &pn);
1758 pool_free(pool_head_quic_frame, frm);
1759 continue;
1760 }
1761
1762 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1763 /* Do not resend this frame if in the "already acked range" */
1764 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001765 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001766 QUIC_EV_CONN_PRSAFRM, qc, frm);
1767 continue;
1768 }
1769 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1770 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001771 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001772 QUIC_EV_CONN_PRSAFRM, qc, frm);
1773 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001774 break;
1775 }
1776
1777 default:
1778 break;
1779 }
1780
1781 /* Do not resend probing packet with old data */
1782 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001783 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1784 qc, frm, &pn);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001785 if (frm->origin)
1786 LIST_DELETE(&frm->ref);
1787 pool_free(pool_head_quic_frame, frm);
1788 continue;
1789 }
1790
1791 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001792 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1793 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001794 qc, frm, &pn);
1795 pool_free(pool_head_quic_frame, frm);
1796 }
1797 else {
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001798 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1799 /* Mark this STREAM frame as lost. A look up their stream descriptor
1800 * will be performed to check the stream is not consumed or released.
1801 */
Amaury Denoyellef372e742022-08-16 14:41:57 +02001802 frm->flags |= QUIC_FL_TX_FRAME_LOST;
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001803 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001804 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001805 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001806 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001807 }
1808
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001809 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001810
1811 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001812}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001813
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001814/* Free <pkt> TX packet and its attached frames.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001815 * This is the responsibility of the caller to remove this packet of
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001816 * any data structure it was possibly attached to.
1817 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001818static inline void free_quic_tx_packet(struct quic_conn *qc,
1819 struct quic_tx_packet *pkt)
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001820{
1821 struct quic_frame *frm, *frmbak;
1822
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001823 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1824
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001825 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001826 goto leave;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001827
1828 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1829 LIST_DELETE(&frm->list);
1830 pool_free(pool_head_quic_frame, frm);
1831 }
1832 pool_free(pool_head_quic_tx_packet, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001833
1834 leave:
1835 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001836}
1837
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001838/* Free the TX packets of <pkts> list */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001839static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001840{
1841 struct quic_tx_packet *pkt, *tmp;
1842
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001843 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1844
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001845 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001846 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001847 eb64_delete(&pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001848 free_quic_tx_packet(qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001849 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001850
1851 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001852}
1853
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001854/* Remove already sent ranges of acknowledged packet numbers from
1855 * <pktns> packet number space tree below <largest_acked_pn> possibly
1856 * updating the range which contains <largest_acked_pn>.
1857 * Never fails.
1858 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001859static void qc_treat_ack_of_ack(struct quic_conn *qc,
1860 struct quic_pktns *pktns,
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001861 int64_t largest_acked_pn)
1862{
1863 struct eb64_node *ar, *next_ar;
1864 struct quic_arngs *arngs = &pktns->rx.arngs;
1865
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001866 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1867
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001868 ar = eb64_first(&arngs->root);
1869 while (ar) {
1870 struct quic_arng_node *ar_node;
1871
1872 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001873 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001874
1875 if ((int64_t)ar_node->first.key > largest_acked_pn) {
1876 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001877 break;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001878 }
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001879
1880 if (largest_acked_pn < ar_node->last) {
1881 eb64_delete(ar);
1882 ar_node->first.key = largest_acked_pn + 1;
1883 eb64_insert(&arngs->root, ar);
1884 break;
1885 }
1886
1887 eb64_delete(ar);
1888 pool_free(pool_head_quic_arng, ar_node);
1889 arngs->sz--;
1890 ar = next_ar;
1891 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001892
1893 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001894}
1895
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001896/* Send a packet ack event nofication for each newly acked packet of
1897 * <newly_acked_pkts> list and free them.
1898 * Always succeeds.
1899 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001900static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001901 struct list *newly_acked_pkts)
1902{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001903 struct quic_tx_packet *pkt, *tmp;
1904 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1905
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001906 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1907
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001908 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1909 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001910 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001911 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001912 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001913 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001914 /* If this packet contained an ACK frame, proceed to the
1915 * acknowledging of range of acks from the largest acknowledged
1916 * packet number which was sent in an ACK frame by this packet.
1917 */
1918 if (pkt->largest_acked_pn != -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001919 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001920 ev.ack.acked = pkt->in_flight_len;
1921 ev.ack.time_sent = pkt->time_sent;
1922 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001923 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001924 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001925 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001926 }
1927
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001928 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1929
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001930}
1931
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001932/* Release all the frames attached to <pktns> packet number space */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001933static inline void qc_release_pktns_frms(struct quic_conn *qc,
1934 struct quic_pktns *pktns)
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001935{
1936 struct quic_frame *frm, *frmbak;
1937
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001938 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
1939
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001940 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1941 LIST_DELETE(&frm->list);
1942 pool_free(pool_head_quic_frame, frm);
1943 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001944
1945 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001946}
1947
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001948/* Handle <pkts> list of lost packets detected at <now_us> handling
1949 * their TX frames.
1950 * Send a packet loss event to the congestion controller if
1951 * in flight packet have been lost.
1952 * Also frees the packet in <pkts> list.
1953 * Never fails.
1954 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001955static inline void qc_release_lost_pkts(struct quic_conn *qc,
1956 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001957 struct list *pkts,
1958 uint64_t now_us)
1959{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001960 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001961
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001962 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1963
Frédéric Lécaille277c4622022-08-24 17:06:04 +02001964 if (LIST_ISEMPTY(pkts))
1965 goto leave;
1966
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001967 oldest_lost = newest_lost = NULL;
1968 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001969 struct list tmp = LIST_HEAD_INIT(tmp);
1970
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001971 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001972 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001973 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001974 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001975 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001976 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001977 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001978 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001979 if (!oldest_lost) {
1980 oldest_lost = newest_lost = pkt;
1981 }
1982 else {
1983 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001984 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001985 newest_lost = pkt;
1986 }
1987 }
1988
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001989 if (newest_lost) {
1990 /* Sent a congestion event to the controller */
Benoit DOLEZ69e3f052022-06-08 09:28:56 +02001991 struct quic_cc_event ev = { };
1992
1993 ev.type = QUIC_CC_EVT_LOSS;
1994 ev.loss.time_sent = newest_lost->time_sent;
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001995
1996 quic_cc_event(&qc->path->cc, &ev);
1997 }
1998
1999 /* If an RTT have been already sampled, <rtt_min> has been set.
2000 * We must check if we are experiencing a persistent congestion.
2001 * If this is the case, the congestion controller must re-enter
2002 * slow start state.
2003 */
2004 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2005 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
2006
2007 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2008 now_ms, qc->max_ack_delay))
2009 qc->path->cc.algo->slow_start(&qc->path->cc);
2010 }
2011
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002012 quic_tx_packet_refdec(oldest_lost);
2013 if (newest_lost != oldest_lost)
2014 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002015
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002016 leave:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002017 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002018}
2019
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002020/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2021 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002022 * 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 +01002023 * acked ack-eliciting packet.
2024 * Return 1, if succeeded, 0 if not.
2025 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002026static inline int qc_parse_ack_frm(struct quic_conn *qc,
2027 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002028 struct quic_enc_level *qel,
2029 unsigned int *rtt_sample,
2030 const unsigned char **pos, const unsigned char *end)
2031{
2032 struct quic_ack *ack = &frm->ack;
2033 uint64_t smallest, largest;
2034 struct eb_root *pkts;
2035 struct eb64_node *largest_node;
2036 unsigned int time_sent, pkt_flags;
2037 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2038 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002039 int ret = 0;
2040
2041 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002042
2043 if (ack->largest_ack > qel->pktns->tx.next_pn) {
2044 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002045 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002046 goto err;
2047 }
2048
2049 if (ack->first_ack_range > ack->largest_ack) {
2050 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002051 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002052 goto err;
2053 }
2054
2055 largest = ack->largest_ack;
2056 smallest = largest - ack->first_ack_range;
2057 pkts = &qel->pktns->tx.pkts;
2058 pkt_flags = 0;
2059 largest_node = NULL;
2060 time_sent = 0;
2061
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002062 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002063 largest_node = eb64_lookup(pkts, largest);
2064 if (!largest_node) {
2065 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002066 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002067 }
2068 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002069 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002070 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002071 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002072 }
2073
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002074 TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002075 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002076 do {
2077 uint64_t gap, ack_range;
2078
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002079 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2080 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002081 if (!ack->ack_range_num--)
2082 break;
2083
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002084 if (!quic_dec_int(&gap, pos, end)) {
2085 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002086 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002087 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002088
2089 if (smallest < gap + 2) {
2090 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002091 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002092 goto err;
2093 }
2094
2095 largest = smallest - gap - 2;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002096 if (!quic_dec_int(&ack_range, pos, end)) {
2097 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002098 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002099 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002100
2101 if (largest < ack_range) {
2102 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002103 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002104 goto err;
2105 }
2106
2107 /* Do not use this node anymore. */
2108 largest_node = NULL;
2109 /* Next range */
2110 smallest = largest - ack_range;
2111
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002112 TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002113 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002114 } while (1);
2115
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002116 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2117 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002118 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002119 }
2120
2121 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2122 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002123 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002124 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002125 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002126 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2127 if (quic_peer_validated_addr(qc))
2128 qc->path->loss.pto_count = 0;
2129 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002130 }
2131
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002132 ret = 1;
2133 leave:
2134 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2135 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002136
2137 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002138 free_quic_tx_pkts(qc, &newly_acked_pkts);
2139 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002140}
2141
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002142/* This function gives the detail of the SSL error. It is used only
2143 * if the debug mode and the verbose mode are activated. It dump all
2144 * the SSL error until the stack was empty.
2145 */
2146static forceinline void qc_ssl_dump_errors(struct connection *conn)
2147{
2148 if (unlikely(global.mode & MODE_DEBUG)) {
2149 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002150 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002151 unsigned long ret;
2152
Willy Tarreau325fc632022-04-11 18:47:38 +02002153 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002154 ret = ERR_get_error();
2155 if (!ret)
2156 return;
2157
2158 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002159 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002160 }
2161 }
2162}
2163
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002164int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2165 const char **str, int *len);
2166
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002167/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2168 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002169 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002170 * Return 1 if succeeded, 0 if not.
2171 */
2172static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002173 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002174 const unsigned char *data, size_t len,
2175 struct quic_rx_packet *pkt,
2176 struct quic_rx_crypto_frm *cf)
2177{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002178 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002179 struct quic_conn *qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002180 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002181
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002182 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002183 qc = ctx->qc;
2184
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002185 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2186
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002187 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002188 TRACE_ERROR("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002189 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002190 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002191 }
2192
2193 el->rx.crypto.offset += len;
2194 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002195 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002196
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002197 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002198 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002199 ssl_err = SSL_do_handshake(ctx->ssl);
2200 if (ssl_err != 1) {
2201 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2202 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002203 TRACE_PROTO("SSL handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002204 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002205 goto out;
2206 }
2207
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002208 /* TODO: Should close the connection asap */
2209 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2210 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2211 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2212 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2213 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002214 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002215 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002216 ERR_clear_error();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002217 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002218 }
2219
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002220 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002221
2222 /* Check the alpn could be negotiated */
2223 if (!qc->app_ops) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002224 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002225 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002226 goto leave;
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002227 }
2228
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002229 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002230 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002231 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2232 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2233 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002234 /* I/O callback switch */
2235 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002236 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002237 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002238 /* The connection is ready to be accepted. */
2239 quic_accept_push_qc(qc);
2240 }
2241 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002242 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002243 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002244 } else {
2245 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2246 if (ssl_err != 1) {
2247 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2248 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002249 TRACE_PROTO("SSL post handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002250 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002251 goto out;
2252 }
2253
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002254 TRACE_ERROR("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002255 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002256 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002257 }
2258
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002259 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002260 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002261
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002262 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002263 ret = 1;
2264 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002265 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002266 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002267}
2268
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002269/* Parse a STREAM frame <strm_frm>
2270 *
2271 * Return 1 on success. On error, 0 is returned. In this case, the packet
2272 * containing the frame must not be acknowledged.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002273 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002274static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2275 struct quic_stream *strm_frm,
2276 struct quic_conn *qc)
2277{
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002278 int ret;
2279
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002280 /* RFC9000 13.1. Packet Processing
2281 *
2282 * A packet MUST NOT be acknowledged until packet protection has been
2283 * successfully removed and all frames contained in the packet have
2284 * been processed. For STREAM frames, this means the data has been
2285 * enqueued in preparation to be received by the application protocol,
2286 * but it does not require that data be delivered and consumed.
2287 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002288 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2289
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002290 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2291 strm_frm->offset.key, strm_frm->fin,
2292 (char *)strm_frm->data);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002293
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002294 /* frame rejected - packet must not be acknowledeged */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002295 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2296 return !ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002297}
2298
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002299/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2300 * for <qc> QUIC connection.
2301 * This is a best effort function which never fails even if no memory could be
2302 * allocated to duplicate these frames.
2303 */
2304static void qc_dup_pkt_frms(struct quic_conn *qc,
2305 struct list *pkt_frm_list, struct list *out_frm_list)
2306{
2307 struct quic_frame *frm, *frmbak;
2308 struct list tmp = LIST_HEAD_INIT(tmp);
2309
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002310 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2311
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002312 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2313 struct quic_frame *dup_frm, *origin;
2314
2315 switch (frm->type) {
2316 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2317 {
2318 struct quic_stream *strm_frm = &frm->stream;
2319 struct eb64_node *node = NULL;
2320 struct qc_stream_desc *stream_desc;
2321
2322 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2323 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002324 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002325 continue;
2326 }
2327
2328 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2329 /* Do not resend this frame if in the "already acked range" */
2330 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002331 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002332 QUIC_EV_CONN_PRSAFRM, qc, frm);
2333 continue;
2334 }
2335 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2336 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002337 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002338 QUIC_EV_CONN_PRSAFRM, qc, frm);
2339 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002340 break;
2341 }
2342
2343 default:
2344 break;
2345 }
2346
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002347 dup_frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002348 if (!dup_frm) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002349 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002350 break;
2351 }
2352
2353 /* If <frm> is already a copy of another frame, we must take
2354 * its original frame as source for the copy.
2355 */
2356 origin = frm->origin ? frm->origin : frm;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002357 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002358 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002359 qc, NULL, &origin->pkt->pn_node.key);
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002360 *dup_frm = *origin;
2361 dup_frm->pkt = NULL;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002362 dup_frm->origin = origin;
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002363 dup_frm->flags = 0;
2364 LIST_INIT(&dup_frm->reflist);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002365 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2366 LIST_APPEND(&tmp, &dup_frm->list);
2367 }
2368
2369 LIST_SPLICE(out_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002370
2371 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002372}
2373
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002374/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002375static void qc_prep_fast_retrans(struct quic_conn *qc,
2376 struct quic_enc_level *qel,
2377 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002378{
2379 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002380 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002381 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002382 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002383
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002384 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2385
2386 BUG_ON(frms1 == frms2);
2387
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002388 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002389 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002390 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002391 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002392 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2393 node = eb64_next(node);
2394 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002395 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002396 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002397 }
2398
2399 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002400 goto leave;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002401
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002402 /* When building a packet from another one, the field which may increase the
2403 * packet size is the packet number. And the maximum increase is 4 bytes.
2404 */
2405 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2406 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2407 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002408 goto leave;
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002409 }
2410
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002411 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002412 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2413 if (frms == frms1 && frms2) {
2414 frms = frms2;
2415 goto start;
2416 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002417 leave:
2418 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002419}
2420
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002421/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002422 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002423 * Initial packets send them coalescing with others (Handshake here).
2424 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002425 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002426static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2427 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002428{
2429 struct list itmp = LIST_HEAD_INIT(itmp);
2430 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002431
2432 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2433 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2434 struct quic_enc_level *qel = iqel;
2435 struct eb_root *pkts;
2436 struct eb64_node *node;
2437 struct quic_tx_packet *pkt;
2438 struct list *tmp = &itmp;
2439
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002440 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002441 start:
2442 pkt = NULL;
2443 pkts = &qel->pktns->tx.pkts;
2444 node = eb64_first(pkts);
2445 /* Skip the empty packet (they have already been retransmitted) */
2446 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002447 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002448 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002449 break;
2450 node = eb64_next(node);
2451 }
2452
2453 if (!pkt)
2454 goto end;
2455
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002456 /* When building a packet from another one, the field which may increase the
2457 * packet size is the packet number. And the maximum increase is 4 bytes.
2458 */
2459 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2460 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2461 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2462 goto end;
2463 }
2464
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002465 qel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002466
2467 /* No risk to loop here, #packet per datagram is bounded */
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002468 requeue:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002469 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002470 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002471 if (qel == iqel) {
2472 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2473 pkt = pkt->next;
2474 tmp = &htmp;
2475 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002476 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002477 goto requeue;
2478 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002479 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002480
2481 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002482 LIST_SPLICE(ifrms, &itmp);
2483 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002484
2485 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002486}
2487
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002488static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002489{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002490 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2491
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002492 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2493 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2494 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2495 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002496 goto out;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002497
2498 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2499 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002500
2501 out:
2502 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002503}
2504
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002505/* Enqueue a STOP_SENDING frame to send into 1RTT packet number space
2506 * frame list to send.
2507 * Return 1 if succeeded, 0 if not.
2508 */
2509static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id)
2510{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002511 int ret = 0;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002512 struct quic_frame *frm;
2513 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2514 uint64_t app_error_code;
2515
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002516 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2517
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002518 /* TODO: the mux may be released, we cannot have more
2519 * information about the application error code to send
2520 * at this time.
2521 */
2522 app_error_code = H3_REQUEST_REJECTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002523 // fixme: zalloc
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002524 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002525 if (!frm) {
2526 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2527 goto out;
2528 }
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002529
2530 frm->type = QUIC_FT_STOP_SENDING;
2531 frm->stop_sending.id = id;
2532 frm->stop_sending.app_error_code = app_error_code;
2533 LIST_INIT(&frm->reflist);
2534 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002535 ret = 1;
2536 out:
2537 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2538 return ret;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002539}
2540
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002541/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2542 * as I/O handler context and <qel> as encryption level.
2543 * Returns 1 if succeeded, 0 if failed.
2544 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002545static 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 +01002546 struct quic_enc_level *qel)
2547{
2548 struct quic_frame frm;
2549 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002550 struct quic_conn *qc = ctx->qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002551 int fast_retrans = 0, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002552
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002553 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002554 /* Skip the AAD */
2555 pos = pkt->data + pkt->aad_len;
2556 end = pkt->data + pkt->len;
2557
2558 while (pos < end) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002559 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
2560 // trace already emitted by function above
2561 goto leave;
2562 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002563
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002564 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002565 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002566 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002567 break;
2568 case QUIC_FT_PING:
2569 break;
2570 case QUIC_FT_ACK:
2571 {
2572 unsigned int rtt_sample;
2573
2574 rtt_sample = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002575 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
2576 // trace already emitted by function above
2577 goto leave;
2578 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002579
2580 if (rtt_sample) {
2581 unsigned int ack_delay;
2582
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002583 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002584 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002585 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2586 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002587 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002588 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002589 break;
2590 }
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002591 case QUIC_FT_RESET_STREAM:
2592 /* TODO: handle this frame at STREAM level */
2593 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002594 case QUIC_FT_STOP_SENDING:
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002595 {
2596 struct quic_stop_sending *stop_sending = &frm.stop_sending;
2597 if (qc->mux_state == QC_MUX_READY) {
2598 if (qcc_recv_stop_sending(qc->qcc, stop_sending->id,
2599 stop_sending->app_error_code)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002600 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
2601 goto leave;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002602 }
2603 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002604 break;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002605 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002606 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002607 {
2608 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002609
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002610 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002611 /* XXX TO DO: <cfdebug> is used only for the traces. */
2612 struct quic_rx_crypto_frm cfdebug = { };
2613
2614 cfdebug.offset_node.key = frm.crypto.offset;
2615 cfdebug.len = frm.crypto.len;
2616 TRACE_PROTO("CRYPTO data discarded",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002617 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002618 break;
2619 }
2620
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002621 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2622 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2623 /* XXX TO DO: <cfdebug> is used only for the traces. */
2624 struct quic_rx_crypto_frm cfdebug = { };
2625
2626 cfdebug.offset_node.key = frm.crypto.offset;
2627 cfdebug.len = frm.crypto.len;
2628 /* Nothing to do */
2629 TRACE_PROTO("Already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002630 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002631 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002632 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2633 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002634 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002635 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002636 else {
2637 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2638 /* XXX TO DO: <cfdebug> is used only for the traces. */
2639 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002640
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002641 cfdebug.offset_node.key = frm.crypto.offset;
2642 cfdebug.len = frm.crypto.len;
2643 TRACE_PROTO("Partially already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002644 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002645 frm.crypto.len -= diff;
2646 frm.crypto.data += diff;
2647 frm.crypto.offset = qel->rx.crypto.offset;
2648 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002649 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002650
2651 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002653 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002654
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002655 cfdebug.offset_node.key = frm.crypto.offset;
2656 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002657 if (!qc_provide_cdata(qel, ctx,
2658 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002659 pkt, &cfdebug)) {
2660 // trace already emitted by function above
2661 goto leave;
2662 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002663
2664 break;
2665 }
2666
2667 /* frm.crypto.offset > qel->rx.crypto.offset */
2668 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2669 if (!cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002670 TRACE_ERROR("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002671 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002672 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002674
2675 cf->offset_node.key = frm.crypto.offset;
2676 cf->len = frm.crypto.len;
2677 cf->data = frm.crypto.data;
2678 cf->pkt = pkt;
2679 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2680 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002681 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002682 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002683 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002684 {
2685 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002686 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002687
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002688 /* The upper layer may not be allocated. */
2689 if (qc->mux_state != QC_MUX_READY) {
2690 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002691 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002692 break;
2693 }
2694 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002695 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002696 if (!qc_stop_sending_frm_enqueue(qc, stream->id))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002697 TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc);
2698 /* This packet will not be acknowledged */
2699 goto leave;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002700 }
2701 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002702
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002703 if (!qc_handle_strm_frm(pkt, stream, qc)) {
2704 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
2705 goto leave;
2706 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002707
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002708 break;
2709 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002710 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002711 if (qc->mux_state == QC_MUX_READY) {
2712 struct quic_max_data *data = &frm.max_data;
2713 qcc_recv_max_data(qc->qcc, data->max_data);
2714 }
2715 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002716 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002717 if (qc->mux_state == QC_MUX_READY) {
2718 struct quic_max_stream_data *data = &frm.max_stream_data;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002719 if (qcc_recv_max_stream_data(qc->qcc, data->id,
2720 data->max_stream_data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002721 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
2722 goto leave;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002723 }
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002724 }
2725 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002726 case QUIC_FT_MAX_STREAMS_BIDI:
2727 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002728 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002729 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002730 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2731 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002732 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002733 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2734 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002735 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002736 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2737 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002738 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002739 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002740 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002741 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002742 case QUIC_FT_RETIRE_CONNECTION_ID:
2743 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002744 break;
2745 case QUIC_FT_CONNECTION_CLOSE:
2746 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002747 /* Increment the error counters */
2748 qc_cc_err_count_inc(qc, &frm);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002749 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002750 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2751 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002752 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002753 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002754 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002755 /* RFC 9000 10.2. Immediate Close:
2756 * The closing and draining connection states exist to ensure
2757 * that connections close cleanly and that delayed or reordered
2758 * packets are properly discarded. These states SHOULD persist
2759 * for at least three times the current PTO interval...
2760 *
2761 * Rearm the idle timeout only one time when entering draining
2762 * state.
2763 */
2764 qc_idle_timer_do_rearm(qc);
2765 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002766 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002767 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002768 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002769 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002770 if (qc_is_listener(ctx->qc)) {
2771 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
2772 QUIC_EV_CONN_PRSHPKT, qc);
2773 goto leave;
2774 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002775
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002776 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002777 break;
2778 default:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002779 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
2780 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002781 }
2782 }
2783
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002784 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002785 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002786
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002787 if (fast_retrans) {
2788 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2789 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2790
2791 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2792 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002793
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002794 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2795 * has successfully parse a Handshake packet. The Initial encryption must also
2796 * be discarded.
2797 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002798 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002799 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002800 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2801 QUIC_FL_TLS_SECRETS_DCD)) {
2802 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2803 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2804 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2805 qc_set_timer(ctx->qc);
2806 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002807 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002808 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002809 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2810 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002811 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002812 }
2813
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002814 ret = 1;
2815 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002816 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002817 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002818}
2819
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002820/* Allocate Tx buffer from <qc> quic-conn if needed.
2821 *
2822 * Returns allocated buffer or NULL on error.
2823 */
2824static struct buffer *qc_txb_alloc(struct quic_conn *qc)
2825{
2826 struct buffer *buf = &qc->tx.buf;
2827 if (!b_alloc(buf))
2828 return NULL;
2829
2830 return buf;
2831}
2832
2833/* Free Tx buffer from <qc> if it is empty. */
2834static void qc_txb_release(struct quic_conn *qc)
2835{
2836 struct buffer *buf = &qc->tx.buf;
2837
2838 /* For the moment sending function is responsible to purge the buffer
2839 * entirely. It may change in the future but this requires to be able
2840 * to reuse old data.
2841 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002842 BUG_ON_HOT(buf && b_data(buf));
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002843
2844 if (!b_data(buf)) {
2845 b_free(buf);
2846 offer_buffers(NULL, 1);
2847 }
2848}
2849
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002850/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
2851 * must contains the address of the first packet stored in the payload.
2852 *
2853 * Caller is responsible that there is enough space in the buffer.
2854 */
2855static void qc_txb_store(struct buffer *buf, uint16_t length,
2856 struct quic_tx_packet *first_pkt)
2857{
2858 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
2859 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
2860
2861 write_u16(b_tail(buf), length);
2862 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
2863 b_add(buf, hdlen + length);
2864}
2865
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002866/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2867 * with <frms> as ack-eliciting frame list to send, 0 if not.
2868 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2869 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002870 * <force_ack> may be set to 1 if you want to force an ack.
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002871 */
2872static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002873 struct quic_enc_level *qel, int cc, int probe, int force_ack)
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002874{
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002875 unsigned int must_ack = force_ack ||
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002876 (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED));
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002877
2878 /* Do not build any more packet if the TX secrets are not available or
2879 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2880 * and if there is no more packets to send upon PTO expiration
2881 * and if there is no more ack-eliciting frames to send or in flight
2882 * congestion control limit is reached for prepared data
2883 */
2884 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2885 (!cc && !probe && !must_ack &&
2886 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002887 return 0;
2888 }
2889
2890 return 1;
2891}
2892
Amaury Denoyellef2476052022-08-04 16:19:57 +02002893/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2894 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
2895 *
2896 * Each datagram is prepended by a two fields header : the datagram length and
2897 * the address of the packet contained in the datagram.
2898 *
2899 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2900 * -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002901 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002902static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002903 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002904{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002905 int ret = -1;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002906 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002907 unsigned char *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002908 struct quic_tx_packet *pkt;
2909 size_t total;
Amaury Denoyellef2476052022-08-04 16:19:57 +02002910 /* Each datagram is prepended with its length followed by the address
2911 * of the first packet in the datagram.
2912 */
2913 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002914
2915 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002916
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002917 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2918 total = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002919 pos = (unsigned char *)b_tail(buf);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002920 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002921 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002922
2923 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002924 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002925 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002926 /* We do not probe if an immediate close was asked */
2927 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002928 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002929
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002930 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002931 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002932
2933 /* Leave room for the datagram header */
2934 pos += dg_headlen;
2935 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002936 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 +01002937 }
2938 else {
2939 end = pos + qc->path->mtu;
2940 }
2941
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002942 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
2943 QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002944 switch (err) {
2945 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002946 // trace already emitted by function above
2947 goto leave;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002948 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002949 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2950 * MTU, we are here because of the congestion control window. There is
2951 * no need to try to reuse this buffer.
2952 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002953 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002954 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002955 default:
2956 break;
2957 }
2958
2959 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002960 BUG_ON(!pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002961
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002962 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2963 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2964
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002965 total += pkt->len;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002966
Amaury Denoyellef2476052022-08-04 16:19:57 +02002967 /* Write datagram header. */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002968 qc_txb_store(buf, pkt->len, pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002969 }
2970
2971 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002972 ret = total;
2973 leave:
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002974 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002975 return ret;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002976}
2977
Amaury Denoyellef2476052022-08-04 16:19:57 +02002978/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2979 * <frms>. Several packets can be regrouped in a single datagram. The result is
2980 * written into <buf>.
2981 *
2982 * Each datagram is prepended by a two fields header : the datagram length and
2983 * the address of first packet in the datagram.
2984 *
2985 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2986 * -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002987 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002988static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002989 enum quic_tls_enc_level tel, struct list *tel_frms,
2990 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002991{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002992 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002993 unsigned char *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002994 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2995 /* length of datagrams */
2996 uint16_t dglen;
2997 size_t total;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002998 int ret = -1, padding;
Amaury Denoyellef2476052022-08-04 16:19:57 +02002999 /* Each datagram is prepended with its length followed by the address
3000 * of the first packet in the datagram.
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003001 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003002 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003003 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003004
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003005 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3006
Amaury Denoyellef2476052022-08-04 16:19:57 +02003007 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
3008 * caller must ensure that buf is resetted.
3009 */
3010 BUG_ON_HOT(buf->head || buf->data);
3011
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003012 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003013 qel = &qc->els[tel];
3014 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003015 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003016 padding = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003017 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003018 first_pkt = prv_pkt = NULL;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003019 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003020 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003021 enum quic_pkt_type pkt_type;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003022 struct quic_tls_ctx *tls_ctx;
3023 const struct quic_version *ver;
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003024 int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3025 (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3026 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003027
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003028 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003029 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003030 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003031 /* We do not probe if an immediate close was asked */
3032 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01003033 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003034
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003035 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003036 if (prv_pkt)
3037 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003038 /* Let's select the next encryption level */
3039 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3040 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003041 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003042 qel = &qc->els[tel];
3043 /* Build a new datagram */
3044 prv_pkt = NULL;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003045 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003046 continue;
3047 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003048 break;
3049 }
3050
3051 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003052 if (!prv_pkt) {
3053 /* Leave room for the datagram header */
3054 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003055 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02003056 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 +01003057 }
3058 else {
3059 end = pos + qc->path->mtu;
3060 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003061 }
3062
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003063 if (qc->negotiated_version) {
3064 ver = qc->negotiated_version;
3065 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3066 tls_ctx = &qc->negotiated_ictx;
3067 else
3068 tls_ctx = &qel->tls_ctx;
3069 }
3070 else {
3071 ver = qc->original_version;
3072 tls_ctx = &qel->tls_ctx;
3073 }
3074
3075 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02003076 qc, ver, dglen, pkt_type,
3077 force_ack, padding, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003078 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003079 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003080 // trace already emitted by function above
3081 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003082 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003083 /* If there was already a correct packet present, set the
3084 * current datagram as prepared into <cbuf>.
3085 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003086 if (prv_pkt)
3087 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003088 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003089 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003090 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01003091 break;
3092 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02003093
Frédéric Lécaille63556772021-12-29 17:18:21 +01003094 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003095 BUG_ON(!cur_pkt);
Frédéric Lécaille63556772021-12-29 17:18:21 +01003096
Frédéric Lécaille1809c332022-04-25 10:24:12 +02003097 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3098 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3099
Frédéric Lécaille63556772021-12-29 17:18:21 +01003100 total += cur_pkt->len;
3101 /* keep trace of the first packet in the datagram */
3102 if (!first_pkt)
3103 first_pkt = cur_pkt;
3104 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003105 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003106 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003107 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3108 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01003109 /* Let's say we have to build a new dgram */
3110 prv_pkt = NULL;
3111 dglen += cur_pkt->len;
3112 /* Client: discard the Initial encryption keys as soon as
3113 * a handshake packet could be built.
3114 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003115 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01003116 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3117 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3118 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3119 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3120 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01003121 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003122 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003123 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003124 }
3125 /* If the data for the current encryption level have all been sent,
3126 * select the next level.
3127 */
3128 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003129 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 +01003130 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3131 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3132 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3133 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003134 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3135 frms = &qc->els[tel].pktns->tx.frms;
3136 else
3137 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003138 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003139 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003140 /* If there is data for the next level, do not
3141 * consume a datagram.
3142 */
3143 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003144 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003145 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02003146
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003147 /* If we have to build a new datagram, set the current datagram as
3148 * prepared into <cbuf>.
3149 */
3150 if (!prv_pkt) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003151 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003152 first_pkt = NULL;
3153 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003154 padding = 0;
3155 }
3156 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003157 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003158 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3159 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003160 }
3161 }
3162
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003163 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003164 ret = total;
3165 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003166 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003167 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003168}
3169
Amaury Denoyellef2476052022-08-04 16:19:57 +02003170/* Send datagrams stored in <buf>.
3171 *
3172 * This function always returns 1 for success. Even if sendto() syscall failed,
3173 * buffer is drained and packets are considered as emitted. QUIC loss detection
3174 * mechanism is used as a back door way to retry sending.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003175 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003176int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003177{
3178 struct quic_conn *qc;
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003179 char skip_sendto = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003180
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003181 qc = ctx->qc;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003182 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003183 while (b_contig_data(buf, 0)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003184 unsigned char *pos;
3185 struct buffer tmpbuf = { };
3186 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3187 uint16_t dglen;
3188 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003189 unsigned int time_sent;
3190
Amaury Denoyellef2476052022-08-04 16:19:57 +02003191 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003192 dglen = read_u16(pos);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003193 BUG_ON_HOT(!dglen); /* this should not happen */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003194
3195 pos += sizeof dglen;
3196 first_pkt = read_ptr(pos);
3197 pos += sizeof first_pkt;
3198 tmpbuf.area = (char *)pos;
3199 tmpbuf.size = tmpbuf.data = dglen;
3200
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003201 /* If sendto is on error just skip the call to it for the rest
3202 * of the loop but continue to purge the buffer. Data will be
3203 * transmitted when QUIC packets are detected as lost on our
3204 * side.
3205 *
3206 * TODO use fd-monitoring to detect when send operation can be
3207 * retry. This should improve the bandwidth without relying on
3208 * retransmission timer. However, it requires a major rework on
3209 * quic-conn fd management.
3210 */
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003211 if (!skip_sendto) {
3212 if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) {
3213 skip_sendto = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003214 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003215 }
3216 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003217
Amaury Denoyellef2476052022-08-04 16:19:57 +02003218 b_del(buf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003219 qc->tx.bytes += tmpbuf.data;
3220 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003221
3222 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3223 pkt->time_sent = time_sent;
3224 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3225 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003226 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003227 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3228 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003229 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003230 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3231 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3232 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003233 qc_notify_close(qc);
3234
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003235 /* RFC 9000 10.2. Immediate Close:
3236 * The closing and draining connection states exist to ensure
3237 * that connections close cleanly and that delayed or reordered
3238 * packets are properly discarded. These states SHOULD persist
3239 * for at least three times the current PTO interval...
3240 *
3241 * Rearm the idle timeout only one time when entering closing
3242 * state.
3243 */
3244 qc_idle_timer_do_rearm(qc);
3245 if (qc->timer_task) {
3246 task_destroy(qc->timer_task);
3247 qc->timer_task = NULL;
3248 }
3249 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003250 qc->path->in_flight += pkt->in_flight_len;
3251 pkt->pktns->tx.in_flight += pkt->in_flight_len;
Frédéric Lécaille5f6c25e2022-08-24 16:23:44 +02003252 if (pkt->in_flight_len)
Frédéric Lécailleda9c4412022-08-22 18:47:51 +02003253 qc_set_timer(qc);
Frédéric Lécaille5f6c25e2022-08-24 16:23:44 +02003254 TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
3255 next_pkt = pkt->next;
3256 quic_tx_packet_refinc(pkt);
3257 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003258 }
3259 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003260
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003261 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3262
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003263 return 1;
3264}
3265
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003266/* Copy into <buf> buffer a stateless reset token depending on the
3267 * <salt> salt input. This is the cluster secret which will be derived
3268 * as HKDF input secret to generate this token.
3269 * Return 1 if succeeded, 0 if not.
3270 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003271static int quic_stateless_reset_token_cpy(struct quic_conn *qc,
3272 unsigned char *buf, size_t len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003273 const unsigned char *salt, size_t saltlen)
3274{
3275 /* Input secret */
3276 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3277 size_t keylen = strlen(global.cluster_secret);
3278 /* Info */
3279 const unsigned char label[] = "stateless token";
3280 size_t labellen = sizeof label - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003281 int ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003282
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003283 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3284
3285 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003286 key, keylen, salt, saltlen, label, labellen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003287 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3288 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003289}
3290
3291/* Initialize the stateless reset token attached to <cid> connection ID.
3292 * Returns 1 if succeeded, 0 if not.
3293 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003294static int quic_stateless_reset_token_init(struct quic_conn *qc,
3295 struct quic_connection_id *quic_cid)
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003296{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003297 int ret;
3298
3299 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3300
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003301 if (global.cluster_secret) {
3302 /* Output secret */
3303 unsigned char *token = quic_cid->stateless_reset_token;
3304 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3305 /* Salt */
3306 const unsigned char *cid = quic_cid->cid.data;
3307 size_t cidlen = quic_cid->cid.len;
3308
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003309 ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003310 }
3311 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003312 /* TODO: RAND_bytes() should be replaced */
3313 ret = RAND_bytes(quic_cid->stateless_reset_token,
3314 sizeof quic_cid->stateless_reset_token) == 1;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003315 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003316
3317 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3318 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003319}
3320
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003321/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3322 * ebtree.
3323 *
3324 * The CID is randomly generated in part with the result altered to be
3325 * associated with the current thread ID. This means this function must only
3326 * be called by the quic_conn thread.
3327 *
3328 * Returns the new CID if succeeded, NULL if not.
3329 */
3330static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3331 struct quic_conn *qc,
3332 int seq_num)
3333{
3334 struct quic_connection_id *cid;
3335
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003336 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3337
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003338 cid = pool_alloc(pool_head_quic_connection_id);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003339 if (!cid) {
3340 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
3341 goto err;
3342 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003343
3344 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003345 /* TODO: RAND_bytes() should be replaced */
3346 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) {
3347 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003348 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003349 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003350
3351 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003352 if (quic_stateless_reset_token_init(qc, cid) != 1) {
3353 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003354 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003355 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003356
3357 cid->qc = qc;
3358
3359 cid->seq_num.key = seq_num;
3360 cid->retire_prior_to = 0;
3361 /* insert the allocated CID in the quic_conn tree */
3362 eb64_insert(root, &cid->seq_num);
3363
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003364 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003365 return cid;
3366
3367 err:
3368 pool_free(pool_head_quic_connection_id, cid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003369 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003370 return NULL;
3371}
3372
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003373/* Build all the frames which must be sent just after the handshake have succeeded.
3374 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3375 * a HANDSHAKE_DONE frame.
3376 * Return 1 if succeeded, 0 if not.
3377 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003378static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003379{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003380 int ret = 0, i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003381 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003382 struct quic_frame *frm, *frmbak;
3383 struct list frm_list = LIST_HEAD_INIT(frm_list);
3384 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003385
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003386 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
3387
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003388 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003389 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003390 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003391 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003392 if (!frm) {
3393 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3394 goto leave;
3395 }
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003396
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003397 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003398 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003399 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003400 }
3401
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003402 /* Initialize <max> connection IDs minus one: there is
3403 * already one connection ID used for the current connection.
3404 */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003405 first = 1;
3406 max = qc->tx.params.active_connection_id_limit;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003407
3408 /* TODO: check limit */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003409 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003410 struct quic_connection_id *cid;
3411
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003412 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003413 if (!frm) {
3414 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003415 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003416 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003417
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003418 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003419 cid = new_quic_cid(&qc->cids, qc, i);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003420 if (!cid) {
3421 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003422 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003423 }
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003424
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003425 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003426 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003427
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003428 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003429 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003430 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003431
3432 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003433 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003434
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003435 ret = 1;
3436 leave:
3437 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
3438 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003439
3440 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003441 /* free the frames */
3442 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3443 pool_free(pool_head_quic_frame, frm);
3444
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003445 node = eb64_lookup_ge(&qc->cids, first);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003446 while (node) {
3447 struct quic_connection_id *cid;
3448
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003449 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003450 if (cid->seq_num.key >= max)
3451 break;
3452
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003453 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003454 ebmb_delete(&cid->node);
3455 eb64_delete(&cid->seq_num);
3456 pool_free(pool_head_quic_connection_id, cid);
3457 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003458 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003459}
3460
3461/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003462void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003463{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003464 struct eb64_node *n;
3465 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003466
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003467 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
3468
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003469 n = eb64_first(&arngs->root);
3470 while (n) {
3471 struct eb64_node *next;
3472
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003473 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003474 next = eb64_next(n);
3475 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003476 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003477 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003478 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003479
3480 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003481}
3482
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003483/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3484 * descending order.
3485 */
3486static inline size_t sack_gap(struct quic_arng_node *p,
3487 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003488{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003489 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490}
3491
3492
3493/* Remove the last elements of <ack_ranges> list of ack range updating its
3494 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003495 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003496 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003497static int quic_rm_last_ack_ranges(struct quic_conn *qc,
3498 struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003499{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003500 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003501 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003502
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003503 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3504
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003505 last = eb64_last(&arngs->root);
3506 while (last && arngs->enc_sz > limit) {
3507 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003508
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003509 prev = eb64_prev(last);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003510 if (!prev) {
3511 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
3512 goto out;
3513 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003514
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003515 last_node = eb64_entry(last, struct quic_arng_node, first);
3516 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003517 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3518 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3519 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3520 --arngs->sz;
3521 eb64_delete(last);
3522 pool_free(pool_head_quic_arng, last);
3523 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524 }
3525
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003526 ret = 1;
3527 out:
3528 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3529 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003530}
3531
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003532/* Set the encoded size of <arngs> QUIC ack ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003533static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003534{
3535 struct eb64_node *node, *next;
3536 struct quic_arng_node *ar, *ar_next;
3537
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003538 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3539
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003540 node = eb64_last(&arngs->root);
3541 if (!node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003542 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003543
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003544 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003545 arngs->enc_sz = quic_int_getsize(ar->last) +
3546 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3547
3548 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003549 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003550 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3551 quic_int_getsize(ar_next->last - ar_next->first.key);
3552 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003553 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003554 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003555
3556 leave:
3557 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003558}
3559
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003560/* Insert <ar> ack range into <argns> tree of ack ranges.
3561 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003562 */
3563static inline
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003564struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
3565 struct quic_arngs *arngs,
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003566 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003567{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003568 struct quic_arng_node *new_ar;
3569
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003570 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3571
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003572 new_ar = pool_alloc(pool_head_quic_arng);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003573 if (!new_ar) {
3574 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
3575 goto leave;
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003576 }
3577
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003578 new_ar->first.key = ar->first;
3579 new_ar->last = ar->last;
3580 eb64_insert(&arngs->root, &new_ar->first);
3581 arngs->sz++;
3582
3583 leave:
3584 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003585 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003586}
3587
3588/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003589 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003590 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003591 *
3592 * Descending order
3593 * ------------->
3594 * range1 range2
3595 * ..........|--------|..............|--------|
3596 * ^ ^ ^ ^
3597 * | | | |
3598 * last1 first1 last2 first2
3599 * ..........+--------+--------------+--------+......
3600 * diff1 gap12 diff2
3601 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003602 * To encode the previous list of ranges we must encode integers as follows in
3603 * descending order:
3604 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003605 * with diff1 = last1 - first1
3606 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003607 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003608 *
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003609
3610returns 0 on error
3611
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003613int quic_update_ack_ranges_list(struct quic_conn *qc,
3614 struct quic_arngs *arngs,
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003615 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003616{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003617 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003618 struct eb64_node *le;
3619 struct quic_arng_node *new_node;
3620 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003621
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003622 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3623
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003624 new = NULL;
3625 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003626 new_node = quic_insert_new_range(qc, arngs, ar);
3627 if (new_node)
3628 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003629
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003630 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003631 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003632
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003633 le = eb64_lookup_le(&arngs->root, ar->first);
3634 if (!le) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003635 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003636 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003637 goto leave;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003638
3639 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003640 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003641 else {
3642 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003643 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003644
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003645 /* Already existing range */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003646 if (le_ar->last >= ar->last) {
3647 ret = 1;
3648 }
3649 else if (le_ar->last + 1 >= ar->first) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003650 le_ar->last = ar->last;
3651 new = le;
3652 new_node = le_ar;
3653 }
3654 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003655 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003656 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003657 goto leave;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003658
3659 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003660 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003661 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003662
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003663 /* Verify that the new inserted node does not overlap the nodes
3664 * which follow it.
3665 */
3666 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003667 struct eb64_node *next;
3668 struct quic_arng_node *next_node;
3669
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003670 while ((next = eb64_next(new))) {
3671 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003672 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003673 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003674 break;
3675
3676 if (next_node->last > new_node->last)
3677 new_node->last = next_node->last;
3678 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003679 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003680 /* Decrement the size of these ranges. */
3681 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003682 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003683 }
3684
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003685 ret = 1;
3686 leave:
3687 quic_arngs_set_enc_sz(qc, arngs);
3688 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
3689 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003690}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003691/* Remove the header protection of packets at <el> encryption level.
3692 * Always succeeds.
3693 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003694static 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 +01003695{
3696 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003697 struct quic_rx_packet *pqpkt, *pkttmp;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003698 struct quic_enc_level *app_qel;
3699
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003700 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3701 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003702 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003703 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003704 TRACE_DEVEL("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003705 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003706 goto out;
3707 }
3708 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003709 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003710 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02003711 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003712 TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003713 }
3714 else {
3715 /* The AAD includes the packet number field */
3716 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3717 /* Store the packet into the tree of packets to decrypt. */
3718 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003719 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3720 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003721 TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003722 }
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003723 LIST_DELETE(&pqpkt->list);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003724 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003725 }
3726
3727 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003728 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003729}
3730
3731/* Process all the CRYPTO frame at <el> encryption level.
3732 * Return 1 if succeeded, 0 if not.
3733 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003734static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
3735 struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003736 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003737{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003738 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003739 struct eb64_node *node;
3740
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003741 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3742
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003743 node = eb64_first(&el->rx.crypto.frms);
3744 while (node) {
3745 struct quic_rx_crypto_frm *cf;
3746
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003747 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003748 if (cf->offset_node.key != el->rx.crypto.offset)
3749 break;
3750
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003751 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) {
3752 TRACE_ERROR("qc_provide_cdata() failed", QUIC_EV_CONN_TRMHP);
3753 goto leave;
3754 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003755
3756 node = eb64_next(node);
3757 quic_rx_packet_refdec(cf->pkt);
3758 eb64_delete(&cf->offset_node);
3759 pool_free(pool_head_quic_rx_crypto_frm, cf);
3760 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003761
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003762 ret = 1;
3763 leave:
3764 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3765 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003766}
3767
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003768/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003769 * 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 +02003770 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003771 * Return 1 if succeeded, 0 if not.
3772 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003773int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3774 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003775{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003776 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003777 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003778 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003779 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003780 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003781 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003782
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003783 TRACE_ENTER(QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003784 qel = cur_el;
3785 next_tel:
3786 if (!qel)
3787 goto out;
3788
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003789 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003790 while (node) {
3791 struct quic_rx_packet *pkt;
3792
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003793 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003794 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003795 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003796 if (!qc_pkt_decrypt(pkt, qel, qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003797 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003798 TRACE_ERROR("packet decryption failed -> dropped",
3799 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003800 }
3801 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003802 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003803 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003804 TRACE_ERROR("packet parsing failed -> dropped",
3805 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02003806 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003807 }
3808 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003809 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3810
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003811 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3812 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3813 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003814 qc_idle_timer_rearm(qc, 1);
3815 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003816 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003817 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003818 largest_pn_time_received = pkt->time_received;
3819 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003820 /* Update the list of ranges to acknowledge. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003821 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
3822 TRACE_ERROR("Could not update ack range list",
3823 QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003824 }
3825 }
3826 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003827 eb64_delete(&pkt->pn_node);
3828 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003829 }
3830
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003831 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3832 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003833 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003834 /* Update the largest acknowledged packet timestamps */
3835 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3836 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3837 }
3838
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003839 if (!qc_treat_rx_crypto_frms(qc, qel, ctx)) {
3840 // trace already emitted by function above
3841 goto leave;
3842 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003843
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003844 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003845 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003846 qel = next_el;
Frédéric Lécaille91a211f2022-05-24 10:54:42 +02003847 largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003848 goto next_tel;
3849 }
3850
3851 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003852 ret = 1;
3853 leave:
3854 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, ctx->qc);
3855 return ret;
3856}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003857
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003858/* Check if it's possible to remove header protection for packets related to
3859 * encryption level <qel>. If <qel> is NULL, assume it's false.
3860 *
3861 * Return true if the operation is possible else false.
3862 */
3863static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3864{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003865 int ret = 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003866 enum quic_tls_enc_level tel;
3867
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003868 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3869
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003870 if (!qel)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003871 goto cant_rm_hp;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003872
3873 tel = ssl_to_quic_enc_level(qel->level);
3874
3875 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003876 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003877 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003878 goto cant_rm_hp;
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003879 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003880
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003881 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3882 TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc);
3883 goto cant_rm_hp;
3884 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003885
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003886 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003887 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003888 qc->mux_state == QC_MUX_NULL) {
3889 TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
3890 goto cant_rm_hp;
3891 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003892
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003893 ret = 1;
3894 cant_rm_hp:
3895 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3896 return ret;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003897}
3898
Amaury Denoyellecc130472022-08-17 10:08:16 +02003899/* Try to send application frames from list <frms> on connection <qc>.
3900 *
Amaury Denoyelle70467562022-08-17 16:33:13 +02003901 * Use qc_send_app_probing wrapper when probing with old data.
Amaury Denoyellecc130472022-08-17 10:08:16 +02003902 *
3903 * Returns 1 on success. Some data might not have been sent due to congestion,
3904 * in this case they are left in <frms> input list. The caller may subscribe on
3905 * quic-conn to retry later.
3906 *
3907 * Returns 0 on critical error.
3908 * TODO review and classify more distinctly transient from definitive errors to
3909 * allow callers to properly handle it.
3910 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003911static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003912{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003913 int status = 0;
3914 struct buffer *buf;
3915
3916 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3917
3918 buf = qc_txb_alloc(qc);
3919 if (!buf) {
3920 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
3921 goto leave;
3922 }
3923
Frédéric Lécailledc077512022-07-26 09:17:19 +02003924 /* Prepare and send packets until we could not further prepare packets. */
3925 while (1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003926 int ret;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003927 /* Currently buf cannot be non-empty at this stage. Even if a
3928 * previous sendto() has failed it is emptied to simulate
3929 * packet emission and rely on QUIC lost detection to try to
3930 * emit it.
3931 */
3932 BUG_ON_HOT(b_data(buf));
3933 b_reset(buf);
3934
Amaury Denoyellef2476052022-08-04 16:19:57 +02003935 ret = qc_prep_app_pkts(qc, buf, frms);
Frédéric Lécailledc077512022-07-26 09:17:19 +02003936 if (ret == -1)
3937 goto err;
3938 else if (ret == 0)
3939 goto out;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003940
Amaury Denoyellef2476052022-08-04 16:19:57 +02003941 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécailledc077512022-07-26 09:17:19 +02003942 goto err;
3943 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003944
3945 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003946 status = 1;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003947 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003948 leave:
3949 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3950 return status;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003951
3952 err:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003953 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003954 goto leave;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003955}
3956
Amaury Denoyellecc130472022-08-17 10:08:16 +02003957/* Try to send application frames from list <frms> on connection <qc>. Use this
Amaury Denoyelle70467562022-08-17 16:33:13 +02003958 * function when probing is required.
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003959 *
3960 * Returns the result from qc_send_app_pkts function.
3961 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003962static forceinline int qc_send_app_probing(struct quic_conn *qc,
3963 struct list *frms)
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003964{
3965 int ret;
3966
3967 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3968
Amaury Denoyelle70467562022-08-17 16:33:13 +02003969 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
3970 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003971 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003972 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003973
3974 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3975 return ret;
3976}
3977
Amaury Denoyelle70467562022-08-17 16:33:13 +02003978/* Try to send application frames from list <frms> on connection <qc>. This
3979 * function is provided for MUX upper layer usage only.
Amaury Denoyellecc130472022-08-17 10:08:16 +02003980 *
3981 * Returns the result from qc_send_app_pkts function.
3982 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003983int qc_send_mux(struct quic_conn *qc, struct list *frms)
Amaury Denoyellecc130472022-08-17 10:08:16 +02003984{
3985 int ret;
3986
3987 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003988 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 +02003989
Amaury Denoyelle70467562022-08-17 16:33:13 +02003990 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
3991 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02003992 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003993 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02003994
3995 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3996 return ret;
3997}
3998
Frédéric Lécaillea9568412022-04-25 08:58:04 +02003999/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4000 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004001 * QUIC connection. <old_data> is used as boolean to send data already sent but
4002 * not already acknowledged (in flight).
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004003 * Returns 1 if succeeded, 0 if not.
4004 */
4005int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4006 enum quic_tls_enc_level tel, struct list *tel_frms,
4007 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4008{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004009 int ret, status = 0;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004010 struct buffer *buf = qc_txb_alloc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004011
4012 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4013
4014 if (!buf) {
4015 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4016 goto leave;
4017 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004018
Amaury Denoyellef2476052022-08-04 16:19:57 +02004019 /* Currently buf cannot be non-empty at this stage. Even if a previous
4020 * sendto() has failed it is emptied to simulate packet emission and
4021 * rely on QUIC lost detection to try to emit it.
4022 */
4023 BUG_ON_HOT(b_data(buf));
4024 b_reset(buf);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004025
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004026 if (old_data) {
4027 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004028 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004029 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02004030
4031 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004032 if (ret == -1)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004033 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004034 else if (ret == 0)
4035 goto skip_send;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004036
Amaury Denoyellef2476052022-08-04 16:19:57 +02004037 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004038 goto out;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004039
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004040 skip_send:
4041 status = 1;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004042 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004043 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004044 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004045 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004046 leave:
4047 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4048 return status;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004049}
4050
4051/* Retransmit up to two datagrams depending on packet number space */
4052static void qc_dgrams_retransmit(struct quic_conn *qc)
4053{
4054 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4055 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4056 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4057
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004058 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4059
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004060 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4061 struct list ifrms = LIST_HEAD_INIT(ifrms);
4062 struct list hfrms = LIST_HEAD_INIT(hfrms);
4063
4064 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004065 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4066 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004067 if (!LIST_ISEMPTY(&ifrms)) {
4068 iqel->pktns->tx.pto_probe = 1;
4069 if (!LIST_ISEMPTY(&hfrms)) {
4070 hqel->pktns->tx.pto_probe = 1;
4071 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4072 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
4073 }
4074 }
4075 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4076 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004077 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004078 if (!LIST_ISEMPTY(&hfrms)) {
4079 hqel->pktns->tx.pto_probe = 1;
4080 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
4081 QUIC_TLS_ENC_LEVEL_NONE, NULL);
4082 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004083 TRACE_STATE("no more need to probe Handshake packet number space",
4084 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004085 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4086 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004087 TRACE_STATE("no more need to probe Initial packet number space",
4088 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004089 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4090 }
4091 else {
4092 int i;
4093 struct list frms1 = LIST_HEAD_INIT(frms1);
4094 struct list frms2 = LIST_HEAD_INIT(frms2);
4095
4096 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4097 hqel->pktns->tx.pto_probe = 0;
4098 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4099 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004100 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004101 if (!LIST_ISEMPTY(&frms1)) {
4102 hqel->pktns->tx.pto_probe = 1;
4103 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4104 QUIC_TLS_ENC_LEVEL_NONE, NULL);
4105 }
4106 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004107 TRACE_STATE("no more need to probe Handshake packet number space",
4108 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004109 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4110 }
4111 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4112 aqel->pktns->tx.pto_probe = 0;
4113 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4114 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4115 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4116 if (!LIST_ISEMPTY(&frms1)) {
4117 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004118 qc_send_app_probing(qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004119 }
4120 if (!LIST_ISEMPTY(&frms2)) {
4121 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004122 qc_send_app_probing(qc, &frms2);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004123 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004124 TRACE_STATE("no more need to probe 01RTT packet number space",
4125 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004126 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4127 }
4128 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004129 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004130}
4131
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004132/* QUIC connection packet handler task (post handshake) */
4133static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4134{
4135 struct ssl_sock_ctx *ctx;
4136 struct quic_conn *qc;
4137 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004138
4139
4140 ctx = context;
4141 qc = ctx->qc;
4142 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004143
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004144 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4145 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004146
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004147 /* Retranmissions */
4148 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004149 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004150 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4151 qc_dgrams_retransmit(qc);
4152 }
4153
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004154 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004155 qc_rm_hp_pkts(qc, qel);
4156
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004157 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) {
4158 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4159 goto out;
4160 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004161
Frédéric Lécaille47756802022-03-25 09:12:16 +01004162 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004163 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4164 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01004165 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004166 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004167
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004168 /* XXX TODO: how to limit the list frames to send */
Amaury Denoyelle70467562022-08-17 16:33:13 +02004169 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
4170 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004171 goto out;
4172 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004173
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004174 out:
4175 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004176 return t;
4177}
4178
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004179/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4180static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4181{
4182 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
4183 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
4184 qel->pktns->tx.pto_probe ||
4185 !LIST_ISEMPTY(&qel->pktns->tx.frms);
4186}
4187
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004188/* QUIC connection packet handler task. */
4189struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004190{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004191 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004192 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004193 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004194 enum quic_tls_enc_level tel, next_tel;
4195 struct quic_enc_level *qel, *next_qel;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004196 struct buffer *buf = NULL;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004197 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004198
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004199 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004200 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004201 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004202 st = qc->state;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004203 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004204
4205 /* Retranmissions */
4206 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004207 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004208 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4209 qc_dgrams_retransmit(qc);
4210 }
4211
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004212 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
4213 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004214 TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached",
4215 QUIC_EV_CONN_IO_CB, qc);
4216 /* 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 +01004217 * after the anti-amplification was reached.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004218 *
4219 * TODO: this part should be removed. This was there because the
4220 * datagram parser was not executed by only one thread.
Frédéric Lécaille59507de2022-08-11 12:14:07 +02004221 */
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004222 qc_set_timer(qc);
4223 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
4224 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
4225 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004226 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004227 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004228 (!LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004229 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004230 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004231 if (st >= QUIC_HS_ST_COMPLETE &&
4232 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004233 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004234 /* There may be remaining Handshake packets to treat and acknowledge. */
4235 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4236 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4237 }
4238 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004239 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004240
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004241 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02004242 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004243
4244 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004245 /* Treat packets waiting for header packet protection decryption */
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004246 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004247 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004248
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004249 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
4250 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4251 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004252 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004253
Frédéric Lécaille47756802022-03-25 09:12:16 +01004254 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4255 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4256 goto out;
4257
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004258 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004259 !LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004260 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
4261 qel = next_qel;
4262 next_qel = NULL;
4263 goto next_level;
4264 }
4265 else {
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004266 struct quic_rx_packet *pkt, *pkttmp;
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004267 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écaillea2d8ad22022-08-23 17:45:52 +02004271 list_for_each_entry_safe(pkt, pkttmp, &aqel->rx.pqpkts, list) {
4272 LIST_DELETE(&pkt->list);
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004273 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écaillea2d8ad22022-08-23 17:45:52 +02004338 (!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écaillea2d8ad22022-08-23 17:45:52 +02004388 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);
Frédéric Lécailleea4a5cb2022-08-20 18:59:36 +02004454 qc_stream_desc_free(stream, 1);
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);
Frédéric Lécaille15773f22022-08-24 17:57:09 +02004546 if (!LIST_ISEMPTY(&lost_pkts))
4547 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécaille277c4622022-08-24 17:06:04 +02004548 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004549 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004550 goto out;
4551 }
4552
4553 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004554 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004555 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4556 qc->qcc->subs->events & SUB_RETRY_SEND) {
4557 struct qcc *qcc = qc->qcc;
4558
4559 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4560 tasklet_wakeup(qcc->subs->tasklet);
4561 qcc->subs->events &= ~SUB_RETRY_SEND;
4562 if (!qcc->subs->events)
4563 qcc->subs = NULL;
4564 }
4565 else {
4566 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4567 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4568 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004569 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4570 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004571 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004572 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4573 }
4574 }
4575 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
4576 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4577 }
4578 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
4579 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004580 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004581 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004582 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004583 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004584 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4585 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4586
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004587 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004588 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004589 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004590 iel->pktns->tx.pto_probe = 1;
4591 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004592
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004593 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4594 qc->path->loss.pto_count++;
4595
4596 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004597 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004598
4599 return task;
4600}
4601
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004602/* Parse the Retry token from buffer <token> with <end> a pointer to
4603 * one byte past the end of this buffer. This will extract the ODCID
4604 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004605 *
4606 * Returns 0 on success else non-zero.
4607 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004608static int parse_retry_token(struct quic_conn *qc,
4609 const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004610 struct quic_cid *odcid)
4611{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004612 int ret = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004613 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004614 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004615
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004616 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
4617
4618 if (!quic_dec_int(&odcid_len, &token, end)) {
4619 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
4620 goto leave;
4621 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004622
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004623 /* RFC 9000 7.2. Negotiating Connection IDs:
4624 * When an Initial packet is sent by a client that has not previously
4625 * received an Initial or Retry packet from the server, the client
4626 * populates the Destination Connection ID field with an unpredictable
4627 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4628 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004629 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
4630 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
4631 goto leave;
4632 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004633
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004634 if (end - token < odcid_len + sizeof timestamp) {
4635 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
4636 goto leave;
4637 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004638
4639 timestamp = ntohl(read_u32(token + odcid_len));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004640 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
4641 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
4642 goto leave;
4643 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004644
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004645 ret = 1;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004646 memcpy(odcid->data, token, odcid_len);
4647 odcid->len = odcid_len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004648 leave:
4649 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
4650 return !ret;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004651}
4652
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004653/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4654 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4655 * for QUIC servers (or haproxy listeners).
4656 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4657 * <token> the token found to be used for this connection with <token_len> as
4658 * length. <saddr> is the source address.
4659 * Returns the connection if succeeded, NULL if not.
4660 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004661static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004662 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02004663 const struct quic_cid *token_odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004664 struct sockaddr_storage *saddr,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004665 int server, int token, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004666{
4667 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004668 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004669 /* Initial CID. */
4670 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004671 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004672 struct listener *l = NULL;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004673 struct quic_cc_algo *cc_algo = NULL;
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004674 struct quic_tls_ctx *ictx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004675 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004676 qc = pool_zalloc(pool_head_quic_conn);
4677 if (!qc) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004678 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004679 goto err;
4680 }
4681
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004682 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4683 if (!buf_area) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004684 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004685 goto err;
4686 }
4687
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004688 qc->cids = EB_ROOT;
4689 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004690 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004691 struct proxy *prx;
4692
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004693 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004694 prx = l->bind_conf->frontend;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004695 cc_algo = l->bind_conf->quic_cc_algo;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004696
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004697 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4698 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004699 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004700 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004701 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004702 qc->odcid.len = dcid->len;
4703 qc->odcid.addrlen = dcid->addrlen;
4704 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004705
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004706 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004707 if (scid->len)
4708 memcpy(qc->dcid.data, scid->data, scid->len);
4709 qc->dcid.len = scid->len;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004710 qc->tx.buf = BUF_NULL;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004711 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004712 }
4713 /* QUIC Client (outgoing connection to servers) */
4714 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004715 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004716 if (dcid->len)
4717 memcpy(qc->dcid.data, dcid->data, dcid->len);
4718 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004719 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004720 qc->mux_state = QC_MUX_NULL;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02004721 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004722
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004723 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004724 if (!icid) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004725 TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004726 goto err;
4727 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004728
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004729 /* insert the allocated CID in the receiver datagram handler tree */
4730 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004731 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004732
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004733 /* Select our SCID which is the first CID with 0 as sequence number. */
4734 qc->scid = icid->cid;
4735
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004736 /* Packet number spaces initialization. */
4737 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4738 quic_pktns_init(&qc->pktns[i]);
4739 /* QUIC encryption level context initialization. */
4740 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004741 if (!quic_conn_enc_level_init(qc, i)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004742 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004743 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004744 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004745 /* Initialize the packet number space. */
4746 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4747 }
4748
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004749 qc->original_version = qv;
4750 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004751 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4752 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004753 /* TX part. */
4754 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004755 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4756 qc->tx.wbuf = qc->tx.rbuf = 0;
4757 qc->tx.bytes = 0;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004758 qc->tx.buf = BUF_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004759 /* RX part. */
4760 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004761 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004762 for (i = 0; i < QCS_MAX_TYPES; i++)
4763 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004764
4765 qc->nb_pkt_for_cc = 1;
4766 qc->nb_pkt_since_cc = 0;
4767
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004768 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004769 if (!quic_tls_ku_init(qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004770 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004771 goto err;
4772 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004773
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004774 /* XXX TO DO: Only one path at this time. */
4775 qc->path = &qc->paths[0];
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004776 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 +01004777
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004778 /* required to use MTLIST_IN_LIST */
4779 MT_LIST_INIT(&qc->accept_list);
4780
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004781 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004782 qc->stream_buf_count = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004783 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4784
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004785 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
4786 icid->stateless_reset_token,
4787 dcid->data, dcid->len,
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02004788 qc->scid.data, qc->scid.len, token_odcid))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004789 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004790
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004791 if (qc_conn_alloc_ssl_ctx(qc) ||
4792 !quic_conn_init_timer(qc) ||
4793 !quic_conn_init_idle_timer_task(qc))
4794 goto err;
4795
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004796 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
4797 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004798 goto err;
4799
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004800 if (!quic_tls_dec_aes_ctx_init(&ictx->rx.hp_ctx, ictx->rx.hp, ictx->rx.hp_key) ||
4801 !quic_tls_enc_aes_ctx_init(&ictx->tx.hp_ctx, ictx->tx.hp, ictx->tx.hp_key))
4802 goto err;
4803
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004804 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004805
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004806 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004807
4808 err:
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004809 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4810 if (qc)
4811 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004812 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004813 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004814 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004815}
4816
4817/* Initialize the timer task of <qc> QUIC connection.
4818 * Returns 1 if succeeded, 0 if not.
4819 */
4820static int quic_conn_init_timer(struct quic_conn *qc)
4821{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004822 int ret = 0;
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004823 /* Attach this task to the same thread ID used for the connection */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004824 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4825
Willy Tarreau87168752022-06-13 16:31:53 +02004826 qc->timer_task = task_new_on(qc->tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004827 if (!qc->timer_task) {
4828 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4829 goto leave;
4830 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004831
4832 qc->timer = TICK_ETERNITY;
4833 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004834 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004835
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004836 ret = 1;
4837 leave:
4838 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4839 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004840}
4841
Frédéric Lécaille47756802022-03-25 09:12:16 +01004842/* Rearm the idle timer for <qc> QUIC connection. */
4843static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4844{
4845 unsigned int expire;
4846
4847 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4848 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4849}
4850
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004851/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4852 * which is set to 1 when receiving a packet , and 0 when sending packet
4853 */
4854static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4855{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004856 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4857
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004858 if (read) {
4859 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4860 }
4861 else {
4862 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4863 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004864 qc_idle_timer_do_rearm(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004865
4866 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004867}
4868
4869/* The task handling the idle timeout */
4870static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4871{
4872 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004873 struct quic_counters *prx_counters = qc->prx_counters;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004874 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004875
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004876 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4877
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004878 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4879 * might free the quic-conn too early via quic_close().
4880 */
4881 qc_notify_close(qc);
4882
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004883 /* If the MUX is still alive, keep the quic-conn. The MUX is
4884 * responsible to call quic_close to release it.
4885 */
4886 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4887 if (qc->mux_state != QC_MUX_READY)
4888 quic_conn_release(qc);
4889
4890 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4891 * least clean some parts of it such as the tasklet.
4892 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004893
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004894 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
4895 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004896 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004897 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004898 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004899
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004900 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004901 return NULL;
4902}
4903
4904/* Initialize the idle timeout task for <qc>.
4905 * Returns 1 if succeeded, 0 if not.
4906 */
4907static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4908{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004909 int ret = 0;
4910
4911 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4912
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004913 qc->idle_timer_task = task_new_here();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004914 if (!qc->idle_timer_task) {
4915 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4916 goto leave;
4917 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004918
4919 qc->idle_timer_task->process = qc_idle_timer_task;
4920 qc->idle_timer_task->context = qc;
4921 qc_idle_timer_rearm(qc, 1);
4922 task_queue(qc->idle_timer_task);
4923
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004924 ret = 1;
4925 leave:
4926 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4927 return ret;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004928}
4929
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004930/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4931 * past one byte of this buffer.
4932 */
4933static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4934 struct quic_rx_packet *pkt)
4935{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004936 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004937 unsigned char dcid_len, scid_len;
4938
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004939 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
4940
4941 if (end == *buf) {
4942 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
4943 goto leave;
4944 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004945
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004946 /* Destination Connection ID Length */
4947 dcid_len = *(*buf)++;
4948 /* 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 +02004949 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
4950 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
4951 goto leave;
4952 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004953
4954 if (dcid_len) {
4955 /* Check that the length of this received DCID matches the CID lengths
4956 * of our implementation for non Initials packets only.
4957 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004958 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4959 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004960 dcid_len != QUIC_HAP_CID_LEN) {
4961 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
4962 goto leave;
4963 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004964
4965 memcpy(pkt->dcid.data, *buf, dcid_len);
4966 }
4967
4968 pkt->dcid.len = dcid_len;
4969 *buf += dcid_len;
4970
4971 /* Source Connection ID Length */
4972 scid_len = *(*buf)++;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004973 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
4974 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
4975 goto leave;
4976 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004977
4978 if (scid_len)
4979 memcpy(pkt->scid.data, *buf, scid_len);
4980 pkt->scid.len = scid_len;
4981 *buf += scid_len;
4982
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004983 ret = 1;
4984 leave:
4985 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
4986 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004987}
4988
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004989/* Insert <pkt> RX packet in its <qel> RX packets tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004990static void qc_pkt_insert(struct quic_conn *qc,
4991 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004992{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004993 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4994
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004995 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004996 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004997 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004998
4999 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005000}
5001
5002/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005003 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
5004 * byte past the end of the buffer containing this packet and <beg> the address of
5005 * the packet first byte.
5006 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
5007 * Returns 1 if succeeded, 0 if not.
5008 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005009static inline int qc_try_rm_hp(struct quic_conn *qc,
5010 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005011 unsigned char *buf, unsigned char *beg,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005012 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005013{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005014 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005015 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005016 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005017 struct quic_enc_level *qel;
5018 /* Only for traces. */
5019 struct quic_rx_packet *qpkt_trace;
5020
5021 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005022 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005023 /* The packet number is here. This is also the start minus
5024 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5025 * protection.
5026 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005027 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005028
5029 tel = quic_packet_type_enc_level(pkt->type);
5030 qel = &qc->els[tel];
5031
5032 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005033 /* Note that the following function enables us to unprotect the packet
5034 * number and its length subsequently used to decrypt the entire
5035 * packets.
5036 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005037 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02005038 qel->pktns->rx.largest_pn, pn, beg)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005039 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005040 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005041 }
5042
5043 /* The AAD includes the packet number field found at <pn>. */
5044 pkt->aad_len = pn - beg + pkt->pnl;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005045 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
5046 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005047 goto out;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005048 }
5049
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005050 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005051 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01005052 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01005053 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005054 /* If the packet number space has been discarded, this packet
5055 * will be not parsed.
5056 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005057 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005058 goto out;
5059 }
5060
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005061 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005062 pkt->pn_offset = pn - beg;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02005063 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02005064 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005065 }
5066
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005067 *el = qel;
5068 /* No reference counter incrementation here!!! */
5069 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
5070 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
5071 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
5072 b_add(&qc->rx.buf, pkt->len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005073
5074 ret = 1;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005075 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005076 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005077 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005078}
5079
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005080/* 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 +02005081 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
5082 * of this packet into <*version>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005083 */
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005084static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
5085 unsigned char **buf, const unsigned char *end,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005086 int *long_header, uint32_t *version)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005087{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005088 int ret = 0;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005089 const unsigned char byte0 = **buf;
5090
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005091 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5092
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005093 (*buf)++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005094 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005095 unsigned char type =
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005096 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005097
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005098 *long_header = 1;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005099 /* Version */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005100 if (!quic_read_uint32(version, (const unsigned char **)buf, end)) {
5101 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
5102 goto out;
5103 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005104
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005105 if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005106 pkt->type = type;
5107 }
5108 else {
5109 switch (type) {
5110 case 0:
5111 pkt->type = QUIC_PACKET_TYPE_RETRY;
5112 break;
5113 case 1:
5114 pkt->type = QUIC_PACKET_TYPE_INITIAL;
5115 break;
5116 case 2:
5117 pkt->type = QUIC_PACKET_TYPE_0RTT;
5118 break;
5119 case 3:
5120 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
5121 break;
5122 }
5123 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005124 }
5125 else {
5126 pkt->type = QUIC_PACKET_TYPE_SHORT;
5127 *long_header = 0;
5128 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005129
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005130 ret = 1;
5131 out:
5132 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005133 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005134}
5135
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005136/* Return the QUIC version (quic_version struct) with <version> as version number
5137 * if supported or NULL if not.
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005138 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005139static inline const struct quic_version *qc_supported_version(uint32_t version)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005140{
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005141 int i;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005142
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005143 for (i = 0; i < quic_versions_nb; i++)
5144 if (quic_versions[i].num == version)
5145 return &quic_versions[i];
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005146
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005147 return NULL;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005148}
5149
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005150/*
5151 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
5152 * address <addr>.
5153 * Implementation of RFC9000 6. Version Negotiation
5154 *
5155 * TODO implement a rate-limiting sending of Version Negotiation packets
5156 *
5157 * Returns 0 on success else non-zero
5158 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005159static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
5160 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005161{
5162 char buf[256];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005163 int ret = 0, i = 0, j;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005164 uint32_t version;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005165 const socklen_t addrlen = get_addr_len(addr);
5166
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005167 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005168 /*
5169 * header form
5170 * long header, fixed bit to 0 for Version Negotiation
5171 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005172 /* TODO: RAND_bytes() should be replaced? */
5173 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
5174 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
5175 goto out;
5176 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005177
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01005178 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005179 /* null version for Version Negotiation */
5180 buf[i++] = '\x00';
5181 buf[i++] = '\x00';
5182 buf[i++] = '\x00';
5183 buf[i++] = '\x00';
5184
5185 /* source connection id */
5186 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005187 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005188 i += pkt->scid.len;
5189
5190 /* destination connection id */
5191 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005192 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005193 i += pkt->dcid.len;
5194
5195 /* supported version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005196 for (j = 0; j < quic_versions_nb; j++) {
5197 version = htonl(quic_versions[j].num);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005198 memcpy(&buf[i], &version, sizeof(version));
5199 i += sizeof(version);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005200 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005201
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005202 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005203 goto out;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005204
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005205 ret = 1;
5206 out:
5207 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5208 return !ret;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005209}
5210
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005211/* Send a stateless reset packet depending on <pkt> RX packet information
5212 * from <fd> UDP socket to <dst>
5213 * Return 1 if succeeded, 0 if not.
5214 */
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005215static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005216 struct quic_rx_packet *rxpkt)
5217{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005218 int ret = 0, pktlen, rndlen;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005219 unsigned char pkt[64];
5220 const socklen_t addrlen = get_addr_len(dstaddr);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005221 struct proxy *prx;
5222 struct quic_counters *prx_counters;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005223
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005224 TRACE_ENTER(QUIC_EV_STATELESS_RST);
5225
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005226 prx = l->bind_conf->frontend;
5227 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005228 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
5229 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
5230 * Reset is difficult to distinguish from other packets if the recipient requires
5231 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
5232 * that all packets it sends are at least 22 bytes longer than the minimum
5233 * connection ID length that it requests the peer to include in its packets,
5234 * adding PADDING frames as necessary. This ensures that any Stateless Reset
5235 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
5236 * An endpoint that sends a Stateless Reset in response to a packet that is
5237 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
5238 * than the packet it responds to.
5239 */
5240
5241 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
5242 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
5243 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
5244 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005245
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005246 /* Put a header of random bytes */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005247 /* TODO: RAND_bytes() should be replaced */
5248 if (RAND_bytes(pkt, rndlen) != 1) {
5249 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
5250 goto leave;
5251 }
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005252
5253 /* Clear the most significant bit, and set the second one */
5254 *pkt = (*pkt & ~0x80) | 0x40;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005255 if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005256 rxpkt->dcid.data, rxpkt->dcid.len))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005257 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005258
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005259 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005260 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005261
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005262 ret = 1;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005263 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005264 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005265 leave:
5266 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
5267 return ret;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005268}
5269
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005270/* QUIC server only function.
5271 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005272 * This is the responsibility of the caller to check <aad> size is big enough
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005273 * to contain these data.
5274 * Return the number of bytes copied to <aad>.
5275 */
5276static int quic_generate_retry_token_aad(unsigned char *aad,
5277 uint32_t version,
5278 const struct quic_cid *cid,
5279 const struct sockaddr_storage *addr)
5280{
5281 unsigned char *p;
5282
5283 p = aad;
5284 memcpy(p, &version, sizeof version);
5285 p += sizeof version;
5286 p += quic_saddr_cpy(p, addr);
5287 memcpy(p, cid->data, cid->len);
5288 p += cid->len;
5289
5290 return p - aad;
5291}
5292
5293/* QUIC server only function.
5294 * Generate the token to be used in Retry packets. The token is written to
5295 * <buf> whith <len> as length. <odcid> is the original destination connection
5296 * ID and <dcid> is our side destination connection ID (or client source
5297 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005298 * Returns the length of the encoded token or 0 on error.
5299 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005300static int quic_generate_retry_token(unsigned char *buf, size_t len,
5301 const uint32_t version,
5302 const struct quic_cid *odcid,
5303 const struct quic_cid *dcid,
5304 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005305{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005306 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005307 unsigned char *p;
5308 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5309 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5310 size_t aadlen;
5311 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
5312 unsigned char key[QUIC_TLS_KEY_LEN];
5313 unsigned char iv[QUIC_TLS_IV_LEN];
5314 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5315 size_t seclen = strlen(global.cluster_secret);
5316 EVP_CIPHER_CTX *ctx = NULL;
5317 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5318 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005319
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005320 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5321
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005322 /* We copy the odcid into the token, prefixed by its one byte
5323 * length, the format token byte. It is followed by an AEAD TAG, and finally
5324 * the random bytes used to derive the secret to encrypt the token.
5325 */
5326 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005327 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005328
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005329 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005330 /* TODO: RAND_bytes() should be replaced */
5331 if (RAND_bytes(salt, sizeof salt) != 1) {
5332 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005333 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005334 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005335
5336 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 +02005337 salt, sizeof salt, sec, seclen)) {
5338 TRACE_ERROR("quic_tls_derive_retry_token_secret() 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 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005341
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005342 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
5343 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005344 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005345 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005346
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005347 /* Token build */
5348 p = buf;
5349 *p++ = QUIC_TOKEN_FMT_RETRY,
5350 *p++ = odcid->len;
5351 memcpy(p, odcid->data, odcid->len);
5352 p += odcid->len;
5353 write_u32(p, htonl(timestamp));
5354 p += sizeof timestamp;
5355
5356 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005357 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
5358 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005359 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005360 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005361
5362 p += QUIC_TLS_TAG_LEN;
5363 memcpy(p, salt, sizeof salt);
5364 p += sizeof salt;
5365 EVP_CIPHER_CTX_free(ctx);
5366
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005367 ret = p - buf;
5368 leave:
5369 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5370 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005371
5372 err:
5373 if (ctx)
5374 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005375 goto leave;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005376}
5377
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005378/* QUIC server only function.
5379 * Check the validity of the Retry token from <token> buffer with <tokenlen>
5380 * as length. If valid, the ODCID of <qc> QUIC connection will be put
5381 * into <odcid> connection ID. <dcid> is our side destination connection ID
5382 * of client source connection ID.
5383 * Return 1 if succeeded, 0 if not.
5384 */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005385static int quic_retry_token_check(const unsigned char *token, size_t tokenlen,
5386 const struct quic_version *qv,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005387 struct quic_cid *odcid,
5388 const struct quic_cid *dcid,
5389 struct quic_conn *qc,
5390 struct sockaddr_storage *addr)
5391{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005392 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005393 unsigned char buf[128];
5394 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5395 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5396 size_t aadlen;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005397 const unsigned char *salt;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005398 unsigned char key[QUIC_TLS_KEY_LEN];
5399 unsigned char iv[QUIC_TLS_IV_LEN];
5400 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5401 size_t seclen = strlen(global.cluster_secret);
5402 EVP_CIPHER_CTX *ctx = NULL;
5403 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5404
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005405 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5406
5407 if (sizeof buf < tokenlen) {
5408 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
5409 goto err;
5410 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005411
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005412 aadlen = quic_generate_retry_token_aad(aad, qv->num, dcid, addr);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005413 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
5414 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 +02005415 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005416 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
5417 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005418 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005419
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005420 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
5421 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005422 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005423 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005424
5425 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
5426 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 +02005427 ctx, aead, key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005428 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005429 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005430 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005431
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005432 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
5433 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005434 goto err;
5435 }
5436
5437 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005438
5439 ret = 1;
5440 leave:
5441 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5442 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005443
5444 err:
5445 if (ctx)
5446 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005447 goto leave;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005448}
5449
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005450/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
5451 * the Initial <pkt> packet.
5452 *
5453 * Returns 0 on success else non-zero.
5454 */
5455static int send_retry(int fd, struct sockaddr_storage *addr,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005456 struct quic_rx_packet *pkt, const struct quic_version *qv)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005457{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005458 int ret = 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005459 unsigned char buf[128];
5460 int i = 0, token_len;
5461 const socklen_t addrlen = get_addr_len(addr);
5462 struct quic_cid scid;
5463
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005464 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5465
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005466 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
5467 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
5468 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005469 /* version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005470 buf[i++] = *((unsigned char *)&qv->num + 3);
5471 buf[i++] = *((unsigned char *)&qv->num + 2);
5472 buf[i++] = *((unsigned char *)&qv->num + 1);
5473 buf[i++] = *(unsigned char *)&qv->num;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005474
5475 /* Use the SCID from <pkt> for Retry DCID. */
5476 buf[i++] = pkt->scid.len;
5477 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
5478 i += pkt->scid.len;
5479
5480 /* Generate a new CID to be used as SCID for the Retry packet. */
5481 scid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005482 /* TODO: RAND_bytes() should be replaced */
5483 if (RAND_bytes(scid.data, scid.len) != 1) {
5484 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
5485 goto out;
5486 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005487
5488 buf[i++] = scid.len;
5489 memcpy(&buf[i], scid.data, scid.len);
5490 i += scid.len;
5491
5492 /* token */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005493 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005494 &pkt->dcid, &pkt->scid, addr))) {
5495 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
5496 goto out;
5497 }
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01005498
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005499 i += token_len;
5500
5501 /* token integrity tag */
5502 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
5503 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005504 pkt->dcid.len, buf, i, qv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005505 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5506 goto out;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005507 }
5508
5509 i += QUIC_TLS_TAG_LEN;
5510
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005511 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
5512 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5513 goto out;
5514 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005515
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005516 ret = 1;
5517 out:
5518 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5519 return !ret;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005520}
5521
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005522/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5523 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5524 * concatenated to the <pkt> DCID field.
5525 *
5526 * Returns the instance or NULL if not found.
5527 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005528static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005529 struct listener *l,
5530 struct sockaddr_storage *saddr)
5531{
5532 struct quic_conn *qc = NULL;
5533 struct ebmb_node *node;
5534 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005535 /* set if the quic_conn is found in the second DCID tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005536
5537 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005538
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005539 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5540 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5541 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5542 /* DCIDs of first packets coming from multiple clients may have
5543 * the same values. Let's distinguish them by concatenating the
5544 * socket addresses.
5545 */
5546 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005547 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005548 pkt->dcid.len + pkt->dcid.addrlen);
5549 if (node) {
5550 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5551 goto end;
5552 }
5553 }
5554
5555 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5556 * also for INITIAL/0-RTT non-first packets with the final DCID in
5557 * used.
5558 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005559 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005560 if (!node)
5561 goto end;
5562
5563 id = ebmb_entry(node, struct quic_connection_id, node);
5564 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005565
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005566 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5567 * If already done, this is a noop.
5568 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005569 if (qc)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005570 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005571
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005572 end:
5573 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005574 return qc;
5575}
5576
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005577/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5578 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5579 * parameters of this session.
5580 * This is the responsibility of the caller to check the validity of all the
5581 * pointers passed as parameter to this function.
5582 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5583 * CO_ER_SSL_NO_MEM.
5584 */
5585static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5586 unsigned char *params, size_t params_len)
5587{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005588 int retry, ret = -1;
5589
5590 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005591
5592 retry = 1;
5593 retry:
5594 *ssl = SSL_new(ssl_ctx);
5595 if (!*ssl) {
5596 if (!retry--)
5597 goto err;
5598
5599 pool_gc(NULL);
5600 goto retry;
5601 }
5602
5603 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005604 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005605 SSL_free(*ssl);
5606 *ssl = NULL;
5607 if (!retry--)
5608 goto err;
5609
5610 pool_gc(NULL);
5611 goto retry;
5612 }
5613
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005614 ret = 0;
5615 leave:
5616 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5617 return ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005618
5619 err:
5620 qc->conn->err_code = CO_ER_SSL_NO_MEM;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005621 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005622}
5623
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005624/* Finalize <qc> QUIC connection:
5625 * - initialize the Initial QUIC TLS context for negotiated version,
5626 * - derive the secrets for this context,
5627 * - encode the transport parameters to be sent,
5628 * - set them into the TLS stack,
5629 * - initialize ->max_ack_delay and max_idle_timeout,
5630 *
5631 * MUST be called after having received the remote transport parameters.
5632 * Return 1 if succeeded, 0 if not.
5633 */
5634int qc_conn_finalize(struct quic_conn *qc, int server)
5635{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005636 int ret = 0;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005637 struct quic_transport_params *tx_tp = &qc->tx.params;
5638 struct quic_transport_params *rx_tp = &qc->rx.params;
5639 const struct quic_version *ver;
5640
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005641 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5642
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005643 if (tx_tp->version_information.negotiated_version &&
5644 tx_tp->version_information.negotiated_version != qc->original_version) {
5645 qc->negotiated_version =
5646 qc->tx.params.version_information.negotiated_version;
5647 if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
5648 qc->odcid.data, qc->odcid.len, !server))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005649 goto out;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005650
5651 ver = qc->negotiated_version;
5652 }
5653 else {
5654 ver = qc->original_version;
5655 }
5656
5657 qc->enc_params_len =
5658 quic_transport_params_encode(qc->enc_params,
5659 qc->enc_params + sizeof qc->enc_params,
5660 &qc->rx.params, ver, 1);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005661 if (!qc->enc_params_len) {
5662 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_TXPKT);
5663 goto out;
5664 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005665
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005666 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
5667 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_TXPKT);
5668 goto out;
5669 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005670
5671 if (tx_tp->max_ack_delay)
5672 qc->max_ack_delay = tx_tp->max_ack_delay;
5673
5674 if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout)
5675 qc->max_idle_timeout =
5676 QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5677 else
5678 qc->max_idle_timeout =
5679 QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5680
5681 TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp);
5682
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005683 ret = 1;
5684 out:
5685 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5686 return ret;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005687}
5688
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005689/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5690 * used to process <qc> received packets. The allocated context is stored in
5691 * <qc.xprt_ctx>.
5692 *
5693 * Returns 0 on success else non-zero.
5694 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005695static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005696{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005697 int ret = 0;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005698 struct bind_conf *bc = qc->li->bind_conf;
5699 struct ssl_sock_ctx *ctx = NULL;
5700
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005701 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5702
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005703 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005704 if (!ctx) {
5705 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005706 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005707 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005708
5709 ctx->wait_event.tasklet = tasklet_new();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005710 if (!ctx->wait_event.tasklet) {
5711 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005712 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005713 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005714
5715 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5716 ctx->wait_event.tasklet->context = ctx;
5717 ctx->wait_event.events = 0;
5718 ctx->subs = NULL;
5719 ctx->xprt_ctx = NULL;
5720 ctx->qc = qc;
5721
5722 /* Set tasklet tid based on the SCID selected by us for this
5723 * connection. The upper layer will also be binded on the same thread.
5724 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005725 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005726
5727 if (qc_is_listener(qc)) {
5728 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5729 qc->enc_params, qc->enc_params_len) == -1) {
5730 goto err;
5731 }
5732
5733 /* Enabling 0-RTT */
5734 if (bc->ssl_conf.early_data)
5735 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5736
5737 SSL_set_accept_state(ctx->ssl);
5738 }
5739
5740 ctx->xprt = xprt_get(XPRT_QUIC);
5741
5742 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005743 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005744
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005745 ret = 1;
5746 leave:
5747 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5748 return !ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005749
5750 err:
5751 if (ctx && ctx->wait_event.tasklet)
5752 tasklet_free(ctx->wait_event.tasklet);
5753 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005754 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005755}
5756
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005757/* Check that all the bytes between <buf> included and <end> address
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005758 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005759 * check that there is at least one byte between <buf> end <end>.
5760 * Return 1 if this all the bytes are null, 0 if not.
5761 */
5762static inline int quic_padding_check(const unsigned char *buf,
5763 const unsigned char *end)
5764{
5765 while (buf < end && !*buf)
5766 buf++;
5767
5768 return buf == end;
5769}
5770
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005771/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5772 * end of this buffer past one byte and populate <pkt> RX packet structure
5773 * with the information collected from the packet.
5774 * This function sets ->len <pkt> field value to the end of the packet past one
5775 * byte to enable the caller to run this function again to continue to parse
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005776 * the remaining QUIC packets carried by the datagram.
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005777 * Note that this function always sets this ->len value. If a paquet could
5778 * not be correctly found, ->len value will be set to the remaining number
5779 * bytes in the datagram to entirely consume this latter.
5780 */
5781static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5782 struct quic_rx_packet *pkt, int first_pkt,
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02005783 struct quic_dgram *dgram, struct list **tasklist_head)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005784{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005785 unsigned char *beg, *payload;
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02005786 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005787 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005788 struct proxy *prx;
5789 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005790 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005791 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005792 size_t b_cspace;
5793 struct quic_enc_level *qel;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005794 uint32_t version;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005795 const struct quic_version *qv = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005796
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005797 TRACE_ENTER(QUIC_EV_CONN_LPKT);
5798
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005799 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005800 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005801 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005802 qel = NULL;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005803 l = dgram->owner;
5804 prx = l->bind_conf->frontend;
5805 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005806 /* This ist only to please to traces and distinguish the
5807 * packet with parsed packet number from others.
5808 */
5809 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005810 if (end <= buf) {
5811 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5812 goto drop;
5813 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005814
5815 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005816 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005817 if (!first_pkt && quic_padding_check(buf, end)) {
5818 /* Some browsers may pad the remaining datagram space with null bytes.
5819 * That is what we called add padding out of QUIC packets. Such
5820 * datagrams must be considered as valid. But we can only consume
5821 * the remaining space.
5822 */
5823 pkt->len = end - buf;
5824 goto drop_no_conn;
5825 }
5826
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005827 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005828 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005829 }
5830
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005831 /* Header form */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005832 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005833 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5834 goto drop;
5835 }
5836
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005837 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005838 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005839 struct quic_cid odcid;
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02005840 const struct quic_cid *token_odcid = NULL; // ODCID received from client token
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005841
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005842 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005843 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005844 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005845 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005846 }
5847
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005848 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5849 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005850 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005851 }
5852 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5853 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005854 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02005855 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
Frédéric Lécaillee7df68a2022-08-05 09:34:44 +02005856 goto drop;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005857 }
5858
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005859 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5860 * they must have the same DCID.
5861 */
5862 if (!first_pkt &&
5863 (pkt->dcid.len != dgram->dcid_len ||
5864 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5865 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005866 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005867 }
5868
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005869 /* Retry of Version Negotiation packets are only sent by servers */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005870 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005871 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005872 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005873 }
5874
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005875 /* RFC9000 6. Version Negotiation */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005876 qv = qc_supported_version(version);
5877 if (!qv) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005878 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005879 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005880 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005881 goto err;
5882 }
5883
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005884 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005885 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005886 }
5887
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005888 /* For Initial packets, and for servers (QUIC clients connections),
5889 * there is no Initial connection IDs storage.
5890 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005891 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005892 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005893
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005894 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5895 end - buf < token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005896 TRACE_PROTO("Packet dropped",
5897 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005898 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005899 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005900
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005901 /* TODO Retry should be automatically activated if
5902 * suspect network usage is detected.
5903 */
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005904 if (global.cluster_secret && !token_len) {
5905 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
5906 TRACE_PROTO("Initial without token, sending retry",
5907 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
5908 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
5909 TRACE_PROTO("Error during Retry generation",
5910 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005911 goto err;
5912 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005913
5914 HA_ATOMIC_INC(&prx_counters->retry_sent);
5915 goto err;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005916 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005917 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005918 else if (!global.cluster_secret && token_len) {
5919 /* Impossible case: a token was received without configured
5920 * cluster secret.
5921 */
5922 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
5923 NULL, NULL, NULL, qv);
5924 goto drop;
5925 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005926
5927 pkt->token = buf;
5928 pkt->token_len = token_len;
5929 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005930 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005931 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005932 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005933 TRACE_PROTO("Packet dropped",
5934 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005935 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005936 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005937 }
5938
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005939 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5940 end - buf < len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005941 TRACE_PROTO("Packet dropped",
5942 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005943 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005944 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005945
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005946 payload = buf;
5947 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005948 if (drop_no_conn)
5949 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005950
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005951 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005952 if (global.cluster_secret && pkt->token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005953 if (*pkt->token == QUIC_TOKEN_FMT_RETRY) {
5954 const struct quic_version *ver = qc ? qc->original_version : qv;
5955 if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid,
5956 &pkt->scid, qc, &dgram->saddr)) {
5957 HA_ATOMIC_INC(&prx_counters->retry_error);
5958 TRACE_PROTO("Wrong retry token",
5959 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5960 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
5961 * with an INVALID_TOKEN error.
5962 */
5963 goto drop;
5964 }
5965
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005966 token_odcid = &odcid;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005967 HA_ATOMIC_INC(&prx_counters->retry_validated);
5968 }
5969 else {
5970 /* TODO: New token check */
5971 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5972 goto drop;
5973 }
5974 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005975 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005976 int ipv4;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005977
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005978 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005979 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005980 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005981 }
5982
Willy Tarreau787e92a2022-05-20 16:06:01 +02005983 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 +02005984 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005985 TRACE_PROTO("Initial without token, sending retry",
5986 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005987 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005988 TRACE_ERROR("Error during Retry generation",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005989 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005990 goto err;
5991 }
5992
5993 HA_ATOMIC_INC(&prx_counters->retry_sent);
5994 goto err;
5995 }
5996
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005997 /* RFC 9000 7.2. Negotiating Connection IDs:
5998 * When an Initial packet is sent by a client that has not previously
5999 * received an Initial or Retry packet from the server, the client
6000 * populates the Destination Connection ID field with an unpredictable
6001 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6002 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01006003 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006004 TRACE_PROTO("dropped packet",
6005 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006006 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01006007 }
6008
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006009 pkt->saddr = dgram->saddr;
6010 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006011
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02006012 qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, token_odcid,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02006013 &pkt->saddr, 1, !!pkt->token_len, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02006014 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006015 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006016
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006017 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02006018 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02006019 ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
6020 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006021 }
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02006022
6023 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006024 }
6025 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006026 TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006027 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006028 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006029 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006030 }
6031
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006032 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006033 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006034
6035 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6036 * they must have the same DCID.
6037 */
6038 if (!first_pkt &&
6039 (pkt->dcid.len != dgram->dcid_len ||
6040 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
6041 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006042 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006043 }
6044
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006045 buf += QUIC_HAP_CID_LEN;
6046
6047 /* A short packet is the last one of a UDP datagram. */
6048 payload = buf;
6049 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006050
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006051 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006052 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006053 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006054 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006055 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006056 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006057 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006058 }
6059
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006060 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006061 }
6062
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006063 if (qc->flags & QUIC_FL_CONN_CLOSING) {
6064 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
6065 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
6066 qc->nb_pkt_for_cc++;
6067 qc->nb_pkt_since_cc = 0;
6068 }
6069 /* Skip the entire datagram */
6070 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006071 TRACE_STATE("Closing state connection",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006072 QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006073 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006074 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006075
6076 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6077 * they must have the same DCID.
6078 *
6079 * This check must be done after the final update to pkt.len to
6080 * properly drop the packet on failure.
6081 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006082 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006083 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006084 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006085 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006086 /* Reset the anti-amplification bit. It will be set again
6087 * when sending the next packet if reached again.
6088 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006089 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
6090 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006091 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006092 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006093
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006094 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006095
Amaury Denoyelle72d86502022-07-13 15:08:23 +02006096 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006097 TRACE_PROTO("Connection error",
6098 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006099 goto out;
6100 }
6101
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006102 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006103 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006104 b_cspace = b_contig_space(&qc->rx.buf);
6105 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006106 /* Do not consume buf if space not at the end. */
6107 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006108 TRACE_PROTO("Packet dropped",
6109 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006110 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6111 goto drop_no_conn;
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006112 }
6113
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006114 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006115 if (b_cspace) {
6116 b_putchr(&qc->rx.buf, 0x00);
6117 b_cspace--;
6118 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006119 b_add(&qc->rx.buf, b_cspace);
6120 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006121 TRACE_PROTO("Too big packet",
6122 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006123 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6124 goto drop_no_conn;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006125 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006126 }
6127
Frédéric Lécaille99897d12022-08-08 10:28:07 +02006128 if (!qc_try_rm_hp(qc, pkt, payload, beg, &qel)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006129 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006130 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02006131 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006132
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006133 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006134 if (pkt->aad_len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006135 qc_pkt_insert(qc, pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006136 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02006137 /* Wake up the connection packet handler task from here only if all
6138 * the contexts have been initialized, especially the mux context
6139 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
6140 * will start it if these contexts for the connection are not already
6141 * initialized.
6142 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006143 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006144 if (conn_ctx)
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02006145 *tasklist_head = tasklet_wakeup_after(*tasklist_head, conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02006146
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006147 drop_no_conn:
6148 if (drop_no_conn)
6149 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006150 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006151
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006152 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006153
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006154 drop:
6155 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006156 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006157 /* Wakeup the I/O handler callback if the PTO timer must be armed.
6158 * This cannot be done by this thread.
6159 */
6160 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006161 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006162 if (conn_ctx && conn_ctx->wait_event.tasklet)
6163 tasklet_wakeup(conn_ctx->wait_event.tasklet);
6164 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01006165 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006166 if (!pkt->len)
6167 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006168 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006169}
6170
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006171/* This function builds into <buf> buffer a QUIC long packet header.
6172 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006173 */
6174static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006175 int type, size_t pn_len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006176 struct quic_conn *qc, const struct quic_version *ver)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006177{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006178 int ret = 0;
6179
6180 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6181
6182 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
6183 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6184 goto leave;
6185 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006186
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006187 type = quic_pkt_type(type, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006188 /* #0 byte flags */
6189 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
6190 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
6191 /* Version */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006192 quic_write_uint32(buf, end, ver->num);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006193 *(*buf)++ = qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006194 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006195 if (qc->dcid.len) {
6196 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6197 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006198 }
6199 /* Source connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006200 *(*buf)++ = qc->scid.len;
6201 if (qc->scid.len) {
6202 memcpy(*buf, qc->scid.data, qc->scid.len);
6203 *buf += qc->scid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006204 }
6205
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006206 ret = 1;
6207 leave:
6208 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6209 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006210}
6211
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006212/* This function builds into <buf> buffer a QUIC short packet header.
6213 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006214 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006215static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006216 size_t pn_len, struct quic_conn *qc,
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006217 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006218{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006219 int ret = 0;
6220
6221 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6222
6223 if (end - *buf < 1 + qc->dcid.len) {
6224 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6225 goto leave;
6226 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006227
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006228 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01006229 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
6230 ((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 +01006231 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006232 if (qc->dcid.len) {
6233 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6234 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006235 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006236
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006237 ret = 1;
6238 leave:
6239 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6240 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006241}
6242
6243/* Apply QUIC header protection to the packet with <buf> as first byte address,
6244 * <pn> as address of the Packet number field, <pnlen> being this field length
6245 * with <aead> as AEAD cipher and <key> as secret key.
6246 * Returns 1 if succeeded or 0 if failed.
6247 */
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006248static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf,
6249 unsigned char *pn, size_t pnlen,
6250 struct quic_tls_ctx *tls_ctx)
6251
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006252{
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006253 int i, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006254 /* We need an IV of at least 5 bytes: one byte for bytes #0
6255 * and at most 4 bytes for the packet number
6256 */
6257 unsigned char mask[5] = {0};
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006258 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006259
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006260 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6261
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006262 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
6263 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006264 goto out;
6265 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006266
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006267 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
6268 for (i = 0; i < pnlen; i++)
6269 pn[i] ^= mask[i + 1];
6270
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006271 ret = 1;
6272 out:
6273 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006274 return ret;
6275}
6276
6277/* Reduce the encoded size of <ack_frm> ACK frame removing the last
6278 * ACK ranges if needed to a value below <limit> in bytes.
6279 * Return 1 if succeeded, 0 if not.
6280 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006281static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
6282 struct quic_frame *ack_frm, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006283{
6284 size_t room, ack_delay_sz;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006285 int ret = 0;
6286
6287 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006288
6289 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
6290 /* A frame is made of 1 byte for the frame type. */
6291 room = limit - ack_delay_sz - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006292 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
6293 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006294
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006295 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
6296 leave:
6297 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6298 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006299}
6300
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006301/* Prepare into <outlist> as most as possible ack-eliciting frame from their
6302 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
6303 * with <room> as available room, and <*len> the packet Length field initialized
6304 * with the number of bytes already present in this buffer which must be taken
6305 * into an account for the Length packet field value. <headlen> is the number of
6306 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006307 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006308 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006309 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006310 * 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 +01006311 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006312static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006313 size_t room, size_t *len, size_t headlen,
6314 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006315 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006316{
Frédéric Lécailleea604992020-12-24 13:01:37 +01006317 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006318 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006319
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006320 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
6321
Frédéric Lécailleea604992020-12-24 13:01:37 +01006322 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006323 if (*len > room)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006324 goto leave;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006325
Frédéric Lécailleea604992020-12-24 13:01:37 +01006326 /* If we are not probing we must take into an account the congestion
6327 * control window.
6328 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006329 if (!qel->pktns->tx.pto_probe) {
6330 size_t remain = quic_path_prep_data(qc->path);
6331
6332 if (headlen > remain)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006333 goto leave;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006334
6335 room = QUIC_MIN(room, remain - headlen);
6336 }
6337
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006338 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006339 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006340
6341 /* NOTE: switch/case block inside a loop, a successful status must be
6342 * returned by this function only if at least one frame could be built
6343 * in the switch/case block.
6344 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006345 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006346 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006347 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006348
Frédéric Lécailleea604992020-12-24 13:01:37 +01006349 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006350 break;
6351
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006352 switch (cf->type) {
6353 case QUIC_FT_CRYPTO:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006354 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006355 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006356 /* Compute the length of this CRYPTO frame header */
6357 hlen = 1 + quic_int_getsize(cf->crypto.offset);
6358 /* Compute the data length of this CRyPTO frame. */
6359 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006360 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006361 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006362 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006363 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006364
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006365 /* CRYPTO frame length. */
6366 flen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006367 TRACE_DEVEL(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006368 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006369 /* Add the CRYPTO data length and its encoded length to the packet
6370 * length and the length of this length.
6371 */
6372 *len += flen;
6373 room -= flen;
6374 if (dlen == cf->crypto.len) {
6375 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006376 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006377 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006378 }
6379 else {
6380 struct quic_frame *new_cf;
6381
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006382 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006383 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006384 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006385 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006386 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006387
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006388 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006389 new_cf->type = QUIC_FT_CRYPTO;
6390 new_cf->crypto.len = dlen;
6391 new_cf->crypto.offset = cf->crypto.offset;
6392 new_cf->crypto.qel = qel;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006393 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6394 if (cf->origin) {
6395 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6396 /* This <cf> frame was duplicated */
6397 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6398 new_cf->origin = cf->origin;
6399 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006400 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006401 /* Consume <dlen> bytes of the current frame. */
6402 cf->crypto.len -= dlen;
6403 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006404 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006405 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006406
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006407 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006408 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
6409 struct eb64_node *node = NULL;
6410 struct qc_stream_desc *stream_desc = NULL;
6411 struct quic_stream *strm = &cf->stream;
6412
6413 /* As this frame has been already lost, ensure the stream is always
6414 * available or the range of this frame is not consumed before
6415 * resending it.
6416 */
6417 node = eb64_lookup(&qc->streams_by_id, strm->id);
6418 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006419 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006420 LIST_DELETE(&cf->list);
6421 pool_free(pool_head_quic_frame, cf);
6422 continue;
6423 }
6424
6425 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
6426 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006427 TRACE_DEVEL("ignored frame frame in already acked range",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006428 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006429 LIST_DELETE(&cf->list);
6430 pool_free(pool_head_quic_frame, cf);
6431 continue;
6432 }
6433 else if (strm->offset.key < stream_desc->ack_offset) {
6434 strm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006435 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006436 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006437 }
6438 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006439 /* Note that these frames are accepted in short packets only without
6440 * "Length" packet field. Here, <*len> is used only to compute the
6441 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01006442 *
6443 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006444 * excepting the variable ones. Note that +1 is for the type of this frame.
6445 */
6446 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006447 ((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 +02006448 /* Compute the data length of this STREAM frame. */
6449 avail_room = room - hlen - *len;
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006450 if ((ssize_t)avail_room <= 0)
6451 continue;
6452
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006453 TRACE_DEVEL(" New STREAM frame build (room, len)",
Frédéric Lécailleadc76412022-08-08 16:09:46 +02006454 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006455 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
6456 dlen = max_available_room(avail_room, &dlen_sz);
6457 if (dlen > cf->stream.len) {
6458 dlen = cf->stream.len;
6459 }
6460 dlen_sz = quic_int_getsize(dlen);
6461 flen = hlen + dlen_sz + dlen;
6462 }
6463 else {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02006464 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006465 flen = hlen + dlen;
6466 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006467 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006468 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006469 TRACE_DEVEL(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006470 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006471 /* Add the STREAM data length and its encoded length to the packet
6472 * length and the length of this length.
6473 */
6474 *len += flen;
6475 room -= flen;
6476 if (dlen == cf->stream.len) {
6477 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006478 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006479 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006480
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006481 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006482 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006483 qcc_streams_sent_done(cf->stream.stream->ctx,
6484 cf->stream.len,
6485 cf->stream.offset.key);
6486 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006487 }
6488 else {
6489 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006490 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006491
6492 new_cf = pool_zalloc(pool_head_quic_frame);
6493 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006494 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006495 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006496 }
6497
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006498 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006499 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006500 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006501 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006502 new_cf->stream.id = cf->stream.id;
6503 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
6504 new_cf->stream.offset = cf->stream.offset;
6505 new_cf->stream.len = dlen;
6506 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
6507 /* FIN bit reset */
6508 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
6509 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006510 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6511 if (cf->origin) {
6512 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6513 /* This <cf> frame was duplicated */
6514 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6515 new_cf->origin = cf->origin;
6516 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006517 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006518 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
6519 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006520 cf_buf = b_make(b_orig(cf->stream.buf),
6521 b_size(cf->stream.buf),
6522 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006523 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006524 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006525 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006526
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006527 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006528 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006529 qcc_streams_sent_done(new_cf->stream.stream->ctx,
6530 new_cf->stream.len,
6531 new_cf->stream.offset.key);
6532 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006533 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006534
6535 /* TODO the MUX is notified about the frame sending via
6536 * previous qcc_streams_sent_done call. However, the
6537 * sending can fail later, for example if the sendto
6538 * system call returns an error. As the MUX has been
6539 * notified, the transport layer is responsible to
6540 * bufferize and resent the announced data later.
6541 */
6542
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006543 break;
6544
6545 default:
6546 flen = qc_frm_len(cf);
6547 BUG_ON(!flen);
6548 if (flen > room)
6549 continue;
6550
6551 *len += flen;
6552 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006553 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006554 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006555 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006556 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006557
6558 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01006559 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006560 }
6561
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006562 leave:
6563 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécailleea604992020-12-24 13:01:37 +01006564 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006565}
6566
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006567/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
6568 * is used as return parameter and should be zero'ed by the caller.
6569 */
6570static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
6571 struct quic_frame *out)
6572{
6573 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
6574 *
6575 * A CONNECTION_CLOSE frame should be sent in several packets with
6576 * different encryption levels depending on the client context. This is
6577 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
6578 * more details on how to implement it.
6579 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006580 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
6581
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006582
6583 if (qc->err.app) {
6584 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
6585 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
6586 /* RFC 9000 10.2.3. Immediate Close during the Handshake
6587 *
6588 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
6589 * packet could expose application state or be used to alter application
6590 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
6591 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
6592 * Handshake packets. Otherwise, information about the application
6593 * state might be revealed. Endpoints MUST clear the value of the
6594 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
6595 * converting to a CONNECTION_CLOSE of type 0x1c.
6596 */
6597 out->type = QUIC_FT_CONNECTION_CLOSE;
6598 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
6599 out->connection_close.reason_phrase_len = 0;
6600 }
6601 else {
6602 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
6603 out->connection_close.error_code = qc->err.code;
6604 }
6605 }
6606 else {
6607 out->type = QUIC_FT_CONNECTION_CLOSE;
6608 out->connection_close.error_code = qc->err.code;
6609 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006610 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
6611
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006612}
6613
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006614/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006615 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
6616 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006617 * filling the buffer with as much frames as possible from <frms> list of
6618 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006619 * 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 +02006620 * 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 +02006621 * having returned from this function.
6622 * 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 +01006623 * number field in this packet. <pn_len> will also have the packet number
6624 * length as value.
6625 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006626 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006627 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006628static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
6629 size_t dglen, struct quic_tx_packet *pkt,
6630 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006631 int force_ack, int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006632 struct quic_enc_level *qel, struct quic_conn *qc,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006633 const struct quic_version *ver, struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006634{
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006635 unsigned char *beg, *payload;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006636 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006637 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
6638 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006639 struct quic_frame cc_frm = { };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006640 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006641 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006642 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006643 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006644 struct quic_frame *cf;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006645 int must_ack, ret = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006646 int nb_aepkts_since_last_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006647
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006648 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6649
Frédéric Lécailleea604992020-12-24 13:01:37 +01006650 /* Length field value with CRYPTO frames if present. */
6651 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006652 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006653 /* When not probing, and no immediate close is required, reduce the size of this
6654 * buffer to respect the congestion controller window.
6655 * 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 +01006656 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006657 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006658 size_t path_room;
6659
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006660 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006661 if (end - beg > path_room)
6662 end = beg + path_room;
6663 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006664
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006665 /* Ensure there is enough room for the TLS encryption tag and a zero token
6666 * length field if any.
6667 */
6668 if (end - pos < QUIC_TLS_TAG_LEN +
6669 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
6670 goto no_room;
6671
6672 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006673 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006674 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006675 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006676 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006677 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006678 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006679 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006680 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006681 goto no_room;
6682
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006683 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006684 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006685 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006686 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006687 /* Build an ACK frame if required. */
6688 ack_frm_len = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006689 nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
6690 must_ack = !qel->pktns->tx.pto_probe &&
6691 (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
6692 (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK)));
6693 if (force_ack || must_ack) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006694 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006695 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006696 ack_frm.tx_ack.arngs = arngs;
6697 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6698 qel->pktns->tx.ack_delay =
6699 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6700 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6701 }
6702 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006703 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6704 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6705 * that from here, we do not know if we will have to send a PING frame.
6706 * This will be decided after having computed the ack-eliciting frames
6707 * to be added to this packet.
6708 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006709 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 +01006710 if (!ack_frm_len)
6711 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006712 }
6713
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006714 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006715 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006716 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006717 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006718 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006719
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006720 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006721 /* Initialize the length of the frames built below to <len>.
6722 * If any frame could be successfully built by qc_build_frms(),
6723 * we will have len_frms > len.
6724 */
6725 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006726 if (!qc_build_frms(&frm_list, frms,
6727 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006728 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006729 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006730 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6731 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006732 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006733 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006734
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006735 /* Length (of the remaining data). Must not fail because, the buffer size
6736 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6737 * for the encryption tag. It must be taken into an account for the length
6738 * of this packet.
6739 */
6740 if (len_frms)
6741 len = len_frms + QUIC_TLS_TAG_LEN;
6742 else
6743 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006744 /* CONNECTION_CLOSE frame */
6745 if (cc) {
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006746 qc_build_cc_frm(qc, qel, &cc_frm);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006747 len += qc_frm_len(&cc_frm);
6748 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006749 add_ping_frm = 0;
6750 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006751 len_sz = quic_int_getsize(len);
6752 /* Add this packet size to <dglen> */
6753 dglen += head_len + len_sz + len;
6754 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6755 /* This is a maximum padding size */
6756 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6757 /* The length field value is of this packet is <len> + <padding_len>
6758 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006759 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006760 * sizes from <padding_len>.
6761 */
6762 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6763 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006764 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006765 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006766 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006767 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006768 add_ping_frm = 1;
6769 len += 1;
6770 }
6771 /* 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 +01006772 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006773 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6774 }
6775
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006776 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6777 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006778
6779 /* Packet number field address. */
6780 *buf_pn = pos;
6781
6782 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006783 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6784 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006785
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006786 /* payload building (ack-eliciting or not frames) */
6787 payload = pos;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006788 if (ack_frm_len) {
6789 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6790 goto no_room;
6791
6792 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006793 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006794 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006795
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006796 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006797 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillea8a60432022-08-23 11:42:48 +02006798 struct quic_frame *tmp_cf;
6799 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02006800 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006801 ssize_t room = end - pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006802 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006803 qc, NULL, NULL, &room);
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006804 /* TODO: this should not have happened except if we
6805 * are limited by the congestion control.
6806 * Note that <cf> was added from <frm_list> to <frms> list by
6807 * qc_build_frms().
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006808 */
6809 LIST_DELETE(&cf->list);
6810 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006811 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006812 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006813
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006814 quic_tx_packet_refinc(pkt);
6815 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006816 }
6817 }
6818
6819 /* Build a PING frame if needed. */
6820 if (add_ping_frm) {
6821 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006822 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006823 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006824 }
6825
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006826 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006827 if (cc) {
6828 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6829 goto no_room;
6830
6831 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6832 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006833
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006834 /* Build a PADDING frame if needed. */
6835 if (padding_len) {
6836 frm.type = QUIC_FT_PADDING;
6837 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006838 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006839 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006840 }
6841
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006842 if (pos == payload) {
6843 /* No payload was built because of congestion control */
6844 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
6845 goto no_room;
6846 }
6847
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006848 /* If this packet is ack-eliciting and we are probing let's
6849 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006850 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006851 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6852 qel->pktns->tx.pto_probe)
6853 qel->pktns->tx.pto_probe--;
6854
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006855 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006856 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006857
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006858 ret = 1;
6859 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
6860 leave:
6861 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6862 return ret;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006863
6864 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006865 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006866 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006867 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
6868 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006869}
6870
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006871static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006872{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006873 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006874 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006875 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006876 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006877 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006878 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006879 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006880 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006881 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006882 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006883}
6884
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006885/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006886 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6887 * of prebuilt frames.
6888 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006889 * Return -2 if the packet could not be allocated or encrypted for any reason,
6890 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006891 * XXX NOTE XXX
6892 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6893 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6894 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006895 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006896static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6897 const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006898 struct quic_enc_level *qel,
6899 struct quic_tls_ctx *tls_ctx, struct list *frms,
6900 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006901 size_t dglen, int pkt_type, int force_ack,
6902 int padding, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006903{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006904 struct quic_tx_packet *ret_pkt = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006905 /* The pointer to the packet number field. */
6906 unsigned char *buf_pn;
6907 unsigned char *beg, *end, *payload;
6908 int64_t pn;
6909 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006910 struct quic_tx_packet *pkt;
6911
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006912 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006913 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006914 pkt = pool_alloc(pool_head_quic_tx_packet);
6915 if (!pkt) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006916 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006917 *err = -2;
6918 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006919 }
6920
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006921 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006922 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006923 pn_len = 0;
6924 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006925
6926 pn = qel->pktns->tx.next_pn + 1;
6927 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 +02006928 force_ack, padding, cc, probe, qel, qc, ver, frms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006929 // trace already emitted by function above
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006930 *err = -1;
6931 goto err;
6932 }
6933
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006934 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006935 payload = buf_pn + pn_len;
6936 payload_len = end - payload;
6937 aad_len = payload - beg;
6938
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006939 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006940 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006941 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006942 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006943 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006944
6945 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006946 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006947 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006948 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006949 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006950 goto err;
6951 }
6952
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006953 /* Consume a packet number */
6954 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006955 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006956 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006957 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006958 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
6959 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006960 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6961 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006962 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006963 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006964 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006965 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006966 pkt->in_flight_len = pkt->len;
6967 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006968 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01006969 /* Always reset this flags */
6970 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006971 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
6972 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
6973 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
6974 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006975
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006976 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006977
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006978 ret_pkt = pkt;
6979 leave:
6980 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt);
6981 return ret_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006982
6983 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006984 /* TODO: what about the frames which have been built
6985 * for this packet.
6986 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006987 free_quic_tx_packet(qc, pkt);
6988 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006989}
6990
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006991
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006992/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6993 * event subscriber <es> is not allowed to change from a previous call as long
6994 * as at least one event is still subscribed. The <event_type> must only be a
6995 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
6996 */
6997static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6998{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006999 struct quic_conn *qc = conn->handle.qc;
7000 struct qcc *qcc = qc->qcc;
7001
7002 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007003
7004 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
7005 BUG_ON(qcc->subs && qcc->subs != es);
7006
7007 es->events |= event_type;
7008 qcc->subs = es;
7009
7010 if (event_type & SUB_RETRY_RECV)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007011 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007012
7013 if (event_type & SUB_RETRY_SEND)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007014 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7015
7016 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007017
7018 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007019}
7020
7021/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
7022 * The <es> pointer is not allowed to differ from the one passed to the
7023 * subscribe() call. It always returns zero.
7024 */
7025static int quic_conn_unsubscribe(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 int ret;
7028 struct quic_conn *qc = conn->handle.qc;
7029 struct qcc *qcc = qc->qcc;
7030
7031 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
7032
7033 if (event_type & SUB_RETRY_RECV)
7034 TRACE_DEVEL("unsubscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
7035 if (event_type & SUB_RETRY_SEND)
7036 TRACE_DEVEL("unsubscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7037
7038 ret = conn_unsubscribe(conn, xprt_ctx, event_type, es);
7039
7040 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
7041
7042 return ret;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007043}
7044
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007045/* Store in <xprt_ctx> the context attached to <conn>.
7046 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007047 */
7048static int qc_conn_init(struct connection *conn, void **xprt_ctx)
7049{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01007050 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007051
7052 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
7053
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007054 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007055 if (*xprt_ctx)
7056 goto out;
7057
Willy Tarreau784b8682022-04-11 14:18:10 +02007058 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007059
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007060 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01007061 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007062
7063 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007064}
7065
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007066/* Start the QUIC transport layer */
7067static int qc_xprt_start(struct connection *conn, void *ctx)
7068{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007069 int ret = 0;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007070 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02007071 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007072
Willy Tarreau784b8682022-04-11 14:18:10 +02007073 qc = conn->handle.qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007074 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007075 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
7076 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02007077 /* prepare a CONNECTION_CLOSE frame */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02007078 quic_set_connection_close(qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007079 goto out;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007080 }
7081
7082 /* mux-quic can now be considered ready. */
7083 qc->mux_state = QC_MUX_READY;
7084
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007085 tasklet_wakeup(qctx->wait_event.tasklet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007086
7087 ret = 1;
7088 out:
7089 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
7090 return ret;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007091}
7092
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007093static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
7094{
Willy Tarreau784b8682022-04-11 14:18:10 +02007095 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007096 return NULL;
7097
Willy Tarreau784b8682022-04-11 14:18:10 +02007098 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007099}
7100
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007101/* transport-layer operations for QUIC connections. */
7102static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02007103 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007104 .subscribe = quic_conn_subscribe,
7105 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007106 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007107 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007108 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
7109 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01007110 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007111 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007112 .name = "QUIC",
7113};
7114
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007115static void __quic_conn_init(void)
7116{
7117 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
7118 xprt_register(XPRT_QUIC, &ssl_quic);
7119}
Willy Tarreau79367f92022-04-25 19:18:24 +02007120INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007121
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007122static void __quic_conn_deinit(void)
7123{
7124 BIO_meth_free(ha_quic_meth);
7125}
Willy Tarreau79367f92022-04-25 19:18:24 +02007126REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007127
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01007128/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
7129 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05007130 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007131 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007132struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007133{
7134 unsigned char *pos;
7135 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007136 struct quic_dghdlr *dghdlr = ctx;
7137 struct quic_dgram *dgram;
7138 int first_pkt = 1;
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007139 struct list *tasklist_head = NULL;
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007140 int max_dgrams = global.tune.maxpollevents;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007141
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007142 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7143
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007144 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007145 pos = dgram->buf;
7146 end = pos + dgram->len;
7147 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007148 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007149
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007150 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007151 if (!pkt) {
7152 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
7153 goto leave;
7154 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007155
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007156 LIST_INIT(&pkt->qc_rx_pkt_list);
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02007157 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007158 quic_rx_packet_refinc(pkt);
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007159 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram, &tasklist_head);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007160 first_pkt = 0;
7161 pos += pkt->len;
7162 quic_rx_packet_refdec(pkt);
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007163
7164 /* Free rejected packets */
7165 if (!pkt->refcnt) {
7166 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
7167 pool_free(pool_head_quic_rx_packet, pkt);
7168 }
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007169 } while (pos < end);
7170
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007171 /* Increasing the received bytes counter by the UDP datagram length
7172 * if this datagram could be associated to a connection.
7173 */
7174 if (dgram->qc)
7175 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01007176
7177 /* Mark this datagram as consumed */
7178 HA_ATOMIC_STORE(&dgram->buf, NULL);
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007179
7180 if (--max_dgrams <= 0)
7181 goto stop_here;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007182 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007183
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007184 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
7185
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007186 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007187
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007188 stop_here:
7189 /* too much work done at once, come back here later */
7190 if (!MT_LIST_ISEMPTY(&dghdlr->dgrams))
7191 tasklet_wakeup((struct tasklet *)t);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007192 leave:
7193 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007194 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007195}
7196
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05007197/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007198 * Returns 1 if succeeded, 0 if not.
7199 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02007200int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
7201 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007202{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007203 int ret = 0, long_header;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007204 size_t minlen, skip;
7205
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007206 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
7207
7208 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
7209 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007210 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007211 }
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007212
7213 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02007214 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
7215 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007216 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Willy Tarreauaf5138f2022-08-05 10:09:32 +02007217 if (end - buf < minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007218 goto err;
7219
7220 buf += skip;
7221 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
7222 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
7223 goto err;
7224
7225 *dcid = buf;
7226
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007227 ret = 1;
7228 leave:
7229 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
7230 return ret;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007231
7232 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007233 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
7234 goto leave;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007235}
7236
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007237/* Notify the MUX layer if alive about an imminent close of <qc>. */
7238void qc_notify_close(struct quic_conn *qc)
7239{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007240 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
7241
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007242 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007243 goto leave;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007244
7245 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007246 /* wake up the MUX */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007247 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
7248 TRACE_STATE("connection closure notidfied to mux",
7249 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007250 qc->conn->mux->wake(qc->conn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007251 }
7252 else
7253 TRACE_STATE("connection closure not notidfied to mux",
7254 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
7255 leave:
7256 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007257}
7258
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007259/*
7260 * Local variables:
7261 * c-indent-level: 8
7262 * c-basic-offset: 8
7263 * End:
7264 */