blob: 16c8c435277f8e42c1ea7385d59cc13d5afa6237 [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>
37#include <haproxy/log.h>
38#include <haproxy/pipe.h>
39#include <haproxy/proxy.h>
40#include <haproxy/quic_cc.h>
41#include <haproxy/quic_frame.h>
42#include <haproxy/quic_loss.h>
43#include <haproxy/quic_tls.h>
44#include <haproxy/ssl_sock.h>
45#include <haproxy/stream_interface.h>
46#include <haproxy/task.h>
47#include <haproxy/trace.h>
48#include <haproxy/xprt_quic.h>
49
50struct quic_transport_params quic_dflt_transport_params = {
51 .max_packet_size = QUIC_DFLT_MAX_PACKET_SIZE,
52 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
53 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
54};
55
56/* trace source and events */
57static void quic_trace(enum trace_level level, uint64_t mask, \
58 const struct trace_source *src,
59 const struct ist where, const struct ist func,
60 const void *a1, const void *a2, const void *a3, const void *a4);
61
62static const struct trace_event quic_trace_events[] = {
63 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
64 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
65 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
66 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
67 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
68 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
69 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010070 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
71 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
72 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
73 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
74 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
75 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
76 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
77 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
78 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
79 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
80 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
81 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
82 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
83 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
84 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
85 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
86 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
87 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
88 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
89 { .mask = QUIC_EV_CONN_CPAPKT, .name = "phdshk_cpakt", .desc = "clear post handhshake app. packet preparation" },
90 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
91 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
92 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
93 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
94 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
95 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
96
97 { .mask = QUIC_EV_CONN_ENEW, .name = "new_conn_err", .desc = "error on new QUIC connection" },
98 { .mask = QUIC_EV_CONN_EISEC, .name = "init_secs_err", .desc = "error on initial secrets derivation" },
99 { .mask = QUIC_EV_CONN_ERSEC, .name = "read_secs_err", .desc = "error on read secrets derivation" },
100 { .mask = QUIC_EV_CONN_EWSEC, .name = "write_secs_err", .desc = "error on write secrets derivation" },
101 { .mask = QUIC_EV_CONN_ELPKT, .name = "lstnr_packet_err", .desc = "error on new listener received packet" },
102 { .mask = QUIC_EV_CONN_ESPKT, .name = "srv_packet_err", .desc = "error on new server received packet" },
103 { .mask = QUIC_EV_CONN_ECHPKT, .name = "chdshk_pkt_err", .desc = "error on clear handhshake packet building" },
104 { .mask = QUIC_EV_CONN_EHPKT, .name = "hdshk_pkt_err", .desc = "error on handhshake packet building" },
105 { .mask = QUIC_EV_CONN_EPAPKT, .name = "phdshk_apkt_err", .desc = "error on post handhshake application packet building" },
106 { /* end */ }
107};
108
109static const struct name_desc quic_trace_lockon_args[4] = {
110 /* arg1 */ { /* already used by the connection */ },
111 /* arg2 */ { .name="quic", .desc="QUIC transport" },
112 /* arg3 */ { },
113 /* arg4 */ { }
114};
115
116static const struct name_desc quic_trace_decoding[] = {
117#define QUIC_VERB_CLEAN 1
118 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
119 { /* end */ }
120};
121
122
123struct trace_source trace_quic = {
124 .name = IST("quic"),
125 .desc = "QUIC xprt",
126 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
127 .default_cb = quic_trace,
128 .known_events = quic_trace_events,
129 .lockon_args = quic_trace_lockon_args,
130 .decoding = quic_trace_decoding,
131 .report_events = ~0, /* report everything by default */
132};
133
134#define TRACE_SOURCE &trace_quic
135INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
136
137static BIO_METHOD *ha_quic_meth;
138
139/* QUIC xprt connection context. */
140struct quic_conn_ctx {
141 struct connection *conn;
142 SSL *ssl;
143 BIO *bio;
144 int state;
145 const struct xprt_ops *xprt;
146 void *xprt_ctx;
147 struct wait_event wait_event;
148 struct wait_event *subs;
149};
150
151DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
152 "quic_conn_ctx_pool", sizeof(struct quic_conn_ctx));
153
154DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
155
156DECLARE_POOL(pool_head_quic_connection_id,
157 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
158
159DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
160
161DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
162
163DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
164
165DECLARE_POOL(pool_head_quic_tx_frm, "quic_tx_frm_pool", sizeof(struct quic_tx_frm));
166
167DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
168
169DECLARE_STATIC_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
170
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100171DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100172
173static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type,
174 struct quic_enc_level *qel);
175
176int qc_prep_phdshk_pkts(struct quic_conn *qc);
177
178/* Add traces to <buf> depending on <frm> TX frame type. */
179static inline void chunk_tx_frm_appendf(struct buffer *buf,
180 const struct quic_tx_frm *frm)
181{
182 switch (frm->type) {
183 case QUIC_FT_CRYPTO:
184 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
185 (unsigned long long)frm->crypto.offset,
186 (unsigned long long)frm->crypto.len);
187 break;
188 default:
189 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
190 }
191}
192
193/* Trace callback for QUIC.
194 * These traces always expect that arg1, if non-null, is of type connection.
195 */
196static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
197 const struct ist where, const struct ist func,
198 const void *a1, const void *a2, const void *a3, const void *a4)
199{
200 const struct connection *conn = a1;
201
202 if (conn) {
203 struct quic_tls_secrets *secs;
204 struct quic_conn *qc;
205
206 qc = conn->qc;
207 chunk_appendf(&trace_buf, " : conn@%p", conn);
208 if ((mask & QUIC_EV_CONN_INIT) && qc) {
209 chunk_appendf(&trace_buf, "\n odcid");
210 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100211 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100212 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100213 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215 }
216
217 if (mask & QUIC_EV_CONN_ADDDATA) {
218 const enum ssl_encryption_level_t *level = a2;
219 const size_t *len = a3;
220
221 if (level) {
222 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
223
224 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
225 }
226 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100227 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100228 }
229 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
230 /* Initial read & write secrets. */
231 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
232 const unsigned char *rx_sec = a2;
233 const unsigned char *tx_sec = a3;
234
235 secs = &qc->els[level].tls_ctx.rx;
236 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
237 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
238 if (rx_sec)
239 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
240 quic_tls_keys_hexdump(&trace_buf, secs);
241 }
242 secs = &qc->els[level].tls_ctx.tx;
243 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
244 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
245 if (tx_sec)
246 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
247 quic_tls_keys_hexdump(&trace_buf, secs);
248 }
249 }
250 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
251 const enum ssl_encryption_level_t *level = a2;
252 const unsigned char *secret = a3;
253 const size_t *secret_len = a4;
254
255 if (level) {
256 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
257
258 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
259 if (secret && secret_len)
260 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
261 secs = &qc->els[lvl].tls_ctx.rx;
262 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
263 quic_tls_keys_hexdump(&trace_buf, secs);
264 }
265 }
266
267 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
268 const enum ssl_encryption_level_t *level = a2;
269 const unsigned char *secret = a3;
270 const size_t *secret_len = a4;
271
272 if (level) {
273 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
274
275 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
276 if (secret && secret_len)
277 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
278 secs = &qc->els[lvl].tls_ctx.tx;
279 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
280 quic_tls_keys_hexdump(&trace_buf, secs);
281 }
282
283 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100284
285 if (mask & QUIC_EV_CONN_HPKT) {
286 const struct quic_tx_packet *pkt = a2;
287 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100288 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100289
290 if (qel) {
291 struct quic_pktns *pktns;
292
293 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100294 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
295 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100296 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100297 (unsigned long long)qc->path->cwnd,
298 (unsigned long long)qc->path->prep_in_flight,
299 (unsigned long long)qc->path->in_flight,
300 (unsigned long long)pktns->tx.in_flight,
301 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100302 }
303 if (pkt) {
304 const struct quic_tx_frm *frm;
305 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
306 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
307 list_for_each_entry(frm, &pkt->frms, list)
308 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100309 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
310 }
311
312 if (room) {
313 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
314 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
315 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100316 }
317 }
318
319 if (mask & QUIC_EV_CONN_HDSHK) {
320 const enum quic_handshake_state *state = a2;
321 const int *err = a3;
322
323 if (state)
324 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
325 if (err)
326 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
327 }
328
329 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_ESPKT|QUIC_EV_CONN_SPKT)) {
330 const struct quic_rx_packet *pkt = a2;
331 const unsigned long *pktlen = a3;
332 const SSL *ssl = a4;
333
334 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100335 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100336 pkt, quic_packet_type_enc_level_char(pkt->type));
337 if (pkt->pnl)
338 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
339 (unsigned long long)pkt->pn);
340 if (pkt->token_len)
341 chunk_appendf(&trace_buf, " toklen=%llu",
342 (unsigned long long)pkt->token_len);
343 if (pkt->aad_len)
344 chunk_appendf(&trace_buf, " aadlen=%llu",
345 (unsigned long long)pkt->aad_len);
346 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
347 pkt->flags, (unsigned long long)pkt->len);
348 }
349 if (pktlen)
350 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
351 if (ssl) {
352 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
353 chunk_appendf(&trace_buf, " el=%c",
354 quic_enc_level_char(ssl_to_quic_enc_level(level)));
355 }
356 }
357
358 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
359 const struct quic_rx_packet *pkt = a2;
360 const struct quic_rx_crypto_frm *cf = a3;
361 const SSL *ssl = a4;
362
363 if (pkt)
364 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
365 quic_packet_type_enc_level_char(pkt->type),
366 (unsigned long long)pkt->pn);
367 if (cf)
368 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
369 (unsigned long long)cf->offset_node.key,
370 (unsigned long long)cf->len);
371 if (ssl) {
372 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
373 chunk_appendf(&trace_buf, " el=%c",
374 quic_enc_level_char(ssl_to_quic_enc_level(level)));
375 }
376 }
377
378 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
379 const struct quic_frame *frm = a2;
380
381 if (frm)
382 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
383 }
384
385 if (mask & QUIC_EV_CONN_PHPKTS) {
386 const struct quic_enc_level *qel = a2;
387
388 if (qel) {
389 struct quic_pktns *pktns;
390
391 pktns = qc->pktns;
392 chunk_appendf(&trace_buf,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100393 " qel=%c ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u pdg=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100394 quic_enc_level_char_from_qel(qel, qc),
395 !!(pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100396 (unsigned long long)qc->path->cwnd,
397 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100398 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100399 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
400 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100401 }
402 }
403
404 if (mask & QUIC_EV_CONN_RMHP) {
405 const struct quic_rx_packet *pkt = a2;
406
407 if (pkt) {
408 const int *ret = a3;
409
410 chunk_appendf(&trace_buf, " pkt@%p", pkt);
411 if (ret && *ret)
412 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
413 pkt->pnl, (unsigned long long)pkt->pn);
414 }
415 }
416
417 if (mask & QUIC_EV_CONN_PRSAFRM) {
418 const struct quic_tx_frm *frm = a2;
419 const unsigned long *val1 = a3;
420 const unsigned long *val2 = a4;
421
422 if (frm)
423 chunk_tx_frm_appendf(&trace_buf, frm);
424 if (val1)
425 chunk_appendf(&trace_buf, " %lu", *val1);
426 if (val2)
427 chunk_appendf(&trace_buf, "..%lu", *val2);
428 }
429
430 if (mask & QUIC_EV_CONN_RTTUPDT) {
431 const unsigned int *rtt_sample = a2;
432 const unsigned int *ack_delay = a3;
433 const struct quic_loss *ql = a4;
434
435 if (rtt_sample)
436 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
437 if (ack_delay)
438 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
439 if (ql)
440 chunk_appendf(&trace_buf,
441 " srtt=%ums rttvar=%ums min_rtt=%ums",
442 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
443 }
444 if (mask & QUIC_EV_CONN_CC) {
445 const struct quic_cc_event *ev = a2;
446 const struct quic_cc *cc = a3;
447
448 if (a2)
449 quic_cc_event_trace(&trace_buf, ev);
450 if (a3)
451 quic_cc_state_trace(&trace_buf, cc);
452 }
453
454 if (mask & QUIC_EV_CONN_PKTLOSS) {
455 const struct quic_pktns *pktns = a2;
456 const struct list *lost_pkts = a3;
457 struct quic_conn *qc = conn->qc;
458
459 if (pktns) {
460 chunk_appendf(&trace_buf, " pktns=%s",
461 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
462 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
463 if (pktns->tx.loss_time)
464 chunk_appendf(&trace_buf, " loss_time=%dms",
465 TICKS_TO_MS(qc->timer - now_ms));
466 }
467 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
468 struct quic_tx_packet *pkt;
469
470 chunk_appendf(&trace_buf, " lost_pkts:");
471 list_for_each_entry(pkt, lost_pkts, list)
472 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
473 }
474 }
475
476 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
477 struct quic_conn *qc = conn->qc;
478 const struct quic_pktns *pktns = a2;
479 const int *duration = a3;
480
481 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100482 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100483 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100484 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
485 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100486 if (mask & QUIC_EV_CONN_STIMER) {
487 if (pktns->tx.loss_time)
488 chunk_appendf(&trace_buf, " loss_time=%dms",
489 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
490 }
491 if (mask & QUIC_EV_CONN_SPTO) {
492 if (pktns->tx.time_of_last_eliciting)
493 chunk_appendf(&trace_buf, " tole=%dms",
494 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
495 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100496 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100497 }
498 }
499
500 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
501 chunk_appendf(&trace_buf,
502 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
503 }
504 }
505
506 if (mask & QUIC_EV_CONN_SPPKTS) {
507 const struct quic_tx_packet *pkt = a2;
508
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100509 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
510 (unsigned long long)qc->path->cwnd,
511 (unsigned long long)qc->path->prep_in_flight,
512 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100513 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100514 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100515 (unsigned long)pkt->pn_node.key,
516 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
517 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100518 (unsigned long long)pkt->in_flight_len,
519 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100520 }
521 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100522
523 if (mask & QUIC_EV_CONN_SSLALERT) {
524 const uint8_t *alert = a2;
525 const enum ssl_encryption_level_t *level = a3;
526
527 if (alert)
528 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
529 if (level)
530 chunk_appendf(&trace_buf, " el=%c",
531 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
532 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100533 }
534 if (mask & QUIC_EV_CONN_LPKT) {
535 const struct quic_rx_packet *pkt = a2;
536
537 if (conn)
538 chunk_appendf(&trace_buf, " xprt_ctx@%p", conn->xprt_ctx);
539 if (pkt)
540 chunk_appendf(&trace_buf, " type=0x%02x %s",
541 pkt->type, qc_pkt_long(pkt) ? "long" : "short");
542 }
543
544}
545
546/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
547static inline int quic_peer_validated_addr(struct quic_conn_ctx *ctx)
548{
549 struct quic_conn *qc;
550
551 qc = ctx->conn->qc;
552 if (objt_server(qc->conn->target))
553 return 1;
554
555 if ((qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
556 (qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
557 (ctx->state & QUIC_HS_ST_COMPLETE))
558 return 1;
559
560 return 0;
561}
562
563/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
564 * both loss detection and PTO and schedule the task assiated to this timer if needed.
565 */
566static inline void qc_set_timer(struct quic_conn_ctx *ctx)
567{
568 struct quic_conn *qc;
569 struct quic_pktns *pktns;
570 unsigned int pto;
571
572 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn);
573 qc = ctx->conn->qc;
574 pktns = quic_loss_pktns(qc);
575 if (tick_isset(pktns->tx.loss_time)) {
576 qc->timer = pktns->tx.loss_time;
577 goto out;
578 }
579
580 /* XXX TODO: anti-amplification: the timer must be
581 * cancelled for a server which reached the anti-amplification limit.
582 */
583
584 if (!qc->path->in_flight_ae_pkts && quic_peer_validated_addr(ctx)) {
585 /* Timer cancellation. */
586 qc->timer = TICK_ETERNITY;
587 goto out;
588 }
589
590 pktns = quic_pto_pktns(qc, ctx->state & QUIC_HS_ST_COMPLETE, &pto);
591 if (tick_isset(pto))
592 qc->timer = pto;
593 out:
594 task_schedule(qc->timer_task, qc->timer);
595 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
596}
597
598#ifndef OPENSSL_IS_BORINGSSL
599int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
600 const uint8_t *read_secret,
601 const uint8_t *write_secret, size_t secret_len)
602{
603 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
604 struct quic_tls_ctx *tls_ctx =
605 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
606 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
607
608 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
609 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
610 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
611 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
612
613 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
614 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
615 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
616 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
617 read_secret, secret_len)) {
618 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
619 return 0;
620 }
621
622 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
623 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
624 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
625 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
626 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
627 write_secret, secret_len)) {
628 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
629 return 0;
630 }
631
632 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
633 if (objt_server(conn->target) && level == ssl_encryption_application) {
634 const unsigned char *buf;
635 size_t buflen;
636
637 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
638 if (!buflen)
639 return 0;
640
641 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
642 return 0;
643 }
644 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
645
646 return 1;
647}
648#else
649/* ->set_read_secret callback to derive the RX secrets at <level> encryption
650 * level.
651 * Returns 1 if succedded, 0 if not.
652 */
653int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
654 const SSL_CIPHER *cipher,
655 const uint8_t *secret, size_t secret_len)
656{
657 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
658 struct quic_tls_ctx *tls_ctx =
659 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
660
661 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
662 tls_ctx->rx.aead = tls_aead(cipher);
663 tls_ctx->rx.md = tls_md(cipher);
664 tls_ctx->rx.hp = tls_hp(cipher);
665
666 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
667 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
668 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
669 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
670 secret, secret_len)) {
671 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
672 goto err;
673 }
674
675 if (objt_server(conn->target) && level == ssl_encryption_application) {
676 const unsigned char *buf;
677 size_t buflen;
678
679 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
680 if (!buflen)
681 goto err;
682
683 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
684 goto err;
685 }
686
687 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
688 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
689
690 return 1;
691
692 err:
693 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ERSEC, conn);
694 return 0;
695}
696
697/* ->set_write_secret callback to derive the TX secrets at <level>
698 * encryption level.
699 * Returns 1 if succedded, 0 if not.
700 */
701int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
702 const SSL_CIPHER *cipher,
703 const uint8_t *secret, size_t secret_len)
704{
705 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
706 struct quic_tls_ctx *tls_ctx =
707 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
708
709 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
710 tls_ctx->tx.aead = tls_aead(cipher);
711 tls_ctx->tx.md = tls_md(cipher);
712 tls_ctx->tx.hp = tls_hp(cipher);
713
714 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
715 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
716 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
717 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
718 secret, secret_len)) {
719 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
720 goto err;
721 }
722
723 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
724 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
725
726 return 1;
727
728 err:
729 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_EWSEC, conn);
730 return 0;
731}
732#endif
733
734/* This function copies the CRYPTO data provided by the TLS stack found at <data>
735 * with <len> as size in CRYPTO buffers dedicated to store the information about
736 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
737 * It fails only if it could not managed to allocate enough CRYPTO buffers to
738 * store all the data.
739 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
740 */
741static int quic_crypto_data_cpy(struct quic_enc_level *qel,
742 const unsigned char *data, size_t len)
743{
744 struct quic_crypto_buf **qcb;
745 /* The remaining byte to store in CRYPTO buffers. */
746 size_t cf_offset, cf_len, *nb_buf;
747 unsigned char *pos;
748
749 nb_buf = &qel->tx.crypto.nb_buf;
750 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
751 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
752 cf_len = len;
753
754 while (len) {
755 size_t to_copy, room;
756
757 pos = (*qcb)->data + (*qcb)->sz;
758 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
759 to_copy = len > room ? room : len;
760 if (to_copy) {
761 memcpy(pos, data, to_copy);
762 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
763 qel->tx.crypto.sz += to_copy;
764 (*qcb)->sz += to_copy;
765 pos += to_copy;
766 len -= to_copy;
767 data += to_copy;
768 }
769 else {
770 struct quic_crypto_buf **tmp;
771
772 tmp = realloc(qel->tx.crypto.bufs,
773 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
774 if (tmp) {
775 qel->tx.crypto.bufs = tmp;
776 qcb = &qel->tx.crypto.bufs[*nb_buf];
777 *qcb = pool_alloc(pool_head_quic_crypto_buf);
778 if (!*qcb)
779 return 0;
780
781 (*qcb)->sz = 0;
782 ++*nb_buf;
783 }
784 else {
785 break;
786 }
787 }
788 }
789
790 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
791 * have been buffered.
792 */
793 if (!len) {
794 struct quic_tx_frm *frm;
795
796 frm = pool_alloc(pool_head_quic_tx_frm);
797 if (!frm)
798 return 0;
799
800 frm->type = QUIC_FT_CRYPTO;
801 frm->crypto.offset = cf_offset;
802 frm->crypto.len = cf_len;
803 LIST_ADDQ(&qel->pktns->tx.frms, &frm->list);
804 }
805
806 return len == 0;
807}
808
809
810/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
811 * wants to provide the QUIC layer with CRYPTO data.
812 * Returns 1 if succeeded, 0 if not.
813 */
814int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
815 const uint8_t *data, size_t len)
816{
817 struct connection *conn;
818 enum quic_tls_enc_level tel;
819 struct quic_enc_level *qel;
820
821 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
822 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
823 tel = ssl_to_quic_enc_level(level);
824 qel = &conn->qc->els[tel];
825
826 if (tel == -1) {
827 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
828 goto err;
829 }
830
831 if (!quic_crypto_data_cpy(qel, data, len)) {
832 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
833 goto err;
834 }
835
836 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
837 conn, &level, &len);
838
839 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
840 return 1;
841
842 err:
843 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
844 return 0;
845}
846
847int ha_quic_flush_flight(SSL *ssl)
848{
849 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
850
851 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
852 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
853
854 return 1;
855}
856
857int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
858{
859 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
860
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100861 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100862 return 1;
863}
864
865/* QUIC TLS methods */
866static SSL_QUIC_METHOD ha_quic_method = {
867#ifdef OPENSSL_IS_BORINGSSL
868 .set_read_secret = ha_set_rsec,
869 .set_write_secret = ha_set_wsec,
870#else
871 .set_encryption_secrets = ha_quic_set_encryption_secrets,
872#endif
873 .add_handshake_data = ha_quic_add_handshake_data,
874 .flush_flight = ha_quic_flush_flight,
875 .send_alert = ha_quic_send_alert,
876};
877
878/* Initialize the TLS context of a listener with <bind_conf> as configuration.
879 * Returns an error count.
880 */
881int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
882{
883 struct proxy *curproxy = bind_conf->frontend;
884 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
885 int cfgerr = 0;
886
887#if 0
888 /* XXX Did not manage to use this. */
889 const char *ciphers =
890 "TLS_AES_128_GCM_SHA256:"
891 "TLS_AES_256_GCM_SHA384:"
892 "TLS_CHACHA20_POLY1305_SHA256:"
893 "TLS_AES_128_CCM_SHA256";
894#endif
895 const char *groups = "P-256:X25519:P-384:P-521";
896 long options =
897 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
898 SSL_OP_SINGLE_ECDH_USE |
899 SSL_OP_CIPHER_SERVER_PREFERENCE;
900 SSL_CTX *ctx;
901
902 ctx = SSL_CTX_new(TLS_server_method());
903 bind_conf->initial_ctx = ctx;
904
905 SSL_CTX_set_options(ctx, options);
906#if 0
907 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
908 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
909 "for bind '%s' at [%s:%d].\n",
910 curproxy->id, ciphers,
911 bind_conf->arg, bind_conf->file, bind_conf->line);
912 cfgerr++;
913 }
914#endif
915
916 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
917 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
918 "for bind '%s' at [%s:%d].\n",
919 curproxy->id, groups,
920 bind_conf->arg, bind_conf->file, bind_conf->line);
921 cfgerr++;
922 }
923
924 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
925 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
926 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
927 SSL_CTX_set_default_verify_paths(ctx);
928
929#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
930#ifdef OPENSSL_IS_BORINGSSL
931 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
932 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
933#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
934 if (bind_conf->ssl_conf.early_data) {
935 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
936 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
937 }
938 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
939 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
940#else
941 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
942#endif
943 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
944#endif
945 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
946
947 return cfgerr;
948}
949
950/* Decode an expected packet number from <truncated_on> its truncated value,
951 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
952 * the number of bits used to encode this packet number (its length in bytes * 8).
953 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
954 */
955static uint64_t decode_packet_number(uint64_t largest_pn,
956 uint32_t truncated_pn, unsigned int pn_nbits)
957{
958 uint64_t expected_pn = largest_pn + 1;
959 uint64_t pn_win = (uint64_t)1 << pn_nbits;
960 uint64_t pn_hwin = pn_win / 2;
961 uint64_t pn_mask = pn_win - 1;
962 uint64_t candidate_pn;
963
964
965 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
966 /* Note that <pn_win> > <pn_hwin>. */
967 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
968 candidate_pn + pn_hwin <= expected_pn)
969 return candidate_pn + pn_win;
970
971 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
972 return candidate_pn - pn_win;
973
974 return candidate_pn;
975}
976
977/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
978 * cryptographic context.
979 * <largest_pn> is the largest received packet number and <pn> the address of
980 * the packet number field for this packet with <byte0> address of its first byte.
981 * <end> points to one byte past the end of this packet.
982 * Returns 1 if succeeded, 0 if not.
983 */
984static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
985 int64_t largest_pn, unsigned char *pn,
986 unsigned char *byte0, const unsigned char *end,
987 struct quic_conn_ctx *ctx)
988{
989 int ret, outlen, i, pnlen;
990 uint64_t packet_number;
991 uint32_t truncated_pn = 0;
992 unsigned char mask[5] = {0};
993 unsigned char *sample;
994 EVP_CIPHER_CTX *cctx;
995 unsigned char *hp_key;
996
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100997 /* Check there is enough data in this packet. */
998 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
999 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1000 return 0;
1001 }
1002
1003 cctx = EVP_CIPHER_CTX_new();
1004 if (!cctx) {
1005 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1006 return 0;
1007 }
1008
1009 ret = 0;
1010 sample = pn + QUIC_PACKET_PN_MAXLEN;
1011
1012 hp_key = tls_ctx->rx.hp_key;
1013 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1014 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1015 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1016 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1017 goto out;
1018 }
1019
1020 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1021 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1022 for (i = 0; i < pnlen; i++) {
1023 pn[i] ^= mask[i + 1];
1024 truncated_pn = (truncated_pn << 8) | pn[i];
1025 }
1026
1027 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1028 /* Store remaining information for this unprotected header */
1029 pkt->pn = packet_number;
1030 pkt->pnl = pnlen;
1031
1032 ret = 1;
1033
1034 out:
1035 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001036
1037 return ret;
1038}
1039
1040/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1041 * address, with <payload_len> as payload length, <aad> as address of
1042 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1043 * context.
1044 * Returns 1 if succeeded, 0 if not.
1045 */
1046static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1047 unsigned char *aad, size_t aad_len, uint64_t pn,
1048 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1049{
1050 unsigned char iv[12];
1051 unsigned char *tx_iv = tls_ctx->tx.iv;
1052 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
1053
1054 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1055 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
1056 return 0;
1057 }
1058
1059 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1060 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1061 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
1062 return 0;
1063 }
1064
1065 return 1;
1066}
1067
1068/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1069 * Returns 1 if succeeded, 0 if not.
1070 */
1071static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1072{
1073 int ret;
1074 unsigned char iv[12];
1075 unsigned char *rx_iv = tls_ctx->rx.iv;
1076 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1077
1078 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1079 return 0;
1080
1081 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1082 pkt->data, pkt->aad_len,
1083 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1084 if (!ret)
1085 return 0;
1086
1087 /* Update the packet length (required to parse the frames). */
1088 pkt->len = pkt->aad_len + ret;
1089
1090 return 1;
1091}
1092
1093/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
1094static inline void qc_treat_acked_tx_frm(struct quic_tx_frm *frm,
1095 struct quic_conn_ctx *ctx)
1096{
1097 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001098 LIST_DEL(&frm->list);
1099 pool_free(pool_head_quic_tx_frm, frm);
1100}
1101
1102/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1103 * deallocating them, and their TX frames.
1104 * Returns the last node reached to be used for the next range.
1105 * May be NULL if <largest> node could not be found.
1106 */
1107static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1108 struct list *newly_acked_pkts,
1109 struct eb64_node *largest_node,
1110 uint64_t largest, uint64_t smallest,
1111 struct quic_conn_ctx *ctx)
1112{
1113 struct eb64_node *node;
1114 struct quic_tx_packet *pkt;
1115
1116 if (largest_node)
1117 node = largest_node;
1118 else {
1119 node = eb64_lookup(pkts, largest);
1120 while (!node && largest > smallest) {
1121 node = eb64_lookup(pkts, --largest);
1122 }
1123 }
1124
1125 while (node && node->key >= smallest) {
1126 struct quic_tx_frm *frm, *frmbak;
1127
1128 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1129 *pkt_flags |= pkt->flags;
1130 LIST_ADD(newly_acked_pkts, &pkt->list);
1131 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1132 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1133 qc_treat_acked_tx_frm(frm, ctx);
1134 node = eb64_prev(node);
1135 eb64_delete(&pkt->pn_node);
1136 }
1137
1138 return node;
1139}
1140
1141/* Treat <frm> frame whose packet it is attached to has just been detected as non
1142 * acknowledged.
1143 */
1144static inline void qc_treat_nacked_tx_frm(struct quic_tx_frm *frm,
1145 struct quic_pktns *pktns,
1146 struct quic_conn_ctx *ctx)
1147{
1148 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001149 LIST_DEL(&frm->list);
1150 LIST_ADD(&pktns->tx.frms, &frm->list);
1151}
1152
1153
1154/* Free the TX packets of <pkts> list */
1155static inline void free_quic_tx_pkts(struct list *pkts)
1156{
1157 struct quic_tx_packet *pkt, *tmp;
1158
1159 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1160 LIST_DEL(&pkt->list);
1161 eb64_delete(&pkt->pn_node);
1162 pool_free(pool_head_quic_tx_packet, pkt);
1163 }
1164}
1165
1166/* Send a packet loss event nofification to the congestion controller
1167 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1168 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1169 * at <now_us> current time.
1170 * Always succeeds.
1171 */
1172static inline void qc_cc_loss_event(struct quic_conn *qc,
1173 unsigned int lost_bytes,
1174 unsigned int newest_time_sent,
1175 unsigned int period,
1176 unsigned int now_us)
1177{
1178 struct quic_cc_event ev = {
1179 .type = QUIC_CC_EVT_LOSS,
1180 .loss.now_ms = now_ms,
1181 .loss.max_ack_delay = qc->max_ack_delay,
1182 .loss.lost_bytes = lost_bytes,
1183 .loss.newest_time_sent = newest_time_sent,
1184 .loss.period = period,
1185 };
1186
1187 quic_cc_event(&qc->path->cc, &ev);
1188}
1189
1190/* Send a packet ack event nofication for each newly acked packet of
1191 * <newly_acked_pkts> list and free them.
1192 * Always succeeds.
1193 */
1194static inline void qc_treat_newly_acked_pkts(struct quic_conn_ctx *ctx,
1195 struct list *newly_acked_pkts)
1196{
1197 struct quic_conn *qc = ctx->conn->qc;
1198 struct quic_tx_packet *pkt, *tmp;
1199 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1200
1201 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1202 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001203 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001204 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
1205 qc->path->in_flight_ae_pkts--;
1206 ev.ack.acked = pkt->in_flight_len;
1207 ev.ack.time_sent = pkt->time_sent;
1208 quic_cc_event(&qc->path->cc, &ev);
1209 LIST_DEL(&pkt->list);
1210 eb64_delete(&pkt->pn_node);
1211 pool_free(pool_head_quic_tx_packet, pkt);
1212 }
1213
1214}
1215
1216/* Handle <pkts> list of lost packets detected at <now_us> handling
1217 * their TX frames.
1218 * Send a packet loss event to the congestion controller if
1219 * in flight packet have been lost.
1220 * Also frees the packet in <pkts> list.
1221 * Never fails.
1222 */
1223static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
1224 struct quic_conn_ctx *ctx,
1225 struct list *pkts,
1226 uint64_t now_us)
1227{
1228 struct quic_conn *qc = ctx->conn->qc;
1229 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
1230 struct quic_tx_frm *frm, *frmbak;
1231 uint64_t lost_bytes;
1232
1233 lost_bytes = 0;
1234 oldest_lost = newest_lost = NULL;
1235 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1236 lost_bytes += pkt->in_flight_len;
1237 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001238 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001239 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
1240 qc->path->in_flight_ae_pkts--;
1241 /* Treat the frames of this lost packet. */
1242 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1243 qc_treat_nacked_tx_frm(frm, pktns, ctx);
1244 LIST_DEL(&pkt->list);
1245 if (!oldest_lost) {
1246 oldest_lost = newest_lost = pkt;
1247 }
1248 else {
1249 if (newest_lost != oldest_lost)
1250 pool_free(pool_head_quic_tx_packet, newest_lost);
1251 newest_lost = pkt;
1252 }
1253 }
1254
1255 if (lost_bytes) {
1256 /* Sent a packet loss event to the congestion controller. */
1257 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1258 newest_lost->time_sent - oldest_lost->time_sent, now_us);
1259 pool_free(pool_head_quic_tx_packet, oldest_lost);
1260 if (newest_lost != oldest_lost)
1261 pool_free(pool_head_quic_tx_packet, newest_lost);
1262 }
1263}
1264
1265/* Look for packet loss from sent packets for <qel> encryption level of a
1266 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1267 * their tree if deemed as lost or set the <loss_time> value the packet number
1268 * space if any not deemed lost.
1269 * Should be called after having received an ACK frame with newly acknowledged
1270 * packets or when the the loss detection timer has expired.
1271 * Always succeeds.
1272 */
1273static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1274 struct quic_conn *qc,
1275 struct list *lost_pkts)
1276{
1277 struct eb_root *pkts;
1278 struct eb64_node *node;
1279 struct quic_loss *ql;
1280 unsigned int loss_delay;
1281
1282 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1283 pkts = &pktns->tx.pkts;
1284 pktns->tx.loss_time = TICK_ETERNITY;
1285 if (eb_is_empty(pkts))
1286 goto out;
1287
1288 ql = &qc->path->loss;
1289 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1290 loss_delay += loss_delay >> 3;
1291 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1292
1293 node = eb64_first(pkts);
1294 while (node) {
1295 struct quic_tx_packet *pkt;
1296 int64_t largest_acked_pn;
1297 unsigned int loss_time_limit, time_sent;
1298
1299 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1300 largest_acked_pn = pktns->tx.largest_acked_pn;
1301 node = eb64_next(node);
1302 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1303 break;
1304
1305 time_sent = pkt->time_sent;
1306 loss_time_limit = tick_add(time_sent, loss_delay);
1307 if (tick_is_le(time_sent, now_ms) ||
1308 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1309 eb64_delete(&pkt->pn_node);
1310 LIST_ADDQ(lost_pkts, &pkt->list);
1311 }
1312 else {
1313 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1314 }
1315 }
1316
1317 out:
1318 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1319}
1320
1321/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1322 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
1323 * if the largest acked packet was newly acked and if there was at leas one newly
1324 * acked ack-eliciting packet.
1325 * Return 1, if succeeded, 0 if not.
1326 */
1327static inline int qc_parse_ack_frm(struct quic_frame *frm, struct quic_conn_ctx *ctx,
1328 struct quic_enc_level *qel,
1329 unsigned int *rtt_sample,
1330 const unsigned char **pos, const unsigned char *end)
1331{
1332 struct quic_ack *ack = &frm->ack;
1333 uint64_t smallest, largest;
1334 struct eb_root *pkts;
1335 struct eb64_node *largest_node;
1336 unsigned int time_sent, pkt_flags;
1337 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1338 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1339
1340 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1341 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1342 ctx->conn,, &ack->largest_ack);
1343 goto err;
1344 }
1345
1346 if (ack->first_ack_range > ack->largest_ack) {
1347 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1348 ctx->conn,, &ack->first_ack_range);
1349 goto err;
1350 }
1351
1352 largest = ack->largest_ack;
1353 smallest = largest - ack->first_ack_range;
1354 pkts = &qel->pktns->tx.pkts;
1355 pkt_flags = 0;
1356 largest_node = NULL;
1357 time_sent = 0;
1358
1359 if ((int64_t)ack->largest_ack > qel->pktns->tx.largest_acked_pn) {
1360 largest_node = eb64_lookup(pkts, largest);
1361 if (!largest_node) {
1362 TRACE_DEVEL("Largest acked packet not found",
1363 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1364 goto err;
1365 }
1366
1367 time_sent = eb64_entry(&largest_node->node,
1368 struct quic_tx_packet, pn_node)->time_sent;
1369 }
1370
1371 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1372 ctx->conn,, &largest, &smallest);
1373 do {
1374 uint64_t gap, ack_range;
1375
1376 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1377 largest_node, largest, smallest, ctx);
1378 if (!ack->ack_range_num--)
1379 break;
1380
1381 if (!quic_dec_int(&gap, pos, end))
1382 goto err;
1383
1384 if (smallest < gap + 2) {
1385 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1386 ctx->conn,, &gap, &smallest);
1387 goto err;
1388 }
1389
1390 largest = smallest - gap - 2;
1391 if (!quic_dec_int(&ack_range, pos, end))
1392 goto err;
1393
1394 if (largest < ack_range) {
1395 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1396 ctx->conn,, &largest, &ack_range);
1397 goto err;
1398 }
1399
1400 /* Do not use this node anymore. */
1401 largest_node = NULL;
1402 /* Next range */
1403 smallest = largest - ack_range;
1404
1405 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1406 ctx->conn,, &largest, &smallest);
1407 } while (1);
1408
1409 /* Flag this packet number space as having received an ACK. */
1410 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_RECEIVED;
1411
1412 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1413 *rtt_sample = tick_remain(time_sent, now_ms);
1414 qel->pktns->tx.largest_acked_pn = ack->largest_ack;
1415 }
1416
1417 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1418 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1419 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1420 if (!LIST_ISEMPTY(&lost_pkts))
1421 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1422 }
1423 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1424 if (quic_peer_validated_addr(ctx))
1425 ctx->conn->qc->path->loss.pto_count = 0;
1426 qc_set_timer(ctx);
1427 }
1428
1429
1430 return 1;
1431
1432 err:
1433 free_quic_tx_pkts(&newly_acked_pkts);
1434 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1435 return 0;
1436}
1437
1438/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1439 * from <qel> encryption level with <ctx> as QUIC connection context.
1440 * Remaining parameter are there for debuging purposes.
1441 * Return 1 if succeeded, 0 if not.
1442 */
1443static inline int qc_provide_cdata(struct quic_enc_level *el,
1444 struct quic_conn_ctx *ctx,
1445 const unsigned char *data, size_t len,
1446 struct quic_rx_packet *pkt,
1447 struct quic_rx_crypto_frm *cf)
1448{
1449 int ssl_err;
1450
1451 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1452 ssl_err = SSL_ERROR_NONE;
1453 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1454 TRACE_PROTO("SSL_provide_quic_data() error",
1455 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1456 goto err;
1457 }
1458
1459 el->rx.crypto.offset += len;
1460 TRACE_PROTO("in order CRYPTO data",
1461 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1462
1463 if (ctx->state < QUIC_HS_ST_COMPLETE) {
1464 ssl_err = SSL_do_handshake(ctx->ssl);
1465 if (ssl_err != 1) {
1466 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1467 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1468 TRACE_PROTO("SSL handshake",
1469 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
1470 goto out;
1471 }
1472
1473 TRACE_DEVEL("SSL handshake error",
1474 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
1475 goto err;
1476 }
1477
1478 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state);
1479 if (objt_listener(ctx->conn->target))
1480 ctx->state = QUIC_HS_ST_CONFIRMED;
1481 else
1482 ctx->state = QUIC_HS_ST_COMPLETE;
1483 } else {
1484 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1485 if (ssl_err != 1) {
1486 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1487 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1488 TRACE_DEVEL("SSL post handshake",
1489 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
1490 goto out;
1491 }
1492
1493 TRACE_DEVEL("SSL post handshake error",
1494 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
1495 goto err;
1496 }
1497
1498 TRACE_PROTO("SSL post handshake succeeded",
1499 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state);
1500 }
1501
1502 out:
1503 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1504 return 1;
1505
1506 err:
1507 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1508 return 0;
1509}
1510
1511/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1512 * as I/O handler context and <qel> as encryption level.
1513 * Returns 1 if succeeded, 0 if failed.
1514 */
1515static int qc_parse_pkt_frms(struct quic_rx_packet *pkt, struct quic_conn_ctx *ctx,
1516 struct quic_enc_level *qel)
1517{
1518 struct quic_frame frm;
1519 const unsigned char *pos, *end;
1520 struct quic_conn *conn = ctx->conn->qc;
1521
1522 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1523 /* Skip the AAD */
1524 pos = pkt->data + pkt->aad_len;
1525 end = pkt->data + pkt->len;
1526
1527 while (pos < end) {
1528 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1529 goto err;
1530
1531 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001532 case QUIC_FT_PADDING:
1533 if (pos != end) {
1534 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1535 goto err;
1536 }
1537 break;
1538 case QUIC_FT_PING:
1539 break;
1540 case QUIC_FT_ACK:
1541 {
1542 unsigned int rtt_sample;
1543
1544 rtt_sample = 0;
1545 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1546 goto err;
1547
1548 if (rtt_sample) {
1549 unsigned int ack_delay;
1550
1551 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1552 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1553 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1554 }
1555 tasklet_wakeup(ctx->wait_event.tasklet);
1556 break;
1557 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001558 case QUIC_FT_CRYPTO:
1559 if (frm.crypto.offset != qel->rx.crypto.offset) {
1560 struct quic_rx_crypto_frm *cf;
1561
1562 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1563 if (!cf) {
1564 TRACE_DEVEL("CRYPTO frame allocation failed",
1565 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1566 goto err;
1567 }
1568
1569 cf->offset_node.key = frm.crypto.offset;
1570 cf->len = frm.crypto.len;
1571 cf->data = frm.crypto.data;
1572 cf->pkt = pkt;
1573 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1574 quic_rx_packet_refinc(pkt);
1575 }
1576 else {
1577 /* XXX TO DO: <cf> is used only for the traces. */
1578 struct quic_rx_crypto_frm cf = { };
1579
1580 cf.offset_node.key = frm.crypto.offset;
1581 cf.len = frm.crypto.len;
1582 if (!qc_provide_cdata(qel, ctx,
1583 frm.crypto.data, frm.crypto.len,
1584 pkt, &cf))
1585 goto err;
1586 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001587 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001588 case QUIC_FT_STREAM_8:
1589 case QUIC_FT_STREAM_9:
1590 case QUIC_FT_STREAM_A:
1591 case QUIC_FT_STREAM_B:
1592 case QUIC_FT_STREAM_C:
1593 case QUIC_FT_STREAM_D:
1594 case QUIC_FT_STREAM_E:
1595 case QUIC_FT_STREAM_F:
1596 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001597 break;
1598 case QUIC_FT_CONNECTION_CLOSE:
1599 case QUIC_FT_CONNECTION_CLOSE_APP:
1600 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001601 case QUIC_FT_HANDSHAKE_DONE:
1602 if (objt_listener(ctx->conn->target))
1603 goto err;
1604
1605 ctx->state = QUIC_HS_ST_CONFIRMED;
1606 break;
1607 default:
1608 goto err;
1609 }
1610 }
1611
1612 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
1613 * has successfully parse a Handshake packet. The Initial encryption must also
1614 * be discarded.
1615 */
1616 if (ctx->state == QUIC_HS_ST_SERVER_INITIAL &&
1617 pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) {
1618 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
1619 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
1620 qc_set_timer(ctx);
1621 ctx->state = QUIC_HS_ST_SERVER_HANDSHAKE;
1622 }
1623
1624 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1625 return 1;
1626
1627 err:
1628 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
1629 return 0;
1630}
1631
1632/* Prepare as much as possible handshake packets for the QUIC connection
1633 * with <ctx> as I/O handler context.
1634 * Returns 1 if succeeded, or 0 if something wrong happened.
1635 */
1636static int qc_prep_hdshk_pkts(struct quic_conn_ctx *ctx)
1637{
1638 struct quic_conn *qc;
1639 enum quic_tls_enc_level tel, next_tel;
1640 struct quic_enc_level *qel;
1641 struct q_buf *wbuf;
1642 /* A boolean to flag <wbuf> as reusable, even if not empty. */
1643 int reuse_wbuf;
1644
1645 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
1646 qc = ctx->conn->qc;
1647 if (!quic_get_tls_enc_levels(&tel, &next_tel, ctx->state)) {
1648 TRACE_DEVEL("unknown enc. levels",
1649 QUIC_EV_CONN_PHPKTS, ctx->conn);
1650 goto err;
1651 }
1652
1653 reuse_wbuf = 0;
1654 wbuf = q_wbuf(qc);
1655 qel = &qc->els[tel];
1656 /* When entering this function, the writter buffer must be empty.
1657 * Most of the time it points to the reader buffer.
1658 */
1659 while ((q_buf_empty(wbuf) || reuse_wbuf)) {
1660 ssize_t ret;
1661 enum quic_pkt_type pkt_type;
1662
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01001663 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001664 /* Do not build any more packet f the TX secrets are not available or
1665 * f there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001666 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001667 * and if there is no more CRYPTO data available or in flight
1668 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001669 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001670 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
1671 (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001672 !qc->tx.nb_pto_dgrams &&
1673 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001674 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001675 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
1676 /* Consume the buffer if we were supposed to reuse it. */
1677 if (reuse_wbuf)
1678 wbuf = q_next_wbuf(qc);
1679 break;
1680 }
1681
1682 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001683 ret = qc_build_hdshk_pkt(wbuf, qc, pkt_type, qel);
1684 switch (ret) {
1685 case -2:
1686 goto err;
1687 case -1:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001688 if (!reuse_wbuf)
1689 goto out;
1690
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001691 /* Not enough room in <wbuf>. */
1692 wbuf = q_next_wbuf(qc);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001693 reuse_wbuf = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001694 continue;
1695 case 0:
1696 goto out;
1697 default:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001698 reuse_wbuf = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001699 /* Discard the Initial encryption keys as soon as
1700 * a handshake packet could be built.
1701 */
1702 if (ctx->state == QUIC_HS_ST_CLIENT_INITIAL &&
1703 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
1704 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
1705 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
1706 qc_set_timer(ctx);
1707 ctx->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
1708 }
1709 /* Special case for Initial packets: when they have all
1710 * been sent, select the next level.
1711 */
1712 if ((LIST_ISEMPTY(&qel->pktns->tx.frms) || qc->els[next_tel].pktns->tx.in_flight) &&
1713 tel == QUIC_TLS_ENC_LEVEL_INITIAL) {
1714 tel = next_tel;
1715 qel = &qc->els[tel];
1716 if (LIST_ISEMPTY(&qel->pktns->tx.frms)) {
1717 /* If there is no more data for the next level, let's
1718 * consume a buffer. This is the case for a client
1719 * which sends only one Initial packet, then wait
1720 * for additional CRYPTO data from the server to enter the
1721 * next level.
1722 */
1723 wbuf = q_next_wbuf(qc);
1724 }
1725 else {
1726 /* Let's try to reuse this buffer. */
1727 reuse_wbuf = 1;
1728 }
1729 }
1730 else {
1731 wbuf = q_next_wbuf(qc);
1732 }
1733 }
1734 }
1735
1736 out:
1737 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
1738 return 1;
1739
1740 err:
1741 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
1742 return 0;
1743}
1744
1745/* Send the QUIC packets which have been prepared for QUIC connections
1746 * with <ctx> as I/O handler context.
1747 */
1748int qc_send_ppkts(struct quic_conn_ctx *ctx)
1749{
1750 struct quic_conn *qc;
1751 struct buffer tmpbuf = { };
1752 struct q_buf *rbuf;
1753
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001754 qc = ctx->conn->qc;
1755 for (rbuf = q_rbuf(qc); !q_buf_empty(rbuf) ; rbuf = q_next_rbuf(qc)) {
1756 struct quic_tx_packet *p, *q;
1757 unsigned int time_sent;
1758
1759 tmpbuf.area = (char *)rbuf->area;
1760 tmpbuf.size = tmpbuf.data = rbuf->data;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001761 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001762 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
1763 &tmpbuf, tmpbuf.data, 0) <= 0)
1764 break;
1765
1766 qc->tx.bytes += tmpbuf.data;
1767 time_sent = now_ms;
1768 /* Reset this buffer to make it available for the next packet to prepare. */
1769 q_buf_reset(rbuf);
1770 /* Remove from <rbuf> the packets which have just been sent. */
1771 list_for_each_entry_safe(p, q, &rbuf->pkts, list) {
1772 p->time_sent = time_sent;
1773 if (p->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
1774 p->pktns->tx.time_of_last_eliciting = time_sent;
1775 qc->path->in_flight_ae_pkts++;
1776 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001777 qc->path->in_flight += p->in_flight_len;
1778 p->pktns->tx.in_flight += p->in_flight_len;
1779 if (p->in_flight_len)
1780 qc_set_timer(ctx);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001781 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, p);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001782 LIST_DEL(&p->list);
1783 }
1784 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001785
1786 return 1;
1787}
1788
1789/* Build all the frames which must be sent just after the handshake have succeeded.
1790 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
1791 * a HANDSHAKE_DONE frame.
1792 * Return 1 if succeeded, 0 if not.
1793 */
1794static int quic_build_post_handshake_frames(struct quic_conn *conn)
1795{
1796 int i;
1797 struct quic_frame *frm;
1798
1799 /* Only servers must send a HANDSHAKE_DONE frame. */
1800 if (!objt_server(conn->conn->target)) {
1801 frm = pool_alloc(pool_head_quic_frame);
1802 frm->type = QUIC_FT_HANDSHAKE_DONE;
1803 LIST_ADDQ(&conn->tx.frms_to_send, &frm->list);
1804 }
1805
1806 for (i = 1; i < conn->rx_tps.active_connection_id_limit; i++) {
1807 struct quic_connection_id *cid;
1808
1809 frm = pool_alloc(pool_head_quic_frame);
1810 memset(frm, 0, sizeof *frm);
1811 cid = new_quic_cid(&conn->cids, i);
1812 if (!frm || !cid)
1813 goto err;
1814
1815 quic_connection_id_to_frm_cpy(frm, cid);
1816 LIST_ADDQ(&conn->tx.frms_to_send, &frm->list);
1817 }
1818
1819 return 1;
1820
1821 err:
1822 free_quic_conn_cids(conn);
1823 return 0;
1824}
1825
1826/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001827void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001828{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001829 struct eb64_node *n;
1830 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001831
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001832 n = eb64_first(&arngs->root);
1833 while (n) {
1834 struct eb64_node *next;
1835
1836 ar = eb64_entry(&n->node, struct quic_arng_node, first);
1837 next = eb64_next(n);
1838 eb64_delete(n);
1839 free(ar);
1840 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001841 }
1842}
1843
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001844/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
1845 * descending order.
1846 */
1847static inline size_t sack_gap(struct quic_arng_node *p,
1848 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001849{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001850 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001851}
1852
1853
1854/* Remove the last elements of <ack_ranges> list of ack range updating its
1855 * encoded size until it goes below <limit>.
1856 * Returns 1 if succeded, 0 if not (no more element to remove).
1857 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001858static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001859{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001860 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001861
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001862 last = eb64_last(&arngs->root);
1863 while (last && arngs->enc_sz > limit) {
1864 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001865
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001866 prev = eb64_prev(last);
1867 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001868 return 0;
1869
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001870 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
1871 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
1872 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
1873 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
1874 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
1875 --arngs->sz;
1876 eb64_delete(last);
1877 pool_free(pool_head_quic_arng, last);
1878 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001879 }
1880
1881 return 1;
1882}
1883
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001884/* Set the encoded size of <arngs> QUIC ack ranges. */
1885static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
1886{
1887 struct eb64_node *node, *next;
1888 struct quic_arng_node *ar, *ar_next;
1889
1890 node = eb64_last(&arngs->root);
1891 if (!node)
1892 return;
1893
1894 ar = eb64_entry(&node->node, struct quic_arng_node, first);
1895 arngs->enc_sz = quic_int_getsize(ar->last) +
1896 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
1897
1898 while ((next = eb64_prev(node))) {
1899 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
1900 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
1901 quic_int_getsize(ar_next->last - ar_next->first.key);
1902 node = next;
1903 ar = eb64_entry(&node->node, struct quic_arng_node, first);
1904 }
1905}
1906
1907/* Insert in <root> ebtree <node> node with <ar> as range value.
1908 * Returns the ebtree node which has been inserted.
1909 */
1910static inline
1911struct eb64_node *quic_insert_new_range(struct eb_root *root,
1912 struct quic_arng_node *node,
1913 struct quic_arng *ar)
1914{
1915 node->first.key = ar->first;
1916 node->last = ar->last;
1917 return eb64_insert(root, &node->first);
1918}
1919
1920/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001921 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001922 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001923 *
1924 * Descending order
1925 * ------------->
1926 * range1 range2
1927 * ..........|--------|..............|--------|
1928 * ^ ^ ^ ^
1929 * | | | |
1930 * last1 first1 last2 first2
1931 * ..........+--------+--------------+--------+......
1932 * diff1 gap12 diff2
1933 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001934 * To encode the previous list of ranges we must encode integers as follows in
1935 * descending order:
1936 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001937 * with diff1 = last1 - first1
1938 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001939 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001940 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001941 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001942int quic_update_ack_ranges_list(struct quic_arngs *arngs,
1943 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001945 struct eb64_node *le;
1946 struct quic_arng_node *new_node;
1947 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001948
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001949 new = NULL;
1950 if (eb_is_empty(&arngs->root)) {
1951 /* First range insertion. */
1952 new_node = pool_alloc(pool_head_quic_arng);
1953 if (!new_node)
1954 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001955
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001956 quic_insert_new_range(&arngs->root, new_node, ar);
1957 /* Increment the size of these ranges. */
1958 arngs->sz++;
1959 goto out;
1960 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001961
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001962 le = eb64_lookup_le(&arngs->root, ar->first);
1963 if (!le) {
1964 /* New insertion */
1965 new_node = pool_alloc(pool_head_quic_arng);
1966 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001967 return 0;
1968
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001969 new = quic_insert_new_range(&arngs->root, new_node, ar);
1970 /* Increment the size of these ranges. */
1971 arngs->sz++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001972 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001973 else {
1974 struct quic_arng_node *le_ar =
1975 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001976
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001977 /* Already existing range */
1978 if (le_ar->first.key <= ar->first && le_ar->last >= ar->last)
1979 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001980
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001981 if (le_ar->last + 1 >= ar->first) {
1982 le_ar->last = ar->last;
1983 new = le;
1984 new_node = le_ar;
1985 }
1986 else {
1987 /* New insertion */
1988 new_node = pool_alloc(pool_head_quic_arng);
1989 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001990 return 0;
1991
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001992 new = quic_insert_new_range(&arngs->root, new_node, ar);
1993 /* Increment the size of these ranges. */
1994 arngs->sz++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001995 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001996 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001997
Frédéric Lécaille8090b512020-11-30 16:19:22 +01001998 /* Verify that the new inserted node does not overlap the nodes
1999 * which follow it.
2000 */
2001 if (new) {
2002 uint64_t new_node_last;
2003 struct eb64_node *next;
2004 struct quic_arng_node *next_node;
2005
2006 new_node_last = new_node->last;
2007 while ((next = eb64_next(new))) {
2008 next_node =
2009 eb64_entry(&next->node, struct quic_arng_node, first);
2010 if (new_node_last + 1 < next_node->first.key)
2011 break;
2012
2013 if (next_node->last > new_node->last)
2014 new_node->last = next_node->last;
2015 eb64_delete(next);
2016 free(next_node);
2017 /* Decrement the size of these ranges. */
2018 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002019 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002020 }
2021
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002022 quic_arngs_set_enc_sz(arngs);
2023
2024 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002025 return 1;
2026}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002027/* Remove the header protection of packets at <el> encryption level.
2028 * Always succeeds.
2029 */
2030static inline void qc_rm_hp_pkts(struct quic_enc_level *el, struct quic_conn_ctx *ctx)
2031{
2032 struct quic_tls_ctx *tls_ctx;
2033 struct quic_rx_packet *pqpkt, *qqpkt;
2034 struct quic_enc_level *app_qel;
2035
2036 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2037 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2038 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
2039 if (el == app_qel && objt_listener(ctx->conn->target) && ctx->state < QUIC_HS_ST_COMPLETE) {
2040 TRACE_PROTO("hp not removed (handshake not completed)",
2041 QUIC_EV_CONN_ELRMHP, ctx->conn);
2042 goto out;
2043 }
2044 tls_ctx = &el->tls_ctx;
2045 list_for_each_entry_safe(pqpkt, qqpkt, &el->rx.pqpkts, list) {
2046 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2047 pqpkt->data + pqpkt->pn_offset,
2048 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2049 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2050 /* XXX TO DO XXX */
2051 }
2052 else {
2053 /* The AAD includes the packet number field */
2054 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2055 /* Store the packet into the tree of packets to decrypt. */
2056 pqpkt->pn_node.key = pqpkt->pn;
2057 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2058 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2059 }
2060 quic_rx_packet_list_del(pqpkt);
2061 }
2062
2063 out:
2064 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2065}
2066
2067/* Process all the CRYPTO frame at <el> encryption level.
2068 * Return 1 if succeeded, 0 if not.
2069 */
2070static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
2071 struct quic_conn_ctx *ctx)
2072{
2073 struct eb64_node *node;
2074
2075 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
2076 node = eb64_first(&el->rx.crypto.frms);
2077 while (node) {
2078 struct quic_rx_crypto_frm *cf;
2079
2080 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2081 if (cf->offset_node.key != el->rx.crypto.offset)
2082 break;
2083
2084 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2085 goto err;
2086
2087 node = eb64_next(node);
2088 quic_rx_packet_refdec(cf->pkt);
2089 eb64_delete(&cf->offset_node);
2090 pool_free(pool_head_quic_rx_crypto_frm, cf);
2091 }
2092
2093 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2094 return 1;
2095
2096 err:
2097 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2098 return 0;
2099}
2100
2101/* Process all the packets at <el> encryption level.
2102 * Return 1 if succeeded, 0 if not.
2103 */
2104int qc_treat_rx_pkts(struct quic_enc_level *el, struct quic_conn_ctx *ctx)
2105{
2106 struct quic_tls_ctx *tls_ctx;
2107 struct eb64_node *node;
2108
2109 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2110 tls_ctx = &el->tls_ctx;
2111 node = eb64_first(&el->rx.pkts);
2112 while (node) {
2113 struct quic_rx_packet *pkt;
2114
2115 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
2116 if (!qc_pkt_decrypt(pkt, tls_ctx)) {
2117 /* Drop the packet */
2118 TRACE_PROTO("packet decryption failed -> dropped",
2119 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2120 }
2121 else {
2122 int drop;
2123
2124 drop = 0;
2125 if (!qc_parse_pkt_frms(pkt, ctx, el))
2126 drop = 1;
2127
2128 if (drop) {
2129 /* Drop the packet */
2130 TRACE_PROTO("packet parsing failed -> dropped",
2131 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2132 }
2133 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002134 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2135
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002136 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
2137 el->pktns->rx.nb_ack_eliciting++;
2138 if (!(el->pktns->rx.nb_ack_eliciting & 1))
2139 el->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
2140 }
2141
2142 /* Update the largest packet number. */
2143 if (pkt->pn > el->pktns->rx.largest_pn)
2144 el->pktns->rx.largest_pn = pkt->pn;
2145
2146 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002147 if (!quic_update_ack_ranges_list(&el->pktns->rx.arngs, &ar)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002148 TRACE_DEVEL("Could not update ack range list",
2149 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2150 node = eb64_next(node);
2151 quic_rx_packet_eb64_delete(&pkt->pn_node);
2152 free_quic_rx_packet(pkt);
2153 goto err;
2154 }
2155
2156 }
2157 }
2158 node = eb64_next(node);
2159 quic_rx_packet_eb64_delete(&pkt->pn_node);
2160 free_quic_rx_packet(pkt);
2161 }
2162
2163 if (!qc_treat_rx_crypto_frms(el, ctx))
2164 goto err;
2165
2166 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2167 return 1;
2168
2169 err:
2170 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2171 return 0;
2172}
2173
2174/* Called during handshakes to parse and build Initial and Handshake packets for QUIC
2175 * connections with <ctx> as I/O handler context.
2176 * Returns 1 if succeeded, 0 if not.
2177 */
2178int qc_do_hdshk(struct quic_conn_ctx *ctx)
2179{
2180 int ssl_err;
2181 struct quic_conn *quic_conn;
2182 enum quic_tls_enc_level tel, next_tel;
2183 struct quic_enc_level *qel, *next_qel;
2184 struct quic_tls_ctx *tls_ctx;
2185
2186 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state);
2187
2188 ssl_err = SSL_ERROR_NONE;
2189 quic_conn = ctx->conn->qc;
2190 if (!quic_get_tls_enc_levels(&tel, &next_tel, ctx->state))
2191 goto err;
2192
2193 qel = &quic_conn->els[tel];
2194 next_qel = &quic_conn->els[next_tel];
2195
2196 next_level:
2197 tls_ctx = &qel->tls_ctx;
2198
2199 /* If the header protection key for this level has been derived,
2200 * remove the packet header protections.
2201 */
2202 if (!LIST_ISEMPTY(&qel->rx.pqpkts) &&
2203 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2204 qc_rm_hp_pkts(qel, ctx);
2205
2206 if (!eb_is_empty(&qel->rx.pkts) &&
2207 !qc_treat_rx_pkts(qel, ctx))
2208 goto err;
2209
2210 if (!qc_prep_hdshk_pkts(ctx))
2211 goto err;
2212
2213 if (!qc_send_ppkts(ctx))
2214 goto err;
2215
2216 /* Check if there is something to do for the next level.
2217 */
2218 if ((next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
2219 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
2220 qel = next_qel;
2221 goto next_level;
2222 }
2223
2224 /* If the handshake has not been completed -> out! */
2225 if (ctx->state < QUIC_HS_ST_COMPLETE)
2226 goto out;
2227
2228 /* Discard the Handshake keys. */
2229 quic_tls_discard_keys(&quic_conn->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2230 quic_pktns_discard(quic_conn->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, quic_conn);
2231 qc_set_timer(ctx);
2232 if (!quic_build_post_handshake_frames(quic_conn) ||
2233 !qc_prep_phdshk_pkts(quic_conn) ||
2234 !qc_send_ppkts(ctx))
2235 goto err;
2236
2237 out:
2238 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state);
2239 return 1;
2240
2241 err:
2242 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
2243 return 0;
2244}
2245
2246/* Allocate a new QUIC connection and return it if succeeded, NULL if not. */
2247struct quic_conn *new_quic_conn(uint32_t version)
2248{
2249 struct quic_conn *quic_conn;
2250
2251 quic_conn = pool_alloc(pool_head_quic_conn);
2252 if (quic_conn) {
2253 memset(quic_conn, 0, sizeof *quic_conn);
2254 quic_conn->version = version;
2255 }
2256
2257 return quic_conn;
2258}
2259
2260/* Unitialize <qel> QUIC encryption level. Never fails. */
2261static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2262{
2263 int i;
2264
2265 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2266 if (qel->tx.crypto.bufs[i]) {
2267 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2268 qel->tx.crypto.bufs[i] = NULL;
2269 }
2270 }
2271 free(qel->tx.crypto.bufs);
2272 qel->tx.crypto.bufs = NULL;
2273}
2274
2275/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
2276 * connetion allocating everything needed.
2277 * Returns 1 if succeeded, 0 if not.
2278 */
2279static int quic_conn_enc_level_init(struct quic_conn *qc,
2280 enum quic_tls_enc_level level)
2281{
2282 struct quic_enc_level *qel;
2283
2284 qel = &qc->els[level];
2285 qel->level = quic_to_ssl_enc_level(level);
2286 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2287 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2288 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2289 qel->tls_ctx.rx.flags = 0;
2290 qel->tls_ctx.tx.flags = 0;
2291
2292 qel->rx.pkts = EB_ROOT;
2293 LIST_INIT(&qel->rx.pqpkts);
2294
2295 /* Allocate only one buffer. */
2296 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2297 if (!qel->tx.crypto.bufs)
2298 goto err;
2299
2300 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2301 if (!qel->tx.crypto.bufs[0])
2302 goto err;
2303
2304 qel->tx.crypto.bufs[0]->sz = 0;
2305 qel->tx.crypto.nb_buf = 1;
2306
2307 qel->tx.crypto.sz = 0;
2308 qel->tx.crypto.offset = 0;
2309
2310 return 1;
2311
2312 err:
2313 free(qel->tx.crypto.bufs);
2314 qel->tx.crypto.bufs = NULL;
2315 return 0;
2316}
2317
2318/* Release the memory allocated for <buf> array of buffers, with <nb> as size.
2319 * Never fails.
2320 */
2321static inline void free_quic_conn_tx_bufs(struct q_buf **bufs, size_t nb)
2322{
2323 struct q_buf **p;
2324
2325 if (!bufs)
2326 return;
2327
2328 p = bufs;
2329 while (--nb) {
2330 if (!*p) {
2331 p++;
2332 continue;
2333 }
2334 free((*p)->area);
2335 (*p)->area = NULL;
2336 free(*p);
2337 *p = NULL;
2338 p++;
2339 }
2340 free(bufs);
2341}
2342
2343/* Allocate an array or <nb> buffers of <sz> bytes each.
2344 * Return this array if succeeded, NULL if failed.
2345 */
2346static inline struct q_buf **quic_conn_tx_bufs_alloc(size_t nb, size_t sz)
2347{
2348 int i;
2349 struct q_buf **bufs, **p;
2350
2351 bufs = calloc(nb, sizeof *bufs);
2352 if (!bufs)
2353 return NULL;
2354
2355 i = 0;
2356 p = bufs;
2357 while (i++ < nb) {
2358 *p = calloc(1, sizeof **p);
2359 if (!*p)
2360 goto err;
2361
2362 (*p)->area = malloc(sz);
2363 if (!(*p)->area)
2364 goto err;
2365
2366 (*p)->pos = (*p)->area;
2367 (*p)->end = (*p)->area + sz;
2368 (*p)->data = 0;
2369 LIST_INIT(&(*p)->pkts);
2370 p++;
2371 }
2372
2373 return bufs;
2374
2375 err:
2376 free_quic_conn_tx_bufs(bufs, nb);
2377 return NULL;
2378}
2379
2380/* Release all the memory allocated for <conn> QUIC connection. */
2381static void quic_conn_free(struct quic_conn *conn)
2382{
2383 int i;
2384
2385 free_quic_conn_cids(conn);
2386 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2387 quic_conn_enc_level_uninit(&conn->els[i]);
2388 free_quic_conn_tx_bufs(conn->tx.bufs, conn->tx.nb_buf);
2389 if (conn->timer_task)
2390 task_destroy(conn->timer_task);
2391 pool_free(pool_head_quic_conn, conn);
2392}
2393
2394/* Callback called upon loss detection and PTO timer expirations. */
2395static struct task *process_timer(struct task *task, void *ctx, unsigned short state)
2396{
2397 struct quic_conn_ctx *conn_ctx;
2398 struct quic_conn *qc;
2399 struct quic_pktns *pktns;
2400
2401
2402 conn_ctx = task->context;
2403 qc = conn_ctx->conn->qc;
2404 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn);
2405 task->expire = TICK_ETERNITY;
2406 pktns = quic_loss_pktns(qc);
2407 if (tick_isset(pktns->tx.loss_time)) {
2408 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2409
2410 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2411 if (!LIST_ISEMPTY(&lost_pkts))
2412 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2413 qc_set_timer(conn_ctx);
2414 goto out;
2415 }
2416
2417 if (qc->path->in_flight) {
2418 pktns = quic_pto_pktns(qc, conn_ctx->state >= QUIC_HS_ST_COMPLETE, NULL);
2419 pktns->tx.pto_probe = 1;
2420 }
2421 else if (objt_server(qc->conn->target) && conn_ctx->state <= QUIC_HS_ST_COMPLETE) {
2422 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2423 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2424
2425 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2426 hel->pktns->tx.pto_probe = 1;
2427 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2428 iel->pktns->tx.pto_probe = 1;
2429 }
2430 qc->tx.nb_pto_dgrams = QUIC_MAX_NB_PTO_DGRAMS;
2431 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2432 qc->path->loss.pto_count++;
2433
2434 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002435 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002436
2437 return task;
2438}
2439
2440/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2441 * connections used to identify the first Initial packets of client connecting
2442 * to listeners. This parameter must be NULL for QUIC connections attached
2443 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2444 * <scid> is the source connection ID with <scid_len> as length.
2445 * Returns 1 if succeeded, 0 if not.
2446 */
2447int qc_new_conn_init(struct quic_conn *qc, int ipv4,
2448 struct eb_root *quic_initial_clients,
2449 struct eb_root *quic_clients,
2450 unsigned char *dcid, size_t dcid_len,
2451 unsigned char *scid, size_t scid_len)
2452{
2453 int i;
2454 /* Initial CID. */
2455 struct quic_connection_id *icid;
2456
2457 TRACE_ENTER(QUIC_EV_CONN_INIT, qc->conn);
2458 qc->cids = EB_ROOT;
2459 /* QUIC Server (or listener). */
2460 if (objt_listener(qc->conn->target)) {
2461 /* Copy the initial DCID. */
2462 qc->odcid.len = dcid_len;
2463 if (qc->odcid.len)
2464 memcpy(qc->odcid.data, dcid, dcid_len);
2465
2466 /* Copy the SCID as our DCID for this connection. */
2467 if (scid_len)
2468 memcpy(qc->dcid.data, scid, scid_len);
2469 qc->dcid.len = scid_len;
2470 }
2471 /* QUIC Client (outgoing connection to servers) */
2472 else {
2473 if (dcid_len)
2474 memcpy(qc->dcid.data, dcid, dcid_len);
2475 qc->dcid.len = dcid_len;
2476 }
2477
2478 /* Initialize the output buffer */
2479 qc->obuf.pos = qc->obuf.data;
2480
2481 icid = new_quic_cid(&qc->cids, 0);
2482 if (!icid)
2483 return 0;
2484
2485 /* Select our SCID which is the first CID with 0 as sequence number. */
2486 qc->scid = icid->cid;
2487
2488 /* Insert the DCID the QUIC client has choosen (only for listeners) */
2489 if (objt_listener(qc->conn->target))
2490 ebmb_insert(quic_initial_clients, &qc->odcid_node, qc->odcid.len);
2491
2492 /* Insert our SCID, the connection ID for the QUIC client. */
2493 ebmb_insert(quic_clients, &qc->scid_node, qc->scid.len);
2494
2495 /* Packet number spaces initialization. */
2496 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2497 quic_pktns_init(&qc->pktns[i]);
2498 /* QUIC encryption level context initialization. */
2499 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
2500 if (!quic_conn_enc_level_init(qc, i))
2501 goto err;
2502 /* Initialize the packet number space. */
2503 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2504 }
2505
2506 /* TX part. */
2507 LIST_INIT(&qc->tx.frms_to_send);
2508 qc->tx.bufs = quic_conn_tx_bufs_alloc(QUIC_CONN_TX_BUFS_NB, QUIC_CONN_TX_BUF_SZ);
2509 if (!qc->tx.bufs)
2510 goto err;
2511
2512 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2513 qc->tx.wbuf = qc->tx.rbuf = 0;
2514 qc->tx.bytes = 0;
2515 qc->tx.nb_pto_dgrams = 0;
2516 /* RX part. */
2517 qc->rx.bytes = 0;
2518
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002519 /* XXX TO DO: Only one path at this time. */
2520 qc->path = &qc->paths[0];
2521 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2522
2523 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc->conn);
2524
2525 return 1;
2526
2527 err:
2528 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc->conn);
2529 quic_conn_free(qc);
2530 return 0;
2531}
2532
2533/* Initialize the timer task of <qc> QUIC connection.
2534 * Returns 1 if succeeded, 0 if not.
2535 */
2536static int quic_conn_init_timer(struct quic_conn *qc)
2537{
2538 qc->timer_task = task_new(MAX_THREADS_MASK);
2539 if (!qc->timer_task)
2540 return 0;
2541
2542 qc->timer = TICK_ETERNITY;
2543 qc->timer_task->process = process_timer;
2544 qc->timer_task->context = qc->conn->xprt_ctx;
2545
2546 return 1;
2547}
2548
2549/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
2550 * past one byte of this buffer.
2551 */
2552static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
2553 struct quic_rx_packet *pkt)
2554{
2555 unsigned char dcid_len, scid_len;
2556
2557 /* Version */
2558 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
2559 return 0;
2560
2561 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
2562
2563 /* Destination Connection ID Length */
2564 dcid_len = *(*buf)++;
2565 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
2566 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
2567 /* XXX MUST BE DROPPED */
2568 return 0;
2569
2570 if (dcid_len) {
2571 /* Check that the length of this received DCID matches the CID lengths
2572 * of our implementation for non Initials packets only.
2573 */
2574 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
2575 return 0;
2576
2577 memcpy(pkt->dcid.data, *buf, dcid_len);
2578 }
2579
2580 pkt->dcid.len = dcid_len;
2581 *buf += dcid_len;
2582
2583 /* Source Connection ID Length */
2584 scid_len = *(*buf)++;
2585 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
2586 /* XXX MUST BE DROPPED */
2587 return 0;
2588
2589 if (scid_len)
2590 memcpy(pkt->scid.data, *buf, scid_len);
2591 pkt->scid.len = scid_len;
2592 *buf += scid_len;
2593
2594 return 1;
2595}
2596
2597/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
2598 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
2599 * byte past the end of the buffer containing this packet and <beg> the address of
2600 * the packet first byte.
2601 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
2602 * Returns 1 if succeeded, 0 if not.
2603 */
2604static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
2605 unsigned char **buf, unsigned char *beg,
2606 const unsigned char *end,
2607 struct quic_conn_ctx *ctx)
2608{
2609 unsigned char *pn = NULL; /* Packet number field */
2610 enum quic_tls_enc_level tel;
2611 struct quic_enc_level *qel;
2612 /* Only for traces. */
2613 struct quic_rx_packet *qpkt_trace;
2614
2615 qpkt_trace = NULL;
2616 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx->conn);
2617 /* The packet number is here. This is also the start minus
2618 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
2619 * protection.
2620 */
2621 pn = *buf;
2622 tel = quic_packet_type_enc_level(pkt->type);
2623 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
2624 TRACE_DEVEL("Wrong enc. level", QUIC_EV_CONN_TRMHP, ctx->conn);
2625 goto err;
2626 }
2627
2628 qel = &ctx->conn->qc->els[tel];
2629
2630 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
2631 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
2632 goto err;
2633 }
2634
2635 if ((qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
2636 (tel != QUIC_TLS_ENC_LEVEL_APP || ctx->state >= QUIC_HS_ST_COMPLETE)) {
2637 /* Note that the following function enables us to unprotect the packet
2638 * number and its length subsequently used to decrypt the entire
2639 * packets.
2640 */
2641 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
2642 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
2643 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx->conn);
2644 goto err;
2645 }
2646
2647 /* The AAD includes the packet number field found at <pn>. */
2648 pkt->aad_len = pn - beg + pkt->pnl;
2649 qpkt_trace = pkt;
2650 /* Store the packet */
2651 pkt->pn_node.key = pkt->pn;
2652 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
2653 }
2654 else {
2655 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx->conn, pkt);
2656 pkt->pn_offset = pn - beg;
2657 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
2658 }
2659
2660 memcpy(pkt->data, beg, pkt->len);
2661 /* Updtate the offset of <*buf> for the next QUIC packet. */
2662 *buf = beg + pkt->len;
2663
2664 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx->conn, qpkt_trace);
2665 return 1;
2666
2667 err:
2668 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx->conn, qpkt_trace);
2669 return 0;
2670}
2671
2672/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
2673 * Also set <*long_header> to 1 if this form is long, 0 if not.
2674 */
2675static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
2676 unsigned char byte0, int *long_header)
2677{
2678 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
2679 pkt->type =
2680 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
2681 *long_header = 1;
2682 }
2683 else {
2684 pkt->type = QUIC_PACKET_TYPE_SHORT;
2685 *long_header = 0;
2686 }
2687}
2688
2689static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
2690 struct quic_rx_packet *pkt,
2691 struct quic_dgram_ctx *dgram_ctx,
2692 struct sockaddr_storage *saddr)
2693{
2694 unsigned char *beg;
2695 uint64_t len;
2696 struct quic_conn *qc;
2697 struct eb_root *cids;
2698 struct ebmb_node *node;
2699 struct connection *srv_conn;
2700 struct quic_conn_ctx *conn_ctx;
2701 int long_header;
2702
2703 qc = NULL;
2704 TRACE_ENTER(QUIC_EV_CONN_SPKT);
2705 if (end <= *buf)
2706 goto err;
2707
2708 /* Fixed bit */
2709 if (!(**buf & QUIC_PACKET_FIXED_BIT))
2710 /* XXX TO BE DISCARDED */
2711 goto err;
2712
2713 srv_conn = dgram_ctx->owner;
2714 beg = *buf;
2715 /* Header form */
2716 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
2717 if (long_header) {
2718 size_t cid_lookup_len;
2719
2720 if (!quic_packet_read_long_header(buf, end, pkt))
2721 goto err;
2722
2723 /* For Initial packets, and for servers (QUIC clients connections),
2724 * there is no Initial connection IDs storage.
2725 */
2726 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
2727 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
2728 cid_lookup_len = pkt->dcid.len;
2729 }
2730 else {
2731 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
2732 cid_lookup_len = QUIC_CID_LEN;
2733 }
2734
2735 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
2736 if (!node)
2737 goto err;
2738
2739 qc = ebmb_entry(node, struct quic_conn, scid_node);
2740
2741 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
2742 qc->dcid.len = pkt->scid.len;
2743 if (pkt->scid.len)
2744 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
2745 }
2746
2747 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
2748 uint64_t token_len;
2749
2750 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
2751 goto err;
2752
2753 /* XXX TO DO XXX 0 value means "the token is not present".
2754 * A server which sends an Initial packet must not set the token.
2755 * So, a client which receives an Initial packet with a token
2756 * MUST discard the packet or generate a connection error with
2757 * PROTOCOL_VIOLATION as type.
2758 * The token must be provided in a Retry packet or NEW_TOKEN frame.
2759 */
2760 pkt->token_len = token_len;
2761 }
2762 }
2763 else {
2764 /* XXX TO DO: Short header XXX */
2765 if (end - *buf < QUIC_CID_LEN)
2766 goto err;
2767
2768 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
2769 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
2770 if (!node)
2771 goto err;
2772
2773 qc = ebmb_entry(node, struct quic_conn, scid_node);
2774 *buf += QUIC_CID_LEN;
2775 }
2776 /* Store the DCID used for this packet to check the packet which
2777 * come in this UDP datagram match with it.
2778 */
2779 if (!dgram_ctx->dcid_node)
2780 dgram_ctx->dcid_node = node;
2781 /* Only packets packets with long headers and not RETRY or VERSION as type
2782 * have a length field.
2783 */
2784 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
2785 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
2786 goto err;
2787
2788 pkt->len = len;
2789 }
2790 else if (!long_header) {
2791 /* A short packet is the last one of an UDP datagram. */
2792 pkt->len = end - *buf;
2793 }
2794
2795 conn_ctx = qc->conn->xprt_ctx;
2796
2797 /* Increase the total length of this packet by the header length. */
2798 pkt->len += *buf - beg;
2799 /* Do not check the DCID node before the length. */
2800 if (dgram_ctx->dcid_node != node) {
2801 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
2802 goto err;
2803 }
2804
2805 if (pkt->len > sizeof pkt->data) {
2806 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
2807 goto err;
2808 }
2809
2810 if (!qc_try_rm_hp(pkt, buf, beg, end, conn_ctx))
2811 goto err;
2812
2813 /* Wake the tasklet of the QUIC connection packet handler. */
2814 if (conn_ctx)
2815 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2816
2817 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
2818
2819 return pkt->len;
2820
2821 err:
2822 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_ESPKT, qc ? qc->conn : NULL);
2823 return -1;
2824}
2825
2826static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
2827 struct quic_rx_packet *pkt,
2828 struct quic_dgram_ctx *dgram_ctx,
2829 struct sockaddr_storage *saddr)
2830{
2831 unsigned char *beg;
2832 uint64_t len;
2833 struct quic_conn *qc;
2834 struct eb_root *cids;
2835 struct ebmb_node *node;
2836 struct listener *l;
2837 struct quic_conn_ctx *conn_ctx;
2838 int long_header = 0;
2839
2840 qc = NULL;
2841 TRACE_ENTER(QUIC_EV_CONN_LPKT);
2842 if (end <= *buf)
2843 goto err;
2844
2845 /* Fixed bit */
2846 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
2847 /* XXX TO BE DISCARDED */
2848 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
2849 goto err;
2850 }
2851
2852 l = dgram_ctx->owner;
2853 beg = *buf;
2854 /* Header form */
2855 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
2856 if (long_header) {
2857 unsigned char dcid_len;
2858
2859 if (!quic_packet_read_long_header(buf, end, pkt)) {
2860 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
2861 goto err;
2862 }
2863
2864 dcid_len = pkt->dcid.len;
2865 /* For Initial packets, and for servers (QUIC clients connections),
2866 * there is no Initial connection IDs storage.
2867 */
2868 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
2869 /* DCIDs of first packets coming from clients may have the same values.
2870 * Let's distinguish them concatenating the socket addresses to the DCIDs.
2871 */
2872 quic_cid_saddr_cat(&pkt->dcid, saddr);
2873 cids = &l->rx.odcids;
2874 }
2875 else {
2876 if (pkt->dcid.len != QUIC_CID_LEN) {
2877 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
2878 goto err;
2879 }
2880
2881 cids = &l->rx.cids;
2882 }
2883
2884 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
2885 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
2886 cids == &l->rx.odcids) {
2887 /* Switch to the definitive tree ->cids containing the final CIDs. */
2888 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
2889 if (node) {
2890 /* If found, signal this with NULL as special value for <cids>. */
2891 pkt->dcid.len = dcid_len;
2892 cids = NULL;
2893 }
2894 }
2895
2896 if (!node) {
2897 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
2898 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
2899 goto err;
2900 }
2901
2902 qc = new_quic_conn(pkt->version);
2903 if (!qc) {
2904 TRACE_PROTO("Non allocated new connection", QUIC_EV_CONN_LPKT);
2905 goto err;
2906 }
2907
2908 pkt->qc = qc;
2909 pkt->saddr = *saddr;
2910 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
2911 * of <saddr>.
2912 */
2913 pkt->odcid_len = dcid_len;
2914 /* Enqueue this packet. */
2915 LIST_ADDQ(&l->rx.qpkts, &pkt->rx_list);
2916 /* Try to accept a new connection. */
2917 listener_accept(l);
2918 if (!qc->conn) {
2919 TRACE_PROTO("Non accepted connection", QUIC_EV_CONN_LPKT, qc->conn);
2920 goto err;
2921 }
2922
2923 if (!quic_conn_init_timer(qc)) {
2924 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc->conn);
2925 goto err;
2926 }
2927
2928 /* This is the DCID node sent in this packet by the client. */
2929 node = &qc->odcid_node;
2930 conn_ctx = qc->conn->xprt_ctx;
2931 SSL_set_quic_transport_params(conn_ctx->ssl,
2932 qc->enc_params, qc->enc_params_len);
2933 }
2934 else {
2935 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
2936 qc = ebmb_entry(node, struct quic_conn, odcid_node);
2937 else
2938 qc = ebmb_entry(node, struct quic_conn, scid_node);
2939 }
2940
2941 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
2942 uint64_t token_len;
2943 struct quic_tls_ctx *ctx =
2944 &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
2945
2946 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
2947 end - *buf < token_len) {
2948 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
2949 goto err;
2950 }
2951
2952 /* XXX TO DO XXX 0 value means "the token is not present".
2953 * A server which sends an Initial packet must not set the token.
2954 * So, a client which receives an Initial packet with a token
2955 * MUST discard the packet or generate a connection error with
2956 * PROTOCOL_VIOLATION as type.
2957 * The token must be provided in a Retry packet or NEW_TOKEN frame.
2958 */
2959 pkt->token_len = token_len;
2960 /* NOTE: the socket address has been concatenated to the destination ID
2961 * choosen by the client for Initial packets.
2962 */
2963 if (!ctx->rx.hp && !qc_new_isecs(qc->conn, pkt->dcid.data,
2964 pkt->odcid_len, 1)) {
2965 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
2966 goto err;
2967 }
2968 }
2969 }
2970 else {
2971 if (end - *buf < QUIC_CID_LEN) {
2972 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
2973 goto err;
2974 }
2975
2976 cids = &l->rx.cids;
2977 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
2978 if (!node) {
2979 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
2980 goto err;
2981 }
2982
2983 qc = ebmb_entry(node, struct quic_conn, scid_node);
2984 *buf += QUIC_CID_LEN;
2985 }
2986
2987 /* Store the DCID used for this packet to check the packet which
2988 * come in this UDP datagram match with it.
2989 */
2990 if (!dgram_ctx->dcid_node) {
2991 dgram_ctx->dcid_node = node;
2992 dgram_ctx->qc = qc;
2993 }
2994
2995 /* Only packets packets with long headers and not RETRY or VERSION as type
2996 * have a length field.
2997 */
2998 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
2999 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3000 end - *buf < len) {
3001 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3002 goto err;
3003 }
3004
3005 pkt->len = len;
3006 }
3007 else if (!long_header) {
3008 /* A short packet is the last one of an UDP datagram. */
3009 pkt->len = end - *buf;
3010 }
3011
3012 /* Update the state if needed. */
3013 conn_ctx = qc->conn->xprt_ctx;
3014
3015 /* Increase the total length of this packet by the header length. */
3016 pkt->len += *buf - beg;
3017 /* Do not check the DCID node before the length. */
3018 if (dgram_ctx->dcid_node != node) {
3019 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3020 goto err;
3021 }
3022
3023 if (pkt->len > sizeof pkt->data) {
3024 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3025 goto err;
3026 }
3027
3028 if (!qc_try_rm_hp(pkt, buf, beg, end, conn_ctx)) {
3029 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3030 goto err;
3031 }
3032
3033 /* Wake the tasklet of the QUIC connection packet handler. */
3034 if (conn_ctx)
3035 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3036 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3037
3038 return pkt->len;
3039
3040 err:
3041 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT|QUIC_EV_CONN_ELPKT,
3042 qc ? qc->conn : NULL, pkt);
3043 return -1;
3044}
3045
3046/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3047 * in advance. This is the reponsability of the caller to check there is enough room in this
3048 * buffer to build a long header.
3049 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3050 */
3051static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3052 int type, size_t pn_len, struct quic_conn *conn)
3053{
3054 if (type > QUIC_PACKET_TYPE_RETRY)
3055 return 0;
3056
3057 /* #0 byte flags */
3058 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3059 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3060 /* Version */
3061 quic_write_uint32(buf, end, conn->version);
3062 *(*buf)++ = conn->dcid.len;
3063 /* Destination connection ID */
3064 if (conn->dcid.len) {
3065 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3066 *buf += conn->dcid.len;
3067 }
3068 /* Source connection ID */
3069 *(*buf)++ = conn->scid.len;
3070 if (conn->scid.len) {
3071 memcpy(*buf, conn->scid.data, conn->scid.len);
3072 *buf += conn->scid.len;
3073 }
3074
3075 return 1;
3076}
3077
3078/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3079 * in advance. This is the reponsability of the caller to check there is enough room in this
3080 * buffer to build a long header.
3081 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3082 */
3083static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3084 size_t pn_len, struct quic_conn *conn)
3085{
3086 /* #0 byte flags */
3087 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3088 /* Destination connection ID */
3089 if (conn->dcid.len) {
3090 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3091 *buf += conn->dcid.len;
3092 }
3093
3094 return 1;
3095}
3096
3097/* Apply QUIC header protection to the packet with <buf> as first byte address,
3098 * <pn> as address of the Packet number field, <pnlen> being this field length
3099 * with <aead> as AEAD cipher and <key> as secret key.
3100 * Returns 1 if succeeded or 0 if failed.
3101 */
3102static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3103 const EVP_CIPHER *aead, const unsigned char *key)
3104{
3105 int i, ret, outlen;
3106 EVP_CIPHER_CTX *ctx;
3107 /* We need an IV of at least 5 bytes: one byte for bytes #0
3108 * and at most 4 bytes for the packet number
3109 */
3110 unsigned char mask[5] = {0};
3111
3112 ret = 0;
3113 ctx = EVP_CIPHER_CTX_new();
3114 if (!ctx)
3115 return 0;
3116
3117 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3118 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3119 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3120 goto out;
3121
3122 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3123 for (i = 0; i < pnlen; i++)
3124 pn[i] ^= mask[i + 1];
3125
3126 ret = 1;
3127
3128 out:
3129 EVP_CIPHER_CTX_free(ctx);
3130
3131 return ret;
3132}
3133
3134/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3135 * ACK ranges if needed to a value below <limit> in bytes.
3136 * Return 1 if succeeded, 0 if not.
3137 */
3138static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3139{
3140 size_t room, ack_delay_sz;
3141
3142 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3143 /* A frame is made of 1 byte for the frame type. */
3144 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003145 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003146 return 0;
3147
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003148 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003149}
3150
3151/* Prepare as most as possible CRYPTO frames from prebuilt CRYPTO frames for <qel>
3152 * encryption level to be encoded in a buffer with <room> as available room,
3153 * and <*len> as number of bytes already present in this buffer.
3154 * Update consequently <*len> to reflect the size of these CRYPTO frames built
3155 * by this function. Also attach these CRYPTO frames to <pkt> QUIC packet.
3156 * Return 1 if succeeded, 0 if not.
3157 */
3158static inline int qc_build_cfrms(struct quic_tx_packet *pkt,
3159 size_t room, size_t *len,
3160 struct quic_enc_level *qel,
3161 struct quic_conn *conn)
3162{
3163 struct quic_tx_frm *cf, *cfbak;
3164 size_t max_cdata_len;
3165
3166 if (conn->tx.nb_pto_dgrams)
3167 max_cdata_len = room;
3168 else
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003169 max_cdata_len = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003170 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
3171 /* header length, data length, frame length. */
3172 size_t hlen, dlen, cflen;
3173
3174 if (!max_cdata_len)
3175 break;
3176
3177 /* Compute the length of this CRYPTO frame header */
3178 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3179 /* Compute the data length of this CRyPTO frame. */
3180 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3181 if (!dlen)
3182 break;
3183
3184 if (dlen > max_cdata_len)
3185 dlen = max_cdata_len;
3186 max_cdata_len -= dlen;
3187 pkt->cdata_len += dlen;
3188 /* CRYPTO frame length. */
3189 cflen = hlen + quic_int_getsize(dlen) + dlen;
3190 /* Add the CRYPTO data length and its encoded length to the packet
3191 * length and the length of this length.
3192 */
3193 *len += cflen;
3194 if (dlen == cf->crypto.len) {
3195 /* <cf> CRYPTO data have been consumed. */
3196 LIST_DEL(&cf->list);
3197 LIST_ADDQ(&pkt->frms, &cf->list);
3198 }
3199 else {
3200 struct quic_tx_frm *new_cf;
3201
3202 new_cf = pool_alloc(pool_head_quic_tx_frm);
3203 if (!new_cf) {
3204 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_ECHPKT, conn->conn);
3205 return 0;
3206 }
3207
3208 new_cf->type = QUIC_FT_CRYPTO;
3209 new_cf->crypto.len = dlen;
3210 new_cf->crypto.offset = cf->crypto.offset;
3211 LIST_ADDQ(&pkt->frms, &new_cf->list);
3212 /* Consume <dlen> bytes of the current frame. */
3213 cf->crypto.len -= dlen;
3214 cf->crypto.offset += dlen;
3215 }
3216 }
3217
3218 return 1;
3219}
3220
3221/* This function builds a clear handshake packet used during a QUIC TLS handshakes
3222 * into <wbuf> the current <wbuf> for <conn> QUIC connection with <qel> as QUIC
3223 * TLS encryption level for outgoing packets filling it with as much as CRYPTO
3224 * data as possible from <offset> offset in the CRYPTO data stream. Note that
3225 * this offset value is updated by the length of the CRYPTO frame used to embed
3226 * the CRYPTO data if this packet and only if the packet is successfully built.
3227 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
3228 * reserved so that to be sure there is enough room to build this AEAD TAG after
3229 * having successfully returned from this function and to be sure the position
3230 * pointer of <wbuf> may be safely incremented by QUIC_TLS_TAG_LEN. After having
3231 * returned from this funciton, <wbuf> position will point one past the last
3232 * byte of the payload with the confidence there is at least QUIC_TLS_TAG_LEN bytes
3233 * available packet to encrypt this packet.
3234 * This function also update the value of <buf_pn> pointer to point to the packet
3235 * number field in this packet. <pn_len> will also have the packet number
3236 * length as value.
3237 *
3238 * Return the length of the packet if succeeded minus QUIC_TLS_TAG_LEN, or -1 if
3239 * failed (not enough room in <wbuf> to build this packet plus QUIC_TLS_TAG_LEN
3240 * bytes), -2 if there are too much CRYPTO data in flight to build a packet.
3241 */
3242static ssize_t qc_do_build_hdshk_pkt(struct q_buf *wbuf,
3243 struct quic_tx_packet *pkt, int pkt_type,
3244 int64_t pn, size_t *pn_len,
3245 unsigned char **buf_pn,
3246 struct quic_enc_level *qel,
3247 struct quic_conn *conn)
3248{
3249 unsigned char *beg, *pos;
3250 const unsigned char *end;
3251 /* This packet type. */
3252 /* Packet number. */
3253 /* The Length QUIC packet field value which is the length
3254 * of the remaining data after this field after encryption.
3255 */
3256 size_t len, token_fields_len, padding_len;
3257 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3258 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3259 struct quic_crypto *crypto = &frm.crypto;
3260 size_t ack_frm_len;
3261 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003262 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003263
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003264 beg = pos = q_buf_getpos(wbuf);
3265 end = q_buf_end(wbuf);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003266 /* When not probing and not acking, reduce the size of this buffer to respect
3267 * the congestion controller window.
3268 */
3269 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3270 size_t path_room;
3271
3272 path_room = quic_path_prep_data(conn->path);
3273 if (end - beg > path_room)
3274 end = beg + path_room;
3275 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003276
3277 /* For a server, the token field of an Initial packet is empty. */
3278 token_fields_len = pkt_type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3279
3280 /* Check there is enough room to build the header followed by a token. */
3281 if (end - pos < QUIC_LONG_PACKET_MINLEN + conn->dcid.len +
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003282 conn->scid.len + token_fields_len + QUIC_TLS_TAG_LEN) {
3283 ssize_t room = end - pos;
3284 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3285 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003286 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003287 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003288
3289 /* Reserve enough room at the end of the packet for the AEAD TAG. */
3290 end -= QUIC_TLS_TAG_LEN;
3291 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3292 /* packet number length */
3293 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3294
3295 quic_build_packet_long_header(&pos, end, pkt_type, *pn_len, conn);
3296
3297 /* Encode the token length (0) for an Initial packet. */
3298 if (pkt_type == QUIC_PACKET_TYPE_INITIAL)
3299 *pos++ = 0;
3300
3301 /* Build an ACK frame if required. */
3302 ack_frm_len = 0;
3303 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003304 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003305 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003306 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003307 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003308 if (!ack_frm_len) {
3309 ssize_t room = end - pos;
3310 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3311 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003312 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003313 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003314
3315 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3316 }
3317
3318 /* Length field value without the CRYPTO frames data length. */
3319 len = ack_frm_len + *pn_len;
3320 if (!LIST_ISEMPTY(&qel->pktns->tx.frms) &&
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003321 !qc_build_cfrms(pkt, end - pos, &len, qel, conn)) {
3322 ssize_t room = end - pos;
3323 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3324 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003325 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003326 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003327
3328 add_ping_frm = 0;
3329 padding_len = 0;
3330 if (objt_server(conn->conn->target) &&
3331 pkt_type == QUIC_PACKET_TYPE_INITIAL &&
3332 len < QUIC_INITIAL_PACKET_MINLEN) {
3333 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
3334 }
3335 else if (LIST_ISEMPTY(&pkt->frms)) {
3336 if (qel->pktns->tx.pto_probe) {
3337 /* If we cannot send a CRYPTO frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003338 add_ping_frm = 1;
3339 len += 1;
3340 }
3341 /* If there is no frame at all to follow, add at least a PADDING frame. */
3342 if (!ack_frm_len)
3343 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
3344 }
3345
3346 /* Length (of the remaining data). Must not fail because, the buffer size
3347 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
3348 * for the encryption TAG. It must be taken into an account for the length
3349 * of this packet.
3350 */
3351 quic_enc_int(&pos, end, len + QUIC_TLS_TAG_LEN);
3352
3353 /* Packet number field address. */
3354 *buf_pn = pos;
3355
3356 /* Packet number encoding. */
3357 quic_packet_number_encode(&pos, end, pn, *pn_len);
3358
3359 if (ack_frm_len)
3360 qc_build_frm(&pos, end, &ack_frm, pkt, conn);
3361
3362 /* Crypto frame */
3363 if (!LIST_ISEMPTY(&pkt->frms)) {
3364 struct quic_tx_frm *cf;
3365
3366 list_for_each_entry(cf, &pkt->frms, list) {
3367 crypto->offset = cf->crypto.offset;
3368 crypto->len = cf->crypto.len;
3369 crypto->qel = qel;
3370 qc_build_frm(&pos, end, &frm, pkt, conn);
3371 }
3372 }
3373
3374 /* Build a PING frame if needed. */
3375 if (add_ping_frm) {
3376 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003377 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3378 ssize_t room = end - pos;
3379 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3380 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003381 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003382 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003383 }
3384
3385 /* Build a PADDING frame if needed. */
3386 if (padding_len) {
3387 frm.type = QUIC_FT_PADDING;
3388 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003389 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3390 ssize_t room = end - pos;
3391 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3392 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003393 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003394 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003395 }
3396
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003397 /* Always reset this variable as this function has no idea
3398 * if it was set. It is handle by the loss detection timer.
3399 */
3400 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003401
3402 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003403 return pos - beg;
3404
3405 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003406 return -1;
3407}
3408
3409static inline void quic_tx_packet_init(struct quic_tx_packet *pkt)
3410{
3411 pkt->cdata_len = 0;
3412 pkt->in_flight_len = 0;
3413 LIST_INIT(&pkt->frms);
3414}
3415
3416/* Free <pkt> TX packet which has not already attache to any tree. */
3417static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
3418{
3419 struct quic_tx_frm *frm, *frmbak;
3420
3421 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
3422 LIST_DEL(&frm->list);
3423 pool_free(pool_head_quic_tx_frm, frm);
3424 }
3425 pool_free(pool_head_quic_tx_packet, pkt);
3426}
3427
3428/* Build a handshake packet into <buf> packet buffer with <pkt_type> as packet
3429 * type for <qc> QUIC connection from CRYPTO data stream at <*offset> offset to
3430 * be encrypted at <qel> encryption level.
3431 * Return -2 if the packet could not be encrypted for any reason, -1 if there was
3432 * not enough room in <buf> to build the packet, or the size of the built packet
3433 * if succeeded (may be zero if there is too much crypto data in flight to build the packet).
3434 */
3435static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type,
3436 struct quic_enc_level *qel)
3437{
3438 /* The pointer to the packet number field. */
3439 unsigned char *buf_pn;
3440 unsigned char *beg, *end, *payload;
3441 int64_t pn;
3442 size_t pn_len, payload_len, aad_len;
3443 ssize_t pkt_len;
3444 struct quic_tls_ctx *tls_ctx;
3445 struct quic_tx_packet *pkt;
3446
3447 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn,, qel);
3448 pkt = pool_alloc(pool_head_quic_tx_packet);
3449 if (!pkt) {
3450 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn);
3451 return -2;
3452 }
3453
3454 quic_tx_packet_init(pkt);
3455 beg = q_buf_getpos(buf);
3456 pn_len = 0;
3457 buf_pn = NULL;
3458 pn = qel->pktns->tx.next_pn + 1;
3459 pkt_len = qc_do_build_hdshk_pkt(buf, pkt, pkt_type, pn, &pn_len, &buf_pn, qel, qc);
3460 if (pkt_len <= 0) {
3461 free_quic_tx_packet(pkt);
3462 return pkt_len;
3463 }
3464
3465 end = beg + pkt_len;
3466 payload = buf_pn + pn_len;
3467 payload_len = end - payload;
3468 aad_len = payload - beg;
3469
3470 tls_ctx = &qel->tls_ctx;
3471 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn))
3472 goto err;
3473
3474 end += QUIC_TLS_TAG_LEN;
3475 pkt_len += QUIC_TLS_TAG_LEN;
3476 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
3477 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
3478 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
3479 goto err;
3480 }
3481
3482 /* Now that a correct packet is built, let us set the position pointer of
3483 * <buf> buffer for the next packet.
3484 */
3485 q_buf_setpos(buf, end);
3486 /* Consume a packet number. */
3487 ++qel->pktns->tx.next_pn;
3488 /* Attach the built packet to its tree. */
3489 pkt->pn_node.key = qel->pktns->tx.next_pn;
3490 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003491 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003492 pkt->in_flight_len = pkt_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003493 qc->path->prep_in_flight += pkt_len;
3494 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003495 pkt->pktns = qel->pktns;
3496 eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node);
3497 /* Increment the number of bytes in <buf> buffer by the length of this packet. */
3498 buf->data += pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003499 /* Attach this packet to <buf>. */
3500 LIST_ADDQ(&buf->pkts, &pkt->list);
3501 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
3502
3503 return pkt_len;
3504
3505 err:
3506 free_quic_tx_packet(pkt);
3507 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
3508 return -2;
3509}
3510
3511/* Prepare a clear post handhskake packet for <conn> QUIC connnection.
3512 * Return the length of this packet if succeeded, -1 <wbuf> was full.
3513 */
3514static ssize_t qc_do_build_phdshk_apkt(struct q_buf *wbuf,
3515 struct quic_tx_packet *pkt,
3516 int64_t pn, size_t *pn_len,
3517 unsigned char **buf_pn, struct quic_enc_level *qel,
3518 struct quic_conn *conn)
3519{
3520 const unsigned char *beg, *end;
3521 unsigned char *pos;
3522 struct quic_frame *frm, *sfrm;
3523 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3524 size_t fake_len, ack_frm_len;
3525 int64_t largest_acked_pn;
3526
3527 TRACE_ENTER(QUIC_EV_CONN_CPAPKT, conn->conn);
3528 beg = pos = q_buf_getpos(wbuf);
3529 end = q_buf_end(wbuf);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003530 /* When not probing and not acking, reduce the size of this buffer to respect
3531 * the congestion controller window.
3532 */
3533 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3534 size_t path_room;
3535
3536 path_room = quic_path_prep_data(conn->path);
3537 if (end - beg > path_room)
3538 end = beg + path_room;
3539 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003540 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3541 /* Packet number length */
3542 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3543 /* Check there is enough room to build this packet (without payload). */
3544 if (end - pos < QUIC_SHORT_PACKET_MINLEN + sizeof_quic_cid(&conn->dcid) +
3545 *pn_len + QUIC_TLS_TAG_LEN)
3546 goto err;
3547
3548 /* Reserve enough room at the end of the packet for the AEAD TAG. */
3549 end -= QUIC_TLS_TAG_LEN;
3550 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3551 /* Packet number field. */
3552 *buf_pn = pos;
3553 /* Packet number encoding. */
3554 quic_packet_number_encode(&pos, end, pn, *pn_len);
3555
3556 /* Build an ACK frame if required. */
3557 ack_frm_len = 0;
3558 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003559 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003560 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003561 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003562 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
3563 if (!ack_frm_len)
3564 goto err;
3565
3566 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3567 }
3568
3569 if (ack_frm_len)
3570 qc_build_frm(&pos, end, &ack_frm, pkt, conn);
3571
3572 fake_len = ack_frm_len;
3573 if (!LIST_ISEMPTY(&qel->pktns->tx.frms) &&
3574 !qc_build_cfrms(pkt, end - pos, &fake_len, qel, conn)) {
3575 TRACE_DEVEL("some CRYPTO frames could not be built",
3576 QUIC_EV_CONN_CPAPKT, conn->conn);
3577 goto err;
3578 }
3579
3580 /* Crypto frame */
3581 if (!LIST_ISEMPTY(&pkt->frms)) {
3582 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3583 struct quic_crypto *crypto = &frm.crypto;
3584 struct quic_tx_frm *cf;
3585
3586 list_for_each_entry(cf, &pkt->frms, list) {
3587 crypto->offset = cf->crypto.offset;
3588 crypto->len = cf->crypto.len;
3589 crypto->qel = qel;
3590 qc_build_frm(&pos, end, &frm, pkt, conn);
3591 }
3592 }
3593
3594 /* Encode a maximum of frames. */
3595 list_for_each_entry_safe(frm, sfrm, &conn->tx.frms_to_send, list) {
3596 unsigned char *ppos;
3597
3598 ppos = pos;
3599 if (!qc_build_frm(&ppos, end, frm, pkt, conn)) {
3600 TRACE_DEVEL("Frames not built", QUIC_EV_CONN_CPAPKT, conn->conn);
3601 break;
3602 }
3603
3604 LIST_DEL(&frm->list);
3605 LIST_ADDQ(&pkt->frms, &frm->list);
3606 pos = ppos;
3607 }
3608
3609 out:
3610 TRACE_LEAVE(QUIC_EV_CONN_CPAPKT, conn->conn, (int *)(pos - beg));
3611 return pos - beg;
3612
3613 err:
3614 TRACE_DEVEL("leaving in error (buffer full)", QUIC_EV_CONN_CPAPKT, conn->conn);
3615 return -1;
3616}
3617
3618/* Prepare a post handhskake packet at Application encryption level for <conn>
3619 * QUIC connnection.
3620 * Return the length of this packet if succeeded, -1 if <wbuf> was full,
3621 * -2 in case of major error (encryption failure).
3622 */
3623static ssize_t qc_build_phdshk_apkt(struct q_buf *wbuf, struct quic_conn *qc)
3624{
3625 /* A pointer to the packet number fiel in <buf> */
3626 unsigned char *buf_pn;
3627 unsigned char *beg, *end, *payload;
3628 int64_t pn;
3629 size_t pn_len, aad_len, payload_len;
3630 ssize_t pkt_len;
3631 struct quic_tls_ctx *tls_ctx;
3632 struct quic_enc_level *qel;
3633 struct quic_tx_packet *pkt;
3634
3635 TRACE_ENTER(QUIC_EV_CONN_PAPKT, qc->conn);
3636 pkt = pool_alloc(pool_head_quic_tx_packet);
3637 if (!pkt) {
3638 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_PAPKT, qc->conn);
3639 return -2;
3640 }
3641
3642 quic_tx_packet_init(pkt);
3643 beg = q_buf_getpos(wbuf);
3644 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3645 pn_len = 0;
3646 buf_pn = NULL;
3647 pn = qel->pktns->tx.next_pn + 1;
3648 pkt_len = qc_do_build_phdshk_apkt(wbuf, pkt, pn, &pn_len, &buf_pn, qel, qc);
3649 if (pkt_len <= 0) {
3650 free_quic_tx_packet(pkt);
3651 return pkt_len;
3652 }
3653
3654 end = beg + pkt_len;
3655 payload = buf_pn + pn_len;
3656 payload_len = end - payload;
3657 aad_len = payload - beg;
3658
3659 tls_ctx = &qel->tls_ctx;
3660 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn))
3661 return -2;
3662
3663 end += QUIC_TLS_TAG_LEN;
3664 pkt_len += QUIC_TLS_TAG_LEN;
3665 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
3666 tls_ctx->tx.hp, tls_ctx->tx.hp_key))
3667 return -2;
3668
3669 q_buf_setpos(wbuf, end);
3670 /* Consume a packet number. */
3671 ++qel->pktns->tx.next_pn;
3672 /* Attach the built packet to its tree. */
3673 pkt->pn_node.key = qel->pktns->tx.next_pn;
3674 eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node);
3675 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003676 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003677 pkt->in_flight_len = pkt_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003678 qc->path->prep_in_flight += pkt_len;
3679 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003680 pkt->pktns = qel->pktns;
3681 /* Increment the number of bytes in <buf> buffer by the length of this packet. */
3682 wbuf->data += pkt_len;
3683 /* Attach this packet to <buf>. */
3684 LIST_ADDQ(&wbuf->pkts, &pkt->list);
3685
3686 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, qc->conn);
3687
3688 return pkt_len;
3689}
3690
3691/* Prepare a maximum of QUIC Application level packets from <ctx> QUIC
3692 * connection I/O handler context.
3693 * Returns 1 if succeeded, 0 if not.
3694 */
3695int qc_prep_phdshk_pkts(struct quic_conn *qc)
3696{
3697 struct q_buf *wbuf;
3698 struct quic_enc_level *qel;
3699
3700 TRACE_ENTER(QUIC_EV_CONN_PAPKTS, qc->conn);
3701 wbuf = q_wbuf(qc);
3702 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3703 while (q_buf_empty(wbuf)) {
3704 ssize_t ret;
3705
3706 if (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
3707 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003708 qc->path->prep_in_flight >= qc->path->cwnd)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003709 TRACE_DEVEL("nothing more to do",
3710 QUIC_EV_CONN_PAPKTS, qc->conn);
3711 break;
3712 }
3713
3714 ret = qc_build_phdshk_apkt(wbuf, qc);
3715 switch (ret) {
3716 case -1:
3717 /* Not enough room left in <wbuf>. */
3718 wbuf = q_next_wbuf(qc);
3719 continue;
3720 case -2:
3721 return 0;
3722 default:
3723 /* XXX TO CHECK: consume a buffer. */
3724 wbuf = q_next_wbuf(qc);
3725 continue;
3726 }
3727 }
3728 TRACE_LEAVE(QUIC_EV_CONN_PAPKTS, qc->conn);
3729
3730 return 1;
3731}
3732
3733/* QUIC connection packet handler task. */
3734static struct task *quic_conn_io_cb(struct task *t, void *context, unsigned short state)
3735{
3736 struct quic_conn_ctx *ctx = context;
3737
3738 if (ctx->state < QUIC_HS_ST_COMPLETE) {
3739 qc_do_hdshk(ctx);
3740 }
3741 else {
3742 struct quic_conn *qc = ctx->conn->qc;
3743
3744 /* XXX TO DO: may fail!!! XXX */
3745 qc_treat_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_APP], ctx);
3746 qc_prep_phdshk_pkts(qc);
3747 qc_send_ppkts(ctx);
3748 }
3749
3750 return NULL;
3751}
3752
3753/* Receive up to <count> bytes from connection <conn>'s socket and store them
3754 * into buffer <buf>. Only one call to recv() is performed, unless the
3755 * buffer wraps, in which case a second call may be performed. The connection's
3756 * flags are updated with whatever special event is detected (error, read0,
3757 * empty). The caller is responsible for taking care of those events and
3758 * avoiding the call if inappropriate. The function does not call the
3759 * connection's polling update function, so the caller is responsible for this.
3760 * errno is cleared before starting so that the caller knows that if it spots an
3761 * error without errno, it's pending and can be retrieved via getsockopt(SO_ERROR).
3762 */
3763static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx, struct buffer *buf, size_t count, int flags)
3764{
3765 ssize_t ret;
3766 size_t try, done = 0;
3767
3768 if (!conn_ctrl_ready(conn))
3769 return 0;
3770
3771 if (!fd_recv_ready(conn->handle.fd))
3772 return 0;
3773
3774 conn->flags &= ~CO_FL_WAIT_ROOM;
3775 errno = 0;
3776
3777 if (unlikely(!(fdtab[conn->handle.fd].ev & FD_POLL_IN))) {
3778 /* stop here if we reached the end of data */
3779 if ((fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_HUP)
3780 goto read0;
3781
3782 /* report error on POLL_ERR before connection establishment */
3783 if ((fdtab[conn->handle.fd].ev & FD_POLL_ERR) && (conn->flags & CO_FL_WAIT_L4_CONN)) {
3784 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
3785 goto leave;
3786 }
3787 }
3788
3789 /* read the largest possible block. For this, we perform only one call
3790 * to recv() unless the buffer wraps and we exactly fill the first hunk,
3791 * in which case we accept to do it once again. A new attempt is made on
3792 * EINTR too.
3793 */
3794 while (count > 0) {
3795 try = b_contig_space(buf);
3796 if (!try)
3797 break;
3798
3799 if (try > count)
3800 try = count;
3801
3802 ret = recvfrom(conn->handle.fd, b_tail(buf), try, 0, NULL, 0);
3803
3804 if (ret > 0) {
3805 b_add(buf, ret);
3806 done += ret;
3807 if (ret < try) {
3808 /* unfortunately, on level-triggered events, POLL_HUP
3809 * is generally delivered AFTER the system buffer is
3810 * empty, unless the poller supports POLL_RDHUP. If
3811 * we know this is the case, we don't try to read more
3812 * as we know there's no more available. Similarly, if
3813 * there's no problem with lingering we don't even try
3814 * to read an unlikely close from the client since we'll
3815 * close first anyway.
3816 */
3817 if (fdtab[conn->handle.fd].ev & FD_POLL_HUP)
3818 goto read0;
3819
3820 if ((!fdtab[conn->handle.fd].linger_risk) ||
3821 (cur_poller.flags & HAP_POLL_F_RDHUP)) {
3822 break;
3823 }
3824 }
3825 count -= ret;
3826 }
3827 else if (ret == 0) {
3828 goto read0;
3829 }
3830 else if (errno == EAGAIN || errno == ENOTCONN) {
3831 fd_cant_recv(conn->handle.fd);
3832 break;
3833 }
3834 else if (errno != EINTR) {
3835 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
3836 break;
3837 }
3838 }
3839
3840 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
3841 conn->flags &= ~CO_FL_WAIT_L4_CONN;
3842
3843 leave:
3844 return done;
3845
3846 read0:
3847 conn_sock_read0(conn);
3848 conn->flags &= ~CO_FL_WAIT_L4_CONN;
3849
3850 /* Now a final check for a possible asynchronous low-level error
3851 * report. This can happen when a connection receives a reset
3852 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
3853 * we might have come from there by just checking POLL_HUP instead
3854 * of recv()'s return value 0, so we have no way to tell there was
3855 * an error without checking.
3856 */
3857 if (unlikely(fdtab[conn->handle.fd].ev & FD_POLL_ERR))
3858 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
3859 goto leave;
3860}
3861
3862
3863/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
3864 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
3865 * other pending data for example, but this flag is ignored at the moment.
3866 * Only one call to send() is performed, unless the buffer wraps, in which case
3867 * a second call may be performed. The connection's flags are updated with
3868 * whatever special event is detected (error, empty). The caller is responsible
3869 * for taking care of those events and avoiding the call if inappropriate. The
3870 * function does not call the connection's polling update function, so the caller
3871 * is responsible for this. It's up to the caller to update the buffer's contents
3872 * based on the return value.
3873 */
3874static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
3875{
3876 ssize_t ret;
3877 size_t try, done;
3878 int send_flag;
3879
3880 if (!conn_ctrl_ready(conn))
3881 return 0;
3882
3883 if (!fd_send_ready(conn->handle.fd))
3884 return 0;
3885
3886 done = 0;
3887 /* send the largest possible block. For this we perform only one call
3888 * to send() unless the buffer wraps and we exactly fill the first hunk,
3889 * in which case we accept to do it once again.
3890 */
3891 while (count) {
3892 try = b_contig_data(buf, done);
3893 if (try > count)
3894 try = count;
3895
3896 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
3897 if (try < count || flags & CO_SFL_MSG_MORE)
3898 send_flag |= MSG_MORE;
3899
3900 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
3901 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
3902 if (ret > 0) {
3903 count -= ret;
3904 done += ret;
3905
3906 /* A send succeeded, so we can consier ourself connected */
3907 conn->flags |= CO_FL_WAIT_L4L6;
3908 /* if the system buffer is full, don't insist */
3909 if (ret < try)
3910 break;
3911 }
3912 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
3913 /* nothing written, we need to poll for write first */
3914 fd_cant_send(conn->handle.fd);
3915 break;
3916 }
3917 else if (errno != EINTR) {
3918 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
3919 break;
3920 }
3921 }
3922 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
3923 conn->flags &= ~CO_FL_WAIT_L4_CONN;
3924
3925 if (done > 0) {
3926 /* we count the total bytes sent, and the send rate for 32-byte
3927 * blocks. The reason for the latter is that freq_ctr are
3928 * limited to 4GB and that it's not enough per second.
3929 */
3930 _HA_ATOMIC_ADD(&global.out_bytes, done);
3931 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
3932 }
3933 return done;
3934}
3935
3936/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
3937 * connection with <xprt_ctx> as address of the xprt context.
3938 * Returns 1 if succeeded, 0 if not.
3939 */
3940static int qc_conn_init(struct connection *conn, void **xprt_ctx)
3941{
3942 struct quic_conn_ctx *ctx;
3943
3944 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
3945
3946 if (*xprt_ctx)
3947 goto out;
3948
3949 if (!conn_ctrl_ready(conn))
3950 goto out;
3951
3952 ctx = pool_alloc(pool_head_quic_conn_ctx);
3953 if (!ctx) {
3954 conn->err_code = CO_ER_SYS_MEMLIM;
3955 goto err;
3956 }
3957
3958 ctx->wait_event.tasklet = tasklet_new();
3959 if (!ctx->wait_event.tasklet) {
3960 conn->err_code = CO_ER_SYS_MEMLIM;
3961 goto err;
3962 }
3963
3964 ctx->wait_event.tasklet->process = quic_conn_io_cb;
3965 ctx->wait_event.tasklet->context = ctx;
3966 ctx->wait_event.events = 0;
3967 ctx->conn = conn;
3968 ctx->subs = NULL;
3969 ctx->xprt_ctx = NULL;
3970
3971 ctx->xprt = xprt_get(XPRT_QUIC);
3972 if (objt_server(conn->target)) {
3973 /* Server */
3974 struct server *srv = __objt_server(conn->target);
3975 unsigned char dcid[QUIC_CID_LEN];
3976 struct quic_conn *quic_conn;
3977 int ssl_err, ipv4;
3978
3979 ssl_err = SSL_ERROR_NONE;
3980 if (RAND_bytes(dcid, sizeof dcid) != 1)
3981 goto err;
3982
3983 conn->qc = new_quic_conn(QUIC_PROTOCOL_VERSION_DRAFT_28);
3984 if (!conn->qc)
3985 goto err;
3986
3987 quic_conn = conn->qc;
3988 quic_conn->conn = conn;
3989 ipv4 = conn->dst->ss_family == AF_INET;
3990 if (!qc_new_conn_init(quic_conn, ipv4, NULL, &srv->cids,
3991 dcid, sizeof dcid, NULL, 0))
3992 goto err;
3993
3994 if (!qc_new_isecs(conn, dcid, sizeof dcid, 0))
3995 goto err;
3996
3997 ctx->state = QUIC_HS_ST_CLIENT_INITIAL;
3998 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
3999 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4000 goto err;
4001
4002 quic_conn->params = srv->quic_params;
4003 /* Copy the initial source connection ID. */
4004 quic_cid_cpy(&quic_conn->params.initial_source_connection_id, &quic_conn->scid);
4005 quic_conn->enc_params_len =
4006 quic_transport_params_encode(quic_conn->enc_params,
4007 quic_conn->enc_params + sizeof quic_conn->enc_params,
4008 &quic_conn->params, 0);
4009 if (!quic_conn->enc_params_len)
4010 goto err;
4011
4012 SSL_set_quic_transport_params(ctx->ssl, quic_conn->enc_params, quic_conn->enc_params_len);
4013 SSL_set_connect_state(ctx->ssl);
4014 ssl_err = SSL_do_handshake(ctx->ssl);
4015 if (ssl_err != 1) {
4016 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4017 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
4018 TRACE_PROTO("SSL handshake",
4019 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
4020 }
4021 else {
4022 TRACE_DEVEL("SSL handshake error",
4023 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
4024 goto err;
4025 }
4026 }
4027 }
4028 else if (objt_listener(conn->target)) {
4029 /* Listener */
4030 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
4031
4032 ctx->state = QUIC_HS_ST_SERVER_INITIAL;
4033
4034 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4035 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4036 goto err;
4037
4038 SSL_set_accept_state(ctx->ssl);
4039 }
4040
4041 *xprt_ctx = ctx;
4042
4043 /* Leave init state and start handshake */
4044 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
4045 /* Start the handshake */
4046 tasklet_wakeup(ctx->wait_event.tasklet);
4047
4048 out:
4049 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4050
4051 return 0;
4052
4053 err:
4054 if (ctx->wait_event.tasklet)
4055 tasklet_free(ctx->wait_event.tasklet);
4056 pool_free(pool_head_quic_conn_ctx, ctx);
4057 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW|QUIC_EV_CONN_ENEW, conn);
4058 return -1;
4059}
4060
4061/* transport-layer operations for QUIC connections. */
4062static struct xprt_ops ssl_quic = {
4063 .snd_buf = quic_conn_from_buf,
4064 .rcv_buf = quic_conn_to_buf,
4065 .init = qc_conn_init,
4066 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4067 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4068 .name = "QUIC",
4069};
4070
4071__attribute__((constructor))
4072static void __quic_conn_init(void)
4073{
4074 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4075 xprt_register(XPRT_QUIC, &ssl_quic);
4076}
4077
4078__attribute__((destructor))
4079static void __quic_conn_deinit(void)
4080{
4081 BIO_meth_free(ha_quic_meth);
4082}
4083
4084/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4085 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4086 * calling <func> function;
4087 * Return the number of bytes read if succeded, -1 if not.
4088 */
4089static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4090 struct sockaddr_storage *saddr, qpkt_read_func *func)
4091{
4092 unsigned char *pos;
4093 const unsigned char *end;
4094 struct quic_dgram_ctx dgram_ctx = {
4095 .dcid_node = NULL,
4096 .owner = owner,
4097 };
4098
4099 pos = (unsigned char *)buf;
4100 end = pos + len;
4101
4102 do {
4103 int ret;
4104 struct quic_rx_packet *pkt;
4105
4106 pkt = pool_alloc(pool_head_quic_rx_packet);
4107 if (!pkt)
4108 goto err;
4109
4110 memset(pkt, 0, sizeof(*pkt));
4111 quic_rx_packet_refinc(pkt);
4112 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4113 if (ret == -1) {
4114 size_t pkt_len;
4115
4116 pkt_len = pkt->len;
4117 free_quic_rx_packet(pkt);
4118 /* If the packet length could not be found, we cannot continue. */
4119 if (!pkt_len)
4120 break;
4121 }
4122 } while (pos < end);
4123
4124 /* Increasing the received bytes counter by the UDP datagram length
4125 * if this datagram could be associated to a connection.
4126 */
4127 if (dgram_ctx.qc)
4128 dgram_ctx.qc->rx.bytes += len;
4129
4130 return pos - (unsigned char *)buf;
4131
4132 err:
4133 return -1;
4134}
4135
4136ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4137 struct sockaddr_storage *saddr)
4138{
4139 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4140}
4141
4142ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4143 struct sockaddr_storage *saddr)
4144{
4145 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4146}
4147
4148/* QUIC I/O handler for connection to local listeners or remove servers
4149 * depending on <listener> boolean value, with <fd> as socket file
4150 * descriptor and <ctx> as context.
4151 */
4152static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4153{
4154 ssize_t ret;
4155 size_t done = 0;
4156 struct buffer *buf = get_trash_chunk();
4157 /* Source address */
4158 struct sockaddr_storage saddr = {0};
4159 socklen_t saddrlen = sizeof saddr;
4160
4161 if (!fd_recv_ready(fd))
4162 return 0;
4163
4164 do {
4165 ret = recvfrom(fd, buf->area, buf->size, 0,
4166 (struct sockaddr *)&saddr, &saddrlen);
4167 if (ret < 0) {
4168 if (errno == EINTR)
4169 continue;
4170 if (errno == EAGAIN)
4171 fd_cant_recv(fd);
4172 goto out;
4173 }
4174 } while (0);
4175
4176 done = buf->data = ret;
4177 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4178
4179 out:
4180 return done;
4181}
4182
4183/* QUIC I/O handler for connections to local listeners with <fd> as socket
4184 * file descriptor.
4185 */
4186void quic_fd_handler(int fd)
4187{
4188 if (fdtab[fd].ev & FD_POLL_IN)
4189 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4190}
4191
4192/* QUIC I/O handler for connections to remote servers with <fd> as socket
4193 * file descriptor.
4194 */
4195void quic_conn_fd_handler(int fd)
4196{
4197 if (fdtab[fd].ev & FD_POLL_IN)
4198 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4199}
4200
4201/*
4202 * Local variables:
4203 * c-indent-level: 8
4204 * c-basic-offset: 8
4205 * End:
4206 */