blob: ffa2cba1facf540c522c0bb368e007cd165d7cec [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écaille1ba25c22022-08-18 17:21:19 +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 /* XXX TODO: what must be done for such a frame */
1484 TRACE_DEVEL("remove frame reference for unsent frame",
1485 QUIC_EV_CONN_PRSAFRM, qc, f);
1486 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001487 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001488
1489 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001490}
1491
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001492/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001493void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001494{
1495 uint64_t pn;
1496 struct quic_frame *origin, *f, *tmp;
1497
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001498 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1499
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001500 /* Identify this frame: a frame copy or one of its copies */
1501 origin = frm->origin ? frm->origin : frm;
1502 /* Ensure the source of the copies is flagged as acked, <frm> being
1503 * possibly a copy of <origin>
1504 */
1505 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1506 /* Mark all the copy of <origin> as acknowledged. We must
1507 * not release the packets (releasing the frames) at this time as
1508 * they are possibly also to be acknowledged alongside the
1509 * the current one.
1510 */
1511 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001512 if (f->pkt) {
1513 pn = f->pkt->pn_node.key;
1514 TRACE_DEVEL("mark frame as acked from packet",
1515 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1516 }
1517 else {
1518 /* XXX TODO: what must be done for such a frame */
Frédéric Lécaillea846a172022-08-19 12:08:13 +02001519 TRACE_DEVEL("mark frame as acked for unsent frame",
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001520 QUIC_EV_CONN_PRSAFRM, qc, f);
1521 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001522 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1523 f->origin = NULL;
1524 LIST_DELETE(&f->ref);
1525 }
1526 LIST_DELETE(&frm->list);
1527 pn = frm->pkt->pn_node.key;
1528 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001529 TRACE_DEVEL("freeing frame from packet",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001530 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1531 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001532
1533 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001534}
1535
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001536/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
1537 * and all STREAM data are acknowledged. The MUX is responsible to have set
1538 * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
1539 *
1540 * TODO this should also be called on lost packet detection
1541 */
1542static void qc_check_close_on_released_mux(struct quic_conn *qc)
1543{
1544 struct ssl_sock_ctx *ctx = qc->xprt_ctx;
1545
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001546 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
1547
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001548 if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
1549 /* Reuse errcode which should have been previously set by the MUX on release. */
1550 quic_set_connection_close(qc, qc->err);
1551 tasklet_wakeup(ctx->wait_event.tasklet);
1552 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001553
1554 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001555}
1556
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001557/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001558 *
1559 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001560 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001561static int quic_stream_try_to_consume(struct quic_conn *qc,
1562 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001563{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001564 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001565 struct eb64_node *frm_node;
1566
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001567 TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
1568
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001569 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001570 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001571 while (frm_node) {
1572 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001573 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001574 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001575
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001576 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001577 offset = strm->offset.key;
1578 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001579
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001580 if (offset > stream->ack_offset)
1581 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001582
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001583 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001584 /* cf. next comment : frame may be freed at this stage. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001585 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001586 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001587 ret = 1;
1588 }
1589
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001590 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1591 * has been freed. with the stream frames tree. Nothing to do
1592 * anymore in here.
1593 */
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001594 if (!stream) {
1595 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001596 ret = 1;
1597 goto leave;
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001598 }
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001599
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001600 frm_node = eb64_next(frm_node);
1601 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001602
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001603 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001604 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001605 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001606
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001607 leave:
1608 TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001609 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001610}
1611
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001612/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001613static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1614 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001615{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001616 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001617
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001618 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc, frm);
1619
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001620 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001621 switch (frm->type) {
1622 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1623 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001624 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001625 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001626 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001627 const size_t offset = strm_frm->offset.key;
1628 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001629
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001630 /* do not use strm_frm->stream as the qc_stream_desc instance
1631 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001632 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001633 *
1634 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001635 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001636 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001637 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1638 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001639 TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001640 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001641 /* early return */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001642 goto leave;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001643 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001644 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001645
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001646 TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001647 if (offset <= stream->ack_offset) {
1648 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001649 stream_acked = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001650 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001651 qc, strm_frm, stream);
1652 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001653
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001654 if (!stream) {
1655 /* no need to continue if stream freed. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001656 TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001657 qc_release_frm(qc, frm);
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02001658 qc_check_close_on_released_mux(qc);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001659 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001660 }
1661
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001662 TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
Frédéric Lécailleda342552022-04-25 10:28:49 +02001663 qc, strm_frm, stream);
1664 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001665 }
1666 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001667 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001668 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001669
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001670 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001671 }
1672 break;
1673 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001674 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001675 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001676
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001677 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001678 struct qcc *qcc = qc->qcc;
1679
1680 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1681 tasklet_wakeup(qcc->subs->tasklet);
1682 qcc->subs->events &= ~SUB_RETRY_SEND;
1683 if (!qcc->subs->events)
1684 qcc->subs = NULL;
1685 }
1686 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001687 leave:
1688 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001689}
1690
1691/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1692 * deallocating them, and their TX frames.
1693 * Returns the last node reached to be used for the next range.
1694 * May be NULL if <largest> node could not be found.
1695 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001696static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1697 struct eb_root *pkts,
1698 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001699 struct list *newly_acked_pkts,
1700 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001701 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001702{
1703 struct eb64_node *node;
1704 struct quic_tx_packet *pkt;
1705
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001706 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1707
Frédéric Lécaille843399f2022-07-22 16:27:44 +02001708 node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001709 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001710 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001711
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001712 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001713 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001714 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001715 TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001716 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001717 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001718 node = eb64_prev(node);
1719 eb64_delete(&pkt->pn_node);
1720 }
1721
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001722 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001723 return node;
1724}
1725
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001726/* Remove all frames from <pkt_frm_list> and reinsert them in the
1727 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001728 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001729static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001730 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001731 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001732{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001733 struct quic_frame *frm, *frmbak;
1734 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001735 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001736 uint64_t pn = pkt->pn_node.key;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001737
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001738 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1739
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001740 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001741 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001742 LIST_DELETE(&frm->list);
Frédéric Lécaille1ba25c22022-08-18 17:21:19 +02001743 quic_tx_packet_refdec(pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001744 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001745 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001746 /* Remove any reference to this frame */
1747 qc_frm_unref(qc, frm);
1748 switch (frm->type) {
1749 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1750 {
1751 struct quic_stream *strm_frm = &frm->stream;
1752 struct eb64_node *node = NULL;
1753 struct qc_stream_desc *stream_desc;
1754
1755 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1756 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001757 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
1758 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001759 qc, frm, &pn);
1760 pool_free(pool_head_quic_frame, frm);
1761 continue;
1762 }
1763
1764 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1765 /* Do not resend this frame if in the "already acked range" */
1766 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001767 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001768 QUIC_EV_CONN_PRSAFRM, qc, frm);
1769 continue;
1770 }
1771 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1772 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001773 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaille96367152022-04-25 09:40:19 +02001774 QUIC_EV_CONN_PRSAFRM, qc, frm);
1775 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001776 break;
1777 }
1778
1779 default:
1780 break;
1781 }
1782
1783 /* Do not resend probing packet with old data */
1784 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001785 TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1786 qc, frm, &pn);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001787 if (frm->origin)
1788 LIST_DELETE(&frm->ref);
1789 pool_free(pool_head_quic_frame, frm);
1790 continue;
1791 }
1792
1793 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001794 TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1795 TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001796 qc, frm, &pn);
1797 pool_free(pool_head_quic_frame, frm);
1798 }
1799 else {
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001800 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1801 /* Mark this STREAM frame as lost. A look up their stream descriptor
1802 * will be performed to check the stream is not consumed or released.
1803 */
Amaury Denoyellef372e742022-08-16 14:41:57 +02001804 frm->flags |= QUIC_FL_TX_FRAME_LOST;
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001805 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001806 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001807 TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001808 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001809 }
1810
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001811 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001812
1813 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001814}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001815
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001816/* Free <pkt> TX packet and its attached frames.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001817 * This is the responsibility of the caller to remove this packet of
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001818 * any data structure it was possibly attached to.
1819 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001820static inline void free_quic_tx_packet(struct quic_conn *qc,
1821 struct quic_tx_packet *pkt)
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001822{
1823 struct quic_frame *frm, *frmbak;
1824
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001825 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1826
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001827 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001828 goto leave;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001829
1830 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1831 LIST_DELETE(&frm->list);
1832 pool_free(pool_head_quic_frame, frm);
1833 }
1834 pool_free(pool_head_quic_tx_packet, pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001835
1836 leave:
1837 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001838}
1839
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001840/* Free the TX packets of <pkts> list */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001841static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001842{
1843 struct quic_tx_packet *pkt, *tmp;
1844
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001845 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
1846
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001847 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001848 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001849 eb64_delete(&pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001850 free_quic_tx_packet(qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001851 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001852
1853 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001854}
1855
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001856/* Remove already sent ranges of acknowledged packet numbers from
1857 * <pktns> packet number space tree below <largest_acked_pn> possibly
1858 * updating the range which contains <largest_acked_pn>.
1859 * Never fails.
1860 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001861static void qc_treat_ack_of_ack(struct quic_conn *qc,
1862 struct quic_pktns *pktns,
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001863 int64_t largest_acked_pn)
1864{
1865 struct eb64_node *ar, *next_ar;
1866 struct quic_arngs *arngs = &pktns->rx.arngs;
1867
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001868 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1869
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001870 ar = eb64_first(&arngs->root);
1871 while (ar) {
1872 struct quic_arng_node *ar_node;
1873
1874 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001875 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001876
1877 if ((int64_t)ar_node->first.key > largest_acked_pn) {
1878 TRACE_DEVEL("first.key > largest", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001879 break;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001880 }
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001881
1882 if (largest_acked_pn < ar_node->last) {
1883 eb64_delete(ar);
1884 ar_node->first.key = largest_acked_pn + 1;
1885 eb64_insert(&arngs->root, ar);
1886 break;
1887 }
1888
1889 eb64_delete(ar);
1890 pool_free(pool_head_quic_arng, ar_node);
1891 arngs->sz--;
1892 ar = next_ar;
1893 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001894
1895 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001896}
1897
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001898/* Send a packet ack event nofication for each newly acked packet of
1899 * <newly_acked_pkts> list and free them.
1900 * Always succeeds.
1901 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001902static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001903 struct list *newly_acked_pkts)
1904{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001905 struct quic_tx_packet *pkt, *tmp;
1906 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1907
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001908 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1909
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001910 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1911 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001912 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001913 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001914 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001915 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001916 /* If this packet contained an ACK frame, proceed to the
1917 * acknowledging of range of acks from the largest acknowledged
1918 * packet number which was sent in an ACK frame by this packet.
1919 */
1920 if (pkt->largest_acked_pn != -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001921 qc_treat_ack_of_ack(qc, pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001922 ev.ack.acked = pkt->in_flight_len;
1923 ev.ack.time_sent = pkt->time_sent;
1924 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001925 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001926 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001927 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001928 }
1929
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001930 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1931
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001932}
1933
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001934/* Release all the frames attached to <pktns> packet number space */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001935static inline void qc_release_pktns_frms(struct quic_conn *qc,
1936 struct quic_pktns *pktns)
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001937{
1938 struct quic_frame *frm, *frmbak;
1939
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001940 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
1941
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001942 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1943 LIST_DELETE(&frm->list);
1944 pool_free(pool_head_quic_frame, frm);
1945 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001946
1947 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001948}
1949
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001950/* Handle <pkts> list of lost packets detected at <now_us> handling
1951 * their TX frames.
1952 * Send a packet loss event to the congestion controller if
1953 * in flight packet have been lost.
1954 * Also frees the packet in <pkts> list.
1955 * Never fails.
1956 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001957static inline void qc_release_lost_pkts(struct quic_conn *qc,
1958 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001959 struct list *pkts,
1960 uint64_t now_us)
1961{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001962 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001963
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001964 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1965
Frédéric Lécaille277c4622022-08-24 17:06:04 +02001966 if (LIST_ISEMPTY(pkts))
1967 goto leave;
1968
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001969 oldest_lost = newest_lost = NULL;
1970 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001971 struct list tmp = LIST_HEAD_INIT(tmp);
1972
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001973 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001974 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001975 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001976 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001977 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001978 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001979 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001980 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001981 if (!oldest_lost) {
1982 oldest_lost = newest_lost = pkt;
1983 }
1984 else {
1985 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001986 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001987 newest_lost = pkt;
1988 }
1989 }
1990
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001991 if (newest_lost) {
1992 /* Sent a congestion event to the controller */
Benoit DOLEZ69e3f052022-06-08 09:28:56 +02001993 struct quic_cc_event ev = { };
1994
1995 ev.type = QUIC_CC_EVT_LOSS;
1996 ev.loss.time_sent = newest_lost->time_sent;
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001997
1998 quic_cc_event(&qc->path->cc, &ev);
1999 }
2000
2001 /* If an RTT have been already sampled, <rtt_min> has been set.
2002 * We must check if we are experiencing a persistent congestion.
2003 * If this is the case, the congestion controller must re-enter
2004 * slow start state.
2005 */
2006 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
2007 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
2008
2009 if (quic_loss_persistent_congestion(&qc->path->loss, period,
2010 now_ms, qc->max_ack_delay))
2011 qc->path->cc.algo->slow_start(&qc->path->cc);
2012 }
2013
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002014 quic_tx_packet_refdec(oldest_lost);
2015 if (newest_lost != oldest_lost)
2016 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002017
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002018 leave:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002019 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002020}
2021
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002022/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
2023 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002024 * 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 +01002025 * acked ack-eliciting packet.
2026 * Return 1, if succeeded, 0 if not.
2027 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002028static inline int qc_parse_ack_frm(struct quic_conn *qc,
2029 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002030 struct quic_enc_level *qel,
2031 unsigned int *rtt_sample,
2032 const unsigned char **pos, const unsigned char *end)
2033{
2034 struct quic_ack *ack = &frm->ack;
2035 uint64_t smallest, largest;
2036 struct eb_root *pkts;
2037 struct eb64_node *largest_node;
2038 unsigned int time_sent, pkt_flags;
2039 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
2040 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002041 int ret = 0;
2042
2043 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002044
2045 if (ack->largest_ack > qel->pktns->tx.next_pn) {
2046 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002047 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002048 goto err;
2049 }
2050
2051 if (ack->first_ack_range > ack->largest_ack) {
2052 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002053 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002054 goto err;
2055 }
2056
2057 largest = ack->largest_ack;
2058 smallest = largest - ack->first_ack_range;
2059 pkts = &qel->pktns->tx.pkts;
2060 pkt_flags = 0;
2061 largest_node = NULL;
2062 time_sent = 0;
2063
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002064 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002065 largest_node = eb64_lookup(pkts, largest);
2066 if (!largest_node) {
2067 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002068 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002069 }
2070 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002071 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002072 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002073 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002074 }
2075
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002076 TRACE_PROTO("rcvd ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002077 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002078 do {
2079 uint64_t gap, ack_range;
2080
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002081 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2082 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002083 if (!ack->ack_range_num--)
2084 break;
2085
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002086 if (!quic_dec_int(&gap, pos, end)) {
2087 TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002088 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002089 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002090
2091 if (smallest < gap + 2) {
2092 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002093 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002094 goto err;
2095 }
2096
2097 largest = smallest - gap - 2;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002098 if (!quic_dec_int(&ack_range, pos, end)) {
2099 TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002100 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002101 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002102
2103 if (largest < ack_range) {
2104 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002105 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002106 goto err;
2107 }
2108
2109 /* Do not use this node anymore. */
2110 largest_node = NULL;
2111 /* Next range */
2112 smallest = largest - ack_range;
2113
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002114 TRACE_PROTO("rcvd next ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002115 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002116 } while (1);
2117
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002118 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2119 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002120 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002121 }
2122
2123 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2124 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002125 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaille277c4622022-08-24 17:06:04 +02002126 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002127 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002128 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2129 if (quic_peer_validated_addr(qc))
2130 qc->path->loss.pto_count = 0;
2131 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002132 }
2133
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002134 ret = 1;
2135 leave:
2136 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
2137 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002138
2139 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002140 free_quic_tx_pkts(qc, &newly_acked_pkts);
2141 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002142}
2143
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002144/* This function gives the detail of the SSL error. It is used only
2145 * if the debug mode and the verbose mode are activated. It dump all
2146 * the SSL error until the stack was empty.
2147 */
2148static forceinline void qc_ssl_dump_errors(struct connection *conn)
2149{
2150 if (unlikely(global.mode & MODE_DEBUG)) {
2151 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002152 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002153 unsigned long ret;
2154
Willy Tarreau325fc632022-04-11 18:47:38 +02002155 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002156 ret = ERR_get_error();
2157 if (!ret)
2158 return;
2159
2160 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002161 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002162 }
2163 }
2164}
2165
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002166int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2167 const char **str, int *len);
2168
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002169/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2170 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002171 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002172 * Return 1 if succeeded, 0 if not.
2173 */
2174static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002175 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002176 const unsigned char *data, size_t len,
2177 struct quic_rx_packet *pkt,
2178 struct quic_rx_crypto_frm *cf)
2179{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002180 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002181 struct quic_conn *qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002182 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002183
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002184 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002185 qc = ctx->qc;
2186
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002187 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2188
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002189 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002190 TRACE_ERROR("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002191 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002192 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002193 }
2194
2195 el->rx.crypto.offset += len;
2196 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002197 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002198
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002199 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002200 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002201 ssl_err = SSL_do_handshake(ctx->ssl);
2202 if (ssl_err != 1) {
2203 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2204 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002205 TRACE_PROTO("SSL handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002206 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002207 goto out;
2208 }
2209
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002210 /* TODO: Should close the connection asap */
2211 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2212 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2213 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2214 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2215 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002216 TRACE_ERROR("SSL handshake error", QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002217 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002218 ERR_clear_error();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002219 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002220 }
2221
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002222 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002223
2224 /* Check the alpn could be negotiated */
2225 if (!qc->app_ops) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002226 TRACE_ERROR("No negotiated ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002227 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002228 goto leave;
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002229 }
2230
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002231 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002232 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002233 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2234 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2235 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002236 /* I/O callback switch */
2237 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002238 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002239 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002240 /* The connection is ready to be accepted. */
2241 quic_accept_push_qc(qc);
2242 }
2243 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002244 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002245 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002246 } else {
2247 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2248 if (ssl_err != 1) {
2249 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2250 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002251 TRACE_PROTO("SSL post handshake in progress",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002252 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002253 goto out;
2254 }
2255
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002256 TRACE_ERROR("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002257 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002258 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002259 }
2260
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002261 TRACE_STATE("SSL post handshake succeeded", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002262 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002263
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002264 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002265 ret = 1;
2266 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002267 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002268 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002269}
2270
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002271/* Parse a STREAM frame <strm_frm>
2272 *
2273 * Return 1 on success. On error, 0 is returned. In this case, the packet
2274 * containing the frame must not be acknowledged.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002275 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002276static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2277 struct quic_stream *strm_frm,
2278 struct quic_conn *qc)
2279{
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002280 int ret;
2281
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002282 /* RFC9000 13.1. Packet Processing
2283 *
2284 * A packet MUST NOT be acknowledged until packet protection has been
2285 * successfully removed and all frames contained in the packet have
2286 * been processed. For STREAM frames, this means the data has been
2287 * enqueued in preparation to be received by the application protocol,
2288 * but it does not require that data be delivered and consumed.
2289 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002290 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
2291
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002292 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2293 strm_frm->offset.key, strm_frm->fin,
2294 (char *)strm_frm->data);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002295
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002296 /* frame rejected - packet must not be acknowledeged */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002297 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
2298 return !ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002299}
2300
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002301/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2302 * for <qc> QUIC connection.
2303 * This is a best effort function which never fails even if no memory could be
2304 * allocated to duplicate these frames.
2305 */
2306static void qc_dup_pkt_frms(struct quic_conn *qc,
2307 struct list *pkt_frm_list, struct list *out_frm_list)
2308{
2309 struct quic_frame *frm, *frmbak;
2310 struct list tmp = LIST_HEAD_INIT(tmp);
2311
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002312 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2313
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002314 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2315 struct quic_frame *dup_frm, *origin;
2316
2317 switch (frm->type) {
2318 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2319 {
2320 struct quic_stream *strm_frm = &frm->stream;
2321 struct eb64_node *node = NULL;
2322 struct qc_stream_desc *stream_desc;
2323
2324 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2325 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002326 TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002327 continue;
2328 }
2329
2330 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2331 /* Do not resend this frame if in the "already acked range" */
2332 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002333 TRACE_DEVEL("ignored frame in already acked range",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002334 QUIC_EV_CONN_PRSAFRM, qc, frm);
2335 continue;
2336 }
2337 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2338 strm_frm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002339 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002340 QUIC_EV_CONN_PRSAFRM, qc, frm);
2341 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002342 break;
2343 }
2344
2345 default:
2346 break;
2347 }
2348
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002349 dup_frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002350 if (!dup_frm) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002351 TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002352 break;
2353 }
2354
2355 /* If <frm> is already a copy of another frame, we must take
2356 * its original frame as source for the copy.
2357 */
2358 origin = frm->origin ? frm->origin : frm;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002359 TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002360 TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002361 qc, NULL, &origin->pkt->pn_node.key);
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002362 *dup_frm = *origin;
2363 dup_frm->pkt = NULL;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002364 dup_frm->origin = origin;
Frédéric Lécaille4173a392022-08-18 08:20:47 +02002365 dup_frm->flags = 0;
2366 LIST_INIT(&dup_frm->reflist);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002367 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2368 LIST_APPEND(&tmp, &dup_frm->list);
2369 }
2370
2371 LIST_SPLICE(out_frm_list, &tmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002372
2373 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002374}
2375
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002376/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002377static void qc_prep_fast_retrans(struct quic_conn *qc,
2378 struct quic_enc_level *qel,
2379 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002380{
2381 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002382 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002383 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002384 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002385
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002386 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
2387
2388 BUG_ON(frms1 == frms2);
2389
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002390 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002391 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002392 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002393 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002394 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2395 node = eb64_next(node);
2396 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002397 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002398 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002399 }
2400
2401 if (!pkt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002402 goto leave;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002403
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002404 /* When building a packet from another one, the field which may increase the
2405 * packet size is the packet number. And the maximum increase is 4 bytes.
2406 */
2407 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2408 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2409 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002410 goto leave;
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002411 }
2412
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002413 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002414 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2415 if (frms == frms1 && frms2) {
2416 frms = frms2;
2417 goto start;
2418 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002419 leave:
2420 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002421}
2422
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002423/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002424 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002425 * Initial packets send them coalescing with others (Handshake here).
2426 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002427 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002428static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2429 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002430{
2431 struct list itmp = LIST_HEAD_INIT(itmp);
2432 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002433
2434 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2435 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2436 struct quic_enc_level *qel = iqel;
2437 struct eb_root *pkts;
2438 struct eb64_node *node;
2439 struct quic_tx_packet *pkt;
2440 struct list *tmp = &itmp;
2441
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002442 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002443 start:
2444 pkt = NULL;
2445 pkts = &qel->pktns->tx.pkts;
2446 node = eb64_first(pkts);
2447 /* Skip the empty packet (they have already been retransmitted) */
2448 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002449 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002450 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002451 break;
2452 node = eb64_next(node);
2453 }
2454
2455 if (!pkt)
2456 goto end;
2457
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002458 /* When building a packet from another one, the field which may increase the
2459 * packet size is the packet number. And the maximum increase is 4 bytes.
2460 */
2461 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2462 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2463 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2464 goto end;
2465 }
2466
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002467 qel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002468
2469 /* No risk to loop here, #packet per datagram is bounded */
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002470 requeue:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002471 TRACE_DEVEL("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002472 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002473 if (qel == iqel) {
2474 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2475 pkt = pkt->next;
2476 tmp = &htmp;
2477 hqel->pktns->tx.pto_probe += 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002478 TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002479 goto requeue;
2480 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002481 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002482
2483 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002484 LIST_SPLICE(ifrms, &itmp);
2485 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002486
2487 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002488}
2489
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002490static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002491{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002492 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
2493
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002494 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2495 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2496 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2497 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002498 goto out;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002499
2500 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2501 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002502
2503 out:
2504 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002505}
2506
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002507/* Enqueue a STOP_SENDING frame to send into 1RTT packet number space
2508 * frame list to send.
2509 * Return 1 if succeeded, 0 if not.
2510 */
2511static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id)
2512{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002513 int ret = 0;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002514 struct quic_frame *frm;
2515 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2516 uint64_t app_error_code;
2517
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002518 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
2519
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002520 /* TODO: the mux may be released, we cannot have more
2521 * information about the application error code to send
2522 * at this time.
2523 */
2524 app_error_code = H3_REQUEST_REJECTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002525 // fixme: zalloc
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002526 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002527 if (!frm) {
2528 TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
2529 goto out;
2530 }
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002531
2532 frm->type = QUIC_FT_STOP_SENDING;
2533 frm->stop_sending.id = id;
2534 frm->stop_sending.app_error_code = app_error_code;
2535 LIST_INIT(&frm->reflist);
2536 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002537 ret = 1;
2538 out:
2539 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
2540 return ret;
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002541}
2542
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002543/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2544 * as I/O handler context and <qel> as encryption level.
2545 * Returns 1 if succeeded, 0 if failed.
2546 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002547static 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 +01002548 struct quic_enc_level *qel)
2549{
2550 struct quic_frame frm;
2551 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002552 struct quic_conn *qc = ctx->qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002553 int fast_retrans = 0, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002554
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002555 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002556 /* Skip the AAD */
2557 pos = pkt->data + pkt->aad_len;
2558 end = pkt->data + pkt->len;
2559
2560 while (pos < end) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002561 if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
2562 // trace already emitted by function above
2563 goto leave;
2564 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002565
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002566 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002567 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002568 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002569 break;
2570 case QUIC_FT_PING:
2571 break;
2572 case QUIC_FT_ACK:
2573 {
2574 unsigned int rtt_sample;
2575
2576 rtt_sample = 0;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002577 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
2578 // trace already emitted by function above
2579 goto leave;
2580 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002581
2582 if (rtt_sample) {
2583 unsigned int ack_delay;
2584
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002585 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002586 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002587 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2588 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002589 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002590 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002591 break;
2592 }
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002593 case QUIC_FT_RESET_STREAM:
2594 /* TODO: handle this frame at STREAM level */
2595 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002596 case QUIC_FT_STOP_SENDING:
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002597 {
2598 struct quic_stop_sending *stop_sending = &frm.stop_sending;
2599 if (qc->mux_state == QC_MUX_READY) {
2600 if (qcc_recv_stop_sending(qc->qcc, stop_sending->id,
2601 stop_sending->app_error_code)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002602 TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
2603 goto leave;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002604 }
2605 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002606 break;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02002607 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002608 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002609 {
2610 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002611
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002612 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002613 /* XXX TO DO: <cfdebug> is used only for the traces. */
2614 struct quic_rx_crypto_frm cfdebug = { };
2615
2616 cfdebug.offset_node.key = frm.crypto.offset;
2617 cfdebug.len = frm.crypto.len;
2618 TRACE_PROTO("CRYPTO data discarded",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002619 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002620 break;
2621 }
2622
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002623 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2624 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2625 /* XXX TO DO: <cfdebug> is used only for the traces. */
2626 struct quic_rx_crypto_frm cfdebug = { };
2627
2628 cfdebug.offset_node.key = frm.crypto.offset;
2629 cfdebug.len = frm.crypto.len;
2630 /* Nothing to do */
2631 TRACE_PROTO("Already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002632 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002633 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002634 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2635 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002636 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002637 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002638 else {
2639 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2640 /* XXX TO DO: <cfdebug> is used only for the traces. */
2641 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002642
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002643 cfdebug.offset_node.key = frm.crypto.offset;
2644 cfdebug.len = frm.crypto.len;
2645 TRACE_PROTO("Partially already received CRYPTO data",
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002646 QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002647 frm.crypto.len -= diff;
2648 frm.crypto.data += diff;
2649 frm.crypto.offset = qel->rx.crypto.offset;
2650 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002651 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002652
2653 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002654 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002655 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002656
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002657 cfdebug.offset_node.key = frm.crypto.offset;
2658 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002659 if (!qc_provide_cdata(qel, ctx,
2660 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002661 pkt, &cfdebug)) {
2662 // trace already emitted by function above
2663 goto leave;
2664 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002665
2666 break;
2667 }
2668
2669 /* frm.crypto.offset > qel->rx.crypto.offset */
2670 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2671 if (!cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002672 TRACE_ERROR("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002673 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002674 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002675 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002676
2677 cf->offset_node.key = frm.crypto.offset;
2678 cf->len = frm.crypto.len;
2679 cf->data = frm.crypto.data;
2680 cf->pkt = pkt;
2681 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2682 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002683 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002684 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002685 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002686 {
2687 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002688 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002689
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002690 /* The upper layer may not be allocated. */
2691 if (qc->mux_state != QC_MUX_READY) {
2692 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002693 TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002694 break;
2695 }
2696 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002697 TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002698 if (!qc_stop_sending_frm_enqueue(qc, stream->id))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002699 TRACE_ERROR("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc);
2700 /* This packet will not be acknowledged */
2701 goto leave;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002702 }
2703 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002704
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002705 if (!qc_handle_strm_frm(pkt, stream, qc)) {
2706 TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
2707 goto leave;
2708 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002709
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002710 break;
2711 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002712 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002713 if (qc->mux_state == QC_MUX_READY) {
2714 struct quic_max_data *data = &frm.max_data;
2715 qcc_recv_max_data(qc->qcc, data->max_data);
2716 }
2717 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002718 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002719 if (qc->mux_state == QC_MUX_READY) {
2720 struct quic_max_stream_data *data = &frm.max_stream_data;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002721 if (qcc_recv_max_stream_data(qc->qcc, data->id,
2722 data->max_stream_data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002723 TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
2724 goto leave;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02002725 }
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002726 }
2727 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002728 case QUIC_FT_MAX_STREAMS_BIDI:
2729 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002730 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002731 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002732 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2733 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002734 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002735 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2736 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002737 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002738 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2739 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002740 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002741 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002742 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002743 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002744 case QUIC_FT_RETIRE_CONNECTION_ID:
2745 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002746 break;
2747 case QUIC_FT_CONNECTION_CLOSE:
2748 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002749 /* Increment the error counters */
2750 qc_cc_err_count_inc(qc, &frm);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002751 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002752 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2753 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002754 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002755 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002756 TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002757 /* RFC 9000 10.2. Immediate Close:
2758 * The closing and draining connection states exist to ensure
2759 * that connections close cleanly and that delayed or reordered
2760 * packets are properly discarded. These states SHOULD persist
2761 * for at least three times the current PTO interval...
2762 *
2763 * Rearm the idle timeout only one time when entering draining
2764 * state.
2765 */
2766 qc_idle_timer_do_rearm(qc);
2767 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002768 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002769 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002770 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002771 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002772 if (qc_is_listener(ctx->qc)) {
2773 TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
2774 QUIC_EV_CONN_PRSHPKT, qc);
2775 goto leave;
2776 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002777
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002778 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002779 break;
2780 default:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002781 TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
2782 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002783 }
2784 }
2785
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002786 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002787 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002788
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002789 if (fast_retrans) {
2790 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2791 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2792
2793 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2794 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002795
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002796 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2797 * has successfully parse a Handshake packet. The Initial encryption must also
2798 * be discarded.
2799 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002800 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002801 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002802 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2803 QUIC_FL_TLS_SECRETS_DCD)) {
2804 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2805 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2806 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2807 qc_set_timer(ctx->qc);
2808 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002809 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002810 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002811 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2812 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002813 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002814 }
2815
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002816 ret = 1;
2817 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002818 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002819 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002820}
2821
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002822/* Allocate Tx buffer from <qc> quic-conn if needed.
2823 *
2824 * Returns allocated buffer or NULL on error.
2825 */
2826static struct buffer *qc_txb_alloc(struct quic_conn *qc)
2827{
2828 struct buffer *buf = &qc->tx.buf;
2829 if (!b_alloc(buf))
2830 return NULL;
2831
2832 return buf;
2833}
2834
2835/* Free Tx buffer from <qc> if it is empty. */
2836static void qc_txb_release(struct quic_conn *qc)
2837{
2838 struct buffer *buf = &qc->tx.buf;
2839
2840 /* For the moment sending function is responsible to purge the buffer
2841 * entirely. It may change in the future but this requires to be able
2842 * to reuse old data.
2843 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002844 BUG_ON_HOT(buf && b_data(buf));
Amaury Denoyelle5b689862022-08-08 16:07:30 +02002845
2846 if (!b_data(buf)) {
2847 b_free(buf);
2848 offer_buffers(NULL, 1);
2849 }
2850}
2851
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002852/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
2853 * must contains the address of the first packet stored in the payload.
2854 *
2855 * Caller is responsible that there is enough space in the buffer.
2856 */
2857static void qc_txb_store(struct buffer *buf, uint16_t length,
2858 struct quic_tx_packet *first_pkt)
2859{
2860 const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
2861 BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
2862
2863 write_u16(b_tail(buf), length);
2864 write_ptr(b_tail(buf) + sizeof(length), first_pkt);
2865 b_add(buf, hdlen + length);
2866}
2867
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002868/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2869 * with <frms> as ack-eliciting frame list to send, 0 if not.
2870 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2871 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002872 * <force_ack> may be set to 1 if you want to force an ack.
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002873 */
2874static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002875 struct quic_enc_level *qel, int cc, int probe, int force_ack)
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002876{
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002877 unsigned int must_ack = force_ack ||
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002878 (LIST_ISEMPTY(frms) && (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED));
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002879
2880 /* Do not build any more packet if the TX secrets are not available or
2881 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2882 * and if there is no more packets to send upon PTO expiration
2883 * and if there is no more ack-eliciting frames to send or in flight
2884 * congestion control limit is reached for prepared data
2885 */
2886 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2887 (!cc && !probe && !must_ack &&
2888 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002889 return 0;
2890 }
2891
2892 return 1;
2893}
2894
Amaury Denoyellef2476052022-08-04 16:19:57 +02002895/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2896 * <frms>. Each packet is stored in a distinct datagram written to <buf>.
2897 *
2898 * Each datagram is prepended by a two fields header : the datagram length and
2899 * the address of the packet contained in the datagram.
2900 *
2901 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2902 * -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002903 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002904static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002905 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002906{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002907 int ret = -1;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002908 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002909 unsigned char *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002910 struct quic_tx_packet *pkt;
2911 size_t total;
Amaury Denoyellef2476052022-08-04 16:19:57 +02002912 /* Each datagram is prepended with its length followed by the address
2913 * of the first packet in the datagram.
2914 */
2915 const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002916
2917 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002918
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002919 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2920 total = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002921 pos = (unsigned char *)b_tail(buf);
Amaury Denoyellef2476052022-08-04 16:19:57 +02002922 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002923 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002924
2925 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002926 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002927 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002928 /* We do not probe if an immediate close was asked */
2929 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002930 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002931
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002932 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002933 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002934
2935 /* Leave room for the datagram header */
2936 pos += dg_headlen;
2937 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002938 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 +01002939 }
2940 else {
2941 end = pos + qc->path->mtu;
2942 }
2943
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02002944 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
2945 QUIC_PACKET_TYPE_SHORT, 0, 0, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002946 switch (err) {
2947 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002948 // trace already emitted by function above
2949 goto leave;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002950 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002951 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2952 * MTU, we are here because of the congestion control window. There is
2953 * no need to try to reuse this buffer.
2954 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002955 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002956 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002957 default:
2958 break;
2959 }
2960
2961 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002962 BUG_ON(!pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002963
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002964 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2965 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2966
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002967 total += pkt->len;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002968
Amaury Denoyellef2476052022-08-04 16:19:57 +02002969 /* Write datagram header. */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002970 qc_txb_store(buf, pkt->len, pkt);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002971 }
2972
2973 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002974 ret = total;
2975 leave:
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002976 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02002977 return ret;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002978}
2979
Amaury Denoyellef2476052022-08-04 16:19:57 +02002980/* Prepare as much as possible QUIC packets for sending from prebuilt frames
2981 * <frms>. Several packets can be regrouped in a single datagram. The result is
2982 * written into <buf>.
2983 *
2984 * Each datagram is prepended by a two fields header : the datagram length and
2985 * the address of first packet in the datagram.
2986 *
2987 * Returns the number of bytes prepared in packets if succeeded (may be 0), or
2988 * -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002989 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02002990static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002991 enum quic_tls_enc_level tel, struct list *tel_frms,
2992 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002994 struct quic_enc_level *qel;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02002995 unsigned char *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002996 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2997 /* length of datagrams */
2998 uint16_t dglen;
2999 size_t total;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003000 int ret = -1, padding;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003001 /* Each datagram is prepended with its length followed by the address
3002 * of the first packet in the datagram.
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003003 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003004 const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003005 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003006
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003007 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3008
Amaury Denoyellef2476052022-08-04 16:19:57 +02003009 /* Currently qc_prep_pkts() does not handle buffer wrapping so the
3010 * caller must ensure that buf is resetted.
3011 */
3012 BUG_ON_HOT(buf->head || buf->data);
3013
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003014 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003015 qel = &qc->els[tel];
3016 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003017 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003018 padding = 0;
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003019 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003020 first_pkt = prv_pkt = NULL;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003021 while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003022 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003023 enum quic_pkt_type pkt_type;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003024 struct quic_tls_ctx *tls_ctx;
3025 const struct quic_version *ver;
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003026 int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3027 (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3028 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003029
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003030 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003031 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003032 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003033 /* We do not probe if an immediate close was asked */
3034 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01003035 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003036
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02003037 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003038 if (prv_pkt)
3039 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003040 /* Let's select the next encryption level */
3041 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3042 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003043 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003044 qel = &qc->els[tel];
3045 /* Build a new datagram */
3046 prv_pkt = NULL;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003047 TRACE_DEVEL("next encryption level selected", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003048 continue;
3049 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003050 break;
3051 }
3052
3053 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003054 if (!prv_pkt) {
3055 /* Leave room for the datagram header */
3056 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003057 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02003058 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 +01003059 }
3060 else {
3061 end = pos + qc->path->mtu;
3062 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003063 }
3064
Frédéric Lécaille301425b2022-06-14 17:40:39 +02003065 if (qc->negotiated_version) {
3066 ver = qc->negotiated_version;
3067 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
3068 tls_ctx = &qc->negotiated_ictx;
3069 else
3070 tls_ctx = &qel->tls_ctx;
3071 }
3072 else {
3073 ver = qc->original_version;
3074 tls_ctx = &qel->tls_ctx;
3075 }
3076
3077 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02003078 qc, ver, dglen, pkt_type,
3079 force_ack, padding, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003080 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003081 case -2:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003082 // trace already emitted by function above
3083 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003084 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003085 /* If there was already a correct packet present, set the
3086 * current datagram as prepared into <cbuf>.
3087 */
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003088 if (prv_pkt)
3089 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003090 TRACE_DEVEL("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003091 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003092 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01003093 break;
3094 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02003095
Frédéric Lécaille63556772021-12-29 17:18:21 +01003096 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003097 BUG_ON(!cur_pkt);
Frédéric Lécaille63556772021-12-29 17:18:21 +01003098
Frédéric Lécaille1809c332022-04-25 10:24:12 +02003099 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3100 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3101
Frédéric Lécaille63556772021-12-29 17:18:21 +01003102 total += cur_pkt->len;
3103 /* keep trace of the first packet in the datagram */
3104 if (!first_pkt)
3105 first_pkt = cur_pkt;
3106 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003107 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003108 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003109 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3110 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01003111 /* Let's say we have to build a new dgram */
3112 prv_pkt = NULL;
3113 dglen += cur_pkt->len;
3114 /* Client: discard the Initial encryption keys as soon as
3115 * a handshake packet could be built.
3116 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003117 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01003118 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3119 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3120 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3121 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3122 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01003123 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003124 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003125 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003126 }
3127 /* If the data for the current encryption level have all been sent,
3128 * select the next level.
3129 */
3130 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003131 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 +01003132 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3133 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3134 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3135 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003136 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3137 frms = &qc->els[tel].pktns->tx.frms;
3138 else
3139 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003140 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003141 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003142 /* If there is data for the next level, do not
3143 * consume a datagram.
3144 */
3145 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003146 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003147 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02003148
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003149 /* If we have to build a new datagram, set the current datagram as
3150 * prepared into <cbuf>.
3151 */
3152 if (!prv_pkt) {
Amaury Denoyelle654269c2022-08-08 15:38:57 +02003153 qc_txb_store(buf, dglen, first_pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003154 first_pkt = NULL;
3155 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003156 padding = 0;
3157 }
3158 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003159 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003160 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3161 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003162 }
3163 }
3164
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003165 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003166 ret = total;
3167 leave:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003168 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003169 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003170}
3171
Amaury Denoyellef2476052022-08-04 16:19:57 +02003172/* Send datagrams stored in <buf>.
3173 *
3174 * This function always returns 1 for success. Even if sendto() syscall failed,
3175 * buffer is drained and packets are considered as emitted. QUIC loss detection
3176 * mechanism is used as a back door way to retry sending.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003177 */
Amaury Denoyellef2476052022-08-04 16:19:57 +02003178int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003179{
3180 struct quic_conn *qc;
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003181 char skip_sendto = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003182
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003183 qc = ctx->qc;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003184 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003185 while (b_contig_data(buf, 0)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003186 unsigned char *pos;
3187 struct buffer tmpbuf = { };
3188 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3189 uint16_t dglen;
3190 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003191 unsigned int time_sent;
3192
Amaury Denoyellef2476052022-08-04 16:19:57 +02003193 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003194 dglen = read_u16(pos);
Amaury Denoyellef2476052022-08-04 16:19:57 +02003195 BUG_ON_HOT(!dglen); /* this should not happen */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003196
3197 pos += sizeof dglen;
3198 first_pkt = read_ptr(pos);
3199 pos += sizeof first_pkt;
3200 tmpbuf.area = (char *)pos;
3201 tmpbuf.size = tmpbuf.data = dglen;
3202
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003203 /* If sendto is on error just skip the call to it for the rest
3204 * of the loop but continue to purge the buffer. Data will be
3205 * transmitted when QUIC packets are detected as lost on our
3206 * side.
3207 *
3208 * TODO use fd-monitoring to detect when send operation can be
3209 * retry. This should improve the bandwidth without relying on
3210 * retransmission timer. However, it requires a major rework on
3211 * quic-conn fd management.
3212 */
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003213 if (!skip_sendto) {
3214 if (qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0)) {
3215 skip_sendto = 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003216 TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
Amaury Denoyelle906b0582022-08-05 15:22:28 +02003217 }
3218 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003219
Amaury Denoyellef2476052022-08-04 16:19:57 +02003220 b_del(buf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003221 qc->tx.bytes += tmpbuf.data;
3222 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003223
3224 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3225 pkt->time_sent = time_sent;
3226 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3227 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003228 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003229 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3230 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003231 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003232 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3233 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3234 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003235 qc_notify_close(qc);
3236
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003237 /* RFC 9000 10.2. Immediate Close:
3238 * The closing and draining connection states exist to ensure
3239 * that connections close cleanly and that delayed or reordered
3240 * packets are properly discarded. These states SHOULD persist
3241 * for at least three times the current PTO interval...
3242 *
3243 * Rearm the idle timeout only one time when entering closing
3244 * state.
3245 */
3246 qc_idle_timer_do_rearm(qc);
3247 if (qc->timer_task) {
3248 task_destroy(qc->timer_task);
3249 qc->timer_task = NULL;
3250 }
3251 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003252 qc->path->in_flight += pkt->in_flight_len;
3253 pkt->pktns->tx.in_flight += pkt->in_flight_len;
Frédéric Lécaille5f6c25e2022-08-24 16:23:44 +02003254 if (pkt->in_flight_len)
Frédéric Lécailleda9c4412022-08-22 18:47:51 +02003255 qc_set_timer(qc);
Frédéric Lécaille5f6c25e2022-08-24 16:23:44 +02003256 TRACE_DATA("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
3257 next_pkt = pkt->next;
3258 quic_tx_packet_refinc(pkt);
3259 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003260 }
3261 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003262
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003263 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3264
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003265 return 1;
3266}
3267
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003268/* Copy into <buf> buffer a stateless reset token depending on the
3269 * <salt> salt input. This is the cluster secret which will be derived
3270 * as HKDF input secret to generate this token.
3271 * Return 1 if succeeded, 0 if not.
3272 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003273static int quic_stateless_reset_token_cpy(struct quic_conn *qc,
3274 unsigned char *buf, size_t len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003275 const unsigned char *salt, size_t saltlen)
3276{
3277 /* Input secret */
3278 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3279 size_t keylen = strlen(global.cluster_secret);
3280 /* Info */
3281 const unsigned char label[] = "stateless token";
3282 size_t labellen = sizeof label - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003283 int ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003284
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003285 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3286
3287 ret = quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003288 key, keylen, salt, saltlen, label, labellen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003289 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3290 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003291}
3292
3293/* Initialize the stateless reset token attached to <cid> connection ID.
3294 * Returns 1 if succeeded, 0 if not.
3295 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003296static int quic_stateless_reset_token_init(struct quic_conn *qc,
3297 struct quic_connection_id *quic_cid)
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003298{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003299 int ret;
3300
3301 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3302
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003303 if (global.cluster_secret) {
3304 /* Output secret */
3305 unsigned char *token = quic_cid->stateless_reset_token;
3306 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3307 /* Salt */
3308 const unsigned char *cid = quic_cid->cid.data;
3309 size_t cidlen = quic_cid->cid.len;
3310
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003311 ret = quic_stateless_reset_token_cpy(qc, token, tokenlen, cid, cidlen);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003312 }
3313 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003314 /* TODO: RAND_bytes() should be replaced */
3315 ret = RAND_bytes(quic_cid->stateless_reset_token,
3316 sizeof quic_cid->stateless_reset_token) == 1;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003317 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003318
3319 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3320 return ret;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003321}
3322
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003323/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3324 * ebtree.
3325 *
3326 * The CID is randomly generated in part with the result altered to be
3327 * associated with the current thread ID. This means this function must only
3328 * be called by the quic_conn thread.
3329 *
3330 * Returns the new CID if succeeded, NULL if not.
3331 */
3332static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3333 struct quic_conn *qc,
3334 int seq_num)
3335{
3336 struct quic_connection_id *cid;
3337
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003338 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3339
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003340 cid = pool_alloc(pool_head_quic_connection_id);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003341 if (!cid) {
3342 TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
3343 goto err;
3344 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003345
3346 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003347 /* TODO: RAND_bytes() should be replaced */
3348 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1) {
3349 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003350 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003351 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003352
3353 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003354 if (quic_stateless_reset_token_init(qc, cid) != 1) {
3355 TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003356 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003357 }
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003358
3359 cid->qc = qc;
3360
3361 cid->seq_num.key = seq_num;
3362 cid->retire_prior_to = 0;
3363 /* insert the allocated CID in the quic_conn tree */
3364 eb64_insert(root, &cid->seq_num);
3365
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003366 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003367 return cid;
3368
3369 err:
3370 pool_free(pool_head_quic_connection_id, cid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003371 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003372 return NULL;
3373}
3374
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003375/* Build all the frames which must be sent just after the handshake have succeeded.
3376 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3377 * a HANDSHAKE_DONE frame.
3378 * Return 1 if succeeded, 0 if not.
3379 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003380static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003381{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003382 int ret = 0, i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003383 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003384 struct quic_frame *frm, *frmbak;
3385 struct list frm_list = LIST_HEAD_INIT(frm_list);
3386 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003387
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003388 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
3389
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003390 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003391 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003392 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003393 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003394 if (!frm) {
3395 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
3396 goto leave;
3397 }
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003398
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003399 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003400 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003401 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003402 }
3403
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003404 /* Initialize <max> connection IDs minus one: there is
3405 * already one connection ID used for the current connection.
3406 */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003407 first = 1;
3408 max = qc->tx.params.active_connection_id_limit;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003409
3410 /* TODO: check limit */
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003411 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003412 struct quic_connection_id *cid;
3413
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003414 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003415 if (!frm) {
3416 TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003417 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003418 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003419
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003420 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003421 cid = new_quic_cid(&qc->cids, qc, i);
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003422 if (!cid) {
3423 TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003424 goto err;
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003425 }
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003426
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003427 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003428 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003429
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003430 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003431 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003432 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003433
3434 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003435 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003436
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003437 ret = 1;
3438 leave:
3439 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
3440 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003441
3442 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003443 /* free the frames */
3444 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3445 pool_free(pool_head_quic_frame, frm);
3446
Frédéric Lécaille96d08d32022-08-11 12:04:07 +02003447 node = eb64_lookup_ge(&qc->cids, first);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003448 while (node) {
3449 struct quic_connection_id *cid;
3450
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003451 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003452 if (cid->seq_num.key >= max)
3453 break;
3454
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003455 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003456 ebmb_delete(&cid->node);
3457 eb64_delete(&cid->seq_num);
3458 pool_free(pool_head_quic_connection_id, cid);
3459 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003460 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003461}
3462
3463/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003464void quic_free_arngs(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003465{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003466 struct eb64_node *n;
3467 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003468
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003469 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
3470
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003471 n = eb64_first(&arngs->root);
3472 while (n) {
3473 struct eb64_node *next;
3474
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003475 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003476 next = eb64_next(n);
3477 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003478 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003479 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003480 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003481
3482 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003483}
3484
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003485/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3486 * descending order.
3487 */
3488static inline size_t sack_gap(struct quic_arng_node *p,
3489 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003491 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003492}
3493
3494
3495/* Remove the last elements of <ack_ranges> list of ack range updating its
3496 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003497 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003498 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003499static int quic_rm_last_ack_ranges(struct quic_conn *qc,
3500 struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003501{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003502 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003503 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003504
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003505 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3506
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003507 last = eb64_last(&arngs->root);
3508 while (last && arngs->enc_sz > limit) {
3509 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003510
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003511 prev = eb64_prev(last);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003512 if (!prev) {
3513 TRACE_DEVEL("<last> not found", QUIC_EV_CONN_TXPKT, qc);
3514 goto out;
3515 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003516
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003517 last_node = eb64_entry(last, struct quic_arng_node, first);
3518 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003519 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3520 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3521 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3522 --arngs->sz;
3523 eb64_delete(last);
3524 pool_free(pool_head_quic_arng, last);
3525 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003526 }
3527
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003528 ret = 1;
3529 out:
3530 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3531 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003532}
3533
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003534/* Set the encoded size of <arngs> QUIC ack ranges. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003535static void quic_arngs_set_enc_sz(struct quic_conn *qc, struct quic_arngs *arngs)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003536{
3537 struct eb64_node *node, *next;
3538 struct quic_arng_node *ar, *ar_next;
3539
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003540 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3541
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003542 node = eb64_last(&arngs->root);
3543 if (!node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003544 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003545
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003546 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003547 arngs->enc_sz = quic_int_getsize(ar->last) +
3548 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3549
3550 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003551 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003552 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3553 quic_int_getsize(ar_next->last - ar_next->first.key);
3554 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003555 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003556 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003557
3558 leave:
3559 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003560}
3561
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003562/* Insert <ar> ack range into <argns> tree of ack ranges.
3563 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003564 */
3565static inline
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003566struct quic_arng_node *quic_insert_new_range(struct quic_conn *qc,
3567 struct quic_arngs *arngs,
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003568 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003569{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003570 struct quic_arng_node *new_ar;
3571
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003572 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3573
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003574 new_ar = pool_alloc(pool_head_quic_arng);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003575 if (!new_ar) {
3576 TRACE_ERROR("ack range allocation failed", QUIC_EV_CONN_RXPKT, qc);
3577 goto leave;
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003578 }
3579
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003580 new_ar->first.key = ar->first;
3581 new_ar->last = ar->last;
3582 eb64_insert(&arngs->root, &new_ar->first);
3583 arngs->sz++;
3584
3585 leave:
3586 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003587 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003588}
3589
3590/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003591 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003592 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003593 *
3594 * Descending order
3595 * ------------->
3596 * range1 range2
3597 * ..........|--------|..............|--------|
3598 * ^ ^ ^ ^
3599 * | | | |
3600 * last1 first1 last2 first2
3601 * ..........+--------+--------------+--------+......
3602 * diff1 gap12 diff2
3603 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003604 * To encode the previous list of ranges we must encode integers as follows in
3605 * descending order:
3606 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003607 * with diff1 = last1 - first1
3608 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003609 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003610 *
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003611
3612returns 0 on error
3613
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003614 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003615int quic_update_ack_ranges_list(struct quic_conn *qc,
3616 struct quic_arngs *arngs,
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003617 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003618{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003619 int ret = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003620 struct eb64_node *le;
3621 struct quic_arng_node *new_node;
3622 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003623
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003624 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
3625
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003626 new = NULL;
3627 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003628 new_node = quic_insert_new_range(qc, arngs, ar);
3629 if (new_node)
3630 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003631
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003632 goto leave;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003633 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003634
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003635 le = eb64_lookup_le(&arngs->root, ar->first);
3636 if (!le) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003637 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003638 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003639 goto leave;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003640
3641 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003642 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003643 else {
3644 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003645 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003646
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003647 /* Already existing range */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003648 if (le_ar->last >= ar->last) {
3649 ret = 1;
3650 }
3651 else if (le_ar->last + 1 >= ar->first) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003652 le_ar->last = ar->last;
3653 new = le;
3654 new_node = le_ar;
3655 }
3656 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003657 new_node = quic_insert_new_range(qc, arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003658 if (!new_node)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003659 goto leave;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003660
3661 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003662 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003663 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003664
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003665 /* Verify that the new inserted node does not overlap the nodes
3666 * which follow it.
3667 */
3668 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003669 struct eb64_node *next;
3670 struct quic_arng_node *next_node;
3671
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003672 while ((next = eb64_next(new))) {
3673 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003674 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003675 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003676 break;
3677
3678 if (next_node->last > new_node->last)
3679 new_node->last = next_node->last;
3680 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003681 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003682 /* Decrement the size of these ranges. */
3683 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003684 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003685 }
3686
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003687 ret = 1;
3688 leave:
3689 quic_arngs_set_enc_sz(qc, arngs);
3690 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
3691 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003692}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003693/* Remove the header protection of packets at <el> encryption level.
3694 * Always succeeds.
3695 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003696static 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 +01003697{
3698 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003699 struct quic_rx_packet *pqpkt, *pkttmp;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003700 struct quic_enc_level *app_qel;
3701
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003702 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3703 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003704 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003705 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003706 TRACE_DEVEL("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003707 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003708 goto out;
3709 }
3710 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003711 list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003712 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02003713 pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003714 TRACE_ERROR("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003715 }
3716 else {
3717 /* The AAD includes the packet number field */
3718 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3719 /* Store the packet into the tree of packets to decrypt. */
3720 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003721 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3722 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003723 TRACE_DEVEL("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003724 }
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02003725 LIST_DELETE(&pqpkt->list);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003726 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003727 }
3728
3729 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003730 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003731}
3732
3733/* Process all the CRYPTO frame at <el> encryption level.
3734 * Return 1 if succeeded, 0 if not.
3735 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003736static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
3737 struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003738 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003739{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003740 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003741 struct eb64_node *node;
3742
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003743 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3744
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003745 node = eb64_first(&el->rx.crypto.frms);
3746 while (node) {
3747 struct quic_rx_crypto_frm *cf;
3748
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003749 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003750 if (cf->offset_node.key != el->rx.crypto.offset)
3751 break;
3752
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003753 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf)) {
3754 TRACE_ERROR("qc_provide_cdata() failed", QUIC_EV_CONN_TRMHP);
3755 goto leave;
3756 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003757
3758 node = eb64_next(node);
3759 quic_rx_packet_refdec(cf->pkt);
3760 eb64_delete(&cf->offset_node);
3761 pool_free(pool_head_quic_rx_crypto_frm, cf);
3762 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003763
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003764 ret = 1;
3765 leave:
3766 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3767 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003768}
3769
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003770/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003771 * 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 +02003772 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003773 * Return 1 if succeeded, 0 if not.
3774 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003775int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3776 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003777{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003778 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003779 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003780 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003781 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003782 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003783 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003784
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003785 TRACE_ENTER(QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003786 qel = cur_el;
3787 next_tel:
3788 if (!qel)
3789 goto out;
3790
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003791 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792 while (node) {
3793 struct quic_rx_packet *pkt;
3794
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003795 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003796 TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003797 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003798 if (!qc_pkt_decrypt(pkt, qel, qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003799 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003800 TRACE_ERROR("packet decryption failed -> dropped",
3801 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003802 }
3803 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003804 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003805 /* Drop the packet */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003806 TRACE_ERROR("packet parsing failed -> dropped",
3807 QUIC_EV_CONN_RXPKT, ctx->qc, pkt);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02003808 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003809 }
3810 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003811 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3812
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003813 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3814 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3815 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003816 qc_idle_timer_rearm(qc, 1);
3817 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003818 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003819 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003820 largest_pn_time_received = pkt->time_received;
3821 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003822 /* Update the list of ranges to acknowledge. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003823 if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
3824 TRACE_ERROR("Could not update ack range list",
3825 QUIC_EV_CONN_RXPKT, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003826 }
3827 }
3828 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003829 eb64_delete(&pkt->pn_node);
3830 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003831 }
3832
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003833 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3834 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003835 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003836 /* Update the largest acknowledged packet timestamps */
3837 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3838 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3839 }
3840
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003841 if (!qc_treat_rx_crypto_frms(qc, qel, ctx)) {
3842 // trace already emitted by function above
3843 goto leave;
3844 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003845
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003846 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003847 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003848 qel = next_el;
Frédéric Lécaille91a211f2022-05-24 10:54:42 +02003849 largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003850 goto next_tel;
3851 }
3852
3853 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003854 ret = 1;
3855 leave:
3856 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, ctx->qc);
3857 return ret;
3858}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003859
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003860/* Check if it's possible to remove header protection for packets related to
3861 * encryption level <qel>. If <qel> is NULL, assume it's false.
3862 *
3863 * Return true if the operation is possible else false.
3864 */
3865static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3866{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003867 int ret = 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003868 enum quic_tls_enc_level tel;
3869
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003870 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
3871
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003872 if (!qel)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003873 goto cant_rm_hp;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003874
3875 tel = ssl_to_quic_enc_level(qel->level);
3876
3877 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003878 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003879 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003880 goto cant_rm_hp;
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003881 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003882
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003883 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3884 TRACE_DEVEL("non available secrets", QUIC_EV_CONN_TRMHP, qc);
3885 goto cant_rm_hp;
3886 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003887
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003888 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003889 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003890 qc->mux_state == QC_MUX_NULL) {
3891 TRACE_DEVEL("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
3892 goto cant_rm_hp;
3893 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003894
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003895 ret = 1;
3896 cant_rm_hp:
3897 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
3898 return ret;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003899}
3900
Amaury Denoyellecc130472022-08-17 10:08:16 +02003901/* Try to send application frames from list <frms> on connection <qc>.
3902 *
Amaury Denoyelle70467562022-08-17 16:33:13 +02003903 * Use qc_send_app_probing wrapper when probing with old data.
Amaury Denoyellecc130472022-08-17 10:08:16 +02003904 *
3905 * Returns 1 on success. Some data might not have been sent due to congestion,
3906 * in this case they are left in <frms> input list. The caller may subscribe on
3907 * quic-conn to retry later.
3908 *
3909 * Returns 0 on critical error.
3910 * TODO review and classify more distinctly transient from definitive errors to
3911 * allow callers to properly handle it.
3912 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003913static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003914{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003915 int status = 0;
3916 struct buffer *buf;
3917
3918 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3919
3920 buf = qc_txb_alloc(qc);
3921 if (!buf) {
3922 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
3923 goto leave;
3924 }
3925
Frédéric Lécailledc077512022-07-26 09:17:19 +02003926 /* Prepare and send packets until we could not further prepare packets. */
3927 while (1) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003928 int ret;
Amaury Denoyellef2476052022-08-04 16:19:57 +02003929 /* Currently buf cannot be non-empty at this stage. Even if a
3930 * previous sendto() has failed it is emptied to simulate
3931 * packet emission and rely on QUIC lost detection to try to
3932 * emit it.
3933 */
3934 BUG_ON_HOT(b_data(buf));
3935 b_reset(buf);
3936
Amaury Denoyellef2476052022-08-04 16:19:57 +02003937 ret = qc_prep_app_pkts(qc, buf, frms);
Frédéric Lécailledc077512022-07-26 09:17:19 +02003938 if (ret == -1)
3939 goto err;
3940 else if (ret == 0)
3941 goto out;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003942
Amaury Denoyellef2476052022-08-04 16:19:57 +02003943 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécailledc077512022-07-26 09:17:19 +02003944 goto err;
3945 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003946
3947 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003948 status = 1;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003949 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003950 leave:
3951 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3952 return status;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003953
3954 err:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02003955 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02003956 goto leave;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003957}
3958
Amaury Denoyellecc130472022-08-17 10:08:16 +02003959/* Try to send application frames from list <frms> on connection <qc>. Use this
Amaury Denoyelle70467562022-08-17 16:33:13 +02003960 * function when probing is required.
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003961 *
3962 * Returns the result from qc_send_app_pkts function.
3963 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003964static forceinline int qc_send_app_probing(struct quic_conn *qc,
3965 struct list *frms)
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003966{
3967 int ret;
3968
3969 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
3970
Amaury Denoyelle70467562022-08-17 16:33:13 +02003971 TRACE_STATE("preparing old data (probing)", QUIC_EV_CONN_TXPKT, qc);
3972 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003973 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003974 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02003975
3976 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3977 return ret;
3978}
3979
Amaury Denoyelle70467562022-08-17 16:33:13 +02003980/* Try to send application frames from list <frms> on connection <qc>. This
3981 * function is provided for MUX upper layer usage only.
Amaury Denoyellecc130472022-08-17 10:08:16 +02003982 *
3983 * Returns the result from qc_send_app_pkts function.
3984 */
Amaury Denoyelle70467562022-08-17 16:33:13 +02003985int qc_send_mux(struct quic_conn *qc, struct list *frms)
Amaury Denoyellecc130472022-08-17 10:08:16 +02003986{
3987 int ret;
3988
3989 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003990 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 +02003991
Amaury Denoyelle70467562022-08-17 16:33:13 +02003992 TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
3993 qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02003994 ret = qc_send_app_pkts(qc, frms);
Amaury Denoyelle70467562022-08-17 16:33:13 +02003995 qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
Amaury Denoyellecc130472022-08-17 10:08:16 +02003996
3997 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
3998 return ret;
3999}
4000
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004001/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
4002 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004003 * QUIC connection. <old_data> is used as boolean to send data already sent but
4004 * not already acknowledged (in flight).
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004005 * Returns 1 if succeeded, 0 if not.
4006 */
4007int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
4008 enum quic_tls_enc_level tel, struct list *tel_frms,
4009 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
4010{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004011 int ret, status = 0;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004012 struct buffer *buf = qc_txb_alloc(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004013
4014 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4015
4016 if (!buf) {
4017 TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
4018 goto leave;
4019 }
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004020
Amaury Denoyellef2476052022-08-04 16:19:57 +02004021 /* Currently buf cannot be non-empty at this stage. Even if a previous
4022 * sendto() has failed it is emptied to simulate packet emission and
4023 * rely on QUIC lost detection to try to emit it.
4024 */
4025 BUG_ON_HOT(b_data(buf));
4026 b_reset(buf);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004027
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004028 if (old_data) {
4029 TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004030 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004031 }
Amaury Denoyellef2476052022-08-04 16:19:57 +02004032
4033 ret = qc_prep_pkts(qc, buf, tel, tel_frms, next_tel, next_tel_frms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004034 if (ret == -1)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004035 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004036 else if (ret == 0)
4037 goto skip_send;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004038
Amaury Denoyellef2476052022-08-04 16:19:57 +02004039 if (!qc_send_ppkts(buf, qc->xprt_ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004040 goto out;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004041
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004042 skip_send:
4043 status = 1;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004044 out:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004045 TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004046 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004047 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004048 leave:
4049 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
4050 return status;
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004051}
4052
4053/* Retransmit up to two datagrams depending on packet number space */
4054static void qc_dgrams_retransmit(struct quic_conn *qc)
4055{
4056 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4057 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4058 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4059
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004060 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
4061
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004062 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4063 struct list ifrms = LIST_HEAD_INIT(ifrms);
4064 struct list hfrms = LIST_HEAD_INIT(hfrms);
4065
4066 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004067 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
4068 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004069 if (!LIST_ISEMPTY(&ifrms)) {
4070 iqel->pktns->tx.pto_probe = 1;
4071 if (!LIST_ISEMPTY(&hfrms)) {
4072 hqel->pktns->tx.pto_probe = 1;
4073 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
4074 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
4075 }
4076 }
4077 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4078 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004079 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004080 if (!LIST_ISEMPTY(&hfrms)) {
4081 hqel->pktns->tx.pto_probe = 1;
4082 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
4083 QUIC_TLS_ENC_LEVEL_NONE, NULL);
4084 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004085 TRACE_STATE("no more need to probe Handshake packet number space",
4086 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004087 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4088 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004089 TRACE_STATE("no more need to probe Initial packet number space",
4090 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004091 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4092 }
4093 else {
4094 int i;
4095 struct list frms1 = LIST_HEAD_INIT(frms1);
4096 struct list frms2 = LIST_HEAD_INIT(frms2);
4097
4098 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4099 hqel->pktns->tx.pto_probe = 0;
4100 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
4101 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004102 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004103 if (!LIST_ISEMPTY(&frms1)) {
4104 hqel->pktns->tx.pto_probe = 1;
4105 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
4106 QUIC_TLS_ENC_LEVEL_NONE, NULL);
4107 }
4108 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004109 TRACE_STATE("no more need to probe Handshake packet number space",
4110 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004111 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4112 }
4113 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
4114 aqel->pktns->tx.pto_probe = 0;
4115 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
4116 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
4117 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
4118 if (!LIST_ISEMPTY(&frms1)) {
4119 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004120 qc_send_app_probing(qc, &frms1);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004121 }
4122 if (!LIST_ISEMPTY(&frms2)) {
4123 aqel->pktns->tx.pto_probe = 1;
Amaury Denoyellecc130472022-08-17 10:08:16 +02004124 qc_send_app_probing(qc, &frms2);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004125 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004126 TRACE_STATE("no more need to probe 01RTT packet number space",
4127 QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004128 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
4129 }
4130 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004131 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02004132}
4133
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004134/* QUIC connection packet handler task (post handshake) */
4135static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
4136{
4137 struct ssl_sock_ctx *ctx;
4138 struct quic_conn *qc;
4139 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004140
4141
4142 ctx = context;
4143 qc = ctx->qc;
4144 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004145
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004146 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
4147 TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004148
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004149 /* Retranmissions */
4150 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004151 TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004152 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4153 qc_dgrams_retransmit(qc);
4154 }
4155
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004156 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004157 qc_rm_hp_pkts(qc, qel);
4158
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004159 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0)) {
4160 TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
4161 goto out;
4162 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004163
Frédéric Lécaille47756802022-03-25 09:12:16 +01004164 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004165 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4166 TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01004167 goto out;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004168 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004169
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004170 /* XXX TODO: how to limit the list frames to send */
Amaury Denoyelle70467562022-08-17 16:33:13 +02004171 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
4172 TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004173 goto out;
4174 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004175
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004176 out:
4177 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004178 return t;
4179}
4180
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004181/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
4182static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
4183{
4184 return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
4185 (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
4186 qel->pktns->tx.pto_probe ||
4187 !LIST_ISEMPTY(&qel->pktns->tx.frms);
4188}
4189
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004190/* QUIC connection packet handler task. */
4191struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004192{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004193 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004194 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004195 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004196 enum quic_tls_enc_level tel, next_tel;
4197 struct quic_enc_level *qel, *next_qel;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004198 struct buffer *buf = NULL;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004199 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004200
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004201 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004202 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004203 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004204 st = qc->state;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004205 TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004206
4207 /* Retranmissions */
4208 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004209 TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004210 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4211 qc_dgrams_retransmit(qc);
4212 }
4213
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004214 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
4215 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004216 TRACE_DEVEL("needs to wakeup the timer task after the anti-amplicaiton limit was reached",
4217 QUIC_EV_CONN_IO_CB, qc);
4218 /* 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 +01004219 * after the anti-amplification was reached.
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004220 *
4221 * TODO: this part should be removed. This was there because the
4222 * datagram parser was not executed by only one thread.
Frédéric Lécaille59507de2022-08-11 12:14:07 +02004223 */
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004224 qc_set_timer(qc);
4225 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
4226 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
4227 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004228 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004229 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004230 (!LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004231 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004232 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004233 if (st >= QUIC_HS_ST_COMPLETE &&
4234 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004235 TRACE_DEVEL("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004236 /* There may be remaining Handshake packets to treat and acknowledge. */
4237 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4238 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4239 }
4240 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004241 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004242
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004243 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02004244 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004245
4246 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004247 /* Treat packets waiting for header packet protection decryption */
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004248 if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004249 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004250
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004251 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
4252 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4253 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004254 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004255
Frédéric Lécaille47756802022-03-25 09:12:16 +01004256 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4257 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4258 goto out;
4259
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004260 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004261 !LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004262 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
4263 qel = next_qel;
4264 next_qel = NULL;
4265 goto next_level;
4266 }
4267 else {
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004268 struct quic_rx_packet *pkt, *pkttmp;
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004269 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
4270
4271 /* Drop these 0-RTT packets */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004272 TRACE_DEVEL("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004273 list_for_each_entry_safe(pkt, pkttmp, &aqel->rx.pqpkts, list) {
4274 LIST_DELETE(&pkt->list);
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004275 quic_rx_packet_refdec(pkt);
4276 }
4277 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004278 }
4279
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004280 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004281 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004282 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004283 !quic_build_post_handshake_frames(qc))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004284 goto out;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01004285
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004286 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004287 QUIC_FL_TLS_SECRETS_DCD)) {
4288 /* Discard the Handshake keys. */
4289 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
4290 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
4291 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
4292 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004293 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004294 qc_release_pktns_frms(qc, qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004295 }
4296
4297 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
4298 /* There may be remaining handshake to build (acks) */
4299 st = QUIC_HS_ST_SERVER_HANDSHAKE;
4300 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004301 }
4302
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01004303 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
4304 * be considered.
4305 */
4306 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004307 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004308
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004309 if (!qc_need_sending(qc, qel) &&
4310 (!next_qel || !qc_need_sending(qc, next_qel))) {
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004311 goto skip_send;
Amaury Denoyellef0f92b22022-08-09 17:52:52 +02004312 }
Amaury Denoyellec09ef0c2022-08-08 18:15:24 +02004313
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004314 buf = qc_txb_alloc(qc);
Amaury Denoyellef2476052022-08-04 16:19:57 +02004315 if (!buf)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004316 goto out;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004317
4318 /* Currently buf cannot be non-empty at this stage. Even if a previous
4319 * sendto() has failed it is emptied to simulate packet emission and
4320 * rely on QUIC lost detection to try to emit it.
4321 */
4322 BUG_ON_HOT(b_data(buf));
4323 b_reset(buf);
4324
4325 ret = qc_prep_pkts(qc, buf, tel, &qc->els[tel].pktns->tx.frms,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02004326 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004327 if (ret == -1)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004328 goto out;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004329 else if (ret == 0)
4330 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004331
Amaury Denoyellef2476052022-08-04 16:19:57 +02004332 if (!qc_send_ppkts(buf, ctx))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004333 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004334
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004335 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004336 /* Check if there is something to do for the next level.
4337 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004338 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004339 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004340 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004341 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004342 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004343 goto next_level;
4344 }
4345
Frédéric Lécaille47756802022-03-25 09:12:16 +01004346 out:
Amaury Denoyelle5b689862022-08-08 16:07:30 +02004347 qc_txb_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004348 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004349 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004350}
4351
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004352/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004353static 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 +01004354{
4355 int i;
4356
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004357 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4358
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004359 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4360 if (qel->tx.crypto.bufs[i]) {
4361 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4362 qel->tx.crypto.bufs[i] = NULL;
4363 }
4364 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004365 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004366
4367 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004368}
4369
4370/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004371 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004372 * Returns 1 if succeeded, 0 if not.
4373 */
4374static int quic_conn_enc_level_init(struct quic_conn *qc,
4375 enum quic_tls_enc_level level)
4376{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004377 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004378 struct quic_enc_level *qel;
4379
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004380 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4381
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004382 qel = &qc->els[level];
4383 qel->level = quic_to_ssl_enc_level(level);
4384 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4385 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4386 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004387 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004388
4389 qel->rx.pkts = EB_ROOT;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02004390 LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004391 qel->rx.crypto.offset = 0;
4392 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004393
4394 /* Allocate only one buffer. */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004395 /* TODO: use a pool */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004396 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4397 if (!qel->tx.crypto.bufs)
4398 goto err;
4399
4400 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4401 if (!qel->tx.crypto.bufs[0])
4402 goto err;
4403
4404 qel->tx.crypto.bufs[0]->sz = 0;
4405 qel->tx.crypto.nb_buf = 1;
4406
4407 qel->tx.crypto.sz = 0;
4408 qel->tx.crypto.offset = 0;
4409
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004410 ret = 1;
4411 leave:
4412 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
4413 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004414
4415 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004416 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004417 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004418}
4419
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004420/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4421 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004422 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004423 * This function must only be called by the thread responsible of the quic_conn
4424 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004425 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004426static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004427{
4428 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004429 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004430 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004431 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004432 struct quic_rx_packet *pkt, *pktback;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004433
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004434 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
4435
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004436 /* We must not free the quic-conn if the MUX is still allocated. */
4437 BUG_ON(qc->mux_state == QC_MUX_READY);
4438
Willy Tarreau54bc7862022-08-10 07:26:27 +02004439 /* in the unlikely (but possible) case the connection was just added to
4440 * the accept_list we must delete it from there.
4441 */
4442 MT_LIST_DELETE(&qc->accept_list);
4443
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004444 /* free remaining stream descriptors */
4445 node = eb64_first(&qc->streams_by_id);
4446 while (node) {
4447 struct qc_stream_desc *stream;
4448
4449 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4450 node = eb64_next(node);
4451
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004452 /* all streams attached to the quic-conn are released, so
4453 * qc_stream_desc_free will liberate the stream instance.
4454 */
4455 BUG_ON(!stream->release);
Frédéric Lécailleea4a5cb2022-08-20 18:59:36 +02004456 qc_stream_desc_free(stream, 1);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004457 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004458
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004459 /* Purge Rx packet list. */
4460 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
4461 LIST_DELETE(&pkt->qc_rx_pkt_list);
4462 pool_free(pool_head_quic_rx_packet, pkt);
4463 }
4464
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004465 if (qc->idle_timer_task) {
4466 task_destroy(qc->idle_timer_task);
4467 qc->idle_timer_task = NULL;
4468 }
4469
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004470 if (qc->timer_task) {
4471 task_destroy(qc->timer_task);
4472 qc->timer_task = NULL;
4473 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004474
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004475 /* remove the connection from receiver cids trees */
4476 ebmb_delete(&qc->odcid_node);
4477 ebmb_delete(&qc->scid_node);
4478 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004479
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004480 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004481 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004482 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004483 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004484 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004485 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004486
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004487 quic_tls_ku_free(qc);
4488 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4489 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004490 quic_conn_enc_level_uninit(qc, &qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004491 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004492 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004493
4494 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4495 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4496 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004497
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004498 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
Frédéric Lécaille8ddde4f2022-08-01 14:07:50 +02004499 quic_pktns_tx_pkts_release(&qc->pktns[i], qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004500 quic_free_arngs(qc, &qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004501 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004502
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004503 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4504 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004505 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004506
4507 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004508}
4509
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004510static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004511{
4512 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004513 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004514
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004515 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004516
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004517 /* Next application data can be dropped. */
4518 qc->mux_state = QC_MUX_RELEASED;
4519
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004520 /* If the quic-conn timer has already expired free the quic-conn. */
4521 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4522 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004523 goto leave;
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004524 }
4525
Amaury Denoyelle240b1b12022-07-13 15:18:16 +02004526 qc_check_close_on_released_mux(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004527 leave:
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004528 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004529}
4530
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004531/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004532static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004533{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004534 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004535 struct quic_conn *qc;
4536 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004537
4538 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004539 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004540 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004541 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004542 task->expire = TICK_ETERNITY;
4543 pktns = quic_loss_pktns(qc);
4544 if (tick_isset(pktns->tx.loss_time)) {
4545 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4546
4547 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
Frédéric Lécaille15773f22022-08-24 17:57:09 +02004548 if (!LIST_ISEMPTY(&lost_pkts))
4549 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécaille277c4622022-08-24 17:06:04 +02004550 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004551 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004552 goto out;
4553 }
4554
4555 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004556 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004557 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4558 qc->qcc->subs->events & SUB_RETRY_SEND) {
4559 struct qcc *qcc = qc->qcc;
4560
4561 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4562 tasklet_wakeup(qcc->subs->tasklet);
4563 qcc->subs->events &= ~SUB_RETRY_SEND;
4564 if (!qcc->subs->events)
4565 qcc->subs = NULL;
4566 }
4567 else {
4568 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4569 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4570 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004571 TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
4572 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight) {
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004573 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004574 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4575 }
4576 }
4577 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE]) {
4578 TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
4579 }
4580 else if (pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT]) {
4581 TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004582 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004583 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004584 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004585 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004586 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4587 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4588
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004589 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004590 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004591 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004592 iel->pktns->tx.pto_probe = 1;
4593 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004594
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004595 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4596 qc->path->loss.pto_count++;
4597
4598 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004599 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004600
4601 return task;
4602}
4603
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004604/* Parse the Retry token from buffer <token> with <end> a pointer to
4605 * one byte past the end of this buffer. This will extract the ODCID
4606 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004607 *
4608 * Returns 0 on success else non-zero.
4609 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004610static int parse_retry_token(struct quic_conn *qc,
4611 const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004612 struct quic_cid *odcid)
4613{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004614 int ret = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004615 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004616 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004617
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004618 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
4619
4620 if (!quic_dec_int(&odcid_len, &token, end)) {
4621 TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
4622 goto leave;
4623 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004624
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004625 /* RFC 9000 7.2. Negotiating Connection IDs:
4626 * When an Initial packet is sent by a client that has not previously
4627 * received an Initial or Retry packet from the server, the client
4628 * populates the Destination Connection ID field with an unpredictable
4629 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4630 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004631 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
4632 TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
4633 goto leave;
4634 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004635
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004636 if (end - token < odcid_len + sizeof timestamp) {
4637 TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
4638 goto leave;
4639 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004640
4641 timestamp = ntohl(read_u32(token + odcid_len));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004642 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms) {
4643 TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
4644 goto leave;
4645 }
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004646
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004647 ret = 1;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004648 memcpy(odcid->data, token, odcid_len);
4649 odcid->len = odcid_len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004650 leave:
4651 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
4652 return !ret;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004653}
4654
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004655/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4656 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4657 * for QUIC servers (or haproxy listeners).
4658 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4659 * <token> the token found to be used for this connection with <token_len> as
4660 * length. <saddr> is the source address.
4661 * Returns the connection if succeeded, NULL if not.
4662 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004663static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004664 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02004665 const struct quic_cid *token_odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004666 struct sockaddr_storage *saddr,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004667 int server, int token, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004668{
4669 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004670 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004671 /* Initial CID. */
4672 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004673 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004674 struct listener *l = NULL;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004675 struct quic_cc_algo *cc_algo = NULL;
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004676 struct quic_tls_ctx *ictx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004677 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004678 qc = pool_zalloc(pool_head_quic_conn);
4679 if (!qc) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004680 TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004681 goto err;
4682 }
4683
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004684 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4685 if (!buf_area) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004686 TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004687 goto err;
4688 }
4689
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004690 qc->cids = EB_ROOT;
4691 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004692 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004693 struct proxy *prx;
4694
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004695 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004696 prx = l->bind_conf->frontend;
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004697 cc_algo = l->bind_conf->quic_cc_algo;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004698
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004699 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4700 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004701 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004702 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004703 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004704 qc->odcid.len = dcid->len;
4705 qc->odcid.addrlen = dcid->addrlen;
4706 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004707
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004708 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004709 if (scid->len)
4710 memcpy(qc->dcid.data, scid->data, scid->len);
4711 qc->dcid.len = scid->len;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004712 qc->tx.buf = BUF_NULL;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004713 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004714 }
4715 /* QUIC Client (outgoing connection to servers) */
4716 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004717 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004718 if (dcid->len)
4719 memcpy(qc->dcid.data, dcid->data, dcid->len);
4720 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004722 qc->mux_state = QC_MUX_NULL;
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02004723 qc->err = quic_err_transport(QC_ERR_NO_ERROR);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004724
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004725 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004726 if (!icid) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004727 TRACE_ERROR("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004728 goto err;
4729 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004730
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004731 /* insert the allocated CID in the receiver datagram handler tree */
4732 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004733 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004734
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004735 /* Select our SCID which is the first CID with 0 as sequence number. */
4736 qc->scid = icid->cid;
4737
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004738 /* Packet number spaces initialization. */
4739 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4740 quic_pktns_init(&qc->pktns[i]);
4741 /* QUIC encryption level context initialization. */
4742 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004743 if (!quic_conn_enc_level_init(qc, i)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004744 TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004745 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004746 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004747 /* Initialize the packet number space. */
4748 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4749 }
4750
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004751 qc->original_version = qv;
4752 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004753 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4754 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004755 /* TX part. */
4756 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004757 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4758 qc->tx.wbuf = qc->tx.rbuf = 0;
4759 qc->tx.bytes = 0;
Amaury Denoyellef2476052022-08-04 16:19:57 +02004760 qc->tx.buf = BUF_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004761 /* RX part. */
4762 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004763 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004764 for (i = 0; i < QCS_MAX_TYPES; i++)
4765 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004766
4767 qc->nb_pkt_for_cc = 1;
4768 qc->nb_pkt_since_cc = 0;
4769
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004770 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004771 if (!quic_tls_ku_init(qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004772 TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004773 goto err;
4774 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004775
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004776 /* XXX TO DO: Only one path at this time. */
4777 qc->path = &qc->paths[0];
Frédéric Lécaille43910a92022-07-11 10:24:21 +02004778 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 +01004779
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004780 /* required to use MTLIST_IN_LIST */
4781 MT_LIST_INIT(&qc->accept_list);
4782
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004783 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004784 qc->stream_buf_count = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004785 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4786
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004787 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
4788 icid->stateless_reset_token,
4789 dcid->data, dcid->len,
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02004790 qc->scid.data, qc->scid.len, token_odcid))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004791 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004792
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004793 if (qc_conn_alloc_ssl_ctx(qc) ||
4794 !quic_conn_init_timer(qc) ||
4795 !quic_conn_init_idle_timer_task(qc))
4796 goto err;
4797
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004798 ictx = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
4799 if (!qc_new_isecs(qc, ictx,qc->original_version, dcid->data, dcid->len, 1))
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004800 goto err;
4801
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02004802 if (!quic_tls_dec_aes_ctx_init(&ictx->rx.hp_ctx, ictx->rx.hp, ictx->rx.hp_key) ||
4803 !quic_tls_enc_aes_ctx_init(&ictx->tx.hp_ctx, ictx->tx.hp, ictx->tx.hp_key))
4804 goto err;
4805
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004806 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004807
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004808 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004809
4810 err:
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004811 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4812 if (qc)
4813 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004814 quic_conn_release(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004815 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004816 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004817}
4818
4819/* Initialize the timer task of <qc> QUIC connection.
4820 * Returns 1 if succeeded, 0 if not.
4821 */
4822static int quic_conn_init_timer(struct quic_conn *qc)
4823{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004824 int ret = 0;
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004825 /* Attach this task to the same thread ID used for the connection */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004826 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4827
Willy Tarreau87168752022-06-13 16:31:53 +02004828 qc->timer_task = task_new_on(qc->tid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004829 if (!qc->timer_task) {
4830 TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4831 goto leave;
4832 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004833
4834 qc->timer = TICK_ETERNITY;
4835 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004836 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004837
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004838 ret = 1;
4839 leave:
4840 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4841 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004842}
4843
Frédéric Lécaille47756802022-03-25 09:12:16 +01004844/* Rearm the idle timer for <qc> QUIC connection. */
4845static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4846{
4847 unsigned int expire;
4848
4849 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4850 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4851}
4852
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004853/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4854 * which is set to 1 when receiving a packet , and 0 when sending packet
4855 */
4856static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4857{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004858 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4859
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004860 if (read) {
4861 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4862 }
4863 else {
4864 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4865 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004866 qc_idle_timer_do_rearm(qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004867
4868 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004869}
4870
4871/* The task handling the idle timeout */
4872static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4873{
4874 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004875 struct quic_counters *prx_counters = qc->prx_counters;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004876 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004877
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004878 TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
4879
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004880 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4881 * might free the quic-conn too early via quic_close().
4882 */
4883 qc_notify_close(qc);
4884
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004885 /* If the MUX is still alive, keep the quic-conn. The MUX is
4886 * responsible to call quic_close to release it.
4887 */
4888 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4889 if (qc->mux_state != QC_MUX_READY)
4890 quic_conn_release(qc);
4891
4892 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4893 * least clean some parts of it such as the tasklet.
4894 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004895
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004896 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
4897 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004898 TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_SSLALERT, qc);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004899 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004900 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004901
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004902 TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004903 return NULL;
4904}
4905
4906/* Initialize the idle timeout task for <qc>.
4907 * Returns 1 if succeeded, 0 if not.
4908 */
4909static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4910{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004911 int ret = 0;
4912
4913 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
4914
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004915 qc->idle_timer_task = task_new_here();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004916 if (!qc->idle_timer_task) {
4917 TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
4918 goto leave;
4919 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004920
4921 qc->idle_timer_task->process = qc_idle_timer_task;
4922 qc->idle_timer_task->context = qc;
4923 qc_idle_timer_rearm(qc, 1);
4924 task_queue(qc->idle_timer_task);
4925
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004926 ret = 1;
4927 leave:
4928 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
4929 return ret;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004930}
4931
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004932/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4933 * past one byte of this buffer.
4934 */
4935static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4936 struct quic_rx_packet *pkt)
4937{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004938 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004939 unsigned char dcid_len, scid_len;
4940
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004941 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
4942
4943 if (end == *buf) {
4944 TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
4945 goto leave;
4946 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004947
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004948 /* Destination Connection ID Length */
4949 dcid_len = *(*buf)++;
4950 /* 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 +02004951 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1) {
4952 TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
4953 goto leave;
4954 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004955
4956 if (dcid_len) {
4957 /* Check that the length of this received DCID matches the CID lengths
4958 * of our implementation for non Initials packets only.
4959 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004960 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4961 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004962 dcid_len != QUIC_HAP_CID_LEN) {
4963 TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
4964 goto leave;
4965 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004966
4967 memcpy(pkt->dcid.data, *buf, dcid_len);
4968 }
4969
4970 pkt->dcid.len = dcid_len;
4971 *buf += dcid_len;
4972
4973 /* Source Connection ID Length */
4974 scid_len = *(*buf)++;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004975 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len) {
4976 TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
4977 goto leave;
4978 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004979
4980 if (scid_len)
4981 memcpy(pkt->scid.data, *buf, scid_len);
4982 pkt->scid.len = scid_len;
4983 *buf += scid_len;
4984
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004985 ret = 1;
4986 leave:
4987 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
4988 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004989}
4990
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004991/* Insert <pkt> RX packet in its <qel> RX packets tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004992static void qc_pkt_insert(struct quic_conn *qc,
4993 struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004994{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02004995 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
4996
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004997 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004998 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004999 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005000
5001 TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005002}
5003
5004/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005005 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
5006 * byte past the end of the buffer containing this packet and <beg> the address of
5007 * the packet first byte.
5008 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
5009 * Returns 1 if succeeded, 0 if not.
5010 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005011static inline int qc_try_rm_hp(struct quic_conn *qc,
5012 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005013 unsigned char *buf, unsigned char *beg,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005014 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005015{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005016 int ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005017 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005018 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005019 struct quic_enc_level *qel;
5020 /* Only for traces. */
5021 struct quic_rx_packet *qpkt_trace;
5022
5023 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005024 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005025 /* The packet number is here. This is also the start minus
5026 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
5027 * protection.
5028 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005029 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01005030
5031 tel = quic_packet_type_enc_level(pkt->type);
5032 qel = &qc->els[tel];
5033
5034 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005035 /* Note that the following function enables us to unprotect the packet
5036 * number and its length subsequently used to decrypt the entire
5037 * packets.
5038 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005039 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
Frédéric Lécaille99897d12022-08-08 10:28:07 +02005040 qel->pktns->rx.largest_pn, pn, beg)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005041 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005042 goto out;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005043 }
5044
5045 /* The AAD includes the packet number field found at <pn>. */
5046 pkt->aad_len = pn - beg + pkt->pnl;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005047 if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
5048 TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005049 goto out;
Frédéric Lécailleffde3162022-08-08 18:41:16 +02005050 }
5051
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005052 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005053 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01005054 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01005055 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005056 /* If the packet number space has been discarded, this packet
5057 * will be not parsed.
5058 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005059 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005060 goto out;
5061 }
5062
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005063 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005064 pkt->pn_offset = pn - beg;
Frédéric Lécaillea2d8ad22022-08-23 17:45:52 +02005065 LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02005066 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005067 }
5068
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005069 *el = qel;
5070 /* No reference counter incrementation here!!! */
5071 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
5072 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
5073 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
5074 b_add(&qc->rx.buf, pkt->len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005075
5076 ret = 1;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005077 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005078 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005079 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005080}
5081
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005082/* 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 +02005083 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
5084 * of this packet into <*version>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005085 */
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005086static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
5087 unsigned char **buf, const unsigned char *end,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005088 int *long_header, uint32_t *version)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005089{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005090 int ret = 0;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005091 const unsigned char byte0 = **buf;
5092
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005093 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
5094
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005095 (*buf)++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005096 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005097 unsigned char type =
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005098 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005099
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005100 *long_header = 1;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005101 /* Version */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005102 if (!quic_read_uint32(version, (const unsigned char **)buf, end)) {
5103 TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
5104 goto out;
5105 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005106
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005107 if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005108 pkt->type = type;
5109 }
5110 else {
5111 switch (type) {
5112 case 0:
5113 pkt->type = QUIC_PACKET_TYPE_RETRY;
5114 break;
5115 case 1:
5116 pkt->type = QUIC_PACKET_TYPE_INITIAL;
5117 break;
5118 case 2:
5119 pkt->type = QUIC_PACKET_TYPE_0RTT;
5120 break;
5121 case 3:
5122 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
5123 break;
5124 }
5125 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005126 }
5127 else {
5128 pkt->type = QUIC_PACKET_TYPE_SHORT;
5129 *long_header = 0;
5130 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005131
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005132 ret = 1;
5133 out:
5134 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005135 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005136}
5137
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005138/* Return the QUIC version (quic_version struct) with <version> as version number
5139 * if supported or NULL if not.
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005140 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005141static inline const struct quic_version *qc_supported_version(uint32_t version)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005142{
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005143 int i;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005144
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005145 for (i = 0; i < quic_versions_nb; i++)
5146 if (quic_versions[i].num == version)
5147 return &quic_versions[i];
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005148
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005149 return NULL;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005150}
5151
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005152/*
5153 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
5154 * address <addr>.
5155 * Implementation of RFC9000 6. Version Negotiation
5156 *
5157 * TODO implement a rate-limiting sending of Version Negotiation packets
5158 *
5159 * Returns 0 on success else non-zero
5160 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005161static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
5162 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005163{
5164 char buf[256];
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005165 int ret = 0, i = 0, j;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005166 uint32_t version;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005167 const socklen_t addrlen = get_addr_len(addr);
5168
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005169 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005170 /*
5171 * header form
5172 * long header, fixed bit to 0 for Version Negotiation
5173 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005174 /* TODO: RAND_bytes() should be replaced? */
5175 if (RAND_bytes((unsigned char *)buf, 1) != 1) {
5176 TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
5177 goto out;
5178 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005179
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01005180 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005181 /* null version for Version Negotiation */
5182 buf[i++] = '\x00';
5183 buf[i++] = '\x00';
5184 buf[i++] = '\x00';
5185 buf[i++] = '\x00';
5186
5187 /* source connection id */
5188 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005189 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005190 i += pkt->scid.len;
5191
5192 /* destination connection id */
5193 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01005194 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005195 i += pkt->dcid.len;
5196
5197 /* supported version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005198 for (j = 0; j < quic_versions_nb; j++) {
5199 version = htonl(quic_versions[j].num);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005200 memcpy(&buf[i], &version, sizeof(version));
5201 i += sizeof(version);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005202 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005203
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005204 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005205 goto out;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005206
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005207 ret = 1;
5208 out:
5209 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5210 return !ret;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005211}
5212
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005213/* Send a stateless reset packet depending on <pkt> RX packet information
5214 * from <fd> UDP socket to <dst>
5215 * Return 1 if succeeded, 0 if not.
5216 */
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005217static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005218 struct quic_rx_packet *rxpkt)
5219{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005220 int ret = 0, pktlen, rndlen;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005221 unsigned char pkt[64];
5222 const socklen_t addrlen = get_addr_len(dstaddr);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005223 struct proxy *prx;
5224 struct quic_counters *prx_counters;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005225
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005226 TRACE_ENTER(QUIC_EV_STATELESS_RST);
5227
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005228 prx = l->bind_conf->frontend;
5229 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005230 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
5231 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
5232 * Reset is difficult to distinguish from other packets if the recipient requires
5233 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
5234 * that all packets it sends are at least 22 bytes longer than the minimum
5235 * connection ID length that it requests the peer to include in its packets,
5236 * adding PADDING frames as necessary. This ensures that any Stateless Reset
5237 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
5238 * An endpoint that sends a Stateless Reset in response to a packet that is
5239 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
5240 * than the packet it responds to.
5241 */
5242
5243 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
5244 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
5245 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
5246 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005247
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005248 /* Put a header of random bytes */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005249 /* TODO: RAND_bytes() should be replaced */
5250 if (RAND_bytes(pkt, rndlen) != 1) {
5251 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
5252 goto leave;
5253 }
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005254
5255 /* Clear the most significant bit, and set the second one */
5256 *pkt = (*pkt & ~0x80) | 0x40;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005257 if (!quic_stateless_reset_token_cpy(NULL, pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005258 rxpkt->dcid.data, rxpkt->dcid.len))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005259 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005260
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005261 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005262 goto leave;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005263
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005264 ret = 1;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005265 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005266 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005267 leave:
5268 TRACE_LEAVE(QUIC_EV_STATELESS_RST);
5269 return ret;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005270}
5271
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005272/* QUIC server only function.
5273 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005274 * This is the responsibility of the caller to check <aad> size is big enough
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005275 * to contain these data.
5276 * Return the number of bytes copied to <aad>.
5277 */
5278static int quic_generate_retry_token_aad(unsigned char *aad,
5279 uint32_t version,
5280 const struct quic_cid *cid,
5281 const struct sockaddr_storage *addr)
5282{
5283 unsigned char *p;
5284
5285 p = aad;
5286 memcpy(p, &version, sizeof version);
5287 p += sizeof version;
5288 p += quic_saddr_cpy(p, addr);
5289 memcpy(p, cid->data, cid->len);
5290 p += cid->len;
5291
5292 return p - aad;
5293}
5294
5295/* QUIC server only function.
5296 * Generate the token to be used in Retry packets. The token is written to
5297 * <buf> whith <len> as length. <odcid> is the original destination connection
5298 * ID and <dcid> is our side destination connection ID (or client source
5299 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005300 * Returns the length of the encoded token or 0 on error.
5301 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005302static int quic_generate_retry_token(unsigned char *buf, size_t len,
5303 const uint32_t version,
5304 const struct quic_cid *odcid,
5305 const struct quic_cid *dcid,
5306 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005307{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005308 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005309 unsigned char *p;
5310 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5311 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5312 size_t aadlen;
5313 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
5314 unsigned char key[QUIC_TLS_KEY_LEN];
5315 unsigned char iv[QUIC_TLS_IV_LEN];
5316 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5317 size_t seclen = strlen(global.cluster_secret);
5318 EVP_CIPHER_CTX *ctx = NULL;
5319 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5320 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005321
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005322 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5323
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005324 /* We copy the odcid into the token, prefixed by its one byte
5325 * length, the format token byte. It is followed by an AEAD TAG, and finally
5326 * the random bytes used to derive the secret to encrypt the token.
5327 */
5328 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005329 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005330
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005331 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005332 /* TODO: RAND_bytes() should be replaced */
5333 if (RAND_bytes(salt, sizeof salt) != 1) {
5334 TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005335 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005336 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005337
5338 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 +02005339 salt, sizeof salt, sec, seclen)) {
5340 TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005341 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005342 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005343
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005344 if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
5345 TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005346 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005347 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005348
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005349 /* Token build */
5350 p = buf;
5351 *p++ = QUIC_TOKEN_FMT_RETRY,
5352 *p++ = odcid->len;
5353 memcpy(p, odcid->data, odcid->len);
5354 p += odcid->len;
5355 write_u32(p, htonl(timestamp));
5356 p += sizeof timestamp;
5357
5358 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005359 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv)) {
5360 TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005361 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005362 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005363
5364 p += QUIC_TLS_TAG_LEN;
5365 memcpy(p, salt, sizeof salt);
5366 p += sizeof salt;
5367 EVP_CIPHER_CTX_free(ctx);
5368
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005369 ret = p - buf;
5370 leave:
5371 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5372 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005373
5374 err:
5375 if (ctx)
5376 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005377 goto leave;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005378}
5379
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005380/* QUIC server only function.
5381 * Check the validity of the Retry token from <token> buffer with <tokenlen>
5382 * as length. If valid, the ODCID of <qc> QUIC connection will be put
5383 * into <odcid> connection ID. <dcid> is our side destination connection ID
5384 * of client source connection ID.
5385 * Return 1 if succeeded, 0 if not.
5386 */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005387static int quic_retry_token_check(const unsigned char *token, size_t tokenlen,
5388 const struct quic_version *qv,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005389 struct quic_cid *odcid,
5390 const struct quic_cid *dcid,
5391 struct quic_conn *qc,
5392 struct sockaddr_storage *addr)
5393{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005394 int ret = 0;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005395 unsigned char buf[128];
5396 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
5397 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
5398 size_t aadlen;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005399 const unsigned char *salt;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005400 unsigned char key[QUIC_TLS_KEY_LEN];
5401 unsigned char iv[QUIC_TLS_IV_LEN];
5402 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
5403 size_t seclen = strlen(global.cluster_secret);
5404 EVP_CIPHER_CTX *ctx = NULL;
5405 const EVP_CIPHER *aead = EVP_aes_128_gcm();
5406
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005407 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
5408
5409 if (sizeof buf < tokenlen) {
5410 TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
5411 goto err;
5412 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005413
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005414 aadlen = quic_generate_retry_token_aad(aad, qv->num, dcid, addr);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005415 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
5416 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 +02005417 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005418 TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
5419 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005420 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005421
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005422 if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
5423 TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005424 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005425 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005426
5427 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
5428 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 +02005429 ctx, aead, key, iv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005430 TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005431 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02005432 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005433
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005434 if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
5435 TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005436 goto err;
5437 }
5438
5439 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005440
5441 ret = 1;
5442 leave:
5443 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
5444 return ret;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005445
5446 err:
5447 if (ctx)
5448 EVP_CIPHER_CTX_free(ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005449 goto leave;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005450}
5451
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005452/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
5453 * the Initial <pkt> packet.
5454 *
5455 * Returns 0 on success else non-zero.
5456 */
5457static int send_retry(int fd, struct sockaddr_storage *addr,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005458 struct quic_rx_packet *pkt, const struct quic_version *qv)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005459{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005460 int ret = 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005461 unsigned char buf[128];
5462 int i = 0, token_len;
5463 const socklen_t addrlen = get_addr_len(addr);
5464 struct quic_cid scid;
5465
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005466 TRACE_ENTER(QUIC_EV_CONN_TXPKT);
5467
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005468 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
5469 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
5470 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005471 /* version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005472 buf[i++] = *((unsigned char *)&qv->num + 3);
5473 buf[i++] = *((unsigned char *)&qv->num + 2);
5474 buf[i++] = *((unsigned char *)&qv->num + 1);
5475 buf[i++] = *(unsigned char *)&qv->num;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005476
5477 /* Use the SCID from <pkt> for Retry DCID. */
5478 buf[i++] = pkt->scid.len;
5479 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
5480 i += pkt->scid.len;
5481
5482 /* Generate a new CID to be used as SCID for the Retry packet. */
5483 scid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005484 /* TODO: RAND_bytes() should be replaced */
5485 if (RAND_bytes(scid.data, scid.len) != 1) {
5486 TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
5487 goto out;
5488 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005489
5490 buf[i++] = scid.len;
5491 memcpy(&buf[i], scid.data, scid.len);
5492 i += scid.len;
5493
5494 /* token */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005495 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005496 &pkt->dcid, &pkt->scid, addr))) {
5497 TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
5498 goto out;
5499 }
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01005500
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005501 i += token_len;
5502
5503 /* token integrity tag */
5504 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
5505 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005506 pkt->dcid.len, buf, i, qv)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005507 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5508 goto out;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005509 }
5510
5511 i += QUIC_TLS_TAG_LEN;
5512
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005513 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
5514 TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
5515 goto out;
5516 }
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005517
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005518 ret = 1;
5519 out:
5520 TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
5521 return !ret;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005522}
5523
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005524/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5525 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5526 * concatenated to the <pkt> DCID field.
5527 *
5528 * Returns the instance or NULL if not found.
5529 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005530static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005531 struct listener *l,
5532 struct sockaddr_storage *saddr)
5533{
5534 struct quic_conn *qc = NULL;
5535 struct ebmb_node *node;
5536 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005537 /* set if the quic_conn is found in the second DCID tree */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005538
5539 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005540
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005541 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5542 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5543 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5544 /* DCIDs of first packets coming from multiple clients may have
5545 * the same values. Let's distinguish them by concatenating the
5546 * socket addresses.
5547 */
5548 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005549 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005550 pkt->dcid.len + pkt->dcid.addrlen);
5551 if (node) {
5552 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5553 goto end;
5554 }
5555 }
5556
5557 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5558 * also for INITIAL/0-RTT non-first packets with the final DCID in
5559 * used.
5560 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005561 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005562 if (!node)
5563 goto end;
5564
5565 id = ebmb_entry(node, struct quic_connection_id, node);
5566 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005567
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005568 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5569 * If already done, this is a noop.
5570 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005571 if (qc)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005572 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005573
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005574 end:
5575 TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005576 return qc;
5577}
5578
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005579/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5580 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5581 * parameters of this session.
5582 * This is the responsibility of the caller to check the validity of all the
5583 * pointers passed as parameter to this function.
5584 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5585 * CO_ER_SSL_NO_MEM.
5586 */
5587static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5588 unsigned char *params, size_t params_len)
5589{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005590 int retry, ret = -1;
5591
5592 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005593
5594 retry = 1;
5595 retry:
5596 *ssl = SSL_new(ssl_ctx);
5597 if (!*ssl) {
5598 if (!retry--)
5599 goto err;
5600
5601 pool_gc(NULL);
5602 goto retry;
5603 }
5604
5605 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005606 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005607 SSL_free(*ssl);
5608 *ssl = NULL;
5609 if (!retry--)
5610 goto err;
5611
5612 pool_gc(NULL);
5613 goto retry;
5614 }
5615
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005616 ret = 0;
5617 leave:
5618 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5619 return ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005620
5621 err:
5622 qc->conn->err_code = CO_ER_SSL_NO_MEM;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005623 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005624}
5625
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005626/* Finalize <qc> QUIC connection:
5627 * - initialize the Initial QUIC TLS context for negotiated version,
5628 * - derive the secrets for this context,
5629 * - encode the transport parameters to be sent,
5630 * - set them into the TLS stack,
5631 * - initialize ->max_ack_delay and max_idle_timeout,
5632 *
5633 * MUST be called after having received the remote transport parameters.
5634 * Return 1 if succeeded, 0 if not.
5635 */
5636int qc_conn_finalize(struct quic_conn *qc, int server)
5637{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005638 int ret = 0;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005639 struct quic_transport_params *tx_tp = &qc->tx.params;
5640 struct quic_transport_params *rx_tp = &qc->rx.params;
5641 const struct quic_version *ver;
5642
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005643 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5644
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005645 if (tx_tp->version_information.negotiated_version &&
5646 tx_tp->version_information.negotiated_version != qc->original_version) {
5647 qc->negotiated_version =
5648 qc->tx.params.version_information.negotiated_version;
5649 if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
5650 qc->odcid.data, qc->odcid.len, !server))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005651 goto out;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005652
5653 ver = qc->negotiated_version;
5654 }
5655 else {
5656 ver = qc->original_version;
5657 }
5658
5659 qc->enc_params_len =
5660 quic_transport_params_encode(qc->enc_params,
5661 qc->enc_params + sizeof qc->enc_params,
5662 &qc->rx.params, ver, 1);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005663 if (!qc->enc_params_len) {
5664 TRACE_ERROR("quic_transport_params_encode() failed", QUIC_EV_CONN_TXPKT);
5665 goto out;
5666 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005667
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005668 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len)) {
5669 TRACE_ERROR("SSL_set_quic_transport_params() failed", QUIC_EV_CONN_TXPKT);
5670 goto out;
5671 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005672
5673 if (tx_tp->max_ack_delay)
5674 qc->max_ack_delay = tx_tp->max_ack_delay;
5675
5676 if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout)
5677 qc->max_idle_timeout =
5678 QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5679 else
5680 qc->max_idle_timeout =
5681 QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5682
5683 TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp);
5684
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005685 ret = 1;
5686 out:
5687 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5688 return ret;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005689}
5690
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005691/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5692 * used to process <qc> received packets. The allocated context is stored in
5693 * <qc.xprt_ctx>.
5694 *
5695 * Returns 0 on success else non-zero.
5696 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005697static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005698{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005699 int ret = 0;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005700 struct bind_conf *bc = qc->li->bind_conf;
5701 struct ssl_sock_ctx *ctx = NULL;
5702
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005703 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
5704
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005705 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005706 if (!ctx) {
5707 TRACE_ERROR("SSL context allocation failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005708 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005709 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005710
5711 ctx->wait_event.tasklet = tasklet_new();
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005712 if (!ctx->wait_event.tasklet) {
5713 TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005714 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005715 }
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005716
5717 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5718 ctx->wait_event.tasklet->context = ctx;
5719 ctx->wait_event.events = 0;
5720 ctx->subs = NULL;
5721 ctx->xprt_ctx = NULL;
5722 ctx->qc = qc;
5723
5724 /* Set tasklet tid based on the SCID selected by us for this
5725 * connection. The upper layer will also be binded on the same thread.
5726 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005727 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005728
5729 if (qc_is_listener(qc)) {
5730 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5731 qc->enc_params, qc->enc_params_len) == -1) {
5732 goto err;
5733 }
5734
5735 /* Enabling 0-RTT */
5736 if (bc->ssl_conf.early_data)
5737 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5738
5739 SSL_set_accept_state(ctx->ssl);
5740 }
5741
5742 ctx->xprt = xprt_get(XPRT_QUIC);
5743
5744 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005745 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005746
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005747 ret = 1;
5748 leave:
5749 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
5750 return !ret;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005751
5752 err:
5753 if (ctx && ctx->wait_event.tasklet)
5754 tasklet_free(ctx->wait_event.tasklet);
5755 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005756 goto leave;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005757}
5758
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005759/* Check that all the bytes between <buf> included and <end> address
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005760 * excluded are null. This is the responsibility of the caller to
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005761 * check that there is at least one byte between <buf> end <end>.
5762 * Return 1 if this all the bytes are null, 0 if not.
5763 */
5764static inline int quic_padding_check(const unsigned char *buf,
5765 const unsigned char *end)
5766{
5767 while (buf < end && !*buf)
5768 buf++;
5769
5770 return buf == end;
5771}
5772
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005773/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5774 * end of this buffer past one byte and populate <pkt> RX packet structure
5775 * with the information collected from the packet.
5776 * This function sets ->len <pkt> field value to the end of the packet past one
5777 * byte to enable the caller to run this function again to continue to parse
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05005778 * the remaining QUIC packets carried by the datagram.
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005779 * Note that this function always sets this ->len value. If a paquet could
5780 * not be correctly found, ->len value will be set to the remaining number
5781 * bytes in the datagram to entirely consume this latter.
5782 */
5783static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5784 struct quic_rx_packet *pkt, int first_pkt,
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02005785 struct quic_dgram *dgram, struct list **tasklist_head)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005786{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005787 unsigned char *beg, *payload;
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02005788 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005789 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005790 struct proxy *prx;
5791 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005792 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005793 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005794 size_t b_cspace;
5795 struct quic_enc_level *qel;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005796 uint32_t version;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005797 const struct quic_version *qv = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005798
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005799 TRACE_ENTER(QUIC_EV_CONN_LPKT);
5800
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005801 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005802 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005803 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005804 qel = NULL;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005805 l = dgram->owner;
5806 prx = l->bind_conf->frontend;
5807 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005808 /* This ist only to please to traces and distinguish the
5809 * packet with parsed packet number from others.
5810 */
5811 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005812 if (end <= buf) {
5813 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5814 goto drop;
5815 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005816
5817 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005818 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005819 if (!first_pkt && quic_padding_check(buf, end)) {
5820 /* Some browsers may pad the remaining datagram space with null bytes.
5821 * That is what we called add padding out of QUIC packets. Such
5822 * datagrams must be considered as valid. But we can only consume
5823 * the remaining space.
5824 */
5825 pkt->len = end - buf;
5826 goto drop_no_conn;
5827 }
5828
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005829 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005830 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005831 }
5832
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005833 /* Header form */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005834 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005835 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5836 goto drop;
5837 }
5838
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005839 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005840 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005841 struct quic_cid odcid;
Frédéric Lécaille7629f5d2022-08-11 18:54:26 +02005842 const struct quic_cid *token_odcid = NULL; // ODCID received from client token
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005843
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005844 TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005845 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005846 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005847 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005848 }
5849
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005850 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5851 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005852 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005853 }
5854 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5855 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005856 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02005857 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
Frédéric Lécaillee7df68a2022-08-05 09:34:44 +02005858 goto drop;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005859 }
5860
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005861 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5862 * they must have the same DCID.
5863 */
5864 if (!first_pkt &&
5865 (pkt->dcid.len != dgram->dcid_len ||
5866 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5867 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005868 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005869 }
5870
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005871 /* Retry of Version Negotiation packets are only sent by servers */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005872 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005873 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005874 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005875 }
5876
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005877 /* RFC9000 6. Version Negotiation */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005878 qv = qc_supported_version(version);
5879 if (!qv) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005880 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005881 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02005882 TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005883 goto err;
5884 }
5885
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005886 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005887 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005888 }
5889
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005890 /* For Initial packets, and for servers (QUIC clients connections),
5891 * there is no Initial connection IDs storage.
5892 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005893 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005894 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005895
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005896 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5897 end - buf < token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005898 TRACE_PROTO("Packet dropped",
5899 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005900 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005901 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005902
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005903 /* TODO Retry should be automatically activated if
5904 * suspect network usage is detected.
5905 */
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005906 if (global.cluster_secret && !token_len) {
5907 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
5908 TRACE_PROTO("Initial without token, sending retry",
5909 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
5910 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
5911 TRACE_PROTO("Error during Retry generation",
5912 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005913 goto err;
5914 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005915
5916 HA_ATOMIC_INC(&prx_counters->retry_sent);
5917 goto err;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005918 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005919 }
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005920 else if (!global.cluster_secret && token_len) {
5921 /* Impossible case: a token was received without configured
5922 * cluster secret.
5923 */
5924 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
5925 NULL, NULL, NULL, qv);
5926 goto drop;
5927 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005928
5929 pkt->token = buf;
5930 pkt->token_len = token_len;
5931 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005932 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005933 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005934 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005935 TRACE_PROTO("Packet dropped",
5936 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005937 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005938 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005939 }
5940
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005941 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5942 end - buf < len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005943 TRACE_PROTO("Packet dropped",
5944 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005945 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005946 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005947
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005948 payload = buf;
5949 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005950 if (drop_no_conn)
5951 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005952
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005953 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005954 if (global.cluster_secret && pkt->token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005955 if (*pkt->token == QUIC_TOKEN_FMT_RETRY) {
5956 const struct quic_version *ver = qc ? qc->original_version : qv;
5957 if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid,
5958 &pkt->scid, qc, &dgram->saddr)) {
5959 HA_ATOMIC_INC(&prx_counters->retry_error);
5960 TRACE_PROTO("Wrong retry token",
5961 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5962 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
5963 * with an INVALID_TOKEN error.
5964 */
5965 goto drop;
5966 }
5967
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02005968 token_odcid = &odcid;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005969 HA_ATOMIC_INC(&prx_counters->retry_validated);
5970 }
5971 else {
5972 /* TODO: New token check */
5973 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5974 goto drop;
5975 }
5976 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005977 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005978 int ipv4;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005979
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005980 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005981 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005982 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005983 }
5984
Willy Tarreau787e92a2022-05-20 16:06:01 +02005985 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 +02005986 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005987 TRACE_PROTO("Initial without token, sending retry",
5988 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005989 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille59507de2022-08-11 12:14:07 +02005990 TRACE_ERROR("Error during Retry generation",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005991 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005992 goto err;
5993 }
5994
5995 HA_ATOMIC_INC(&prx_counters->retry_sent);
5996 goto err;
5997 }
5998
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005999 /* RFC 9000 7.2. Negotiating Connection IDs:
6000 * When an Initial packet is sent by a client that has not previously
6001 * received an Initial or Retry packet from the server, the client
6002 * populates the Destination Connection ID field with an unpredictable
6003 * value. This Destination Connection ID MUST be at least 8 bytes in length.
6004 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01006005 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006006 TRACE_PROTO("dropped packet",
6007 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006008 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01006009 }
6010
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006011 pkt->saddr = dgram->saddr;
6012 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006013
Frédéric Lécaillee9325e92022-08-11 17:24:38 +02006014 qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, token_odcid,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02006015 &pkt->saddr, 1, !!pkt->token_len, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02006016 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006017 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006018
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006019 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02006020 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02006021 ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
6022 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006023 }
Frédéric Lécaille560ddfa2022-08-24 18:59:23 +02006024
6025 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006026 }
6027 else {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006028 TRACE_PROTO("short header packet received", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006029 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006030 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006031 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006032 }
6033
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006034 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006035 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006036
6037 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6038 * they must have the same DCID.
6039 */
6040 if (!first_pkt &&
6041 (pkt->dcid.len != dgram->dcid_len ||
6042 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
6043 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006044 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006045 }
6046
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006047 buf += QUIC_HAP_CID_LEN;
6048
6049 /* A short packet is the last one of a UDP datagram. */
6050 payload = buf;
6051 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006052
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006053 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006054 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006055 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006056 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02006057 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006058 TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006059 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01006060 }
6061
Frédéric Lécaille8370c932021-11-08 17:01:46 +01006062 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006063 }
6064
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006065 if (qc->flags & QUIC_FL_CONN_CLOSING) {
6066 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
6067 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
6068 qc->nb_pkt_for_cc++;
6069 qc->nb_pkt_since_cc = 0;
6070 }
6071 /* Skip the entire datagram */
6072 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006073 TRACE_STATE("Closing state connection",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006074 QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006075 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006076 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006077
6078 /* When multiple QUIC packets are coalesced on the same UDP datagram,
6079 * they must have the same DCID.
6080 *
6081 * This check must be done after the final update to pkt.len to
6082 * properly drop the packet on failure.
6083 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006084 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006085 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006086 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006087 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006088 /* Reset the anti-amplification bit. It will be set again
6089 * when sending the next packet if reached again.
6090 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006091 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
6092 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006093 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006094 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01006095
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006096 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006097
Amaury Denoyelle72d86502022-07-13 15:08:23 +02006098 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006099 TRACE_PROTO("Connection error",
6100 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006101 goto out;
6102 }
6103
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006104 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006105 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006106 b_cspace = b_contig_space(&qc->rx.buf);
6107 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006108 /* Do not consume buf if space not at the end. */
6109 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006110 TRACE_PROTO("Packet dropped",
6111 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006112 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6113 goto drop_no_conn;
Amaury Denoyelle80d05722022-05-16 18:13:56 +02006114 }
6115
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006116 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01006117 if (b_cspace) {
6118 b_putchr(&qc->rx.buf, 0x00);
6119 b_cspace--;
6120 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006121 b_add(&qc->rx.buf, b_cspace);
6122 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006123 TRACE_PROTO("Too big packet",
6124 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaille45a16292022-06-29 12:03:34 +02006125 HA_ATOMIC_INC(&prx_counters->dropped_pkt_bufoverrun);
6126 goto drop_no_conn;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006127 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006128 }
6129
Frédéric Lécaille99897d12022-08-08 10:28:07 +02006130 if (!qc_try_rm_hp(qc, pkt, payload, beg, &qel)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006131 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006132 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02006133 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006134
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006135 TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006136 if (pkt->aad_len)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006137 qc_pkt_insert(qc, pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006138 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02006139 /* Wake up the connection packet handler task from here only if all
6140 * the contexts have been initialized, especially the mux context
6141 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
6142 * will start it if these contexts for the connection are not already
6143 * initialized.
6144 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006145 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006146 if (conn_ctx)
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02006147 *tasklist_head = tasklet_wakeup_after(*tasklist_head, conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02006148
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006149 drop_no_conn:
6150 if (drop_no_conn)
6151 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006152 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006153
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006154 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006155
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02006156 drop:
6157 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006158 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006159 /* Wakeup the I/O handler callback if the PTO timer must be armed.
6160 * This cannot be done by this thread.
6161 */
6162 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006163 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01006164 if (conn_ctx && conn_ctx->wait_event.tasklet)
6165 tasklet_wakeup(conn_ctx->wait_event.tasklet);
6166 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01006167 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01006168 if (!pkt->len)
6169 pkt->len = end - beg;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006170 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006171}
6172
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006173/* This function builds into <buf> buffer a QUIC long packet header.
6174 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006175 */
6176static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006177 int type, size_t pn_len,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006178 struct quic_conn *qc, const struct quic_version *ver)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006179{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006180 int ret = 0;
6181
6182 TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
6183
6184 if (end - *buf < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
6185 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6186 goto leave;
6187 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006188
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006189 type = quic_pkt_type(type, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006190 /* #0 byte flags */
6191 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
6192 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
6193 /* Version */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006194 quic_write_uint32(buf, end, ver->num);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006195 *(*buf)++ = qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006196 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006197 if (qc->dcid.len) {
6198 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6199 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006200 }
6201 /* Source connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006202 *(*buf)++ = qc->scid.len;
6203 if (qc->scid.len) {
6204 memcpy(*buf, qc->scid.data, qc->scid.len);
6205 *buf += qc->scid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006206 }
6207
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006208 ret = 1;
6209 leave:
6210 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
6211 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006212}
6213
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006214/* This function builds into <buf> buffer a QUIC short packet header.
6215 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006216 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006217static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006218 size_t pn_len, struct quic_conn *qc,
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006219 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006220{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006221 int ret = 0;
6222
6223 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6224
6225 if (end - *buf < 1 + qc->dcid.len) {
6226 TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
6227 goto leave;
6228 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006229
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006230 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01006231 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
6232 ((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 +01006233 /* Destination connection ID */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006234 if (qc->dcid.len) {
6235 memcpy(*buf, qc->dcid.data, qc->dcid.len);
6236 *buf += qc->dcid.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006237 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006238
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006239 ret = 1;
6240 leave:
6241 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6242 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006243}
6244
6245/* Apply QUIC header protection to the packet with <buf> as first byte address,
6246 * <pn> as address of the Packet number field, <pnlen> being this field length
6247 * with <aead> as AEAD cipher and <key> as secret key.
6248 * Returns 1 if succeeded or 0 if failed.
6249 */
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006250static int quic_apply_header_protection(struct quic_conn *qc, unsigned char *buf,
6251 unsigned char *pn, size_t pnlen,
6252 struct quic_tls_ctx *tls_ctx)
6253
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006254{
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006255 int i, ret = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006256 /* We need an IV of at least 5 bytes: one byte for bytes #0
6257 * and at most 4 bytes for the packet number
6258 */
6259 unsigned char mask[5] = {0};
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006260 EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006261
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006262 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6263
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006264 if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
6265 TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006266 goto out;
6267 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006268
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006269 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
6270 for (i = 0; i < pnlen; i++)
6271 pn[i] ^= mask[i + 1];
6272
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006273 ret = 1;
6274 out:
6275 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006276 return ret;
6277}
6278
6279/* Reduce the encoded size of <ack_frm> ACK frame removing the last
6280 * ACK ranges if needed to a value below <limit> in bytes.
6281 * Return 1 if succeeded, 0 if not.
6282 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006283static int quic_ack_frm_reduce_sz(struct quic_conn *qc,
6284 struct quic_frame *ack_frm, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006285{
6286 size_t room, ack_delay_sz;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006287 int ret = 0;
6288
6289 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006290
6291 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
6292 /* A frame is made of 1 byte for the frame type. */
6293 room = limit - ack_delay_sz - 1;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006294 if (!quic_rm_last_ack_ranges(qc, ack_frm->tx_ack.arngs, room))
6295 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006296
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006297 ret = 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
6298 leave:
6299 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6300 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006301}
6302
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006303/* Prepare into <outlist> as most as possible ack-eliciting frame from their
6304 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
6305 * with <room> as available room, and <*len> the packet Length field initialized
6306 * with the number of bytes already present in this buffer which must be taken
6307 * into an account for the Length packet field value. <headlen> is the number of
6308 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006309 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006310 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006311 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006312 * 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 +01006313 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006314static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006315 size_t room, size_t *len, size_t headlen,
6316 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006317 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006318{
Frédéric Lécailleea604992020-12-24 13:01:37 +01006319 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006320 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006321
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006322 TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
6323
Frédéric Lécailleea604992020-12-24 13:01:37 +01006324 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006325 if (*len > room)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006326 goto leave;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006327
Frédéric Lécailleea604992020-12-24 13:01:37 +01006328 /* If we are not probing we must take into an account the congestion
6329 * control window.
6330 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006331 if (!qel->pktns->tx.pto_probe) {
6332 size_t remain = quic_path_prep_data(qc->path);
6333
6334 if (headlen > remain)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006335 goto leave;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01006336
6337 room = QUIC_MIN(room, remain - headlen);
6338 }
6339
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006340 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006341 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006342
6343 /* NOTE: switch/case block inside a loop, a successful status must be
6344 * returned by this function only if at least one frame could be built
6345 * in the switch/case block.
6346 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006347 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006348 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006349 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006350
Frédéric Lécailleea604992020-12-24 13:01:37 +01006351 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006352 break;
6353
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006354 switch (cf->type) {
6355 case QUIC_FT_CRYPTO:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006356 TRACE_DEVEL(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006357 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006358 /* Compute the length of this CRYPTO frame header */
6359 hlen = 1 + quic_int_getsize(cf->crypto.offset);
6360 /* Compute the data length of this CRyPTO frame. */
6361 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006362 TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006363 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006364 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006365 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006366
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006367 /* CRYPTO frame length. */
6368 flen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006369 TRACE_DEVEL(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006370 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006371 /* Add the CRYPTO data length and its encoded length to the packet
6372 * length and the length of this length.
6373 */
6374 *len += flen;
6375 room -= flen;
6376 if (dlen == cf->crypto.len) {
6377 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006378 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006379 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006380 }
6381 else {
6382 struct quic_frame *new_cf;
6383
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006384 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006385 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006386 TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006387 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006388 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006389
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006390 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006391 new_cf->type = QUIC_FT_CRYPTO;
6392 new_cf->crypto.len = dlen;
6393 new_cf->crypto.offset = cf->crypto.offset;
6394 new_cf->crypto.qel = qel;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006395 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6396 if (cf->origin) {
6397 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6398 /* This <cf> frame was duplicated */
6399 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6400 new_cf->origin = cf->origin;
6401 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006402 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006403 /* Consume <dlen> bytes of the current frame. */
6404 cf->crypto.len -= dlen;
6405 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006406 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006407 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006408
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006409 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006410 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
6411 struct eb64_node *node = NULL;
6412 struct qc_stream_desc *stream_desc = NULL;
6413 struct quic_stream *strm = &cf->stream;
6414
6415 /* As this frame has been already lost, ensure the stream is always
6416 * available or the range of this frame is not consumed before
6417 * resending it.
6418 */
6419 node = eb64_lookup(&qc->streams_by_id, strm->id);
6420 if (!node) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006421 TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006422 LIST_DELETE(&cf->list);
6423 pool_free(pool_head_quic_frame, cf);
6424 continue;
6425 }
6426
6427 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
6428 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006429 TRACE_DEVEL("ignored frame frame in already acked range",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006430 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006431 LIST_DELETE(&cf->list);
6432 pool_free(pool_head_quic_frame, cf);
6433 continue;
6434 }
6435 else if (strm->offset.key < stream_desc->ack_offset) {
6436 strm->offset.key = stream_desc->ack_offset;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006437 TRACE_DEVEL("updated partially acked frame",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02006438 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02006439 }
6440 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006441 /* Note that these frames are accepted in short packets only without
6442 * "Length" packet field. Here, <*len> is used only to compute the
6443 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01006444 *
6445 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006446 * excepting the variable ones. Note that +1 is for the type of this frame.
6447 */
6448 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006449 ((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 +02006450 /* Compute the data length of this STREAM frame. */
6451 avail_room = room - hlen - *len;
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006452 if ((ssize_t)avail_room <= 0)
6453 continue;
6454
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006455 TRACE_DEVEL(" New STREAM frame build (room, len)",
Frédéric Lécailleadc76412022-08-08 16:09:46 +02006456 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006457 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
6458 dlen = max_available_room(avail_room, &dlen_sz);
6459 if (dlen > cf->stream.len) {
6460 dlen = cf->stream.len;
6461 }
6462 dlen_sz = quic_int_getsize(dlen);
6463 flen = hlen + dlen_sz + dlen;
6464 }
6465 else {
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02006466 dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006467 flen = hlen + dlen;
6468 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006469 TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006470 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006471 TRACE_DEVEL(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006472 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006473 /* Add the STREAM data length and its encoded length to the packet
6474 * length and the length of this length.
6475 */
6476 *len += flen;
6477 room -= flen;
6478 if (dlen == cf->stream.len) {
6479 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006480 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006481 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006482
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006483 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006484 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006485 qcc_streams_sent_done(cf->stream.stream->ctx,
6486 cf->stream.len,
6487 cf->stream.offset.key);
6488 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006489 }
6490 else {
6491 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006492 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006493
6494 new_cf = pool_zalloc(pool_head_quic_frame);
6495 if (!new_cf) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006496 TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006497 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006498 }
6499
Frédéric Lécailleb9171912022-04-21 17:32:10 +02006500 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006501 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006502 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006503 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006504 new_cf->stream.id = cf->stream.id;
6505 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
6506 new_cf->stream.offset = cf->stream.offset;
6507 new_cf->stream.len = dlen;
6508 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
6509 /* FIN bit reset */
6510 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
6511 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleb8278402022-08-19 09:32:14 +02006512 TRACE_DEVEL("splitted frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
6513 if (cf->origin) {
6514 TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
6515 /* This <cf> frame was duplicated */
6516 LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
6517 new_cf->origin = cf->origin;
6518 }
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006519 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006520 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
6521 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006522 cf_buf = b_make(b_orig(cf->stream.buf),
6523 b_size(cf->stream.buf),
6524 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006525 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02006526 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01006527 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006528
Amaury Denoyelleb29a1dc2022-08-12 14:30:04 +02006529 /* Do not notify MUX on retransmission. */
Amaury Denoyelle70467562022-08-17 16:33:13 +02006530 if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02006531 qcc_streams_sent_done(new_cf->stream.stream->ctx,
6532 new_cf->stream.len,
6533 new_cf->stream.offset.key);
6534 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02006535 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01006536
6537 /* TODO the MUX is notified about the frame sending via
6538 * previous qcc_streams_sent_done call. However, the
6539 * sending can fail later, for example if the sendto
6540 * system call returns an error. As the MUX has been
6541 * notified, the transport layer is responsible to
6542 * bufferize and resent the announced data later.
6543 */
6544
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006545 break;
6546
6547 default:
6548 flen = qc_frm_len(cf);
6549 BUG_ON(!flen);
6550 if (flen > room)
6551 continue;
6552
6553 *len += flen;
6554 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01006555 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006556 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02006557 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006558 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02006559
6560 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01006561 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006562 }
6563
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006564 leave:
6565 TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécailleea604992020-12-24 13:01:37 +01006566 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006567}
6568
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006569/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
6570 * is used as return parameter and should be zero'ed by the caller.
6571 */
6572static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
6573 struct quic_frame *out)
6574{
6575 /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
6576 *
6577 * A CONNECTION_CLOSE frame should be sent in several packets with
6578 * different encryption levels depending on the client context. This is
6579 * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
6580 * more details on how to implement it.
6581 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006582 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
6583
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006584
6585 if (qc->err.app) {
6586 if (unlikely(qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
6587 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
6588 /* RFC 9000 10.2.3. Immediate Close during the Handshake
6589 *
6590 * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
6591 * packet could expose application state or be used to alter application
6592 * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
6593 * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
6594 * Handshake packets. Otherwise, information about the application
6595 * state might be revealed. Endpoints MUST clear the value of the
6596 * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
6597 * converting to a CONNECTION_CLOSE of type 0x1c.
6598 */
6599 out->type = QUIC_FT_CONNECTION_CLOSE;
6600 out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
6601 out->connection_close.reason_phrase_len = 0;
6602 }
6603 else {
6604 out->type = QUIC_FT_CONNECTION_CLOSE_APP;
6605 out->connection_close.error_code = qc->err.code;
6606 }
6607 }
6608 else {
6609 out->type = QUIC_FT_CONNECTION_CLOSE;
6610 out->connection_close.error_code = qc->err.code;
6611 }
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006612 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
6613
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006614}
6615
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006616/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006617 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
6618 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006619 * filling the buffer with as much frames as possible from <frms> list of
6620 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006621 * 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 +02006622 * 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 +02006623 * having returned from this function.
6624 * 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 +01006625 * number field in this packet. <pn_len> will also have the packet number
6626 * length as value.
6627 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006628 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006629 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006630static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
6631 size_t dglen, struct quic_tx_packet *pkt,
6632 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006633 int force_ack, int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006634 struct quic_enc_level *qel, struct quic_conn *qc,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006635 const struct quic_version *ver, struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006636{
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006637 unsigned char *beg, *payload;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006638 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006639 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
6640 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006641 struct quic_frame cc_frm = { };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006642 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006643 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006644 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006645 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006646 struct quic_frame *cf;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006647 int must_ack, ret = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006648 int nb_aepkts_since_last_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006649
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006650 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
6651
Frédéric Lécailleea604992020-12-24 13:01:37 +01006652 /* Length field value with CRYPTO frames if present. */
6653 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006654 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006655 /* When not probing, and no immediate close is required, reduce the size of this
6656 * buffer to respect the congestion controller window.
6657 * 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 +01006658 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006659 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006660 size_t path_room;
6661
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006662 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006663 if (end - beg > path_room)
6664 end = beg + path_room;
6665 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006666
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006667 /* Ensure there is enough room for the TLS encryption tag and a zero token
6668 * length field if any.
6669 */
6670 if (end - pos < QUIC_TLS_TAG_LEN +
6671 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
6672 goto no_room;
6673
6674 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006675 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006676 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006677 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006678 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006679 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006680 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006681 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006682 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006683 goto no_room;
6684
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006685 /* Encode the token length (0) for an Initial packet. */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006686 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006687 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006688 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006689 /* Build an ACK frame if required. */
6690 ack_frm_len = 0;
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006691 nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
6692 must_ack = !qel->pktns->tx.pto_probe &&
6693 (force_ack || ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
6694 (LIST_ISEMPTY(frms) || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK)));
6695 if (force_ack || must_ack) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006696 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006697 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006698 ack_frm.tx_ack.arngs = arngs;
6699 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6700 qel->pktns->tx.ack_delay =
6701 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6702 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6703 }
6704 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006705 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6706 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6707 * that from here, we do not know if we will have to send a PING frame.
6708 * This will be decided after having computed the ack-eliciting frames
6709 * to be added to this packet.
6710 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006711 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 +01006712 if (!ack_frm_len)
6713 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006714 }
6715
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006716 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006717 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006718 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006719 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006720 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006721
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006722 TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006723 /* Initialize the length of the frames built below to <len>.
6724 * If any frame could be successfully built by qc_build_frms(),
6725 * we will have len_frms > len.
6726 */
6727 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006728 if (!qc_build_frms(&frm_list, frms,
6729 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006730 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006731 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006732 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6733 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006734 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006735 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006736
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006737 /* Length (of the remaining data). Must not fail because, the buffer size
6738 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6739 * for the encryption tag. It must be taken into an account for the length
6740 * of this packet.
6741 */
6742 if (len_frms)
6743 len = len_frms + QUIC_TLS_TAG_LEN;
6744 else
6745 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006746 /* CONNECTION_CLOSE frame */
6747 if (cc) {
Amaury Denoyelle0933c7b2022-07-18 14:11:50 +02006748 qc_build_cc_frm(qc, qel, &cc_frm);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006749 len += qc_frm_len(&cc_frm);
6750 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006751 add_ping_frm = 0;
6752 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006753 len_sz = quic_int_getsize(len);
6754 /* Add this packet size to <dglen> */
6755 dglen += head_len + len_sz + len;
6756 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6757 /* This is a maximum padding size */
6758 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6759 /* The length field value is of this packet is <len> + <padding_len>
6760 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006761 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006762 * sizes from <padding_len>.
6763 */
6764 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6765 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006766 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006767 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006768 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006769 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006770 add_ping_frm = 1;
6771 len += 1;
6772 }
6773 /* 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 +01006774 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006775 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6776 }
6777
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006778 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6779 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006780
6781 /* Packet number field address. */
6782 *buf_pn = pos;
6783
6784 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006785 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6786 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006787
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006788 /* payload building (ack-eliciting or not frames) */
6789 payload = pos;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006790 if (ack_frm_len) {
6791 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6792 goto no_room;
6793
6794 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006795 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006796 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006797
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006798 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006799 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillea8a60432022-08-23 11:42:48 +02006800 struct quic_frame *tmp_cf;
6801 list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02006802 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006803 ssize_t room = end - pos;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006804 TRACE_DEVEL("Not enough room", QUIC_EV_CONN_TXPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006805 qc, NULL, NULL, &room);
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006806 /* TODO: this should not have happened except if we
6807 * are limited by the congestion control.
6808 * Note that <cf> was added from <frm_list> to <frms> list by
6809 * qc_build_frms().
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006810 */
6811 LIST_DELETE(&cf->list);
6812 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006813 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006814 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006815
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006816 quic_tx_packet_refinc(pkt);
6817 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006818 }
6819 }
6820
6821 /* Build a PING frame if needed. */
6822 if (add_ping_frm) {
6823 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006824 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006825 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006826 }
6827
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006828 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006829 if (cc) {
6830 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6831 goto no_room;
6832
6833 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6834 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006835
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006836 /* Build a PADDING frame if needed. */
6837 if (padding_len) {
6838 frm.type = QUIC_FT_PADDING;
6839 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006840 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006841 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006842 }
6843
Frédéric Lécailleebb10702022-08-16 12:03:00 +02006844 if (pos == payload) {
6845 /* No payload was built because of congestion control */
6846 TRACE_DEVEL("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
6847 goto no_room;
6848 }
6849
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006850 /* If this packet is ack-eliciting and we are probing let's
6851 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006852 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006853 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6854 qel->pktns->tx.pto_probe)
6855 qel->pktns->tx.pto_probe--;
6856
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006857 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006858 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006859
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006860 ret = 1;
6861 TRACE_DEVEL("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
6862 leave:
6863 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
6864 return ret;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006865
6866 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006867 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006868 LIST_SPLICE(frms, &frm_list);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006869 TRACE_DEVEL("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
6870 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006871}
6872
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006873static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006874{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006875 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006876 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006877 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006878 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006879 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006880 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006881 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006882 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006883 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006884 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006885}
6886
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006887/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006888 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6889 * of prebuilt frames.
6890 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006891 * Return -2 if the packet could not be allocated or encrypted for any reason,
6892 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006893 * XXX NOTE XXX
6894 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6895 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6896 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006897 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006898static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6899 const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006900 struct quic_enc_level *qel,
6901 struct quic_tls_ctx *tls_ctx, struct list *frms,
6902 struct quic_conn *qc, const struct quic_version *ver,
Frédéric Lécaille77ac6f52022-06-21 15:14:59 +02006903 size_t dglen, int pkt_type, int force_ack,
6904 int padding, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006905{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006906 struct quic_tx_packet *ret_pkt = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006907 /* The pointer to the packet number field. */
6908 unsigned char *buf_pn;
6909 unsigned char *beg, *end, *payload;
6910 int64_t pn;
6911 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006912 struct quic_tx_packet *pkt;
6913
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006914 TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006915 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006916 pkt = pool_alloc(pool_head_quic_tx_packet);
6917 if (!pkt) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006918 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006919 *err = -2;
6920 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006921 }
6922
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006923 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006924 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006925 pn_len = 0;
6926 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006927
6928 pn = qel->pktns->tx.next_pn + 1;
6929 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 +02006930 force_ack, padding, cc, probe, qel, qc, ver, frms)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006931 // trace already emitted by function above
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006932 *err = -1;
6933 goto err;
6934 }
6935
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006936 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006937 payload = buf_pn + pn_len;
6938 payload_len = end - payload;
6939 aad_len = payload - beg;
6940
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006941 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006942 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006943 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006944 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006945 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006946
6947 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006948 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille86a53c52022-08-19 18:18:13 +02006949 if (!quic_apply_header_protection(qc, beg, buf_pn, pn_len, tls_ctx)) {
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006950 // trace already emitted by function above
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006951 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006952 goto err;
6953 }
6954
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006955 /* Consume a packet number */
6956 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006957 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006958 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006959 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006960 TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
6961 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006962 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6963 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006964 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006965 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006966 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006967 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006968 pkt->in_flight_len = pkt->len;
6969 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006970 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01006971 /* Always reset this flags */
6972 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006973 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
6974 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
6975 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
6976 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006977
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006978 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006979
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006980 ret_pkt = pkt;
6981 leave:
6982 TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc, ret_pkt);
6983 return ret_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006984
6985 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006986 /* TODO: what about the frames which have been built
6987 * for this packet.
6988 */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02006989 free_quic_tx_packet(qc, pkt);
6990 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006991}
6992
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006993
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006994/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6995 * event subscriber <es> is not allowed to change from a previous call as long
6996 * as at least one event is still subscribed. The <event_type> must only be a
6997 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
6998 */
6999static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7000{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007001 struct quic_conn *qc = conn->handle.qc;
7002 struct qcc *qcc = qc->qcc;
7003
7004 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007005
7006 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
7007 BUG_ON(qcc->subs && qcc->subs != es);
7008
7009 es->events |= event_type;
7010 qcc->subs = es;
7011
7012 if (event_type & SUB_RETRY_RECV)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007013 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007014
7015 if (event_type & SUB_RETRY_SEND)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007016 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7017
7018 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02007019
7020 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007021}
7022
7023/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
7024 * The <es> pointer is not allowed to differ from the one passed to the
7025 * subscribe() call. It always returns zero.
7026 */
7027static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
7028{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007029 int ret;
7030 struct quic_conn *qc = conn->handle.qc;
7031 struct qcc *qcc = qc->qcc;
7032
7033 TRACE_ENTER(QUIC_EV_CONN_SUB, qc);
7034
7035 if (event_type & SUB_RETRY_RECV)
7036 TRACE_DEVEL("unsubscribe(recv)", QUIC_EV_CONN_XPRTRECV, qc, qcc);
7037 if (event_type & SUB_RETRY_SEND)
7038 TRACE_DEVEL("unsubscribe(send)", QUIC_EV_CONN_XPRTSEND, qc, qcc);
7039
7040 ret = conn_unsubscribe(conn, xprt_ctx, event_type, es);
7041
7042 TRACE_LEAVE(QUIC_EV_CONN_SUB, qc);
7043
7044 return ret;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007045}
7046
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007047/* Store in <xprt_ctx> the context attached to <conn>.
7048 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007049 */
7050static int qc_conn_init(struct connection *conn, void **xprt_ctx)
7051{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01007052 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007053
7054 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
7055
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01007056 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007057 if (*xprt_ctx)
7058 goto out;
7059
Willy Tarreau784b8682022-04-11 14:18:10 +02007060 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007061
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007062 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01007063 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007064
7065 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007066}
7067
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007068/* Start the QUIC transport layer */
7069static int qc_xprt_start(struct connection *conn, void *ctx)
7070{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007071 int ret = 0;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007072 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02007073 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007074
Willy Tarreau784b8682022-04-11 14:18:10 +02007075 qc = conn->handle.qc;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007076 TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007077 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
7078 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02007079 /* prepare a CONNECTION_CLOSE frame */
Amaury Denoyelle57e6db72022-07-13 15:07:56 +02007080 quic_set_connection_close(qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007081 goto out;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01007082 }
7083
7084 /* mux-quic can now be considered ready. */
7085 qc->mux_state = QC_MUX_READY;
7086
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007087 tasklet_wakeup(qctx->wait_event.tasklet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007088
7089 ret = 1;
7090 out:
7091 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
7092 return ret;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007093}
7094
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007095static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
7096{
Willy Tarreau784b8682022-04-11 14:18:10 +02007097 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007098 return NULL;
7099
Willy Tarreau784b8682022-04-11 14:18:10 +02007100 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007101}
7102
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007103/* transport-layer operations for QUIC connections. */
7104static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02007105 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01007106 .subscribe = quic_conn_subscribe,
7107 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007108 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02007109 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007110 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
7111 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01007112 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02007113 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007114 .name = "QUIC",
7115};
7116
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007117static void __quic_conn_init(void)
7118{
7119 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
7120 xprt_register(XPRT_QUIC, &ssl_quic);
7121}
Willy Tarreau79367f92022-04-25 19:18:24 +02007122INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007123
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007124static void __quic_conn_deinit(void)
7125{
7126 BIO_meth_free(ha_quic_meth);
7127}
Willy Tarreau79367f92022-04-25 19:18:24 +02007128REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007129
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01007130/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
7131 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05007132 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007133 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007134struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007135{
7136 unsigned char *pos;
7137 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007138 struct quic_dghdlr *dghdlr = ctx;
7139 struct quic_dgram *dgram;
7140 int first_pkt = 1;
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007141 struct list *tasklist_head = NULL;
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007142 int max_dgrams = global.tune.maxpollevents;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007143
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007144 TRACE_ENTER(QUIC_EV_CONN_LPKT);
7145
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007146 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007147 pos = dgram->buf;
7148 end = pos + dgram->len;
7149 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007150 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007151
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007152 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007153 if (!pkt) {
7154 TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
7155 goto leave;
7156 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007157
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007158 LIST_INIT(&pkt->qc_rx_pkt_list);
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02007159 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007160 quic_rx_packet_refinc(pkt);
Frédéric Lécaille1b0707f2022-06-30 11:28:56 +02007161 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram, &tasklist_head);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007162 first_pkt = 0;
7163 pos += pkt->len;
7164 quic_rx_packet_refdec(pkt);
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02007165
7166 /* Free rejected packets */
7167 if (!pkt->refcnt) {
7168 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
7169 pool_free(pool_head_quic_rx_packet, pkt);
7170 }
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007171 } while (pos < end);
7172
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007173 /* Increasing the received bytes counter by the UDP datagram length
7174 * if this datagram could be associated to a connection.
7175 */
7176 if (dgram->qc)
7177 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01007178
7179 /* Mark this datagram as consumed */
7180 HA_ATOMIC_STORE(&dgram->buf, NULL);
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007181
7182 if (--max_dgrams <= 0)
7183 goto stop_here;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01007184 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007185
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007186 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
7187
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007188 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007189
Willy Tarreauf9d4a7d2022-08-05 08:45:56 +02007190 stop_here:
7191 /* too much work done at once, come back here later */
7192 if (!MT_LIST_ISEMPTY(&dghdlr->dgrams))
7193 tasklet_wakeup((struct tasklet *)t);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007194 leave:
7195 TRACE_LEAVE(QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01007196 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007197}
7198
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05007199/* Retrieve the DCID from a QUIC datagram or packet with <buf> as first octet.
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007200 * Returns 1 if succeeded, 0 if not.
7201 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02007202int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
7203 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007204{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007205 int ret = 0, long_header;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007206 size_t minlen, skip;
7207
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007208 TRACE_ENTER(QUIC_EV_CONN_RXPKT);
7209
7210 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
7211 TRACE_PROTO("fixed bit not set", QUIC_EV_CONN_RXPKT);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007212 goto err;
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007213 }
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007214
7215 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02007216 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
7217 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007218 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Willy Tarreauaf5138f2022-08-05 10:09:32 +02007219 if (end - buf < minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007220 goto err;
7221
7222 buf += skip;
7223 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
7224 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
7225 goto err;
7226
7227 *dcid = buf;
7228
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007229 ret = 1;
7230 leave:
7231 TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
7232 return ret;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007233
7234 err:
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007235 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_RXPKT);
7236 goto leave;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01007237}
7238
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007239/* Notify the MUX layer if alive about an imminent close of <qc>. */
7240void qc_notify_close(struct quic_conn *qc)
7241{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007242 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
7243
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007244 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007245 goto leave;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007246
7247 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007248 /* wake up the MUX */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007249 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake) {
7250 TRACE_STATE("connection closure notidfied to mux",
7251 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007252 qc->conn->mux->wake(qc->conn);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02007253 }
7254 else
7255 TRACE_STATE("connection closure not notidfied to mux",
7256 QUIC_FL_CONN_NOTIFY_CLOSE, qc);
7257 leave:
7258 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02007259}
7260
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007261/*
7262 * Local variables:
7263 * c-indent-level: 8
7264 * c-basic-offset: 8
7265 * End:
7266 */