blob: 8c08a189f533efc30b3f5f1fed9d5c58e3b591c4 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
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>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
25#include <haproxy/buf-t.h>
26#include <haproxy/compat.h>
27#include <haproxy/api.h>
28#include <haproxy/debug.h>
29#include <haproxy/tools.h>
30#include <haproxy/ticks.h>
31#include <haproxy/time.h>
32
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>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010038#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010039#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/pipe.h>
41#include <haproxy/proxy.h>
42#include <haproxy/quic_cc.h>
43#include <haproxy/quic_frame.h>
44#include <haproxy/quic_loss.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020045#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010046#include <haproxy/quic_tls.h>
47#include <haproxy/ssl_sock.h>
48#include <haproxy/stream_interface.h>
49#include <haproxy/task.h>
50#include <haproxy/trace.h>
51#include <haproxy/xprt_quic.h>
52
53struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020054 .max_udp_payload_size = QUIC_DFLT_MAX_UDP_PAYLOAD_SIZE,
55 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
56 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010057};
58
59/* trace source and events */
60static void quic_trace(enum trace_level level, uint64_t mask, \
61 const struct trace_source *src,
62 const struct ist where, const struct ist func,
63 const void *a1, const void *a2, const void *a3, const void *a4);
64
65static const struct trace_event quic_trace_events[] = {
66 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
67 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
68 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
69 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
70 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
71 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
72 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050073 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010074 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
75 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
76 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
77 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
78 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
79 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
80 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
81 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
82 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
83 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
84 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
85 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
86 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
87 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
88 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
89 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
90 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
91 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
92 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010093 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
94 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
95 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
96 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
97 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
98 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +010099 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100100 { /* end */ }
101};
102
103static const struct name_desc quic_trace_lockon_args[4] = {
104 /* arg1 */ { /* already used by the connection */ },
105 /* arg2 */ { .name="quic", .desc="QUIC transport" },
106 /* arg3 */ { },
107 /* arg4 */ { }
108};
109
110static const struct name_desc quic_trace_decoding[] = {
111#define QUIC_VERB_CLEAN 1
112 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
113 { /* end */ }
114};
115
116
117struct trace_source trace_quic = {
118 .name = IST("quic"),
119 .desc = "QUIC xprt",
120 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
121 .default_cb = quic_trace,
122 .known_events = quic_trace_events,
123 .lockon_args = quic_trace_lockon_args,
124 .decoding = quic_trace_decoding,
125 .report_events = ~0, /* report everything by default */
126};
127
128#define TRACE_SOURCE &trace_quic
129INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
130
131static BIO_METHOD *ha_quic_meth;
132
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100133DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200134 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100135DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100136DECLARE_POOL(pool_head_quic_connection_id,
137 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100138DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100139DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100140DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100141DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100142DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200143DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100144DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100145
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +0200146static struct quic_tx_packet *qc_build_hdshk_pkt(unsigned char **pos, const unsigned char *buf_end,
147 struct quic_conn *qc, int pkt_type,
148 struct quic_enc_level *qel, int *err);
149int qc_prep_phdshk_pkts(struct qring *qr, struct quic_conn *qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100150
151/* Add traces to <buf> depending on <frm> TX frame type. */
152static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200153 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100154{
155 switch (frm->type) {
156 case QUIC_FT_CRYPTO:
157 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
158 (unsigned long long)frm->crypto.offset,
159 (unsigned long long)frm->crypto.len);
160 break;
161 default:
162 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
163 }
164}
165
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100166/* Only for debug purpose */
167struct enc_debug_info {
168 unsigned char *payload;
169 size_t payload_len;
170 unsigned char *aad;
171 size_t aad_len;
172 uint64_t pn;
173};
174
175/* Initializes a enc_debug_info struct (only for debug purpose) */
176static inline void enc_debug_info_init(struct enc_debug_info *edi,
177 unsigned char *payload, size_t payload_len,
178 unsigned char *aad, size_t aad_len, uint64_t pn)
179{
180 edi->payload = payload;
181 edi->payload_len = payload_len;
182 edi->aad = aad;
183 edi->aad_len = aad_len;
184 edi->pn = pn;
185}
186
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100187/* Trace callback for QUIC.
188 * These traces always expect that arg1, if non-null, is of type connection.
189 */
190static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
191 const struct ist where, const struct ist func,
192 const void *a1, const void *a2, const void *a3, const void *a4)
193{
194 const struct connection *conn = a1;
195
196 if (conn) {
197 struct quic_tls_secrets *secs;
198 struct quic_conn *qc;
199
200 qc = conn->qc;
201 chunk_appendf(&trace_buf, " : conn@%p", conn);
202 if ((mask & QUIC_EV_CONN_INIT) && qc) {
203 chunk_appendf(&trace_buf, "\n odcid");
204 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100205 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100206 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100207 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100208 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100209 }
210
211 if (mask & QUIC_EV_CONN_ADDDATA) {
212 const enum ssl_encryption_level_t *level = a2;
213 const size_t *len = a3;
214
215 if (level) {
216 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
217
218 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
219 }
220 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100221 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100222 }
223 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
224 /* Initial read & write secrets. */
225 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
226 const unsigned char *rx_sec = a2;
227 const unsigned char *tx_sec = a3;
228
229 secs = &qc->els[level].tls_ctx.rx;
230 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
231 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
232 if (rx_sec)
233 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
234 quic_tls_keys_hexdump(&trace_buf, secs);
235 }
236 secs = &qc->els[level].tls_ctx.tx;
237 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
238 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
239 if (tx_sec)
240 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
241 quic_tls_keys_hexdump(&trace_buf, secs);
242 }
243 }
244 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
245 const enum ssl_encryption_level_t *level = a2;
246 const unsigned char *secret = a3;
247 const size_t *secret_len = a4;
248
249 if (level) {
250 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
251
252 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
253 if (secret && secret_len)
254 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
255 secs = &qc->els[lvl].tls_ctx.rx;
256 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
257 quic_tls_keys_hexdump(&trace_buf, secs);
258 }
259 }
260
261 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
262 const enum ssl_encryption_level_t *level = a2;
263 const unsigned char *secret = a3;
264 const size_t *secret_len = a4;
265
266 if (level) {
267 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
268
269 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
270 if (secret && secret_len)
271 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
272 secs = &qc->els[lvl].tls_ctx.tx;
273 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
274 quic_tls_keys_hexdump(&trace_buf, secs);
275 }
276
277 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100278
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100279 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100280 const struct quic_tx_packet *pkt = a2;
281 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100282 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100283
284 if (qel) {
285 struct quic_pktns *pktns;
286
287 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100288 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
289 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100291 (unsigned long long)qc->path->cwnd,
292 (unsigned long long)qc->path->prep_in_flight,
293 (unsigned long long)qc->path->in_flight,
294 (unsigned long long)pktns->tx.in_flight,
295 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100296 }
297 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200298 const struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100299 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
300 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
301 list_for_each_entry(frm, &pkt->frms, list)
302 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100303 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
304 }
305
306 if (room) {
307 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
308 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
309 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100310 }
311 }
312
313 if (mask & QUIC_EV_CONN_HDSHK) {
314 const enum quic_handshake_state *state = a2;
315 const int *err = a3;
316
317 if (state)
318 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
319 if (err)
320 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
321 }
322
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100323 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100324 const struct quic_rx_packet *pkt = a2;
325 const unsigned long *pktlen = a3;
326 const SSL *ssl = a4;
327
328 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100329 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100330 pkt, quic_packet_type_enc_level_char(pkt->type));
331 if (pkt->pnl)
332 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
333 (unsigned long long)pkt->pn);
334 if (pkt->token_len)
335 chunk_appendf(&trace_buf, " toklen=%llu",
336 (unsigned long long)pkt->token_len);
337 if (pkt->aad_len)
338 chunk_appendf(&trace_buf, " aadlen=%llu",
339 (unsigned long long)pkt->aad_len);
340 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
341 pkt->flags, (unsigned long long)pkt->len);
342 }
343 if (pktlen)
344 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
345 if (ssl) {
346 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
347 chunk_appendf(&trace_buf, " el=%c",
348 quic_enc_level_char(ssl_to_quic_enc_level(level)));
349 }
350 }
351
352 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
353 const struct quic_rx_packet *pkt = a2;
354 const struct quic_rx_crypto_frm *cf = a3;
355 const SSL *ssl = a4;
356
357 if (pkt)
358 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
359 quic_packet_type_enc_level_char(pkt->type),
360 (unsigned long long)pkt->pn);
361 if (cf)
362 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
363 (unsigned long long)cf->offset_node.key,
364 (unsigned long long)cf->len);
365 if (ssl) {
366 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
367 chunk_appendf(&trace_buf, " el=%c",
368 quic_enc_level_char(ssl_to_quic_enc_level(level)));
369 }
370 }
371
372 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
373 const struct quic_frame *frm = a2;
374
375 if (frm)
376 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
377 }
378
379 if (mask & QUIC_EV_CONN_PHPKTS) {
380 const struct quic_enc_level *qel = a2;
381
382 if (qel) {
383 struct quic_pktns *pktns;
384
385 pktns = qc->pktns;
386 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200387 " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u pdg=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100388 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200389 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100390 !!(pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100391 (unsigned long long)qc->path->cwnd,
392 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100393 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100394 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
395 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100396 }
397 }
398
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100399 if (mask & QUIC_EV_CONN_ENCPKT) {
400 const struct enc_debug_info *edi = a2;
401
402 if (edi)
403 chunk_appendf(&trace_buf,
404 " payload=@%p payload_len=%llu"
405 " aad=@%p aad_len=%llu pn=%llu",
406 edi->payload, (unsigned long long)edi->payload_len,
407 edi->aad, (unsigned long long)edi->aad_len,
408 (unsigned long long)edi->pn);
409 }
410
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100411 if (mask & QUIC_EV_CONN_RMHP) {
412 const struct quic_rx_packet *pkt = a2;
413
414 if (pkt) {
415 const int *ret = a3;
416
417 chunk_appendf(&trace_buf, " pkt@%p", pkt);
418 if (ret && *ret)
419 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
420 pkt->pnl, (unsigned long long)pkt->pn);
421 }
422 }
423
424 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200425 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100426 const unsigned long *val1 = a3;
427 const unsigned long *val2 = a4;
428
429 if (frm)
430 chunk_tx_frm_appendf(&trace_buf, frm);
431 if (val1)
432 chunk_appendf(&trace_buf, " %lu", *val1);
433 if (val2)
434 chunk_appendf(&trace_buf, "..%lu", *val2);
435 }
436
437 if (mask & QUIC_EV_CONN_RTTUPDT) {
438 const unsigned int *rtt_sample = a2;
439 const unsigned int *ack_delay = a3;
440 const struct quic_loss *ql = a4;
441
442 if (rtt_sample)
443 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
444 if (ack_delay)
445 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
446 if (ql)
447 chunk_appendf(&trace_buf,
448 " srtt=%ums rttvar=%ums min_rtt=%ums",
449 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
450 }
451 if (mask & QUIC_EV_CONN_CC) {
452 const struct quic_cc_event *ev = a2;
453 const struct quic_cc *cc = a3;
454
455 if (a2)
456 quic_cc_event_trace(&trace_buf, ev);
457 if (a3)
458 quic_cc_state_trace(&trace_buf, cc);
459 }
460
461 if (mask & QUIC_EV_CONN_PKTLOSS) {
462 const struct quic_pktns *pktns = a2;
463 const struct list *lost_pkts = a3;
464 struct quic_conn *qc = conn->qc;
465
466 if (pktns) {
467 chunk_appendf(&trace_buf, " pktns=%s",
468 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
469 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
470 if (pktns->tx.loss_time)
471 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100472 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100473 }
474 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
475 struct quic_tx_packet *pkt;
476
477 chunk_appendf(&trace_buf, " lost_pkts:");
478 list_for_each_entry(pkt, lost_pkts, list)
479 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
480 }
481 }
482
483 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
484 struct quic_conn *qc = conn->qc;
485 const struct quic_pktns *pktns = a2;
486 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100487 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100488
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100489 if (ifae_pkts)
490 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
491 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100492 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100493 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100495 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
496 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100497 if (mask & QUIC_EV_CONN_STIMER) {
498 if (pktns->tx.loss_time)
499 chunk_appendf(&trace_buf, " loss_time=%dms",
500 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
501 }
502 if (mask & QUIC_EV_CONN_SPTO) {
503 if (pktns->tx.time_of_last_eliciting)
504 chunk_appendf(&trace_buf, " tole=%dms",
505 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
506 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100507 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100508 }
509 }
510
511 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
512 chunk_appendf(&trace_buf,
513 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
514 }
515 }
516
517 if (mask & QUIC_EV_CONN_SPPKTS) {
518 const struct quic_tx_packet *pkt = a2;
519
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100520 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
521 (unsigned long long)qc->path->cwnd,
522 (unsigned long long)qc->path->prep_in_flight,
523 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100524 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100525 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100526 (unsigned long)pkt->pn_node.key,
527 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
528 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100529 (unsigned long long)pkt->in_flight_len,
530 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100531 }
532 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100533
534 if (mask & QUIC_EV_CONN_SSLALERT) {
535 const uint8_t *alert = a2;
536 const enum ssl_encryption_level_t *level = a3;
537
538 if (alert)
539 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
540 if (level)
541 chunk_appendf(&trace_buf, " el=%c",
542 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
543 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100544
545 if (mask & QUIC_EV_CONN_BCFRMS) {
546 const size_t *sz1 = a2;
547 const size_t *sz2 = a3;
548 const size_t *sz3 = a4;
549
550 if (sz1)
551 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
552 if (sz2)
553 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
554 if (sz3)
555 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
556 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100557
558 if (mask & QUIC_EV_CONN_PSTRM) {
559 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100560
561 if (a2) {
562 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100563
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100564 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
565 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
566 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
567 (unsigned long long)s->id,
568 (unsigned long long)s->offset,
569 (unsigned long long)s->len);
570 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100571 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100572 }
573 if (mask & QUIC_EV_CONN_LPKT) {
574 const struct quic_rx_packet *pkt = a2;
575
576 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200577 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100578 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200579 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
580 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100581 }
582
583}
584
585/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200586static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587{
588 struct quic_conn *qc;
589
590 qc = ctx->conn->qc;
591 if (objt_server(qc->conn->target))
592 return 1;
593
594 if ((qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
595 (qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +0200596 (qc->state & QUIC_HS_ST_COMPLETE))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100597 return 1;
598
599 return 0;
600}
601
602/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
603 * both loss detection and PTO and schedule the task assiated to this timer if needed.
604 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200605static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100606{
607 struct quic_conn *qc;
608 struct quic_pktns *pktns;
609 unsigned int pto;
610
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100611 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
612 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613 qc = ctx->conn->qc;
614 pktns = quic_loss_pktns(qc);
615 if (tick_isset(pktns->tx.loss_time)) {
616 qc->timer = pktns->tx.loss_time;
617 goto out;
618 }
619
620 /* XXX TODO: anti-amplification: the timer must be
621 * cancelled for a server which reached the anti-amplification limit.
622 */
623
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100624 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
625 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100626 /* Timer cancellation. */
627 qc->timer = TICK_ETERNITY;
628 goto out;
629 }
630
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +0200631 pktns = quic_pto_pktns(qc, qc->state & QUIC_HS_ST_COMPLETE, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100632 if (tick_isset(pto))
633 qc->timer = pto;
634 out:
635 task_schedule(qc->timer_task, qc->timer);
636 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
637}
638
639#ifndef OPENSSL_IS_BORINGSSL
640int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
641 const uint8_t *read_secret,
642 const uint8_t *write_secret, size_t secret_len)
643{
644 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
645 struct quic_tls_ctx *tls_ctx =
646 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
647 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
648
649 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
650 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
651 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
652 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
653
654 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
655 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
656 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
657 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
658 read_secret, secret_len)) {
659 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
660 return 0;
661 }
662
663 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
664 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
665 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
666 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
667 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
668 write_secret, secret_len)) {
669 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
670 return 0;
671 }
672
673 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
674 if (objt_server(conn->target) && level == ssl_encryption_application) {
675 const unsigned char *buf;
676 size_t buflen;
677
678 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
679 if (!buflen)
680 return 0;
681
682 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
683 return 0;
684 }
685 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
686
687 return 1;
688}
689#else
690/* ->set_read_secret callback to derive the RX secrets at <level> encryption
691 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500692 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100693 */
694int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
695 const SSL_CIPHER *cipher,
696 const uint8_t *secret, size_t secret_len)
697{
698 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
699 struct quic_tls_ctx *tls_ctx =
700 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
701
702 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
703 tls_ctx->rx.aead = tls_aead(cipher);
704 tls_ctx->rx.md = tls_md(cipher);
705 tls_ctx->rx.hp = tls_hp(cipher);
706
707 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
708 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
709 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
710 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
711 secret, secret_len)) {
712 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
713 goto err;
714 }
715
716 if (objt_server(conn->target) && level == ssl_encryption_application) {
717 const unsigned char *buf;
718 size_t buflen;
719
720 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
721 if (!buflen)
722 goto err;
723
724 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
725 goto err;
726 }
727
728 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
729 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
730
731 return 1;
732
733 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100734 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100735 return 0;
736}
737
738/* ->set_write_secret callback to derive the TX secrets at <level>
739 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500740 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100741 */
742int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
743 const SSL_CIPHER *cipher,
744 const uint8_t *secret, size_t secret_len)
745{
746 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
747 struct quic_tls_ctx *tls_ctx =
748 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
749
750 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
751 tls_ctx->tx.aead = tls_aead(cipher);
752 tls_ctx->tx.md = tls_md(cipher);
753 tls_ctx->tx.hp = tls_hp(cipher);
754
755 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
756 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
757 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
758 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
759 secret, secret_len)) {
760 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
761 goto err;
762 }
763
764 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
765 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
766
767 return 1;
768
769 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100770 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100771 return 0;
772}
773#endif
774
775/* This function copies the CRYPTO data provided by the TLS stack found at <data>
776 * with <len> as size in CRYPTO buffers dedicated to store the information about
777 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
778 * It fails only if it could not managed to allocate enough CRYPTO buffers to
779 * store all the data.
780 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
781 */
782static int quic_crypto_data_cpy(struct quic_enc_level *qel,
783 const unsigned char *data, size_t len)
784{
785 struct quic_crypto_buf **qcb;
786 /* The remaining byte to store in CRYPTO buffers. */
787 size_t cf_offset, cf_len, *nb_buf;
788 unsigned char *pos;
789
790 nb_buf = &qel->tx.crypto.nb_buf;
791 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
792 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
793 cf_len = len;
794
795 while (len) {
796 size_t to_copy, room;
797
798 pos = (*qcb)->data + (*qcb)->sz;
799 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
800 to_copy = len > room ? room : len;
801 if (to_copy) {
802 memcpy(pos, data, to_copy);
803 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
804 qel->tx.crypto.sz += to_copy;
805 (*qcb)->sz += to_copy;
806 pos += to_copy;
807 len -= to_copy;
808 data += to_copy;
809 }
810 else {
811 struct quic_crypto_buf **tmp;
812
813 tmp = realloc(qel->tx.crypto.bufs,
814 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
815 if (tmp) {
816 qel->tx.crypto.bufs = tmp;
817 qcb = &qel->tx.crypto.bufs[*nb_buf];
818 *qcb = pool_alloc(pool_head_quic_crypto_buf);
819 if (!*qcb)
820 return 0;
821
822 (*qcb)->sz = 0;
823 ++*nb_buf;
824 }
825 else {
826 break;
827 }
828 }
829 }
830
831 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
832 * have been buffered.
833 */
834 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200835 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100836
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200837 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100838 if (!frm)
839 return 0;
840
841 frm->type = QUIC_FT_CRYPTO;
842 frm->crypto.offset = cf_offset;
843 frm->crypto.len = cf_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200844 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100845 }
846
847 return len == 0;
848}
849
850
851/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
852 * wants to provide the QUIC layer with CRYPTO data.
853 * Returns 1 if succeeded, 0 if not.
854 */
855int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
856 const uint8_t *data, size_t len)
857{
858 struct connection *conn;
859 enum quic_tls_enc_level tel;
860 struct quic_enc_level *qel;
861
862 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
863 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
864 tel = ssl_to_quic_enc_level(level);
865 qel = &conn->qc->els[tel];
866
867 if (tel == -1) {
868 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
869 goto err;
870 }
871
872 if (!quic_crypto_data_cpy(qel, data, len)) {
873 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
874 goto err;
875 }
876
877 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
878 conn, &level, &len);
879
880 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
881 return 1;
882
883 err:
884 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
885 return 0;
886}
887
888int ha_quic_flush_flight(SSL *ssl)
889{
890 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
891
892 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
893 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
894
895 return 1;
896}
897
898int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
899{
900 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
901
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100902 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100903 return 1;
904}
905
906/* QUIC TLS methods */
907static SSL_QUIC_METHOD ha_quic_method = {
908#ifdef OPENSSL_IS_BORINGSSL
909 .set_read_secret = ha_set_rsec,
910 .set_write_secret = ha_set_wsec,
911#else
912 .set_encryption_secrets = ha_quic_set_encryption_secrets,
913#endif
914 .add_handshake_data = ha_quic_add_handshake_data,
915 .flush_flight = ha_quic_flush_flight,
916 .send_alert = ha_quic_send_alert,
917};
918
919/* Initialize the TLS context of a listener with <bind_conf> as configuration.
920 * Returns an error count.
921 */
922int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
923{
924 struct proxy *curproxy = bind_conf->frontend;
925 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
926 int cfgerr = 0;
927
928#if 0
929 /* XXX Did not manage to use this. */
930 const char *ciphers =
931 "TLS_AES_128_GCM_SHA256:"
932 "TLS_AES_256_GCM_SHA384:"
933 "TLS_CHACHA20_POLY1305_SHA256:"
934 "TLS_AES_128_CCM_SHA256";
935#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200936 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100937 long options =
938 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
939 SSL_OP_SINGLE_ECDH_USE |
940 SSL_OP_CIPHER_SERVER_PREFERENCE;
941 SSL_CTX *ctx;
942
943 ctx = SSL_CTX_new(TLS_server_method());
944 bind_conf->initial_ctx = ctx;
945
946 SSL_CTX_set_options(ctx, options);
947#if 0
948 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
949 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
950 "for bind '%s' at [%s:%d].\n",
951 curproxy->id, ciphers,
952 bind_conf->arg, bind_conf->file, bind_conf->line);
953 cfgerr++;
954 }
955#endif
956
957 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
958 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
959 "for bind '%s' at [%s:%d].\n",
960 curproxy->id, groups,
961 bind_conf->arg, bind_conf->file, bind_conf->line);
962 cfgerr++;
963 }
964
965 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
966 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
967 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
968 SSL_CTX_set_default_verify_paths(ctx);
969
970#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
971#ifdef OPENSSL_IS_BORINGSSL
972 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
973 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
974#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
975 if (bind_conf->ssl_conf.early_data) {
976 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
977 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
978 }
979 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
980 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
981#else
982 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
983#endif
984 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
985#endif
986 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
987
988 return cfgerr;
989}
990
991/* Decode an expected packet number from <truncated_on> its truncated value,
992 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
993 * the number of bits used to encode this packet number (its length in bytes * 8).
994 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
995 */
996static uint64_t decode_packet_number(uint64_t largest_pn,
997 uint32_t truncated_pn, unsigned int pn_nbits)
998{
999 uint64_t expected_pn = largest_pn + 1;
1000 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1001 uint64_t pn_hwin = pn_win / 2;
1002 uint64_t pn_mask = pn_win - 1;
1003 uint64_t candidate_pn;
1004
1005
1006 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1007 /* Note that <pn_win> > <pn_hwin>. */
1008 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1009 candidate_pn + pn_hwin <= expected_pn)
1010 return candidate_pn + pn_win;
1011
1012 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1013 return candidate_pn - pn_win;
1014
1015 return candidate_pn;
1016}
1017
1018/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1019 * cryptographic context.
1020 * <largest_pn> is the largest received packet number and <pn> the address of
1021 * the packet number field for this packet with <byte0> address of its first byte.
1022 * <end> points to one byte past the end of this packet.
1023 * Returns 1 if succeeded, 0 if not.
1024 */
1025static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1026 int64_t largest_pn, unsigned char *pn,
1027 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001028 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001029{
1030 int ret, outlen, i, pnlen;
1031 uint64_t packet_number;
1032 uint32_t truncated_pn = 0;
1033 unsigned char mask[5] = {0};
1034 unsigned char *sample;
1035 EVP_CIPHER_CTX *cctx;
1036 unsigned char *hp_key;
1037
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001038 /* Check there is enough data in this packet. */
1039 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1040 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1041 return 0;
1042 }
1043
1044 cctx = EVP_CIPHER_CTX_new();
1045 if (!cctx) {
1046 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1047 return 0;
1048 }
1049
1050 ret = 0;
1051 sample = pn + QUIC_PACKET_PN_MAXLEN;
1052
1053 hp_key = tls_ctx->rx.hp_key;
1054 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1055 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1056 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1057 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1058 goto out;
1059 }
1060
1061 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1062 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1063 for (i = 0; i < pnlen; i++) {
1064 pn[i] ^= mask[i + 1];
1065 truncated_pn = (truncated_pn << 8) | pn[i];
1066 }
1067
1068 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1069 /* Store remaining information for this unprotected header */
1070 pkt->pn = packet_number;
1071 pkt->pnl = pnlen;
1072
1073 ret = 1;
1074
1075 out:
1076 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001077
1078 return ret;
1079}
1080
1081/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1082 * address, with <payload_len> as payload length, <aad> as address of
1083 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1084 * context.
1085 * Returns 1 if succeeded, 0 if not.
1086 */
1087static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1088 unsigned char *aad, size_t aad_len, uint64_t pn,
1089 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1090{
1091 unsigned char iv[12];
1092 unsigned char *tx_iv = tls_ctx->tx.iv;
1093 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001094 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001095
1096 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1097 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001098 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099 }
1100
1101 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1102 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1103 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001104 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001105 }
1106
1107 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001108
1109 err:
1110 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1111 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1112 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001113}
1114
1115/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1116 * Returns 1 if succeeded, 0 if not.
1117 */
1118static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1119{
1120 int ret;
1121 unsigned char iv[12];
1122 unsigned char *rx_iv = tls_ctx->rx.iv;
1123 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1124
1125 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1126 return 0;
1127
1128 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1129 pkt->data, pkt->aad_len,
1130 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1131 if (!ret)
1132 return 0;
1133
1134 /* Update the packet length (required to parse the frames). */
1135 pkt->len = pkt->aad_len + ret;
1136
1137 return 1;
1138}
1139
1140/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001141static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001142 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001143{
1144 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001145 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001146 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001147}
1148
1149/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1150 * deallocating them, and their TX frames.
1151 * Returns the last node reached to be used for the next range.
1152 * May be NULL if <largest> node could not be found.
1153 */
1154static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1155 struct list *newly_acked_pkts,
1156 struct eb64_node *largest_node,
1157 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001158 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001159{
1160 struct eb64_node *node;
1161 struct quic_tx_packet *pkt;
1162
1163 if (largest_node)
1164 node = largest_node;
1165 else {
1166 node = eb64_lookup(pkts, largest);
1167 while (!node && largest > smallest) {
1168 node = eb64_lookup(pkts, --largest);
1169 }
1170 }
1171
1172 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001173 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001174
1175 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1176 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001177 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001178 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1179 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1180 qc_treat_acked_tx_frm(frm, ctx);
1181 node = eb64_prev(node);
1182 eb64_delete(&pkt->pn_node);
1183 }
1184
1185 return node;
1186}
1187
1188/* Treat <frm> frame whose packet it is attached to has just been detected as non
1189 * acknowledged.
1190 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001191static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001192 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001193 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001194{
1195 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001196 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001197 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001198}
1199
1200
1201/* Free the TX packets of <pkts> list */
1202static inline void free_quic_tx_pkts(struct list *pkts)
1203{
1204 struct quic_tx_packet *pkt, *tmp;
1205
1206 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001207 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001208 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001209 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001210 }
1211}
1212
1213/* Send a packet loss event nofification to the congestion controller
1214 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1215 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1216 * at <now_us> current time.
1217 * Always succeeds.
1218 */
1219static inline void qc_cc_loss_event(struct quic_conn *qc,
1220 unsigned int lost_bytes,
1221 unsigned int newest_time_sent,
1222 unsigned int period,
1223 unsigned int now_us)
1224{
1225 struct quic_cc_event ev = {
1226 .type = QUIC_CC_EVT_LOSS,
1227 .loss.now_ms = now_ms,
1228 .loss.max_ack_delay = qc->max_ack_delay,
1229 .loss.lost_bytes = lost_bytes,
1230 .loss.newest_time_sent = newest_time_sent,
1231 .loss.period = period,
1232 };
1233
1234 quic_cc_event(&qc->path->cc, &ev);
1235}
1236
1237/* Send a packet ack event nofication for each newly acked packet of
1238 * <newly_acked_pkts> list and free them.
1239 * Always succeeds.
1240 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001241static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001242 struct list *newly_acked_pkts)
1243{
1244 struct quic_conn *qc = ctx->conn->qc;
1245 struct quic_tx_packet *pkt, *tmp;
1246 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1247
1248 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1249 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001250 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001251 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001252 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001253 ev.ack.acked = pkt->in_flight_len;
1254 ev.ack.time_sent = pkt->time_sent;
1255 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001256 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001257 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001258 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001259 }
1260
1261}
1262
1263/* Handle <pkts> list of lost packets detected at <now_us> handling
1264 * their TX frames.
1265 * Send a packet loss event to the congestion controller if
1266 * in flight packet have been lost.
1267 * Also frees the packet in <pkts> list.
1268 * Never fails.
1269 */
1270static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001271 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001272 struct list *pkts,
1273 uint64_t now_us)
1274{
1275 struct quic_conn *qc = ctx->conn->qc;
1276 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001277 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001278 uint64_t lost_bytes;
1279
1280 lost_bytes = 0;
1281 oldest_lost = newest_lost = NULL;
1282 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1283 lost_bytes += pkt->in_flight_len;
1284 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001285 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001286 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001287 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001288 /* Treat the frames of this lost packet. */
1289 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1290 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001291 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001292 if (!oldest_lost) {
1293 oldest_lost = newest_lost = pkt;
1294 }
1295 else {
1296 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001297 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001298 newest_lost = pkt;
1299 }
1300 }
1301
1302 if (lost_bytes) {
1303 /* Sent a packet loss event to the congestion controller. */
1304 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1305 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001306 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001307 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001308 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001309 }
1310}
1311
1312/* Look for packet loss from sent packets for <qel> encryption level of a
1313 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1314 * their tree if deemed as lost or set the <loss_time> value the packet number
1315 * space if any not deemed lost.
1316 * Should be called after having received an ACK frame with newly acknowledged
1317 * packets or when the the loss detection timer has expired.
1318 * Always succeeds.
1319 */
1320static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1321 struct quic_conn *qc,
1322 struct list *lost_pkts)
1323{
1324 struct eb_root *pkts;
1325 struct eb64_node *node;
1326 struct quic_loss *ql;
1327 unsigned int loss_delay;
1328
1329 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1330 pkts = &pktns->tx.pkts;
1331 pktns->tx.loss_time = TICK_ETERNITY;
1332 if (eb_is_empty(pkts))
1333 goto out;
1334
1335 ql = &qc->path->loss;
1336 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1337 loss_delay += loss_delay >> 3;
1338 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1339
1340 node = eb64_first(pkts);
1341 while (node) {
1342 struct quic_tx_packet *pkt;
1343 int64_t largest_acked_pn;
1344 unsigned int loss_time_limit, time_sent;
1345
1346 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001347 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001348 node = eb64_next(node);
1349 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1350 break;
1351
1352 time_sent = pkt->time_sent;
1353 loss_time_limit = tick_add(time_sent, loss_delay);
1354 if (tick_is_le(time_sent, now_ms) ||
1355 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1356 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001357 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001358 }
1359 else {
1360 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1361 }
1362 }
1363
1364 out:
1365 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1366}
1367
1368/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1369 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001370 * 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 +01001371 * acked ack-eliciting packet.
1372 * Return 1, if succeeded, 0 if not.
1373 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001374static inline int qc_parse_ack_frm(struct quic_frame *frm, struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001375 struct quic_enc_level *qel,
1376 unsigned int *rtt_sample,
1377 const unsigned char **pos, const unsigned char *end)
1378{
1379 struct quic_ack *ack = &frm->ack;
1380 uint64_t smallest, largest;
1381 struct eb_root *pkts;
1382 struct eb64_node *largest_node;
1383 unsigned int time_sent, pkt_flags;
1384 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1385 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1386
1387 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1388 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1389 ctx->conn,, &ack->largest_ack);
1390 goto err;
1391 }
1392
1393 if (ack->first_ack_range > ack->largest_ack) {
1394 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1395 ctx->conn,, &ack->first_ack_range);
1396 goto err;
1397 }
1398
1399 largest = ack->largest_ack;
1400 smallest = largest - ack->first_ack_range;
1401 pkts = &qel->pktns->tx.pkts;
1402 pkt_flags = 0;
1403 largest_node = NULL;
1404 time_sent = 0;
1405
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001406 if ((int64_t)ack->largest_ack > HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001407 largest_node = eb64_lookup(pkts, largest);
1408 if (!largest_node) {
1409 TRACE_DEVEL("Largest acked packet not found",
1410 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1411 goto err;
1412 }
1413
1414 time_sent = eb64_entry(&largest_node->node,
1415 struct quic_tx_packet, pn_node)->time_sent;
1416 }
1417
1418 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1419 ctx->conn,, &largest, &smallest);
1420 do {
1421 uint64_t gap, ack_range;
1422
1423 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1424 largest_node, largest, smallest, ctx);
1425 if (!ack->ack_range_num--)
1426 break;
1427
1428 if (!quic_dec_int(&gap, pos, end))
1429 goto err;
1430
1431 if (smallest < gap + 2) {
1432 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1433 ctx->conn,, &gap, &smallest);
1434 goto err;
1435 }
1436
1437 largest = smallest - gap - 2;
1438 if (!quic_dec_int(&ack_range, pos, end))
1439 goto err;
1440
1441 if (largest < ack_range) {
1442 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1443 ctx->conn,, &largest, &ack_range);
1444 goto err;
1445 }
1446
1447 /* Do not use this node anymore. */
1448 largest_node = NULL;
1449 /* Next range */
1450 smallest = largest - ack_range;
1451
1452 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1453 ctx->conn,, &largest, &smallest);
1454 } while (1);
1455
1456 /* Flag this packet number space as having received an ACK. */
1457 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_RECEIVED;
1458
1459 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1460 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001461 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001462 }
1463
1464 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1465 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1466 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1467 if (!LIST_ISEMPTY(&lost_pkts))
1468 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1469 }
1470 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1471 if (quic_peer_validated_addr(ctx))
1472 ctx->conn->qc->path->loss.pto_count = 0;
1473 qc_set_timer(ctx);
1474 }
1475
1476
1477 return 1;
1478
1479 err:
1480 free_quic_tx_pkts(&newly_acked_pkts);
1481 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1482 return 0;
1483}
1484
1485/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1486 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001487 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001488 * Return 1 if succeeded, 0 if not.
1489 */
1490static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001491 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001492 const unsigned char *data, size_t len,
1493 struct quic_rx_packet *pkt,
1494 struct quic_rx_crypto_frm *cf)
1495{
1496 int ssl_err;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001497 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001498
1499 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1500 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001501 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001502 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1503 TRACE_PROTO("SSL_provide_quic_data() error",
1504 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1505 goto err;
1506 }
1507
1508 el->rx.crypto.offset += len;
1509 TRACE_PROTO("in order CRYPTO data",
1510 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1511
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001512 if (qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001513 ssl_err = SSL_do_handshake(ctx->ssl);
1514 if (ssl_err != 1) {
1515 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1516 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1517 TRACE_PROTO("SSL handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001518 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001519 goto out;
1520 }
1521
1522 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001523 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001524 goto err;
1525 }
1526
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001527 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001528 if (objt_listener(ctx->conn->target))
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001529 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001530 else
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001531 qc->state = QUIC_HS_ST_COMPLETE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001532 } else {
1533 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1534 if (ssl_err != 1) {
1535 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1536 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1537 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001538 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001539 goto out;
1540 }
1541
1542 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001543 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001544 goto err;
1545 }
1546
1547 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001548 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001549 }
1550
1551 out:
1552 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1553 return 1;
1554
1555 err:
1556 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1557 return 0;
1558}
1559
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001560/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1561 * <pkt> RX packet.
1562 * Return it if succeeded, NULL if not.
1563 */
1564static inline
1565struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1566 struct quic_rx_packet *pkt)
1567{
1568 struct quic_rx_strm_frm *frm;
1569
1570 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1571 if (frm) {
1572 frm->offset_node.key = stream_frm->offset;
1573 frm->len = stream_frm->len;
1574 frm->data = stream_frm->data;
1575 frm->pkt = pkt;
1576 }
1577
1578 return frm;
1579}
1580
1581/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1582 * several streams, depending on the already open onces.
1583 * Return this node if succeeded, NULL if not.
1584 */
1585static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1586{
1587 unsigned int strm_type;
1588 int64_t sub_id;
1589 struct eb64_node *strm_node;
1590
1591 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1592
1593 strm_type = id & QCS_ID_TYPE_MASK;
1594 sub_id = id >> QCS_ID_TYPE_SHIFT;
1595 strm_node = NULL;
1596 if (qc_local_stream_id(qcc, id)) {
1597 /* Local streams: this stream must be already opened. */
1598 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1599 if (!strm_node) {
1600 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1601 goto out;
1602 }
1603 }
1604 else {
1605 /* Remote streams. */
1606 struct eb_root *strms;
1607 uint64_t largest_id;
1608 enum qcs_type qcs_type;
1609
1610 strms = &qcc->streams_by_id;
1611 qcs_type = qcs_id_type(id);
1612 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1613 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1614 goto out;
1615 }
1616
1617 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1618 * correct value.
1619 */
1620 largest_id = qcc->strms[qcs_type].largest_id;
1621 if (sub_id > (int64_t)largest_id) {
1622 /* RFC: "A stream ID that is used out of order results in all streams
1623 * of that type with lower-numbered stream IDs also being opened".
1624 * So, let's "open" these streams.
1625 */
1626 int64_t i;
1627 struct qcs *qcs;
1628
1629 qcs = NULL;
1630 for (i = largest_id + 1; i <= sub_id; i++) {
1631 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1632 if (!qcs) {
1633 TRACE_PROTO("Could not allocate a new stream",
1634 QUIC_EV_CONN_PSTRM, qcc->conn);
1635 goto out;
1636 }
1637
1638 qcc->strms[qcs_type].largest_id = i;
1639 }
1640 if (qcs)
1641 strm_node = &qcs->by_id;
1642 }
1643 else {
1644 strm_node = eb64_lookup(strms, id);
1645 }
1646 }
1647
1648 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1649 return strm_node;
1650
1651 out:
1652 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1653 return NULL;
1654}
1655
1656/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1657 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1658 * to reflect the data which have been consumed.
1659 */
1660static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1661{
1662 size_t ret;
1663
1664 ret = 0;
1665 while (strm_frm->len) {
1666 size_t try;
1667
1668 try = b_contig_space(buf);
1669 if (!try)
1670 break;
1671
1672 if (try > strm_frm->len)
1673 try = strm_frm->len;
1674 memcpy(b_tail(buf), strm_frm->data, try);
1675 strm_frm->len -= try;
1676 strm_frm->offset += try;
1677 b_add(buf, try);
1678 ret += try;
1679 }
1680
1681 return ret;
1682}
1683
1684/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1685 * streams may be open. The data are copied to the stream RX buffer if possible.
1686 * If not, the STREAM frame is stored to be treated again later.
1687 * We rely on the flow control so that not to store too much STREAM frames.
1688 * Return 1 if succeeded, 0 if not.
1689 */
1690static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1691 struct quic_stream *strm_frm,
1692 struct quic_conn *qc)
1693{
1694 struct qcs *strm;
1695 struct eb64_node *strm_node, *frm_node;
1696 struct quic_rx_strm_frm *frm;
1697
1698 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1699 if (!strm_node) {
1700 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1701 return 0;
1702 }
1703
1704 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1705 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1706 /* FIXME: handle the case where this frame overlap others */
1707 if (frm_node) {
1708 TRACE_PROTO("Already existing stream data",
1709 QUIC_EV_CONN_PSTRM, qc->conn);
1710 goto out;
1711 }
1712
1713 if (strm_frm->offset == strm->rx.offset) {
1714 int ret;
1715
1716 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1717 goto store_frm;
1718
1719 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1720 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1721 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1722 return 0;
1723 }
1724
1725 strm->rx.offset += ret;
1726 }
1727
1728 if (!strm_frm->len)
1729 goto out;
1730
1731 store_frm:
1732 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1733 if (!frm) {
1734 TRACE_PROTO("Could not alloc RX STREAM frame",
1735 QUIC_EV_CONN_PSTRM, qc->conn);
1736 return 0;
1737 }
1738
1739 eb64_insert(&strm->frms, &frm->offset_node);
1740 quic_rx_packet_refinc(pkt);
1741
1742 out:
1743 return 1;
1744}
1745
1746/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1747 * streams may be open. The data are copied to the stream RX buffer if possible.
1748 * If not, the STREAM frame is stored to be treated again later.
1749 * We rely on the flow control so that not to store too much STREAM frames.
1750 * Return 1 if succeeded, 0 if not.
1751 */
1752static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1753 struct quic_stream *strm_frm,
1754 struct quic_conn *qc)
1755{
1756 struct qcs *strm;
1757 struct eb64_node *strm_node, *frm_node;
1758 struct quic_rx_strm_frm *frm;
1759 size_t strm_frm_len;
1760
1761 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1762 if (!strm_node) {
1763 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1764 return 0;
1765 }
1766
1767 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1768 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1769 /* FIXME: handle the case where this frame overlap others */
1770 if (frm_node) {
1771 TRACE_PROTO("Already existing stream data",
1772 QUIC_EV_CONN_PSTRM, qc->conn);
1773 goto out;
1774 }
1775
1776 strm_frm_len = strm_frm->len;
1777 if (strm_frm->offset == strm->rx.offset) {
1778 int ret;
1779
1780 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1781 goto store_frm;
1782
1783 /* qc_strm_cpy() will modify the offset, depending on the number
1784 * of bytes copied.
1785 */
1786 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1787 /* Inform the application of the arrival of this new stream */
1788 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1789 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1790 return 0;
1791 }
1792
1793 if (ret)
1794 ruqs_notify_recv(strm);
1795
1796 strm_frm->offset += ret;
1797 }
1798 /* Take this frame into an account for the stream flow control */
1799 strm->rx.offset += strm_frm_len;
1800 /* It all the data were provided to the application, there is no need to
1801 * store any more inforamtion for it.
1802 */
1803 if (!strm_frm->len)
1804 goto out;
1805
1806 store_frm:
1807 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1808 if (!frm) {
1809 TRACE_PROTO("Could not alloc RX STREAM frame",
1810 QUIC_EV_CONN_PSTRM, qc->conn);
1811 return 0;
1812 }
1813
1814 eb64_insert(&strm->frms, &frm->offset_node);
1815 quic_rx_packet_refinc(pkt);
1816
1817 out:
1818 return 1;
1819}
1820
1821static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1822 struct quic_stream *strm_frm,
1823 struct quic_conn *qc)
1824{
1825 if (strm_frm->id & QCS_ID_DIR_BIT)
1826 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1827 else
1828 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1829}
1830
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001831/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1832 * as I/O handler context and <qel> as encryption level.
1833 * Returns 1 if succeeded, 0 if failed.
1834 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001835static 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 +01001836 struct quic_enc_level *qel)
1837{
1838 struct quic_frame frm;
1839 const unsigned char *pos, *end;
1840 struct quic_conn *conn = ctx->conn->qc;
1841
1842 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1843 /* Skip the AAD */
1844 pos = pkt->data + pkt->aad_len;
1845 end = pkt->data + pkt->len;
1846
1847 while (pos < end) {
1848 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1849 goto err;
1850
1851 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001852 case QUIC_FT_PADDING:
1853 if (pos != end) {
1854 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1855 goto err;
1856 }
1857 break;
1858 case QUIC_FT_PING:
1859 break;
1860 case QUIC_FT_ACK:
1861 {
1862 unsigned int rtt_sample;
1863
1864 rtt_sample = 0;
1865 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1866 goto err;
1867
1868 if (rtt_sample) {
1869 unsigned int ack_delay;
1870
1871 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1872 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1873 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1874 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001875 break;
1876 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001877 case QUIC_FT_CRYPTO:
1878 if (frm.crypto.offset != qel->rx.crypto.offset) {
1879 struct quic_rx_crypto_frm *cf;
1880
1881 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1882 if (!cf) {
1883 TRACE_DEVEL("CRYPTO frame allocation failed",
1884 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1885 goto err;
1886 }
1887
1888 cf->offset_node.key = frm.crypto.offset;
1889 cf->len = frm.crypto.len;
1890 cf->data = frm.crypto.data;
1891 cf->pkt = pkt;
1892 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1893 quic_rx_packet_refinc(pkt);
1894 }
1895 else {
1896 /* XXX TO DO: <cf> is used only for the traces. */
1897 struct quic_rx_crypto_frm cf = { };
1898
1899 cf.offset_node.key = frm.crypto.offset;
1900 cf.len = frm.crypto.len;
1901 if (!qc_provide_cdata(qel, ctx,
1902 frm.crypto.data, frm.crypto.len,
1903 pkt, &cf))
1904 goto err;
1905 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001906 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001907 case QUIC_FT_STREAM_8:
1908 case QUIC_FT_STREAM_9:
1909 case QUIC_FT_STREAM_A:
1910 case QUIC_FT_STREAM_B:
1911 case QUIC_FT_STREAM_C:
1912 case QUIC_FT_STREAM_D:
1913 case QUIC_FT_STREAM_E:
1914 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001915 {
1916 struct quic_stream *stream = &frm.stream;
1917
1918 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1919 if (objt_listener(ctx->conn->target)) {
1920 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
1921 goto err;
1922 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
1923 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001924
1925 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
1926 goto err;
1927
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001928 break;
1929 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001930 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001931 break;
1932 case QUIC_FT_CONNECTION_CLOSE:
1933 case QUIC_FT_CONNECTION_CLOSE_APP:
1934 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001935 case QUIC_FT_HANDSHAKE_DONE:
1936 if (objt_listener(ctx->conn->target))
1937 goto err;
1938
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001939 conn->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001940 break;
1941 default:
1942 goto err;
1943 }
1944 }
1945
1946 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
1947 * has successfully parse a Handshake packet. The Initial encryption must also
1948 * be discarded.
1949 */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001950 if (conn->state == QUIC_HS_ST_SERVER_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001951 pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) {
1952 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
1953 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
1954 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001955 conn->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001956 }
1957
1958 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1959 return 1;
1960
1961 err:
1962 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
1963 return 0;
1964}
1965
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001966/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
1967 * buffer. This is the responsability of the caller to check there is enough
1968 * room in <cbuf>. Also increase the <cbuf> write index consequently.
1969 * This function must be called only after having built a correct datagram.
1970 * Always succeeds.
1971 */
1972static inline void qc_set_dg(struct cbuf *cbuf,
1973 uint16_t dglen, struct quic_tx_packet *pkt)
1974{
1975 write_u16(cb_wr(cbuf), dglen);
1976 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
1977 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
1978}
1979
1980/* Prepare as much as possible handshake packets into <qr> ring buffer for
1981 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
1982 * several packets in the same datagram. A header made of two fields is added
1983 * to each datagram: the datagram length followed by the address of the first
1984 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001985 * Returns 1 if succeeded, or 0 if something wrong happened.
1986 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001987static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001988{
1989 struct quic_conn *qc;
1990 enum quic_tls_enc_level tel, next_tel;
1991 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001992 struct cbuf *cbuf;
1993 unsigned char *end_buf, *end, *pos, *spos;
1994 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
1995 /* length of datagrams */
1996 uint16_t dglen;
1997 size_t total;
1998 /* Each datagram is prepended with its length followed by the
1999 * address of the first packet in the datagram.
2000 */
2001 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002002
2003 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2004 qc = ctx->conn->qc;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002005 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc->state)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002006 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002007 goto err;
2008 }
2009
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002010 start:
2011 total = 0;
2012 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002013 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002014 cbuf = qr->cbuf;
2015 spos = pos = cb_wr(cbuf);
2016 /* Leave at least <dglen> bytes at the end of this buffer
2017 * to ensure there is enough room to mark the end of prepared
2018 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002019 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002020 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2021 first_pkt = prv_pkt = NULL;
2022 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
2023 int err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002024 enum quic_pkt_type pkt_type;
2025
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002026 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002027 /* Do not build any more packet if the TX secrets are not available or
2028 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002029 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002030 * and if there is no more CRYPTO data available or in flight
2031 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002032 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002033 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2034 (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002035 !qc->tx.nb_pto_dgrams &&
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002036 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002037 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002038 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002039 /* Set the current datagram as prepared into <cbuf> if
2040 * the was already a correct packet which was previously written.
2041 */
2042 if (prv_pkt)
2043 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002044 break;
2045 }
2046
2047 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002048 if (!prv_pkt) {
2049 /* Leave room for the datagram header */
2050 pos += dg_headlen;
2051 end = pos + qc->path->mtu;
2052 }
2053
2054 cur_pkt = qc_build_hdshk_pkt(&pos, end, qc, pkt_type, qel, &err);
2055 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002056 case -2:
2057 goto err;
2058 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002059 /* If there was already a correct packet present, set the
2060 * current datagram as prepared into <cbuf>.
2061 */
2062 if (prv_pkt) {
2063 qc_set_dg(cbuf, dglen, first_pkt);
2064 goto stop_build;
2065 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002066 goto out;
2067 default:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002068 total += cur_pkt->len;
2069 /* keep trace of the first packet in the datagram */
2070 if (!first_pkt)
2071 first_pkt = cur_pkt;
2072 /* Attach the current one to the previous one */
2073 if (prv_pkt)
2074 prv_pkt->next = cur_pkt;
2075 /* Let's say we have to build a new dgram */
2076 prv_pkt = NULL;
2077 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002078 /* Discard the Initial encryption keys as soon as
2079 * a handshake packet could be built.
2080 */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002081 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002082 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2083 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2084 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2085 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002086 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002087 }
2088 /* Special case for Initial packets: when they have all
2089 * been sent, select the next level.
2090 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002091 if (tel == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002092 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || qc->els[next_tel].pktns->tx.in_flight)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002093 tel = next_tel;
2094 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002095 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002096 /* If there is data for the next level, do not
2097 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002098 * which sends only one Initial packet, then wait
2099 * for additional CRYPTO data from the server to enter the
2100 * next level.
2101 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002102 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002103 }
2104 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002105 }
2106
2107 /* If we have to build a new datagram, set the current datagram as
2108 * prepared into <cbuf>.
2109 */
2110 if (!prv_pkt) {
2111 qc_set_dg(cbuf, dglen, first_pkt);
2112 first_pkt = NULL;
2113 dglen = 0;
2114 }
2115 }
2116
2117 stop_build:
2118 /* Reset <wr> writer index if in front of <rd> index */
2119 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2120 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2121
2122 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2123 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2124 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2125 /* Mark the end of contiguous data for the reader */
2126 write_u16(cb_wr(cbuf), 0);
2127 cb_add(cbuf, sizeof(uint16_t));
2128 }
2129 }
2130
2131 if (rd && rd <= cbuf->wr) {
2132 cb_wr_reset(cbuf);
2133 if (pos == spos) {
2134 /* Reuse the same buffer if nothing was built. */
2135 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002136 }
2137 }
2138 }
2139
2140 out:
2141 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002142 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002143
2144 err:
2145 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002146 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002147}
2148
2149/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002150 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002151 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002152int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002153{
2154 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002155 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002156
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002157 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002158 cbuf = qr->cbuf;
2159 while (cb_contig_data(cbuf)) {
2160 unsigned char *pos;
2161 struct buffer tmpbuf = { };
2162 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2163 uint16_t dglen;
2164 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002165 unsigned int time_sent;
2166
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002167 pos = cb_rd(cbuf);
2168 dglen = read_u16(pos);
2169 /* End of prepared datagrams.
2170 * Reset the reader index only if in front of the writer index.
2171 */
2172 if (!dglen) {
2173 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2174
2175 if (wr && wr < cbuf->rd) {
2176 cb_rd_reset(cbuf);
2177 continue;
2178 }
2179 break;
2180 }
2181
2182 pos += sizeof dglen;
2183 first_pkt = read_ptr(pos);
2184 pos += sizeof first_pkt;
2185 tmpbuf.area = (char *)pos;
2186 tmpbuf.size = tmpbuf.data = dglen;
2187
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002188 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002189 for (pkt = first_pkt; pkt; pkt = pkt->next)
2190 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002191 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002192 &tmpbuf, tmpbuf.data, 0) <= 0) {
2193 for (pkt = first_pkt; pkt; pkt = pkt->next)
2194 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002195 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002196 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002197
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002198 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002199 qc->tx.bytes += tmpbuf.data;
2200 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002201
2202 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2203 pkt->time_sent = time_sent;
2204 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2205 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002206 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002207 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002208 qc->path->in_flight += pkt->in_flight_len;
2209 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2210 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002211 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002212 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2213 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002214 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002215 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002216 }
2217 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002218
2219 return 1;
2220}
2221
2222/* Build all the frames which must be sent just after the handshake have succeeded.
2223 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2224 * a HANDSHAKE_DONE frame.
2225 * Return 1 if succeeded, 0 if not.
2226 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002227static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002228{
2229 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002230 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002231 struct quic_frame *frm;
2232
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002233 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002234 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002235 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002236 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002237 if (!frm)
2238 return 0;
2239
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002240 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002241 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002242 }
2243
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002244 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002245 struct quic_connection_id *cid;
2246
2247 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002248 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002249 if (!frm || !cid)
2250 goto err;
2251
2252 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002253 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002254 }
2255
2256 return 1;
2257
2258 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002259 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002260 return 0;
2261}
2262
2263/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002264void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002265{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002266 struct eb64_node *n;
2267 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002268
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002269 n = eb64_first(&arngs->root);
2270 while (n) {
2271 struct eb64_node *next;
2272
2273 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2274 next = eb64_next(n);
2275 eb64_delete(n);
2276 free(ar);
2277 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002278 }
2279}
2280
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002281/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2282 * descending order.
2283 */
2284static inline size_t sack_gap(struct quic_arng_node *p,
2285 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002286{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002287 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002288}
2289
2290
2291/* Remove the last elements of <ack_ranges> list of ack range updating its
2292 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002293 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002294 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002295static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002296{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002297 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002298
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002299 last = eb64_last(&arngs->root);
2300 while (last && arngs->enc_sz > limit) {
2301 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002302
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002303 prev = eb64_prev(last);
2304 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002305 return 0;
2306
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002307 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2308 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2309 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2310 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2311 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2312 --arngs->sz;
2313 eb64_delete(last);
2314 pool_free(pool_head_quic_arng, last);
2315 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002316 }
2317
2318 return 1;
2319}
2320
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002321/* Set the encoded size of <arngs> QUIC ack ranges. */
2322static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2323{
2324 struct eb64_node *node, *next;
2325 struct quic_arng_node *ar, *ar_next;
2326
2327 node = eb64_last(&arngs->root);
2328 if (!node)
2329 return;
2330
2331 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2332 arngs->enc_sz = quic_int_getsize(ar->last) +
2333 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2334
2335 while ((next = eb64_prev(node))) {
2336 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2337 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2338 quic_int_getsize(ar_next->last - ar_next->first.key);
2339 node = next;
2340 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2341 }
2342}
2343
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002344/* Insert <ar> ack range into <argns> tree of ack ranges.
2345 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002346 */
2347static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002348struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2349 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002350{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002351 struct quic_arng_node *new_ar;
2352
2353 new_ar = pool_alloc(pool_head_quic_arng);
2354 if (new_ar) {
2355 new_ar->first.key = ar->first;
2356 new_ar->last = ar->last;
2357 eb64_insert(&arngs->root, &new_ar->first);
2358 arngs->sz++;
2359 }
2360
2361 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002362}
2363
2364/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002365 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002366 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002367 *
2368 * Descending order
2369 * ------------->
2370 * range1 range2
2371 * ..........|--------|..............|--------|
2372 * ^ ^ ^ ^
2373 * | | | |
2374 * last1 first1 last2 first2
2375 * ..........+--------+--------------+--------+......
2376 * diff1 gap12 diff2
2377 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002378 * To encode the previous list of ranges we must encode integers as follows in
2379 * descending order:
2380 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002381 * with diff1 = last1 - first1
2382 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002383 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002384 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002385 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002386int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2387 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002388{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002389 struct eb64_node *le;
2390 struct quic_arng_node *new_node;
2391 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002392
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002393 new = NULL;
2394 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002395 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002396 if (!new_node)
2397 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002398
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002399 goto out;
2400 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002401
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002402 le = eb64_lookup_le(&arngs->root, ar->first);
2403 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002404 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002405 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002406 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002407 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002408 else {
2409 struct quic_arng_node *le_ar =
2410 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002411
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002412 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002413 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002414 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002415
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002416 if (le_ar->last + 1 >= ar->first) {
2417 le_ar->last = ar->last;
2418 new = le;
2419 new_node = le_ar;
2420 }
2421 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002422 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002423 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002424 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002425
2426 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002427 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002428 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002429
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002430 /* Verify that the new inserted node does not overlap the nodes
2431 * which follow it.
2432 */
2433 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002434 struct eb64_node *next;
2435 struct quic_arng_node *next_node;
2436
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002437 while ((next = eb64_next(new))) {
2438 next_node =
2439 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002440 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002441 break;
2442
2443 if (next_node->last > new_node->last)
2444 new_node->last = next_node->last;
2445 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002446 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002447 /* Decrement the size of these ranges. */
2448 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002449 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002450 }
2451
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002452 quic_arngs_set_enc_sz(arngs);
2453
2454 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002455 return 1;
2456}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002457/* Remove the header protection of packets at <el> encryption level.
2458 * Always succeeds.
2459 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002460static inline void qc_rm_hp_pkts(struct quic_enc_level *el, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002461{
2462 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002463 struct quic_rx_packet *pqpkt;
2464 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002465 struct quic_enc_level *app_qel;
2466
2467 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2468 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2469 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002470 if (el == app_qel && objt_listener(ctx->conn->target) &&
2471 ctx->conn->qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002472 TRACE_PROTO("hp not removed (handshake not completed)",
2473 QUIC_EV_CONN_ELRMHP, ctx->conn);
2474 goto out;
2475 }
2476 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002477 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002478 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2479 pqpkt->data + pqpkt->pn_offset,
2480 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2481 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2482 /* XXX TO DO XXX */
2483 }
2484 else {
2485 /* The AAD includes the packet number field */
2486 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2487 /* Store the packet into the tree of packets to decrypt. */
2488 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002489 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002490 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002491 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2493 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002494 MT_LIST_DELETE_SAFE(pkttmp1);
2495 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002496 }
2497
2498 out:
2499 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2500}
2501
2502/* Process all the CRYPTO frame at <el> encryption level.
2503 * Return 1 if succeeded, 0 if not.
2504 */
2505static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002506 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002507{
2508 struct eb64_node *node;
2509
2510 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002511 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002512 node = eb64_first(&el->rx.crypto.frms);
2513 while (node) {
2514 struct quic_rx_crypto_frm *cf;
2515
2516 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2517 if (cf->offset_node.key != el->rx.crypto.offset)
2518 break;
2519
2520 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2521 goto err;
2522
2523 node = eb64_next(node);
2524 quic_rx_packet_refdec(cf->pkt);
2525 eb64_delete(&cf->offset_node);
2526 pool_free(pool_head_quic_rx_crypto_frm, cf);
2527 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002528 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002529 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2530 return 1;
2531
2532 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002533 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2535 return 0;
2536}
2537
2538/* Process all the packets at <el> encryption level.
2539 * Return 1 if succeeded, 0 if not.
2540 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002541int qc_treat_rx_pkts(struct quic_enc_level *el, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002542{
2543 struct quic_tls_ctx *tls_ctx;
2544 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002545 int64_t largest_pn = -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002546
2547 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2548 tls_ctx = &el->tls_ctx;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002549 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002550 node = eb64_first(&el->rx.pkts);
2551 while (node) {
2552 struct quic_rx_packet *pkt;
2553
2554 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
2555 if (!qc_pkt_decrypt(pkt, tls_ctx)) {
2556 /* Drop the packet */
2557 TRACE_PROTO("packet decryption failed -> dropped",
2558 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2559 }
2560 else {
Frédéric Lécaillec4b93ea2021-06-04 10:12:43 +02002561 if (!qc_parse_pkt_frms(pkt, ctx, el)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002562 /* Drop the packet */
2563 TRACE_PROTO("packet parsing failed -> dropped",
2564 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2565 }
2566 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002567 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2568
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002569 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
2570 !(HA_ATOMIC_ADD_FETCH(&el->pktns->rx.nb_ack_eliciting, 1) & 1))
2571 HA_ATOMIC_OR(&el->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002572
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002573 if (pkt->pn > largest_pn)
2574 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002575 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille654c6912021-06-04 10:27:23 +02002576 if (!quic_update_ack_ranges_list(&el->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002577 TRACE_DEVEL("Could not update ack range list",
2578 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002579 }
2580 }
2581 node = eb64_next(node);
2582 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002583 }
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002584 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002585
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002586 /* Update the largest packet number. */
2587 if (largest_pn != -1)
2588 HA_ATOMIC_UPDATE_MAX(&el->pktns->rx.largest_pn, largest_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002589 if (!qc_treat_rx_crypto_frms(el, ctx))
2590 goto err;
2591
2592 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2593 return 1;
2594
2595 err:
2596 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2597 return 0;
2598}
2599
2600/* Called during handshakes to parse and build Initial and Handshake packets for QUIC
2601 * connections with <ctx> as I/O handler context.
2602 * Returns 1 if succeeded, 0 if not.
2603 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002604int qc_do_hdshk(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002605{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002606 int ret, ssl_err;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002607 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002608 enum quic_tls_enc_level tel, next_tel;
2609 struct quic_enc_level *qel, *next_qel;
2610 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002611 struct qring *qr; // Tx ring
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002613 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002614 qr = NULL;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002615 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002616 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002617 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc->state))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002618 goto err;
2619
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002620 qel = &qc->els[tel];
2621 next_qel = &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002622
2623 next_level:
2624 tls_ctx = &qel->tls_ctx;
2625
2626 /* If the header protection key for this level has been derived,
2627 * remove the packet header protections.
2628 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002629 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002630 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2631 qc_rm_hp_pkts(qel, ctx);
2632
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002633 if (!qc_treat_rx_pkts(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002634 goto err;
2635
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002636 if (!qr)
2637 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2638 ret = qc_prep_hdshk_pkts(qr, ctx);
2639 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002640 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002641 else if (ret == 0)
2642 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002643
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002644 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002645 goto err;
2646
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002647 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002648 /* Check if there is something to do for the next level.
2649 */
2650 if ((next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002651 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652 qel = next_qel;
2653 goto next_level;
2654 }
2655
2656 /* If the handshake has not been completed -> out! */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002657 if (qc->state < QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002658 goto out;
2659
2660 /* Discard the Handshake keys. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002661 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2662 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002663 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002664 if (!quic_build_post_handshake_frames(qc) ||
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002665 !qc_prep_phdshk_pkts(qr, qc) ||
2666 !qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002667 goto err;
2668
2669 out:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002670 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002671 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002672 return 1;
2673
2674 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002675 if (qr)
2676 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002677 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002678 return 0;
2679}
2680
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002681/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002682static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2683{
2684 int i;
2685
2686 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2687 if (qel->tx.crypto.bufs[i]) {
2688 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2689 qel->tx.crypto.bufs[i] = NULL;
2690 }
2691 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002692 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002693}
2694
2695/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002696 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002697 * Returns 1 if succeeded, 0 if not.
2698 */
2699static int quic_conn_enc_level_init(struct quic_conn *qc,
2700 enum quic_tls_enc_level level)
2701{
2702 struct quic_enc_level *qel;
2703
2704 qel = &qc->els[level];
2705 qel->level = quic_to_ssl_enc_level(level);
2706 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2707 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2708 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2709 qel->tls_ctx.rx.flags = 0;
2710 qel->tls_ctx.tx.flags = 0;
2711
2712 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002713 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002714 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002715 qel->rx.crypto.offset = 0;
2716 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2717 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002718
2719 /* Allocate only one buffer. */
2720 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2721 if (!qel->tx.crypto.bufs)
2722 goto err;
2723
2724 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2725 if (!qel->tx.crypto.bufs[0])
2726 goto err;
2727
2728 qel->tx.crypto.bufs[0]->sz = 0;
2729 qel->tx.crypto.nb_buf = 1;
2730
2731 qel->tx.crypto.sz = 0;
2732 qel->tx.crypto.offset = 0;
2733
2734 return 1;
2735
2736 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002737 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002738 return 0;
2739}
2740
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002741/* Release all the memory allocated for <conn> QUIC connection. */
2742static void quic_conn_free(struct quic_conn *conn)
2743{
2744 int i;
2745
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002746 if (!conn)
2747 return;
2748
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002749 free_quic_conn_cids(conn);
2750 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2751 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002752 if (conn->timer_task)
2753 task_destroy(conn->timer_task);
2754 pool_free(pool_head_quic_conn, conn);
2755}
2756
2757/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002758static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002759{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002760 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002761 struct quic_conn *qc;
2762 struct quic_pktns *pktns;
2763
2764
2765 conn_ctx = task->context;
2766 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002767 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2768 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002769 task->expire = TICK_ETERNITY;
2770 pktns = quic_loss_pktns(qc);
2771 if (tick_isset(pktns->tx.loss_time)) {
2772 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2773
2774 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2775 if (!LIST_ISEMPTY(&lost_pkts))
2776 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2777 qc_set_timer(conn_ctx);
2778 goto out;
2779 }
2780
2781 if (qc->path->in_flight) {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002782 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002783 pktns->tx.pto_probe = 1;
2784 }
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002785 else if (objt_server(qc->conn->target) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002786 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2787 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2788
2789 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2790 hel->pktns->tx.pto_probe = 1;
2791 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2792 iel->pktns->tx.pto_probe = 1;
2793 }
2794 qc->tx.nb_pto_dgrams = QUIC_MAX_NB_PTO_DGRAMS;
2795 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2796 qc->path->loss.pto_count++;
2797
2798 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002799 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002800
2801 return task;
2802}
2803
2804/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2805 * connections used to identify the first Initial packets of client connecting
2806 * to listeners. This parameter must be NULL for QUIC connections attached
2807 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2808 * <scid> is the source connection ID with <scid_len> as length.
2809 * Returns 1 if succeeded, 0 if not.
2810 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002811static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2812 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002813 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002814{
2815 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002816 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002817 /* Initial CID. */
2818 struct quic_connection_id *icid;
2819
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002820 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002821 qc = pool_zalloc(pool_head_quic_conn);
2822 if (!qc) {
2823 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2824 goto err;
2825 }
2826
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002827 qc->cids = EB_ROOT;
2828 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002829 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002830 struct listener *l = owner;
2831
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002832 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002833 /* Copy the initial DCID. */
2834 qc->odcid.len = dcid_len;
2835 if (qc->odcid.len)
2836 memcpy(qc->odcid.data, dcid, dcid_len);
2837
2838 /* Copy the SCID as our DCID for this connection. */
2839 if (scid_len)
2840 memcpy(qc->dcid.data, scid, scid_len);
2841 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002842 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002843 }
2844 /* QUIC Client (outgoing connection to servers) */
2845 else {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002846 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002847 if (dcid_len)
2848 memcpy(qc->dcid.data, dcid, dcid_len);
2849 qc->dcid.len = dcid_len;
2850 }
2851
2852 /* Initialize the output buffer */
2853 qc->obuf.pos = qc->obuf.data;
2854
2855 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002856 if (!icid) {
2857 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2858 goto err;
2859 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002860
2861 /* Select our SCID which is the first CID with 0 as sequence number. */
2862 qc->scid = icid->cid;
2863
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002864 /* Packet number spaces initialization. */
2865 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2866 quic_pktns_init(&qc->pktns[i]);
2867 /* QUIC encryption level context initialization. */
2868 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002869 if (!quic_conn_enc_level_init(qc, i)) {
2870 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002871 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002872 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002873 /* Initialize the packet number space. */
2874 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2875 }
2876
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02002877 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002878 /* TX part. */
2879 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002880 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2881 qc->tx.wbuf = qc->tx.rbuf = 0;
2882 qc->tx.bytes = 0;
2883 qc->tx.nb_pto_dgrams = 0;
2884 /* RX part. */
2885 qc->rx.bytes = 0;
2886
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002887 /* XXX TO DO: Only one path at this time. */
2888 qc->path = &qc->paths[0];
2889 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2890
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002891 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002892
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002893 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002894
2895 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002896 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002897 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002898 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002899}
2900
2901/* Initialize the timer task of <qc> QUIC connection.
2902 * Returns 1 if succeeded, 0 if not.
2903 */
2904static int quic_conn_init_timer(struct quic_conn *qc)
2905{
2906 qc->timer_task = task_new(MAX_THREADS_MASK);
2907 if (!qc->timer_task)
2908 return 0;
2909
2910 qc->timer = TICK_ETERNITY;
2911 qc->timer_task->process = process_timer;
2912 qc->timer_task->context = qc->conn->xprt_ctx;
2913
2914 return 1;
2915}
2916
2917/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
2918 * past one byte of this buffer.
2919 */
2920static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
2921 struct quic_rx_packet *pkt)
2922{
2923 unsigned char dcid_len, scid_len;
2924
2925 /* Version */
2926 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
2927 return 0;
2928
2929 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
2930
2931 /* Destination Connection ID Length */
2932 dcid_len = *(*buf)++;
2933 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
2934 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
2935 /* XXX MUST BE DROPPED */
2936 return 0;
2937
2938 if (dcid_len) {
2939 /* Check that the length of this received DCID matches the CID lengths
2940 * of our implementation for non Initials packets only.
2941 */
2942 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
2943 return 0;
2944
2945 memcpy(pkt->dcid.data, *buf, dcid_len);
2946 }
2947
2948 pkt->dcid.len = dcid_len;
2949 *buf += dcid_len;
2950
2951 /* Source Connection ID Length */
2952 scid_len = *(*buf)++;
2953 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
2954 /* XXX MUST BE DROPPED */
2955 return 0;
2956
2957 if (scid_len)
2958 memcpy(pkt->scid.data, *buf, scid_len);
2959 pkt->scid.len = scid_len;
2960 *buf += scid_len;
2961
2962 return 1;
2963}
2964
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002965/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
2966 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
2967 * encryption level matching with the packet type. <*qel> may be null if not found.
2968 * Note that <ctx> may be null (for Initial packets).
2969 */
2970static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002971 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002972 struct quic_enc_level **qel)
2973{
2974 enum quic_tls_enc_level tel;
2975
2976 /* Special case without connection context (firt Initial packets) */
2977 if (!ctx) {
2978 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2979 return 1;
2980 }
2981
2982 tel = quic_packet_type_enc_level(pkt->type);
2983 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
2984 *qel = NULL;
2985 return 0;
2986 }
2987
2988 *qel = &qc->els[tel];
2989 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
2990 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
2991 return 0;
2992 }
2993
2994 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002995 (tel != QUIC_TLS_ENC_LEVEL_APP || ctx->conn->qc->state >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002996 return 1;
2997
2998 return 0;
2999}
3000
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003001/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3002 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3003 * byte past the end of the buffer containing this packet and <beg> the address of
3004 * the packet first byte.
3005 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3006 * Returns 1 if succeeded, 0 if not.
3007 */
3008static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3009 unsigned char **buf, unsigned char *beg,
3010 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003011 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003012{
3013 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003014 struct quic_enc_level *qel;
3015 /* Only for traces. */
3016 struct quic_rx_packet *qpkt_trace;
3017
3018 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003019 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003020 /* The packet number is here. This is also the start minus
3021 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3022 * protection.
3023 */
3024 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003025 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026 /* Note that the following function enables us to unprotect the packet
3027 * number and its length subsequently used to decrypt the entire
3028 * packets.
3029 */
3030 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3031 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003032 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003033 goto err;
3034 }
3035
3036 /* The AAD includes the packet number field found at <pn>. */
3037 pkt->aad_len = pn - beg + pkt->pnl;
3038 qpkt_trace = pkt;
3039 /* Store the packet */
3040 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003041 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003042 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003043 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003045 else if (qel) {
3046 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003047 pkt->pn_offset = pn - beg;
3048 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3049 }
3050
3051 memcpy(pkt->data, beg, pkt->len);
3052 /* Updtate the offset of <*buf> for the next QUIC packet. */
3053 *buf = beg + pkt->len;
3054
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003055 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003056 return 1;
3057
3058 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003059 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003060 return 0;
3061}
3062
3063/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3064 * Also set <*long_header> to 1 if this form is long, 0 if not.
3065 */
3066static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3067 unsigned char byte0, int *long_header)
3068{
3069 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3070 pkt->type =
3071 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3072 *long_header = 1;
3073 }
3074 else {
3075 pkt->type = QUIC_PACKET_TYPE_SHORT;
3076 *long_header = 0;
3077 }
3078}
3079
3080static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3081 struct quic_rx_packet *pkt,
3082 struct quic_dgram_ctx *dgram_ctx,
3083 struct sockaddr_storage *saddr)
3084{
3085 unsigned char *beg;
3086 uint64_t len;
3087 struct quic_conn *qc;
3088 struct eb_root *cids;
3089 struct ebmb_node *node;
3090 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003091 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003092 int long_header;
3093
3094 qc = NULL;
3095 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3096 if (end <= *buf)
3097 goto err;
3098
3099 /* Fixed bit */
3100 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3101 /* XXX TO BE DISCARDED */
3102 goto err;
3103
3104 srv_conn = dgram_ctx->owner;
3105 beg = *buf;
3106 /* Header form */
3107 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3108 if (long_header) {
3109 size_t cid_lookup_len;
3110
3111 if (!quic_packet_read_long_header(buf, end, pkt))
3112 goto err;
3113
3114 /* For Initial packets, and for servers (QUIC clients connections),
3115 * there is no Initial connection IDs storage.
3116 */
3117 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3118 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3119 cid_lookup_len = pkt->dcid.len;
3120 }
3121 else {
3122 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3123 cid_lookup_len = QUIC_CID_LEN;
3124 }
3125
3126 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3127 if (!node)
3128 goto err;
3129
3130 qc = ebmb_entry(node, struct quic_conn, scid_node);
3131
3132 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3133 qc->dcid.len = pkt->scid.len;
3134 if (pkt->scid.len)
3135 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3136 }
3137
3138 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3139 uint64_t token_len;
3140
3141 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3142 goto err;
3143
3144 /* XXX TO DO XXX 0 value means "the token is not present".
3145 * A server which sends an Initial packet must not set the token.
3146 * So, a client which receives an Initial packet with a token
3147 * MUST discard the packet or generate a connection error with
3148 * PROTOCOL_VIOLATION as type.
3149 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3150 */
3151 pkt->token_len = token_len;
3152 }
3153 }
3154 else {
3155 /* XXX TO DO: Short header XXX */
3156 if (end - *buf < QUIC_CID_LEN)
3157 goto err;
3158
3159 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3160 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3161 if (!node)
3162 goto err;
3163
3164 qc = ebmb_entry(node, struct quic_conn, scid_node);
3165 *buf += QUIC_CID_LEN;
3166 }
3167 /* Store the DCID used for this packet to check the packet which
3168 * come in this UDP datagram match with it.
3169 */
3170 if (!dgram_ctx->dcid_node)
3171 dgram_ctx->dcid_node = node;
3172 /* Only packets packets with long headers and not RETRY or VERSION as type
3173 * have a length field.
3174 */
3175 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3176 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3177 goto err;
3178
3179 pkt->len = len;
3180 }
3181 else if (!long_header) {
3182 /* A short packet is the last one of an UDP datagram. */
3183 pkt->len = end - *buf;
3184 }
3185
3186 conn_ctx = qc->conn->xprt_ctx;
3187
3188 /* Increase the total length of this packet by the header length. */
3189 pkt->len += *buf - beg;
3190 /* Do not check the DCID node before the length. */
3191 if (dgram_ctx->dcid_node != node) {
3192 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3193 goto err;
3194 }
3195
3196 if (pkt->len > sizeof pkt->data) {
3197 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3198 goto err;
3199 }
3200
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003201 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003202 goto err;
3203
3204 /* Wake the tasklet of the QUIC connection packet handler. */
3205 if (conn_ctx)
3206 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3207
3208 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3209
3210 return pkt->len;
3211
3212 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003213 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003214 return -1;
3215}
3216
3217static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3218 struct quic_rx_packet *pkt,
3219 struct quic_dgram_ctx *dgram_ctx,
3220 struct sockaddr_storage *saddr)
3221{
3222 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003223 struct quic_conn *qc;
3224 struct eb_root *cids;
3225 struct ebmb_node *node;
3226 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003227 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003228 int long_header = 0;
3229
3230 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003231 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003232 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003233 if (end <= *buf)
3234 goto err;
3235
3236 /* Fixed bit */
3237 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3238 /* XXX TO BE DISCARDED */
3239 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3240 goto err;
3241 }
3242
3243 l = dgram_ctx->owner;
3244 beg = *buf;
3245 /* Header form */
3246 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3247 if (long_header) {
3248 unsigned char dcid_len;
3249
3250 if (!quic_packet_read_long_header(buf, end, pkt)) {
3251 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3252 goto err;
3253 }
3254
3255 dcid_len = pkt->dcid.len;
3256 /* For Initial packets, and for servers (QUIC clients connections),
3257 * there is no Initial connection IDs storage.
3258 */
3259 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003260 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003261 /* DCIDs of first packets coming from clients may have the same values.
3262 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3263 */
3264 quic_cid_saddr_cat(&pkt->dcid, saddr);
3265 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003266
3267 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3268 end - *buf < token_len) {
3269 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3270 goto err;
3271 }
3272
3273 /* XXX TO DO XXX 0 value means "the token is not present".
3274 * A server which sends an Initial packet must not set the token.
3275 * So, a client which receives an Initial packet with a token
3276 * MUST discard the packet or generate a connection error with
3277 * PROTOCOL_VIOLATION as type.
3278 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3279 */
3280 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003281 }
3282 else {
3283 if (pkt->dcid.len != QUIC_CID_LEN) {
3284 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3285 goto err;
3286 }
3287
3288 cids = &l->rx.cids;
3289 }
3290
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003291 /* Only packets packets with long headers and not RETRY or VERSION as type
3292 * have a length field.
3293 */
3294 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3295 uint64_t len;
3296
3297 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3298 end - *buf < len) {
3299 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3300 goto err;
3301 }
3302
3303 pkt->len = len;
3304 }
3305
3306
3307 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003308 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3309 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3310 cids == &l->rx.odcids) {
3311 /* Switch to the definitive tree ->cids containing the final CIDs. */
3312 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3313 if (node) {
3314 /* If found, signal this with NULL as special value for <cids>. */
3315 pkt->dcid.len = dcid_len;
3316 cids = NULL;
3317 }
3318 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003319 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003320
3321 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003322 int ipv4;
3323 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003324 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003325
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003326 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3327 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3328 goto err;
3329 }
3330
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003331 pkt->saddr = *saddr;
3332 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3333 * of <saddr>.
3334 */
3335 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003336 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003337 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003338 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003339 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003340 goto err;
3341
3342 odcid = &qc->rx.params.original_destination_connection_id;
3343 /* Copy the transport parameters. */
3344 qc->rx.params = l->bind_conf->quic_params;
3345 /* Copy original_destination_connection_id transport parameter. */
3346 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3347 odcid->len = pkt->odcid_len;
3348 /* Copy the initial source connection ID. */
3349 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3350 qc->enc_params_len =
3351 quic_transport_params_encode(qc->enc_params,
3352 qc->enc_params + sizeof qc->enc_params,
3353 &qc->rx.params, 1);
3354 if (!qc->enc_params_len)
3355 goto err;
3356
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003357 /* NOTE: the socket address has been concatenated to the destination ID
3358 * chosen by the client for Initial packets.
3359 */
3360 if (!qc_new_isecs(qc, pkt->dcid.data, pkt->odcid_len, 1)) {
3361 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3362 goto err;
3363 }
3364
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003365 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003366 /* This is the DCID node sent in this packet by the client. */
3367 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003368 /* Enqueue this packet. */
3369 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3370 /* Try to accept a new connection. */
3371 listener_accept(l);
3372
3373 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3374 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3375 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3376 /* Insert our SCID, the connection ID for the QUIC client. */
3377 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3378 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3379 if (n != &qc->scid_node) {
3380 quic_conn_free(qc);
3381 qc = ebmb_entry(n, struct quic_conn, scid_node);
3382 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003383 }
3384 else {
3385 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3386 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3387 else
3388 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003389 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003390 }
3391 }
3392 else {
3393 if (end - *buf < QUIC_CID_LEN) {
3394 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3395 goto err;
3396 }
3397
3398 cids = &l->rx.cids;
3399 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3400 if (!node) {
3401 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3402 goto err;
3403 }
3404
3405 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003406 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003407 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003408 /* A short packet is the last one of an UDP datagram. */
3409 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003410 }
3411
3412 /* Store the DCID used for this packet to check the packet which
3413 * come in this UDP datagram match with it.
3414 */
3415 if (!dgram_ctx->dcid_node) {
3416 dgram_ctx->dcid_node = node;
3417 dgram_ctx->qc = qc;
3418 }
3419
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003420 /* Increase the total length of this packet by the header length. */
3421 pkt->len += *buf - beg;
3422 /* Do not check the DCID node before the length. */
3423 if (dgram_ctx->dcid_node != node) {
3424 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3425 goto err;
3426 }
3427
3428 if (pkt->len > sizeof pkt->data) {
3429 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3430 goto err;
3431 }
3432
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003433 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3434 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3435 goto err;
3436 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003437
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003438
3439 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003440 /* Wake up the connection packet handler task from here only if all
3441 * the contexts have been initialized, especially the mux context
3442 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3443 * will start it if these contexts for the connection are not already
3444 * initialized.
3445 */
3446 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003447 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003448
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003449 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3450
3451 return pkt->len;
3452
3453 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003454 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003455 qc ? qc->conn : NULL, pkt);
3456 return -1;
3457}
3458
3459/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3460 * in advance. This is the reponsability of the caller to check there is enough room in this
3461 * buffer to build a long header.
3462 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3463 */
3464static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3465 int type, size_t pn_len, struct quic_conn *conn)
3466{
3467 if (type > QUIC_PACKET_TYPE_RETRY)
3468 return 0;
3469
3470 /* #0 byte flags */
3471 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3472 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3473 /* Version */
3474 quic_write_uint32(buf, end, conn->version);
3475 *(*buf)++ = conn->dcid.len;
3476 /* Destination connection ID */
3477 if (conn->dcid.len) {
3478 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3479 *buf += conn->dcid.len;
3480 }
3481 /* Source connection ID */
3482 *(*buf)++ = conn->scid.len;
3483 if (conn->scid.len) {
3484 memcpy(*buf, conn->scid.data, conn->scid.len);
3485 *buf += conn->scid.len;
3486 }
3487
3488 return 1;
3489}
3490
3491/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3492 * in advance. This is the reponsability of the caller to check there is enough room in this
3493 * buffer to build a long header.
3494 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3495 */
3496static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3497 size_t pn_len, struct quic_conn *conn)
3498{
3499 /* #0 byte flags */
3500 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3501 /* Destination connection ID */
3502 if (conn->dcid.len) {
3503 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3504 *buf += conn->dcid.len;
3505 }
3506
3507 return 1;
3508}
3509
3510/* Apply QUIC header protection to the packet with <buf> as first byte address,
3511 * <pn> as address of the Packet number field, <pnlen> being this field length
3512 * with <aead> as AEAD cipher and <key> as secret key.
3513 * Returns 1 if succeeded or 0 if failed.
3514 */
3515static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3516 const EVP_CIPHER *aead, const unsigned char *key)
3517{
3518 int i, ret, outlen;
3519 EVP_CIPHER_CTX *ctx;
3520 /* We need an IV of at least 5 bytes: one byte for bytes #0
3521 * and at most 4 bytes for the packet number
3522 */
3523 unsigned char mask[5] = {0};
3524
3525 ret = 0;
3526 ctx = EVP_CIPHER_CTX_new();
3527 if (!ctx)
3528 return 0;
3529
3530 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3531 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3532 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3533 goto out;
3534
3535 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3536 for (i = 0; i < pnlen; i++)
3537 pn[i] ^= mask[i + 1];
3538
3539 ret = 1;
3540
3541 out:
3542 EVP_CIPHER_CTX_free(ctx);
3543
3544 return ret;
3545}
3546
3547/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3548 * ACK ranges if needed to a value below <limit> in bytes.
3549 * Return 1 if succeeded, 0 if not.
3550 */
3551static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3552{
3553 size_t room, ack_delay_sz;
3554
3555 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3556 /* A frame is made of 1 byte for the frame type. */
3557 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003558 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003559 return 0;
3560
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003561 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003562}
3563
3564/* Prepare as most as possible CRYPTO frames from prebuilt CRYPTO frames for <qel>
3565 * encryption level to be encoded in a buffer with <room> as available room,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003566 * and <*len> the packet Length field initialized with the number of bytes already present
3567 * in this buffer which must be taken into an account for the Length packet field value.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003568 * <headlen> is the number of bytes already present in this packet before building
Frédéric Lécailleea604992020-12-24 13:01:37 +01003569 * CRYPTO frames.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003570 * This is the responsibility of the caller to check that <*len> < <room> as this is
3571 * the responsibility to check that <headlen> < quic_path_prep_data(conn->path).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003572 * Update consequently <*len> to reflect the size of these CRYPTO frames built
3573 * by this function. Also attach these CRYPTO frames to <pkt> QUIC packet.
3574 * Return 1 if succeeded, 0 if not.
3575 */
3576static inline int qc_build_cfrms(struct quic_tx_packet *pkt,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003577 size_t room, size_t *len, size_t headlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003578 struct quic_enc_level *qel,
3579 struct quic_conn *conn)
3580{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003581 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003582 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003583 struct mt_list *tmp1, tmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003584
Frédéric Lécailleea604992020-12-24 13:01:37 +01003585 ret = 0;
3586 /* If we are not probing we must take into an account the congestion
3587 * control window.
3588 */
3589 if (!conn->tx.nb_pto_dgrams)
3590 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003591 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003592 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003593 mt_list_for_each_entry_safe(cf, &qel->pktns->tx.frms, mt_list, tmp1, tmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003594 /* header length, data length, frame length. */
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003595 size_t hlen, dlen, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003596
Frédéric Lécailleea604992020-12-24 13:01:37 +01003597 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003598 break;
3599
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003600 switch (cf->type) {
3601 case QUIC_FT_CRYPTO:
3602 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3603 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3604 /* Compute the length of this CRYPTO frame header */
3605 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3606 /* Compute the data length of this CRyPTO frame. */
3607 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3608 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3609 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3610 if (!dlen)
3611 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003613 pkt->cdata_len += dlen;
3614 /* CRYPTO frame length. */
3615 flen = hlen + quic_int_getsize(dlen) + dlen;
3616 TRACE_PROTO(" CRYPTO frame length (flen)",
3617 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3618 /* Add the CRYPTO data length and its encoded length to the packet
3619 * length and the length of this length.
3620 */
3621 *len += flen;
3622 room -= flen;
3623 if (dlen == cf->crypto.len) {
3624 /* <cf> CRYPTO data have been consumed. */
3625 MT_LIST_DELETE_SAFE(tmp1);
3626 LIST_APPEND(&pkt->frms, &cf->list);
3627 }
3628 else {
3629 struct quic_frame *new_cf;
3630
3631 new_cf = pool_alloc(pool_head_quic_frame);
3632 if (!new_cf) {
3633 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3634 return 0;
3635 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003636
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003637 new_cf->type = QUIC_FT_CRYPTO;
3638 new_cf->crypto.len = dlen;
3639 new_cf->crypto.offset = cf->crypto.offset;
3640 new_cf->crypto.qel = qel;
3641 LIST_APPEND(&pkt->frms, &new_cf->list);
3642 /* Consume <dlen> bytes of the current frame. */
3643 cf->crypto.len -= dlen;
3644 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003645 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003646 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003647
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003648 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
3649 break;
3650
3651 default:
3652 flen = qc_frm_len(cf);
3653 BUG_ON(!flen);
3654 if (flen > room)
3655 continue;
3656
3657 *len += flen;
3658 room -= flen;
3659 MT_LIST_DELETE_SAFE(tmp1);
3660 LIST_APPEND(&pkt->frms, &cf->list);
3661 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003662 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003663 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003664 }
3665
Frédéric Lécailleea604992020-12-24 13:01:37 +01003666 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003667}
3668
3669/* This function builds a clear handshake packet used during a QUIC TLS handshakes
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003670 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption level
3671 * for <conn> QUIC connection and <qel> as QUIC TLS encryption level, filling the buffer
3672 * with as much as CRYPTO.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003673 * 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 +02003674 * reserved so that to ensure there is enough room to build this AEAD TAG after
3675 * having successfully returned from this function and to ensure the position
3676 * pointer <pos> may be safely incremented by QUIC_TLS_TAG_LEN.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003677 * This function also update the value of <buf_pn> pointer to point to the packet
3678 * number field in this packet. <pn_len> will also have the packet number
3679 * length as value.
3680 *
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003681 * Return 1 packet if succeeded or 0 if failed (not enough room in the buffer to build
3682 * this packet, QUIC_TLS_TAG_LEN bytes for the encryption TAG included).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003683 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003684static int qc_do_build_hdshk_pkt(unsigned char *pos, const unsigned char *end,
3685 struct quic_tx_packet *pkt, int pkt_type,
3686 int64_t pn, size_t *pn_len,
3687 unsigned char **buf_pn,
3688 struct quic_enc_level *qel,
3689 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003690{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003691 unsigned char *beg;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003692 size_t len, len_frms, token_fields_len, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003693 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3694 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003695 size_t ack_frm_len;
3696 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003697 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003698
Frédéric Lécailleea604992020-12-24 13:01:37 +01003699 /* Length field value with CRYPTO frames if present. */
3700 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003701 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003702 /* When not probing and not acking, reduce the size of this buffer to respect
3703 * the congestion controller window.
3704 */
3705 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3706 size_t path_room;
3707
3708 path_room = quic_path_prep_data(conn->path);
3709 if (end - beg > path_room)
3710 end = beg + path_room;
3711 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712
3713 /* For a server, the token field of an Initial packet is empty. */
3714 token_fields_len = pkt_type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3715
3716 /* Check there is enough room to build the header followed by a token. */
3717 if (end - pos < QUIC_LONG_PACKET_MINLEN + conn->dcid.len +
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003718 conn->scid.len + token_fields_len + QUIC_TLS_TAG_LEN) {
3719 ssize_t room = end - pos;
3720 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3721 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003722 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003723 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003724
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003725 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003726 /* packet number length */
3727 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3728
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003729 if (pkt_type == QUIC_PACKET_TYPE_SHORT)
3730 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3731 else
3732 quic_build_packet_long_header(&pos, end, pkt_type, *pn_len, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003733
3734 /* Encode the token length (0) for an Initial packet. */
3735 if (pkt_type == QUIC_PACKET_TYPE_INITIAL)
3736 *pos++ = 0;
3737
3738 /* Build an ACK frame if required. */
3739 ack_frm_len = 0;
3740 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003741 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003742 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003743 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003744 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003745 if (!ack_frm_len) {
3746 ssize_t room = end - pos;
3747 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3748 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003749 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003750 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003751
3752 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3753 }
3754
3755 /* Length field value without the CRYPTO frames data length. */
3756 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003757 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003758 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003759
3760 len_frms = len + QUIC_TLS_TAG_LEN;
3761 if (!qc_build_cfrms(pkt, end - pos, &len_frms, pos - beg, qel, conn)) {
3762 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3763 conn->conn, NULL, NULL, &room);
3764 goto err;
3765 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003766 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003767
3768 add_ping_frm = 0;
3769 padding_len = 0;
3770 if (objt_server(conn->conn->target) &&
3771 pkt_type == QUIC_PACKET_TYPE_INITIAL &&
3772 len < QUIC_INITIAL_PACKET_MINLEN) {
3773 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
3774 }
3775 else if (LIST_ISEMPTY(&pkt->frms)) {
3776 if (qel->pktns->tx.pto_probe) {
3777 /* If we cannot send a CRYPTO frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003778 add_ping_frm = 1;
3779 len += 1;
3780 }
3781 /* If there is no frame at all to follow, add at least a PADDING frame. */
3782 if (!ack_frm_len)
3783 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
3784 }
3785
3786 /* Length (of the remaining data). Must not fail because, the buffer size
3787 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
3788 * for the encryption TAG. It must be taken into an account for the length
3789 * of this packet.
3790 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01003791 if (len_frms)
3792 len = len_frms;
3793 else
3794 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003795 if (pkt_type != QUIC_PACKET_TYPE_SHORT)
3796 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003797
3798 /* Packet number field address. */
3799 *buf_pn = pos;
3800
3801 /* Packet number encoding. */
3802 quic_packet_number_encode(&pos, end, pn, *pn_len);
3803
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003804 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3805 ssize_t room = end - pos;
3806 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3807 conn->conn, NULL, NULL, &room);
3808 goto err;
3809 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003810
3811 /* Crypto frame */
3812 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003813 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814
3815 list_for_each_entry(cf, &pkt->frms, list) {
3816 crypto->offset = cf->crypto.offset;
3817 crypto->len = cf->crypto.len;
3818 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003819 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3820 ssize_t room = end - pos;
3821 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3822 conn->conn, NULL, NULL, &room);
3823 goto err;
3824 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003825 }
3826 }
3827
3828 /* Build a PING frame if needed. */
3829 if (add_ping_frm) {
3830 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003831 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3832 ssize_t room = end - pos;
3833 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3834 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003835 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003836 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003837 }
3838
3839 /* Build a PADDING frame if needed. */
3840 if (padding_len) {
3841 frm.type = QUIC_FT_PADDING;
3842 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003843 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3844 ssize_t room = end - pos;
3845 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3846 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003847 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003848 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003849 }
3850
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003851 /* Always reset this variable as this function has no idea
3852 * if it was set. It is handle by the loss detection timer.
3853 */
3854 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003855 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003856
3857 out:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003858 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003859
3860 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003861 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003862}
3863
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02003864static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003865{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02003866 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003867 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003868 pkt->cdata_len = 0;
3869 pkt->in_flight_len = 0;
3870 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003871 pkt->next = NULL;
3872 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003873}
3874
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003875/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003876static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
3877{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003878 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003879
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003880 if (!pkt)
3881 return;
3882
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003883 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003884 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003885 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003886 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003887 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003888}
3889
3890/* Build a handshake packet into <buf> packet buffer with <pkt_type> as packet
3891 * type for <qc> QUIC connection from CRYPTO data stream at <*offset> offset to
3892 * be encrypted at <qel> encryption level.
3893 * Return -2 if the packet could not be encrypted for any reason, -1 if there was
3894 * not enough room in <buf> to build the packet, or the size of the built packet
3895 * if succeeded (may be zero if there is too much crypto data in flight to build the packet).
3896 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003897static struct quic_tx_packet *qc_build_hdshk_pkt(unsigned char **pos,
3898 const unsigned char *buf_end,
3899 struct quic_conn *qc, int pkt_type,
3900 struct quic_enc_level *qel, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003901{
3902 /* The pointer to the packet number field. */
3903 unsigned char *buf_pn;
3904 unsigned char *beg, *end, *payload;
3905 int64_t pn;
3906 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003907 struct quic_tls_ctx *tls_ctx;
3908 struct quic_tx_packet *pkt;
3909
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003910 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003911 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003912 pkt = pool_alloc(pool_head_quic_tx_packet);
3913 if (!pkt) {
3914 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003915 *err = -2;
3916 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003917 }
3918
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02003919 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003920 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003921 pn_len = 0;
3922 buf_pn = NULL;
3923 pn = qel->pktns->tx.next_pn + 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003924 if (!qc_do_build_hdshk_pkt(*pos, buf_end, pkt, pkt_type, pn, &pn_len, &buf_pn, qel, qc)) {
3925 *err = -1;
3926 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003927 }
3928
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003929 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003930 payload = buf_pn + pn_len;
3931 payload_len = end - payload;
3932 aad_len = payload - beg;
3933
3934 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003935 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
3936 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003937 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003938 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003939
3940 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003941 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003942 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
3943 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
3944 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003945 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003946 goto err;
3947 }
3948
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003949 /* Now that a correct packet is built, let us consume <*pos> buffer. */
3950 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003951 /* Consume a packet number. */
3952 ++qel->pktns->tx.next_pn;
3953 /* Attach the built packet to its tree. */
3954 pkt->pn_node.key = qel->pktns->tx.next_pn;
3955 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003956 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003957 pkt->in_flight_len = pkt->len;
3958 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003959 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003960 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003961 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
3962
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003963 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003964
3965 err:
3966 free_quic_tx_packet(pkt);
3967 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003968 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003969}
3970
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003971/* Prepare a clear post handhskake packet for <conn> QUIC connection.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003972 * Return the length of this packet if succeeded, -1 <wbuf> was full.
3973 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003974static int qc_do_build_phdshk_apkt(unsigned char *pos, const unsigned char *end,
3975 struct quic_tx_packet *pkt,
3976 int64_t pn, size_t *pn_len,
3977 unsigned char **buf_pn,
3978 struct quic_enc_level *qel,
3979 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003980{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003981 const unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003982 struct quic_frame *frm, *sfrm;
3983 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3984 size_t fake_len, ack_frm_len;
3985 int64_t largest_acked_pn;
3986
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003987 TRACE_ENTER(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003988 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003989 /* When not probing and not acking, reduce the size of this buffer to respect
3990 * the congestion controller window.
3991 */
3992 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3993 size_t path_room;
3994
3995 path_room = quic_path_prep_data(conn->path);
3996 if (end - beg > path_room)
3997 end = beg + path_room;
3998 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003999 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
4000 /* Packet number length */
4001 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
4002 /* Check there is enough room to build this packet (without payload). */
4003 if (end - pos < QUIC_SHORT_PACKET_MINLEN + sizeof_quic_cid(&conn->dcid) +
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004004 *pn_len + QUIC_TLS_TAG_LEN) {
4005 ssize_t room = end - pos;
4006 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
4007 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004008 goto err;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004009 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004010
4011 /* Reserve enough room at the end of the packet for the AEAD TAG. */
4012 end -= QUIC_TLS_TAG_LEN;
4013 quic_build_packet_short_header(&pos, end, *pn_len, conn);
4014 /* Packet number field. */
4015 *buf_pn = pos;
4016 /* Packet number encoding. */
4017 quic_packet_number_encode(&pos, end, pn, *pn_len);
4018
4019 /* Build an ACK frame if required. */
4020 ack_frm_len = 0;
4021 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004022 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004023 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004024 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004025 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
4026 if (!ack_frm_len)
4027 goto err;
4028
4029 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
4030 }
4031
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004032 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4033 ssize_t room = end - pos;
4034 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
4035 conn->conn, NULL, NULL, &room);
4036 goto err;
4037 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004038
4039 fake_len = ack_frm_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004040 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms) &&
Frédéric Lécailleea604992020-12-24 13:01:37 +01004041 !qc_build_cfrms(pkt, end - pos, &fake_len, pos - beg, qel, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004042 ssize_t room = end - pos;
4043 TRACE_PROTO("some CRYPTO frames could not be built",
4044 QUIC_EV_CONN_PAPKT, conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004045 goto err;
4046 }
4047
4048 /* Crypto frame */
4049 if (!LIST_ISEMPTY(&pkt->frms)) {
4050 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4051 struct quic_crypto *crypto = &frm.crypto;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004052 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004053
4054 list_for_each_entry(cf, &pkt->frms, list) {
4055 crypto->offset = cf->crypto.offset;
4056 crypto->len = cf->crypto.len;
4057 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004058 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4059 ssize_t room = end - pos;
4060 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
4061 conn->conn, NULL, NULL, &room);
4062 goto err;
4063 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004064 }
4065 }
4066
4067 /* Encode a maximum of frames. */
4068 list_for_each_entry_safe(frm, sfrm, &conn->tx.frms_to_send, list) {
4069 unsigned char *ppos;
4070
4071 ppos = pos;
4072 if (!qc_build_frm(&ppos, end, frm, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004073 TRACE_DEVEL("Frames not built", QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004074 break;
4075 }
4076
Willy Tarreau2b718102021-04-21 07:32:39 +02004077 LIST_DELETE(&frm->list);
4078 LIST_APPEND(&pkt->frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004079 pos = ppos;
4080 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004081 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004082
4083 out:
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004084 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004085 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004086
4087 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004088 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004089}
4090
4091/* Prepare a post handhskake packet at Application encryption level for <conn>
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004092 * QUIC connection.
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004093 * Return the length if succeeded, -1 if <wbuf> was full, -2 in case of major error
4094 * (allocation or encryption failures).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004095 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004096static struct quic_tx_packet *qc_build_phdshk_apkt(unsigned char **pos,
4097 const unsigned char *buf_end,
4098 struct quic_conn *qc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004099{
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004100 /* A pointer to the packet number field in <buf> */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004101 unsigned char *buf_pn;
4102 unsigned char *beg, *end, *payload;
4103 int64_t pn;
4104 size_t pn_len, aad_len, payload_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004105 struct quic_tls_ctx *tls_ctx;
4106 struct quic_enc_level *qel;
4107 struct quic_tx_packet *pkt;
4108
4109 TRACE_ENTER(QUIC_EV_CONN_PAPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004110 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004111 pkt = pool_alloc(pool_head_quic_tx_packet);
4112 if (!pkt) {
4113 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_PAPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004114 *err = -2;
4115 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004116 }
4117
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004118 quic_tx_packet_init(pkt, QUIC_PACKET_TYPE_SHORT);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004119 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004120 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4121 pn_len = 0;
4122 buf_pn = NULL;
4123 pn = qel->pktns->tx.next_pn + 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004124 if (!qc_do_build_phdshk_apkt(*pos, buf_end, pkt, pn, &pn_len, &buf_pn, qel, qc)) {
4125 *err = -1;
4126 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004127 }
4128
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004129 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004130 payload = buf_pn + pn_len;
4131 payload_len = end - payload;
4132 aad_len = payload - beg;
4133
4134 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004135 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4136 *err = -2;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004137 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004138 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004139
4140 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004141 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004142 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004143 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4144 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_PAPKT, qc->conn);
4145 *err = -2;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004146 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004147 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004148
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004149 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4150 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004151 /* Consume a packet number. */
4152 ++qel->pktns->tx.next_pn;
4153 /* Attach the built packet to its tree. */
4154 pkt->pn_node.key = qel->pktns->tx.next_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004155 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004156 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004157 pkt->in_flight_len = pkt->len;
4158 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004159 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004160 pkt->pktns = qel->pktns;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004161 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, qc->conn, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004162
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004163 return pkt;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004164
4165 err:
4166 free_quic_tx_packet(pkt);
4167 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PAPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004168 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004169}
4170
4171/* Prepare a maximum of QUIC Application level packets from <ctx> QUIC
4172 * connection I/O handler context.
4173 * Returns 1 if succeeded, 0 if not.
4174 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004175int qc_prep_phdshk_pkts(struct qring *qr, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004176{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004177 struct cbuf *cbuf;
4178 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004179 struct quic_enc_level *qel;
4180
4181 TRACE_ENTER(QUIC_EV_CONN_PAPKTS, qc->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004182 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004183 cbuf = qr->cbuf;
4184 pos = cb_wr(cbuf);
4185 end = end_buf = pos + cb_contig_space(cbuf);
4186 while (pos < end_buf) {
4187 int err;
4188 uint16_t dglen;
4189 struct quic_tx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004190
4191 if (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004192 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004193 qc->path->prep_in_flight >= qc->path->cwnd)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004194 TRACE_DEVEL("nothing more to do",
4195 QUIC_EV_CONN_PAPKTS, qc->conn);
4196 break;
4197 }
4198
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004199 /* Leave room for the datagram header */
4200 pos += sizeof dglen + sizeof pkt;
4201 if (end - pos > qc->path->mtu)
4202 end = pos + qc->path->mtu;
4203 pkt = qc_build_phdshk_apkt(&pos, end, qc, &err);
4204 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004205 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004206 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004207 case -2:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004208 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004209 default:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004210 dglen = pkt->len;
4211 qc_set_dg(cbuf, dglen, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004212 }
4213 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004214 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215 TRACE_LEAVE(QUIC_EV_CONN_PAPKTS, qc->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004216 return 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004217
4218 err:
4219 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PAPKTS, qc->conn);
4220 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004221}
4222
4223/* QUIC connection packet handler task. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004224struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004225{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004226 struct ssl_sock_ctx *ctx = context;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004227
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004228 if (ctx->conn->qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004229 qc_do_hdshk(ctx);
4230 }
4231 else {
4232 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004233 struct qring *qr;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004234
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004235 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004236 /* XXX TO DO: may fail!!! XXX */
4237 qc_treat_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_APP], ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004238 qc_prep_phdshk_pkts(qr, qc);
4239 qc_send_ppkts(qr, ctx);
4240 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004241 }
4242
Willy Tarreau74163142021-03-13 11:30:19 +01004243 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004244}
4245
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004246/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4247 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004248 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004249static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4250 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004251{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004252 size_t try, done = 0;
4253
4254 if (!conn_ctrl_ready(conn))
4255 return 0;
4256
4257 if (!fd_recv_ready(conn->handle.fd))
4258 return 0;
4259
4260 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004261
4262 /* read the largest possible block. For this, we perform only one call
4263 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004264 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004265 */
4266 while (count > 0) {
4267 try = b_contig_space(buf);
4268 if (!try)
4269 break;
4270
4271 if (try > count)
4272 try = count;
4273
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004274 b_add(buf, try);
4275 done += try;
4276 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004277 }
4278
4279 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4280 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4281
4282 leave:
4283 return done;
4284
4285 read0:
4286 conn_sock_read0(conn);
4287 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4288
4289 /* Now a final check for a possible asynchronous low-level error
4290 * report. This can happen when a connection receives a reset
4291 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4292 * we might have come from there by just checking POLL_HUP instead
4293 * of recv()'s return value 0, so we have no way to tell there was
4294 * an error without checking.
4295 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004296 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004297 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4298 goto leave;
4299}
4300
4301
4302/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4303 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4304 * other pending data for example, but this flag is ignored at the moment.
4305 * Only one call to send() is performed, unless the buffer wraps, in which case
4306 * a second call may be performed. The connection's flags are updated with
4307 * whatever special event is detected (error, empty). The caller is responsible
4308 * for taking care of those events and avoiding the call if inappropriate. The
4309 * function does not call the connection's polling update function, so the caller
4310 * is responsible for this. It's up to the caller to update the buffer's contents
4311 * based on the return value.
4312 */
4313static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4314{
4315 ssize_t ret;
4316 size_t try, done;
4317 int send_flag;
4318
4319 if (!conn_ctrl_ready(conn))
4320 return 0;
4321
4322 if (!fd_send_ready(conn->handle.fd))
4323 return 0;
4324
4325 done = 0;
4326 /* send the largest possible block. For this we perform only one call
4327 * to send() unless the buffer wraps and we exactly fill the first hunk,
4328 * in which case we accept to do it once again.
4329 */
4330 while (count) {
4331 try = b_contig_data(buf, done);
4332 if (try > count)
4333 try = count;
4334
4335 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4336 if (try < count || flags & CO_SFL_MSG_MORE)
4337 send_flag |= MSG_MORE;
4338
4339 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4340 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4341 if (ret > 0) {
4342 count -= ret;
4343 done += ret;
4344
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004345 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004346 conn->flags |= CO_FL_WAIT_L4L6;
4347 /* if the system buffer is full, don't insist */
4348 if (ret < try)
4349 break;
4350 }
4351 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4352 /* nothing written, we need to poll for write first */
4353 fd_cant_send(conn->handle.fd);
4354 break;
4355 }
4356 else if (errno != EINTR) {
4357 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4358 break;
4359 }
4360 }
4361 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4362 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4363
4364 if (done > 0) {
4365 /* we count the total bytes sent, and the send rate for 32-byte
4366 * blocks. The reason for the latter is that freq_ctr are
4367 * limited to 4GB and that it's not enough per second.
4368 */
4369 _HA_ATOMIC_ADD(&global.out_bytes, done);
4370 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4371 }
4372 return done;
4373}
4374
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004375/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4376 * event subscriber <es> is not allowed to change from a previous call as long
4377 * as at least one event is still subscribed. The <event_type> must only be a
4378 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4379 */
4380static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4381{
4382 return conn_subscribe(conn, xprt_ctx, event_type, es);
4383}
4384
4385/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4386 * The <es> pointer is not allowed to differ from the one passed to the
4387 * subscribe() call. It always returns zero.
4388 */
4389static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4390{
4391 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4392}
4393
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004394/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4395 * connection with <xprt_ctx> as address of the xprt context.
4396 * Returns 1 if succeeded, 0 if not.
4397 */
4398static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4399{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004400 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004401
4402 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4403
4404 if (*xprt_ctx)
4405 goto out;
4406
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004407 ctx = pool_alloc(pool_head_quic_conn_ctx);
4408 if (!ctx) {
4409 conn->err_code = CO_ER_SYS_MEMLIM;
4410 goto err;
4411 }
4412
4413 ctx->wait_event.tasklet = tasklet_new();
4414 if (!ctx->wait_event.tasklet) {
4415 conn->err_code = CO_ER_SYS_MEMLIM;
4416 goto err;
4417 }
4418
4419 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4420 ctx->wait_event.tasklet->context = ctx;
4421 ctx->wait_event.events = 0;
4422 ctx->conn = conn;
4423 ctx->subs = NULL;
4424 ctx->xprt_ctx = NULL;
4425
4426 ctx->xprt = xprt_get(XPRT_QUIC);
4427 if (objt_server(conn->target)) {
4428 /* Server */
4429 struct server *srv = __objt_server(conn->target);
4430 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004431 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004432 int ssl_err, ipv4;
4433
4434 ssl_err = SSL_ERROR_NONE;
4435 if (RAND_bytes(dcid, sizeof dcid) != 1)
4436 goto err;
4437
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004438 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004439 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004440 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004441 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004442 goto err;
4443
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004444 /* Insert our SCID, the connection ID for the QUIC client. */
4445 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4446
4447 conn->qc = qc;
4448 qc->conn = conn;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004449 if (!qc_new_isecs(qc, dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004450 goto err;
4451
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004452 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4453 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4454 goto err;
4455
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004456 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004457 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004458 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4459 qc->enc_params_len =
4460 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4461 &qc->rx.params, 0);
4462 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004463 goto err;
4464
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004465 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004466 SSL_set_connect_state(ctx->ssl);
4467 ssl_err = SSL_do_handshake(ctx->ssl);
4468 if (ssl_err != 1) {
4469 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4470 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
4471 TRACE_PROTO("SSL handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004472 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004473 }
4474 else {
4475 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004476 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004477 goto err;
4478 }
4479 }
4480 }
4481 else if (objt_listener(conn->target)) {
4482 /* Listener */
4483 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004484 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004485
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004486 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4487 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4488 goto err;
4489
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004490 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004491 SSL_set_accept_state(ctx->ssl);
4492 }
4493
4494 *xprt_ctx = ctx;
4495
4496 /* Leave init state and start handshake */
4497 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004498
4499 out:
4500 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4501
4502 return 0;
4503
4504 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004505 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004506 tasklet_free(ctx->wait_event.tasklet);
4507 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004508 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004509 return -1;
4510}
4511
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004512/* Start the QUIC transport layer */
4513static int qc_xprt_start(struct connection *conn, void *ctx)
4514{
4515 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004516 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004517
4518 qc = conn->qc;
4519 if (!quic_conn_init_timer(qc)) {
4520 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4521 return 0;
4522 }
4523
4524 tasklet_wakeup(qctx->wait_event.tasklet);
4525 return 1;
4526}
4527
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004528/* transport-layer operations for QUIC connections. */
4529static struct xprt_ops ssl_quic = {
4530 .snd_buf = quic_conn_from_buf,
4531 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004532 .subscribe = quic_conn_subscribe,
4533 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004534 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004535 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004536 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4537 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4538 .name = "QUIC",
4539};
4540
4541__attribute__((constructor))
4542static void __quic_conn_init(void)
4543{
4544 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4545 xprt_register(XPRT_QUIC, &ssl_quic);
4546}
4547
4548__attribute__((destructor))
4549static void __quic_conn_deinit(void)
4550{
4551 BIO_meth_free(ha_quic_meth);
4552}
4553
4554/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4555 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4556 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004557 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004558 */
4559static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4560 struct sockaddr_storage *saddr, qpkt_read_func *func)
4561{
4562 unsigned char *pos;
4563 const unsigned char *end;
4564 struct quic_dgram_ctx dgram_ctx = {
4565 .dcid_node = NULL,
4566 .owner = owner,
4567 };
4568
4569 pos = (unsigned char *)buf;
4570 end = pos + len;
4571
4572 do {
4573 int ret;
4574 struct quic_rx_packet *pkt;
4575
Willy Tarreaue4498932021-03-22 21:13:05 +01004576 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004577 if (!pkt)
4578 goto err;
4579
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004580 quic_rx_packet_refinc(pkt);
4581 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4582 if (ret == -1) {
4583 size_t pkt_len;
4584
4585 pkt_len = pkt->len;
4586 free_quic_rx_packet(pkt);
4587 /* If the packet length could not be found, we cannot continue. */
4588 if (!pkt_len)
4589 break;
4590 }
4591 } while (pos < end);
4592
4593 /* Increasing the received bytes counter by the UDP datagram length
4594 * if this datagram could be associated to a connection.
4595 */
4596 if (dgram_ctx.qc)
4597 dgram_ctx.qc->rx.bytes += len;
4598
4599 return pos - (unsigned char *)buf;
4600
4601 err:
4602 return -1;
4603}
4604
4605ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4606 struct sockaddr_storage *saddr)
4607{
4608 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4609}
4610
4611ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4612 struct sockaddr_storage *saddr)
4613{
4614 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4615}
4616
4617/* QUIC I/O handler for connection to local listeners or remove servers
4618 * depending on <listener> boolean value, with <fd> as socket file
4619 * descriptor and <ctx> as context.
4620 */
4621static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4622{
4623 ssize_t ret;
4624 size_t done = 0;
4625 struct buffer *buf = get_trash_chunk();
4626 /* Source address */
4627 struct sockaddr_storage saddr = {0};
4628 socklen_t saddrlen = sizeof saddr;
4629
4630 if (!fd_recv_ready(fd))
4631 return 0;
4632
4633 do {
4634 ret = recvfrom(fd, buf->area, buf->size, 0,
4635 (struct sockaddr *)&saddr, &saddrlen);
4636 if (ret < 0) {
4637 if (errno == EINTR)
4638 continue;
4639 if (errno == EAGAIN)
4640 fd_cant_recv(fd);
4641 goto out;
4642 }
4643 } while (0);
4644
4645 done = buf->data = ret;
4646 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4647
4648 out:
4649 return done;
4650}
4651
4652/* QUIC I/O handler for connections to local listeners with <fd> as socket
4653 * file descriptor.
4654 */
4655void quic_fd_handler(int fd)
4656{
Willy Tarreauf5090652021-04-06 17:23:40 +02004657 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004658 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4659}
4660
4661/* QUIC I/O handler for connections to remote servers with <fd> as socket
4662 * file descriptor.
4663 */
4664void quic_conn_fd_handler(int fd)
4665{
Willy Tarreauf5090652021-04-06 17:23:40 +02004666 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004667 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4668}
4669
4670/*
4671 * Local variables:
4672 * c-indent-level: 8
4673 * c-basic-offset: 8
4674 * End:
4675 */