blob: b7a191831a91f309817a849e815b96248bd48aec [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
25#include <haproxy/buf-t.h>
26#include <haproxy/compat.h>
27#include <haproxy/api.h>
28#include <haproxy/debug.h>
29#include <haproxy/tools.h>
30#include <haproxy/ticks.h>
31#include <haproxy/time.h>
32
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010038#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010039#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/pipe.h>
41#include <haproxy/proxy.h>
42#include <haproxy/quic_cc.h>
43#include <haproxy/quic_frame.h>
44#include <haproxy/quic_loss.h>
45#include <haproxy/quic_tls.h>
46#include <haproxy/ssl_sock.h>
47#include <haproxy/stream_interface.h>
48#include <haproxy/task.h>
49#include <haproxy/trace.h>
50#include <haproxy/xprt_quic.h>
51
52struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020053 .max_udp_payload_size = QUIC_DFLT_MAX_UDP_PAYLOAD_SIZE,
54 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
55 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010056};
57
58/* trace source and events */
59static void quic_trace(enum trace_level level, uint64_t mask, \
60 const struct trace_source *src,
61 const struct ist where, const struct ist func,
62 const void *a1, const void *a2, const void *a3, const void *a4);
63
64static const struct trace_event quic_trace_events[] = {
65 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
66 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
67 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
68 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
69 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
70 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
71 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050072 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010073 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
74 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
75 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
76 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
77 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
78 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
79 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
80 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
81 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
82 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
83 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
84 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
85 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
86 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
87 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
88 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
89 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
90 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
91 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010092 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
93 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
94 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
95 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
96 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
97 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +010098 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010099 { /* end */ }
100};
101
102static const struct name_desc quic_trace_lockon_args[4] = {
103 /* arg1 */ { /* already used by the connection */ },
104 /* arg2 */ { .name="quic", .desc="QUIC transport" },
105 /* arg3 */ { },
106 /* arg4 */ { }
107};
108
109static const struct name_desc quic_trace_decoding[] = {
110#define QUIC_VERB_CLEAN 1
111 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
112 { /* end */ }
113};
114
115
116struct trace_source trace_quic = {
117 .name = IST("quic"),
118 .desc = "QUIC xprt",
119 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
120 .default_cb = quic_trace,
121 .known_events = quic_trace_events,
122 .lockon_args = quic_trace_lockon_args,
123 .decoding = quic_trace_decoding,
124 .report_events = ~0, /* report everything by default */
125};
126
127#define TRACE_SOURCE &trace_quic
128INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
129
130static BIO_METHOD *ha_quic_meth;
131
132/* QUIC xprt connection context. */
133struct quic_conn_ctx {
134 struct connection *conn;
135 SSL *ssl;
136 BIO *bio;
137 int state;
138 const struct xprt_ops *xprt;
139 void *xprt_ctx;
140 struct wait_event wait_event;
141 struct wait_event *subs;
142};
143
144DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
145 "quic_conn_ctx_pool", sizeof(struct quic_conn_ctx));
146
147DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
148
149DECLARE_POOL(pool_head_quic_connection_id,
150 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
151
152DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
153
154DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
155
156DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
157
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100158DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
159
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_POOL(pool_head_quic_tx_frm, "quic_tx_frm_pool", sizeof(struct quic_tx_frm));
161
162DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
163
164DECLARE_STATIC_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
165
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100166DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100167
168static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type,
169 struct quic_enc_level *qel);
170
171int qc_prep_phdshk_pkts(struct quic_conn *qc);
172
173/* Add traces to <buf> depending on <frm> TX frame type. */
174static inline void chunk_tx_frm_appendf(struct buffer *buf,
175 const struct quic_tx_frm *frm)
176{
177 switch (frm->type) {
178 case QUIC_FT_CRYPTO:
179 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
180 (unsigned long long)frm->crypto.offset,
181 (unsigned long long)frm->crypto.len);
182 break;
183 default:
184 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
185 }
186}
187
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100188/* Only for debug purpose */
189struct enc_debug_info {
190 unsigned char *payload;
191 size_t payload_len;
192 unsigned char *aad;
193 size_t aad_len;
194 uint64_t pn;
195};
196
197/* Initializes a enc_debug_info struct (only for debug purpose) */
198static inline void enc_debug_info_init(struct enc_debug_info *edi,
199 unsigned char *payload, size_t payload_len,
200 unsigned char *aad, size_t aad_len, uint64_t pn)
201{
202 edi->payload = payload;
203 edi->payload_len = payload_len;
204 edi->aad = aad;
205 edi->aad_len = aad_len;
206 edi->pn = pn;
207}
208
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100209/* Trace callback for QUIC.
210 * These traces always expect that arg1, if non-null, is of type connection.
211 */
212static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
213 const struct ist where, const struct ist func,
214 const void *a1, const void *a2, const void *a3, const void *a4)
215{
216 const struct connection *conn = a1;
217
218 if (conn) {
219 struct quic_tls_secrets *secs;
220 struct quic_conn *qc;
221
222 qc = conn->qc;
223 chunk_appendf(&trace_buf, " : conn@%p", conn);
224 if ((mask & QUIC_EV_CONN_INIT) && qc) {
225 chunk_appendf(&trace_buf, "\n odcid");
226 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100227 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100228 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100229 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100230 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100231 }
232
233 if (mask & QUIC_EV_CONN_ADDDATA) {
234 const enum ssl_encryption_level_t *level = a2;
235 const size_t *len = a3;
236
237 if (level) {
238 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
239
240 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
241 }
242 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100243 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100244 }
245 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
246 /* Initial read & write secrets. */
247 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
248 const unsigned char *rx_sec = a2;
249 const unsigned char *tx_sec = a3;
250
251 secs = &qc->els[level].tls_ctx.rx;
252 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
253 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
254 if (rx_sec)
255 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
256 quic_tls_keys_hexdump(&trace_buf, secs);
257 }
258 secs = &qc->els[level].tls_ctx.tx;
259 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
260 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
261 if (tx_sec)
262 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
263 quic_tls_keys_hexdump(&trace_buf, secs);
264 }
265 }
266 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
267 const enum ssl_encryption_level_t *level = a2;
268 const unsigned char *secret = a3;
269 const size_t *secret_len = a4;
270
271 if (level) {
272 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
273
274 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
275 if (secret && secret_len)
276 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
277 secs = &qc->els[lvl].tls_ctx.rx;
278 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
279 quic_tls_keys_hexdump(&trace_buf, secs);
280 }
281 }
282
283 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
284 const enum ssl_encryption_level_t *level = a2;
285 const unsigned char *secret = a3;
286 const size_t *secret_len = a4;
287
288 if (level) {
289 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
290
291 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
292 if (secret && secret_len)
293 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
294 secs = &qc->els[lvl].tls_ctx.tx;
295 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
296 quic_tls_keys_hexdump(&trace_buf, secs);
297 }
298
299 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100301 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100302 const struct quic_tx_packet *pkt = a2;
303 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100304 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100305
306 if (qel) {
307 struct quic_pktns *pktns;
308
309 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100310 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
311 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100312 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100313 (unsigned long long)qc->path->cwnd,
314 (unsigned long long)qc->path->prep_in_flight,
315 (unsigned long long)qc->path->in_flight,
316 (unsigned long long)pktns->tx.in_flight,
317 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100318 }
319 if (pkt) {
320 const struct quic_tx_frm *frm;
321 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
322 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
323 list_for_each_entry(frm, &pkt->frms, list)
324 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100325 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
326 }
327
328 if (room) {
329 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
330 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
331 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100332 }
333 }
334
335 if (mask & QUIC_EV_CONN_HDSHK) {
336 const enum quic_handshake_state *state = a2;
337 const int *err = a3;
338
339 if (state)
340 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
341 if (err)
342 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
343 }
344
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100345 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100346 const struct quic_rx_packet *pkt = a2;
347 const unsigned long *pktlen = a3;
348 const SSL *ssl = a4;
349
350 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100351 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100352 pkt, quic_packet_type_enc_level_char(pkt->type));
353 if (pkt->pnl)
354 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
355 (unsigned long long)pkt->pn);
356 if (pkt->token_len)
357 chunk_appendf(&trace_buf, " toklen=%llu",
358 (unsigned long long)pkt->token_len);
359 if (pkt->aad_len)
360 chunk_appendf(&trace_buf, " aadlen=%llu",
361 (unsigned long long)pkt->aad_len);
362 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
363 pkt->flags, (unsigned long long)pkt->len);
364 }
365 if (pktlen)
366 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
367 if (ssl) {
368 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
369 chunk_appendf(&trace_buf, " el=%c",
370 quic_enc_level_char(ssl_to_quic_enc_level(level)));
371 }
372 }
373
374 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
375 const struct quic_rx_packet *pkt = a2;
376 const struct quic_rx_crypto_frm *cf = a3;
377 const SSL *ssl = a4;
378
379 if (pkt)
380 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
381 quic_packet_type_enc_level_char(pkt->type),
382 (unsigned long long)pkt->pn);
383 if (cf)
384 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
385 (unsigned long long)cf->offset_node.key,
386 (unsigned long long)cf->len);
387 if (ssl) {
388 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
389 chunk_appendf(&trace_buf, " el=%c",
390 quic_enc_level_char(ssl_to_quic_enc_level(level)));
391 }
392 }
393
394 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
395 const struct quic_frame *frm = a2;
396
397 if (frm)
398 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
399 }
400
401 if (mask & QUIC_EV_CONN_PHPKTS) {
402 const struct quic_enc_level *qel = a2;
403
404 if (qel) {
405 struct quic_pktns *pktns;
406
407 pktns = qc->pktns;
408 chunk_appendf(&trace_buf,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100409 " 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 +0100410 quic_enc_level_char_from_qel(qel, qc),
411 !!(pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100412 (unsigned long long)qc->path->cwnd,
413 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100414 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100415 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
416 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100417 }
418 }
419
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100420 if (mask & QUIC_EV_CONN_ENCPKT) {
421 const struct enc_debug_info *edi = a2;
422
423 if (edi)
424 chunk_appendf(&trace_buf,
425 " payload=@%p payload_len=%llu"
426 " aad=@%p aad_len=%llu pn=%llu",
427 edi->payload, (unsigned long long)edi->payload_len,
428 edi->aad, (unsigned long long)edi->aad_len,
429 (unsigned long long)edi->pn);
430 }
431
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100432 if (mask & QUIC_EV_CONN_RMHP) {
433 const struct quic_rx_packet *pkt = a2;
434
435 if (pkt) {
436 const int *ret = a3;
437
438 chunk_appendf(&trace_buf, " pkt@%p", pkt);
439 if (ret && *ret)
440 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
441 pkt->pnl, (unsigned long long)pkt->pn);
442 }
443 }
444
445 if (mask & QUIC_EV_CONN_PRSAFRM) {
446 const struct quic_tx_frm *frm = a2;
447 const unsigned long *val1 = a3;
448 const unsigned long *val2 = a4;
449
450 if (frm)
451 chunk_tx_frm_appendf(&trace_buf, frm);
452 if (val1)
453 chunk_appendf(&trace_buf, " %lu", *val1);
454 if (val2)
455 chunk_appendf(&trace_buf, "..%lu", *val2);
456 }
457
458 if (mask & QUIC_EV_CONN_RTTUPDT) {
459 const unsigned int *rtt_sample = a2;
460 const unsigned int *ack_delay = a3;
461 const struct quic_loss *ql = a4;
462
463 if (rtt_sample)
464 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
465 if (ack_delay)
466 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
467 if (ql)
468 chunk_appendf(&trace_buf,
469 " srtt=%ums rttvar=%ums min_rtt=%ums",
470 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
471 }
472 if (mask & QUIC_EV_CONN_CC) {
473 const struct quic_cc_event *ev = a2;
474 const struct quic_cc *cc = a3;
475
476 if (a2)
477 quic_cc_event_trace(&trace_buf, ev);
478 if (a3)
479 quic_cc_state_trace(&trace_buf, cc);
480 }
481
482 if (mask & QUIC_EV_CONN_PKTLOSS) {
483 const struct quic_pktns *pktns = a2;
484 const struct list *lost_pkts = a3;
485 struct quic_conn *qc = conn->qc;
486
487 if (pktns) {
488 chunk_appendf(&trace_buf, " pktns=%s",
489 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
490 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
491 if (pktns->tx.loss_time)
492 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100493 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494 }
495 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
496 struct quic_tx_packet *pkt;
497
498 chunk_appendf(&trace_buf, " lost_pkts:");
499 list_for_each_entry(pkt, lost_pkts, list)
500 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
501 }
502 }
503
504 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
505 struct quic_conn *qc = conn->qc;
506 const struct quic_pktns *pktns = a2;
507 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100508 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100509
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100510 if (ifae_pkts)
511 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
512 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100513 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100514 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100515 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100516 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
517 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100518 if (mask & QUIC_EV_CONN_STIMER) {
519 if (pktns->tx.loss_time)
520 chunk_appendf(&trace_buf, " loss_time=%dms",
521 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
522 }
523 if (mask & QUIC_EV_CONN_SPTO) {
524 if (pktns->tx.time_of_last_eliciting)
525 chunk_appendf(&trace_buf, " tole=%dms",
526 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
527 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100528 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100529 }
530 }
531
532 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
533 chunk_appendf(&trace_buf,
534 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
535 }
536 }
537
538 if (mask & QUIC_EV_CONN_SPPKTS) {
539 const struct quic_tx_packet *pkt = a2;
540
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100541 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
542 (unsigned long long)qc->path->cwnd,
543 (unsigned long long)qc->path->prep_in_flight,
544 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100545 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100546 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100547 (unsigned long)pkt->pn_node.key,
548 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
549 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100550 (unsigned long long)pkt->in_flight_len,
551 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100552 }
553 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100554
555 if (mask & QUIC_EV_CONN_SSLALERT) {
556 const uint8_t *alert = a2;
557 const enum ssl_encryption_level_t *level = a3;
558
559 if (alert)
560 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
561 if (level)
562 chunk_appendf(&trace_buf, " el=%c",
563 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
564 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100565
566 if (mask & QUIC_EV_CONN_BCFRMS) {
567 const size_t *sz1 = a2;
568 const size_t *sz2 = a3;
569 const size_t *sz3 = a4;
570
571 if (sz1)
572 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
573 if (sz2)
574 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
575 if (sz3)
576 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
577 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100578
579 if (mask & QUIC_EV_CONN_PSTRM) {
580 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100581
582 if (a2) {
583 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100584
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100585 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
586 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
587 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
588 (unsigned long long)s->id,
589 (unsigned long long)s->offset,
590 (unsigned long long)s->len);
591 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100592 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593 }
594 if (mask & QUIC_EV_CONN_LPKT) {
595 const struct quic_rx_packet *pkt = a2;
596
597 if (conn)
598 chunk_appendf(&trace_buf, " xprt_ctx@%p", conn->xprt_ctx);
599 if (pkt)
600 chunk_appendf(&trace_buf, " type=0x%02x %s",
601 pkt->type, qc_pkt_long(pkt) ? "long" : "short");
602 }
603
604}
605
606/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
607static inline int quic_peer_validated_addr(struct quic_conn_ctx *ctx)
608{
609 struct quic_conn *qc;
610
611 qc = ctx->conn->qc;
612 if (objt_server(qc->conn->target))
613 return 1;
614
615 if ((qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
616 (qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
617 (ctx->state & QUIC_HS_ST_COMPLETE))
618 return 1;
619
620 return 0;
621}
622
623/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
624 * both loss detection and PTO and schedule the task assiated to this timer if needed.
625 */
626static inline void qc_set_timer(struct quic_conn_ctx *ctx)
627{
628 struct quic_conn *qc;
629 struct quic_pktns *pktns;
630 unsigned int pto;
631
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100632 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
633 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100634 qc = ctx->conn->qc;
635 pktns = quic_loss_pktns(qc);
636 if (tick_isset(pktns->tx.loss_time)) {
637 qc->timer = pktns->tx.loss_time;
638 goto out;
639 }
640
641 /* XXX TODO: anti-amplification: the timer must be
642 * cancelled for a server which reached the anti-amplification limit.
643 */
644
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100645 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
646 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100647 /* Timer cancellation. */
648 qc->timer = TICK_ETERNITY;
649 goto out;
650 }
651
652 pktns = quic_pto_pktns(qc, ctx->state & QUIC_HS_ST_COMPLETE, &pto);
653 if (tick_isset(pto))
654 qc->timer = pto;
655 out:
656 task_schedule(qc->timer_task, qc->timer);
657 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
658}
659
660#ifndef OPENSSL_IS_BORINGSSL
661int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
662 const uint8_t *read_secret,
663 const uint8_t *write_secret, size_t secret_len)
664{
665 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
666 struct quic_tls_ctx *tls_ctx =
667 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
668 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
669
670 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
671 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
672 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
673 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
674
675 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
676 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
677 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
678 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
679 read_secret, secret_len)) {
680 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
681 return 0;
682 }
683
684 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
685 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
686 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
687 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
688 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
689 write_secret, secret_len)) {
690 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
691 return 0;
692 }
693
694 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
695 if (objt_server(conn->target) && level == ssl_encryption_application) {
696 const unsigned char *buf;
697 size_t buflen;
698
699 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
700 if (!buflen)
701 return 0;
702
703 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
704 return 0;
705 }
706 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
707
708 return 1;
709}
710#else
711/* ->set_read_secret callback to derive the RX secrets at <level> encryption
712 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500713 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100714 */
715int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
716 const SSL_CIPHER *cipher,
717 const uint8_t *secret, size_t secret_len)
718{
719 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
720 struct quic_tls_ctx *tls_ctx =
721 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
722
723 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
724 tls_ctx->rx.aead = tls_aead(cipher);
725 tls_ctx->rx.md = tls_md(cipher);
726 tls_ctx->rx.hp = tls_hp(cipher);
727
728 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
729 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
730 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
731 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
732 secret, secret_len)) {
733 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
734 goto err;
735 }
736
737 if (objt_server(conn->target) && level == ssl_encryption_application) {
738 const unsigned char *buf;
739 size_t buflen;
740
741 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
742 if (!buflen)
743 goto err;
744
745 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
746 goto err;
747 }
748
749 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
750 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
751
752 return 1;
753
754 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100755 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100756 return 0;
757}
758
759/* ->set_write_secret callback to derive the TX secrets at <level>
760 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500761 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100762 */
763int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
764 const SSL_CIPHER *cipher,
765 const uint8_t *secret, size_t secret_len)
766{
767 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
768 struct quic_tls_ctx *tls_ctx =
769 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
770
771 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
772 tls_ctx->tx.aead = tls_aead(cipher);
773 tls_ctx->tx.md = tls_md(cipher);
774 tls_ctx->tx.hp = tls_hp(cipher);
775
776 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
777 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
778 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
779 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
780 secret, secret_len)) {
781 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
782 goto err;
783 }
784
785 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
786 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
787
788 return 1;
789
790 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100791 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100792 return 0;
793}
794#endif
795
796/* This function copies the CRYPTO data provided by the TLS stack found at <data>
797 * with <len> as size in CRYPTO buffers dedicated to store the information about
798 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
799 * It fails only if it could not managed to allocate enough CRYPTO buffers to
800 * store all the data.
801 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
802 */
803static int quic_crypto_data_cpy(struct quic_enc_level *qel,
804 const unsigned char *data, size_t len)
805{
806 struct quic_crypto_buf **qcb;
807 /* The remaining byte to store in CRYPTO buffers. */
808 size_t cf_offset, cf_len, *nb_buf;
809 unsigned char *pos;
810
811 nb_buf = &qel->tx.crypto.nb_buf;
812 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
813 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
814 cf_len = len;
815
816 while (len) {
817 size_t to_copy, room;
818
819 pos = (*qcb)->data + (*qcb)->sz;
820 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
821 to_copy = len > room ? room : len;
822 if (to_copy) {
823 memcpy(pos, data, to_copy);
824 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
825 qel->tx.crypto.sz += to_copy;
826 (*qcb)->sz += to_copy;
827 pos += to_copy;
828 len -= to_copy;
829 data += to_copy;
830 }
831 else {
832 struct quic_crypto_buf **tmp;
833
834 tmp = realloc(qel->tx.crypto.bufs,
835 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
836 if (tmp) {
837 qel->tx.crypto.bufs = tmp;
838 qcb = &qel->tx.crypto.bufs[*nb_buf];
839 *qcb = pool_alloc(pool_head_quic_crypto_buf);
840 if (!*qcb)
841 return 0;
842
843 (*qcb)->sz = 0;
844 ++*nb_buf;
845 }
846 else {
847 break;
848 }
849 }
850 }
851
852 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
853 * have been buffered.
854 */
855 if (!len) {
856 struct quic_tx_frm *frm;
857
858 frm = pool_alloc(pool_head_quic_tx_frm);
859 if (!frm)
860 return 0;
861
862 frm->type = QUIC_FT_CRYPTO;
863 frm->crypto.offset = cf_offset;
864 frm->crypto.len = cf_len;
Willy Tarreau2b718102021-04-21 07:32:39 +0200865 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100866 }
867
868 return len == 0;
869}
870
871
872/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
873 * wants to provide the QUIC layer with CRYPTO data.
874 * Returns 1 if succeeded, 0 if not.
875 */
876int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
877 const uint8_t *data, size_t len)
878{
879 struct connection *conn;
880 enum quic_tls_enc_level tel;
881 struct quic_enc_level *qel;
882
883 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
884 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
885 tel = ssl_to_quic_enc_level(level);
886 qel = &conn->qc->els[tel];
887
888 if (tel == -1) {
889 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
890 goto err;
891 }
892
893 if (!quic_crypto_data_cpy(qel, data, len)) {
894 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
895 goto err;
896 }
897
898 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
899 conn, &level, &len);
900
901 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
902 return 1;
903
904 err:
905 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
906 return 0;
907}
908
909int ha_quic_flush_flight(SSL *ssl)
910{
911 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
912
913 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
914 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
915
916 return 1;
917}
918
919int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
920{
921 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
922
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100923 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100924 return 1;
925}
926
927/* QUIC TLS methods */
928static SSL_QUIC_METHOD ha_quic_method = {
929#ifdef OPENSSL_IS_BORINGSSL
930 .set_read_secret = ha_set_rsec,
931 .set_write_secret = ha_set_wsec,
932#else
933 .set_encryption_secrets = ha_quic_set_encryption_secrets,
934#endif
935 .add_handshake_data = ha_quic_add_handshake_data,
936 .flush_flight = ha_quic_flush_flight,
937 .send_alert = ha_quic_send_alert,
938};
939
940/* Initialize the TLS context of a listener with <bind_conf> as configuration.
941 * Returns an error count.
942 */
943int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
944{
945 struct proxy *curproxy = bind_conf->frontend;
946 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
947 int cfgerr = 0;
948
949#if 0
950 /* XXX Did not manage to use this. */
951 const char *ciphers =
952 "TLS_AES_128_GCM_SHA256:"
953 "TLS_AES_256_GCM_SHA384:"
954 "TLS_CHACHA20_POLY1305_SHA256:"
955 "TLS_AES_128_CCM_SHA256";
956#endif
957 const char *groups = "P-256:X25519:P-384:P-521";
958 long options =
959 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
960 SSL_OP_SINGLE_ECDH_USE |
961 SSL_OP_CIPHER_SERVER_PREFERENCE;
962 SSL_CTX *ctx;
963
964 ctx = SSL_CTX_new(TLS_server_method());
965 bind_conf->initial_ctx = ctx;
966
967 SSL_CTX_set_options(ctx, options);
968#if 0
969 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
970 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
971 "for bind '%s' at [%s:%d].\n",
972 curproxy->id, ciphers,
973 bind_conf->arg, bind_conf->file, bind_conf->line);
974 cfgerr++;
975 }
976#endif
977
978 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
979 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
980 "for bind '%s' at [%s:%d].\n",
981 curproxy->id, groups,
982 bind_conf->arg, bind_conf->file, bind_conf->line);
983 cfgerr++;
984 }
985
986 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
987 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
988 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
989 SSL_CTX_set_default_verify_paths(ctx);
990
991#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
992#ifdef OPENSSL_IS_BORINGSSL
993 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
994 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
995#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
996 if (bind_conf->ssl_conf.early_data) {
997 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
998 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
999 }
1000 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1001 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1002#else
1003 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1004#endif
1005 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1006#endif
1007 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1008
1009 return cfgerr;
1010}
1011
1012/* Decode an expected packet number from <truncated_on> its truncated value,
1013 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1014 * the number of bits used to encode this packet number (its length in bytes * 8).
1015 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1016 */
1017static uint64_t decode_packet_number(uint64_t largest_pn,
1018 uint32_t truncated_pn, unsigned int pn_nbits)
1019{
1020 uint64_t expected_pn = largest_pn + 1;
1021 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1022 uint64_t pn_hwin = pn_win / 2;
1023 uint64_t pn_mask = pn_win - 1;
1024 uint64_t candidate_pn;
1025
1026
1027 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1028 /* Note that <pn_win> > <pn_hwin>. */
1029 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1030 candidate_pn + pn_hwin <= expected_pn)
1031 return candidate_pn + pn_win;
1032
1033 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1034 return candidate_pn - pn_win;
1035
1036 return candidate_pn;
1037}
1038
1039/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1040 * cryptographic context.
1041 * <largest_pn> is the largest received packet number and <pn> the address of
1042 * the packet number field for this packet with <byte0> address of its first byte.
1043 * <end> points to one byte past the end of this packet.
1044 * Returns 1 if succeeded, 0 if not.
1045 */
1046static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1047 int64_t largest_pn, unsigned char *pn,
1048 unsigned char *byte0, const unsigned char *end,
1049 struct quic_conn_ctx *ctx)
1050{
1051 int ret, outlen, i, pnlen;
1052 uint64_t packet_number;
1053 uint32_t truncated_pn = 0;
1054 unsigned char mask[5] = {0};
1055 unsigned char *sample;
1056 EVP_CIPHER_CTX *cctx;
1057 unsigned char *hp_key;
1058
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001059 /* Check there is enough data in this packet. */
1060 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1061 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1062 return 0;
1063 }
1064
1065 cctx = EVP_CIPHER_CTX_new();
1066 if (!cctx) {
1067 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1068 return 0;
1069 }
1070
1071 ret = 0;
1072 sample = pn + QUIC_PACKET_PN_MAXLEN;
1073
1074 hp_key = tls_ctx->rx.hp_key;
1075 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1076 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1077 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1078 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1079 goto out;
1080 }
1081
1082 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1083 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1084 for (i = 0; i < pnlen; i++) {
1085 pn[i] ^= mask[i + 1];
1086 truncated_pn = (truncated_pn << 8) | pn[i];
1087 }
1088
1089 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1090 /* Store remaining information for this unprotected header */
1091 pkt->pn = packet_number;
1092 pkt->pnl = pnlen;
1093
1094 ret = 1;
1095
1096 out:
1097 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001098
1099 return ret;
1100}
1101
1102/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1103 * address, with <payload_len> as payload length, <aad> as address of
1104 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1105 * context.
1106 * Returns 1 if succeeded, 0 if not.
1107 */
1108static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1109 unsigned char *aad, size_t aad_len, uint64_t pn,
1110 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1111{
1112 unsigned char iv[12];
1113 unsigned char *tx_iv = tls_ctx->tx.iv;
1114 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001115 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001116
1117 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1118 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001119 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001120 }
1121
1122 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1123 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1124 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001125 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001126 }
1127
1128 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001129
1130 err:
1131 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1132 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1133 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001134}
1135
1136/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1137 * Returns 1 if succeeded, 0 if not.
1138 */
1139static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1140{
1141 int ret;
1142 unsigned char iv[12];
1143 unsigned char *rx_iv = tls_ctx->rx.iv;
1144 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1145
1146 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1147 return 0;
1148
1149 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1150 pkt->data, pkt->aad_len,
1151 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1152 if (!ret)
1153 return 0;
1154
1155 /* Update the packet length (required to parse the frames). */
1156 pkt->len = pkt->aad_len + ret;
1157
1158 return 1;
1159}
1160
1161/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
1162static inline void qc_treat_acked_tx_frm(struct quic_tx_frm *frm,
1163 struct quic_conn_ctx *ctx)
1164{
1165 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001166 LIST_DELETE(&frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001167 pool_free(pool_head_quic_tx_frm, frm);
1168}
1169
1170/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1171 * deallocating them, and their TX frames.
1172 * Returns the last node reached to be used for the next range.
1173 * May be NULL if <largest> node could not be found.
1174 */
1175static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1176 struct list *newly_acked_pkts,
1177 struct eb64_node *largest_node,
1178 uint64_t largest, uint64_t smallest,
1179 struct quic_conn_ctx *ctx)
1180{
1181 struct eb64_node *node;
1182 struct quic_tx_packet *pkt;
1183
1184 if (largest_node)
1185 node = largest_node;
1186 else {
1187 node = eb64_lookup(pkts, largest);
1188 while (!node && largest > smallest) {
1189 node = eb64_lookup(pkts, --largest);
1190 }
1191 }
1192
1193 while (node && node->key >= smallest) {
1194 struct quic_tx_frm *frm, *frmbak;
1195
1196 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1197 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001198 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001199 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1200 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1201 qc_treat_acked_tx_frm(frm, ctx);
1202 node = eb64_prev(node);
1203 eb64_delete(&pkt->pn_node);
1204 }
1205
1206 return node;
1207}
1208
1209/* Treat <frm> frame whose packet it is attached to has just been detected as non
1210 * acknowledged.
1211 */
1212static inline void qc_treat_nacked_tx_frm(struct quic_tx_frm *frm,
1213 struct quic_pktns *pktns,
1214 struct quic_conn_ctx *ctx)
1215{
1216 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001217 LIST_DELETE(&frm->list);
1218 LIST_INSERT(&pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001219}
1220
1221
1222/* Free the TX packets of <pkts> list */
1223static inline void free_quic_tx_pkts(struct list *pkts)
1224{
1225 struct quic_tx_packet *pkt, *tmp;
1226
1227 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001228 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001229 eb64_delete(&pkt->pn_node);
1230 pool_free(pool_head_quic_tx_packet, pkt);
1231 }
1232}
1233
1234/* Send a packet loss event nofification to the congestion controller
1235 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1236 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1237 * at <now_us> current time.
1238 * Always succeeds.
1239 */
1240static inline void qc_cc_loss_event(struct quic_conn *qc,
1241 unsigned int lost_bytes,
1242 unsigned int newest_time_sent,
1243 unsigned int period,
1244 unsigned int now_us)
1245{
1246 struct quic_cc_event ev = {
1247 .type = QUIC_CC_EVT_LOSS,
1248 .loss.now_ms = now_ms,
1249 .loss.max_ack_delay = qc->max_ack_delay,
1250 .loss.lost_bytes = lost_bytes,
1251 .loss.newest_time_sent = newest_time_sent,
1252 .loss.period = period,
1253 };
1254
1255 quic_cc_event(&qc->path->cc, &ev);
1256}
1257
1258/* Send a packet ack event nofication for each newly acked packet of
1259 * <newly_acked_pkts> list and free them.
1260 * Always succeeds.
1261 */
1262static inline void qc_treat_newly_acked_pkts(struct quic_conn_ctx *ctx,
1263 struct list *newly_acked_pkts)
1264{
1265 struct quic_conn *qc = ctx->conn->qc;
1266 struct quic_tx_packet *pkt, *tmp;
1267 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1268
1269 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1270 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001271 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001272 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001273 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001274 ev.ack.acked = pkt->in_flight_len;
1275 ev.ack.time_sent = pkt->time_sent;
1276 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001277 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001278 eb64_delete(&pkt->pn_node);
1279 pool_free(pool_head_quic_tx_packet, pkt);
1280 }
1281
1282}
1283
1284/* Handle <pkts> list of lost packets detected at <now_us> handling
1285 * their TX frames.
1286 * Send a packet loss event to the congestion controller if
1287 * in flight packet have been lost.
1288 * Also frees the packet in <pkts> list.
1289 * Never fails.
1290 */
1291static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
1292 struct quic_conn_ctx *ctx,
1293 struct list *pkts,
1294 uint64_t now_us)
1295{
1296 struct quic_conn *qc = ctx->conn->qc;
1297 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
1298 struct quic_tx_frm *frm, *frmbak;
1299 uint64_t lost_bytes;
1300
1301 lost_bytes = 0;
1302 oldest_lost = newest_lost = NULL;
1303 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1304 lost_bytes += pkt->in_flight_len;
1305 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001306 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001307 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001308 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001309 /* Treat the frames of this lost packet. */
1310 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1311 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001312 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001313 if (!oldest_lost) {
1314 oldest_lost = newest_lost = pkt;
1315 }
1316 else {
1317 if (newest_lost != oldest_lost)
1318 pool_free(pool_head_quic_tx_packet, newest_lost);
1319 newest_lost = pkt;
1320 }
1321 }
1322
1323 if (lost_bytes) {
1324 /* Sent a packet loss event to the congestion controller. */
1325 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1326 newest_lost->time_sent - oldest_lost->time_sent, now_us);
1327 pool_free(pool_head_quic_tx_packet, oldest_lost);
1328 if (newest_lost != oldest_lost)
1329 pool_free(pool_head_quic_tx_packet, newest_lost);
1330 }
1331}
1332
1333/* Look for packet loss from sent packets for <qel> encryption level of a
1334 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1335 * their tree if deemed as lost or set the <loss_time> value the packet number
1336 * space if any not deemed lost.
1337 * Should be called after having received an ACK frame with newly acknowledged
1338 * packets or when the the loss detection timer has expired.
1339 * Always succeeds.
1340 */
1341static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1342 struct quic_conn *qc,
1343 struct list *lost_pkts)
1344{
1345 struct eb_root *pkts;
1346 struct eb64_node *node;
1347 struct quic_loss *ql;
1348 unsigned int loss_delay;
1349
1350 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1351 pkts = &pktns->tx.pkts;
1352 pktns->tx.loss_time = TICK_ETERNITY;
1353 if (eb_is_empty(pkts))
1354 goto out;
1355
1356 ql = &qc->path->loss;
1357 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1358 loss_delay += loss_delay >> 3;
1359 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1360
1361 node = eb64_first(pkts);
1362 while (node) {
1363 struct quic_tx_packet *pkt;
1364 int64_t largest_acked_pn;
1365 unsigned int loss_time_limit, time_sent;
1366
1367 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1368 largest_acked_pn = pktns->tx.largest_acked_pn;
1369 node = eb64_next(node);
1370 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1371 break;
1372
1373 time_sent = pkt->time_sent;
1374 loss_time_limit = tick_add(time_sent, loss_delay);
1375 if (tick_is_le(time_sent, now_ms) ||
1376 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1377 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001378 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001379 }
1380 else {
1381 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1382 }
1383 }
1384
1385 out:
1386 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1387}
1388
1389/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1390 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001391 * if the largest acked packet was newly acked and if there was at least one newly
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001392 * acked ack-eliciting packet.
1393 * Return 1, if succeeded, 0 if not.
1394 */
1395static inline int qc_parse_ack_frm(struct quic_frame *frm, struct quic_conn_ctx *ctx,
1396 struct quic_enc_level *qel,
1397 unsigned int *rtt_sample,
1398 const unsigned char **pos, const unsigned char *end)
1399{
1400 struct quic_ack *ack = &frm->ack;
1401 uint64_t smallest, largest;
1402 struct eb_root *pkts;
1403 struct eb64_node *largest_node;
1404 unsigned int time_sent, pkt_flags;
1405 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1406 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1407
1408 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1409 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1410 ctx->conn,, &ack->largest_ack);
1411 goto err;
1412 }
1413
1414 if (ack->first_ack_range > ack->largest_ack) {
1415 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1416 ctx->conn,, &ack->first_ack_range);
1417 goto err;
1418 }
1419
1420 largest = ack->largest_ack;
1421 smallest = largest - ack->first_ack_range;
1422 pkts = &qel->pktns->tx.pkts;
1423 pkt_flags = 0;
1424 largest_node = NULL;
1425 time_sent = 0;
1426
1427 if ((int64_t)ack->largest_ack > qel->pktns->tx.largest_acked_pn) {
1428 largest_node = eb64_lookup(pkts, largest);
1429 if (!largest_node) {
1430 TRACE_DEVEL("Largest acked packet not found",
1431 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1432 goto err;
1433 }
1434
1435 time_sent = eb64_entry(&largest_node->node,
1436 struct quic_tx_packet, pn_node)->time_sent;
1437 }
1438
1439 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1440 ctx->conn,, &largest, &smallest);
1441 do {
1442 uint64_t gap, ack_range;
1443
1444 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1445 largest_node, largest, smallest, ctx);
1446 if (!ack->ack_range_num--)
1447 break;
1448
1449 if (!quic_dec_int(&gap, pos, end))
1450 goto err;
1451
1452 if (smallest < gap + 2) {
1453 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1454 ctx->conn,, &gap, &smallest);
1455 goto err;
1456 }
1457
1458 largest = smallest - gap - 2;
1459 if (!quic_dec_int(&ack_range, pos, end))
1460 goto err;
1461
1462 if (largest < ack_range) {
1463 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1464 ctx->conn,, &largest, &ack_range);
1465 goto err;
1466 }
1467
1468 /* Do not use this node anymore. */
1469 largest_node = NULL;
1470 /* Next range */
1471 smallest = largest - ack_range;
1472
1473 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1474 ctx->conn,, &largest, &smallest);
1475 } while (1);
1476
1477 /* Flag this packet number space as having received an ACK. */
1478 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_RECEIVED;
1479
1480 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1481 *rtt_sample = tick_remain(time_sent, now_ms);
1482 qel->pktns->tx.largest_acked_pn = ack->largest_ack;
1483 }
1484
1485 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1486 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1487 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1488 if (!LIST_ISEMPTY(&lost_pkts))
1489 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1490 }
1491 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1492 if (quic_peer_validated_addr(ctx))
1493 ctx->conn->qc->path->loss.pto_count = 0;
1494 qc_set_timer(ctx);
1495 }
1496
1497
1498 return 1;
1499
1500 err:
1501 free_quic_tx_pkts(&newly_acked_pkts);
1502 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1503 return 0;
1504}
1505
1506/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1507 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001508 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001509 * Return 1 if succeeded, 0 if not.
1510 */
1511static inline int qc_provide_cdata(struct quic_enc_level *el,
1512 struct quic_conn_ctx *ctx,
1513 const unsigned char *data, size_t len,
1514 struct quic_rx_packet *pkt,
1515 struct quic_rx_crypto_frm *cf)
1516{
1517 int ssl_err;
1518
1519 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1520 ssl_err = SSL_ERROR_NONE;
1521 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1522 TRACE_PROTO("SSL_provide_quic_data() error",
1523 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1524 goto err;
1525 }
1526
1527 el->rx.crypto.offset += len;
1528 TRACE_PROTO("in order CRYPTO data",
1529 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1530
1531 if (ctx->state < QUIC_HS_ST_COMPLETE) {
1532 ssl_err = SSL_do_handshake(ctx->ssl);
1533 if (ssl_err != 1) {
1534 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1535 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1536 TRACE_PROTO("SSL handshake",
1537 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
1538 goto out;
1539 }
1540
1541 TRACE_DEVEL("SSL handshake error",
1542 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
1543 goto err;
1544 }
1545
1546 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state);
1547 if (objt_listener(ctx->conn->target))
1548 ctx->state = QUIC_HS_ST_CONFIRMED;
1549 else
1550 ctx->state = QUIC_HS_ST_COMPLETE;
1551 } else {
1552 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1553 if (ssl_err != 1) {
1554 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1555 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1556 TRACE_DEVEL("SSL post handshake",
1557 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
1558 goto out;
1559 }
1560
1561 TRACE_DEVEL("SSL post handshake error",
1562 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
1563 goto err;
1564 }
1565
1566 TRACE_PROTO("SSL post handshake succeeded",
1567 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state);
1568 }
1569
1570 out:
1571 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1572 return 1;
1573
1574 err:
1575 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1576 return 0;
1577}
1578
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001579/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1580 * <pkt> RX packet.
1581 * Return it if succeeded, NULL if not.
1582 */
1583static inline
1584struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1585 struct quic_rx_packet *pkt)
1586{
1587 struct quic_rx_strm_frm *frm;
1588
1589 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1590 if (frm) {
1591 frm->offset_node.key = stream_frm->offset;
1592 frm->len = stream_frm->len;
1593 frm->data = stream_frm->data;
1594 frm->pkt = pkt;
1595 }
1596
1597 return frm;
1598}
1599
1600/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1601 * several streams, depending on the already open onces.
1602 * Return this node if succeeded, NULL if not.
1603 */
1604static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1605{
1606 unsigned int strm_type;
1607 int64_t sub_id;
1608 struct eb64_node *strm_node;
1609
1610 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1611
1612 strm_type = id & QCS_ID_TYPE_MASK;
1613 sub_id = id >> QCS_ID_TYPE_SHIFT;
1614 strm_node = NULL;
1615 if (qc_local_stream_id(qcc, id)) {
1616 /* Local streams: this stream must be already opened. */
1617 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1618 if (!strm_node) {
1619 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1620 goto out;
1621 }
1622 }
1623 else {
1624 /* Remote streams. */
1625 struct eb_root *strms;
1626 uint64_t largest_id;
1627 enum qcs_type qcs_type;
1628
1629 strms = &qcc->streams_by_id;
1630 qcs_type = qcs_id_type(id);
1631 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1632 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1633 goto out;
1634 }
1635
1636 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1637 * correct value.
1638 */
1639 largest_id = qcc->strms[qcs_type].largest_id;
1640 if (sub_id > (int64_t)largest_id) {
1641 /* RFC: "A stream ID that is used out of order results in all streams
1642 * of that type with lower-numbered stream IDs also being opened".
1643 * So, let's "open" these streams.
1644 */
1645 int64_t i;
1646 struct qcs *qcs;
1647
1648 qcs = NULL;
1649 for (i = largest_id + 1; i <= sub_id; i++) {
1650 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1651 if (!qcs) {
1652 TRACE_PROTO("Could not allocate a new stream",
1653 QUIC_EV_CONN_PSTRM, qcc->conn);
1654 goto out;
1655 }
1656
1657 qcc->strms[qcs_type].largest_id = i;
1658 }
1659 if (qcs)
1660 strm_node = &qcs->by_id;
1661 }
1662 else {
1663 strm_node = eb64_lookup(strms, id);
1664 }
1665 }
1666
1667 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1668 return strm_node;
1669
1670 out:
1671 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1672 return NULL;
1673}
1674
1675/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1676 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1677 * to reflect the data which have been consumed.
1678 */
1679static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1680{
1681 size_t ret;
1682
1683 ret = 0;
1684 while (strm_frm->len) {
1685 size_t try;
1686
1687 try = b_contig_space(buf);
1688 if (!try)
1689 break;
1690
1691 if (try > strm_frm->len)
1692 try = strm_frm->len;
1693 memcpy(b_tail(buf), strm_frm->data, try);
1694 strm_frm->len -= try;
1695 strm_frm->offset += try;
1696 b_add(buf, try);
1697 ret += try;
1698 }
1699
1700 return ret;
1701}
1702
1703/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1704 * streams may be open. The data are copied to the stream RX buffer if possible.
1705 * If not, the STREAM frame is stored to be treated again later.
1706 * We rely on the flow control so that not to store too much STREAM frames.
1707 * Return 1 if succeeded, 0 if not.
1708 */
1709static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1710 struct quic_stream *strm_frm,
1711 struct quic_conn *qc)
1712{
1713 struct qcs *strm;
1714 struct eb64_node *strm_node, *frm_node;
1715 struct quic_rx_strm_frm *frm;
1716
1717 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1718 if (!strm_node) {
1719 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1720 return 0;
1721 }
1722
1723 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1724 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1725 /* FIXME: handle the case where this frame overlap others */
1726 if (frm_node) {
1727 TRACE_PROTO("Already existing stream data",
1728 QUIC_EV_CONN_PSTRM, qc->conn);
1729 goto out;
1730 }
1731
1732 if (strm_frm->offset == strm->rx.offset) {
1733 int ret;
1734
1735 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1736 goto store_frm;
1737
1738 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1739 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1740 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1741 return 0;
1742 }
1743
1744 strm->rx.offset += ret;
1745 }
1746
1747 if (!strm_frm->len)
1748 goto out;
1749
1750 store_frm:
1751 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1752 if (!frm) {
1753 TRACE_PROTO("Could not alloc RX STREAM frame",
1754 QUIC_EV_CONN_PSTRM, qc->conn);
1755 return 0;
1756 }
1757
1758 eb64_insert(&strm->frms, &frm->offset_node);
1759 quic_rx_packet_refinc(pkt);
1760
1761 out:
1762 return 1;
1763}
1764
1765/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1766 * streams may be open. The data are copied to the stream RX buffer if possible.
1767 * If not, the STREAM frame is stored to be treated again later.
1768 * We rely on the flow control so that not to store too much STREAM frames.
1769 * Return 1 if succeeded, 0 if not.
1770 */
1771static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1772 struct quic_stream *strm_frm,
1773 struct quic_conn *qc)
1774{
1775 struct qcs *strm;
1776 struct eb64_node *strm_node, *frm_node;
1777 struct quic_rx_strm_frm *frm;
1778 size_t strm_frm_len;
1779
1780 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1781 if (!strm_node) {
1782 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1783 return 0;
1784 }
1785
1786 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1787 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1788 /* FIXME: handle the case where this frame overlap others */
1789 if (frm_node) {
1790 TRACE_PROTO("Already existing stream data",
1791 QUIC_EV_CONN_PSTRM, qc->conn);
1792 goto out;
1793 }
1794
1795 strm_frm_len = strm_frm->len;
1796 if (strm_frm->offset == strm->rx.offset) {
1797 int ret;
1798
1799 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1800 goto store_frm;
1801
1802 /* qc_strm_cpy() will modify the offset, depending on the number
1803 * of bytes copied.
1804 */
1805 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1806 /* Inform the application of the arrival of this new stream */
1807 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1808 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1809 return 0;
1810 }
1811
1812 if (ret)
1813 ruqs_notify_recv(strm);
1814
1815 strm_frm->offset += ret;
1816 }
1817 /* Take this frame into an account for the stream flow control */
1818 strm->rx.offset += strm_frm_len;
1819 /* It all the data were provided to the application, there is no need to
1820 * store any more inforamtion for it.
1821 */
1822 if (!strm_frm->len)
1823 goto out;
1824
1825 store_frm:
1826 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1827 if (!frm) {
1828 TRACE_PROTO("Could not alloc RX STREAM frame",
1829 QUIC_EV_CONN_PSTRM, qc->conn);
1830 return 0;
1831 }
1832
1833 eb64_insert(&strm->frms, &frm->offset_node);
1834 quic_rx_packet_refinc(pkt);
1835
1836 out:
1837 return 1;
1838}
1839
1840static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1841 struct quic_stream *strm_frm,
1842 struct quic_conn *qc)
1843{
1844 if (strm_frm->id & QCS_ID_DIR_BIT)
1845 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1846 else
1847 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1848}
1849
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001850/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1851 * as I/O handler context and <qel> as encryption level.
1852 * Returns 1 if succeeded, 0 if failed.
1853 */
1854static int qc_parse_pkt_frms(struct quic_rx_packet *pkt, struct quic_conn_ctx *ctx,
1855 struct quic_enc_level *qel)
1856{
1857 struct quic_frame frm;
1858 const unsigned char *pos, *end;
1859 struct quic_conn *conn = ctx->conn->qc;
1860
1861 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1862 /* Skip the AAD */
1863 pos = pkt->data + pkt->aad_len;
1864 end = pkt->data + pkt->len;
1865
1866 while (pos < end) {
1867 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1868 goto err;
1869
1870 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001871 case QUIC_FT_PADDING:
1872 if (pos != end) {
1873 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1874 goto err;
1875 }
1876 break;
1877 case QUIC_FT_PING:
1878 break;
1879 case QUIC_FT_ACK:
1880 {
1881 unsigned int rtt_sample;
1882
1883 rtt_sample = 0;
1884 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1885 goto err;
1886
1887 if (rtt_sample) {
1888 unsigned int ack_delay;
1889
1890 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1891 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1892 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1893 }
1894 tasklet_wakeup(ctx->wait_event.tasklet);
1895 break;
1896 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001897 case QUIC_FT_CRYPTO:
1898 if (frm.crypto.offset != qel->rx.crypto.offset) {
1899 struct quic_rx_crypto_frm *cf;
1900
1901 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1902 if (!cf) {
1903 TRACE_DEVEL("CRYPTO frame allocation failed",
1904 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1905 goto err;
1906 }
1907
1908 cf->offset_node.key = frm.crypto.offset;
1909 cf->len = frm.crypto.len;
1910 cf->data = frm.crypto.data;
1911 cf->pkt = pkt;
1912 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1913 quic_rx_packet_refinc(pkt);
1914 }
1915 else {
1916 /* XXX TO DO: <cf> is used only for the traces. */
1917 struct quic_rx_crypto_frm cf = { };
1918
1919 cf.offset_node.key = frm.crypto.offset;
1920 cf.len = frm.crypto.len;
1921 if (!qc_provide_cdata(qel, ctx,
1922 frm.crypto.data, frm.crypto.len,
1923 pkt, &cf))
1924 goto err;
1925 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001926 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001927 case QUIC_FT_STREAM_8:
1928 case QUIC_FT_STREAM_9:
1929 case QUIC_FT_STREAM_A:
1930 case QUIC_FT_STREAM_B:
1931 case QUIC_FT_STREAM_C:
1932 case QUIC_FT_STREAM_D:
1933 case QUIC_FT_STREAM_E:
1934 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001935 {
1936 struct quic_stream *stream = &frm.stream;
1937
1938 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1939 if (objt_listener(ctx->conn->target)) {
1940 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
1941 goto err;
1942 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
1943 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001944
1945 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
1946 goto err;
1947
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001948 break;
1949 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001950 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001951 break;
1952 case QUIC_FT_CONNECTION_CLOSE:
1953 case QUIC_FT_CONNECTION_CLOSE_APP:
1954 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001955 case QUIC_FT_HANDSHAKE_DONE:
1956 if (objt_listener(ctx->conn->target))
1957 goto err;
1958
1959 ctx->state = QUIC_HS_ST_CONFIRMED;
1960 break;
1961 default:
1962 goto err;
1963 }
1964 }
1965
1966 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
1967 * has successfully parse a Handshake packet. The Initial encryption must also
1968 * be discarded.
1969 */
1970 if (ctx->state == QUIC_HS_ST_SERVER_INITIAL &&
1971 pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) {
1972 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
1973 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
1974 qc_set_timer(ctx);
1975 ctx->state = QUIC_HS_ST_SERVER_HANDSHAKE;
1976 }
1977
1978 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1979 return 1;
1980
1981 err:
1982 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
1983 return 0;
1984}
1985
1986/* Prepare as much as possible handshake packets for the QUIC connection
1987 * with <ctx> as I/O handler context.
1988 * Returns 1 if succeeded, or 0 if something wrong happened.
1989 */
1990static int qc_prep_hdshk_pkts(struct quic_conn_ctx *ctx)
1991{
1992 struct quic_conn *qc;
1993 enum quic_tls_enc_level tel, next_tel;
1994 struct quic_enc_level *qel;
1995 struct q_buf *wbuf;
1996 /* A boolean to flag <wbuf> as reusable, even if not empty. */
1997 int reuse_wbuf;
1998
1999 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2000 qc = ctx->conn->qc;
2001 if (!quic_get_tls_enc_levels(&tel, &next_tel, ctx->state)) {
2002 TRACE_DEVEL("unknown enc. levels",
2003 QUIC_EV_CONN_PHPKTS, ctx->conn);
2004 goto err;
2005 }
2006
2007 reuse_wbuf = 0;
2008 wbuf = q_wbuf(qc);
2009 qel = &qc->els[tel];
Ilya Shipitsin01881082021-08-07 14:41:56 +05002010 /* When entering this function, the writer buffer must be empty.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002011 * Most of the time it points to the reader buffer.
2012 */
2013 while ((q_buf_empty(wbuf) || reuse_wbuf)) {
2014 ssize_t ret;
2015 enum quic_pkt_type pkt_type;
2016
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002017 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002018 /* Do not build any more packet f the TX secrets are not available or
2019 * f there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002020 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002021 * and if there is no more CRYPTO data available or in flight
2022 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002023 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002024 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2025 (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002026 !qc->tx.nb_pto_dgrams &&
2027 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002028 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002029 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
2030 /* Consume the buffer if we were supposed to reuse it. */
2031 if (reuse_wbuf)
2032 wbuf = q_next_wbuf(qc);
2033 break;
2034 }
2035
2036 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002037 ret = qc_build_hdshk_pkt(wbuf, qc, pkt_type, qel);
2038 switch (ret) {
2039 case -2:
2040 goto err;
2041 case -1:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002042 if (!reuse_wbuf)
2043 goto out;
2044
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002045 /* Not enough room in <wbuf>. */
2046 wbuf = q_next_wbuf(qc);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002047 reuse_wbuf = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002048 continue;
2049 case 0:
2050 goto out;
2051 default:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002052 reuse_wbuf = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002053 /* Discard the Initial encryption keys as soon as
2054 * a handshake packet could be built.
2055 */
2056 if (ctx->state == QUIC_HS_ST_CLIENT_INITIAL &&
2057 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2058 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2059 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2060 qc_set_timer(ctx);
2061 ctx->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
2062 }
2063 /* Special case for Initial packets: when they have all
2064 * been sent, select the next level.
2065 */
2066 if ((LIST_ISEMPTY(&qel->pktns->tx.frms) || qc->els[next_tel].pktns->tx.in_flight) &&
2067 tel == QUIC_TLS_ENC_LEVEL_INITIAL) {
2068 tel = next_tel;
2069 qel = &qc->els[tel];
2070 if (LIST_ISEMPTY(&qel->pktns->tx.frms)) {
2071 /* If there is no more data for the next level, let's
2072 * consume a buffer. This is the case for a client
2073 * which sends only one Initial packet, then wait
2074 * for additional CRYPTO data from the server to enter the
2075 * next level.
2076 */
2077 wbuf = q_next_wbuf(qc);
2078 }
2079 else {
2080 /* Let's try to reuse this buffer. */
2081 reuse_wbuf = 1;
2082 }
2083 }
2084 else {
2085 wbuf = q_next_wbuf(qc);
2086 }
2087 }
2088 }
2089
2090 out:
2091 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
2092 return 1;
2093
2094 err:
2095 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
2096 return 0;
2097}
2098
2099/* Send the QUIC packets which have been prepared for QUIC connections
2100 * with <ctx> as I/O handler context.
2101 */
2102int qc_send_ppkts(struct quic_conn_ctx *ctx)
2103{
2104 struct quic_conn *qc;
2105 struct buffer tmpbuf = { };
2106 struct q_buf *rbuf;
2107
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002108 qc = ctx->conn->qc;
2109 for (rbuf = q_rbuf(qc); !q_buf_empty(rbuf) ; rbuf = q_next_rbuf(qc)) {
2110 struct quic_tx_packet *p, *q;
2111 unsigned int time_sent;
2112
2113 tmpbuf.area = (char *)rbuf->area;
2114 tmpbuf.size = tmpbuf.data = rbuf->data;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002115 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002116 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
2117 &tmpbuf, tmpbuf.data, 0) <= 0)
2118 break;
2119
2120 qc->tx.bytes += tmpbuf.data;
2121 time_sent = now_ms;
2122 /* Reset this buffer to make it available for the next packet to prepare. */
2123 q_buf_reset(rbuf);
2124 /* Remove from <rbuf> the packets which have just been sent. */
2125 list_for_each_entry_safe(p, q, &rbuf->pkts, list) {
2126 p->time_sent = time_sent;
2127 if (p->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2128 p->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002129 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002130 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002131 qc->path->in_flight += p->in_flight_len;
2132 p->pktns->tx.in_flight += p->in_flight_len;
2133 if (p->in_flight_len)
2134 qc_set_timer(ctx);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002135 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, p);
Willy Tarreau2b718102021-04-21 07:32:39 +02002136 LIST_DELETE(&p->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002137 }
2138 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002139
2140 return 1;
2141}
2142
2143/* Build all the frames which must be sent just after the handshake have succeeded.
2144 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2145 * a HANDSHAKE_DONE frame.
2146 * Return 1 if succeeded, 0 if not.
2147 */
2148static int quic_build_post_handshake_frames(struct quic_conn *conn)
2149{
2150 int i;
2151 struct quic_frame *frm;
2152
2153 /* Only servers must send a HANDSHAKE_DONE frame. */
2154 if (!objt_server(conn->conn->target)) {
2155 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002156 if (!frm)
2157 return 0;
2158
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002159 frm->type = QUIC_FT_HANDSHAKE_DONE;
Willy Tarreau2b718102021-04-21 07:32:39 +02002160 LIST_APPEND(&conn->tx.frms_to_send, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002161 }
2162
Frédéric Lécaille5aa41432021-01-28 16:22:52 +01002163 for (i = 1; i < conn->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002164 struct quic_connection_id *cid;
2165
2166 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002167 cid = new_quic_cid(&conn->cids, i);
2168 if (!frm || !cid)
2169 goto err;
2170
2171 quic_connection_id_to_frm_cpy(frm, cid);
Willy Tarreau2b718102021-04-21 07:32:39 +02002172 LIST_APPEND(&conn->tx.frms_to_send, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002173 }
2174
2175 return 1;
2176
2177 err:
2178 free_quic_conn_cids(conn);
2179 return 0;
2180}
2181
2182/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002183void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002184{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002185 struct eb64_node *n;
2186 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002187
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002188 n = eb64_first(&arngs->root);
2189 while (n) {
2190 struct eb64_node *next;
2191
2192 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2193 next = eb64_next(n);
2194 eb64_delete(n);
2195 free(ar);
2196 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002197 }
2198}
2199
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002200/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2201 * descending order.
2202 */
2203static inline size_t sack_gap(struct quic_arng_node *p,
2204 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002205{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002206 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002207}
2208
2209
2210/* Remove the last elements of <ack_ranges> list of ack range updating its
2211 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002212 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002213 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002214static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002215{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002216 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002217
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002218 last = eb64_last(&arngs->root);
2219 while (last && arngs->enc_sz > limit) {
2220 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002221
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002222 prev = eb64_prev(last);
2223 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002224 return 0;
2225
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002226 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2227 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2228 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2229 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2230 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2231 --arngs->sz;
2232 eb64_delete(last);
2233 pool_free(pool_head_quic_arng, last);
2234 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002235 }
2236
2237 return 1;
2238}
2239
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002240/* Set the encoded size of <arngs> QUIC ack ranges. */
2241static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2242{
2243 struct eb64_node *node, *next;
2244 struct quic_arng_node *ar, *ar_next;
2245
2246 node = eb64_last(&arngs->root);
2247 if (!node)
2248 return;
2249
2250 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2251 arngs->enc_sz = quic_int_getsize(ar->last) +
2252 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2253
2254 while ((next = eb64_prev(node))) {
2255 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2256 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2257 quic_int_getsize(ar_next->last - ar_next->first.key);
2258 node = next;
2259 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2260 }
2261}
2262
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002263/* Insert <ar> ack range into <argns> tree of ack ranges.
2264 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002265 */
2266static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002267struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2268 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002269{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002270 struct quic_arng_node *new_ar;
2271
2272 new_ar = pool_alloc(pool_head_quic_arng);
2273 if (new_ar) {
2274 new_ar->first.key = ar->first;
2275 new_ar->last = ar->last;
2276 eb64_insert(&arngs->root, &new_ar->first);
2277 arngs->sz++;
2278 }
2279
2280 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002281}
2282
2283/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002284 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002285 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002286 *
2287 * Descending order
2288 * ------------->
2289 * range1 range2
2290 * ..........|--------|..............|--------|
2291 * ^ ^ ^ ^
2292 * | | | |
2293 * last1 first1 last2 first2
2294 * ..........+--------+--------------+--------+......
2295 * diff1 gap12 diff2
2296 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002297 * To encode the previous list of ranges we must encode integers as follows in
2298 * descending order:
2299 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002300 * with diff1 = last1 - first1
2301 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002302 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002303 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002304 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002305int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2306 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002307{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002308 struct eb64_node *le;
2309 struct quic_arng_node *new_node;
2310 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002311
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002312 new = NULL;
2313 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002314 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002315 if (!new_node)
2316 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002317
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002318 goto out;
2319 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002320
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002321 le = eb64_lookup_le(&arngs->root, ar->first);
2322 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002323 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002324 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002325 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002326 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002327 else {
2328 struct quic_arng_node *le_ar =
2329 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002330
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002331 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002332 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002333 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002334
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002335 if (le_ar->last + 1 >= ar->first) {
2336 le_ar->last = ar->last;
2337 new = le;
2338 new_node = le_ar;
2339 }
2340 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002341 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002342 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002343 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002344
2345 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002346 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002347 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002348
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002349 /* Verify that the new inserted node does not overlap the nodes
2350 * which follow it.
2351 */
2352 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002353 struct eb64_node *next;
2354 struct quic_arng_node *next_node;
2355
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002356 while ((next = eb64_next(new))) {
2357 next_node =
2358 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002359 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002360 break;
2361
2362 if (next_node->last > new_node->last)
2363 new_node->last = next_node->last;
2364 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002365 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002366 /* Decrement the size of these ranges. */
2367 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002368 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002369 }
2370
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002371 quic_arngs_set_enc_sz(arngs);
2372
2373 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002374 return 1;
2375}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002376/* Remove the header protection of packets at <el> encryption level.
2377 * Always succeeds.
2378 */
2379static inline void qc_rm_hp_pkts(struct quic_enc_level *el, struct quic_conn_ctx *ctx)
2380{
2381 struct quic_tls_ctx *tls_ctx;
2382 struct quic_rx_packet *pqpkt, *qqpkt;
2383 struct quic_enc_level *app_qel;
2384
2385 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2386 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2387 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
2388 if (el == app_qel && objt_listener(ctx->conn->target) && ctx->state < QUIC_HS_ST_COMPLETE) {
2389 TRACE_PROTO("hp not removed (handshake not completed)",
2390 QUIC_EV_CONN_ELRMHP, ctx->conn);
2391 goto out;
2392 }
2393 tls_ctx = &el->tls_ctx;
2394 list_for_each_entry_safe(pqpkt, qqpkt, &el->rx.pqpkts, list) {
2395 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2396 pqpkt->data + pqpkt->pn_offset,
2397 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2398 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2399 /* XXX TO DO XXX */
2400 }
2401 else {
2402 /* The AAD includes the packet number field */
2403 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2404 /* Store the packet into the tree of packets to decrypt. */
2405 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002406 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002407 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002408 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002409 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2410 }
2411 quic_rx_packet_list_del(pqpkt);
2412 }
2413
2414 out:
2415 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2416}
2417
2418/* Process all the CRYPTO frame at <el> encryption level.
2419 * Return 1 if succeeded, 0 if not.
2420 */
2421static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
2422 struct quic_conn_ctx *ctx)
2423{
2424 struct eb64_node *node;
2425
2426 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
2427 node = eb64_first(&el->rx.crypto.frms);
2428 while (node) {
2429 struct quic_rx_crypto_frm *cf;
2430
2431 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2432 if (cf->offset_node.key != el->rx.crypto.offset)
2433 break;
2434
2435 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2436 goto err;
2437
2438 node = eb64_next(node);
2439 quic_rx_packet_refdec(cf->pkt);
2440 eb64_delete(&cf->offset_node);
2441 pool_free(pool_head_quic_rx_crypto_frm, cf);
2442 }
2443
2444 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2445 return 1;
2446
2447 err:
2448 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2449 return 0;
2450}
2451
2452/* Process all the packets at <el> encryption level.
2453 * Return 1 if succeeded, 0 if not.
2454 */
2455int qc_treat_rx_pkts(struct quic_enc_level *el, struct quic_conn_ctx *ctx)
2456{
2457 struct quic_tls_ctx *tls_ctx;
2458 struct eb64_node *node;
2459
2460 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2461 tls_ctx = &el->tls_ctx;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002462 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002463 node = eb64_first(&el->rx.pkts);
2464 while (node) {
2465 struct quic_rx_packet *pkt;
2466
2467 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
2468 if (!qc_pkt_decrypt(pkt, tls_ctx)) {
2469 /* Drop the packet */
2470 TRACE_PROTO("packet decryption failed -> dropped",
2471 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2472 }
2473 else {
Frédéric Lécaillec4b93ea2021-06-04 10:12:43 +02002474 if (!qc_parse_pkt_frms(pkt, ctx, el)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002475 /* Drop the packet */
2476 TRACE_PROTO("packet parsing failed -> dropped",
2477 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2478 }
2479 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002480 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2481
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002482 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
2483 el->pktns->rx.nb_ack_eliciting++;
2484 if (!(el->pktns->rx.nb_ack_eliciting & 1))
2485 el->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
2486 }
2487
2488 /* Update the largest packet number. */
2489 if (pkt->pn > el->pktns->rx.largest_pn)
2490 el->pktns->rx.largest_pn = pkt->pn;
2491
2492 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille654c6912021-06-04 10:27:23 +02002493 if (!quic_update_ack_ranges_list(&el->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002494 TRACE_DEVEL("Could not update ack range list",
2495 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002496 }
2497 }
2498 node = eb64_next(node);
2499 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002500 }
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002501 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002502
2503 if (!qc_treat_rx_crypto_frms(el, ctx))
2504 goto err;
2505
2506 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2507 return 1;
2508
2509 err:
2510 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2511 return 0;
2512}
2513
2514/* Called during handshakes to parse and build Initial and Handshake packets for QUIC
2515 * connections with <ctx> as I/O handler context.
2516 * Returns 1 if succeeded, 0 if not.
2517 */
2518int qc_do_hdshk(struct quic_conn_ctx *ctx)
2519{
2520 int ssl_err;
2521 struct quic_conn *quic_conn;
2522 enum quic_tls_enc_level tel, next_tel;
2523 struct quic_enc_level *qel, *next_qel;
2524 struct quic_tls_ctx *tls_ctx;
2525
2526 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state);
2527
2528 ssl_err = SSL_ERROR_NONE;
2529 quic_conn = ctx->conn->qc;
2530 if (!quic_get_tls_enc_levels(&tel, &next_tel, ctx->state))
2531 goto err;
2532
2533 qel = &quic_conn->els[tel];
2534 next_qel = &quic_conn->els[next_tel];
2535
2536 next_level:
2537 tls_ctx = &qel->tls_ctx;
2538
2539 /* If the header protection key for this level has been derived,
2540 * remove the packet header protections.
2541 */
2542 if (!LIST_ISEMPTY(&qel->rx.pqpkts) &&
2543 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2544 qc_rm_hp_pkts(qel, ctx);
2545
2546 if (!eb_is_empty(&qel->rx.pkts) &&
2547 !qc_treat_rx_pkts(qel, ctx))
2548 goto err;
2549
2550 if (!qc_prep_hdshk_pkts(ctx))
2551 goto err;
2552
2553 if (!qc_send_ppkts(ctx))
2554 goto err;
2555
2556 /* Check if there is something to do for the next level.
2557 */
2558 if ((next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
2559 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
2560 qel = next_qel;
2561 goto next_level;
2562 }
2563
2564 /* If the handshake has not been completed -> out! */
2565 if (ctx->state < QUIC_HS_ST_COMPLETE)
2566 goto out;
2567
2568 /* Discard the Handshake keys. */
2569 quic_tls_discard_keys(&quic_conn->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2570 quic_pktns_discard(quic_conn->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, quic_conn);
2571 qc_set_timer(ctx);
2572 if (!quic_build_post_handshake_frames(quic_conn) ||
2573 !qc_prep_phdshk_pkts(quic_conn) ||
2574 !qc_send_ppkts(ctx))
2575 goto err;
2576
2577 out:
2578 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state);
2579 return 1;
2580
2581 err:
2582 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
2583 return 0;
2584}
2585
2586/* Allocate a new QUIC connection and return it if succeeded, NULL if not. */
2587struct quic_conn *new_quic_conn(uint32_t version)
2588{
2589 struct quic_conn *quic_conn;
2590
Willy Tarreaue4498932021-03-22 21:13:05 +01002591 quic_conn = pool_zalloc(pool_head_quic_conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002592 if (quic_conn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002593 quic_conn->version = version;
2594 }
2595
2596 return quic_conn;
2597}
2598
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002599/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002600static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2601{
2602 int i;
2603
2604 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2605 if (qel->tx.crypto.bufs[i]) {
2606 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2607 qel->tx.crypto.bufs[i] = NULL;
2608 }
2609 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002610 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002611}
2612
2613/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002614 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002615 * Returns 1 if succeeded, 0 if not.
2616 */
2617static int quic_conn_enc_level_init(struct quic_conn *qc,
2618 enum quic_tls_enc_level level)
2619{
2620 struct quic_enc_level *qel;
2621
2622 qel = &qc->els[level];
2623 qel->level = quic_to_ssl_enc_level(level);
2624 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2625 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2626 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2627 qel->tls_ctx.rx.flags = 0;
2628 qel->tls_ctx.tx.flags = 0;
2629
2630 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002631 HA_RWLOCK_INIT(&qel->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002632 LIST_INIT(&qel->rx.pqpkts);
2633
2634 /* Allocate only one buffer. */
2635 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2636 if (!qel->tx.crypto.bufs)
2637 goto err;
2638
2639 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2640 if (!qel->tx.crypto.bufs[0])
2641 goto err;
2642
2643 qel->tx.crypto.bufs[0]->sz = 0;
2644 qel->tx.crypto.nb_buf = 1;
2645
2646 qel->tx.crypto.sz = 0;
2647 qel->tx.crypto.offset = 0;
2648
2649 return 1;
2650
2651 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002652 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002653 return 0;
2654}
2655
2656/* Release the memory allocated for <buf> array of buffers, with <nb> as size.
2657 * Never fails.
2658 */
2659static inline void free_quic_conn_tx_bufs(struct q_buf **bufs, size_t nb)
2660{
2661 struct q_buf **p;
2662
2663 if (!bufs)
2664 return;
2665
2666 p = bufs;
2667 while (--nb) {
2668 if (!*p) {
2669 p++;
2670 continue;
2671 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002672 ha_free(&(*p)->area);
2673 ha_free(p);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002674 p++;
2675 }
2676 free(bufs);
2677}
2678
2679/* Allocate an array or <nb> buffers of <sz> bytes each.
2680 * Return this array if succeeded, NULL if failed.
2681 */
2682static inline struct q_buf **quic_conn_tx_bufs_alloc(size_t nb, size_t sz)
2683{
2684 int i;
2685 struct q_buf **bufs, **p;
2686
2687 bufs = calloc(nb, sizeof *bufs);
2688 if (!bufs)
2689 return NULL;
2690
2691 i = 0;
2692 p = bufs;
2693 while (i++ < nb) {
2694 *p = calloc(1, sizeof **p);
2695 if (!*p)
2696 goto err;
2697
2698 (*p)->area = malloc(sz);
2699 if (!(*p)->area)
2700 goto err;
2701
2702 (*p)->pos = (*p)->area;
2703 (*p)->end = (*p)->area + sz;
2704 (*p)->data = 0;
2705 LIST_INIT(&(*p)->pkts);
2706 p++;
2707 }
2708
2709 return bufs;
2710
2711 err:
2712 free_quic_conn_tx_bufs(bufs, nb);
2713 return NULL;
2714}
2715
2716/* Release all the memory allocated for <conn> QUIC connection. */
2717static void quic_conn_free(struct quic_conn *conn)
2718{
2719 int i;
2720
2721 free_quic_conn_cids(conn);
2722 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2723 quic_conn_enc_level_uninit(&conn->els[i]);
2724 free_quic_conn_tx_bufs(conn->tx.bufs, conn->tx.nb_buf);
2725 if (conn->timer_task)
2726 task_destroy(conn->timer_task);
2727 pool_free(pool_head_quic_conn, conn);
2728}
2729
2730/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002731static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002732{
2733 struct quic_conn_ctx *conn_ctx;
2734 struct quic_conn *qc;
2735 struct quic_pktns *pktns;
2736
2737
2738 conn_ctx = task->context;
2739 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002740 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2741 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002742 task->expire = TICK_ETERNITY;
2743 pktns = quic_loss_pktns(qc);
2744 if (tick_isset(pktns->tx.loss_time)) {
2745 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2746
2747 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2748 if (!LIST_ISEMPTY(&lost_pkts))
2749 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2750 qc_set_timer(conn_ctx);
2751 goto out;
2752 }
2753
2754 if (qc->path->in_flight) {
2755 pktns = quic_pto_pktns(qc, conn_ctx->state >= QUIC_HS_ST_COMPLETE, NULL);
2756 pktns->tx.pto_probe = 1;
2757 }
2758 else if (objt_server(qc->conn->target) && conn_ctx->state <= QUIC_HS_ST_COMPLETE) {
2759 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2760 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2761
2762 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2763 hel->pktns->tx.pto_probe = 1;
2764 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2765 iel->pktns->tx.pto_probe = 1;
2766 }
2767 qc->tx.nb_pto_dgrams = QUIC_MAX_NB_PTO_DGRAMS;
2768 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2769 qc->path->loss.pto_count++;
2770
2771 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002772 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002773
2774 return task;
2775}
2776
2777/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2778 * connections used to identify the first Initial packets of client connecting
2779 * to listeners. This parameter must be NULL for QUIC connections attached
2780 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2781 * <scid> is the source connection ID with <scid_len> as length.
2782 * Returns 1 if succeeded, 0 if not.
2783 */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002784static int qc_new_conn_init(struct quic_conn *qc, int ipv4,
2785 struct eb_root *quic_initial_clients,
2786 struct eb_root *quic_clients,
2787 unsigned char *dcid, size_t dcid_len,
2788 unsigned char *scid, size_t scid_len, int server)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002789{
2790 int i;
2791 /* Initial CID. */
2792 struct quic_connection_id *icid;
2793
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002794 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002795 qc->cids = EB_ROOT;
2796 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002797 if (server) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002798 /* Copy the initial DCID. */
2799 qc->odcid.len = dcid_len;
2800 if (qc->odcid.len)
2801 memcpy(qc->odcid.data, dcid, dcid_len);
2802
2803 /* Copy the SCID as our DCID for this connection. */
2804 if (scid_len)
2805 memcpy(qc->dcid.data, scid, scid_len);
2806 qc->dcid.len = scid_len;
2807 }
2808 /* QUIC Client (outgoing connection to servers) */
2809 else {
2810 if (dcid_len)
2811 memcpy(qc->dcid.data, dcid, dcid_len);
2812 qc->dcid.len = dcid_len;
2813 }
2814
2815 /* Initialize the output buffer */
2816 qc->obuf.pos = qc->obuf.data;
2817
2818 icid = new_quic_cid(&qc->cids, 0);
2819 if (!icid)
2820 return 0;
2821
2822 /* Select our SCID which is the first CID with 0 as sequence number. */
2823 qc->scid = icid->cid;
2824
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002825 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002826 if (server)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002827 ebmb_insert(quic_initial_clients, &qc->odcid_node, qc->odcid.len);
2828
2829 /* Insert our SCID, the connection ID for the QUIC client. */
2830 ebmb_insert(quic_clients, &qc->scid_node, qc->scid.len);
2831
2832 /* Packet number spaces initialization. */
2833 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2834 quic_pktns_init(&qc->pktns[i]);
2835 /* QUIC encryption level context initialization. */
2836 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
2837 if (!quic_conn_enc_level_init(qc, i))
2838 goto err;
2839 /* Initialize the packet number space. */
2840 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2841 }
2842
2843 /* TX part. */
2844 LIST_INIT(&qc->tx.frms_to_send);
2845 qc->tx.bufs = quic_conn_tx_bufs_alloc(QUIC_CONN_TX_BUFS_NB, QUIC_CONN_TX_BUF_SZ);
2846 if (!qc->tx.bufs)
2847 goto err;
2848
2849 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2850 qc->tx.wbuf = qc->tx.rbuf = 0;
2851 qc->tx.bytes = 0;
2852 qc->tx.nb_pto_dgrams = 0;
2853 /* RX part. */
2854 qc->rx.bytes = 0;
2855
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002856 /* XXX TO DO: Only one path at this time. */
2857 qc->path = &qc->paths[0];
2858 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2859
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002860 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002861
2862 return 1;
2863
2864 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002865 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002866 quic_conn_free(qc);
2867 return 0;
2868}
2869
2870/* Initialize the timer task of <qc> QUIC connection.
2871 * Returns 1 if succeeded, 0 if not.
2872 */
2873static int quic_conn_init_timer(struct quic_conn *qc)
2874{
2875 qc->timer_task = task_new(MAX_THREADS_MASK);
2876 if (!qc->timer_task)
2877 return 0;
2878
2879 qc->timer = TICK_ETERNITY;
2880 qc->timer_task->process = process_timer;
2881 qc->timer_task->context = qc->conn->xprt_ctx;
2882
2883 return 1;
2884}
2885
2886/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
2887 * past one byte of this buffer.
2888 */
2889static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
2890 struct quic_rx_packet *pkt)
2891{
2892 unsigned char dcid_len, scid_len;
2893
2894 /* Version */
2895 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
2896 return 0;
2897
2898 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
2899
2900 /* Destination Connection ID Length */
2901 dcid_len = *(*buf)++;
2902 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
2903 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
2904 /* XXX MUST BE DROPPED */
2905 return 0;
2906
2907 if (dcid_len) {
2908 /* Check that the length of this received DCID matches the CID lengths
2909 * of our implementation for non Initials packets only.
2910 */
2911 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
2912 return 0;
2913
2914 memcpy(pkt->dcid.data, *buf, dcid_len);
2915 }
2916
2917 pkt->dcid.len = dcid_len;
2918 *buf += dcid_len;
2919
2920 /* Source Connection ID Length */
2921 scid_len = *(*buf)++;
2922 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
2923 /* XXX MUST BE DROPPED */
2924 return 0;
2925
2926 if (scid_len)
2927 memcpy(pkt->scid.data, *buf, scid_len);
2928 pkt->scid.len = scid_len;
2929 *buf += scid_len;
2930
2931 return 1;
2932}
2933
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002934/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
2935 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
2936 * encryption level matching with the packet type. <*qel> may be null if not found.
2937 * Note that <ctx> may be null (for Initial packets).
2938 */
2939static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
2940 struct quic_conn *qc, struct quic_conn_ctx *ctx,
2941 struct quic_enc_level **qel)
2942{
2943 enum quic_tls_enc_level tel;
2944
2945 /* Special case without connection context (firt Initial packets) */
2946 if (!ctx) {
2947 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2948 return 1;
2949 }
2950
2951 tel = quic_packet_type_enc_level(pkt->type);
2952 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
2953 *qel = NULL;
2954 return 0;
2955 }
2956
2957 *qel = &qc->els[tel];
2958 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
2959 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
2960 return 0;
2961 }
2962
2963 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
2964 (tel != QUIC_TLS_ENC_LEVEL_APP || ctx->state >= QUIC_HS_ST_COMPLETE))
2965 return 1;
2966
2967 return 0;
2968}
2969
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002970/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
2971 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
2972 * byte past the end of the buffer containing this packet and <beg> the address of
2973 * the packet first byte.
2974 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
2975 * Returns 1 if succeeded, 0 if not.
2976 */
2977static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
2978 unsigned char **buf, unsigned char *beg,
2979 const unsigned char *end,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002980 struct quic_conn *qc, struct quic_conn_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002981{
2982 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002983 struct quic_enc_level *qel;
2984 /* Only for traces. */
2985 struct quic_rx_packet *qpkt_trace;
2986
2987 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002988 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002989 /* The packet number is here. This is also the start minus
2990 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
2991 * protection.
2992 */
2993 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002994 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002995 /* Note that the following function enables us to unprotect the packet
2996 * number and its length subsequently used to decrypt the entire
2997 * packets.
2998 */
2999 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3000 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003001 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003002 goto err;
3003 }
3004
3005 /* The AAD includes the packet number field found at <pn>. */
3006 pkt->aad_len = pn - beg + pkt->pnl;
3007 qpkt_trace = pkt;
3008 /* Store the packet */
3009 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02003010 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003011 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02003012 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003013 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003014 else if (qel) {
3015 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003016 pkt->pn_offset = pn - beg;
3017 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3018 }
3019
3020 memcpy(pkt->data, beg, pkt->len);
3021 /* Updtate the offset of <*buf> for the next QUIC packet. */
3022 *buf = beg + pkt->len;
3023
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003024 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025 return 1;
3026
3027 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003028 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003029 return 0;
3030}
3031
3032/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3033 * Also set <*long_header> to 1 if this form is long, 0 if not.
3034 */
3035static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3036 unsigned char byte0, int *long_header)
3037{
3038 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3039 pkt->type =
3040 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3041 *long_header = 1;
3042 }
3043 else {
3044 pkt->type = QUIC_PACKET_TYPE_SHORT;
3045 *long_header = 0;
3046 }
3047}
3048
3049static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3050 struct quic_rx_packet *pkt,
3051 struct quic_dgram_ctx *dgram_ctx,
3052 struct sockaddr_storage *saddr)
3053{
3054 unsigned char *beg;
3055 uint64_t len;
3056 struct quic_conn *qc;
3057 struct eb_root *cids;
3058 struct ebmb_node *node;
3059 struct connection *srv_conn;
3060 struct quic_conn_ctx *conn_ctx;
3061 int long_header;
3062
3063 qc = NULL;
3064 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3065 if (end <= *buf)
3066 goto err;
3067
3068 /* Fixed bit */
3069 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3070 /* XXX TO BE DISCARDED */
3071 goto err;
3072
3073 srv_conn = dgram_ctx->owner;
3074 beg = *buf;
3075 /* Header form */
3076 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3077 if (long_header) {
3078 size_t cid_lookup_len;
3079
3080 if (!quic_packet_read_long_header(buf, end, pkt))
3081 goto err;
3082
3083 /* For Initial packets, and for servers (QUIC clients connections),
3084 * there is no Initial connection IDs storage.
3085 */
3086 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3087 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3088 cid_lookup_len = pkt->dcid.len;
3089 }
3090 else {
3091 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3092 cid_lookup_len = QUIC_CID_LEN;
3093 }
3094
3095 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3096 if (!node)
3097 goto err;
3098
3099 qc = ebmb_entry(node, struct quic_conn, scid_node);
3100
3101 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3102 qc->dcid.len = pkt->scid.len;
3103 if (pkt->scid.len)
3104 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3105 }
3106
3107 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3108 uint64_t token_len;
3109
3110 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3111 goto err;
3112
3113 /* XXX TO DO XXX 0 value means "the token is not present".
3114 * A server which sends an Initial packet must not set the token.
3115 * So, a client which receives an Initial packet with a token
3116 * MUST discard the packet or generate a connection error with
3117 * PROTOCOL_VIOLATION as type.
3118 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3119 */
3120 pkt->token_len = token_len;
3121 }
3122 }
3123 else {
3124 /* XXX TO DO: Short header XXX */
3125 if (end - *buf < QUIC_CID_LEN)
3126 goto err;
3127
3128 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3129 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3130 if (!node)
3131 goto err;
3132
3133 qc = ebmb_entry(node, struct quic_conn, scid_node);
3134 *buf += QUIC_CID_LEN;
3135 }
3136 /* Store the DCID used for this packet to check the packet which
3137 * come in this UDP datagram match with it.
3138 */
3139 if (!dgram_ctx->dcid_node)
3140 dgram_ctx->dcid_node = node;
3141 /* Only packets packets with long headers and not RETRY or VERSION as type
3142 * have a length field.
3143 */
3144 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3145 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3146 goto err;
3147
3148 pkt->len = len;
3149 }
3150 else if (!long_header) {
3151 /* A short packet is the last one of an UDP datagram. */
3152 pkt->len = end - *buf;
3153 }
3154
3155 conn_ctx = qc->conn->xprt_ctx;
3156
3157 /* Increase the total length of this packet by the header length. */
3158 pkt->len += *buf - beg;
3159 /* Do not check the DCID node before the length. */
3160 if (dgram_ctx->dcid_node != node) {
3161 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3162 goto err;
3163 }
3164
3165 if (pkt->len > sizeof pkt->data) {
3166 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3167 goto err;
3168 }
3169
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003170 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003171 goto err;
3172
3173 /* Wake the tasklet of the QUIC connection packet handler. */
3174 if (conn_ctx)
3175 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3176
3177 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3178
3179 return pkt->len;
3180
3181 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003182 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003183 return -1;
3184}
3185
3186static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3187 struct quic_rx_packet *pkt,
3188 struct quic_dgram_ctx *dgram_ctx,
3189 struct sockaddr_storage *saddr)
3190{
3191 unsigned char *beg;
3192 uint64_t len;
3193 struct quic_conn *qc;
3194 struct eb_root *cids;
3195 struct ebmb_node *node;
3196 struct listener *l;
3197 struct quic_conn_ctx *conn_ctx;
3198 int long_header = 0;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003199 /* boolean to denote if a connection exists for this packet.
3200 * This does not mean there is an xprt context for it.
3201 */
3202 int found_conn = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003203
3204 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003205 conn_ctx = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003206 TRACE_ENTER(QUIC_EV_CONN_LPKT);
3207 if (end <= *buf)
3208 goto err;
3209
3210 /* Fixed bit */
3211 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3212 /* XXX TO BE DISCARDED */
3213 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3214 goto err;
3215 }
3216
3217 l = dgram_ctx->owner;
3218 beg = *buf;
3219 /* Header form */
3220 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3221 if (long_header) {
3222 unsigned char dcid_len;
3223
3224 if (!quic_packet_read_long_header(buf, end, pkt)) {
3225 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3226 goto err;
3227 }
3228
3229 dcid_len = pkt->dcid.len;
3230 /* For Initial packets, and for servers (QUIC clients connections),
3231 * there is no Initial connection IDs storage.
3232 */
3233 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3234 /* DCIDs of first packets coming from clients may have the same values.
3235 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3236 */
3237 quic_cid_saddr_cat(&pkt->dcid, saddr);
3238 cids = &l->rx.odcids;
3239 }
3240 else {
3241 if (pkt->dcid.len != QUIC_CID_LEN) {
3242 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3243 goto err;
3244 }
3245
3246 cids = &l->rx.cids;
3247 }
3248
3249 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3250 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3251 cids == &l->rx.odcids) {
3252 /* Switch to the definitive tree ->cids containing the final CIDs. */
3253 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3254 if (node) {
3255 /* If found, signal this with NULL as special value for <cids>. */
3256 pkt->dcid.len = dcid_len;
3257 cids = NULL;
3258 }
3259 }
3260
3261 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003262 int ipv4;
3263 struct quic_cid *odcid;
3264
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003265 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3266 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3267 goto err;
3268 }
3269
3270 qc = new_quic_conn(pkt->version);
3271 if (!qc) {
3272 TRACE_PROTO("Non allocated new connection", QUIC_EV_CONN_LPKT);
3273 goto err;
3274 }
3275
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003276 pkt->saddr = *saddr;
3277 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3278 * of <saddr>.
3279 */
3280 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003281 ipv4 = saddr->ss_family == AF_INET;
3282 if (!qc_new_conn_init(qc, ipv4, &l->rx.odcids, &l->rx.cids,
3283 pkt->dcid.data, pkt->dcid.len,
3284 pkt->scid.data, pkt->scid.len, 1))
3285 goto err;
3286
3287 odcid = &qc->rx.params.original_destination_connection_id;
3288 /* Copy the transport parameters. */
3289 qc->rx.params = l->bind_conf->quic_params;
3290 /* Copy original_destination_connection_id transport parameter. */
3291 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3292 odcid->len = pkt->odcid_len;
3293 /* Copy the initial source connection ID. */
3294 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3295 qc->enc_params_len =
3296 quic_transport_params_encode(qc->enc_params,
3297 qc->enc_params + sizeof qc->enc_params,
3298 &qc->rx.params, 1);
3299 if (!qc->enc_params_len)
3300 goto err;
3301
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003302 /* NOTE: the socket address has been concatenated to the destination ID
3303 * chosen by the client for Initial packets.
3304 */
3305 if (!qc_new_isecs(qc, pkt->dcid.data, pkt->odcid_len, 1)) {
3306 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3307 goto err;
3308 }
3309
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003310 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003311 /* This is the DCID node sent in this packet by the client. */
3312 node = &qc->odcid_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003313 }
3314 else {
3315 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3316 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3317 else
3318 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003319 conn_ctx = qc->conn->xprt_ctx;
3320 found_conn = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321 }
3322
3323 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3324 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003325
3326 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3327 end - *buf < token_len) {
3328 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3329 goto err;
3330 }
3331
3332 /* XXX TO DO XXX 0 value means "the token is not present".
3333 * A server which sends an Initial packet must not set the token.
3334 * So, a client which receives an Initial packet with a token
3335 * MUST discard the packet or generate a connection error with
3336 * PROTOCOL_VIOLATION as type.
3337 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3338 */
3339 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003340 }
3341 }
3342 else {
3343 if (end - *buf < QUIC_CID_LEN) {
3344 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3345 goto err;
3346 }
3347
3348 cids = &l->rx.cids;
3349 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3350 if (!node) {
3351 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3352 goto err;
3353 }
3354
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003355 found_conn = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003356 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003357 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003358 *buf += QUIC_CID_LEN;
3359 }
3360
3361 /* Store the DCID used for this packet to check the packet which
3362 * come in this UDP datagram match with it.
3363 */
3364 if (!dgram_ctx->dcid_node) {
3365 dgram_ctx->dcid_node = node;
3366 dgram_ctx->qc = qc;
3367 }
3368
3369 /* Only packets packets with long headers and not RETRY or VERSION as type
3370 * have a length field.
3371 */
3372 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3373 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3374 end - *buf < len) {
3375 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3376 goto err;
3377 }
3378
3379 pkt->len = len;
3380 }
3381 else if (!long_header) {
3382 /* A short packet is the last one of an UDP datagram. */
3383 pkt->len = end - *buf;
3384 }
3385
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003386 /* Increase the total length of this packet by the header length. */
3387 pkt->len += *buf - beg;
3388 /* Do not check the DCID node before the length. */
3389 if (dgram_ctx->dcid_node != node) {
3390 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3391 goto err;
3392 }
3393
3394 if (pkt->len > sizeof pkt->data) {
3395 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3396 goto err;
3397 }
3398
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003399 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3400 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3401 goto err;
3402 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003403
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003404 if (conn_ctx) {
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003405 /* Wake the tasklet of the QUIC connection packet handler. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003406 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003407 }
3408 else if (!found_conn) {
3409 /* Enqueue this packet. */
3410 LIST_APPEND(&l->rx.qpkts, &pkt->rx_list);
3411 /* Try to accept a new connection. */
3412 listener_accept(l);
3413 }
3414
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003415 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3416
3417 return pkt->len;
3418
3419 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003420 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003421 qc ? qc->conn : NULL, pkt);
3422 return -1;
3423}
3424
3425/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3426 * in advance. This is the reponsability of the caller to check there is enough room in this
3427 * buffer to build a long header.
3428 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3429 */
3430static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3431 int type, size_t pn_len, struct quic_conn *conn)
3432{
3433 if (type > QUIC_PACKET_TYPE_RETRY)
3434 return 0;
3435
3436 /* #0 byte flags */
3437 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3438 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3439 /* Version */
3440 quic_write_uint32(buf, end, conn->version);
3441 *(*buf)++ = conn->dcid.len;
3442 /* Destination connection ID */
3443 if (conn->dcid.len) {
3444 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3445 *buf += conn->dcid.len;
3446 }
3447 /* Source connection ID */
3448 *(*buf)++ = conn->scid.len;
3449 if (conn->scid.len) {
3450 memcpy(*buf, conn->scid.data, conn->scid.len);
3451 *buf += conn->scid.len;
3452 }
3453
3454 return 1;
3455}
3456
3457/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3458 * in advance. This is the reponsability of the caller to check there is enough room in this
3459 * buffer to build a long header.
3460 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3461 */
3462static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3463 size_t pn_len, struct quic_conn *conn)
3464{
3465 /* #0 byte flags */
3466 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3467 /* Destination connection ID */
3468 if (conn->dcid.len) {
3469 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3470 *buf += conn->dcid.len;
3471 }
3472
3473 return 1;
3474}
3475
3476/* Apply QUIC header protection to the packet with <buf> as first byte address,
3477 * <pn> as address of the Packet number field, <pnlen> being this field length
3478 * with <aead> as AEAD cipher and <key> as secret key.
3479 * Returns 1 if succeeded or 0 if failed.
3480 */
3481static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3482 const EVP_CIPHER *aead, const unsigned char *key)
3483{
3484 int i, ret, outlen;
3485 EVP_CIPHER_CTX *ctx;
3486 /* We need an IV of at least 5 bytes: one byte for bytes #0
3487 * and at most 4 bytes for the packet number
3488 */
3489 unsigned char mask[5] = {0};
3490
3491 ret = 0;
3492 ctx = EVP_CIPHER_CTX_new();
3493 if (!ctx)
3494 return 0;
3495
3496 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3497 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3498 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3499 goto out;
3500
3501 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3502 for (i = 0; i < pnlen; i++)
3503 pn[i] ^= mask[i + 1];
3504
3505 ret = 1;
3506
3507 out:
3508 EVP_CIPHER_CTX_free(ctx);
3509
3510 return ret;
3511}
3512
3513/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3514 * ACK ranges if needed to a value below <limit> in bytes.
3515 * Return 1 if succeeded, 0 if not.
3516 */
3517static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3518{
3519 size_t room, ack_delay_sz;
3520
3521 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3522 /* A frame is made of 1 byte for the frame type. */
3523 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003524 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003525 return 0;
3526
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003527 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003528}
3529
3530/* Prepare as most as possible CRYPTO frames from prebuilt CRYPTO frames for <qel>
3531 * encryption level to be encoded in a buffer with <room> as available room,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003532 * and <*len> the packet Length field initialized with the number of bytes already present
3533 * in this buffer which must be taken into an account for the Length packet field value.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003534 * <headlen> is the number of bytes already present in this packet before building
Frédéric Lécailleea604992020-12-24 13:01:37 +01003535 * CRYPTO frames.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003536 * This is the responsibility of the caller to check that <*len> < <room> as this is
3537 * the responsibility to check that <headlen> < quic_path_prep_data(conn->path).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003538 * Update consequently <*len> to reflect the size of these CRYPTO frames built
3539 * by this function. Also attach these CRYPTO frames to <pkt> QUIC packet.
3540 * Return 1 if succeeded, 0 if not.
3541 */
3542static inline int qc_build_cfrms(struct quic_tx_packet *pkt,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003543 size_t room, size_t *len, size_t headlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003544 struct quic_enc_level *qel,
3545 struct quic_conn *conn)
3546{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003547 int ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003548 struct quic_tx_frm *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003549
Frédéric Lécailleea604992020-12-24 13:01:37 +01003550 ret = 0;
3551 /* If we are not probing we must take into an account the congestion
3552 * control window.
3553 */
3554 if (!conn->tx.nb_pto_dgrams)
3555 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
3556 TRACE_PROTO("************** CRYPTO frames build (headlen)",
3557 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
3559 /* header length, data length, frame length. */
3560 size_t hlen, dlen, cflen;
3561
Frédéric Lécailleea604992020-12-24 13:01:37 +01003562 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3563 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3564 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003565 break;
3566
3567 /* Compute the length of this CRYPTO frame header */
3568 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3569 /* Compute the data length of this CRyPTO frame. */
3570 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécailleea604992020-12-24 13:01:37 +01003571 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3572 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003573 if (!dlen)
3574 break;
3575
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003576 pkt->cdata_len += dlen;
3577 /* CRYPTO frame length. */
3578 cflen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003579 TRACE_PROTO(" CRYPTO frame length (cflen)",
3580 QUIC_EV_CONN_BCFRMS, conn->conn, &cflen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003581 /* Add the CRYPTO data length and its encoded length to the packet
3582 * length and the length of this length.
3583 */
3584 *len += cflen;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003585 room -= cflen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003586 if (dlen == cf->crypto.len) {
3587 /* <cf> CRYPTO data have been consumed. */
Willy Tarreau2b718102021-04-21 07:32:39 +02003588 LIST_DELETE(&cf->list);
3589 LIST_APPEND(&pkt->frms, &cf->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590 }
3591 else {
3592 struct quic_tx_frm *new_cf;
3593
3594 new_cf = pool_alloc(pool_head_quic_tx_frm);
3595 if (!new_cf) {
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003596 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003597 return 0;
3598 }
3599
3600 new_cf->type = QUIC_FT_CRYPTO;
3601 new_cf->crypto.len = dlen;
3602 new_cf->crypto.offset = cf->crypto.offset;
Willy Tarreau2b718102021-04-21 07:32:39 +02003603 LIST_APPEND(&pkt->frms, &new_cf->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003604 /* Consume <dlen> bytes of the current frame. */
3605 cf->crypto.len -= dlen;
3606 cf->crypto.offset += dlen;
3607 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003608 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003609 }
3610
Frédéric Lécailleea604992020-12-24 13:01:37 +01003611 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612}
3613
3614/* This function builds a clear handshake packet used during a QUIC TLS handshakes
3615 * into <wbuf> the current <wbuf> for <conn> QUIC connection with <qel> as QUIC
3616 * TLS encryption level for outgoing packets filling it with as much as CRYPTO
3617 * data as possible from <offset> offset in the CRYPTO data stream. Note that
3618 * this offset value is updated by the length of the CRYPTO frame used to embed
3619 * the CRYPTO data if this packet and only if the packet is successfully built.
3620 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
3621 * reserved so that to be sure there is enough room to build this AEAD TAG after
3622 * having successfully returned from this function and to be sure the position
3623 * pointer of <wbuf> may be safely incremented by QUIC_TLS_TAG_LEN. After having
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003624 * returned from this function, <wbuf> position will point one past the last
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003625 * byte of the payload with the confidence there is at least QUIC_TLS_TAG_LEN bytes
3626 * available packet to encrypt this packet.
3627 * This function also update the value of <buf_pn> pointer to point to the packet
3628 * number field in this packet. <pn_len> will also have the packet number
3629 * length as value.
3630 *
3631 * Return the length of the packet if succeeded minus QUIC_TLS_TAG_LEN, or -1 if
3632 * failed (not enough room in <wbuf> to build this packet plus QUIC_TLS_TAG_LEN
3633 * bytes), -2 if there are too much CRYPTO data in flight to build a packet.
3634 */
3635static ssize_t qc_do_build_hdshk_pkt(struct q_buf *wbuf,
3636 struct quic_tx_packet *pkt, int pkt_type,
3637 int64_t pn, size_t *pn_len,
3638 unsigned char **buf_pn,
3639 struct quic_enc_level *qel,
3640 struct quic_conn *conn)
3641{
3642 unsigned char *beg, *pos;
3643 const unsigned char *end;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003644 size_t len, len_frms, token_fields_len, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003645 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3646 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3647 struct quic_crypto *crypto = &frm.crypto;
3648 size_t ack_frm_len;
3649 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003650 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003651
Frédéric Lécailleea604992020-12-24 13:01:37 +01003652 /* Length field value with CRYPTO frames if present. */
3653 len_frms = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003654 beg = pos = q_buf_getpos(wbuf);
3655 end = q_buf_end(wbuf);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003656 /* When not probing and not acking, reduce the size of this buffer to respect
3657 * the congestion controller window.
3658 */
3659 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3660 size_t path_room;
3661
3662 path_room = quic_path_prep_data(conn->path);
3663 if (end - beg > path_room)
3664 end = beg + path_room;
3665 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003666
3667 /* For a server, the token field of an Initial packet is empty. */
3668 token_fields_len = pkt_type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3669
3670 /* Check there is enough room to build the header followed by a token. */
3671 if (end - pos < QUIC_LONG_PACKET_MINLEN + conn->dcid.len +
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003672 conn->scid.len + token_fields_len + QUIC_TLS_TAG_LEN) {
3673 ssize_t room = end - pos;
3674 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3675 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003676 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003677 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003678
3679 /* Reserve enough room at the end of the packet for the AEAD TAG. */
3680 end -= QUIC_TLS_TAG_LEN;
3681 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3682 /* packet number length */
3683 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3684
3685 quic_build_packet_long_header(&pos, end, pkt_type, *pn_len, conn);
3686
3687 /* Encode the token length (0) for an Initial packet. */
3688 if (pkt_type == QUIC_PACKET_TYPE_INITIAL)
3689 *pos++ = 0;
3690
3691 /* Build an ACK frame if required. */
3692 ack_frm_len = 0;
3693 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003694 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003695 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003696 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003697 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003698 if (!ack_frm_len) {
3699 ssize_t room = end - pos;
3700 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3701 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003702 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003703 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003704
3705 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3706 }
3707
3708 /* Length field value without the CRYPTO frames data length. */
3709 len = ack_frm_len + *pn_len;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003710 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003711 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003712
3713 len_frms = len + QUIC_TLS_TAG_LEN;
3714 if (!qc_build_cfrms(pkt, end - pos, &len_frms, pos - beg, qel, conn)) {
3715 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3716 conn->conn, NULL, NULL, &room);
3717 goto err;
3718 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003719 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003720
3721 add_ping_frm = 0;
3722 padding_len = 0;
3723 if (objt_server(conn->conn->target) &&
3724 pkt_type == QUIC_PACKET_TYPE_INITIAL &&
3725 len < QUIC_INITIAL_PACKET_MINLEN) {
3726 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
3727 }
3728 else if (LIST_ISEMPTY(&pkt->frms)) {
3729 if (qel->pktns->tx.pto_probe) {
3730 /* If we cannot send a CRYPTO frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003731 add_ping_frm = 1;
3732 len += 1;
3733 }
3734 /* If there is no frame at all to follow, add at least a PADDING frame. */
3735 if (!ack_frm_len)
3736 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
3737 }
3738
3739 /* Length (of the remaining data). Must not fail because, the buffer size
3740 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
3741 * for the encryption TAG. It must be taken into an account for the length
3742 * of this packet.
3743 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01003744 if (len_frms)
3745 len = len_frms;
3746 else
3747 len += QUIC_TLS_TAG_LEN;
3748 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003749
3750 /* Packet number field address. */
3751 *buf_pn = pos;
3752
3753 /* Packet number encoding. */
3754 quic_packet_number_encode(&pos, end, pn, *pn_len);
3755
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003756 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3757 ssize_t room = end - pos;
3758 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3759 conn->conn, NULL, NULL, &room);
3760 goto err;
3761 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003762
3763 /* Crypto frame */
3764 if (!LIST_ISEMPTY(&pkt->frms)) {
3765 struct quic_tx_frm *cf;
3766
3767 list_for_each_entry(cf, &pkt->frms, list) {
3768 crypto->offset = cf->crypto.offset;
3769 crypto->len = cf->crypto.len;
3770 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003771 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3772 ssize_t room = end - pos;
3773 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3774 conn->conn, NULL, NULL, &room);
3775 goto err;
3776 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003777 }
3778 }
3779
3780 /* Build a PING frame if needed. */
3781 if (add_ping_frm) {
3782 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003783 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3784 ssize_t room = end - pos;
3785 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3786 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003787 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003788 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003789 }
3790
3791 /* Build a PADDING frame if needed. */
3792 if (padding_len) {
3793 frm.type = QUIC_FT_PADDING;
3794 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003795 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3796 ssize_t room = end - pos;
3797 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3798 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003799 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003800 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003801 }
3802
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003803 /* Always reset this variable as this function has no idea
3804 * if it was set. It is handle by the loss detection timer.
3805 */
3806 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003807
3808 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003809 return pos - beg;
3810
3811 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003812 return -1;
3813}
3814
3815static inline void quic_tx_packet_init(struct quic_tx_packet *pkt)
3816{
3817 pkt->cdata_len = 0;
3818 pkt->in_flight_len = 0;
3819 LIST_INIT(&pkt->frms);
3820}
3821
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003822/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003823static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
3824{
3825 struct quic_tx_frm *frm, *frmbak;
3826
3827 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003828 LIST_DELETE(&frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003829 pool_free(pool_head_quic_tx_frm, frm);
3830 }
3831 pool_free(pool_head_quic_tx_packet, pkt);
3832}
3833
3834/* Build a handshake packet into <buf> packet buffer with <pkt_type> as packet
3835 * type for <qc> QUIC connection from CRYPTO data stream at <*offset> offset to
3836 * be encrypted at <qel> encryption level.
3837 * Return -2 if the packet could not be encrypted for any reason, -1 if there was
3838 * not enough room in <buf> to build the packet, or the size of the built packet
3839 * if succeeded (may be zero if there is too much crypto data in flight to build the packet).
3840 */
3841static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type,
3842 struct quic_enc_level *qel)
3843{
3844 /* The pointer to the packet number field. */
3845 unsigned char *buf_pn;
3846 unsigned char *beg, *end, *payload;
3847 int64_t pn;
3848 size_t pn_len, payload_len, aad_len;
3849 ssize_t pkt_len;
3850 struct quic_tls_ctx *tls_ctx;
3851 struct quic_tx_packet *pkt;
3852
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003853 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003854 pkt = pool_alloc(pool_head_quic_tx_packet);
3855 if (!pkt) {
3856 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn);
3857 return -2;
3858 }
3859
3860 quic_tx_packet_init(pkt);
3861 beg = q_buf_getpos(buf);
3862 pn_len = 0;
3863 buf_pn = NULL;
3864 pn = qel->pktns->tx.next_pn + 1;
3865 pkt_len = qc_do_build_hdshk_pkt(buf, pkt, pkt_type, pn, &pn_len, &buf_pn, qel, qc);
3866 if (pkt_len <= 0) {
3867 free_quic_tx_packet(pkt);
3868 return pkt_len;
3869 }
3870
3871 end = beg + pkt_len;
3872 payload = buf_pn + pn_len;
3873 payload_len = end - payload;
3874 aad_len = payload - beg;
3875
3876 tls_ctx = &qel->tls_ctx;
3877 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn))
3878 goto err;
3879
3880 end += QUIC_TLS_TAG_LEN;
3881 pkt_len += QUIC_TLS_TAG_LEN;
3882 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
3883 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
3884 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
3885 goto err;
3886 }
3887
3888 /* Now that a correct packet is built, let us set the position pointer of
3889 * <buf> buffer for the next packet.
3890 */
3891 q_buf_setpos(buf, end);
3892 /* Consume a packet number. */
3893 ++qel->pktns->tx.next_pn;
3894 /* Attach the built packet to its tree. */
3895 pkt->pn_node.key = qel->pktns->tx.next_pn;
3896 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003897 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003898 pkt->in_flight_len = pkt_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003899 qc->path->prep_in_flight += pkt_len;
3900 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003901 pkt->pktns = qel->pktns;
3902 eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node);
3903 /* Increment the number of bytes in <buf> buffer by the length of this packet. */
3904 buf->data += pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003905 /* Attach this packet to <buf>. */
Willy Tarreau2b718102021-04-21 07:32:39 +02003906 LIST_APPEND(&buf->pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003907 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
3908
3909 return pkt_len;
3910
3911 err:
3912 free_quic_tx_packet(pkt);
3913 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
3914 return -2;
3915}
3916
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003917/* Prepare a clear post handhskake packet for <conn> QUIC connection.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003918 * Return the length of this packet if succeeded, -1 <wbuf> was full.
3919 */
3920static ssize_t qc_do_build_phdshk_apkt(struct q_buf *wbuf,
3921 struct quic_tx_packet *pkt,
3922 int64_t pn, size_t *pn_len,
3923 unsigned char **buf_pn, struct quic_enc_level *qel,
3924 struct quic_conn *conn)
3925{
3926 const unsigned char *beg, *end;
3927 unsigned char *pos;
3928 struct quic_frame *frm, *sfrm;
3929 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3930 size_t fake_len, ack_frm_len;
3931 int64_t largest_acked_pn;
3932
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003933 TRACE_ENTER(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003934 beg = pos = q_buf_getpos(wbuf);
3935 end = q_buf_end(wbuf);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003936 /* When not probing and not acking, reduce the size of this buffer to respect
3937 * the congestion controller window.
3938 */
3939 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3940 size_t path_room;
3941
3942 path_room = quic_path_prep_data(conn->path);
3943 if (end - beg > path_room)
3944 end = beg + path_room;
3945 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003946 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3947 /* Packet number length */
3948 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3949 /* Check there is enough room to build this packet (without payload). */
3950 if (end - pos < QUIC_SHORT_PACKET_MINLEN + sizeof_quic_cid(&conn->dcid) +
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003951 *pn_len + QUIC_TLS_TAG_LEN) {
3952 ssize_t room = end - pos;
3953 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3954 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003955 goto err;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003956 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003957
3958 /* Reserve enough room at the end of the packet for the AEAD TAG. */
3959 end -= QUIC_TLS_TAG_LEN;
3960 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3961 /* Packet number field. */
3962 *buf_pn = pos;
3963 /* Packet number encoding. */
3964 quic_packet_number_encode(&pos, end, pn, *pn_len);
3965
3966 /* Build an ACK frame if required. */
3967 ack_frm_len = 0;
3968 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003969 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003970 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003971 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003972 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
3973 if (!ack_frm_len)
3974 goto err;
3975
3976 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3977 }
3978
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003979 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3980 ssize_t room = end - pos;
3981 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3982 conn->conn, NULL, NULL, &room);
3983 goto err;
3984 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003985
3986 fake_len = ack_frm_len;
3987 if (!LIST_ISEMPTY(&qel->pktns->tx.frms) &&
Frédéric Lécailleea604992020-12-24 13:01:37 +01003988 !qc_build_cfrms(pkt, end - pos, &fake_len, pos - beg, qel, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003989 ssize_t room = end - pos;
3990 TRACE_PROTO("some CRYPTO frames could not be built",
3991 QUIC_EV_CONN_PAPKT, conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003992 goto err;
3993 }
3994
3995 /* Crypto frame */
3996 if (!LIST_ISEMPTY(&pkt->frms)) {
3997 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3998 struct quic_crypto *crypto = &frm.crypto;
3999 struct quic_tx_frm *cf;
4000
4001 list_for_each_entry(cf, &pkt->frms, list) {
4002 crypto->offset = cf->crypto.offset;
4003 crypto->len = cf->crypto.len;
4004 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004005 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4006 ssize_t room = end - pos;
4007 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
4008 conn->conn, NULL, NULL, &room);
4009 goto err;
4010 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004011 }
4012 }
4013
4014 /* Encode a maximum of frames. */
4015 list_for_each_entry_safe(frm, sfrm, &conn->tx.frms_to_send, list) {
4016 unsigned char *ppos;
4017
4018 ppos = pos;
4019 if (!qc_build_frm(&ppos, end, frm, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004020 TRACE_DEVEL("Frames not built", QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004021 break;
4022 }
4023
Willy Tarreau2b718102021-04-21 07:32:39 +02004024 LIST_DELETE(&frm->list);
4025 LIST_APPEND(&pkt->frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004026 pos = ppos;
4027 }
4028
4029 out:
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004030 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004031 return pos - beg;
4032
4033 err:
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004034 TRACE_DEVEL("leaving in error (buffer full)", QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004035 return -1;
4036}
4037
4038/* Prepare a post handhskake packet at Application encryption level for <conn>
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004039 * QUIC connection.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004040 * Return the length of this packet if succeeded, -1 if <wbuf> was full,
4041 * -2 in case of major error (encryption failure).
4042 */
4043static ssize_t qc_build_phdshk_apkt(struct q_buf *wbuf, struct quic_conn *qc)
4044{
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004045 /* A pointer to the packet number field in <buf> */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004046 unsigned char *buf_pn;
4047 unsigned char *beg, *end, *payload;
4048 int64_t pn;
4049 size_t pn_len, aad_len, payload_len;
4050 ssize_t pkt_len;
4051 struct quic_tls_ctx *tls_ctx;
4052 struct quic_enc_level *qel;
4053 struct quic_tx_packet *pkt;
4054
4055 TRACE_ENTER(QUIC_EV_CONN_PAPKT, qc->conn);
4056 pkt = pool_alloc(pool_head_quic_tx_packet);
4057 if (!pkt) {
4058 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_PAPKT, qc->conn);
4059 return -2;
4060 }
4061
4062 quic_tx_packet_init(pkt);
4063 beg = q_buf_getpos(wbuf);
4064 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4065 pn_len = 0;
4066 buf_pn = NULL;
4067 pn = qel->pktns->tx.next_pn + 1;
4068 pkt_len = qc_do_build_phdshk_apkt(wbuf, pkt, pn, &pn_len, &buf_pn, qel, qc);
4069 if (pkt_len <= 0) {
4070 free_quic_tx_packet(pkt);
4071 return pkt_len;
4072 }
4073
4074 end = beg + pkt_len;
4075 payload = buf_pn + pn_len;
4076 payload_len = end - payload;
4077 aad_len = payload - beg;
4078
4079 tls_ctx = &qel->tls_ctx;
4080 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn))
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004081 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004082
4083 end += QUIC_TLS_TAG_LEN;
4084 pkt_len += QUIC_TLS_TAG_LEN;
4085 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4086 tls_ctx->tx.hp, tls_ctx->tx.hp_key))
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004087 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004088
4089 q_buf_setpos(wbuf, end);
4090 /* Consume a packet number. */
4091 ++qel->pktns->tx.next_pn;
4092 /* Attach the built packet to its tree. */
4093 pkt->pn_node.key = qel->pktns->tx.next_pn;
4094 eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node);
4095 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004096 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004097 pkt->in_flight_len = pkt_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004098 qc->path->prep_in_flight += pkt_len;
4099 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004100 pkt->pktns = qel->pktns;
4101 /* Increment the number of bytes in <buf> buffer by the length of this packet. */
4102 wbuf->data += pkt_len;
4103 /* Attach this packet to <buf>. */
Willy Tarreau2b718102021-04-21 07:32:39 +02004104 LIST_APPEND(&wbuf->pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004105
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004106 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, qc->conn, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004107
4108 return pkt_len;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004109
4110 err:
4111 free_quic_tx_packet(pkt);
4112 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PAPKT, qc->conn);
4113 return -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004114}
4115
4116/* Prepare a maximum of QUIC Application level packets from <ctx> QUIC
4117 * connection I/O handler context.
4118 * Returns 1 if succeeded, 0 if not.
4119 */
4120int qc_prep_phdshk_pkts(struct quic_conn *qc)
4121{
4122 struct q_buf *wbuf;
4123 struct quic_enc_level *qel;
4124
4125 TRACE_ENTER(QUIC_EV_CONN_PAPKTS, qc->conn);
4126 wbuf = q_wbuf(qc);
4127 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4128 while (q_buf_empty(wbuf)) {
4129 ssize_t ret;
4130
4131 if (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
4132 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004133 qc->path->prep_in_flight >= qc->path->cwnd)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004134 TRACE_DEVEL("nothing more to do",
4135 QUIC_EV_CONN_PAPKTS, qc->conn);
4136 break;
4137 }
4138
4139 ret = qc_build_phdshk_apkt(wbuf, qc);
4140 switch (ret) {
4141 case -1:
4142 /* Not enough room left in <wbuf>. */
4143 wbuf = q_next_wbuf(qc);
4144 continue;
4145 case -2:
4146 return 0;
4147 default:
4148 /* XXX TO CHECK: consume a buffer. */
4149 wbuf = q_next_wbuf(qc);
4150 continue;
4151 }
4152 }
4153 TRACE_LEAVE(QUIC_EV_CONN_PAPKTS, qc->conn);
4154
4155 return 1;
4156}
4157
4158/* QUIC connection packet handler task. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004159struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004160{
4161 struct quic_conn_ctx *ctx = context;
4162
4163 if (ctx->state < QUIC_HS_ST_COMPLETE) {
4164 qc_do_hdshk(ctx);
4165 }
4166 else {
4167 struct quic_conn *qc = ctx->conn->qc;
4168
4169 /* XXX TO DO: may fail!!! XXX */
4170 qc_treat_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_APP], ctx);
4171 qc_prep_phdshk_pkts(qc);
4172 qc_send_ppkts(ctx);
4173 }
4174
Willy Tarreau74163142021-03-13 11:30:19 +01004175 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004176}
4177
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004178/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4179 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004180 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004181static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4182 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004183{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004184 size_t try, done = 0;
4185
4186 if (!conn_ctrl_ready(conn))
4187 return 0;
4188
4189 if (!fd_recv_ready(conn->handle.fd))
4190 return 0;
4191
4192 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004193
4194 /* read the largest possible block. For this, we perform only one call
4195 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004196 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004197 */
4198 while (count > 0) {
4199 try = b_contig_space(buf);
4200 if (!try)
4201 break;
4202
4203 if (try > count)
4204 try = count;
4205
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004206 b_add(buf, try);
4207 done += try;
4208 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004209 }
4210
4211 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4212 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4213
4214 leave:
4215 return done;
4216
4217 read0:
4218 conn_sock_read0(conn);
4219 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4220
4221 /* Now a final check for a possible asynchronous low-level error
4222 * report. This can happen when a connection receives a reset
4223 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4224 * we might have come from there by just checking POLL_HUP instead
4225 * of recv()'s return value 0, so we have no way to tell there was
4226 * an error without checking.
4227 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004228 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004229 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4230 goto leave;
4231}
4232
4233
4234/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4235 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4236 * other pending data for example, but this flag is ignored at the moment.
4237 * Only one call to send() is performed, unless the buffer wraps, in which case
4238 * a second call may be performed. The connection's flags are updated with
4239 * whatever special event is detected (error, empty). The caller is responsible
4240 * for taking care of those events and avoiding the call if inappropriate. The
4241 * function does not call the connection's polling update function, so the caller
4242 * is responsible for this. It's up to the caller to update the buffer's contents
4243 * based on the return value.
4244 */
4245static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4246{
4247 ssize_t ret;
4248 size_t try, done;
4249 int send_flag;
4250
4251 if (!conn_ctrl_ready(conn))
4252 return 0;
4253
4254 if (!fd_send_ready(conn->handle.fd))
4255 return 0;
4256
4257 done = 0;
4258 /* send the largest possible block. For this we perform only one call
4259 * to send() unless the buffer wraps and we exactly fill the first hunk,
4260 * in which case we accept to do it once again.
4261 */
4262 while (count) {
4263 try = b_contig_data(buf, done);
4264 if (try > count)
4265 try = count;
4266
4267 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4268 if (try < count || flags & CO_SFL_MSG_MORE)
4269 send_flag |= MSG_MORE;
4270
4271 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4272 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4273 if (ret > 0) {
4274 count -= ret;
4275 done += ret;
4276
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004277 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004278 conn->flags |= CO_FL_WAIT_L4L6;
4279 /* if the system buffer is full, don't insist */
4280 if (ret < try)
4281 break;
4282 }
4283 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4284 /* nothing written, we need to poll for write first */
4285 fd_cant_send(conn->handle.fd);
4286 break;
4287 }
4288 else if (errno != EINTR) {
4289 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4290 break;
4291 }
4292 }
4293 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4294 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4295
4296 if (done > 0) {
4297 /* we count the total bytes sent, and the send rate for 32-byte
4298 * blocks. The reason for the latter is that freq_ctr are
4299 * limited to 4GB and that it's not enough per second.
4300 */
4301 _HA_ATOMIC_ADD(&global.out_bytes, done);
4302 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4303 }
4304 return done;
4305}
4306
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004307/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4308 * event subscriber <es> is not allowed to change from a previous call as long
4309 * as at least one event is still subscribed. The <event_type> must only be a
4310 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4311 */
4312static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4313{
4314 return conn_subscribe(conn, xprt_ctx, event_type, es);
4315}
4316
4317/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4318 * The <es> pointer is not allowed to differ from the one passed to the
4319 * subscribe() call. It always returns zero.
4320 */
4321static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4322{
4323 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4324}
4325
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004326/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4327 * connection with <xprt_ctx> as address of the xprt context.
4328 * Returns 1 if succeeded, 0 if not.
4329 */
4330static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4331{
4332 struct quic_conn_ctx *ctx;
4333
4334 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4335
4336 if (*xprt_ctx)
4337 goto out;
4338
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004339 ctx = pool_alloc(pool_head_quic_conn_ctx);
4340 if (!ctx) {
4341 conn->err_code = CO_ER_SYS_MEMLIM;
4342 goto err;
4343 }
4344
4345 ctx->wait_event.tasklet = tasklet_new();
4346 if (!ctx->wait_event.tasklet) {
4347 conn->err_code = CO_ER_SYS_MEMLIM;
4348 goto err;
4349 }
4350
4351 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4352 ctx->wait_event.tasklet->context = ctx;
4353 ctx->wait_event.events = 0;
4354 ctx->conn = conn;
4355 ctx->subs = NULL;
4356 ctx->xprt_ctx = NULL;
4357
4358 ctx->xprt = xprt_get(XPRT_QUIC);
4359 if (objt_server(conn->target)) {
4360 /* Server */
4361 struct server *srv = __objt_server(conn->target);
4362 unsigned char dcid[QUIC_CID_LEN];
4363 struct quic_conn *quic_conn;
4364 int ssl_err, ipv4;
4365
4366 ssl_err = SSL_ERROR_NONE;
4367 if (RAND_bytes(dcid, sizeof dcid) != 1)
4368 goto err;
4369
4370 conn->qc = new_quic_conn(QUIC_PROTOCOL_VERSION_DRAFT_28);
4371 if (!conn->qc)
4372 goto err;
4373
4374 quic_conn = conn->qc;
4375 quic_conn->conn = conn;
4376 ipv4 = conn->dst->ss_family == AF_INET;
4377 if (!qc_new_conn_init(quic_conn, ipv4, NULL, &srv->cids,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004378 dcid, sizeof dcid, NULL, 0, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004379 goto err;
4380
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004381 if (!qc_new_isecs(quic_conn, dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004382 goto err;
4383
4384 ctx->state = QUIC_HS_ST_CLIENT_INITIAL;
4385 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4386 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4387 goto err;
4388
Frédéric Lécaille5aa41432021-01-28 16:22:52 +01004389 quic_conn->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004390 /* Copy the initial source connection ID. */
Frédéric Lécaille5aa41432021-01-28 16:22:52 +01004391 quic_cid_cpy(&quic_conn->rx.params.initial_source_connection_id, &quic_conn->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004392 quic_conn->enc_params_len =
4393 quic_transport_params_encode(quic_conn->enc_params,
4394 quic_conn->enc_params + sizeof quic_conn->enc_params,
Frédéric Lécaille5aa41432021-01-28 16:22:52 +01004395 &quic_conn->rx.params, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004396 if (!quic_conn->enc_params_len)
4397 goto err;
4398
4399 SSL_set_quic_transport_params(ctx->ssl, quic_conn->enc_params, quic_conn->enc_params_len);
4400 SSL_set_connect_state(ctx->ssl);
4401 ssl_err = SSL_do_handshake(ctx->ssl);
4402 if (ssl_err != 1) {
4403 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4404 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
4405 TRACE_PROTO("SSL handshake",
4406 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
4407 }
4408 else {
4409 TRACE_DEVEL("SSL handshake error",
4410 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
4411 goto err;
4412 }
4413 }
4414 }
4415 else if (objt_listener(conn->target)) {
4416 /* Listener */
4417 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004418 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004419
4420 ctx->state = QUIC_HS_ST_SERVER_INITIAL;
4421
4422 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4423 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4424 goto err;
4425
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004426 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004427 SSL_set_accept_state(ctx->ssl);
4428 }
4429
4430 *xprt_ctx = ctx;
4431
4432 /* Leave init state and start handshake */
4433 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004434
4435 out:
4436 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4437
4438 return 0;
4439
4440 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004441 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004442 tasklet_free(ctx->wait_event.tasklet);
4443 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004444 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004445 return -1;
4446}
4447
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004448/* Start the QUIC transport layer */
4449static int qc_xprt_start(struct connection *conn, void *ctx)
4450{
4451 struct quic_conn *qc;
4452 struct quic_conn_ctx *qctx = ctx;
4453
4454 qc = conn->qc;
4455 if (!quic_conn_init_timer(qc)) {
4456 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4457 return 0;
4458 }
4459
4460 tasklet_wakeup(qctx->wait_event.tasklet);
4461 return 1;
4462}
4463
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004464/* transport-layer operations for QUIC connections. */
4465static struct xprt_ops ssl_quic = {
4466 .snd_buf = quic_conn_from_buf,
4467 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004468 .subscribe = quic_conn_subscribe,
4469 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004470 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004471 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004472 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4473 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4474 .name = "QUIC",
4475};
4476
4477__attribute__((constructor))
4478static void __quic_conn_init(void)
4479{
4480 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4481 xprt_register(XPRT_QUIC, &ssl_quic);
4482}
4483
4484__attribute__((destructor))
4485static void __quic_conn_deinit(void)
4486{
4487 BIO_meth_free(ha_quic_meth);
4488}
4489
4490/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4491 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4492 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004493 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004494 */
4495static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4496 struct sockaddr_storage *saddr, qpkt_read_func *func)
4497{
4498 unsigned char *pos;
4499 const unsigned char *end;
4500 struct quic_dgram_ctx dgram_ctx = {
4501 .dcid_node = NULL,
4502 .owner = owner,
4503 };
4504
4505 pos = (unsigned char *)buf;
4506 end = pos + len;
4507
4508 do {
4509 int ret;
4510 struct quic_rx_packet *pkt;
4511
Willy Tarreaue4498932021-03-22 21:13:05 +01004512 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004513 if (!pkt)
4514 goto err;
4515
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004516 quic_rx_packet_refinc(pkt);
4517 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4518 if (ret == -1) {
4519 size_t pkt_len;
4520
4521 pkt_len = pkt->len;
4522 free_quic_rx_packet(pkt);
4523 /* If the packet length could not be found, we cannot continue. */
4524 if (!pkt_len)
4525 break;
4526 }
4527 } while (pos < end);
4528
4529 /* Increasing the received bytes counter by the UDP datagram length
4530 * if this datagram could be associated to a connection.
4531 */
4532 if (dgram_ctx.qc)
4533 dgram_ctx.qc->rx.bytes += len;
4534
4535 return pos - (unsigned char *)buf;
4536
4537 err:
4538 return -1;
4539}
4540
4541ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4542 struct sockaddr_storage *saddr)
4543{
4544 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4545}
4546
4547ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4548 struct sockaddr_storage *saddr)
4549{
4550 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4551}
4552
4553/* QUIC I/O handler for connection to local listeners or remove servers
4554 * depending on <listener> boolean value, with <fd> as socket file
4555 * descriptor and <ctx> as context.
4556 */
4557static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4558{
4559 ssize_t ret;
4560 size_t done = 0;
4561 struct buffer *buf = get_trash_chunk();
4562 /* Source address */
4563 struct sockaddr_storage saddr = {0};
4564 socklen_t saddrlen = sizeof saddr;
4565
4566 if (!fd_recv_ready(fd))
4567 return 0;
4568
4569 do {
4570 ret = recvfrom(fd, buf->area, buf->size, 0,
4571 (struct sockaddr *)&saddr, &saddrlen);
4572 if (ret < 0) {
4573 if (errno == EINTR)
4574 continue;
4575 if (errno == EAGAIN)
4576 fd_cant_recv(fd);
4577 goto out;
4578 }
4579 } while (0);
4580
4581 done = buf->data = ret;
4582 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4583
4584 out:
4585 return done;
4586}
4587
4588/* QUIC I/O handler for connections to local listeners with <fd> as socket
4589 * file descriptor.
4590 */
4591void quic_fd_handler(int fd)
4592{
Willy Tarreauf5090652021-04-06 17:23:40 +02004593 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004594 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4595}
4596
4597/* QUIC I/O handler for connections to remote servers with <fd> as socket
4598 * file descriptor.
4599 */
4600void quic_conn_fd_handler(int fd)
4601{
Willy Tarreauf5090652021-04-06 17:23:40 +02004602 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004603 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4604}
4605
4606/*
4607 * Local variables:
4608 * c-indent-level: 8
4609 * c-basic-offset: 8
4610 * End:
4611 */